Skip to content

Commit 226b9b9

Browse files
committed
format analogIO
1 parent 3bb4878 commit 226b9b9

File tree

1 file changed

+42
-48
lines changed

1 file changed

+42
-48
lines changed

src/components/analogIO/Wippersnapper_AnalogIO.cpp

Lines changed: 42 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ void Wippersnapper_AnalogIO::setADCResolution(int resolution) {
8989
_nativeResolution = 12;
9090
#elif defined(ARDUINO_ARCH_ESP32)
9191
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
9393
#elif defined(ARDUINO_ARCH_RP2040)
9494
scaleAnalogRead = true;
9595
_nativeResolution = 10;
@@ -338,7 +338,7 @@ 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){
341+
if (!_analog_input_pins[i].enabled) {
342342
continue;
343343
}
344344

@@ -357,13 +357,13 @@ void Wippersnapper_AnalogIO::update() {
357357
pinValRaw = getPinValue(_analog_input_pins[i].pinName);
358358
} else {
359359
WS_DEBUG_PRINTLN("ERROR: Unable to read pin value, cannot determine "
360-
"analog read mode!");
360+
"analog read mode!");
361361
pinValRaw = 0.0;
362362
}
363363

364364
// Publish a new pin event
365365
encodePinEvent(_analog_input_pins[i].pinName,
366-
_analog_input_pins[i].readMode, pinValRaw, pinValVolts);
366+
_analog_input_pins[i].readMode, pinValRaw, pinValVolts);
367367

368368
// IMPT - reset the digital pin
369369
_analog_input_pins[i].prvPeriod = millis();
@@ -378,17 +378,15 @@ void Wippersnapper_AnalogIO::update() {
378378
WS_DEBUG_PRINT("Diff: ");
379379
WS_DEBUG_PRINTLN((long)millis() - _analog_input_pins[i].prvPeriod);
380380

381-
if (_analog_input_pins[i].prvPeriod == 0L)
382-
{
381+
if (_analog_input_pins[i].prvPeriod == 0L) {
383382
// last time was a clean event, passed hyteresis or 300ms had elapsed
384383
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 {
389386
// 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");
392390
}
393391
// note: on-change requires ADC DEFAULT_HYSTERISIS to check against prv
394392
// pin value
@@ -397,22 +395,21 @@ void Wippersnapper_AnalogIO::update() {
397395
WS_DEBUG_PRINTLN(pinValRaw);
398396

399397
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)
401400
WS_DEBUG_PRINT("CurrentLogValue: ");
402401
WS_DEBUG_PRINTLN(currentLogValue);
403402
WS_DEBUG_PRINT("LastLogValue: ");
404403
WS_DEBUG_PRINTLN(lastLogValue);
405404
bool passed_hysterisys = false;
406405
// Check if the logarithmic change exceeds the threshold
407-
if (abs(currentLogValue - lastLogValue) > DEFAULT_HYSTERISIS)
408-
{
406+
if (abs(currentLogValue - lastLogValue) > DEFAULT_HYSTERISIS) {
409407
passed_hysterisys = true;
410408
WS_DEBUG_PRINTLN("ADC passed hysteresis");
411409
} else {
412410
WS_DEBUG_PRINTLN("ADC did not pass hysteresis");
413411
}
414412

415-
416413
// old technique
417414
uint16_t _pinValThreshHi =
418415
_analog_input_pins[i].prvPinVal +
@@ -425,8 +422,6 @@ void Wippersnapper_AnalogIO::update() {
425422
WS_DEBUG_PRINT("PinValThreshLow: ");
426423
WS_DEBUG_PRINTLN(_pinValThreshLow);
427424

428-
429-
430425
if (pinValRaw > _pinValThreshHi || pinValRaw < _pinValThreshLow) {
431426
// passed_hysterisys = true;
432427
WS_DEBUG_PRINTLN("ADC passed OLD hysteresis");
@@ -435,64 +430,64 @@ void Wippersnapper_AnalogIO::update() {
435430
}
436431

437432
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) {
440434
pinValVolts = getPinValueVolts(_analog_input_pins[i].pinName);
441435
WS_DEBUG_PRINT("PinValVolts: ");
442436
WS_DEBUG_PRINTLN(pinValVolts);
443-
}
444-
else if (
437+
} else if (
445438
_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) {
448440
// already fetched raw value, just print it
449441
WS_DEBUG_PRINT("PinValRaw: ");
450442
WS_DEBUG_PRINTLN(pinValRaw);
451-
}
452-
else
453-
{
443+
} else {
454444
WS_DEBUG_PRINTLN("ERROR: Unable to read pin value, cannot determine "
455-
"analog read mode!");
445+
"analog read mode!");
456446
pinValRaw = 0.0;
457447
}
458448

459449
// prvPeriod is 0 means we just sent a final movement event, so we can
460450
// send another one immediately if the ADC has changed enough while also
461451
// 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
463454
if (passed_hysterisys &&
464455
(((long)millis() - _analog_input_pins[i].prvPeriod) > 200 ||
465-
_analog_input_pins[i].prvPeriod == 0L))
466-
{
456+
_analog_input_pins[i].prvPeriod == 0L)) {
467457
WS_DEBUG_PRINTLN("ADC has changed enough, publishing event...");
468458
_analog_input_pins[i].prvPinVal = pinValRaw;
469459
_analog_input_pins[i].prvPeriod = millis();
470460
// Publish pin event to IO
471461
encodePinEvent(_analog_input_pins[i].pinName,
472462
_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 {
488482
// WS_DEBUG_PRINTLN("ADC has not changed enough, continue...");
489483
// _analog_input_pins[i].prvPeriod = millis();
490484
// _analog_input_pins[i].prvPinVal = pinValRaw;
491485
// continue;
492486
}
493487

494488
// 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+
// {
496491
// pinValVolts = pinValRaw * getAref() / 65536;
497492
// }
498493

@@ -507,6 +502,5 @@ void Wippersnapper_AnalogIO::update() {
507502
// set the pin value in the digital pin object for comparison on next
508503
// run
509504
}
510-
511505
}
512506
}

0 commit comments

Comments
 (0)