diff --git a/components/tx_ultimate_easy/__init__.py b/components/tx_ultimate_easy/__init__.py index 3a598ad..7120ead 100644 --- a/components/tx_ultimate_easy/__init__.py +++ b/components/tx_ultimate_easy/__init__.py @@ -13,6 +13,7 @@ DEPENDENCIES = ['uart'] CONF_TX_ULTIMATE_EASY = "tx_ultimate_easy" CONF_UART = "uart" +CONF_GANG_COUNT = "gang_count" CONF_ON_TOUCH_EVENT = "on_touch_event" CONF_ON_PRESS = "on_press" @@ -33,6 +34,7 @@ CONFIG_SCHEMA = cv.Schema({ cv.GenerateID(): cv.declare_id(TxUltimateTouch), cv.Required(CONF_UART): cv.use_id(uart), + cv.Optional(CONF_GANG_COUNT, default=1): cv.int_range(min=1, max=4), cv.Optional(CONF_ON_TOUCH_EVENT): automation.validate_automation(single=True), cv.Optional(CONF_ON_PRESS): automation.validate_automation(single=True), @@ -49,6 +51,9 @@ 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_GANG_COUNT in config: + cg.add(var.set_gang_count(config[CONF_GANG_COUNT])) + if CONF_ON_TOUCH_EVENT in config: await automation.build_automation( var.get_trigger_touch_event(), diff --git a/components/tx_ultimate_easy/tx_ultimate_easy.cpp b/components/tx_ultimate_easy/tx_ultimate_easy.cpp index 84150db..61be4f6 100644 --- a/components/tx_ultimate_easy/tx_ultimate_easy.cpp +++ b/components/tx_ultimate_easy/tx_ultimate_easy.cpp @@ -48,6 +48,30 @@ namespace esphome { void TxUltimateEasy::dump_config() { ESP_LOGCONFIG(TAG, "TX Ultimate Easy"); + ESP_LOGCONFIG(TAG, " Gang count: %" PRIu8, this->gang_count_); + } + + bool TxUltimateEasy::set_gang_count(const uint8_t gang_count) { + if (gang_count < 1 or gang_count > 4) + return false; + this->gang_count_ = gang_count; + return true; + } + + uint8_t TxUltimateEasy::get_button_from_position(const uint8_t position) { + if (position > TOUCH_MAX_POSITION) // Invalid position + return 0; + if (this->gang_count_ == 1) // For 1 Gang model, any position is button 1 + return 1; + if (this->gang_count_ < 1 or this->gang_count_ > 4) // Invalid gang count + return 0; + const uint8_t width = TOUCH_MAX_POSITION / this->gang_count_; // Width of each button region + if (width < 1 or width > this->gang_count_) // Invalid width - and prevents division by zero + return 0; + uint8_t button = (position / width) + 1; // Determine button region + if (button > this->gang_count_) + button = this->gang_count_; // Clamp to max button count + return button; } void TxUltimateEasy::send_touch_(TouchPoint tp) { @@ -103,7 +127,7 @@ namespace esphome { state == TOUCH_STATE_SWIPE_LEFT || state == TOUCH_STATE_SWIPE_RIGHT || state == TOUCH_STATE_MULTI_TOUCH) && - (uart_received_bytes[6] >= 0 || state == TOUCH_STATE_MULTI_TOUCH); + (uart_received_bytes[6] >= 0 || state == TOUCH_STATE_MULTI_TOUCH); } int TxUltimateEasy::get_touch_position_x(const std::array &uart_received_bytes) { @@ -133,6 +157,8 @@ namespace esphome { TouchPoint TxUltimateEasy::get_touch_point(const std::array &uart_received_bytes) { TouchPoint tp; tp.x = this->get_touch_position_x(uart_received_bytes); + if (tp.x >= 0) + tp.button = this->get_button_from_position(static_cast(tp.x)); tp.state = this->get_touch_state(uart_received_bytes); switch (tp.state) { case TOUCH_STATE_RELEASE: diff --git a/components/tx_ultimate_easy/tx_ultimate_easy.h b/components/tx_ultimate_easy/tx_ultimate_easy.h index 5d7b3a0..cfb9e8b 100644 --- a/components/tx_ultimate_easy/tx_ultimate_easy.h +++ b/components/tx_ultimate_easy/tx_ultimate_easy.h @@ -13,6 +13,9 @@ namespace esphome { namespace tx_ultimate_easy { + // Touch Max Position + constexpr uint8_t TOUCH_MAX_POSITION = 10; + // Touch State Constants constexpr uint8_t TOUCH_STATE_RELEASE = 0x01; constexpr uint8_t TOUCH_STATE_PRESS = 0x02; @@ -32,6 +35,7 @@ namespace esphome { static const char *TAG = "tx_ultimate_easy"; struct TouchPoint { + uint8_t button = 0; int8_t x = -1; int8_t state = -1; std::string state_str = "Unknown"; @@ -53,6 +57,10 @@ namespace esphome { void loop() override; void dump_config() override; + uint8_t get_gang_count() { return this->gang_count_; } + bool set_gang_count(const uint8_t gang_count); + uint8_t get_button_from_position(const uint8_t position); + protected: void send_touch_(TouchPoint tp); void handle_touch(const std::array &bytes); @@ -70,6 +78,8 @@ namespace esphome { Trigger trigger_multi_touch_release_; Trigger trigger_long_touch_release_; + uint8_t gang_count_ = 1; + }; // class TxUltimateEasy } // namespace tx_ultimate_easy