Skip to content

Commit 8ea64f1

Browse files
committed
CurrentRange_R3: allow touchpad setting of sample rate
Use "sample rate" instead of oversample rate to get a more natural match to user expectations: increase should increase the sample rate, which means decrease the oversample rate. Add a preferences byte persisted in EEPROM. Use this to record whether the user wants simultaneous touch of uA+mA to toggle offset (bias) mode (default) or to repurpose the nA and mA keys to increase or decrease the sample rate. Changes to sample rate through the serial interface are persisted to EEPROM. Changes to the sample rate from the touchpads are not persisted. A tone is emitted to audibly reflect the selected sample rate. Signed-off-by: Peter A. Bigot <pab@pabigot.com>
1 parent eb7f571 commit 8ea64f1

1 file changed

Lines changed: 92 additions & 22 deletions

File tree

CurrentRanger_R3.ino

Lines changed: 92 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,22 @@
5454
#define LDO_DEFAULT 3.300 //volts, change to actual LDO output (measure GND-3V on OLED header)
5555
//***********************************************************************************************************
5656
#define BUZZER 1 // BUZZER pin
57+
#define NOTE_C4 262
58+
#define NOTE_D4 294
59+
#define NOTE_E4 330
60+
#define NOTE_F4 349
61+
#define NOTE_G4 392
62+
#define NOTE_A4 440
63+
#define NOTE_B4 494
5764
#define NOTE_C5 523
5865
#define NOTE_D5 587
5966
#define NOTE_E5 659
6067
#define NOTE_F5 698
6168
#define NOTE_G5 784
69+
#define NOTE_A5 880
6270
#define NOTE_B5 988
6371
#define NOTE_C6 1047
72+
#define NOTE_D6 1175
6473
#define TONE_BEEP 4200
6574
//***********************************************************************************************************
6675
#define MODE_MANUAL 0
@@ -110,6 +119,9 @@ Adafruit_FreeTouch qt[3] = {
110119
#define LOGGING_FORMAT_MILLIS 3 //ex: 1234 = 1.234A = 1234000uA = 1234000000nA
111120
#define LOGGING_FORMAT_ADC 4 //raw output for each range (0..4095)
112121
//***********************************************************************************************************
122+
#define PREFERENCE_MU_SAMPLERATE 0x01 // default BIAS
123+
//***********************************************************************************************************
124+
static const char samplerate_id[] = "H987654321L";
113125
int offsetCorrectionValue = 0;
114126
uint16_t gainCorrectionValue = 0;
115127
float ldoValue = 0, ldoOptimized=0;
@@ -130,12 +142,14 @@ uint8_t autoffBuzz=0;
130142
#ifdef BT_SERIAL_EN
131143
uint8_t BT_found=false;
132144
#endif
145+
uint8_t PREFERENCES;
133146
FlashStorage(eeprom_ADCoffset, int);
134147
FlashStorage(eeprom_ADCgain, uint16_t);
135148
FlashStorage(eeprom_LDO, float);
136149
FlashStorage(eeprom_AUTOFF, uint16_t);
137150
FlashStorage(eeprom_LOGGINGFORMAT, uint8_t);
138151
FlashStorage(eeprom_ADCSAMPLEN, uint8_t);
152+
FlashStorage(eeprom_PREFERENCES, uint8_t);
139153
//***********************************************************************************************************
140154

141155
void setup() {
@@ -211,6 +225,8 @@ void setup() {
211225
ADC_SAMPLEN = eeprom_ADCSAMPLEN.read();
212226
refreshADCSamplingSpeed(); //load correct value into ADC_AVGCTRL
213227

228+
PREFERENCES = eeprom_PREFERENCES.read();
229+
214230
if (gainCorrectionValue!=0) //check if anything saved in EEPROM (gain changed via SerialUSB +/-)
215231
analogReadCorrectionForced(offsetCorrectionValue, gainCorrectionValue);
216232
else {
@@ -279,7 +295,7 @@ void setup() {
279295

280296
uint32_t oledInterval=0, lpfInterval=0, offsetInterval=0, autorangeInterval=0, btInterval=0,
281297
autoOffBuzzInterval=0, touchSampleInterval=0, lastRangeChange=0;
282-
byte LPF=0, BIAS=0, AUTORANGE=0;
298+
byte LPF=0, BIAS=0, AUTORANGE=0, SRADJUST=0;
283299
byte readVbatLoop=0;
284300
float vbat=0, VOUT=0;
285301
float read1=0,read2=0,readDiff=0;
@@ -288,6 +304,17 @@ bool rangeSwitched=false;
288304
#define RANGE_UA rangeUnit=='u'
289305
#define RANGE_NA rangeUnit=='n'
290306

307+
void sampleRateBeep()
308+
{
309+
static const uint16_t freq_Hz[] = {
310+
NOTE_D6, NOTE_C6, NOTE_B5, NOTE_A5, NOTE_G5,
311+
NOTE_F5, NOTE_E5, NOTE_D5, NOTE_C5, NOTE_B4,
312+
NOTE_A4,
313+
};
314+
tone(BUZZER, freq_Hz[ADC_SAMPLEN], 200);
315+
delay(100);
316+
}
317+
291318
void rangeBeep(uint16_t switch_delay=0)
292319
{
293320
uint16_t freq = NOTE_C5;
@@ -302,6 +329,29 @@ void rangeBeep(uint16_t switch_delay=0)
302329
}
303330
}
304331

332+
bool sampleRateAdjust(int8_t delta)
333+
{
334+
// delta is positive to increase sample rate = decrease oversample rate
335+
bool ok = ((ADC_SAMPLEN - delta) >= ADC_AVGCTRL_SAMPLENUM_1_Val)
336+
&& ((ADC_SAMPLEN - delta) <= ADC_AVGCTRL_SAMPLENUM_1024_Val);
337+
if (ok) {
338+
ADC_SAMPLEN -= delta;
339+
refreshADCSamplingSpeed();
340+
}
341+
return ok;
342+
}
343+
344+
void sampleRateOutSerial()
345+
{
346+
char buf[2] = {
347+
samplerate_id[ADC_SAMPLEN],
348+
};
349+
Serial.print(buf);
350+
Serial.print(" (div ");
351+
Serial.print(1U << ADC_SAMPLEN);
352+
Serial.println(")");
353+
}
354+
305355
void loop()
306356
{
307357
uint32_t timestamp=micros();
@@ -334,10 +384,11 @@ void loop()
334384
Serial.println("ldo adj mode");
335385
}
336386
break;
337-
case 'O':
387+
case 'S':
338388
if (mode == MODE_DISABLED) {
339389
mode = MODE_SAMPLEN;
340-
Serial.println("oversampling adj mode");
390+
Serial.print("samplerate adj mode: start ");
391+
sampleRateOutSerial();
341392
}
342393
break;
343394
case '+':
@@ -354,12 +405,9 @@ void loop()
354405
Serial.println(ldoValue, 3);
355406
break;
356407
case MODE_SAMPLEN:
357-
if (ADC_SAMPLEN < ADC_AVGCTRL_SAMPLENUM_1024_Val) {
358-
++ADC_SAMPLEN;
359-
Serial.print("oversample ");
360-
Serial.println(1U << ADC_SAMPLEN);
408+
if (sampleRateAdjust(1)) {
409+
sampleRateOutSerial();
361410
eeprom_ADCSAMPLEN.write(ADC_SAMPLEN);
362-
refreshADCSamplingSpeed();
363411
} else {
364412
Serial.println("at upper limit");
365413
}
@@ -383,12 +431,9 @@ void loop()
383431
Serial.println(ldoValue, 3);
384432
break;
385433
case MODE_SAMPLEN:
386-
if (ADC_SAMPLEN > ADC_AVGCTRL_SAMPLENUM_1_Val) {
387-
--ADC_SAMPLEN;
388-
Serial.print("oversample ");
389-
Serial.println(1U << ADC_SAMPLEN);
434+
if (sampleRateAdjust(-1)) {
435+
sampleRateOutSerial();
390436
eeprom_ADCSAMPLEN.write(ADC_SAMPLEN);
391-
refreshADCSamplingSpeed();
392437
} else {
393438
Serial.println("at lower limit");
394439
}
@@ -398,6 +443,13 @@ void loop()
398443
break;
399444
}
400445
break;
446+
case 'o': //toggle touchpad bias mode/sample rate adjustment
447+
PREFERENCES ^= PREFERENCE_MU_SAMPLERATE;
448+
eeprom_PREFERENCES.write(PREFERENCES);
449+
Serial.println((PREFERENCES & PREFERENCE_MU_SAMPLERATE)
450+
? "uA+mU toggle adjust sample rate"
451+
: "uA+mU toggle offset mode");
452+
break;
401453
case 'u': //toggle USB logging
402454
USB_LOGGING_ENABLED =! USB_LOGGING_ENABLED;
403455
Serial.println(USB_LOGGING_ENABLED ? "USB_LOGGING_ENABLED" : "USB_LOGGING_DISABLED");
@@ -532,10 +584,9 @@ void loop()
532584
}
533585

534586
// Display oversample exponent in hex
535-
char buf[2] = {
536-
ADC_SAMPLEN < 10
537-
? ('0' + ADC_SAMPLEN)
538-
: ('A' - 10 + ADC_SAMPLEN),
587+
char buf[3] = {
588+
samplerate_id[ADC_SAMPLEN],
589+
SRADJUST ? '?' : 0,
539590
};
540591
u8g2.drawStr(0,12, buf); // ?? us
541592

@@ -588,18 +639,23 @@ void handleTouchPads() {
588639
if (MA_PRESSED || UA_PRESSED || NA_PRESSED) lastRangeChange=millis();
589640

590641
//range switching
591-
if (!AUTORANGE)
642+
if (!AUTORANGE && !SRADJUST)
592643
{
593644
if (MA_PRESSED && UA_NOT_PRESSED && NA_NOT_PRESSED && rangeUnit!='m') { rangeMA(); rangeBeep(); }
594645
if (UA_PRESSED && MA_NOT_PRESSED && NA_NOT_PRESSED && rangeUnit!='u') { rangeUA(); rangeBeep(); }
595646
if (NA_PRESSED && UA_NOT_PRESSED && MA_NOT_PRESSED && rangeUnit!='n') { rangeNA(); rangeBeep(); }
647+
} else if (!AUTORANGE) {
648+
if (MA_PRESSED && UA_NOT_PRESSED && NA_NOT_PRESSED) { sampleRateAdjust(1); sampleRateBeep(); }
649+
if (MA_NOT_PRESSED && UA_NOT_PRESSED && NA_PRESSED) { sampleRateAdjust(-1); sampleRateBeep(); }
596650
}
597651

598652
//LPF activation --- [NA+UA]
599653
if (UA_PRESSED && NA_PRESSED && MA_NOT_PRESSED && millis()-lpfInterval>1000) { toggleLPF(); Beep(3, false); }
600654

601-
//offset toggling (GNDISO to half supply) --- [MA+UA]
602-
if (MA_PRESSED && UA_PRESSED && NA_NOT_PRESSED && millis()-offsetInterval>1000) { toggleOffset(); Beep(3, false); }
655+
//Alternate mode activation --- [MA+UA]
656+
// PREFERENCE_MU_SAMPLERATE : NA reduce sample rate, MA increase sample rate
657+
// !PREFERENCE_MU_SAMPLERATE : offset sampling (GNDISO to half supply)
658+
if (MA_PRESSED && UA_PRESSED && NA_NOT_PRESSED && millis()-offsetInterval>1000) { toggleModeMU(); Beep(3, false); }
603659

604660
//AUTORANGE toggling
605661
if (MA_PRESSED && NA_PRESSED && UA_NOT_PRESSED && millis()-autorangeInterval>1000) { toggleAutoranging(); Beep(20, false); delay(50); (20, false); }
@@ -694,6 +750,16 @@ void toggleOffset() {
694750
if (AUTORANGE && BIAS) toggleAutoranging(); //turn off AUTORANGE
695751
}
696752

753+
void toggleModeMU() {
754+
if (PREFERENCES & PREFERENCE_MU_SAMPLERATE) {
755+
SRADJUST=!SRADJUST;
756+
Serial.print("sample rate adjust ");
757+
Serial.println(SRADJUST ? "on" : "off");
758+
} else {
759+
toggleOffset();
760+
}
761+
}
762+
697763
void toggleAutoranging() {
698764
autorangeInterval = millis();
699765
AUTORANGE=!AUTORANGE;
@@ -809,14 +875,18 @@ void printCalibInfo() {
809875
Serial.print("Gain="); Serial.println(gainCorrectionValue);
810876
Serial.print("LDO="); Serial.println(ldoValue,3);
811877
Serial.print("Oversample="); Serial.println(1U << ADC_SAMPLEN);
878+
Serial.print("uA+mA: ");
879+
Serial.println((PREFERENCES & PREFERENCE_MU_SAMPLERATE)
880+
? "toggle samplerate adjust"
881+
: "toggle offset enable");
812882
}
813883
void printSerialMenu() {
814884
Serial.println("\r\nUSB serial commands:");
815885
Serial.println("a = toggle Auto-Off function");
816886
Serial.print ("b = toggle BT/serial logging (");Serial.print(SERIAL_UART_BAUD);Serial.println("baud)");
817887
Serial.println("f = cycle serial logging formats (exponent,nA,uA,mA/raw-ADC)");
818888
Serial.println("g = toggle GPIO range indication (SCK=mA,MISO=uA,MOSI=nA)");
819-
Serial.println("s = cycle ADC sampling speeds (average,faster,slower)");
889+
Serial.println("o = toggle touchpad offset (bias) / samplerate selection");
820890
Serial.println("t = toggle touchpad serial output debug info");
821891
Serial.println("u = toggle USB/serial logging");
822892
Serial.println("+ = increase mode value");
@@ -825,7 +895,7 @@ void printSerialMenu() {
825895
Serial.println("\r\nMode commands:");
826896
Serial.println("G = gain calibration mode (1)");
827897
Serial.println("L = ldo calibration mode (1 mV)");
828-
Serial.println("O = oversampling rate");
898+
Serial.println("S = sample rate");
829899
Serial.println("! = exit mode");
830900
Serial.println();
831901
}

0 commit comments

Comments
 (0)