@@ -370,34 +370,126 @@ void Wippersnapper_AnalogIO::update() {
370
370
}
371
371
// Does the pin execute on_change?
372
372
else if (_analog_input_pins[i].period == 0L ) {
373
-
373
+ if (_analog_input_pins[i].prvPeriod == 0L )
374
+ {
375
+ // last time was a clean event, passed hyteresis or 300ms had elapsed
376
+ WS_DEBUG_PRINTLN (" prvPeriod is 0, last time was a clean event, "
377
+ " passed hyteresis or 500ms had elapsed" );
378
+ }
379
+ else
380
+ {
381
+ // We're waiting 300ms before posting, to avoid a flood of events
382
+ WS_DEBUG_PRINTLN (" prvPeriod is not 0, probably waiting 300ms before posting, "
383
+ " to avoid a flood of events" );
384
+ WS_DEBUG_PRINT (" CurrentTime: " );
385
+ WS_DEBUG_PRINTLN (millis ());
386
+ WS_DEBUG_PRINT (" PrvPeriod: " );
387
+ WS_DEBUG_PRINTLN (_analog_input_pins[i].prvPeriod );
388
+ WS_DEBUG_PRINT (" Diff: " );
389
+ WS_DEBUG_PRINTLN ((long )millis () - _analog_input_pins[i].prvPeriod );
390
+ }
374
391
// note: on-change requires ADC DEFAULT_HYSTERISIS to check against prv
375
392
// pin value
376
393
uint16_t pinValRaw = getPinValue (_analog_input_pins[i].pinName );
394
+ WS_DEBUG_PRINT (" PinValRaw: " );
395
+ WS_DEBUG_PRINTLN (pinValRaw);
396
+
397
+ double currentLogValue = log10 (pinValRaw + 1 ); // +1 to avoid log(0)
398
+ double lastLogValue = log10 (_analog_input_pins[i].prvPinVal + 1 ); // +1 to avoid log(0)
399
+ WS_DEBUG_PRINT (" CurrentLogValue: " );
400
+ WS_DEBUG_PRINTLN (currentLogValue);
401
+ WS_DEBUG_PRINT (" LastLogValue: " );
402
+ WS_DEBUG_PRINTLN (lastLogValue);
403
+ bool passed_hysterisys = false ;
404
+ // Check if the logarithmic change exceeds the threshold
405
+ if (abs (currentLogValue - lastLogValue) > DEFAULT_HYSTERISIS)
406
+ {
407
+ passed_hysterisys = true ;
408
+ WS_DEBUG_PRINTLN (" ADC passed hysteresis" );
409
+ } else {
410
+ WS_DEBUG_PRINTLN (" ADC did not pass hysteresis" );
411
+ }
377
412
413
+
414
+ // old technique
378
415
uint16_t _pinValThreshHi =
379
416
_analog_input_pins[i].prvPinVal +
380
417
(_analog_input_pins[i].prvPinVal * DEFAULT_HYSTERISIS);
381
418
uint16_t _pinValThreshLow =
382
419
_analog_input_pins[i].prvPinVal -
383
420
(_analog_input_pins[i].prvPinVal * DEFAULT_HYSTERISIS);
421
+ WS_DEBUG_PRINT (" PinValThreshHi: " );
422
+ WS_DEBUG_PRINTLN (_pinValThreshHi);
423
+ WS_DEBUG_PRINT (" PinValThreshLow: " );
424
+ WS_DEBUG_PRINTLN (_pinValThreshLow);
425
+
384
426
385
- if (pinValRaw > _pinValThreshHi || pinValRaw < _pinValThreshLow) {
386
- // Perform voltage conversion if we need to
387
- if (_analog_input_pins[i].readMode ==
388
- wippersnapper_pin_v1_ConfigurePinRequest_AnalogReadMode_ANALOG_READ_MODE_PIN_VOLTAGE) {
389
- pinValVolts = pinValRaw * getAref () / 65536 ;
390
- }
391
427
428
+ // if (pinValRaw > _pinValThreshHi || pinValRaw < _pinValThreshLow) {
429
+ if (_analog_input_pins[i].readMode ==
430
+ wippersnapper_pin_v1_ConfigurePinRequest_AnalogReadMode_ANALOG_READ_MODE_PIN_VOLTAGE)
431
+ {
432
+ pinValVolts = getPinValueVolts (_analog_input_pins[i].pinName );
433
+ WS_DEBUG_PRINT (" PinValVolts: " );
434
+ WS_DEBUG_PRINTLN (pinValVolts);
435
+ }
436
+ else if (
437
+ _analog_input_pins[i].readMode ==
438
+ wippersnapper_pin_v1_ConfigurePinRequest_AnalogReadMode_ANALOG_READ_MODE_PIN_VALUE)
439
+ {
440
+ WS_DEBUG_PRINT (" PinValRaw: " );
441
+ WS_DEBUG_PRINTLN (pinValRaw);
442
+ }
443
+ else
444
+ {
445
+ WS_DEBUG_PRINTLN (" ERROR: Unable to read pin value, cannot determine "
446
+ " analog read mode!" );
447
+ pinValRaw = 0.0 ;
448
+ }
449
+ if (passed_hysterisys && ((long )millis () - _analog_input_pins[i].prvPeriod ) > 300 )
450
+ {
451
+ // WS_DEBUG_PRINTLN("ADC has changed enough, publishing event...");
452
+ _analog_input_pins[i].prvPinVal = pinValRaw;
453
+ _analog_input_pins[i].prvPeriod = millis ();
392
454
// Publish pin event to IO
393
455
encodePinEvent (_analog_input_pins[i].pinName ,
394
456
_analog_input_pins[i].readMode , pinValRaw,
395
457
pinValVolts);
458
+ }
459
+ else if (_analog_input_pins[i].prvPeriod != 0L && pinValRaw != _analog_input_pins[i].prvPinVal &&
460
+ ((long )millis () - _analog_input_pins[i].prvPeriod ) > 300 )
461
+ {
462
+ // failed hysterisys, but we were waiting 500ms before posting, to avoid a flood of events
463
+ WS_DEBUG_PRINTLN (" ADC has only mildly changed, but we were waiting 300ms before posting, to avoid a flood of events and this is the final value" );
464
+ _analog_input_pins[i].prvPeriod = 0L ;
465
+ _analog_input_pins[i].prvPinVal = pinValRaw;
466
+ // Publish pin event to IO
467
+ encodePinEvent (_analog_input_pins[i].pinName ,
468
+ _analog_input_pins[i].readMode , pinValRaw,
469
+ pinValVolts);
396
470
397
- } else {
471
+ }
472
+ else
473
+ {
398
474
// WS_DEBUG_PRINTLN("ADC has not changed enough, continue...");
475
+ _analog_input_pins[i].prvPeriod = millis ();
476
+ _analog_input_pins[i].prvPinVal = pinValRaw;
399
477
continue ;
400
478
}
479
+
480
+ // if (_analog_input_pins[i].readMode ==
481
+ // wippersnapper_pin_v1_ConfigurePinRequest_AnalogReadMode_ANALOG_READ_MODE_PIN_VOLTAGE) {
482
+ // pinValVolts = pinValRaw * getAref() / 65536;
483
+ // }
484
+
485
+ // // Publish pin event to IO
486
+ // encodePinEvent(_analog_input_pins[i].pinName,
487
+ // _analog_input_pins[i].readMode, pinValRaw,
488
+ // pinValVolts);
489
+ // } else {
490
+ // // WS_DEBUG_PRINTLN("ADC has not changed enough, continue...");
491
+ // continue;
492
+ // }
401
493
// set the pin value in the digital pin object for comparison on next
402
494
// run
403
495
_analog_input_pins[i].prvPinVal = pinValRaw;
0 commit comments