diff --git a/ESPHome/TX-Ultimate-Easy-ESPHome_core.yaml b/ESPHome/TX-Ultimate-Easy-ESPHome_core.yaml index f4b3249..e5d7617 100644 --- a/ESPHome/TX-Ultimate-Easy-ESPHome_core.yaml +++ b/ESPHome/TX-Ultimate-Easy-ESPHome_core.yaml @@ -30,6 +30,46 @@ binary_sensor: platform: template lambda: return sw_vibration_motor->state; + - id: bs_button_1 + name: Button 1 + internal: false + platform: template + on_press: + then: + - switch.toggle: sw_relay_1 + - delay: !lambda "return (isnan(nr_button_timeout->state) ? 500 : int(nr_button_timeout->state));" + - lambda: bs_button_1->publish_state(false); + + - id: bs_button_2 + name: Button 2 + internal: true + platform: template + on_press: + then: + - switch.toggle: sw_relay_2 + - delay: !lambda "return (isnan(nr_button_timeout->state) ? 500 : int(nr_button_timeout->state));" + - lambda: bs_button_2->publish_state(false); + + - id: bs_button_3 + name: Button 3 + internal: true + platform: template + on_press: + then: + - switch.toggle: sw_relay_3 + - delay: !lambda "return (isnan(nr_button_timeout->state) ? 500 : int(nr_button_timeout->state));" + - lambda: bs_button_3->publish_state(false); + + - id: bs_button_4 + name: Button 4 + internal: true + platform: template + on_press: + then: + - switch.toggle: sw_relay_4 + - delay: !lambda "return (isnan(nr_button_timeout->state) ? 500 : int(nr_button_timeout->state));" + - lambda: bs_button_4->publish_state(false); + # bluetooth_proxy: button: @@ -92,10 +132,19 @@ esphome: - lambda: |- auto index = sl_tx_model_gang->active_index(); if (index.has_value()) { + uint8_t idx = index.value(); sw_relay_1->set_internal(false); - sw_relay_2->set_internal(index.value() < 1); - sw_relay_3->set_internal(index.value() < 2); - sw_relay_4->set_internal(index.value() < 3); + sw_relay_2->set_internal(idx < 1); + sw_relay_3->set_internal(idx < 2); + sw_relay_4->set_internal(idx < 3); + bs_button_1->set_internal(false); + bs_button_2->set_internal(idx < 1); + bs_button_3->set_internal(idx < 2); + bs_button_4->set_internal(idx < 3); + bs_button_1->publish_state(false); + bs_button_2->publish_state(false); + bs_button_3->publish_state(false); + bs_button_4->publish_state(false); } - priority: 600 # This is where most sensors are set up. then: @@ -106,7 +155,7 @@ esphome: external_components: - source: type: git - url: https://github.com/edwardtfn/TX-Ultimate-Easy/ + url: https://github.com/edwardtfn/TX-Ultimate-Easy ref: dev components: - tx_ultimate_easy @@ -210,6 +259,20 @@ number: initial_value: 15 optimistic: true restore_value: true + + - id: nr_button_timeout + name: Button timeout + icon: mdi:timer + unit_of_measurement: ms + internal: false + platform: template + min_value: 1 + max_value: 3000 + step: 1 + initial_value: 500 + optimistic: true + restore_value: true + ota: psram: @@ -361,21 +424,20 @@ time: platform: homeassistant tx_ultimate_easy: + id: tx_ultimate + uart: uart_touch + + on_touch_event: + - lambda: |- + ESP_LOGD("tx_ultimate_easy.on_touch_event", "Touch event:"); + ESP_LOGD("tx_ultimate_easy.on_touch_event", " State: %i (%s)", touch.state, touch.state_str.c_str()); + ESP_LOGD("tx_ultimate_easy.on_touch_event", " Position: %i", touch.x); uart: - id: if_uart_touch + id: uart_touch tx_pin: GPIO19 rx_pin: GPIO22 baud_rate: 115200 - debug: - direction: BOTH #RX - dummy_receiver: false - after: - timeout: 2s - bytes: 2048 - sequence: - # - lambda: UARTDebug::log_string(direction, bytes); - - lambda: UARTDebug::log_hex(direction, bytes, ' '); web_server: diff --git a/components/tx_ultimate_easy/__init__.py b/components/tx_ultimate_easy/__init__.py index edac612..545ee57 100644 --- a/components/tx_ultimate_easy/__init__.py +++ b/components/tx_ultimate_easy/__init__.py @@ -14,13 +14,12 @@ CONF_TX_ULTIMATE_EASY = "tx_ultimate_easy" CONF_UART = "uart" +CONF_ON_TOUCH_EVENT = "on_touch_event" CONF_ON_PRESS = "on_press" CONF_ON_RELEASE = "on_release" - CONF_ON_SWIPE_LEFT = "on_swipe_left" CONF_ON_SWIPE_RIGHT = "on_swipe_right" - -CONF_ON_FULL_TOUCH_RELEASE = "on_full_touch_release" +CONF_ON_MULTI_TOUCH_RELEASE = "on_multi_touch_release" CONF_ON_LONG_TOUCH_RELEASE = "on_long_touch_release" tx_ultimate_easy_ns = cg.esphome_ns.namespace('tx_ultimate_easy') @@ -35,11 +34,12 @@ CONFIG_SCHEMA = cv.Schema({ cv.Required(CONF_UART): cv.use_id(uart), + cv.Optional(CONF_ON_TOUCH_EVENT): automation.validate_automation(single=True), cv.Optional(CONF_ON_PRESS): automation.validate_automation(single=True), cv.Optional(CONF_ON_RELEASE): automation.validate_automation(single=True), cv.Optional(CONF_ON_SWIPE_LEFT): automation.validate_automation(single=True), cv.Optional(CONF_ON_SWIPE_RIGHT): automation.validate_automation(single=True), - cv.Optional(CONF_ON_FULL_TOUCH_RELEASE): automation.validate_automation(single=True), + cv.Optional(CONF_ON_MULTI_TOUCH_RELEASE): automation.validate_automation(single=True), cv.Optional(CONF_ON_LONG_TOUCH_RELEASE): automation.validate_automation(single=True), }).extend(cv.COMPONENT_SCHEMA).extend(uart.UART_DEVICE_SCHEMA) @@ -49,44 +49,51 @@ async def register_tx_ultimate_easy(var, config): uart_component = await cg.get_variable(config[CONF_UART]) cg.add(var.set_uart_component(uart_component)) + if CONF_ON_TOUCH_EVENT in config: + await automation.build_automation( + var.get_trigger_touch_event(), + [(TouchPoint, "touch")], + config[CONF_ON_TOUCH_EVENT], + ) + if CONF_ON_PRESS in config: await automation.build_automation( - var.get_touch_trigger(), + var.get_trigger_touch(), [(TouchPoint, "touch")], config[CONF_ON_PRESS], ) if CONF_ON_RELEASE in config: await automation.build_automation( - var.get_release_trigger(), + var.get_trigger_release(), [(TouchPoint, "touch")], config[CONF_ON_RELEASE], ) if CONF_ON_SWIPE_LEFT in config: await automation.build_automation( - var.get_swipe_left_trigger(), + var.get_trigger_swipe_left(), [(TouchPoint, "touch")], config[CONF_ON_SWIPE_LEFT], ) if CONF_ON_SWIPE_RIGHT in config: await automation.build_automation( - var.get_swipe_right_trigger(), + var.get_trigger_swipe_right(), [(TouchPoint, "touch")], config[CONF_ON_SWIPE_RIGHT], ) - if CONF_ON_FULL_TOUCH_RELEASE in config: + if CONF_ON_MULTI_TOUCH_RELEASE in config: await automation.build_automation( - var.get_full_touch_release_trigger(), + var.get_trigger_multi_touch_release(), [(TouchPoint, "touch")], - config[CONF_ON_FULL_TOUCH_RELEASE], + config[CONF_ON_MULTI_TOUCH_RELEASE], ) if CONF_ON_LONG_TOUCH_RELEASE in config: await automation.build_automation( - var.get_long_touch_release_trigger(), + var.get_trigger_long_touch_release(), [(TouchPoint, "touch")], config[CONF_ON_LONG_TOUCH_RELEASE], ) @@ -95,5 +102,4 @@ async def to_code(config): var = cg.new_Pvariable(config[CONF_ID]) await cg.register_component(var, config) await uart.register_uart_device(var, config) - await register_tx_ultimate_easy(var, config) diff --git a/components/tx_ultimate_easy/tx_ultimate_easy.cpp b/components/tx_ultimate_easy/tx_ultimate_easy.cpp index 5bd5af9..5443904 100644 --- a/components/tx_ultimate_easy/tx_ultimate_easy.cpp +++ b/components/tx_ultimate_easy/tx_ultimate_easy.cpp @@ -1,202 +1,152 @@ #include "esphome/core/log.h" #include "tx_ultimate_easy.h" -namespace esphome -{ - namespace tx_ultimate_easy - { +namespace esphome { + namespace tx_ultimate_easy { static const char *TAG = "tx_ultimate_easy"; - void TxUltimateEasy::setup() - { - ESP_LOGI("log", "%s", "Tx Ultimate Touch is initialized"); + void TxUltimateEasy::setup() { + ESP_LOGI("log", "%s", "Tx Ultimate Easy is initialized"); } - void TxUltimateEasy::loop() - { + void TxUltimateEasy::loop() { bool found = false; - - int bytes[15] = {}; + int uart_received_bytes[15] = {}; int byte = -1; int i = 0; - - while (this->available()) - { + while (this->available()) { byte = this->read(); - if (byte == 170) - { - handle_touch(bytes); - i = 0; + if (byte == 170) { + handle_touch(uart_received_bytes); + i = 0; } - - bytes[i] = byte; - + uart_received_bytes[i] = byte; i++; - - if (byte != 0) - { - found = true; + if (byte != 0) { + found = true; } }; + if (found) handle_touch(uart_received_bytes); + } - if (found) - { - handle_touch(bytes); + void TxUltimateEasy::handle_touch(int uart_received_bytes[]) { + ESP_LOGVV(TAG, "------------"); + ESP_LOGVV(TAG, "- UART-Log -"); + ESP_LOGVV(TAG, "------------"); + for (int i = 0; i < 15; i++) ESP_LOGVV(TAG, "UART - Log - Byte[%i]: %i", i, uart_received_bytes[i]); + if (is_valid_data(uart_received_bytes)) send_touch_(get_touch_point(uart_received_bytes)); + } + + void TxUltimateEasy::dump_config() { ESP_LOGCONFIG(TAG, "Tx Ultimate Easy"); } + + void TxUltimateEasy::send_touch_(TouchPoint tp) { + this->trigger_touch_event_.trigger(tp); + switch (tp.state) { + case TOUCH_STATE_RELEASE: + if (tp.x >= 17) { + tp.x = tp.x - 16; + ESP_LOGV(TAG, "Long touch - Released (x=%d)", tp.x); + this->trigger_long_touch_release_.trigger(tp); + } else { + ESP_LOGV(TAG, "Touch - Released (x=%d)", tp.x); + this->trigger_release_.trigger(tp); + } + break; + + case TOUCH_STATE_PRESS: + ESP_LOGV(TAG, "Touch - Pressed (x=%d)", tp.x); + this->trigger_touch_.trigger(tp); + break; + + case TOUCH_STATE_SWIPE_LEFT: + ESP_LOGV(TAG, "Swipe - Left (x=%d)", tp.x); + this->trigger_swipe_left_.trigger(tp); + break; + + case TOUCH_STATE_SWIPE_RIGHT: + ESP_LOGV(TAG, "Swipe - Right (x=%d)", tp.x); + this->trigger_swipe_right_.trigger(tp); + break; + + case TOUCH_STATE_MULTI_TOUCH: + ESP_LOGV(TAG, "Multi touch - Released"); + this->trigger_multi_touch_release_.trigger(tp); + break; + + default: + break; } } - void TxUltimateEasy::handle_touch(int bytes[]) - { - ESP_LOGV("UART-Log", "------------"); - for (int i = 0; i < 15; i++) - { - ESP_LOGV("UART-Log", "%i", bytes[i]); - } - - if (is_valid_data(bytes)) - { - send_touch_(get_touch_point(bytes)); - } - } - - void TxUltimateEasy::dump_config() - { - ESP_LOGCONFIG(TAG, "Tx Ultimate Touch"); - } - - void TxUltimateEasy::send_touch_(TouchPoint tp) - { - switch (tp.state) - { - case TOUCH_STATE_RELEASE: - if (tp.x >= 17) - { - tp.x = tp.x - 16; - ESP_LOGD(TAG, "Long Press Release (x=%d)", tp.x); - this->long_touch_release_trigger_.trigger(tp); - } - else - { - ESP_LOGD(TAG, "Release (x=%d)", tp.x); - this->release_trigger_.trigger(tp); - } - break; - - case TOUCH_STATE_PRESS: - ESP_LOGD(TAG, "Press (x=%d)", tp.x); - this->touch_trigger_.trigger(tp); - break; - - case TOUCH_STATE_SWIPE_LEFT: - ESP_LOGD(TAG, "Swipe Left (x=%d)", tp.x); - this->swipe_trigger_left_.trigger(tp); - break; - - case TOUCH_STATE_SWIPE_RIGHT: - ESP_LOGD(TAG, "Swipe Right (x=%d)", tp.x); - this->swipe_trigger_right_.trigger(tp); - break; - - case TOUCH_STATE_ALL_FIELDS: - ESP_LOGD(TAG, "Full Touch Release"); - this->full_touch_release_trigger_.trigger(tp); - break; - - default: - break; - } - } - - bool TxUltimateEasy::is_valid_data(int bytes[]) - { + bool TxUltimateEasy::is_valid_data(int uart_received_bytes[]) { bool valid = true; + if (!(uart_received_bytes[0] == 170 && uart_received_bytes[1] == 85 && uart_received_bytes[2] == 1 && uart_received_bytes[3] == 2)) return false; - if (!(bytes[0] == 170 && bytes[1] == 85 && bytes[2] == 1 && bytes[3] == 2)) - { - return false; - } - - int state = get_touch_state(bytes); + int state = get_touch_state(uart_received_bytes); if (state != TOUCH_STATE_PRESS && state != TOUCH_STATE_RELEASE && state != TOUCH_STATE_SWIPE_LEFT && state != TOUCH_STATE_SWIPE_RIGHT && - state != TOUCH_STATE_ALL_FIELDS) - { + state != TOUCH_STATE_MULTI_TOUCH) return false; - } - if (bytes[6] < 0 && state != TOUCH_STATE_ALL_FIELDS) - { - return false; - } + if (uart_received_bytes[6] < 0 && state != TOUCH_STATE_MULTI_TOUCH) return false; return true; } - int TxUltimateEasy::get_x_touch_position(int bytes[]) - { - int state = bytes[4]; - switch (state) - { - case TOUCH_STATE_RELEASE: - return bytes[5]; - break; + int TxUltimateEasy::get_touch_position_x(int uart_received_bytes[]) { + int state = uart_received_bytes[4]; + switch (state) { + case TOUCH_STATE_RELEASE: + case TOUCH_STATE_MULTI_TOUCH: + case TOUCH_STATE_SWIPE_LEFT: + case TOUCH_STATE_SWIPE_RIGHT: + return uart_received_bytes[5]; + break; - case TOUCH_STATE_ALL_FIELDS: - return bytes[5]; - break; - - case TOUCH_STATE_SWIPE_LEFT: - return bytes[5]; - break; - - case TOUCH_STATE_SWIPE_RIGHT: - return bytes[5]; - break; - - default: - return bytes[6]; - break; + default: + return uart_received_bytes[6]; + break; } } - int TxUltimateEasy::get_touch_state(int bytes[]) - { - int state = bytes[4]; - - if (state == TOUCH_STATE_PRESS && bytes[5] != 0) - { - state = TOUCH_STATE_RELEASE; + int TxUltimateEasy::get_touch_state(int uart_received_bytes[]) { + int state = uart_received_bytes[4]; + if (state == TOUCH_STATE_PRESS && uart_received_bytes[5] != 0) state = TOUCH_STATE_RELEASE; + if (state == TOUCH_STATE_RELEASE && uart_received_bytes[5] == TOUCH_STATE_MULTI_TOUCH) state = TOUCH_STATE_MULTI_TOUCH; + if (state == TOUCH_STATE_SWIPE) { + if (uart_received_bytes[5] == TOUCH_STATE_SWIPE_RIGHT) state = TOUCH_STATE_SWIPE_RIGHT; + else if (uart_received_bytes[5] == TOUCH_STATE_SWIPE_LEFT) state = TOUCH_STATE_SWIPE_LEFT; } - - if (state == TOUCH_STATE_RELEASE && bytes[5] == TOUCH_STATE_ALL_FIELDS) - { - state = TOUCH_STATE_ALL_FIELDS; - } - - if (state == TOUCH_STATE_SWIPE) - { - if (bytes[5] == TOUCH_STATE_SWIPE_RIGHT) - { - state = TOUCH_STATE_SWIPE_RIGHT; - } - else if (bytes[5] == TOUCH_STATE_SWIPE_LEFT) - { - state = TOUCH_STATE_SWIPE_LEFT; - } - } - return state; } - TouchPoint TxUltimateEasy::get_touch_point(int bytes[]) - { + TouchPoint TxUltimateEasy::get_touch_point(int uart_received_bytes[]) { TouchPoint tp; - - tp.x = get_x_touch_position(bytes); - tp.state = get_touch_state(bytes); - + tp.x = get_touch_position_x(uart_received_bytes); + tp.state = get_touch_state(uart_received_bytes); + tp.state_str = "Unknown"; + switch (tp.state) { + case TOUCH_STATE_RELEASE: + tp.state_str = "RELEASE"; + break; + case TOUCH_STATE_PRESS: + tp.state_str = "PRESS"; + break; + case TOUCH_STATE_SWIPE: + tp.state_str = "SWIPE"; + break; + case TOUCH_STATE_MULTI_TOUCH: + tp.state_str = "MULTI_TOUCH"; + break; + case TOUCH_STATE_SWIPE_RIGHT: + tp.state_str = "SWIPE_RIGHT"; + break; + case TOUCH_STATE_SWIPE_LEFT: + tp.state_str = "SWIPE_LEFT"; + break; + } return tp; } diff --git a/components/tx_ultimate_easy/tx_ultimate_easy.h b/components/tx_ultimate_easy/tx_ultimate_easy.h index 54f33a2..033e471 100644 --- a/components/tx_ultimate_easy/tx_ultimate_easy.h +++ b/components/tx_ultimate_easy/tx_ultimate_easy.h @@ -8,39 +8,32 @@ #include "esphome/components/switch/switch.h" #include "esphome/components/light/addressable_light.h" -namespace esphome -{ - namespace tx_ultimate_easy - { +namespace esphome { + namespace tx_ultimate_easy { const static uint8_t TOUCH_STATE_RELEASE = 1; const static uint8_t TOUCH_STATE_PRESS = 2; const static uint8_t TOUCH_STATE_SWIPE = 3; - - const static uint8_t TOUCH_STATE_ALL_FIELDS = 11; - + const static uint8_t TOUCH_STATE_MULTI_TOUCH = 11; const static uint8_t TOUCH_STATE_SWIPE_RIGHT = 12; const static uint8_t TOUCH_STATE_SWIPE_LEFT = 13; - struct TouchPoint - { + struct TouchPoint { int8_t x = -1; int8_t state = -1; + std::string state_str = "Unknown"; }; - class TxUltimateEasy : public uart::UARTDevice, public Component - { + class TxUltimateEasy : public uart::UARTDevice, public Component { public: - Trigger *get_touch_trigger() { return &this->touch_trigger_; } - Trigger *get_release_trigger() { return &this->release_trigger_; } - Trigger *get_swipe_left_trigger() { return &this->swipe_trigger_left_; } - Trigger *get_swipe_right_trigger() { return &this->swipe_trigger_right_; } - Trigger *get_full_touch_release_trigger() { return &this->full_touch_release_trigger_; } - Trigger *get_long_touch_release_trigger() { return &this->long_touch_release_trigger_; } + Trigger *get_trigger_touch_event() { return &this->trigger_touch_event_; } + Trigger *get_trigger_touch() { return &this->trigger_touch_; } + Trigger *get_trigger_release() { return &this->trigger_release_; } + Trigger *get_trigger_swipe_left() { return &this->trigger_swipe_left_; } + Trigger *get_trigger_swipe_right() { return &this->trigger_swipe_right_; } + Trigger *get_trigger_multi_touch_release() { return &this->trigger_multi_touch_release_; } + Trigger *get_trigger_long_touch_release() { return &this->trigger_long_touch_release_; } - void set_uart_component(esphome::uart::UARTComponent *uart_component) - { - this->set_uart_parent(uart_component); - } + void set_uart_component(esphome::uart::UARTComponent *uart_component) { this->set_uart_parent(uart_component); } void setup() override; void loop() override; @@ -52,15 +45,16 @@ namespace esphome TouchPoint get_touch_point(int bytes[]); bool is_valid_data(int bytes[]); - int get_x_touch_position(int bytes[]); + int get_touch_position_x(int bytes[]); int get_touch_state(int bytes[]); - Trigger touch_trigger_; - Trigger release_trigger_; - Trigger swipe_trigger_left_; - Trigger swipe_trigger_right_; - Trigger full_touch_release_trigger_; - Trigger long_touch_release_trigger_; + Trigger trigger_touch_event_; + Trigger trigger_touch_; + Trigger trigger_release_; + Trigger trigger_swipe_left_; + Trigger trigger_swipe_right_; + Trigger trigger_multi_touch_release_; + Trigger trigger_long_touch_release_; }; // class TxUltimateEasy