Skip to content

BLE rewrite + logging#77

Closed
zjwhitehead wants to merge 28 commits intomasterfrom
ble-logging
Closed

BLE rewrite + logging#77
zjwhitehead wants to merge 28 commits intomasterfrom
ble-logging

Conversation

@zjwhitehead
Copy link
Copy Markdown
Member

@zjwhitehead zjwhitehead commented Nov 20, 2025

This pull request introduces a modular and maintainable BLE (Bluetooth Low Energy) architecture for the SP140 project, adding dedicated header and implementation files for BLE core logic, services, and utilities. It also upgrades the ESP32 platform version for improved compatibility. The changes focus on clean separation of BLE responsibilities and efficient notification handling for telemetry updates.

BLE Architecture & Core Setup:

  • Added new BLE core header (ble_core.h) and implementation (ble_core.cpp) to encapsulate BLE initialization, server setup, connection/disconnection handling, and advertising restart logic. This centralizes BLE management and makes the codebase more maintainable. [1] [2]

Service Modularization:

  • Introduced separate headers for BLE service definitions: configuration (config_service.h), BMS telemetry (bms_service.h), and ESC telemetry (esc_service.h). Each header declares initialization and update functions for its respective BLE service. [1] [2] [3]
  • Implemented BMS telemetry service logic in bms_service.cpp, handling characteristic creation, deterministic startup values, and efficient notification of state changes only when necessary.

BLE Utilities & Notification Efficiency:

  • Added a BLE utilities header (ble_utils.h) and implementation (ble_utils.cpp) providing a templated helper for setting BLE characteristic values and notifying only when the value changes, reducing unnecessary BLE traffic. Includes specialization for uint8_t type for binary data. [1] [2]

Shared BLE Identifiers:

  • Created a centralized header (ble_ids.h) for all BLE UUID definitions, improving consistency and ease of reference for all BLE services and characteristics.

Platform Update:

  • Upgraded the ESP32 platform version in platformio.ini from 6.11.0 to 6.12.0 for better support and stability.

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.
@zjwhitehead zjwhitehead marked this pull request as ready for review November 26, 2025 23:08
@zjwhitehead zjwhitehead changed the title Ble logging BLE rewrite + logging Nov 27, 2025
zjwhitehead and others added 13 commits November 27, 2025 09:26
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.
@zjwhitehead
Copy link
Copy Markdown
Member Author

@copilot how is this looking, coming back to this with fresh eyes have we missed anything?

Copy link
Copy Markdown

Copilot AI commented Jan 13, 2026

@zjwhitehead I've opened a new pull request, #84, to work on those changes. Once the pull request is ready, I'll request review from you.

@zjwhitehead
Copy link
Copy Markdown
Member Author

Superseded by #85

@zjwhitehead zjwhitehead deleted the ble-logging branch January 25, 2026 04:33
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