From 264bcb66f6fb892403199e450af2efe913a5e22a Mon Sep 17 00:00:00 2001 From: Nicolas Bachschwell Date: Thu, 2 May 2024 12:19:08 +0200 Subject: [PATCH] Added TableSoccer as esphome... It's finally no longer an ill-configure Homie --- .../tablesoccer_btn_helper/__init__.py | 41 +++++++++ .../tablesoccer_helper.h | 73 ++++++++++++++++ tablesoccer.yaml | 87 +++++++++++++++++++ 3 files changed, 201 insertions(+) create mode 100644 external_components/tablesoccer_btn_helper/__init__.py create mode 100644 external_components/tablesoccer_btn_helper/tablesoccer_helper.h create mode 100644 tablesoccer.yaml diff --git a/external_components/tablesoccer_btn_helper/__init__.py b/external_components/tablesoccer_btn_helper/__init__.py new file mode 100644 index 0000000..acf18df --- /dev/null +++ b/external_components/tablesoccer_btn_helper/__init__.py @@ -0,0 +1,41 @@ +import esphome.codegen as cg +import esphome.config_validation as cv +from esphome import pins +from esphome.components import mqtt, gpio +from esphome.const import CONF_ID + +DEPENDENCIES=["mqtt"] + +tablesoccer_helper_ns = cg.esphome_ns.namespace('tablesoccer_helper') +BtnHelperClass = tablesoccer_helper_ns.class_('BtnHelperClass', mqtt.MQTTComponent, cg.Component) +#CONF_WS2812_PIN = "pin" +#CONF_WS2812_RGB_MODE = "rgb_mode" + +CONF_SENSOR_RED = "sensor_blue" +CONF_SENSOR_BLUE = "sender_red" +CONF_BTN_RESET = "reset_pin" +CONF_BTN_UNDO = "undo_pin" +CONF_KEEP_ALIVE_OUTPUT = "keep_alive" + + +CONFIG_SCHEMA = cv.COMPONENT_SCHEMA.extend({ + cv.GenerateID(CONF_ID): cv.declare_id(BtnHelperClass), + cv.Required(CONF_BTN_RESET): pins.gpio_output_pin_schema, + cv.Required(CONF_BTN_UNDO): pins.gpio_output_pin_schema, + cv.Required(CONF_KEEP_ALIVE_OUTPUT): cv.use_id(gpio.output.GPIOBinaryOutput), +}) + + +def to_code(config): + var = cg.new_Pvariable(config[CONF_ID]) + #yield cg.register_component(var, config) + yield mqtt.register_mqtt_component(var, config) + + btnReset = yield cg.gpio_pin_expression(config[CONF_BTN_RESET]) + cg.add(var.set_reset_pin(btnReset)) + + btnUndo = yield cg.gpio_pin_expression(config[CONF_BTN_UNDO]) + cg.add(var.set_undo_pin(btnUndo)) + + keep_alive = yield cg.get_variable(config[CONF_KEEP_ALIVE_OUTPUT]) + cg.add(var.set_keep_alive_output(keep_alive)) \ No newline at end of file diff --git a/external_components/tablesoccer_btn_helper/tablesoccer_helper.h b/external_components/tablesoccer_btn_helper/tablesoccer_helper.h new file mode 100644 index 0000000..929c572 --- /dev/null +++ b/external_components/tablesoccer_btn_helper/tablesoccer_helper.h @@ -0,0 +1,73 @@ +#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); + } + + } +}; + +} +} \ No newline at end of file diff --git a/tablesoccer.yaml b/tablesoccer.yaml new file mode 100644 index 0000000..d397829 --- /dev/null +++ b/tablesoccer.yaml @@ -0,0 +1,87 @@ +esphome: + name: tablesoccer + platform: ESP8266 + board: nodemcuv2 + on_boot: + priority: 600 + then: + - output.turn_on: power_control + +mqtt: + broker: !secret mqtt_broker_1 + username: !secret mqtt_broker_1_username + password: !secret mqtt_broker_1_password + +wifi: + ssid: !secret wifi_ssid + password: !secret wifi_password + fast_connect: true +logger: + +external_components: + - source: + type: local + path: external_components/ + components: [ tablesoccer_btn_helper ] + +ota: + password: !secret streamsign_password + +output: + - platform: gpio + pin: 14 + id: power_control + +tablesoccer_btn_helper: + keep_alive: power_control + reset_pin: + number: 12 + mode: + input: true + pullup: true + undo_pin: + number: 13 + mode: + input: true + pullup: true + id: btn_helper + + +binary_sensor: + - platform: gpio + id: sensor_goal_blue + pin: + number: D1 + mode: + input: true + pullup: true + filters: + - delayed_on: 10ms + on_press: + then: + - lambda: |- + id(btn_helper).send_score("0"); + - platform: gpio + id: sensor_goal_red + pin: + number: D2 + mode: + input: true + pullup: true + filters: + - delayed_on: 10ms + on_press: + then: + - lambda: |- + id(btn_helper).send_score("1"); + + + + + +#pinMode(5, INPUT_PULLUP); +#pinMode(4, INPUT_PULLUP); + +#pinMode(14, OUTPUT); +#pinMode(12, INPUT_PULLUP); +#pinMode(13, INPUT_PULLUP); \ No newline at end of file