Skip to content

Commit cfd1d2f

Browse files
authored
Merge pull request #85 from openppg/ble-rewrite
Rewrites the SP140 BLE subsystem with a focus on efficiency and maintainability.
2 parents b2280e0 + 2ed3874 commit cfd1d2f

30 files changed

Lines changed: 1555 additions & 643 deletions

.gemini/settings.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
{
2+
"privacy": {
3+
"usageStatisticsEnabled": false
4+
},
25
"telemetry": {
36
"enabled": false
4-
},
5-
"usageStatisticsEnabled": false
7+
}
68
}

_codeql_detected_source_root

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.

inc/sp140/ble.h

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
#ifndef INC_SP140_BLE_H_
22
#define INC_SP140_BLE_H_
33

4-
#include <BLEDevice.h>
5-
#include <BLEUtils.h>
6-
#include <BLEServer.h>
7-
#include <BLE2902.h>
4+
#include <NimBLEDevice.h>
5+
#include <NimBLEUtils.h>
6+
#include <NimBLEServer.h>
87

98
// BLE globals
10-
extern BLECharacteristic* pThrottleCharacteristic;
11-
extern BLECharacteristic* pDeviceStateCharacteristic;
12-
extern BLEServer* pServer;
13-
extern bool deviceConnected;
14-
extern bool oldDeviceConnected;
9+
extern NimBLECharacteristic* pThrottleCharacteristic;
10+
extern NimBLECharacteristic* pDeviceStateCharacteristic;
11+
extern NimBLEServer* pServer;
12+
extern volatile bool deviceConnected;
13+
extern volatile bool oldDeviceConnected;
1514

1615
#endif // INC_SP140_BLE_H_

inc/sp140/ble/ble_core.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#ifndef INC_SP140_BLE_BLE_CORE_H_
2+
#define INC_SP140_BLE_BLE_CORE_H_
3+
4+
// Core BLE helpers for initializing and maintaining the server.
5+
6+
void setupBLE();
7+
8+
// Allow modules to trigger advertising after disconnects.
9+
void restartBLEAdvertising();
10+
11+
#endif // INC_SP140_BLE_BLE_CORE_H_

inc/sp140/ble/ble_ids.h

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#ifndef INC_SP140_BLE_BLE_IDS_H_
2+
#define INC_SP140_BLE_BLE_IDS_H_
3+
4+
// Shared BLE UUID definitions. Keeping macros to match existing style.
5+
6+
// Configuration service
7+
#define CONFIG_SERVICE_UUID "1779A55B-DEB8-4482-A5D1-A12E62146138"
8+
#define METRIC_ALT_UUID "DF63F19E-7295-4A44-A0DC-184D1AFEDDF7"
9+
#define ARMED_TIME_UUID "58B29259-43EF-4593-B700-250EC839A2B2"
10+
#define SCREEN_ROTATION_UUID "9CBAB736-3705-4ECF-8086-FB7C5FB86282"
11+
#define SEA_PRESSURE_UUID "DB47E20E-D8C1-405A-971A-DA0A2DF7E0F6"
12+
#define METRIC_TEMP_UUID "D4962473-A3FB-4754-AD6A-90B079C3FB38"
13+
#define PERFORMANCE_MODE_UUID "D76C2E92-3547-4F5F-AFB4-515C5C08B06B"
14+
#define THEME_UUID "AD0E4309-1EB2-461A-B36C-697B2E1604D2"
15+
#define HW_REVISION_UUID "2A27"
16+
#define FW_VERSION_UUID "2A26"
17+
#define UNIX_TIME_UUID "E09FF0B7-5D02-4FD5-889E-C4251A58D9E7"
18+
#define TIMEZONE_UUID "CAE49D1A-7C21-4B0C-8520-416F3EF69DB1"
19+
#define THROTTLE_VALUE_UUID "50AB3859-9FBF-4D30-BF97-2516EE632FAD"
20+
#define DEVICE_STATE_UUID "8F80BCF5-B58F-4908-B079-E8AD6F5EE257"
21+
22+
// Device info service
23+
#define DEVICE_INFO_SERVICE_UUID "180A"
24+
#define MANUFACTURER_NAME_UUID "2A29"
25+
#define DEVICE_UNIQUE_ID_UUID "B1571560-345F-4974-A14D-66E98740232F"
26+
27+
// BMS service
28+
#define BMS_TELEMETRY_SERVICE_UUID "9E0F2FA3-3F2B-49C0-A6A3-3D8923062133"
29+
#define BMS_SOC_UUID "ACDEB138-3BD0-4BB3-B159-19F6F70871ED"
30+
#define BMS_VOLTAGE_UUID "AC0768DF-2F49-43D4-B23D-1DC82C90A9E9"
31+
#define BMS_CURRENT_UUID "6FEEC926-BA3C-4E65-BC71-5DB481811186"
32+
#define BMS_POWER_UUID "9DEA1343-434F-4555-A0A1-BB43FCBC68A6"
33+
#define BMS_HIGH_CELL_UUID "49267B41-560F-4CFF-ADC8-90EF85D2BE20"
34+
#define BMS_LOW_CELL_UUID "B9D01E5C-3751-4092-8B06-6D1FFF479E77"
35+
#define BMS_HIGH_TEMP_UUID "0EA08B6D-C905-4D9D-93F8-51E35DA096FC"
36+
#define BMS_LOW_TEMP_UUID "26CD6E8A-175D-4C8E-B487-DEFF0B034F2A"
37+
#define BMS_FAILURE_LEVEL_UUID "396C768B-F348-44CC-9D46-92388F25A557"
38+
#define BMS_VOLTAGE_DIFF_UUID "1C45825B-7C81-430B-8D5F-B644FFFC71BB"
39+
#define BMS_CHARGE_MOS_UUID "4D0C3E4D-E7E2-41EF-8DDD-38C4B0948F9E"
40+
#define BMS_DISCHARGE_MOS_UUID "175A2989-3442-4B69-9C84-0CE4F1BD4F4F"
41+
#define BMS_CELL_VOLTAGES_UUID "4337e58b-8462-49b2-b061-c3bf379ef4af"
42+
#define BMS_TEMPERATURES_UUID "C53898C0-C10D-435B-8B5D-B46A2E06EB53"
43+
44+
// ESC service
45+
#define ESC_TELEMETRY_SERVICE_UUID "C154DAE9-1984-40EA-B20F-5B23F9CBA0A9"
46+
#define ESC_VOLTAGE_UUID "0528ecd8-9337-4249-95e4-9aba69f6c1f4"
47+
#define ESC_CURRENT_UUID "3889e01e-7d2d-4478-b5cc-a06b803e2788"
48+
#define ESC_RPM_UUID "24dc4a84-0be3-4eba-a8c3-ed9748daa599"
49+
#define ESC_TEMPS_UUID "d087f190-5450-4fea-b9ff-17133a0b6f64"
50+
51+
// ============================================================================
52+
// Binary Packed Telemetry Characteristics (V1)
53+
// High-efficiency binary telemetry for reduced BLE overhead
54+
// ============================================================================
55+
56+
// Packed BMS telemetry characteristic (single binary packet per BMS)
57+
// For multi-BMS, multiple notifications sent with different bms_id values
58+
#define BMS_PACKED_TELEMETRY_UUID "ABAB6342-D068-45A7-9D45-FBCE8E4D4DF1"
59+
60+
// Extended BMS data (cell voltages + temps array) - separate packet for detailed data
61+
#define BMS_EXTENDED_TELEMETRY_UUID "93431570-64A3-448D-AC97-4655B2D1458F"
62+
63+
// Packed ESC telemetry characteristic
64+
#define ESC_PACKED_TELEMETRY_UUID "D3C2B470-007F-494E-95C5-8E275A181A3A"
65+
66+
// Controller telemetry service and characteristic (ESP32 sensors: altitude, baro, vario, temps)
67+
#define CONTROLLER_SERVICE_UUID "01C63B60-0891-4655-BBCA-8E745C48A175"
68+
#define CONTROLLER_TELEMETRY_UUID "01C63B61-0891-4655-BBCA-8E745C48A176"
69+
70+
#endif // INC_SP140_BLE_BLE_IDS_H_

inc/sp140/ble/ble_utils.h

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#ifndef INC_SP140_BLE_BLE_UTILS_H_
2+
#define INC_SP140_BLE_BLE_UTILS_H_
3+
4+
#include <NimBLECharacteristic.h>
5+
6+
// Forward declare deviceConnected from ble.h (volatile for thread safety)
7+
extern volatile bool deviceConnected;
8+
9+
/**
10+
* Sets a BLE characteristic value and conditionally notifies only when the value changes.
11+
* Returns true if the value changed and a notification was sent, false otherwise.
12+
*
13+
* @param characteristic The BLE characteristic to update
14+
* @param newValue The new value to set
15+
* @param lastValue Reference to the variable tracking the previous value
16+
* @return true if value changed and notification sent, false if no change
17+
*/
18+
template<typename T>
19+
bool setAndNotifyOnChange(NimBLECharacteristic* characteristic, T newValue, T& lastValue) {
20+
if (characteristic == nullptr) {
21+
return false;
22+
}
23+
24+
characteristic->setValue(newValue);
25+
26+
if (newValue != lastValue) {
27+
lastValue = newValue;
28+
if (deviceConnected) {
29+
characteristic->notify();
30+
}
31+
return true; // Value changed
32+
}
33+
return false; // No change
34+
}
35+
36+
/**
37+
* Specialization for uint8_t that uses pointer-based setValue (for binary data).
38+
* Returns true if the value changed and a notification was sent, false otherwise.
39+
*
40+
* @param characteristic The BLE characteristic to update
41+
* @param newValue The new uint8_t value to set
42+
* @param lastValue Reference to the variable tracking the previous value
43+
* @return true if value changed and notification sent, false if no change
44+
*/
45+
bool setAndNotifyOnChange(NimBLECharacteristic* characteristic, uint8_t newValue, uint8_t& lastValue);
46+
47+
#endif // INC_SP140_BLE_BLE_UTILS_H_

inc/sp140/ble/bms_service.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#ifndef INC_SP140_BLE_BMS_SERVICE_H_
2+
#define INC_SP140_BLE_BMS_SERVICE_H_
3+
4+
#include "sp140/structs.h"
5+
6+
class NimBLEServer;
7+
8+
void initBmsBleService(NimBLEServer* server);
9+
10+
// Legacy individual characteristic updates (can be disabled with DISABLE_LEGACY_BLE_TELEMETRY)
11+
void updateBMSTelemetry(const STR_BMS_TELEMETRY_140& telemetry);
12+
13+
// Binary packed telemetry update (V1 protocol)
14+
// bms_id: 0-3 for multi-BMS support
15+
void updateBMSPackedTelemetry(const STR_BMS_TELEMETRY_140& telemetry, uint8_t bms_id = 0);
16+
17+
#endif // INC_SP140_BLE_BMS_SERVICE_H_

inc/sp140/ble/config_service.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#ifndef INC_SP140_BLE_CONFIG_SERVICE_H_
2+
#define INC_SP140_BLE_CONFIG_SERVICE_H_
3+
4+
#include <string>
5+
6+
class NimBLEServer;
7+
8+
void initConfigBleService(NimBLEServer* server, const std::string& uniqueId);
9+
void updateThrottleBLE(int value);
10+
11+
#endif // INC_SP140_BLE_CONFIG_SERVICE_H_

inc/sp140/ble/controller_service.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#ifndef INC_SP140_BLE_CONTROLLER_SERVICE_H_
2+
#define INC_SP140_BLE_CONTROLLER_SERVICE_H_
3+
4+
#include "sp140/structs.h"
5+
6+
class NimBLEServer;
7+
8+
// Initialize the controller telemetry BLE service
9+
void initControllerBleService(NimBLEServer* server);
10+
11+
// Update controller telemetry (altitude, baro, vario, mcu_temp, pot, uptime)
12+
// Internally throttled to 1Hz - safe to call frequently
13+
void updateControllerPackedTelemetry(float altitude, float baro_temp,
14+
float vario, float mcu_temp);
15+
16+
#endif // INC_SP140_BLE_CONTROLLER_SERVICE_H_

inc/sp140/ble/esc_service.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#ifndef INC_SP140_BLE_ESC_SERVICE_H_
2+
#define INC_SP140_BLE_ESC_SERVICE_H_
3+
4+
#include "sp140/structs.h"
5+
6+
class NimBLEServer;
7+
8+
void initEscBleService(NimBLEServer* server);
9+
10+
// Legacy individual characteristic updates (can be disabled with DISABLE_LEGACY_BLE_TELEMETRY)
11+
void updateESCTelemetryBLE(const STR_ESC_TELEMETRY_140& telemetry);
12+
13+
// Binary packed telemetry update (V1 protocol)
14+
void updateESCPackedTelemetry(const STR_ESC_TELEMETRY_140& telemetry);
15+
16+
#endif // INC_SP140_BLE_ESC_SERVICE_H_

0 commit comments

Comments
 (0)