|
| 1 | +#include <Arduino.h> |
| 2 | + |
| 3 | +#include "sp140/ble/bms_service.h" |
| 4 | + |
| 5 | +#include "sp140/ble.h" |
| 6 | +#include "sp140/ble/ble_ids.h" |
| 7 | + |
| 8 | +namespace { |
| 9 | + |
| 10 | +BLEService* pBmsService = nullptr; |
| 11 | +BLECharacteristic* pBMSSOC = nullptr; |
| 12 | +BLECharacteristic* pBMSVoltage = nullptr; |
| 13 | +BLECharacteristic* pBMSCurrent = nullptr; |
| 14 | +BLECharacteristic* pBMSPower = nullptr; |
| 15 | +BLECharacteristic* pBMSHighCell = nullptr; |
| 16 | +BLECharacteristic* pBMSLowCell = nullptr; |
| 17 | +BLECharacteristic* pBMSHighTemp = nullptr; |
| 18 | +BLECharacteristic* pBMSLowTemp = nullptr; |
| 19 | +BLECharacteristic* pBMSFailureLevel = nullptr; |
| 20 | +BLECharacteristic* pBMSVoltageDiff = nullptr; |
| 21 | +BLECharacteristic* pBMSCellVoltages = nullptr; |
| 22 | +BLECharacteristic* pBMSChargeMos = nullptr; |
| 23 | +BLECharacteristic* pBMSDischargeMos = nullptr; |
| 24 | + |
| 25 | +} // namespace |
| 26 | + |
| 27 | +void initBmsBleService(BLEServer* server) { |
| 28 | + // Lazily guard against repeated init calls. |
| 29 | + if (pBmsService != nullptr) { |
| 30 | + return; |
| 31 | + } |
| 32 | + |
| 33 | + pBmsService = server->createService(BLEUUID(BMS_TELEMETRY_SERVICE_UUID), 30); |
| 34 | + |
| 35 | + pBMSSOC = pBmsService->createCharacteristic( |
| 36 | + BLEUUID(BMS_SOC_UUID), BLECharacteristic::PROPERTY_READ); |
| 37 | + |
| 38 | + pBMSVoltage = pBmsService->createCharacteristic( |
| 39 | + BLEUUID(BMS_VOLTAGE_UUID), BLECharacteristic::PROPERTY_READ); |
| 40 | + |
| 41 | + pBMSCurrent = pBmsService->createCharacteristic( |
| 42 | + BLEUUID(BMS_CURRENT_UUID), BLECharacteristic::PROPERTY_READ); |
| 43 | + |
| 44 | + pBMSPower = pBmsService->createCharacteristic( |
| 45 | + BLEUUID(BMS_POWER_UUID), BLECharacteristic::PROPERTY_READ); |
| 46 | + |
| 47 | + pBMSHighCell = pBmsService->createCharacteristic( |
| 48 | + BLEUUID(BMS_HIGH_CELL_UUID), BLECharacteristic::PROPERTY_READ); |
| 49 | + |
| 50 | + pBMSLowCell = pBmsService->createCharacteristic( |
| 51 | + BLEUUID(BMS_LOW_CELL_UUID), BLECharacteristic::PROPERTY_READ); |
| 52 | + |
| 53 | + pBMSHighTemp = pBmsService->createCharacteristic( |
| 54 | + BLEUUID(BMS_HIGH_TEMP_UUID), BLECharacteristic::PROPERTY_READ); |
| 55 | + |
| 56 | + pBMSLowTemp = pBmsService->createCharacteristic( |
| 57 | + BLEUUID(BMS_LOW_TEMP_UUID), BLECharacteristic::PROPERTY_READ); |
| 58 | + |
| 59 | + pBMSFailureLevel = pBmsService->createCharacteristic( |
| 60 | + BLEUUID(BMS_FAILURE_LEVEL_UUID), BLECharacteristic::PROPERTY_READ); |
| 61 | + |
| 62 | + pBMSVoltageDiff = pBmsService->createCharacteristic( |
| 63 | + BLEUUID(BMS_VOLTAGE_DIFF_UUID), BLECharacteristic::PROPERTY_READ); |
| 64 | + |
| 65 | + pBMSCellVoltages = pBmsService->createCharacteristic( |
| 66 | + BLEUUID(BMS_CELL_VOLTAGES_UUID), BLECharacteristic::PROPERTY_READ); |
| 67 | + |
| 68 | + pBMSChargeMos = pBmsService->createCharacteristic( |
| 69 | + BLEUUID(BMS_CHARGE_MOS_UUID), BLECharacteristic::PROPERTY_READ); |
| 70 | + |
| 71 | + pBMSDischargeMos = pBmsService->createCharacteristic( |
| 72 | + BLEUUID(BMS_DISCHARGE_MOS_UUID), BLECharacteristic::PROPERTY_READ); |
| 73 | + |
| 74 | + // Ensure characteristics have deterministic startup values. |
| 75 | + uint16_t initial_cell_values[BMS_CELLS_NUM] = {0}; |
| 76 | + pBMSCellVoltages->setValue( |
| 77 | + reinterpret_cast<uint8_t*>(initial_cell_values), |
| 78 | + BMS_CELLS_NUM * sizeof(uint16_t)); |
| 79 | + |
| 80 | + uint8_t initial_flag = 0; |
| 81 | + pBMSChargeMos->setValue(&initial_flag, sizeof(initial_flag)); |
| 82 | + pBMSDischargeMos->setValue(&initial_flag, sizeof(initial_flag)); |
| 83 | + |
| 84 | + pBmsService->start(); |
| 85 | +} |
| 86 | + |
| 87 | +void updateBMSTelemetry(const STR_BMS_TELEMETRY_140& telemetry) { |
| 88 | + if (pBmsService == nullptr) { |
| 89 | + return; // Not initialized yet. |
| 90 | + } |
| 91 | + |
| 92 | + float soc = telemetry.soc; |
| 93 | + float voltage = telemetry.battery_voltage; |
| 94 | + float current = telemetry.battery_current; |
| 95 | + float power = telemetry.power; |
| 96 | + float highCell = telemetry.highest_cell_voltage; |
| 97 | + float lowCell = telemetry.lowest_cell_voltage; |
| 98 | + float highTemp = telemetry.highest_temperature; |
| 99 | + float lowTemp = telemetry.lowest_temperature; |
| 100 | + float voltageDiff = telemetry.voltage_differential; |
| 101 | + |
| 102 | + if (pBMSSOC) pBMSSOC->setValue(soc); |
| 103 | + if (pBMSVoltage) pBMSVoltage->setValue(voltage); |
| 104 | + if (pBMSCurrent) pBMSCurrent->setValue(current); |
| 105 | + if (pBMSPower) pBMSPower->setValue(power); |
| 106 | + if (pBMSHighCell) pBMSHighCell->setValue(highCell); |
| 107 | + if (pBMSLowCell) pBMSLowCell->setValue(lowCell); |
| 108 | + if (pBMSHighTemp) pBMSHighTemp->setValue(highTemp); |
| 109 | + if (pBMSLowTemp) pBMSLowTemp->setValue(lowTemp); |
| 110 | + if (pBMSFailureLevel) { |
| 111 | + uint8_t failureLevel = telemetry.battery_failure_level; |
| 112 | + pBMSFailureLevel->setValue(&failureLevel, sizeof(failureLevel)); |
| 113 | + } |
| 114 | + if (pBMSVoltageDiff) pBMSVoltageDiff->setValue(voltageDiff); |
| 115 | + |
| 116 | + if (pBMSChargeMos) { |
| 117 | + uint8_t chargeMos = telemetry.is_charge_mos ? 1 : 0; |
| 118 | + pBMSChargeMos->setValue(&chargeMos, sizeof(chargeMos)); |
| 119 | + } |
| 120 | + |
| 121 | + if (pBMSDischargeMos) { |
| 122 | + uint8_t dischargeMos = telemetry.is_discharge_mos ? 1 : 0; |
| 123 | + pBMSDischargeMos->setValue(&dischargeMos, sizeof(dischargeMos)); |
| 124 | + } |
| 125 | + |
| 126 | + if (pBMSCellVoltages) { |
| 127 | + uint16_t cell_millivolts[BMS_CELLS_NUM]; |
| 128 | + for (uint8_t i = 0; i < BMS_CELLS_NUM; i++) { |
| 129 | + cell_millivolts[i] = static_cast<uint16_t>(telemetry.cell_voltages[i] * 1000.0f); |
| 130 | + } |
| 131 | + pBMSCellVoltages->setValue( |
| 132 | + reinterpret_cast<uint8_t*>(cell_millivolts), |
| 133 | + BMS_CELLS_NUM * sizeof(uint16_t)); |
| 134 | + } |
| 135 | + |
| 136 | + if (!deviceConnected) { |
| 137 | + return; // No notifications needed without a subscriber. |
| 138 | + } |
| 139 | + |
| 140 | + if (pBMSSOC) pBMSSOC->notify(); |
| 141 | + if (pBMSVoltage) pBMSVoltage->notify(); |
| 142 | + if (pBMSCurrent) pBMSCurrent->notify(); |
| 143 | + if (pBMSPower) pBMSPower->notify(); |
| 144 | + if (pBMSHighCell) pBMSHighCell->notify(); |
| 145 | + if (pBMSLowCell) pBMSLowCell->notify(); |
| 146 | + if (pBMSHighTemp) pBMSHighTemp->notify(); |
| 147 | + if (pBMSLowTemp) pBMSLowTemp->notify(); |
| 148 | + if (pBMSFailureLevel) pBMSFailureLevel->notify(); |
| 149 | + if (pBMSVoltageDiff) pBMSVoltageDiff->notify(); |
| 150 | + if (pBMSChargeMos) pBMSChargeMos->notify(); |
| 151 | + if (pBMSDischargeMos) pBMSDischargeMos->notify(); |
| 152 | +} |
0 commit comments