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

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

View File

@@ -1,3 +1,5 @@
// tx_ultimate_easy.h
#pragma once
#include "esphome/core/automation.h"
@@ -7,15 +9,27 @@
#include "esphome/components/binary_sensor/binary_sensor.h"
#include "esphome/components/switch/switch.h"
#include "esphome/components/light/addressable_light.h"
#include <array>
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_MULTI_TOUCH = 11;
const static uint8_t TOUCH_STATE_SWIPE_RIGHT = 12;
const static uint8_t TOUCH_STATE_SWIPE_LEFT = 13;
// Touch State Constants
constexpr uint8_t TOUCH_STATE_RELEASE = 0x01;
constexpr uint8_t TOUCH_STATE_PRESS = 0x02;
constexpr uint8_t TOUCH_STATE_SWIPE = 0x03;
constexpr uint8_t TOUCH_STATE_MULTI_TOUCH = 0x0B;
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 {
int8_t x = -1;
@@ -41,12 +55,12 @@ namespace esphome {
protected:
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[]);
bool is_valid_data(int bytes[]);
int get_touch_position_x(int bytes[]);
int get_touch_state(int bytes[]);
TouchPoint get_touch_point(const std::array<int, UART_RECEIVED_BYTES_SIZE> &bytes);
bool is_valid_data(const std::array<int, UART_RECEIVED_BYTES_SIZE> &bytes);
int get_touch_position_x(const std::array<int, UART_RECEIVED_BYTES_SIZE> &bytes);
int get_touch_state(const std::array<int, UART_RECEIVED_BYTES_SIZE> &bytes);
Trigger<TouchPoint> trigger_touch_event_;
Trigger<TouchPoint> trigger_touch_;