Skip to content

Commit 249bc47

Browse files
authored
Add Neey/EnerKey EK-24S4EB balancer support (Closes: syssi#444) (syssi#452)
1 parent e3c559f commit 249bc47

File tree

1 file changed

+44
-10
lines changed

1 file changed

+44
-10
lines changed

components/heltec_balancer_ble/heltec_balancer_ble.cpp

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,6 @@ uint8_t crc(const uint8_t data[], const uint16_t len) {
8282
return crc;
8383
}
8484

85-
uint8_t checksum_xor(const uint8_t data[], const uint16_t len) {
86-
uint8_t checksum = 0;
87-
for (uint16_t i = 0; i < len; i++) {
88-
checksum = checksum ^ data[i];
89-
}
90-
return checksum;
91-
}
92-
9385
void HeltecBalancerBle::dump_config() { // NOLINT(google-readability-function-size,readability-function-size)
9486
ESP_LOGCONFIG(TAG, "HeltecBalancerBle");
9587
LOG_SENSOR("", "Minimum Cell Voltage", this->min_cell_voltage_sensor_);
@@ -173,6 +165,30 @@ void HeltecBalancerBle::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt
173165
this->parent_->address_str().c_str());
174166
break;
175167
}
168+
169+
// Services and characteristic of a GW-24S4EB, HW-2.8.0, SW-1.1.0, V1.0.0, 20210915 (3C:A5:51:XX:XX:XX)
170+
//
171+
// Service UUID: 0x1800
172+
// start_handle: 0x1 end_handle: 0x7
173+
// Service UUID: 0xFFE0
174+
// start_handle: 0x8 end_handle: 0xffff
175+
// Connected
176+
// characteristic 0xFFE1, handle 0xa, properties 0x1c
177+
// characteristic 0xFFE2, handle 0xd, properties 0x1c
178+
// characteristic 0xFFE3, handle 0x10, properties 0xc
179+
// cfg_mtu status 0, mtu 244
180+
181+
// Services and characteristic of a EK-24S4EB, HW-3.2.0, ZH-1.2.9, V1.2.9, 20230608 (3C:A5:51:XX:XX:XX)
182+
//
183+
// Service UUID: 0x1800
184+
// start_handle: 0x1 end_handle: 0x7
185+
// Service UUID: 0xFFE0
186+
// start_handle: 0x8 end_handle: 0xffff
187+
// Connected
188+
// characteristic 0xFFE1, handle 0xa, properties 0x1c
189+
// characteristic 0xFFE2, handle 0xd, properties 0x1c
190+
// characteristic 0xFFE3, handle 0x10, properties 0xc
191+
// cfg_mtu status 0, mtu 244
176192
this->char_handle_ = chr->handle;
177193

178194
auto status = esp_ble_gattc_register_for_notify(this->parent()->get_gattc_if(), this->parent()->get_remote_bda(),
@@ -689,23 +705,41 @@ void HeltecBalancerBle::decode_device_info_(const std::vector<uint8_t> &data) {
689705

690706
bool HeltecBalancerBle::send_command(uint8_t function, uint8_t command, uint8_t register_address, uint32_t value) {
691707
// Request device info:
708+
//
709+
// (GW-24S4EB, checksum_xor)
692710
// 0xAA 0x55 0x11 0x01 0x01 0x00 0x14 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0xFA 0xFF
711+
// 0xAA 0x55 0x11 0x01 0x01 0x00 0x14 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x26 0xFF
712+
// (EK-24S4EB, crc)
693713
//
694714
// Request cell info:
715+
//
716+
// (GW-24S4EB, checksum_xor, wrong data_len position)
695717
// 0xAA 0x55 0x11 0x01 0x02 0x00 0x00 0x14 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0xF9 0xFF
718+
// 0xAA 0x55 0x11 0x01 0x02 0x00 0x14 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x27 0xFF
719+
// (EK-24S4EB, crc)
696720
//
697721
// Request factory settings:
722+
//
723+
// (GW-24S4EB, checksum_xor)
698724
// 0xAA 0x55 0x11 0x01 0x03 0x00 0x14 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0xF8 0xFF
699725
//
700726
// Request settings:
727+
//
728+
// (GW-24S4EB, checksum_xor)
701729
// 0xAA 0x55 0x11 0x01 0x04 0x00 0x14 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0xFF 0xFF
730+
// 0xAA 0x55 0x11 0x01 0x04 0x00 0x14 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x29 0xFF
731+
// (EK-24S4EB, crc)
702732
//
703733
// Enable balancer:
734+
//
735+
// (GW-24S4EB, checksum_xor)
704736
// 0xAA 0x55 0x11 0x00 0x05 0x0D 0x14 0x00 0x01 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0xF3 0xFF
705737
//
706738
// Disable balancer:
739+
//
740+
// (GW-24S4EB, checksum_xor)
707741
// 0xAA 0x55 0x11 0x00 0x05 0x0D 0x14 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0x00 0xF2 0xFF
708-
uint16_t length = (command == COMMAND_CELL_INFO) ? 0x1400 : 0x0014; // Replicate behavior or bug of the android app
742+
uint16_t length = 0x0014;
709743

710744
uint8_t frame[20];
711745
frame[0] = SOF_REQUEST_BYTE1; // Start sequence
@@ -726,7 +760,7 @@ bool HeltecBalancerBle::send_command(uint8_t function, uint8_t command, uint8_t
726760
frame[15] = 0x00; // Data Byte 8
727761
frame[16] = 0x00; // Data Byte 9
728762
frame[17] = 0x00; // Data Byte 10
729-
frame[18] = checksum_xor(frame, sizeof(frame) - 2);
763+
frame[18] = crc(frame, sizeof(frame) - 2);
730764
frame[19] = END_OF_FRAME; // End sequence
731765

732766
ESP_LOGD(TAG, "Write register: %s", format_hex_pretty(frame, sizeof(frame)).c_str());

0 commit comments

Comments
 (0)