Disable on/off button temporarily

fixes #1
This commit is contained in:
Martin Brodbeck 2022-12-23 12:29:33 +01:00
parent 7b56534fee
commit 0bbb794346
3 changed files with 29 additions and 5 deletions

View File

@ -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});
}
}

View File

@ -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); }

View File

@ -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