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) { void buttonPressedCallback(uint gpio, [[maybe_unused]] uint32_t events) {
absolute_time_t now = get_absolute_time(); absolute_time_t now = get_absolute_time();
if (absolute_time_diff_us(lastPressed, now) < 750000) { if (absolute_time_diff_us(lastPressed, now) < 500000) {
return; return;
} else { } else {
lastPressed = now; lastPressed = now;
@ -65,7 +65,9 @@ int main() {
oneWire.single_device_read_rom(address); oneWire.single_device_read_rom(address);
// Initialize the relais // 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 // Initialize the Buttons
gpio_set_irq_enabled_with_callback(BUTTON_1_PIN, GPIO_IRQ_EDGE_FALL, true, gpio_set_irq_enabled_with_callback(BUTTON_1_PIN, GPIO_IRQ_EDGE_FALL, true,
@ -88,6 +90,8 @@ int main() {
sleep_ms(3000); sleep_ms(3000);
while (true) { while (true) {
uint64_t destTime = time_us_64() + 1000000;
oneWire.convert_temperature(address, true, false); oneWire.convert_temperature(address, true, false);
temp_act = oneWire.temperature(address); temp_act = oneWire.temperature(address);
@ -109,5 +113,7 @@ int main() {
CUSTOM_CHAR_DEG, systemInfo); CUSTOM_CHAR_DEG, systemInfo);
myLCD.setCursor(0, 0); myLCD.setCursor(0, 0);
myLCD.sendString(lcdText); myLCD.sendString(lcdText);
sleep_until((absolute_time_t){destTime});
} }
} }

View file

@ -1,12 +1,26 @@
#include "relais.h" #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_init(gpio);
gpio_set_dir(gpio, GPIO_OUT); gpio_set_dir(gpio, GPIO_OUT);
off(); 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); } void Relais::on() { activate(true); }

View file

@ -4,15 +4,19 @@
#include "hardware/gpio.h" #include "hardware/gpio.h"
#include "pico/stdlib.h" #include "pico/stdlib.h"
extern const uint BUTTON_3_PIN; // declared in gbmanager.cpp
class Relais { class Relais {
public: public:
Relais(uint gpio); Relais(uint gpio, gpio_irq_callback_t callback);
void activate(bool active); void activate(bool active);
void on(); void on();
void off(); void off();
private: private:
uint gpio; uint gpio;
bool lastState;
gpio_irq_callback_t callback;
}; };
#endif #endif