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,102 +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- uint8_t validBitmap = 0 ;
203-
204- // Bytes 1-16: 8×int16_t temperatures in deci-degrees C (0.1°C resolution)
205- int16_t * temps = reinterpret_cast <int16_t *>(&temp_buffer[1 ]);
206- const float sensorTemps[6 ] = {
207- telemetry.mos_temperature ,
208- telemetry.balance_temperature ,
209- telemetry.t1_temperature ,
210- telemetry.t2_temperature ,
211- telemetry.t3_temperature ,
212- telemetry.t4_temperature
213- };
214-
215- for (uint8_t i = 0 ; i < 6 ; i++) {
216- bool isValid = !isnan (sensorTemps[i]);
217-
218- if (isValid) {
219- validBitmap |= static_cast <uint8_t >(1U << i);
220- }
221- temps[i] = isValid ? static_cast <int16_t >(sensorTemps[i] * 10 .0f ) : 0 ;
222- }
223-
224- temp_buffer[0 ] = validBitmap;
225- temps[6 ] = 0 ; // [6] Reserved
226- temps[7 ] = 0 ; // [7] Reserved
227-
228- pBMSTemperatures->setValue (temp_buffer, 17 );
229- }
230-
231- // Handle state-change characteristics - only notify on change
232- setAndNotifyOnChange (pBMSFailureLevel, telemetry.battery_fail_level , lastFailureLevel);
233- setAndNotifyOnChange (pBMSChargeMos, static_cast <uint8_t >(telemetry.is_charge_mos ? 1 : 0 ), lastChargeMos);
234- setAndNotifyOnChange (pBMSDischargeMos, static_cast <uint8_t >(telemetry.is_discharge_mos ? 1 : 0 ), lastDischargeMos);
235-
236- if (!deviceConnected) {
237- return ; // No notifications needed without a subscriber.
238- }
239-
240- if (pBMSSOC) pBMSSOC->notify ();
241- if (pBMSVoltage) pBMSVoltage->notify ();
242- if (pBMSCurrent) pBMSCurrent->notify ();
243- if (pBMSPower) pBMSPower->notify ();
244- if (pBMSHighCell) pBMSHighCell->notify ();
245- if (pBMSLowCell) pBMSLowCell->notify ();
246- if (pBMSHighTemp) pBMSHighTemp->notify ();
247- if (pBMSLowTemp) pBMSLowTemp->notify ();
248- if (pBMSVoltageDiff) pBMSVoltageDiff->notify ();
249- if (pBMSTemperatures) pBMSTemperatures->notify ();
250- // Note: pBMSFailureLevel, pBMSChargeMos, pBMSDischargeMos notify separately above, only on change
251- }
252- #else
253- // Stub when legacy telemetry is disabled
254- void updateBMSTelemetry (const STR_BMS_TELEMETRY_140& telemetry) {
255- (void )telemetry; // Suppress unused parameter warning
256- }
257- #endif // DISABLE_LEGACY_BLE_TELEMETRY
0 commit comments