Skip to content

Ble rewrite#85

Merged
zjwhitehead merged 37 commits intomasterfrom
ble-rewrite
Jan 21, 2026
Merged

Ble rewrite#85
zjwhitehead merged 37 commits intomasterfrom
ble-rewrite

Conversation

@zjwhitehead
Copy link
Copy Markdown
Member

BLE Telemetry Refactor and Efficiency Improvements

Rewrites the SP140 BLE subsystem with a focus on efficiency and maintainability. The primary change involves migrating to a more efficient BLE stack and introducing a new telemetry format that consolidates data into larger binary packets to minimize BLE overhead.

Summary of Changes

  • Migrated from the legacy Arduino BLE library to NimBLE-Arduino for lower memory usage and better performance.
  • Reworked BMS, ESC, and controller telemetry into packed binary structs, sending fewer, larger BLE packets instead of many small updates.
  • Refactored BLE code into modular core, utility, and service layers for cleaner separation of concerns.
  • Centralized all BLE service and characteristic UUIDs for consistency.
  • Added thread-safe helpers for BLE characteristic updates and minor API improvements to support telemetry.
  • Updated PlatformIO configuration and bumped firmware version to 7.4.

zjwhitehead and others added 30 commits September 17, 2025 22:18
Included motor_temp in the temps struct and updated its initialization to use telemetry.motor_temp, allowing motor temperature data to be sent via BLE telemetry.
Introduces BLE characteristics for charge and discharge MOS states in the BMS. Updates telemetry and notification logic to support these new characteristics, allowing external devices to read MOS status via BLE.
Moved BLE service and characteristic definitions from extra-data.ino into separate header and source files under inc/sp140/ble and src/sp140/ble. This modularizes BLE functionality for BMS, ESC, and configuration services, improving code organization and maintainability. Updated sp140.ino to include new BLE headers and removed legacy BLE code from extra-data.ino.
Swapped doc.containsKey(...) and settings.containsKey(...) in src/sp140/extra-data.ino:209-249 for the modern isNull() checks via
  doc["..."]/settings["..."], all guarded with null tests before use.
Introduces a new DEVICE_UNIQUE_ID_UUID constant and updates the BLE config service to expose the device's unique BLE address (in uppercase) as a characteristic in the device info service. The initConfigBleService function now requires the unique ID as a parameter.
Introduces a new BLE characteristic for BMS temperatures, including its UUID definition and initialization in the BMS BLE service. The characteristic supports read and notify properties and is initialized with default values.
Implements updating and notifying the BMS temperatures BLE characteristic with a bitmap and 8 temperature values in deci-degrees Celsius. This provides more detailed telemetry data for connected BLE clients.
Introduced ble_utils.h/cpp with setAndNotifyOnChange to update BLE characteristics and notify only on value changes. Updated bms_service.cpp to use this utility for failure level and MOS state characteristics, reducing unnecessary BLE notifications. Also added notification properties and descriptors to relevant characteristics.
Increased the BLE MTU to 185 in setupBLE() to improve compatibility with iOS devices, which support this size. This change helps ensure reliable BLE communication with mobile devices.
Introduces ThemeCallbacks, SeaPressureCallbacks, and MetricTempCallbacks classes to handle new BLE characteristics for theme selection, sea-level pressure, and metric temperature settings. Updates initConfigBleService to register these characteristics, enabling read/write access and validation for each setting.
Changed deviceConnected and oldDeviceConnected to volatile for thread safety. Updated BLE characteristic callbacks to use static instances instead of dynamic allocation, improving memory usage. Simplified connection checks in BLE update functions for reliability.
Co-authored-by: zjwhitehead <4623792+zjwhitehead@users.noreply.github.com>
Co-authored-by: zjwhitehead <4623792+zjwhitehead@users.noreply.github.com>
Remove manual 2902 descriptor creation as NimBLE automatically creates
them for characteristics with NOTIFY or INDICATE properties.

Co-authored-by: zjwhitehead <4623792+zjwhitehead@users.noreply.github.com>
Upgraded NimBLE-Arduino to v2.3.5 in platformio.ini. Refactored BLE callback methods to use new signatures with NimBLEConnInfo and removed unused NimBLE2902 include. Removed service handle count arguments from createService calls for compatibility with updated NimBLE API.
Introduces functions to calculate cruise control PWM, check cruise activation range, and determine cruise disengagement in throttle.h and throttle.cpp. Refactors cruise activation logic in sp140.ino to use the new calculation function for consistent throttle behavior across modes.
- Add NIMBLE_PROPERTY::NOTIFY to all ESC characteristics (voltage, current, RPM, temps)
- Add notify() calls in updateESCTelemetryBLE() when device is connected
- Ensures consistency with BMS service notification pattern

Co-authored-by: zjwhitehead <4623792+zjwhitehead@users.noreply.github.com>
Copilot AI and others added 6 commits January 13, 2026 05:39
…e ESC service

- Add NIMBLE_PROPERTY::NOTIFY to BMS telemetry characteristics (SOC, voltage, current, power, cells, temps, voltage diff)
- Add ESC_TELEMETRY_SERVICE_UUID to BLE advertising for better discoverability
- Ensures all characteristics that call notify() have proper NOTIFY property

Co-authored-by: zjwhitehead <4623792+zjwhitehead@users.noreply.github.com>
Migrates legacy individual BLE characteristics to efficient binary packed telemetry structures for BMS, ESC, and controller data. Adds new packed characteristic UUIDs, protocol structs, and service logic, with multi-BMS support and a DISABLE_LEGACY_BLE_TELEMETRY flag to toggle legacy characteristics. Throttles all packed telemetry updates to 10Hz for reduced BLE overhead and improved scalability.
Controller telemetry updates are now internally throttled to 1Hz to reduce unnecessary BLE traffic. Added a thread-safe, cached CPU temperature reading function to avoid sensor configuration errors, and updated internal monitors to use the cached value.
Included the raw potentiometer (throttle) reading in the BLE controller telemetry struct and update logic. Added getLastThrottleRaw() to provide the most recent throttle value, and updated relevant BLE services to use this accessor for consistent data reporting.
Refactored all occurrences of the battery telemetry field from battery_failure_level to battery_fail_level in structs, BMS logic, BLE service, and debug code for consistency. No functional changes were made.
@zjwhitehead
Copy link
Copy Markdown
Member Author

Supersedes #84

@zjwhitehead zjwhitehead merged commit cfd1d2f into master Jan 21, 2026
7 checks passed
@zjwhitehead zjwhitehead deleted the ble-rewrite branch January 21, 2026 20:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants