@@ -89,7 +89,7 @@ void Wippersnapper_AnalogIO::setADCResolution(int resolution) {
89
89
_nativeResolution = 12 ;
90
90
#elif defined(ARDUINO_ARCH_ESP32)
91
91
scaleAnalogRead = true ; // probably should be false, handled in bsp
92
- _nativeResolution = 13 ; // S3 ADC is 13-bit, others are 12-bit
92
+ _nativeResolution = 13 ; // S3 ADC is 13-bit, others are 12-bit
93
93
#elif defined(ARDUINO_ARCH_RP2040)
94
94
scaleAnalogRead = true ;
95
95
_nativeResolution = 10 ;
@@ -338,7 +338,7 @@ void Wippersnapper_AnalogIO::update() {
338
338
// Process analog input pins
339
339
for (int i = 0 ; i < _totalAnalogInputPins; i++) {
340
340
// TODO: Can we collapse the conditionals below?
341
- if (!_analog_input_pins[i].enabled ){
341
+ if (!_analog_input_pins[i].enabled ) {
342
342
continue ;
343
343
}
344
344
@@ -357,13 +357,13 @@ void Wippersnapper_AnalogIO::update() {
357
357
pinValRaw = getPinValue (_analog_input_pins[i].pinName );
358
358
} else {
359
359
WS_DEBUG_PRINTLN (" ERROR: Unable to read pin value, cannot determine "
360
- " analog read mode!" );
360
+ " analog read mode!" );
361
361
pinValRaw = 0.0 ;
362
362
}
363
363
364
364
// Publish a new pin event
365
365
encodePinEvent (_analog_input_pins[i].pinName ,
366
- _analog_input_pins[i].readMode , pinValRaw, pinValVolts);
366
+ _analog_input_pins[i].readMode , pinValRaw, pinValVolts);
367
367
368
368
// IMPT - reset the digital pin
369
369
_analog_input_pins[i].prvPeriod = millis ();
@@ -378,17 +378,15 @@ void Wippersnapper_AnalogIO::update() {
378
378
WS_DEBUG_PRINT (" Diff: " );
379
379
WS_DEBUG_PRINTLN ((long )millis () - _analog_input_pins[i].prvPeriod );
380
380
381
- if (_analog_input_pins[i].prvPeriod == 0L )
382
- {
381
+ if (_analog_input_pins[i].prvPeriod == 0L ) {
383
382
// last time was a clean event, passed hyteresis or 300ms had elapsed
384
383
WS_DEBUG_PRINTLN (" prvPeriod is 0, last time was a clean event, "
385
- " passed hyteresis or 500ms had elapsed" );
386
- }
387
- else
388
- {
384
+ " passed hyteresis or 500ms had elapsed" );
385
+ } else {
389
386
// We're waiting 300ms before posting, to avoid a flood of events
390
- WS_DEBUG_PRINTLN (" prvPeriod is not 0, probably waiting 300ms before posting, "
391
- " to avoid a flood of events" );
387
+ WS_DEBUG_PRINTLN (
388
+ " prvPeriod is not 0, probably waiting 300ms before posting, "
389
+ " to avoid a flood of events" );
392
390
}
393
391
// note: on-change requires ADC DEFAULT_HYSTERISIS to check against prv
394
392
// pin value
@@ -397,22 +395,21 @@ void Wippersnapper_AnalogIO::update() {
397
395
WS_DEBUG_PRINTLN (pinValRaw);
398
396
399
397
double currentLogValue = log10 (pinValRaw + 1 ); // +1 to avoid log(0)
400
- double lastLogValue = log10 (_analog_input_pins[i].prvPinVal + 1 ); // +1 to avoid log(0)
398
+ double lastLogValue =
399
+ log10 (_analog_input_pins[i].prvPinVal + 1 ); // +1 to avoid log(0)
401
400
WS_DEBUG_PRINT (" CurrentLogValue: " );
402
401
WS_DEBUG_PRINTLN (currentLogValue);
403
402
WS_DEBUG_PRINT (" LastLogValue: " );
404
403
WS_DEBUG_PRINTLN (lastLogValue);
405
404
bool passed_hysterisys = false ;
406
405
// Check if the logarithmic change exceeds the threshold
407
- if (abs (currentLogValue - lastLogValue) > DEFAULT_HYSTERISIS)
408
- {
406
+ if (abs (currentLogValue - lastLogValue) > DEFAULT_HYSTERISIS) {
409
407
passed_hysterisys = true ;
410
408
WS_DEBUG_PRINTLN (" ADC passed hysteresis" );
411
409
} else {
412
410
WS_DEBUG_PRINTLN (" ADC did not pass hysteresis" );
413
411
}
414
412
415
-
416
413
// old technique
417
414
uint16_t _pinValThreshHi =
418
415
_analog_input_pins[i].prvPinVal +
@@ -425,8 +422,6 @@ void Wippersnapper_AnalogIO::update() {
425
422
WS_DEBUG_PRINT (" PinValThreshLow: " );
426
423
WS_DEBUG_PRINTLN (_pinValThreshLow);
427
424
428
-
429
-
430
425
if (pinValRaw > _pinValThreshHi || pinValRaw < _pinValThreshLow) {
431
426
// passed_hysterisys = true;
432
427
WS_DEBUG_PRINTLN (" ADC passed OLD hysteresis" );
@@ -435,64 +430,64 @@ void Wippersnapper_AnalogIO::update() {
435
430
}
436
431
437
432
if (_analog_input_pins[i].readMode ==
438
- wippersnapper_pin_v1_ConfigurePinRequest_AnalogReadMode_ANALOG_READ_MODE_PIN_VOLTAGE)
439
- {
433
+ wippersnapper_pin_v1_ConfigurePinRequest_AnalogReadMode_ANALOG_READ_MODE_PIN_VOLTAGE) {
440
434
pinValVolts = getPinValueVolts (_analog_input_pins[i].pinName );
441
435
WS_DEBUG_PRINT (" PinValVolts: " );
442
436
WS_DEBUG_PRINTLN (pinValVolts);
443
- }
444
- else if (
437
+ } else if (
445
438
_analog_input_pins[i].readMode ==
446
- wippersnapper_pin_v1_ConfigurePinRequest_AnalogReadMode_ANALOG_READ_MODE_PIN_VALUE)
447
- {
439
+ wippersnapper_pin_v1_ConfigurePinRequest_AnalogReadMode_ANALOG_READ_MODE_PIN_VALUE) {
448
440
// already fetched raw value, just print it
449
441
WS_DEBUG_PRINT (" PinValRaw: " );
450
442
WS_DEBUG_PRINTLN (pinValRaw);
451
- }
452
- else
453
- {
443
+ } else {
454
444
WS_DEBUG_PRINTLN (" ERROR: Unable to read pin value, cannot determine "
455
- " analog read mode!" );
445
+ " analog read mode!" );
456
446
pinValRaw = 0.0 ;
457
447
}
458
448
459
449
// prvPeriod is 0 means we just sent a final movement event, so we can
460
450
// send another one immediately if the ADC has changed enough while also
461
451
// waiting 200ms before posting the next final movement event (or
462
- // continued movement events), to avoid a flood of events when twisting pots
452
+ // continued movement events), to avoid a flood of events when twisting
453
+ // pots
463
454
if (passed_hysterisys &&
464
455
(((long )millis () - _analog_input_pins[i].prvPeriod ) > 200 ||
465
- _analog_input_pins[i].prvPeriod == 0L ))
466
- {
456
+ _analog_input_pins[i].prvPeriod == 0L )) {
467
457
WS_DEBUG_PRINTLN (" ADC has changed enough, publishing event..." );
468
458
_analog_input_pins[i].prvPinVal = pinValRaw;
469
459
_analog_input_pins[i].prvPeriod = millis ();
470
460
// Publish pin event to IO
471
461
encodePinEvent (_analog_input_pins[i].pinName ,
472
462
_analog_input_pins[i].readMode , pinValRaw, pinValVolts);
473
- // } else if (_analog_input_pins[i].prvPeriod != 0L &&
474
- // pinValRaw != _analog_input_pins[i].prvPinVal &&
475
- // ((long)millis() - _analog_input_pins[i].prvPeriod) > 200) {
476
- // // failed hysterisys, but we were waiting 500ms before posting, to avoid
477
- // // a flood of events
478
- // WS_DEBUG_PRINTLN(
479
- // "ADC has only mildly changed, but we were waiting 200ms before "
480
- // "posting, to avoid a flood of events and this is the final value");
481
- // _analog_input_pins[i].prvPeriod = 0L;
482
- // _analog_input_pins[i].prvPinVal = pinValRaw;
483
- // // Publish pin event to IO
484
- // encodePinEvent(_analog_input_pins[i].pinName,
485
- // _analog_input_pins[i].readMode, pinValRaw, pinValVolts);
486
-
487
- // } else {
463
+ // } else if (_analog_input_pins[i].prvPeriod != 0L &&
464
+ // pinValRaw != _analog_input_pins[i].prvPinVal &&
465
+ // ((long)millis() - _analog_input_pins[i].prvPeriod) > 200)
466
+ // {
467
+ // // failed hysterisys, but we were waiting 500ms before posting, to
468
+ // avoid
469
+ // // a flood of events
470
+ // WS_DEBUG_PRINTLN(
471
+ // "ADC has only mildly changed, but we were waiting 200ms before
472
+ // " "posting, to avoid a flood of events and this is the final
473
+ // value");
474
+ // _analog_input_pins[i].prvPeriod = 0L;
475
+ // _analog_input_pins[i].prvPinVal = pinValRaw;
476
+ // // Publish pin event to IO
477
+ // encodePinEvent(_analog_input_pins[i].pinName,
478
+ // _analog_input_pins[i].readMode, pinValRaw,
479
+ // pinValVolts);
480
+
481
+ // } else {
488
482
// WS_DEBUG_PRINTLN("ADC has not changed enough, continue...");
489
483
// _analog_input_pins[i].prvPeriod = millis();
490
484
// _analog_input_pins[i].prvPinVal = pinValRaw;
491
485
// continue;
492
486
}
493
487
494
488
// if (_analog_input_pins[i].readMode ==
495
- // wippersnapper_pin_v1_ConfigurePinRequest_AnalogReadMode_ANALOG_READ_MODE_PIN_VOLTAGE) {
489
+ // wippersnapper_pin_v1_ConfigurePinRequest_AnalogReadMode_ANALOG_READ_MODE_PIN_VOLTAGE)
490
+ // {
496
491
// pinValVolts = pinValRaw * getAref() / 65536;
497
492
// }
498
493
@@ -507,6 +502,5 @@ void Wippersnapper_AnalogIO::update() {
507
502
// set the pin value in the digital pin object for comparison on next
508
503
// run
509
504
}
510
-
511
505
}
512
506
}
0 commit comments