Skip to content

Commit e405ff3

Browse files
committed
CurrentRanger_R3: add preference to timestamp samples
For USB and BT serial data output add options to preface each sample with the system time at which it was taken, in microseconds. This mitigates the loss of information when the sample rate is changed dynamically. Be aware that the counter will wrap at 2^32 us, so relative calculations should be done using unsigned 32-bit operations. Signed-off-by: Peter A. Bigot <pab@pabigot.com>
1 parent 3458059 commit e405ff3

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

CurrentRanger_R3.ino

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ Adafruit_FreeTouch qt[3] = {
120120
#define LOGGING_FORMAT_ADC 4 //raw output for each range (0..4095)
121121
//***********************************************************************************************************
122122
#define PREFERENCE_MU_SAMPLERATE 0x01 // default BIAS
123+
#define PREFERENCE_USB_TIMESTAMP 0x02 // timestamp serial samples
124+
#define PREFERENCE_BT_TIMESTAMP 0x04 // timestamp bluetooth samples
123125
//***********************************************************************************************************
124126
static const char *const loggingFormat_str[] = {
125127
[LOGGING_FORMAT_EXPONENT] = "exp (1.23E-3 = 123 mA)",
@@ -304,6 +306,7 @@ uint32_t oledInterval=0, lpfInterval=0, offsetInterval=0, autorangeInterval=0, b
304306
byte LPF=0, BIAS=0, AUTORANGE=0, SRADJUST=0;
305307
byte readVbatLoop=0;
306308
float vbat=0, VOUT=0;
309+
unsigned long readMicros;
307310
float read1=0,read2=0,readDiff=0;
308311
bool rangeSwitched=false;
309312
#define RANGE_MA rangeUnit=='m'
@@ -470,6 +473,14 @@ void loop()
470473
USB_LOGGING_ENABLED =! USB_LOGGING_ENABLED;
471474
Serial.println(USB_LOGGING_ENABLED ? "USB_LOGGING_ENABLED" : "USB_LOGGING_DISABLED");
472475
break;
476+
case 'U': //toggle USB timestamping
477+
PREFERENCES ^= PREFERENCE_USB_TIMESTAMP;
478+
eeprom_PREFERENCES.write(PREFERENCES);
479+
if (!(PREFERENCES & PREFERENCE_USB_TIMESTAMP)) {
480+
Serial.print("NO ");
481+
}
482+
Serial.println("USB sample timestamps");
483+
break;
473484
case 't': //toggle touchpad serial output debug info
474485
TOUCH_DEBUG_ENABLED =! TOUCH_DEBUG_ENABLED;
475486
Serial.println(TOUCH_DEBUG_ENABLED ? "TOUCH_DEBUG_ENABLED" : "TOUCH_DEBUG_DISABLED");
@@ -487,6 +498,14 @@ void loop()
487498
BT_LOGGING_ENABLED =! BT_LOGGING_ENABLED;
488499
Serial.println(BT_LOGGING_ENABLED ? "BT_LOGGING_ENABLED" : "BT_LOGGING_DISABLED");
489500
break;
501+
case 'B': //toggle BT timestamping
502+
PREFERENCES ^= PREFERENCE_BT_TIMESTAMP;
503+
eeprom_PREFERENCES.write(PREFERENCES);
504+
if (!(PREFERENCES & PREFERENCE_BT_TIMESTAMP)) {
505+
Serial.print("NO ");
506+
}
507+
Serial.println("BT sample timestamps");
508+
break;
490509
case 'f': //cycle through output logging formats
491510
if (++LOGGING_FORMAT>LOGGING_FORMAT_ADC) LOGGING_FORMAT=LOGGING_FORMAT_EXPONENT;
492511
eeprom_LOGGINGFORMAT.write(LOGGING_FORMAT);
@@ -541,6 +560,9 @@ void loop()
541560
if (!AUTORANGE) readVOUT();
542561
VOUT = readDiff*ldoOptimized*(BIAS?1:OUTPUT_CALIB_FACTOR);
543562
VOUTCalculated=true;
563+
if (PREFERENCES & PREFERENCE_USB_TIMESTAMP) {
564+
Serial.print(readMicros); Serial.print(" ");
565+
}
544566
if(LOGGING_FORMAT == LOGGING_FORMAT_EXPONENT) { Serial.print(VOUT); Serial.print("e"); Serial.println(RANGE_NA ? -9 : RANGE_UA ? -6 : -3); } else
545567
if(LOGGING_FORMAT == LOGGING_FORMAT_NANOS) Serial.println(VOUT * (RANGE_NA ? 1 : RANGE_UA ? 1000 : 1000000)); else
546568
if(LOGGING_FORMAT == LOGGING_FORMAT_MICROS) Serial.println(VOUT * (RANGE_NA ? 0.001 : RANGE_UA ? 1 : 1000)); else
@@ -557,6 +579,9 @@ void loop()
557579
VOUT = readDiff*ldoOptimized*(BIAS?1:OUTPUT_CALIB_FACTOR);
558580
VOUTCalculated=true;
559581
}
582+
if (PREFERENCES & PREFERENCE_BT_TIMESTAMP) {
583+
SerialBT.print(readMicros); SerialBT.print(" ");
584+
}
560585
if(LOGGING_FORMAT == LOGGING_FORMAT_EXPONENT) { SerialBT.print(VOUT); SerialBT.print("e"); SerialBT.println(RANGE_NA ? -9 : RANGE_UA ? -6 : -3); } else
561586
if(LOGGING_FORMAT == LOGGING_FORMAT_NANOS) SerialBT.println(VOUT * (RANGE_NA ? 1 : RANGE_UA ? 1000 : 1000000)); else
562587
if(LOGGING_FORMAT == LOGGING_FORMAT_MICROS) SerialBT.println(VOUT * (RANGE_NA ? 0.001 : RANGE_UA ? 1 : 1000)); else
@@ -807,6 +832,7 @@ void setupADC() {
807832

808833
int adcRead(byte ADCpin) { return (int)analogRead(ADCpin); }
809834
void readVOUT() {
835+
readMicros = micros();
810836
readDiff = adcRead(SENSE_OUTPUT) - adcRead(SENSE_GNDISO);
811837

812838
if (!analog_ref_half && readDiff > RANGE_SWITCH_THRESHOLD_LOW && readDiff < RANGE_SWITCH_THRESHOLD_HIGH/3)
@@ -888,16 +914,24 @@ void printCalibInfo() {
888914
Serial.println((PREFERENCES & PREFERENCE_MU_SAMPLERATE)
889915
? "toggle samplerate adjust"
890916
: "toggle offset enable");
917+
Serial.print("USB timestamp: ");
918+
Serial.println((PREFERENCES & PREFERENCE_USB_TIMESTAMP)
919+
? "on" : "off");
920+
Serial.print("BT timestamp: ");
921+
Serial.println((PREFERENCES & PREFERENCE_BT_TIMESTAMP)
922+
? "on" : "off");
891923
}
892924
void printSerialMenu() {
893925
Serial.println("\r\nUSB serial commands:");
894926
Serial.println("a = toggle Auto-Off function");
895927
Serial.print ("b = toggle BT/serial logging (");Serial.print(SERIAL_UART_BAUD);Serial.println("baud)");
928+
Serial.println("B = toggle timestamp on BT/serial output [PREF]");
896929
Serial.println("f = cycle serial logging formats (exponent,nA,uA,mA/raw-ADC) [PREF]");
897930
Serial.println("g = toggle GPIO range indication (SCK=mA,MISO=uA,MOSI=nA)");
898931
Serial.println("o = toggle touchpad offset (bias) / samplerate selection [PREF]");
899932
Serial.println("t = toggle touchpad serial output debug info");
900933
Serial.println("u = toggle USB/serial logging");
934+
Serial.println("U = toggle timestamp in USB/serial output [PREF]");
901935
Serial.println("+ = increase mode value");
902936
Serial.println("- = decrease mode value");
903937
Serial.println("? = Print this menu and calib info");

0 commit comments

Comments
 (0)