Skip to content

Always publish initial pin value if on_change (so we have a previous value on boot) #740

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 64 additions & 1 deletion src/components/digitalIO/Wippersnapper_DigitalGPIO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,26 @@ Wippersnapper_DigitalGPIO::~Wippersnapper_DigitalGPIO() {
delete _digital_input_pins;
}


#ifdef ARDUINO_ARCH_RP2040
void rp2040_dump_gpio_config(void) {
WS_PRINTER.printf("GPIO | FUNC | DIR | OUT | IN | PULL-UP | PULL-DOWN | SLEW\n");
WS_PRINTER.printf("-----+------+-----+-----+-----+---------+-----------+-----\n");

for (int gpio = 0; gpio < NUM_BANK0_GPIOS; gpio++) {
WS_PRINTER.printf(" %2d | %2d | %s | %d | %d | %d | %d | %d\n",
gpio,
gpio_get_function(gpio),
gpio_get_dir(gpio) ? "OUT" : "IN ",
gpio_get_out_level(gpio),
gpio_get(gpio),
gpio_is_pulled_up(gpio),
gpio_is_pulled_down(gpio),
gpio_get_slew_rate(gpio));
}
}
#endif

/*******************************************************************************************************************************/
/*!
@brief Configures a digital pin to behave as an input or an output.
Expand Down Expand Up @@ -99,6 +119,12 @@ void Wippersnapper_DigitalGPIO::initDigitalPin(
if (pull == wippersnapper_pin_v1_ConfigurePinRequest_Pull_PULL_UP) {
WS_DEBUG_PRINTLN("with internal pull-up enabled");
pinMode(pinName, INPUT_PULLUP);
#ifdef INPUT_PULLDOWN
} else if (pull ==
wippersnapper_pin_v1_ConfigurePinRequest_Pull_PULL_DOWN) {
WS_DEBUG_PRINTLN("with internal pull-down enabled");
pinMode(pinName, INPUT_PULLDOWN);
#endif
} else {
pinMode(pinName, INPUT);
WS_DEBUG_PRINT("\n");
Expand All @@ -125,10 +151,19 @@ void Wippersnapper_DigitalGPIO::initDigitalPin(
_digital_input_pins[i].pinName = pinName;
_digital_input_pins[i].period = periodMs;
_digital_input_pins[i].prvPeriod = curTime - periodMs;
if (pull == wippersnapper_pin_v1_ConfigurePinRequest_Pull_PULL_UP) {
_digital_input_pins[i].prvPinVal = HIGH;
} else {
_digital_input_pins[i].prvPinVal = LOW;
}
break;
}
}

#ifdef ARDUINO_ARCH_RP2040
rp2040_dump_gpio_config();
#endif

} else {
WS_DEBUG_PRINTLN("ERROR: Invalid digital pin direction!");
}
Expand Down Expand Up @@ -172,6 +207,10 @@ void Wippersnapper_DigitalGPIO::deinitDigitalPin(
itoa(pinName, cstr, 10);
pinMode(pinName, INPUT); // hi-z

#ifdef ARDUINO_ARCH_RP2040
rp2040_dump_gpio_config();
#endif

// if prv. in-use by DIO, release pin back to application
#ifdef STATUS_LED_PIN
if (pinName == STATUS_LED_PIN)
Expand All @@ -191,6 +230,25 @@ void Wippersnapper_DigitalGPIO::deinitDigitalPin(
int Wippersnapper_DigitalGPIO::digitalReadSvc(int pinName) {
// Service using arduino `digitalRead`
int pinVal = digitalRead(pinName);
// how do i check the digital IO pull up state for one of the
// _digital_input_pins, because I'm experiencing issues that resemble the
// situation where the initially set pull up value is no longer in effect
// For ESP32
// #if defined(ESP32)
// #include "driver/gpio.h"
// #include "esp_err.h"
// #include "esp_log.h"
// #include "esp_system.h"
// #include "esp32-hal-log.h"
// #include "esp32-hal-gpio.h"
// gpio_pull_mode_t pull_mode;
// // can't find in idf, but merged issue
// https://github.com/espressif/esp-idf/issues/12176
// esp_err_t result = gpio_get_pull_mode((gpio_num_t)pinName, &pull_mode);
// if (result == ESP_OK) {
// return (pull_mode == GPIO_PULLUP_ONLY);
// }
// #endif
return pinVal;
}

Expand Down Expand Up @@ -284,7 +342,8 @@ void Wippersnapper_DigitalGPIO::processDigitalInputs() {
} else if (_digital_input_pins[i].period == 0L) {
// read pin
int pinVal = digitalReadSvc(_digital_input_pins[i].pinName);
// only send on-change
// only send on-change, but we don't know initial state of feed
// (prvPinVal at boot)
if (pinVal != _digital_input_pins[i].prvPinVal) {
WS_DEBUG_PRINT("Executing state-based event on D");
WS_DEBUG_PRINTLN(_digital_input_pins[i].pinName);
Expand Down Expand Up @@ -324,6 +383,10 @@ void Wippersnapper_DigitalGPIO::processDigitalInputs() {

// reset the digital pin
_digital_input_pins[i].prvPeriod = curTime;
// } else {
// WS_DEBUG_PRINT("Dio: No change on ");
// WS_DEBUG_PRINT(_digital_input_pins[i].pinName);
// WS_DEBUG_PRINTLN(", skipping...");
}
}
}
Expand Down
Loading