Skip to content

Commit 3bb4878

Browse files
committed
WIP: Tweak Process
1 parent 3e14c55 commit 3bb4878

File tree

1 file changed

+162
-149
lines changed

1 file changed

+162
-149
lines changed

src/components/analogIO/Wippersnapper_AnalogIO.cpp

Lines changed: 162 additions & 149 deletions
Original file line numberDiff line numberDiff line change
@@ -338,162 +338,175 @@ void Wippersnapper_AnalogIO::update() {
338338
// Process analog input pins
339339
for (int i = 0; i < _totalAnalogInputPins; i++) {
340340
// TODO: Can we collapse the conditionals below?
341-
if (_analog_input_pins[i].enabled == true) {
342-
343-
// Does the pin execute on-period?
344-
if ((long)millis() - _analog_input_pins[i].prvPeriod >
345-
_analog_input_pins[i].period &&
346-
_analog_input_pins[i].period != 0L) {
347-
WS_DEBUG_PRINT("Executing periodic event on A");
348-
WS_DEBUG_PRINTLN(_analog_input_pins[i].pinName);
349-
350-
// Read from analog pin
351-
if (_analog_input_pins[i].readMode ==
352-
wippersnapper_pin_v1_ConfigurePinRequest_AnalogReadMode_ANALOG_READ_MODE_PIN_VOLTAGE) {
353-
pinValVolts = getPinValueVolts(_analog_input_pins[i].pinName);
354-
} else if (
355-
_analog_input_pins[i].readMode ==
356-
wippersnapper_pin_v1_ConfigurePinRequest_AnalogReadMode_ANALOG_READ_MODE_PIN_VALUE) {
357-
pinValRaw = getPinValue(_analog_input_pins[i].pinName);
358-
} else {
359-
WS_DEBUG_PRINTLN("ERROR: Unable to read pin value, cannot determine "
360-
"analog read mode!");
361-
pinValRaw = 0.0;
362-
}
363-
364-
// Publish a new pin event
365-
encodePinEvent(_analog_input_pins[i].pinName,
366-
_analog_input_pins[i].readMode, pinValRaw, pinValVolts);
341+
if (!_analog_input_pins[i].enabled){
342+
continue;
343+
}
367344

368-
// IMPT - reset the digital pin
369-
_analog_input_pins[i].prvPeriod = millis();
345+
// Does the pin execute on-period?
346+
if (timerExpired((long)millis(), _analog_input_pins[i])) {
347+
WS_DEBUG_PRINT("Executing periodic event on A");
348+
WS_DEBUG_PRINTLN(_analog_input_pins[i].pinName);
349+
350+
// Read from analog pin
351+
if (_analog_input_pins[i].readMode ==
352+
wippersnapper_pin_v1_ConfigurePinRequest_AnalogReadMode_ANALOG_READ_MODE_PIN_VOLTAGE) {
353+
pinValVolts = getPinValueVolts(_analog_input_pins[i].pinName);
354+
} else if (
355+
_analog_input_pins[i].readMode ==
356+
wippersnapper_pin_v1_ConfigurePinRequest_AnalogReadMode_ANALOG_READ_MODE_PIN_VALUE) {
357+
pinValRaw = getPinValue(_analog_input_pins[i].pinName);
358+
} else {
359+
WS_DEBUG_PRINTLN("ERROR: Unable to read pin value, cannot determine "
360+
"analog read mode!");
361+
pinValRaw = 0.0;
370362
}
371-
// Does the pin execute on_change?
372-
else if (_analog_input_pins[i].period == 0L) {
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-
}
391-
// note: on-change requires ADC DEFAULT_HYSTERISIS to check against prv
392-
// pin value
393-
uint16_t pinValRaw = getPinValue(_analog_input_pins[i].pinName);
363+
364+
// Publish a new pin event
365+
encodePinEvent(_analog_input_pins[i].pinName,
366+
_analog_input_pins[i].readMode, pinValRaw, pinValVolts);
367+
368+
// IMPT - reset the digital pin
369+
_analog_input_pins[i].prvPeriod = millis();
370+
}
371+
// Does the pin execute on_change?
372+
else if (_analog_input_pins[i].period == 0L) {
373+
374+
WS_DEBUG_PRINT("CurrentTime: ");
375+
WS_DEBUG_PRINTLN(millis());
376+
WS_DEBUG_PRINT("PrvPeriod: ");
377+
WS_DEBUG_PRINTLN(_analog_input_pins[i].prvPeriod);
378+
WS_DEBUG_PRINT("Diff: ");
379+
WS_DEBUG_PRINTLN((long)millis() - _analog_input_pins[i].prvPeriod);
380+
381+
if (_analog_input_pins[i].prvPeriod == 0L)
382+
{
383+
// last time was a clean event, passed hyteresis or 300ms had elapsed
384+
WS_DEBUG_PRINTLN("prvPeriod is 0, last time was a clean event, "
385+
"passed hyteresis or 500ms had elapsed");
386+
}
387+
else
388+
{
389+
// 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");
392+
}
393+
// note: on-change requires ADC DEFAULT_HYSTERISIS to check against prv
394+
// pin value
395+
uint16_t pinValRaw = getPinValue(_analog_input_pins[i].pinName);
396+
WS_DEBUG_PRINT("PinValRaw: ");
397+
WS_DEBUG_PRINTLN(pinValRaw);
398+
399+
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)
401+
WS_DEBUG_PRINT("CurrentLogValue: ");
402+
WS_DEBUG_PRINTLN(currentLogValue);
403+
WS_DEBUG_PRINT("LastLogValue: ");
404+
WS_DEBUG_PRINTLN(lastLogValue);
405+
bool passed_hysterisys = false;
406+
// Check if the logarithmic change exceeds the threshold
407+
if (abs(currentLogValue - lastLogValue) > DEFAULT_HYSTERISIS)
408+
{
409+
passed_hysterisys = true;
410+
WS_DEBUG_PRINTLN("ADC passed hysteresis");
411+
} else {
412+
WS_DEBUG_PRINTLN("ADC did not pass hysteresis");
413+
}
414+
415+
416+
// old technique
417+
uint16_t _pinValThreshHi =
418+
_analog_input_pins[i].prvPinVal +
419+
(_analog_input_pins[i].prvPinVal * DEFAULT_HYSTERISIS);
420+
uint16_t _pinValThreshLow =
421+
_analog_input_pins[i].prvPinVal -
422+
(_analog_input_pins[i].prvPinVal * DEFAULT_HYSTERISIS);
423+
WS_DEBUG_PRINT("PinValThreshHi: ");
424+
WS_DEBUG_PRINTLN(_pinValThreshHi);
425+
WS_DEBUG_PRINT("PinValThreshLow: ");
426+
WS_DEBUG_PRINTLN(_pinValThreshLow);
427+
428+
429+
430+
if (pinValRaw > _pinValThreshHi || pinValRaw < _pinValThreshLow) {
431+
// passed_hysterisys = true;
432+
WS_DEBUG_PRINTLN("ADC passed OLD hysteresis");
433+
} else {
434+
WS_DEBUG_PRINTLN("ADC did not pass OLD hysteresis");
435+
}
436+
437+
if (_analog_input_pins[i].readMode ==
438+
wippersnapper_pin_v1_ConfigurePinRequest_AnalogReadMode_ANALOG_READ_MODE_PIN_VOLTAGE)
439+
{
440+
pinValVolts = getPinValueVolts(_analog_input_pins[i].pinName);
441+
WS_DEBUG_PRINT("PinValVolts: ");
442+
WS_DEBUG_PRINTLN(pinValVolts);
443+
}
444+
else if (
445+
_analog_input_pins[i].readMode ==
446+
wippersnapper_pin_v1_ConfigurePinRequest_AnalogReadMode_ANALOG_READ_MODE_PIN_VALUE)
447+
{
448+
// already fetched raw value, just print it
394449
WS_DEBUG_PRINT("PinValRaw: ");
395450
WS_DEBUG_PRINTLN(pinValRaw);
451+
}
452+
else
453+
{
454+
WS_DEBUG_PRINTLN("ERROR: Unable to read pin value, cannot determine "
455+
"analog read mode!");
456+
pinValRaw = 0.0;
457+
}
396458

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-
}
412-
413-
414-
// old technique
415-
uint16_t _pinValThreshHi =
416-
_analog_input_pins[i].prvPinVal +
417-
(_analog_input_pins[i].prvPinVal * DEFAULT_HYSTERISIS);
418-
uint16_t _pinValThreshLow =
419-
_analog_input_pins[i].prvPinVal -
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-
426-
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();
454-
// Publish pin event to IO
455-
encodePinEvent(_analog_input_pins[i].pinName,
456-
_analog_input_pins[i].readMode, pinValRaw,
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);
470-
471-
}
472-
else
473-
{
474-
// WS_DEBUG_PRINTLN("ADC has not changed enough, continue...");
475-
_analog_input_pins[i].prvPeriod = millis();
476-
_analog_input_pins[i].prvPinVal = pinValRaw;
477-
continue;
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-
// }
493-
// set the pin value in the digital pin object for comparison on next
494-
// run
459+
// prvPeriod is 0 means we just sent a final movement event, so we can
460+
// send another one immediately if the ADC has changed enough while also
461+
// waiting 200ms before posting the next final movement event (or
462+
// continued movement events), to avoid a flood of events when twisting pots
463+
if (passed_hysterisys &&
464+
(((long)millis() - _analog_input_pins[i].prvPeriod) > 200 ||
465+
_analog_input_pins[i].prvPeriod == 0L))
466+
{
467+
WS_DEBUG_PRINTLN("ADC has changed enough, publishing event...");
495468
_analog_input_pins[i].prvPinVal = pinValRaw;
469+
_analog_input_pins[i].prvPeriod = millis();
470+
// Publish pin event to IO
471+
encodePinEvent(_analog_input_pins[i].pinName,
472+
_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 {
488+
// WS_DEBUG_PRINTLN("ADC has not changed enough, continue...");
489+
// _analog_input_pins[i].prvPeriod = millis();
490+
// _analog_input_pins[i].prvPinVal = pinValRaw;
491+
// continue;
496492
}
493+
494+
// if (_analog_input_pins[i].readMode ==
495+
// wippersnapper_pin_v1_ConfigurePinRequest_AnalogReadMode_ANALOG_READ_MODE_PIN_VOLTAGE) {
496+
// pinValVolts = pinValRaw * getAref() / 65536;
497+
// }
498+
499+
// // Publish pin event to IO
500+
// encodePinEvent(_analog_input_pins[i].pinName,
501+
// _analog_input_pins[i].readMode, pinValRaw,
502+
// pinValVolts);
503+
// } else {
504+
// // WS_DEBUG_PRINTLN("ADC has not changed enough, continue...");
505+
// continue;
506+
// }
507+
// set the pin value in the digital pin object for comparison on next
508+
// run
497509
}
510+
498511
}
499512
}

0 commit comments

Comments
 (0)