44
55#include " sp140/ble.h"
66#include " sp140/ble/ble_ids.h"
7- #include " sp140/ble/ble_utils.h"
87
98namespace {
109
@@ -13,29 +12,6 @@ NimBLEService* pBmsService = nullptr;
1312// Binary packed telemetry characteristic (V1)
1413NimBLECharacteristic* pBMSPackedTelemetry = nullptr ;
1514
16- #ifndef DISABLE_LEGACY_BLE_TELEMETRY
17- // Legacy individual characteristics
18- NimBLECharacteristic* pBMSSOC = nullptr ;
19- NimBLECharacteristic* pBMSVoltage = nullptr ;
20- NimBLECharacteristic* pBMSCurrent = nullptr ;
21- NimBLECharacteristic* pBMSPower = nullptr ;
22- NimBLECharacteristic* pBMSHighCell = nullptr ;
23- NimBLECharacteristic* pBMSLowCell = nullptr ;
24- NimBLECharacteristic* pBMSHighTemp = nullptr ;
25- NimBLECharacteristic* pBMSLowTemp = nullptr ;
26- NimBLECharacteristic* pBMSFailureLevel = nullptr ;
27- NimBLECharacteristic* pBMSVoltageDiff = nullptr ;
28- NimBLECharacteristic* pBMSCellVoltages = nullptr ;
29- NimBLECharacteristic* pBMSChargeMos = nullptr ;
30- NimBLECharacteristic* pBMSDischargeMos = nullptr ;
31- NimBLECharacteristic* pBMSTemperatures = nullptr ;
32-
33- // Track previous values to only notify on change
34- uint8_t lastFailureLevel = 0 ;
35- uint8_t lastChargeMos = 0 ;
36- uint8_t lastDischargeMos = 0 ;
37- #endif // DISABLE_LEGACY_BLE_TELEMETRY
38-
3915} // namespace
4016
4117void initBmsBleService (NimBLEServer* server) {
@@ -58,66 +34,6 @@ void initBmsBleService(NimBLEServer* server) {
5834 reinterpret_cast <uint8_t *>(&initialPacket),
5935 sizeof (BLE_BMS_Telemetry_V1));
6036
61- #ifndef DISABLE_LEGACY_BLE_TELEMETRY
62- // Legacy individual characteristics
63- pBMSSOC = pBmsService->createCharacteristic (
64- NimBLEUUID (BMS_SOC_UUID), NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::NOTIFY);
65-
66- pBMSVoltage = pBmsService->createCharacteristic (
67- NimBLEUUID (BMS_VOLTAGE_UUID), NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::NOTIFY);
68-
69- pBMSCurrent = pBmsService->createCharacteristic (
70- NimBLEUUID (BMS_CURRENT_UUID), NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::NOTIFY);
71-
72- pBMSPower = pBmsService->createCharacteristic (
73- NimBLEUUID (BMS_POWER_UUID), NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::NOTIFY);
74-
75- pBMSHighCell = pBmsService->createCharacteristic (
76- NimBLEUUID (BMS_HIGH_CELL_UUID), NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::NOTIFY);
77-
78- pBMSLowCell = pBmsService->createCharacteristic (
79- NimBLEUUID (BMS_LOW_CELL_UUID), NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::NOTIFY);
80-
81- pBMSHighTemp = pBmsService->createCharacteristic (
82- NimBLEUUID (BMS_HIGH_TEMP_UUID), NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::NOTIFY);
83-
84- pBMSLowTemp = pBmsService->createCharacteristic (
85- NimBLEUUID (BMS_LOW_TEMP_UUID), NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::NOTIFY);
86-
87- pBMSFailureLevel = pBmsService->createCharacteristic (
88- NimBLEUUID (BMS_FAILURE_LEVEL_UUID), NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::NOTIFY);
89-
90- pBMSVoltageDiff = pBmsService->createCharacteristic (
91- NimBLEUUID (BMS_VOLTAGE_DIFF_UUID), NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::NOTIFY);
92-
93- pBMSCellVoltages = pBmsService->createCharacteristic (
94- NimBLEUUID (BMS_CELL_VOLTAGES_UUID), NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::NOTIFY);
95-
96- pBMSChargeMos = pBmsService->createCharacteristic (
97- NimBLEUUID (BMS_CHARGE_MOS_UUID), NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::NOTIFY);
98-
99- pBMSDischargeMos = pBmsService->createCharacteristic (
100- NimBLEUUID (BMS_DISCHARGE_MOS_UUID), NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::NOTIFY);
101-
102- pBMSTemperatures = pBmsService->createCharacteristic (
103- NimBLEUUID (BMS_TEMPERATURES_UUID),
104- NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::NOTIFY);
105-
106- // Ensure characteristics have deterministic startup values.
107- uint16_t initial_cell_values[BMS_CELLS_NUM] = {0 };
108- pBMSCellVoltages->setValue (
109- reinterpret_cast <uint8_t *>(initial_cell_values),
110- BMS_CELLS_NUM * sizeof (uint16_t ));
111-
112- uint8_t initial_flag = 0 ;
113- pBMSChargeMos->setValue (&initial_flag, sizeof (initial_flag));
114- pBMSDischargeMos->setValue (&initial_flag, sizeof (initial_flag));
115-
116- uint8_t initial_temps[17 ] = {0 };
117- initial_temps[0 ] = 0x00 ; // No valid sensors initially (bitmap = 0x00)
118- pBMSTemperatures->setValue (initial_temps, 17 );
119- #endif // DISABLE_LEGACY_BLE_TELEMETRY
120-
12137 pBmsService->start ();
12238}
12339
@@ -156,92 +72,3 @@ void updateBMSPackedTelemetry(const STR_BMS_TELEMETRY_140& telemetry, uint8_t bm
15672 pBMSPackedTelemetry->notify ();
15773 }
15874}
159-
160- #ifndef DISABLE_LEGACY_BLE_TELEMETRY
161- // Legacy individual characteristic updates
162- void updateBMSTelemetry (const STR_BMS_TELEMETRY_140& telemetry) {
163- if (pBmsService == nullptr ) {
164- return ; // Not initialized yet.
165- }
166-
167- float soc = telemetry.soc ;
168- float voltage = telemetry.battery_voltage ;
169- float current = telemetry.battery_current ;
170- float power = telemetry.power ;
171- float highCell = telemetry.highest_cell_voltage ;
172- float lowCell = telemetry.lowest_cell_voltage ;
173- float highTemp = telemetry.highest_temperature ;
174- float lowTemp = telemetry.lowest_temperature ;
175- float voltageDiff = telemetry.voltage_differential ;
176-
177- if (pBMSSOC) pBMSSOC->setValue (soc);
178- if (pBMSVoltage) pBMSVoltage->setValue (voltage);
179- if (pBMSCurrent) pBMSCurrent->setValue (current);
180- if (pBMSPower) pBMSPower->setValue (power);
181- if (pBMSHighCell) pBMSHighCell->setValue (highCell);
182- if (pBMSLowCell) pBMSLowCell->setValue (lowCell);
183- if (pBMSHighTemp) pBMSHighTemp->setValue (highTemp);
184- if (pBMSLowTemp) pBMSLowTemp->setValue (lowTemp);
185- if (pBMSVoltageDiff) pBMSVoltageDiff->setValue (voltageDiff);
186-
187- if (pBMSCellVoltages) {
188- uint16_t cell_millivolts[BMS_CELLS_NUM];
189- for (uint8_t i = 0 ; i < BMS_CELLS_NUM; i++) {
190- cell_millivolts[i] = static_cast <uint16_t >(telemetry.cell_voltages [i] * 1000 .0f );
191- }
192- pBMSCellVoltages->setValue (
193- reinterpret_cast <uint8_t *>(cell_millivolts),
194- BMS_CELLS_NUM * sizeof (uint16_t ));
195- }
196-
197- // Update temperatures characteristic
198- // Format (17 bytes): bitmap(1) + 8×int16_t temperatures (deci-degrees C)
199- // Sensor mapping: [0]=MOS, [1]=Balance, [2]=T1, [3]=T2, [4]=T3, [5]=T4, [6-7]=Reserved
200- if (pBMSTemperatures) {
201- uint8_t temp_buffer[17 ];
202-
203- // Byte 0: Valid sensor bitmap (bit N = sensor N is valid)
204- // 0b00111111 = sensors 0-5 valid (6 temperature sensors)
205- temp_buffer[0 ] = 0b00111111 ;
206-
207- // Bytes 1-16: 8×int16_t temperatures in deci-degrees C (0.1°C resolution)
208- int16_t * temps = reinterpret_cast <int16_t *>(&temp_buffer[1 ]);
209- temps[0 ] = static_cast <int16_t >(telemetry.mos_temperature * 10 .0f ); // [0] MOS
210- temps[1 ] = static_cast <int16_t >(telemetry.balance_temperature * 10 .0f ); // [1] Balance
211- temps[2 ] = static_cast <int16_t >(telemetry.t1_temperature * 10 .0f ); // [2] T1
212- temps[3 ] = static_cast <int16_t >(telemetry.t2_temperature * 10 .0f ); // [3] T2
213- temps[4 ] = static_cast <int16_t >(telemetry.t3_temperature * 10 .0f ); // [4] T3
214- temps[5 ] = static_cast <int16_t >(telemetry.t4_temperature * 10 .0f ); // [5] T4
215- temps[6 ] = 0 ; // [6] Reserved
216- temps[7 ] = 0 ; // [7] Reserved
217-
218- pBMSTemperatures->setValue (temp_buffer, 17 );
219- }
220-
221- // Handle state-change characteristics - only notify on change
222- setAndNotifyOnChange (pBMSFailureLevel, telemetry.battery_fail_level , lastFailureLevel);
223- setAndNotifyOnChange (pBMSChargeMos, static_cast <uint8_t >(telemetry.is_charge_mos ? 1 : 0 ), lastChargeMos);
224- setAndNotifyOnChange (pBMSDischargeMos, static_cast <uint8_t >(telemetry.is_discharge_mos ? 1 : 0 ), lastDischargeMos);
225-
226- if (!deviceConnected) {
227- return ; // No notifications needed without a subscriber.
228- }
229-
230- if (pBMSSOC) pBMSSOC->notify ();
231- if (pBMSVoltage) pBMSVoltage->notify ();
232- if (pBMSCurrent) pBMSCurrent->notify ();
233- if (pBMSPower) pBMSPower->notify ();
234- if (pBMSHighCell) pBMSHighCell->notify ();
235- if (pBMSLowCell) pBMSLowCell->notify ();
236- if (pBMSHighTemp) pBMSHighTemp->notify ();
237- if (pBMSLowTemp) pBMSLowTemp->notify ();
238- if (pBMSVoltageDiff) pBMSVoltageDiff->notify ();
239- if (pBMSTemperatures) pBMSTemperatures->notify ();
240- // Note: pBMSFailureLevel, pBMSChargeMos, pBMSDischargeMos notify separately above, only on change
241- }
242- #else
243- // Stub when legacy telemetry is disabled
244- void updateBMSTelemetry (const STR_BMS_TELEMETRY_140& telemetry) {
245- (void )telemetry; // Suppress unused parameter warning
246- }
247- #endif // DISABLE_LEGACY_BLE_TELEMETRY
0 commit comments