Fix with suggestions

This commit is contained in:
Silvio 2021-08-18 13:05:15 +02:00
parent 7f6e7ea16b
commit bc618f20cf
3 changed files with 35 additions and 38 deletions

View file

@ -10,17 +10,12 @@ AUTO_LOAD = ["sensor", "text_sensor", "binary_sensor"]
BSM_DALY_ID = "bms_daly_id" BSM_DALY_ID = "bms_daly_id"
daly_bms = cg.esphome_ns.namespace("daly_bms") daly_bms = cg.esphome_ns.namespace("daly_bms")
DalyBmsComponent = daly_bms.class_("DalyBmsComponent", uart.UARTDevice, cg.Component) DalyBmsComponent = daly_bms.class_("DalyBmsComponent", cg.PollingComponent, uart.UARTDevice)
CONFIG_SCHEMA = cv.All( CONFIG_SCHEMA = (
cv.Schema( cv.Schema({cv.GenerateID(): cv.declare_id(DalyBmsComponent)})
{
cv.GenerateID(): cv.declare_id(DalyBmsComponent),
}
)
.extend(cv.COMPONENT_SCHEMA)
.extend(uart.UART_DEVICE_SCHEMA) .extend(uart.UART_DEVICE_SCHEMA)
.extend(cv.polling_component_schema("30s")), .extend(cv.polling_component_schema("30s"))
) )

View file

@ -7,14 +7,15 @@ namespace daly_bms {
static const char *const TAG = "daly_bms"; static const char *const TAG = "daly_bms";
static const unsigned char DALY_TEMPERATURE_OFFSET = 40; static const uint8_t DALY_TEMPERATURE_OFFSET = 40;
static const uint16_t DALY_CURRENT_OFFSET = 30000;
static const unsigned char DALY_REQUEST_BATTERY_LEVEL = 0x90; static const uint8_t DALY_REQUEST_BATTERY_LEVEL = 0x90;
static const unsigned char DALY_REQUEST_MIN_MAX_VOLTAGE = 0x91; static const uint8_t DALY_REQUEST_MIN_MAX_VOLTAGE = 0x91;
static const unsigned char DALY_REQUEST_MIN_MAX_TEMPERATURE = 0x92; static const uint8_t DALY_REQUEST_MIN_MAX_TEMPERATURE = 0x92;
static const unsigned char DALY_REQUEST_MOS = 0x93; static const uint8_t DALY_REQUEST_MOS = 0x93;
static const unsigned char DALY_REQUEST_STATUS = 0x94; static const uint8_t DALY_REQUEST_STATUS = 0x94;
static const unsigned char DALY_REQUEST_TEMPERATURE = 0x96; static const uint8_t DALY_REQUEST_TEMPERATURE = 0x96;
void DalyBmsComponent::setup() {} void DalyBmsComponent::setup() {}
@ -31,19 +32,19 @@ void DalyBmsComponent::update() {
this->request_data(DALY_REQUEST_STATUS); this->request_data(DALY_REQUEST_STATUS);
this->request_data(DALY_REQUEST_TEMPERATURE); this->request_data(DALY_REQUEST_TEMPERATURE);
std::vector<unsigned char> get_battery_level_data; uint8_t *get_battery_level_data;
int available_data = this->available(); int available_data = this->available();
if (available_data >= 13) { if (available_data >= 13) {
get_battery_level_data.resize(available_data); get_battery_level_data = (uint8_t *) malloc(available_data);
this->read_array(&get_battery_level_data[0], available_data); this->read_array(get_battery_level_data, available_data);
this->decode_data(&get_battery_level_data[0], available_data); this->decode_data(get_battery_level_data, available_data);
} }
} }
float DalyBmsComponent::get_setup_priority() const { return setup_priority::DATA; } float DalyBmsComponent::get_setup_priority() const { return setup_priority::DATA; }
void DalyBmsComponent::request_data(unsigned char data_id) { void DalyBmsComponent::request_data(uint8_t data_id) {
unsigned char request_message[13]; uint8_t request_message[13];
request_message[0] = 0xA5; // Start Flag request_message[0] = 0xA5; // Start Flag
request_message[1] = 0x80; // Communication Module Address request_message[1] = 0x80; // Communication Module Address
@ -57,25 +58,25 @@ void DalyBmsComponent::request_data(unsigned char data_id) {
request_message[9] = 0x00; // | request_message[9] = 0x00; // |
request_message[10] = 0x00; // | request_message[10] = 0x00; // |
request_message[11] = 0x00; // Empty Data request_message[11] = 0x00; // Empty Data
request_message[12] = (unsigned char) (request_message[0] + request_message[1] + request_message[2] + request_message[12] = (uint8_t)(request_message[0] + request_message[1] + request_message[2] +
request_message[3]); // Checksum (Lower byte of the other bytes sum) request_message[3]); // Checksum (Lower byte of the other bytes sum)
this->write_array(request_message, sizeof(request_message)); this->write_array(request_message, sizeof(request_message));
this->flush(); this->flush();
} }
void DalyBmsComponent::decode_data(unsigned char *data, int length) { void DalyBmsComponent::decode_data(uint8_t *data, int length) {
unsigned char *start_flag_position; uint8_t *start_flag_position;
while (data != nullptr) { while (data != NULL) {
start_flag_position = (unsigned char *) strchr((const char *) data, 0xA5); start_flag_position = (uint8_t *) strchr((const char *) data, 0xA5);
if (start_flag_position != nullptr) { if (start_flag_position != nullptr) {
length = length - (start_flag_position - data); length = length - (start_flag_position - data);
data = start_flag_position; data = start_flag_position;
if (length >= 13 && data[1] == 0x01) { if (length >= 13 && data[1] == 0x01) {
unsigned char checksum; uint8_t checksum;
int sum = 0; int sum = 0;
for (int i = 0; i < 12; i++) { for (int i = 0; i < 12; i++) {
sum += data[i]; sum += data[i];
@ -86,25 +87,26 @@ void DalyBmsComponent::decode_data(unsigned char *data, int length) {
switch (data[2]) { switch (data[2]) {
case DALY_REQUEST_BATTERY_LEVEL: case DALY_REQUEST_BATTERY_LEVEL:
if (this->voltage_sensor_) { if (this->voltage_sensor_) {
this->voltage_sensor_->publish_state(((float) ((data[4] << 8) | data[5]) / 10)); this->voltage_sensor_->publish_state((float) encode_uint16(data[4], data[5]) / 10);
} }
if (this->current_sensor_) { if (this->current_sensor_) {
this->current_sensor_->publish_state(((float) (((data[8] << 8) | data[9]) - 30000) / 10)); this->current_sensor_->publish_state(
(float) ((encode_uint16(data[8], data[9]) - DALY_CURRENT_OFFSET) / 10));
} }
if (this->battery_level_sensor_) { if (this->battery_level_sensor_) {
this->battery_level_sensor_->publish_state(((float) ((data[10] << 8) | data[11]) / 10)); this->battery_level_sensor_->publish_state((float) encode_uint16(data[10], data[11]) / 10);
} }
break; break;
case DALY_REQUEST_MIN_MAX_VOLTAGE: case DALY_REQUEST_MIN_MAX_VOLTAGE:
if (this->max_cell_voltage_) { if (this->max_cell_voltage_) {
this->max_cell_voltage_->publish_state((float) ((data[4] << 8) | data[5]) / 1000); this->max_cell_voltage_->publish_state((float) encode_uint16(data[4], data[5]) / 1000);
} }
if (this->max_cell_volatge_number_) { if (this->max_cell_volatge_number_) {
this->max_cell_volatge_number_->publish_state(data[6]); this->max_cell_volatge_number_->publish_state(data[6]);
} }
if (this->min_cell_voltage_) { if (this->min_cell_voltage_) {
this->min_cell_voltage_->publish_state((float) ((data[7] << 8) | data[8]) / 1000); this->min_cell_voltage_->publish_state((float) encode_uint16(data[7], data[8]) / 1000);
} }
if (this->min_cell_voltage_number_) { if (this->min_cell_voltage_number_) {
this->min_cell_voltage_number_->publish_state(data[9]); this->min_cell_voltage_number_->publish_state(data[9]);
@ -149,8 +151,8 @@ void DalyBmsComponent::decode_data(unsigned char *data, int length) {
this->discharging_mos_enabled_->publish_state(data[6]); this->discharging_mos_enabled_->publish_state(data[6]);
} }
if (this->remaining_capacity_) { if (this->remaining_capacity_) {
this->remaining_capacity_->publish_state( this->remaining_capacity_->publish_state((float) encode_uint32(data[8], data[9], data[10], data[11]) /
(float) ((data[8] << 24) | (data[9] << 16) | (data[10] << 8) | data[11]) / 1000); 1000);
} }
break; break;

View file

@ -53,8 +53,8 @@ class DalyBmsComponent : public PollingComponent, public uart::UARTDevice {
float get_setup_priority() const override; float get_setup_priority() const override;
protected: protected:
void request_data(unsigned char data_id); void request_data(uint8_t data_id);
void decode_data(unsigned char *data, int length); void decode_data(uint8_t *data, int length);
sensor::Sensor *voltage_sensor_{nullptr}; sensor::Sensor *voltage_sensor_{nullptr};
sensor::Sensor *current_sensor_{nullptr}; sensor::Sensor *current_sensor_{nullptr};