Skip to content

Commit 6582dc4

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 6daf76c commit 6582dc4

1 file changed

Lines changed: 34 additions & 0 deletions

File tree

CurrentRanger_R3.ino

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ Adafruit_FreeTouch qt[3] = {
114114
#define LOGGING_FORMAT_ADC 4 //raw output for each range (0..4095)
115115
//***********************************************************************************************************
116116
#define PREFERENCE_MU_SAMPLERATE 0x01 // default BIAS
117+
#define PREFERENCE_USB_TIMESTAMP 0x02 // timestamp serial samples
118+
#define PREFERENCE_BT_TIMESTAMP 0x04 // timestamp bluetooth samples
117119
//***********************************************************************************************************
118120
static const char *const loggingFormat_str[] = {
119121
[LOGGING_FORMAT_EXPONENT] = "exp (1.23E-3 = 123 mA)",
@@ -298,6 +300,7 @@ uint32_t oledInterval=0, lpfInterval=0, modeInterval=0, autorangeInterval=0, btI
298300
byte LPF=0, BIAS=0, AUTORANGE=0, SRADJUST=0;
299301
byte readVbatLoop=0;
300302
float vbat=0, VOUT=0;
303+
unsigned long readMicros;
301304
float read1=0,read2=0,readDiff=0;
302305
bool rangeSwitched=false;
303306
#define RANGE_MA rangeUnit=='m'
@@ -464,6 +467,14 @@ void loop()
464467
USB_LOGGING_ENABLED =! USB_LOGGING_ENABLED;
465468
Serial.println(USB_LOGGING_ENABLED ? "USB_LOGGING_ENABLED" : "USB_LOGGING_DISABLED");
466469
break;
470+
case 'U': //toggle USB timestamping
471+
PREFERENCES ^= PREFERENCE_USB_TIMESTAMP;
472+
eeprom_PREFERENCES.write(PREFERENCES);
473+
if (!(PREFERENCES & PREFERENCE_USB_TIMESTAMP)) {
474+
Serial.print("NO ");
475+
}
476+
Serial.println("USB sample timestamps");
477+
break;
467478
case 't': //toggle touchpad serial output debug info
468479
TOUCH_DEBUG_ENABLED =! TOUCH_DEBUG_ENABLED;
469480
Serial.println(TOUCH_DEBUG_ENABLED ? "TOUCH_DEBUG_ENABLED" : "TOUCH_DEBUG_DISABLED");
@@ -481,6 +492,14 @@ void loop()
481492
BT_LOGGING_ENABLED =! BT_LOGGING_ENABLED;
482493
Serial.println(BT_LOGGING_ENABLED ? "BT_LOGGING_ENABLED" : "BT_LOGGING_DISABLED");
483494
break;
495+
case 'B': //toggle BT timestamping
496+
PREFERENCES ^= PREFERENCE_BT_TIMESTAMP;
497+
eeprom_PREFERENCES.write(PREFERENCES);
498+
if (!(PREFERENCES & PREFERENCE_BT_TIMESTAMP)) {
499+
Serial.print("NO ");
500+
}
501+
Serial.println("BT sample timestamps");
502+
break;
484503
case 'f': //cycle through output logging formats
485504
if (++LOGGING_FORMAT>LOGGING_FORMAT_ADC) LOGGING_FORMAT=LOGGING_FORMAT_EXPONENT;
486505
eeprom_LOGGINGFORMAT.write(LOGGING_FORMAT);
@@ -535,6 +554,9 @@ void loop()
535554
if (!AUTORANGE) readVOUT();
536555
VOUT = readDiff*ldoOptimized*(BIAS?1:OUTPUT_CALIB_FACTOR);
537556
VOUTCalculated=true;
557+
if (PREFERENCES & PREFERENCE_USB_TIMESTAMP) {
558+
Serial.print(readMicros); Serial.print(" ");
559+
}
538560
if(LOGGING_FORMAT == LOGGING_FORMAT_EXPONENT) { Serial.print(VOUT); Serial.print("e"); Serial.println(RANGE_NA ? -9 : RANGE_UA ? -6 : -3); } else
539561
if(LOGGING_FORMAT == LOGGING_FORMAT_NANOS) Serial.println(VOUT * (RANGE_NA ? 1 : RANGE_UA ? 1000 : 1000000)); else
540562
if(LOGGING_FORMAT == LOGGING_FORMAT_MICROS) Serial.println(VOUT * (RANGE_NA ? 0.001 : RANGE_UA ? 1 : 1000)); else
@@ -556,6 +578,9 @@ void loop()
556578
VOUT = readDiff*ldoOptimized*(BIAS?1:OUTPUT_CALIB_FACTOR);
557579
VOUTCalculated=true;
558580
}
581+
if (PREFERENCES & PREFERENCE_BT_TIMESTAMP) {
582+
SerialBT.print(readMicros); SerialBT.print(" ");
583+
}
559584
if(LOGGING_FORMAT == LOGGING_FORMAT_EXPONENT) { SerialBT.print(VOUT); SerialBT.print("e"); SerialBT.println(RANGE_NA ? -9 : RANGE_UA ? -6 : -3); } else
560585
if(LOGGING_FORMAT == LOGGING_FORMAT_NANOS) SerialBT.println(VOUT * (RANGE_NA ? 1 : RANGE_UA ? 1000 : 1000000)); else
561586
if(LOGGING_FORMAT == LOGGING_FORMAT_MICROS) SerialBT.println(VOUT * (RANGE_NA ? 0.001 : RANGE_UA ? 1 : 1000)); else
@@ -822,6 +847,7 @@ void setupADC() {
822847

823848
int adcRead(byte ADCpin) { return (int)analogRead(ADCpin); }
824849
void readVOUT() {
850+
readMicros = micros();
825851
readDiff = adcRead(SENSE_OUTPUT) - adcRead(SENSE_GNDISO);
826852

827853
if (!analog_ref_half && readDiff > RANGE_SWITCH_THRESHOLD_LOW && readDiff < RANGE_SWITCH_THRESHOLD_HIGH/3)
@@ -903,16 +929,24 @@ void printCalibInfo() {
903929
Serial.println((PREFERENCES & PREFERENCE_MU_SAMPLERATE)
904930
? "toggle samplerate adjust"
905931
: "toggle offset enable");
932+
Serial.print("USB timestamp: ");
933+
Serial.println((PREFERENCES & PREFERENCE_USB_TIMESTAMP)
934+
? "on" : "off");
935+
Serial.print("BT timestamp: ");
936+
Serial.println((PREFERENCES & PREFERENCE_BT_TIMESTAMP)
937+
? "on" : "off");
906938
}
907939
void printSerialMenu() {
908940
Serial.println("\r\nUSB serial commands:");
909941
Serial.println("a = toggle Auto-Off function");
910942
Serial.print ("b = toggle BT/serial logging (");Serial.print(SERIAL_UART_BAUD);Serial.println("baud)");
943+
Serial.println("B = toggle timestamp on BT/serial output [PREF]");
911944
Serial.println("f = cycle serial logging formats (exponent,nA,uA,mA/raw-ADC) [PREF]");
912945
Serial.println("g = toggle GPIO range indication (SCK=mA,MISO=uA,MOSI=nA)");
913946
Serial.println("o = toggle touchpad offset (bias) / samplerate selection [PREF]");
914947
Serial.println("t = toggle touchpad serial output debug info");
915948
Serial.println("u = toggle USB/serial logging");
949+
Serial.println("U = toggle timestamp in USB/serial output [PREF]");
916950
Serial.println("+ = increase mode value");
917951
Serial.println("- = decrease mode value");
918952
Serial.println("? = Print this menu and calib info");

0 commit comments

Comments
 (0)