Skip to content

Commit 8a0e4b8

Browse files
authored
Merge pull request #714 from doudar/copilot/add-ble-log-streaming-toggle
Add BLE log streaming with runtime toggle and read capability
2 parents eee3f7a + 089cc9d commit 8a0e4b8

File tree

11 files changed

+1852
-2
lines changed

11 files changed

+1852
-2
lines changed

CustomCharacteristic.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ From BLE_common.h
7676
|BLE_targetPosition |0x19 |int36|Position (in steps) the motor is maintaining. |
7777
|BLE_externalControl |0x1A |bool |01 disables internal calculation of targetPosition.|
7878
|BLE_syncMode |0x1B |bool |01 stops motor movement for external calibration |
79+
|BLE_UDPLogging |0x2E |bool |Enable/disable UDP log streaming |
80+
|BLE_BLELogging |0x30 |bool/str|Write: enable/disable BLE log streaming. Read: returns last log message|
7981

8082
*syncMode will disable the movement of the stepper motor by forcing stepperPosition = targetPosition prior to the motor control. While this mode is enabled, it allows the client to set parameters like incline and shifterPosition without moving the motor from it's current position. Once the parameters are set, this mode should be turned back off and SS2K will resume normal operation.
8183

Binary file not shown.

Hardware/Common Assets/Inserts/Schwinn AC Star Knob.stp

Lines changed: 1740 additions & 0 deletions
Large diffs are not rendered by default.

include/BLE_Custom_Characteristic.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ const uint8_t BLE_hMax = 0x2B; // Maximum homing value
6363
const uint8_t BLE_homingSensitivity = 0x2C; // Homing sensitivity value
6464
const uint8_t BLE_pTab4Pwr = 0x2D; // Use power values for power table
6565
const uint8_t BLE_UDPLogging = 0x2E; // Enable or disable UDP logging
66+
const uint8_t BLE_BLELogging = 0x30; // Enable or disable BLE logging
6667

6768
class BLE_ss2kCustomCharacteristic {
6869
public:

include/BleAppender.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright (C) 2020 Anthony Doud & Joel Baranick
3+
* All rights reserved
4+
*
5+
* SPDX-License-Identifier: GPL-2.0-only
6+
*/
7+
8+
#pragma once
9+
10+
#include "LogAppender.h"
11+
#include <string>
12+
#include <queue>
13+
14+
class BleAppender : public ILogAppender {
15+
public:
16+
void Log(const char *message);
17+
void Initialize();
18+
std::string getLastMessage();
19+
20+
private:
21+
static const size_t MAX_MESSAGE_SIZE = 500; // MTU-safe size
22+
std::queue<std::string> messageQueue;
23+
void appendMessage(const char *message);
24+
};

include/SmartSpin_parameters.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ class RuntimeParameters {
7676
int minResistance = -DEFAULT_RESISTANCE_RANGE;
7777
int maxResistance = DEFAULT_RESISTANCE_RANGE;
7878
bool simTargetWatts = false;
79+
bool bleLogEnabled = false;
7980

8081
public:
8182
Measurement watts;
@@ -114,6 +115,9 @@ class RuntimeParameters {
114115
void setMaxResistance(int max) { maxResistance = max; }
115116
int getMaxResistance() { return maxResistance; }
116117

118+
void setBleLogEnabled(bool enabled) { bleLogEnabled = enabled; }
119+
bool getBleLogEnabled() { return bleLogEnabled; }
120+
117121
String returnJSON();
118122
};
119123

src/BLE_Custom_Characteristic.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ This characteristic allows for reading and writing various user configuration pa
8787
#include <Power_Table.h>
8888
#include <BLE_Custom_Characteristic.h>
8989
#include <Constants.h>
90+
#include "BleAppender.h"
91+
92+
extern BleAppender bleAppender;
9093

9194
void BLE_ss2kCustomCharacteristic::setupService(NimBLEServer *pServer) {
9295
pSmartSpin2kService = spinBLEServer.pServer->createService(SMARTSPIN2K_SERVICE_UUID);
@@ -602,7 +605,7 @@ void BLE_ss2kCustomCharacteristic::process(std::string rxValue) {
602605
}
603606
if (rxValue[0] == cc_write) {
604607
returnValue[0] = cc_success;
605-
userConfig->setERGSensitivity((bytes_to_u16(rxValue[3], rxValue[2])) / 10);
608+
userConfig->setERGSensitivity((bytes_to_u16(rxValue[3], rxValue[2])) / 10.0);
606609
LOG_BUF_APPEND("(%f)", userConfig->getERGSensitivity());
607610
}
608611
} break;
@@ -828,6 +831,19 @@ void BLE_ss2kCustomCharacteristic::process(std::string rxValue) {
828831
}
829832
break;
830833

834+
case BLE_BLELogging: // 0x30
835+
LOG_BUF_APPEND("<-BLELogging");
836+
if (rxValue[0] == cc_read) {
837+
returnValue[0] = cc_success;
838+
returnString = bleAppender.getLastMessage();
839+
}
840+
if (rxValue[0] == cc_write) {
841+
returnValue[0] = cc_success;
842+
rtConfig->setBleLogEnabled(rxValue[2]);
843+
LOG_BUF_APPEND("(%s)", rtConfig->getBleLogEnabled() ? "true" : "false");
844+
}
845+
break;
846+
831847
default:
832848
LOG_BUF_APPEND("<-Unknown Characteristic");
833849
returnValue[0] = cc_error;

src/BLE_Server.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ void MyServerCallbacks::onDisconnect(NimBLEServer* pServer) {
157157
}
158158

159159
void MyServerCallbacks::onMTUChange(uint16_t MTU, NimBLEConnInfo& connInfo) {
160-
SS2K_LOG(BLE_SERVER_LOG_TAG, "MTU updated: %u for connection ID: %u", MTU, connInfo.getConnHandle());
160+
//SS2K_LOG(BLE_SERVER_LOG_TAG, "MTU updated: %u for connection ID: %u", MTU, connInfo.getConnHandle());
161161
}
162162

163163
bool MyServerCallbacks::onConnParamsUpdateRequest(uint16_t handle, const ble_gap_upd_params* params) {

src/BleAppender.cpp

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright (C) 2020 Anthony Doud & Joel Baranick
3+
* All rights reserved
4+
*
5+
* SPDX-License-Identifier: GPL-2.0-only
6+
*/
7+
8+
#include "BleAppender.h"
9+
#include "Main.h"
10+
#include "BLE_Custom_Characteristic.h"
11+
12+
void BleAppender::Initialize() {}
13+
14+
void BleAppender::Log(const char *message) {
15+
if (!rtConfig->getBleLogEnabled()) {
16+
return;
17+
}
18+
19+
// Cache the message
20+
appendMessage(message);
21+
22+
// Use the existing custom characteristic notification mechanism
23+
// only notify if there are messages to send
24+
if (!messageQueue.empty()) {
25+
BLE_ss2kCustomCharacteristic::notify(BLE_BLELogging);
26+
}
27+
}
28+
29+
std::string BleAppender::getLastMessage() {
30+
if (!messageQueue.empty()) {
31+
std::string msg = messageQueue.front();
32+
messageQueue.pop();
33+
return msg;
34+
}
35+
return "";
36+
}
37+
38+
void BleAppender::appendMessage(const char *message) {
39+
if (message == nullptr) {
40+
return;
41+
}
42+
43+
// Copy message and remove trailing newlines
44+
std::string msg(message);
45+
while (!msg.empty() && (msg.back() == '\n' || msg.back() == '\r')) {
46+
msg.pop_back();
47+
}
48+
49+
if (msg.empty()) {
50+
return;
51+
}
52+
53+
// Truncate message if it's too long on its own
54+
if (msg.length() > MAX_MESSAGE_SIZE) {
55+
msg = msg.substr(0, MAX_MESSAGE_SIZE);
56+
}
57+
58+
messageQueue.push(msg);
59+
}

src/Main.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "Power_Table.h"
1818
#include "UdpAppender.h"
1919
#include "WebsocketAppender.h"
20+
#include "BleAppender.h"
2021
#include "BLE_Custom_Characteristic.h"
2122
#include "BLE_Definitions.h"
2223
#include <Constants.h>
@@ -52,6 +53,7 @@ RuntimeParameters* rtConfig = new RuntimeParameters;
5253
///////////// Log Appender /////////////
5354
UdpAppender udpAppender;
5455
WebSocketAppender webSocketAppender;
56+
BleAppender bleAppender;
5557

5658
///////////// BEGIN SETUP /////////////
5759
#ifndef UNIT_TEST
@@ -141,6 +143,7 @@ extern "C" void app_main() {
141143
// Configure and Initialize Logger
142144
logHandler.addAppender(&webSocketAppender);
143145
logHandler.addAppender(&udpAppender);
146+
logHandler.addAppender(&bleAppender);
144147
logHandler.initialize();
145148
ss2k->startTasks();
146149
httpServer.start();

0 commit comments

Comments
 (0)