Skip to content
Merged
2 changes: 2 additions & 0 deletions CustomCharacteristic.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ From BLE_common.h
|BLE_targetPosition |0x19 |int36|Position (in steps) the motor is maintaining. |
|BLE_externalControl |0x1A |bool |01 disables internal calculation of targetPosition.|
|BLE_syncMode |0x1B |bool |01 stops motor movement for external calibration |
|BLE_UDPLogging |0x2E |bool |Enable/disable UDP log streaming |
|BLE_BLELogging |0x30 |bool/str|Write: enable/disable BLE log streaming. Read: returns last log message|

*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.

Expand Down
Binary file not shown.
1,740 changes: 1,740 additions & 0 deletions Hardware/Common Assets/Inserts/Schwinn AC Star Knob.stp

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions include/BLE_Custom_Characteristic.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ const uint8_t BLE_hMax = 0x2B; // Maximum homing value
const uint8_t BLE_homingSensitivity = 0x2C; // Homing sensitivity value
const uint8_t BLE_pTab4Pwr = 0x2D; // Use power values for power table
const uint8_t BLE_UDPLogging = 0x2E; // Enable or disable UDP logging
const uint8_t BLE_BLELogging = 0x30; // Enable or disable BLE logging

class BLE_ss2kCustomCharacteristic {
public:
Expand Down
24 changes: 24 additions & 0 deletions include/BleAppender.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright (C) 2020 Anthony Doud & Joel Baranick
* All rights reserved
*
* SPDX-License-Identifier: GPL-2.0-only
*/

#pragma once

#include "LogAppender.h"
#include <string>
#include <queue>

class BleAppender : public ILogAppender {
public:
void Log(const char *message);
void Initialize();
std::string getLastMessage();

private:
static const size_t MAX_MESSAGE_SIZE = 500; // MTU-safe size
std::queue<std::string> messageQueue;
void appendMessage(const char *message);
};
4 changes: 4 additions & 0 deletions include/SmartSpin_parameters.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ class RuntimeParameters {
int minResistance = -DEFAULT_RESISTANCE_RANGE;
int maxResistance = DEFAULT_RESISTANCE_RANGE;
bool simTargetWatts = false;
bool bleLogEnabled = false;

public:
Measurement watts;
Expand Down Expand Up @@ -114,6 +115,9 @@ class RuntimeParameters {
void setMaxResistance(int max) { maxResistance = max; }
int getMaxResistance() { return maxResistance; }

void setBleLogEnabled(bool enabled) { bleLogEnabled = enabled; }
bool getBleLogEnabled() { return bleLogEnabled; }

String returnJSON();
};

Expand Down
18 changes: 17 additions & 1 deletion src/BLE_Custom_Characteristic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ This characteristic allows for reading and writing various user configuration pa
#include <Power_Table.h>
#include <BLE_Custom_Characteristic.h>
#include <Constants.h>
#include "BleAppender.h"

extern BleAppender bleAppender;

void BLE_ss2kCustomCharacteristic::setupService(NimBLEServer *pServer) {
pSmartSpin2kService = spinBLEServer.pServer->createService(SMARTSPIN2K_SERVICE_UUID);
Expand Down Expand Up @@ -602,7 +605,7 @@ void BLE_ss2kCustomCharacteristic::process(std::string rxValue) {
}
if (rxValue[0] == cc_write) {
returnValue[0] = cc_success;
userConfig->setERGSensitivity((bytes_to_u16(rxValue[3], rxValue[2])) / 10);
userConfig->setERGSensitivity((bytes_to_u16(rxValue[3], rxValue[2])) / 10.0);
LOG_BUF_APPEND("(%f)", userConfig->getERGSensitivity());
}
} break;
Expand Down Expand Up @@ -828,6 +831,19 @@ void BLE_ss2kCustomCharacteristic::process(std::string rxValue) {
}
break;

case BLE_BLELogging: // 0x30
LOG_BUF_APPEND("<-BLELogging");
if (rxValue[0] == cc_read) {
returnValue[0] = cc_success;
returnString = bleAppender.getLastMessage();
}
if (rxValue[0] == cc_write) {
returnValue[0] = cc_success;
rtConfig->setBleLogEnabled(rxValue[2]);
LOG_BUF_APPEND("(%s)", rtConfig->getBleLogEnabled() ? "true" : "false");
}
break;

default:
LOG_BUF_APPEND("<-Unknown Characteristic");
returnValue[0] = cc_error;
Expand Down
2 changes: 1 addition & 1 deletion src/BLE_Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ void MyServerCallbacks::onDisconnect(NimBLEServer* pServer) {
}

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

bool MyServerCallbacks::onConnParamsUpdateRequest(uint16_t handle, const ble_gap_upd_params* params) {
Expand Down
59 changes: 59 additions & 0 deletions src/BleAppender.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright (C) 2020 Anthony Doud & Joel Baranick
* All rights reserved
*
* SPDX-License-Identifier: GPL-2.0-only
*/

#include "BleAppender.h"
#include "Main.h"
#include "BLE_Custom_Characteristic.h"

void BleAppender::Initialize() {}

void BleAppender::Log(const char *message) {
if (!rtConfig->getBleLogEnabled()) {
return;
}

// Cache the message
appendMessage(message);

// Use the existing custom characteristic notification mechanism
// only notify if there are messages to send
if (!messageQueue.empty()) {
BLE_ss2kCustomCharacteristic::notify(BLE_BLELogging);
}
}

std::string BleAppender::getLastMessage() {
if (!messageQueue.empty()) {
std::string msg = messageQueue.front();
messageQueue.pop();
return msg;
}
return "";
}

void BleAppender::appendMessage(const char *message) {
if (message == nullptr) {
return;
}

// Copy message and remove trailing newlines
std::string msg(message);
while (!msg.empty() && (msg.back() == '\n' || msg.back() == '\r')) {
msg.pop_back();
}

if (msg.empty()) {
return;
}

// Truncate message if it's too long on its own
if (msg.length() > MAX_MESSAGE_SIZE) {
msg = msg.substr(0, MAX_MESSAGE_SIZE);
}

messageQueue.push(msg);
}
3 changes: 3 additions & 0 deletions src/Main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "Power_Table.h"
#include "UdpAppender.h"
#include "WebsocketAppender.h"
#include "BleAppender.h"
#include "BLE_Custom_Characteristic.h"
#include "BLE_Definitions.h"
#include <Constants.h>
Expand Down Expand Up @@ -52,6 +53,7 @@ RuntimeParameters* rtConfig = new RuntimeParameters;
///////////// Log Appender /////////////
UdpAppender udpAppender;
WebSocketAppender webSocketAppender;
BleAppender bleAppender;

///////////// BEGIN SETUP /////////////
#ifndef UNIT_TEST
Expand Down Expand Up @@ -141,6 +143,7 @@ extern "C" void app_main() {
// Configure and Initialize Logger
logHandler.addAppender(&webSocketAppender);
logHandler.addAppender(&udpAppender);
logHandler.addAppender(&bleAppender);
logHandler.initialize();
ss2k->startTasks();
httpServer.start();
Expand Down
1 change: 1 addition & 0 deletions src/SmartSpin_parameters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ JsonDocument doc;
doc["maxStep"] = this->maxStep;
doc["minResistance"] = this->minResistance;
doc["maxResistance"] = this->maxResistance;
doc["bleLogEnabled"] = this->bleLogEnabled;

String output;
serializeJson(doc, output);
Expand Down
Loading