diff --git a/src/gbmanager.cpp b/src/gbmanager.cpp index 1ac44aa..3b3cfdc 100644 --- a/src/gbmanager.cpp +++ b/src/gbmanager.cpp @@ -31,7 +31,7 @@ using std::string; void buttonPressedCallback(uint gpio, [[maybe_unused]] uint32_t events) { absolute_time_t now = get_absolute_time(); - if (absolute_time_diff_us(lastPressed, now) < 750000) { + if (absolute_time_diff_us(lastPressed, now) < 500000) { return; } else { lastPressed = now; @@ -65,7 +65,9 @@ int main() { oneWire.single_device_read_rom(address); // Initialize the relais - Relais relais(RELAIS_PIN); + // It need the button callback function, since it has to disable the on/off + // button for a short amount of time + Relais relais(RELAIS_PIN, &buttonPressedCallback); // Initialize the Buttons gpio_set_irq_enabled_with_callback(BUTTON_1_PIN, GPIO_IRQ_EDGE_FALL, true, @@ -88,6 +90,8 @@ int main() { sleep_ms(3000); while (true) { + uint64_t destTime = time_us_64() + 1000000; + oneWire.convert_temperature(address, true, false); temp_act = oneWire.temperature(address); @@ -109,5 +113,7 @@ int main() { CUSTOM_CHAR_DEG, systemInfo); myLCD.setCursor(0, 0); myLCD.sendString(lcdText); + + sleep_until((absolute_time_t){destTime}); } } \ No newline at end of file diff --git a/src/relais.cpp b/src/relais.cpp index 6742c33..9144eb2 100644 --- a/src/relais.cpp +++ b/src/relais.cpp @@ -1,12 +1,26 @@ #include "relais.h" -Relais::Relais(uint gpio) : gpio{gpio} { +Relais::Relais(uint gpio, gpio_irq_callback_t callback) + : gpio{gpio}, callback{callback} { gpio_init(gpio); gpio_set_dir(gpio, GPIO_OUT); off(); } -void Relais::activate(bool active) { gpio_put(gpio, !active); } +void Relais::activate(bool active) { + if (active == lastState) { + return; + } + + lastState = active; + + gpio_set_irq_enabled_with_callback(BUTTON_3_PIN, GPIO_IRQ_EDGE_FALL, false, + callback); + gpio_put(gpio, !active); + sleep_ms(100); + gpio_set_irq_enabled_with_callback(BUTTON_3_PIN, GPIO_IRQ_EDGE_FALL, true, + callback); +} void Relais::on() { activate(true); } diff --git a/src/relais.h b/src/relais.h index fcc357e..1a13cbb 100644 --- a/src/relais.h +++ b/src/relais.h @@ -4,15 +4,19 @@ #include "hardware/gpio.h" #include "pico/stdlib.h" +extern const uint BUTTON_3_PIN; // declared in gbmanager.cpp + class Relais { public: - Relais(uint gpio); + Relais(uint gpio, gpio_irq_callback_t callback); void activate(bool active); void on(); void off(); private: uint gpio; + bool lastState; + gpio_irq_callback_t callback; }; #endif \ No newline at end of file