Re-write component

This commit is contained in:
Edward Firmo
2024-06-19 23:18:13 +02:00
parent 91773de582
commit 59aadc98c1
3 changed files with 83 additions and 59 deletions

View File

@@ -14,5 +14,5 @@ packages:
refresh: 30s refresh: 30s
files: files:
- ESPHome/TX-Ultimate-Easy-ESPHome_core.yaml - ESPHome/TX-Ultimate-Easy-ESPHome_core.yaml
#- ESPHome/TX-Ultimate-Easy-ESPHome_addon_ble_proxy.yaml # Adds experimental BLE proxy support (limited) # - ESPHome/TX-Ultimate-Easy-ESPHome_addon_ble_proxy.yaml # Adds experimental BLE proxy support (limited)
... ...

View File

@@ -1,50 +1,61 @@
// tx_ultimate_easy.cpp
#include "esphome/core/log.h" #include "esphome/core/log.h"
#include "tx_ultimate_easy.h" #include "tx_ultimate_easy.h"
#include <string>
namespace esphome { namespace esphome {
namespace tx_ultimate_easy { namespace tx_ultimate_easy {
static const char *TAG = "tx_ultimate_easy";
void TxUltimateEasy::setup() { void TxUltimateEasy::setup() {
ESP_LOGI("log", "%s", "Tx Ultimate Easy is initialized"); ESP_LOGI(TAG, "TX Ultimate Easy is initialized");
} }
void TxUltimateEasy::loop() { void TxUltimateEasy::loop() {
bool found = false; bool found = false;
int uart_received_bytes[15] = {}; std::array<int, UART_RECEIVED_BYTES_SIZE> uart_received_bytes{};
int byte = -1; int byte = -1;
int i = 0; int i = 0;
while (this->available()) { while (this->available()) {
byte = this->read(); byte = this->read();
if (byte == 170) { if (byte == HEADER_BYTE_1) {
handle_touch(uart_received_bytes); this->handle_touch(uart_received_bytes);
i = 0; i = 0;
} }
uart_received_bytes[i] = byte; if (i < UART_RECEIVED_BYTES_SIZE) {
i++; uart_received_bytes[i] = byte;
if (byte != 0) { i++;
found = true;
} }
}; if (byte != 0x00) {
if (found) handle_touch(uart_received_bytes); found = true;
}
}
if (found) this->handle_touch(uart_received_bytes);
} }
void TxUltimateEasy::handle_touch(int uart_received_bytes[]) { void TxUltimateEasy::handle_touch(const std::array<int, UART_RECEIVED_BYTES_SIZE> &uart_received_bytes) {
ESP_LOGVV(TAG, "------------"); ESP_LOGV(TAG, "------------");
ESP_LOGVV(TAG, "- UART-Log -"); ESP_LOGV(TAG, "- UART-Log -");
ESP_LOGVV(TAG, "------------"); ESP_LOGV(TAG, "------------");
for (int i = 0; i < 15; i++) ESP_LOGVV(TAG, "UART - Log - Byte[%i]: %i", i, uart_received_bytes[i]); for (int i = 0; i < UART_RECEIVED_BYTES_SIZE; i++) {
if (is_valid_data(uart_received_bytes)) send_touch_(get_touch_point(uart_received_bytes)); ESP_LOGV(TAG, "UART - Log - Byte[%i]: %i", i, uart_received_bytes[i]);
}
if (this->is_valid_data(uart_received_bytes)) {
this->send_touch_(this->get_touch_point(uart_received_bytes));
}
} }
void TxUltimateEasy::dump_config() { ESP_LOGCONFIG(TAG, "Tx Ultimate Easy"); } void TxUltimateEasy::dump_config() {
ESP_LOGCONFIG(TAG, "TX Ultimate Easy");
}
void TxUltimateEasy::send_touch_(TouchPoint tp) { void TxUltimateEasy::send_touch_(TouchPoint tp) {
this->trigger_touch_event_.trigger(tp); this->trigger_touch_event_.trigger(tp);
switch (tp.state) { switch (tp.state) {
case TOUCH_STATE_RELEASE: case TOUCH_STATE_RELEASE:
if (tp.x >= 17) { if (tp.x >= 17) {
tp.x = tp.x - 16; tp.x -= 16;
ESP_LOGV(TAG, "Long touch - Released (x=%d)", tp.x); ESP_LOGV(TAG, "Long touch - Released (x=%d)", tp.x);
this->trigger_long_touch_release_.trigger(tp); this->trigger_long_touch_release_.trigger(tp);
} else { } else {
@@ -78,55 +89,51 @@ namespace esphome {
} }
} }
bool TxUltimateEasy::is_valid_data(int uart_received_bytes[]) { bool TxUltimateEasy::is_valid_data(const std::array<int, UART_RECEIVED_BYTES_SIZE> &uart_received_bytes) {
bool valid = true; if (uart_received_bytes[0] != HEADER_BYTE_1 ||
if (!(uart_received_bytes[0] == 170 && uart_received_bytes[1] == 85 && uart_received_bytes[2] == 1 && uart_received_bytes[3] == 2)) return false; uart_received_bytes[1] != HEADER_BYTE_2 ||
uart_received_bytes[2] != VALID_DATA_BYTE_2 ||
int state = get_touch_state(uart_received_bytes); uart_received_bytes[3] != VALID_DATA_BYTE_3) {
if (state != TOUCH_STATE_PRESS &&
state != TOUCH_STATE_RELEASE &&
state != TOUCH_STATE_SWIPE_LEFT &&
state != TOUCH_STATE_SWIPE_RIGHT &&
state != TOUCH_STATE_MULTI_TOUCH)
return false; return false;
}
if (uart_received_bytes[6] < 0 && state != TOUCH_STATE_MULTI_TOUCH) return false; int state = this->get_touch_state(uart_received_bytes);
return (state == TOUCH_STATE_PRESS ||
return true; state == TOUCH_STATE_RELEASE ||
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);
} }
int TxUltimateEasy::get_touch_position_x(int uart_received_bytes[]) { int TxUltimateEasy::get_touch_position_x(const std::array<int, UART_RECEIVED_BYTES_SIZE> &uart_received_bytes) {
int state = uart_received_bytes[4]; switch (uart_received_bytes[4]) {
switch (state) {
case TOUCH_STATE_RELEASE: case TOUCH_STATE_RELEASE:
case TOUCH_STATE_MULTI_TOUCH: case TOUCH_STATE_MULTI_TOUCH:
case TOUCH_STATE_SWIPE_LEFT: case TOUCH_STATE_SWIPE_LEFT:
case TOUCH_STATE_SWIPE_RIGHT: case TOUCH_STATE_SWIPE_RIGHT:
return uart_received_bytes[5]; return uart_received_bytes[5];
break;
default: default:
return uart_received_bytes[6]; return uart_received_bytes[6];
break;
} }
} }
int TxUltimateEasy::get_touch_state(int uart_received_bytes[]) { int TxUltimateEasy::get_touch_state(const std::array<int, UART_RECEIVED_BYTES_SIZE> &uart_received_bytes) {
int state = uart_received_bytes[4]; int state = uart_received_bytes[4];
if (state == TOUCH_STATE_PRESS && uart_received_bytes[5] != 0) state = TOUCH_STATE_RELEASE; 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_RELEASE && uart_received_bytes[5] == TOUCH_STATE_MULTI_TOUCH) state = TOUCH_STATE_MULTI_TOUCH;
if (state == TOUCH_STATE_SWIPE) { if (state == TOUCH_STATE_SWIPE) {
if (uart_received_bytes[5] == TOUCH_STATE_SWIPE_RIGHT) state = TOUCH_STATE_SWIPE_RIGHT; state = (uart_received_bytes[5] == TOUCH_STATE_SWIPE_RIGHT) ? TOUCH_STATE_SWIPE_RIGHT :
else if (uart_received_bytes[5] == TOUCH_STATE_SWIPE_LEFT) state = TOUCH_STATE_SWIPE_LEFT; (uart_received_bytes[5] == TOUCH_STATE_SWIPE_LEFT) ? TOUCH_STATE_SWIPE_LEFT : state;
} }
return state; return state;
} }
TouchPoint TxUltimateEasy::get_touch_point(int uart_received_bytes[]) { TouchPoint TxUltimateEasy::get_touch_point(const std::array<int, UART_RECEIVED_BYTES_SIZE> &uart_received_bytes) {
TouchPoint tp; TouchPoint tp;
tp.x = get_touch_position_x(uart_received_bytes); tp.x = this->get_touch_position_x(uart_received_bytes);
tp.state = get_touch_state(uart_received_bytes); tp.state = this->get_touch_state(uart_received_bytes);
tp.state_str = "Unknown";
switch (tp.state) { switch (tp.state) {
case TOUCH_STATE_RELEASE: case TOUCH_STATE_RELEASE:
tp.state_str = "RELEASE"; tp.state_str = "RELEASE";
@@ -146,6 +153,9 @@ namespace esphome {
case TOUCH_STATE_SWIPE_LEFT: case TOUCH_STATE_SWIPE_LEFT:
tp.state_str = "SWIPE_LEFT"; tp.state_str = "SWIPE_LEFT";
break; break;
default:
tp.state_str = "Unknown";
break;
} }
return tp; return tp;
} }

View File

@@ -1,3 +1,5 @@
// tx_ultimate_easy.h
#pragma once #pragma once
#include "esphome/core/automation.h" #include "esphome/core/automation.h"
@@ -7,15 +9,27 @@
#include "esphome/components/binary_sensor/binary_sensor.h" #include "esphome/components/binary_sensor/binary_sensor.h"
#include "esphome/components/switch/switch.h" #include "esphome/components/switch/switch.h"
#include "esphome/components/light/addressable_light.h" #include "esphome/components/light/addressable_light.h"
#include <array>
namespace esphome { namespace esphome {
namespace tx_ultimate_easy { namespace tx_ultimate_easy {
const static uint8_t TOUCH_STATE_RELEASE = 1; // Touch State Constants
const static uint8_t TOUCH_STATE_PRESS = 2; constexpr uint8_t TOUCH_STATE_RELEASE = 0x01;
const static uint8_t TOUCH_STATE_SWIPE = 3; constexpr uint8_t TOUCH_STATE_PRESS = 0x02;
const static uint8_t TOUCH_STATE_MULTI_TOUCH = 11; constexpr uint8_t TOUCH_STATE_SWIPE = 0x03;
const static uint8_t TOUCH_STATE_SWIPE_RIGHT = 12; constexpr uint8_t TOUCH_STATE_MULTI_TOUCH = 0x0B;
const static uint8_t TOUCH_STATE_SWIPE_LEFT = 13; constexpr uint8_t TOUCH_STATE_SWIPE_RIGHT = 0x0C;
constexpr uint8_t TOUCH_STATE_SWIPE_LEFT = 0x0D;
// UART Constants
constexpr int UART_RECEIVED_BYTES_SIZE = 15;
constexpr int HEADER_BYTE_1 = 0xAA;
constexpr int HEADER_BYTE_2 = 0x55;
constexpr int VALID_DATA_BYTE_2 = 0x01;
constexpr int VALID_DATA_BYTE_3 = 0x02;
// Log tag
static const char *TAG = "tx_ultimate_easy";
struct TouchPoint { struct TouchPoint {
int8_t x = -1; int8_t x = -1;
@@ -41,12 +55,12 @@ namespace esphome {
protected: protected:
void send_touch_(TouchPoint tp); void send_touch_(TouchPoint tp);
void handle_touch(int bytes[]); void handle_touch(const std::array<int, UART_RECEIVED_BYTES_SIZE> &bytes);
TouchPoint get_touch_point(int bytes[]); TouchPoint get_touch_point(const std::array<int, UART_RECEIVED_BYTES_SIZE> &bytes);
bool is_valid_data(int bytes[]); bool is_valid_data(const std::array<int, UART_RECEIVED_BYTES_SIZE> &bytes);
int get_touch_position_x(int bytes[]); int get_touch_position_x(const std::array<int, UART_RECEIVED_BYTES_SIZE> &bytes);
int get_touch_state(int bytes[]); int get_touch_state(const std::array<int, UART_RECEIVED_BYTES_SIZE> &bytes);
Trigger<TouchPoint> trigger_touch_event_; Trigger<TouchPoint> trigger_touch_event_;
Trigger<TouchPoint> trigger_touch_; Trigger<TouchPoint> trigger_touch_;