espHome-NBS-files/external_components/tablesoccer_btn_helper/tablesoccer_helper.h

73 lines
1.9 KiB
C++

#include "esphome.h"
namespace esphome {
namespace tablesoccer_helper {
class BtnHelperClass : public Component, public mqtt::CustomMQTTDevice {
protected:
InternalGPIOPin *resetBtn;
InternalGPIOPin *undoBtn;
gpio::GPIOBinaryOutput *keep_alive;
unsigned long lastThingDone = 0;
unsigned long enableFrom = 0;
public:
void set_reset_pin(InternalGPIOPin *resetBtn){
this->resetBtn = resetBtn;
}
void set_undo_pin(InternalGPIOPin *undoBtn){
this->undoBtn = undoBtn;
}
void set_keep_alive_output(gpio::GPIOBinaryOutput *keep_alive){
this->keep_alive = keep_alive;
}
void setup() override {
lastThingDone = millis();
enableFrom = millis();
ESP_LOGD("Tablesoccer", "Ready");
}
void reset_last_thing_done(){
lastThingDone = millis();
}
void send_score(std::string side){
publish("tabletenniscounter/control/score/count", side, 2, false);
lastThingDone = millis();
enableFrom = lastThingDone + 200;
}
void loop() override{
if(millis() < enableFrom) return;
if(resetBtn->digital_read() == LOW && undoBtn->digital_read() == LOW){
ESP_LOGD("Tablesoccer", "User requested shutdown, Shutting Down.");
keep_alive->set_state(false);
}
if(resetBtn->digital_read() == LOW){
publish("tabletenniscounter/control/score/reset", "", 2, false);
lastThingDone = millis();
enableFrom = lastThingDone + 500;
}
else if(undoBtn->digital_read() == LOW){
publish("tabletenniscounter/control/score/undo", "", 2, false);
lastThingDone = millis();
enableFrom = lastThingDone + 500;
}
else if((millis() - lastThingDone) >= 300000){
ESP_LOGD("Tablesoccer", "Table soccer table has't been used in over 15 minutes, Shutting Down.");
keep_alive->set_state(false);
}
}
};
}
}