mirror of
https://github.com/joBr99/nspanel-lovelace-ui.git
synced 2025-12-22 15:34:26 +01:00
added esphome component and hmi file
This commit is contained in:
62
components/nextion_custom/__init__.py
Normal file
62
components/nextion_custom/__init__.py
Normal file
@@ -0,0 +1,62 @@
|
||||
from esphome import automation
|
||||
import esphome.config_validation as cv
|
||||
import esphome.codegen as cg
|
||||
|
||||
from esphome.components import uart, time, switch, sensor
|
||||
from esphome.const import (
|
||||
CONF_ID,
|
||||
CONF_NAME,
|
||||
CONF_ON_CLICK,
|
||||
CONF_TEMPERATURE,
|
||||
CONF_THEN,
|
||||
CONF_TIME_ID,
|
||||
CONF_TYPE,
|
||||
CONF_TRIGGER_ID,
|
||||
)
|
||||
|
||||
AUTO_LOAD = ["text_sensor"]
|
||||
CODEOWNERS = ["@joBr99"]
|
||||
DEPENDENCIES = ["uart", "wifi", "esp32"]
|
||||
|
||||
|
||||
nextion_custom_ns = cg.esphome_ns.namespace("NextionCustom")
|
||||
NextionCustom = nextion_custom_ns.class_("NextionCustom", cg.Component, uart.UARTDevice)
|
||||
|
||||
|
||||
NextionCustomMsgIncomingTrigger = nextion_custom_ns.class_(
|
||||
"NextionCustomMsgIncomingTrigger",
|
||||
automation.Trigger.template(cg.std_string)
|
||||
)
|
||||
|
||||
CONF_INCOMING_MSG = 'on_incoming_msg'
|
||||
|
||||
|
||||
CONFIG_SCHEMA = cv.All(
|
||||
cv.Schema(
|
||||
{
|
||||
cv.GenerateID(): cv.declare_id(NextionCustom),
|
||||
cv.Required(CONF_INCOMING_MSG): automation.validate_automation(
|
||||
cv.Schema(
|
||||
{
|
||||
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(NextionCustomMsgIncomingTrigger),
|
||||
}
|
||||
)
|
||||
),
|
||||
}
|
||||
)
|
||||
.extend(uart.UART_DEVICE_SCHEMA)
|
||||
.extend(cv.COMPONENT_SCHEMA),
|
||||
cv.only_with_arduino,
|
||||
)
|
||||
|
||||
|
||||
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)
|
||||
|
||||
for conf in config.get(CONF_INCOMING_MSG, []):
|
||||
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], var)
|
||||
await automation.build_automation(trigger, [(cg.std_string, "x")], conf)
|
||||
|
||||
cg.add_define("USE_NEXTIONCUSTOM")
|
||||
20
components/nextion_custom/automation.h
Normal file
20
components/nextion_custom/automation.h
Normal file
@@ -0,0 +1,20 @@
|
||||
#pragma once
|
||||
|
||||
#include <utility>
|
||||
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/core/automation.h"
|
||||
#include "nextion_custom.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace NextionCustom {
|
||||
|
||||
class NextionCustomMsgIncomingTrigger : public Trigger<std::string> {
|
||||
public:
|
||||
explicit NextionCustomMsgIncomingTrigger(NextionCustom *parent) {
|
||||
parent->add_incoming_msg_callback([this](const std::string &value) { this->trigger(value); });
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace NextionCustom
|
||||
} // namespace esphome
|
||||
171
components/nextion_custom/nextion_custom.cpp
Normal file
171
components/nextion_custom/nextion_custom.cpp
Normal file
@@ -0,0 +1,171 @@
|
||||
#include "nextion_custom.h"
|
||||
|
||||
#ifdef USE_ESP32_FRAMEWORK_ARDUINO
|
||||
|
||||
#include "esphome/components/wifi/wifi_component.h"
|
||||
#include "esphome/core/application.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
#include "esphome/core/util.h"
|
||||
#include "esphome/core/version.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace NextionCustom {
|
||||
|
||||
static const char *const TAG = "nextion_custom";
|
||||
|
||||
static const char *const SUCCESS_RESPONSE = "{\"error\":0}";
|
||||
|
||||
static const uint8_t WAKE_RESPONSE[7] = {0xFF, 0xFF, 0xFF, 0x88, 0xFF, 0xFF, 0xFF};
|
||||
|
||||
void NextionCustom::setup() {
|
||||
}
|
||||
|
||||
void NextionCustom::loop() {
|
||||
uint8_t d;
|
||||
|
||||
while (this->available()) {
|
||||
this->read_byte(&d);
|
||||
this->buffer_.push_back(d);
|
||||
//ESP_LOGD(TAG, "ReceivedD: 0x%02x", d);
|
||||
if (!this->process_data_()) {
|
||||
ESP_LOGD(TAG, "Received: 0x%02x", d);
|
||||
this->buffer_.clear();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
bool NextionCustom::process_data_() {
|
||||
uint32_t at = this->buffer_.size() - 1;
|
||||
auto *data = &this->buffer_[0];
|
||||
uint8_t new_byte = data[at];
|
||||
|
||||
// if (data[0] == WAKE_RESPONSE[0]) { // Screen wake message
|
||||
// if (at < 6)
|
||||
// return new_byte == WAKE_RESPONSE[at];
|
||||
// if (new_byte == WAKE_RESPONSE[at]) {
|
||||
// ESP_LOGD(TAG, "Screen wake message received");
|
||||
// this->initialize();
|
||||
// return false;
|
||||
// }
|
||||
// return false;
|
||||
// }
|
||||
//
|
||||
// Byte 0: HEADER1 (always 0x55)
|
||||
if (at == 0)
|
||||
return new_byte == 0x55;
|
||||
// Byte 1: HEADER2 (always 0xBB)
|
||||
if (at == 1)
|
||||
return new_byte == 0xBB;
|
||||
|
||||
// length
|
||||
if (at == 2){
|
||||
return true;
|
||||
}
|
||||
uint8_t length = data[2];
|
||||
|
||||
// Wait until all data comes in
|
||||
if (at - 3 < length){
|
||||
//ESP_LOGD(TAG, "Message Length: (%d/%d)", at - 2, length);
|
||||
//ESP_LOGD(TAG, "Received: 0x%02x", new_byte);
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
// Wait for CRC
|
||||
if (at == 2 + length || at == 2 + length + 1)
|
||||
return true;
|
||||
|
||||
uint16_t crc16 = encode_uint16(data[3 + length + 1], data[3 + length]);
|
||||
uint16_t calculated_crc16 = NextionCustom::crc16(data, 3 + length);
|
||||
|
||||
if (crc16 != calculated_crc16) {
|
||||
ESP_LOGW(TAG, "Received invalid message checksum %02X!=%02X", crc16, calculated_crc16);
|
||||
return false;
|
||||
}else{
|
||||
ESP_LOGD(TAG, "Received valid message checksum %02X==%02X", crc16, calculated_crc16);
|
||||
}
|
||||
|
||||
const uint8_t *message_data = data + 3;
|
||||
std::string message(message_data, message_data + length);
|
||||
|
||||
#if ESPHOME_VERSION_CODE >= VERSION_CODE(2022, 1, 0)
|
||||
ESP_LOGD(TAG, "Received NextionCustom: PAYLOAD=%s RAW=[%s]", message.c_str(),
|
||||
format_hex_pretty(message_data, length).c_str());
|
||||
#else
|
||||
ESP_LOGD(TAG, "Received NextionCustom: PAYLOAD=%s RAW=[%s]", message.c_str(),
|
||||
hexencode(message_data, length).c_str());
|
||||
#endif
|
||||
this->process_command_(message);
|
||||
return false;
|
||||
}
|
||||
|
||||
void NextionCustom::process_command_(const std::string &message) {
|
||||
this->incoming_msg_callback_.call(message);
|
||||
}
|
||||
|
||||
void NextionCustom::add_incoming_msg_callback(std::function<void(std::string)> callback) {
|
||||
this->incoming_msg_callback_.add(std::move(callback));
|
||||
}
|
||||
|
||||
void NextionCustom::dump_config() { ESP_LOGCONFIG(TAG, "NextionCustom:"); }
|
||||
|
||||
|
||||
void NextionCustom::send_command_(const std::string &command) {
|
||||
ESP_LOGD(TAG, "Sending: %s", command.c_str());
|
||||
this->write_str(command.c_str());
|
||||
const uint8_t to_send[3] = {0xFF, 0xFF, 0xFF};
|
||||
this->write_array(to_send, sizeof(to_send));
|
||||
}
|
||||
|
||||
void NextionCustom::send_special_hex_cmd(const std::string &command) {
|
||||
ESP_LOGD(TAG, "Sending: special hex cmd");
|
||||
|
||||
const uint8_t to_send[3] = {0x09, 0x19, 0x08};
|
||||
this->write_array(to_send, sizeof(to_send));
|
||||
//this->write_str(" fffb 0002 0000 0020");
|
||||
this->write_str(command.c_str());
|
||||
const uint8_t to_send2[3] = {0xFF, 0xFF, 0xFF};
|
||||
this->write_array(to_send2, sizeof(to_send2));
|
||||
}
|
||||
|
||||
void NextionCustom::send_custom_command(const std::string &command) {
|
||||
ESP_LOGD(TAG, "Sending custom command: %s", command.c_str());
|
||||
std::vector<uint8_t> data = {0x55, 0xBB};
|
||||
data.push_back(command.length() & 0xFF);
|
||||
//data.push_back((command.length() >> 8) & 0xFF);
|
||||
data.insert(data.end(), command.begin(), command.end());
|
||||
auto crc = crc16(data.data(), data.size());
|
||||
data.push_back(crc & 0xFF);
|
||||
data.push_back((crc >> 8) & 0xFF);
|
||||
this->write_array(data);
|
||||
|
||||
#if ESPHOME_VERSION_CODE >= VERSION_CODE(2022, 1, 0)
|
||||
ESP_LOGD(TAG, "Send NextionCustom: PAYLOAD=%s RAW=[%s]", command.c_str(),
|
||||
format_hex_pretty(&data[0], data.size()).c_str());
|
||||
#else
|
||||
ESP_LOGD(TAG, "Send NextionCustom: PAYLOAD=%s RAW=[%s]", command.c_str(),
|
||||
hexencode(&data[0], data.size()).c_str());
|
||||
#endif
|
||||
}
|
||||
|
||||
uint16_t NextionCustom::crc16(const uint8_t *data, uint16_t len) {
|
||||
uint16_t crc = 0xFFFF;
|
||||
while (len--) {
|
||||
crc ^= *data++;
|
||||
for (uint8_t i = 0; i < 8; i++) {
|
||||
if ((crc & 0x01) != 0) {
|
||||
crc >>= 1;
|
||||
crc ^= 0xA001;
|
||||
} else {
|
||||
crc >>= 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
return crc;
|
||||
}
|
||||
|
||||
} // namespace NextionCustom
|
||||
} // namespace esphome
|
||||
|
||||
#endif // USE_ESP32_FRAMEWORK_ARDUINO
|
||||
52
components/nextion_custom/nextion_custom.h
Normal file
52
components/nextion_custom/nextion_custom.h
Normal file
@@ -0,0 +1,52 @@
|
||||
#pragma once
|
||||
|
||||
#ifdef USE_ESP32_FRAMEWORK_ARDUINO
|
||||
|
||||
#include <utility>
|
||||
|
||||
#include "esphome/components/text_sensor/text_sensor.h"
|
||||
#include "esphome/components/switch/switch.h"
|
||||
//#include "esphome/components/time/real_time_clock.h"
|
||||
#include "esphome/components/uart/uart.h"
|
||||
#include "esphome/core/automation.h"
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/core/defines.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace NextionCustom {
|
||||
|
||||
class NextionCustom : public Component, public uart::UARTDevice {
|
||||
public:
|
||||
void setup() override;
|
||||
void loop() override;
|
||||
|
||||
float get_setup_priority() const override { return setup_priority::DATA; }
|
||||
|
||||
void dump_config() override;
|
||||
|
||||
void send_custom_command(const std::string &command);
|
||||
|
||||
|
||||
void send_special_hex_cmd(const std::string &command);
|
||||
|
||||
void add_incoming_msg_callback(std::function<void(std::string)> callback);
|
||||
|
||||
void send_command_(const std::string &command);
|
||||
|
||||
|
||||
protected:
|
||||
static uint16_t crc16(const uint8_t *data, uint16_t len);
|
||||
|
||||
bool process_data_();
|
||||
void process_command_(const std::string &message);
|
||||
|
||||
CallbackManager<void(std::string)> incoming_msg_callback_;
|
||||
|
||||
std::vector<uint8_t> buffer_;
|
||||
};
|
||||
|
||||
|
||||
} // namespace NextionCustom
|
||||
} // namespace esphome
|
||||
|
||||
#endif // USE_ESP32_FRAMEWORK_ARDUINO
|
||||
Reference in New Issue
Block a user