From 7befd18c18c3d45f71de242d9c36cfe7044b9a20 Mon Sep 17 00:00:00 2001 From: Anthony Doud Date: Sat, 24 Feb 2024 11:14:08 -0500 Subject: [PATCH] added descriptions --- .gitignore | 1 + include/Custom_Characteristic.h | 73 ++++++++++----------- src/Custom_Characteristic.cpp | 109 +++++++++++++++++++++----------- 3 files changed, 111 insertions(+), 72 deletions(-) diff --git a/.gitignore b/.gitignore index 5b30ec62..7146d618 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ include/telegram_token.h Errors SmartSpin_logfile.txt *.err +.DS_Store diff --git a/include/Custom_Characteristic.h b/include/Custom_Characteristic.h index efba1674..df3e119b 100644 --- a/include/Custom_Characteristic.h +++ b/include/Custom_Characteristic.h @@ -8,39 +8,40 @@ #pragma once // custom characteristic codes -#define BLE_firmwareUpdateURL 0x01 -#define BLE_incline 0x02 -#define BLE_simulatedWatts 0x03 -#define BLE_simulatedHr 0x04 -#define BLE_simulatedCad 0x05 -#define BLE_simulatedSpeed 0x06 -#define BLE_deviceName 0x07 -#define BLE_shiftStep 0x08 -#define BLE_stepperPower 0x09 -#define BLE_stealthChop 0x0A -#define BLE_inclineMultiplier 0x0B -#define BLE_powerCorrectionFactor 0x0C -#define BLE_simulateHr 0x0D -#define BLE_simulateWatts 0x0E -#define BLE_simulateCad 0x0F -#define BLE_FTMSMode 0x10 -#define BLE_autoUpdate 0x11 -#define BLE_ssid 0x12 -#define BLE_password 0x13 -#define BLE_foundDevices 0x14 -#define BLE_connectedPowerMeter 0x15 -#define BLE_connectedHeartMonitor 0x16 -#define BLE_shifterPosition 0x17 -#define BLE_saveToLittleFS 0x18 -#define BLE_targetPosition 0x19 -#define BLE_externalControl 0x1A -#define BLE_syncMode 0x1B -#define BLE_reboot 0x1C -#define BLE_resetToDefaults 0x1D -#define BLE_stepperSpeed 0x1E -#define BLE_ERGSensitivity 0x1F -#define BLE_shiftDir 0x20 -#define BLE_minBrakeWatts 0x21 -#define BLE_maxBrakeWatts 0x22 -#define BLE_restartBLE 0x23 -#define BLE_scanBLE 0x24 +#define BLE_firmwareUpdateURL 0x01 // URL used to update firmware +#define BLE_incline 0x02 // target incline like +#define BLE_simulatedWatts 0x03 // current watts +#define BLE_simulatedHr 0x04 // current hr +#define BLE_simulatedCad 0x05 // currend cad +#define BLE_simulatedSpeed 0x06 // current speed +#define BLE_deviceName 0x07 // Name of SmartSpin2k (Changes BLE name and URL for local access) +#define BLE_shiftStep 0x08 // Amount of stepper steps for a shift +#define BLE_stepperPower 0x09 // Stepper power in ma. Capped at 2000 +#define BLE_stealthChop 0x0A // Stepper StealthChop (Makes it quieter with a little torque sacraficed) +#define BLE_inclineMultiplier 0x0B // Incline * this = steps to move. 3.0 is a good starting vlue for most bikes. +#define BLE_powerCorrectionFactor 0x0C // Correction factor for FTMS and CPS connected devices. .1-2.0 +#define BLE_simulateHr 0x0D // If set to 1, override connected HR and use simulated above. +#define BLE_simulateWatts 0x0E // "" for Power Meter +#define BLE_simulateCad 0x0F // "" for Cad +#define BLE_FTMSMode 0x10 // get or set FTMS mode using values such as FitnessMachineControlPointProcedure::SetTargetPower +#define BLE_autoUpdate 0x11 // Attempt to update firmware on reboot? +#define BLE_ssid 0x12 // WiFi SSID. If it's not a network in range, fallback to AP mode made with devicename and "password" +#define BLE_password 0x13 // WiFi Password for known network. Fallback to "password" if network above not in range +#define BLE_foundDevices 0x14 // BLE fitness devices in range +#define BLE_connectedPowerMeter 0x15 // Desired Power Meter +#define BLE_connectedHeartMonitor 0x16 // Desired HR Monitor +#define BLE_shifterPosition 0x17 // Current Shifter Position +#define BLE_saveToLittleFS 0x18 // Write to make settings Permanent +#define BLE_targetPosition 0x19 // The target position of the stepper motor +#define BLE_externalControl 0x1A // Stop all internal control of stepper motor - let external device control motor +#define BLE_syncMode 0x1B // Stop stepper motor and set the current position to the target position +#define BLE_reboot 0x1C // Reboots the SmartSpin2k +#define BLE_resetToDefaults 0x1D // Resets the SmartSpin2k to defaults - Settings only, not the filesystem +#define BLE_stepperSpeed 0x1E // Stepper motor speed. Use cautiously. numbers above 3000 seem to cause a crash currently +#define BLE_ERGSensitivity 0x1F // Agressiveness of the control loop for ERG mode +#define BLE_shiftDir 0x20 // Flip flops the hardwired shifter direction +#define BLE_minBrakeWatts 0x21 // Minimum watts @ 90rpm . Used to set software end stop so motor doesn't crash and lose steps. +#define BLE_maxBrakeWatts 0x22 // "" Maximum watts for the other direction +#define BLE_restartBLE 0x23 // Closes all connections to the BLE client - used to connect new BLE devices the user selects. +#define BLE_scanBLE 0x24 // Scan for new BLE devices +#define BLE_firmwareVer 0x25 // String of the current firmware version diff --git a/src/Custom_Characteristic.cpp b/src/Custom_Characteristic.cpp index 5849ab47..ecf0e65b 100644 --- a/src/Custom_Characteristic.cpp +++ b/src/Custom_Characteristic.cpp @@ -64,10 +64,11 @@ void ss2kCustomCharacteristicCallbacks::onWrite(BLECharacteristic *pCharacterist switch (rxValue[1]) { case BLE_firmwareUpdateURL: // 0x01 logBufLength += snprintf(logBuf + logBufLength, kLogBufCapacity - logBufLength, "<-Firmware Update URL"); - returnValue[0] = success; if (rxValue[0] == read) { + returnValue[0] = success; returnString = userConfig->getFirmwareUpdateURL(); } else if (rxValue[0] == write) { + returnValue[0] = success; String str = (char *)pData; str.remove(0, 2); userConfig->setFirmwareUpdateURL(str); @@ -77,14 +78,15 @@ void ss2kCustomCharacteristicCallbacks::onWrite(BLECharacteristic *pCharacterist case BLE_incline: { // 0x02 logBufLength += snprintf(logBuf + logBufLength, kLogBufCapacity - logBufLength, "<-incline"); - returnValue[0] = success; if (rxValue[0] == read) { + returnValue[0] = success; int inc = rtConfig->getTargetIncline() * 10; returnValue[2] = (uint8_t)(inc & 0xff); returnValue[3] = (uint8_t)(inc >> 8); returnLength += 2; } if (rxValue[0] == write) { + returnValue[0] = success; rtConfig->setTargetIncline(bytes_to_u16(rxValue[3], rxValue[2]) / 10); logBufLength += snprintf(logBuf + logBufLength, kLogBufCapacity - logBufLength, "(%f)", rtConfig->getTargetIncline()); } @@ -92,13 +94,14 @@ void ss2kCustomCharacteristicCallbacks::onWrite(BLECharacteristic *pCharacterist case BLE_simulatedWatts: // 0x03 logBufLength += snprintf(logBuf + logBufLength, kLogBufCapacity - logBufLength, "<-simulatedWatts"); - returnValue[0] = success; if (rxValue[0] == read) { + returnValue[0] = success; returnValue[2] = (uint8_t)(rtConfig->watts.getValue() & 0xff); returnValue[3] = (uint8_t)(rtConfig->watts.getValue() >> 8); returnLength += 2; } if (rxValue[0] == write) { + returnValue[0] = success; rtConfig->watts.setValue(bytes_to_u16(rxValue[3], rxValue[2])); logBufLength += snprintf(logBuf + logBufLength, kLogBufCapacity - logBufLength, "(%d)", rtConfig->watts.getValue()); } @@ -106,13 +109,14 @@ void ss2kCustomCharacteristicCallbacks::onWrite(BLECharacteristic *pCharacterist case BLE_simulatedHr: // 0x04 logBufLength += snprintf(logBuf + logBufLength, kLogBufCapacity - logBufLength, "<-simulatedHr"); - returnValue[0] = success; if (rxValue[0] == read) { + returnValue[0] = success; returnValue[2] = (uint8_t)(rtConfig->hr.getValue() & 0xff); returnValue[3] = (uint8_t)(rtConfig->hr.getValue() >> 8); returnLength += 2; } if (rxValue[0] == write) { + returnValue[0] = success; rtConfig->hr.setValue(bytes_to_u16(rxValue[3], rxValue[2])); logBufLength += snprintf(logBuf + logBufLength, kLogBufCapacity - logBufLength, "(%d)", rtConfig->hr.getValue()); } @@ -120,13 +124,14 @@ void ss2kCustomCharacteristicCallbacks::onWrite(BLECharacteristic *pCharacterist case BLE_simulatedCad: // 0x05 logBufLength += snprintf(logBuf + logBufLength, kLogBufCapacity - logBufLength, "<-simulatedCad"); - returnValue[0] = success; if (rxValue[0] == read) { + returnValue[0] = success; returnValue[2] = (uint8_t)(rtConfig->cad.getValue() & 0xff); returnValue[3] = (uint8_t)(rtConfig->cad.getValue() >> 8); returnLength += 2; } if (rxValue[0] == write) { + returnValue[0] = success; rtConfig->cad.setValue(bytes_to_u16(rxValue[3], rxValue[2])); logBufLength += snprintf(logBuf + logBufLength, kLogBufCapacity - logBufLength, "(%d)", rtConfig->cad.getValue()); } @@ -134,14 +139,15 @@ void ss2kCustomCharacteristicCallbacks::onWrite(BLECharacteristic *pCharacterist case BLE_simulatedSpeed: { // 0x06 logBufLength += snprintf(logBuf + logBufLength, kLogBufCapacity - logBufLength, "<-simulatedSpeed"); - returnValue[0] = success; int spd = rtConfig->getSimulatedSpeed() * 10; if (rxValue[0] == read) { + returnValue[0] = success; returnValue[2] = (uint8_t)(spd & 0xff); returnValue[3] = (uint8_t)(spd >> 8); returnLength += 2; } if (rxValue[0] == write) { + returnValue[0] = success; rtConfig->setSimulatedSpeed(bytes_to_u16(rxValue[3], rxValue[2]) / 10); logBufLength += snprintf(logBuf + logBufLength, kLogBufCapacity - logBufLength, "(%d)", rtConfig->getSimulatedSpeed()); } @@ -149,10 +155,11 @@ void ss2kCustomCharacteristicCallbacks::onWrite(BLECharacteristic *pCharacterist case BLE_deviceName: // 0x07 logBufLength = snprintf(logBuf + logBufLength, kLogBufCapacity - logBufLength, "<-deviceName"); - returnValue[0] = success; if (rxValue[0] == read) { + returnValue[0] = success; returnString = userConfig->getDeviceName(); } else if (rxValue[0] == write) { + returnValue[0] = success; String str = (char *)pData; str.remove(0, 2); userConfig->setDeviceName(str); @@ -162,13 +169,14 @@ void ss2kCustomCharacteristicCallbacks::onWrite(BLECharacteristic *pCharacterist case BLE_shiftStep: // 0x08 logBufLength += snprintf(logBuf + logBufLength, kLogBufCapacity - logBufLength, "<-shiftStep"); - returnValue[0] = success; if (rxValue[0] == read) { + returnValue[0] = success; returnValue[2] = (uint8_t)(userConfig->getShiftStep() & 0xff); returnValue[3] = (uint8_t)(userConfig->getShiftStep() >> 8); returnLength += 2; } if (rxValue[0] == write) { + returnValue[0] = success; userConfig->setShiftStep(bytes_to_u16(rxValue[3], rxValue[2])); logBufLength += snprintf(logBuf + logBufLength, kLogBufCapacity - logBufLength, "(%d)", userConfig->getShiftStep()); } @@ -176,13 +184,14 @@ void ss2kCustomCharacteristicCallbacks::onWrite(BLECharacteristic *pCharacterist case BLE_stepperPower: // 0x09 logBufLength += snprintf(logBuf + logBufLength, kLogBufCapacity - logBufLength, "<-stepperPower"); - returnValue[0] = success; if (rxValue[0] == read) { + returnValue[0] = success; returnValue[2] = (uint8_t)(userConfig->getStepperPower() & 0xff); returnValue[3] = (uint8_t)(userConfig->getStepperPower() >> 8); returnLength += 2; } if (rxValue[0] == write) { + returnValue[0] = success; userConfig->setStepperPower(bytes_to_u16(rxValue[3], rxValue[2])); ss2k->updateStepperPower(); logBufLength += snprintf(logBuf + logBufLength, kLogBufCapacity - logBufLength, "(%d)", userConfig->getStepperPower()); @@ -191,12 +200,13 @@ void ss2kCustomCharacteristicCallbacks::onWrite(BLECharacteristic *pCharacterist case BLE_stealthChop: // 0x0A logBufLength += snprintf(logBuf + logBufLength, kLogBufCapacity - logBufLength, "<-stealthChop"); - returnValue[0] = success; if (rxValue[0] == read) { + returnValue[0] = success; returnValue[2] = (uint8_t)(userConfig->getStealthChop()); returnLength += 1; } if (rxValue[0] == write) { + returnValue[0] = success; userConfig->setStealthChop(rxValue[2]); ss2k->updateStealthChop(); logBufLength += snprintf(logBuf + logBufLength, kLogBufCapacity - logBufLength, "(%s)", userConfig->getStealthChop() ? "true" : "false"); @@ -205,14 +215,15 @@ void ss2kCustomCharacteristicCallbacks::onWrite(BLECharacteristic *pCharacterist case BLE_inclineMultiplier: { // 0x0B logBufLength += snprintf(logBuf + logBufLength, kLogBufCapacity - logBufLength, "<-inclineMultiplier"); - returnValue[0] = success; int inc = userConfig->getInclineMultiplier(); if (rxValue[0] == read) { + returnValue[0] = success; returnValue[2] = (uint8_t)(inc & 0xff); returnValue[3] = (uint8_t)(inc >> 8); returnLength += 2; } if (rxValue[0] == write) { + returnValue[0] = success; userConfig->setInclineMultiplier(bytes_to_u16(rxValue[3], rxValue[2])); logBufLength += snprintf(logBuf + logBufLength, kLogBufCapacity - logBufLength, "(%f)", userConfig->getInclineMultiplier()); } @@ -220,14 +231,15 @@ void ss2kCustomCharacteristicCallbacks::onWrite(BLECharacteristic *pCharacterist case BLE_powerCorrectionFactor: { // 0x0C logBufLength += snprintf(logBuf + logBufLength, kLogBufCapacity - logBufLength, "<-powerCorrectionFactor"); - returnValue[0] = success; int pcf = userConfig->getPowerCorrectionFactor(); if (rxValue[0] == read) { + returnValue[0] = success; returnValue[2] = (uint8_t)(pcf & 0xff); returnValue[3] = (uint8_t)(pcf >> 8); returnLength += 2; } if (rxValue[0] == write) { + returnValue[0] = success; userConfig->setPowerCorrectionFactor(bytes_to_u16(rxValue[3], rxValue[2])); logBufLength += snprintf(logBuf + logBufLength, kLogBufCapacity - logBufLength, "(%f)", userConfig->getPowerCorrectionFactor()); } @@ -235,12 +247,13 @@ void ss2kCustomCharacteristicCallbacks::onWrite(BLECharacteristic *pCharacterist case BLE_simulateHr: // 0x0D logBufLength += snprintf(logBuf + logBufLength, kLogBufCapacity - logBufLength, "<-simulateHr"); - returnValue[0] = success; if (rxValue[0] == read) { + returnValue[0] = success; returnValue[2] = (uint8_t)(rtConfig->hr.getSimulate()); returnLength += 1; } if (rxValue[0] == write) { + returnValue[0] = success; rtConfig->hr.setSimulate(rxValue[2]); logBufLength += snprintf(logBuf + logBufLength, kLogBufCapacity - logBufLength, "(%s)", rtConfig->hr.getSimulate() ? "true" : "false"); } @@ -248,12 +261,13 @@ void ss2kCustomCharacteristicCallbacks::onWrite(BLECharacteristic *pCharacterist case BLE_simulateWatts: // 0x0E logBufLength += snprintf(logBuf + logBufLength, kLogBufCapacity - logBufLength, "<-simulateWatts"); - returnValue[0] = success; if (rxValue[0] == read) { + returnValue[0] = success; returnValue[2] = (uint8_t)(rtConfig->watts.getSimulate()); returnLength += 1; } if (rxValue[0] == write) { + returnValue[0] = success; rtConfig->watts.setSimulate(rxValue[2]); logBufLength += snprintf(logBuf + logBufLength, kLogBufCapacity - logBufLength, "(%s)", rtConfig->watts.getSimulate() ? "true" : "false"); } @@ -261,12 +275,13 @@ void ss2kCustomCharacteristicCallbacks::onWrite(BLECharacteristic *pCharacterist case BLE_simulateCad: // 0x0F logBufLength += snprintf(logBuf + logBufLength, kLogBufCapacity - logBufLength, "<-simulateCad"); - returnValue[0] = success; if (rxValue[0] == read) { + returnValue[0] = success; returnValue[2] = (uint8_t)(rtConfig->cad.getSimulate()); returnLength += 1; } if (rxValue[0] == write) { + returnValue[0] = success; rtConfig->cad.setSimulate(rxValue[2]); logBufLength += snprintf(logBuf + logBufLength, kLogBufCapacity - logBufLength, "(%s)", rtConfig->cad.getSimulate() ? "true" : "false"); } @@ -274,13 +289,14 @@ void ss2kCustomCharacteristicCallbacks::onWrite(BLECharacteristic *pCharacterist case BLE_FTMSMode: // 0x10 logBufLength += snprintf(logBuf + logBufLength, kLogBufCapacity - logBufLength, "<-FTMSMode"); - returnValue[0] = success; if (rxValue[0] == read) { + returnValue[0] = success; returnValue[2] = (uint8_t)(rtConfig->getFTMSMode() & 0xff); returnValue[3] = (uint8_t)(rtConfig->getFTMSMode() >> 8); returnLength += 2; } if (rxValue[0] == write) { + returnValue[0] = success; rtConfig->setFTMSMode(bytes_to_u16(rxValue[3], rxValue[2])); logBufLength += snprintf(logBuf + logBufLength, kLogBufCapacity - logBufLength, "(%hhu)", rtConfig->getFTMSMode()); } @@ -288,13 +304,13 @@ void ss2kCustomCharacteristicCallbacks::onWrite(BLECharacteristic *pCharacterist case BLE_autoUpdate: // 0x11 logBufLength += snprintf(logBuf + logBufLength, kLogBufCapacity - logBufLength, "<-autoUpdate"); - - returnValue[0] = success; if (rxValue[0] == read) { + returnValue[0] = success; returnValue[2] = (uint8_t)(userConfig->getAutoUpdate()); returnLength += 1; } if (rxValue[0] == write) { + returnValue[0] = success; userConfig->setAutoUpdate(rxValue[2]); logBufLength += snprintf(logBuf + logBufLength, kLogBufCapacity - logBufLength, "(%s)", userConfig->getAutoUpdate() ? "true" : "false"); } @@ -302,10 +318,11 @@ void ss2kCustomCharacteristicCallbacks::onWrite(BLECharacteristic *pCharacterist case BLE_ssid: // 0x12 logBufLength += snprintf(logBuf + logBufLength, kLogBufCapacity - logBufLength, "<-ssid"); - returnValue[0] = success; if (rxValue[0] == read) { + returnValue[0] = success; returnString = userConfig->getSsid(); } else if (rxValue[0] == write) { + returnValue[0] = success; String str = (char *)pData; str.remove(0, 2); userConfig->setSsid(str); @@ -315,10 +332,11 @@ void ss2kCustomCharacteristicCallbacks::onWrite(BLECharacteristic *pCharacterist case BLE_password: // 0x13 logBufLength += snprintf(logBuf + logBufLength, kLogBufCapacity - logBufLength, "<-password"); - returnValue[0] = success; if (rxValue[0] == read) { + returnValue[0] = success; returnString = userConfig->getPassword(); } else if (rxValue[0] == write) { + returnValue[0] = success; String str = (char *)pData; str.remove(0, 2); userConfig->setPassword(str); @@ -328,10 +346,11 @@ void ss2kCustomCharacteristicCallbacks::onWrite(BLECharacteristic *pCharacterist case BLE_foundDevices: // 0x14 logBufLength += snprintf(logBuf + logBufLength, kLogBufCapacity - logBufLength, "<-foundDevices"); - returnValue[0] = success; if (rxValue[0] == read) { + returnValue[0] = success; returnString = userConfig->getFoundDevices(); } else if (rxValue[0] == write) { + returnValue[0] = success; String str = (char *)pData; str.remove(0, 2); userConfig->setFoundDevices(str); @@ -341,10 +360,11 @@ void ss2kCustomCharacteristicCallbacks::onWrite(BLECharacteristic *pCharacterist case BLE_connectedPowerMeter: // 0x15 logBufLength += snprintf(logBuf + logBufLength, kLogBufCapacity - logBufLength, "<-connectedPowerMete"); - returnValue[0] = success; if (rxValue[0] == read) { + returnValue[0] = success; returnString = userConfig->getConnectedPowerMeter(); } else if (rxValue[0] == write) { + returnValue[0] = success; String str = (char *)pData; str.remove(0, 2); userConfig->setConnectedPowerMeter(str); @@ -354,10 +374,11 @@ void ss2kCustomCharacteristicCallbacks::onWrite(BLECharacteristic *pCharacterist case BLE_connectedHeartMonitor: // 0x16 logBufLength += snprintf(logBuf + logBufLength, kLogBufCapacity - logBufLength, "<-connectedHeartMonitor"); - returnValue[0] = success; if (rxValue[0] == read) { + returnValue[0] = success; returnString = userConfig->getConnectedHeartMonitor(); } else if (rxValue[0] == write) { + returnValue[0] = success; String str = (char *)pData; str.remove(0, 2); userConfig->setConnectedHeartMonitor(str); @@ -367,13 +388,14 @@ void ss2kCustomCharacteristicCallbacks::onWrite(BLECharacteristic *pCharacterist case BLE_shifterPosition: // 0x17 logBufLength += snprintf(logBuf + logBufLength, kLogBufCapacity - logBufLength, "<-shifterPosition"); - returnValue[0] = success; if (rxValue[0] == read) { + returnValue[0] = success; returnValue[2] = (uint8_t)(rtConfig->getShifterPosition() & 0xff); returnValue[3] = (uint8_t)(rtConfig->getShifterPosition() >> 8); returnLength += 2; } if (rxValue[0] == write) { + returnValue[0] = success; rtConfig->setShifterPosition(bytes_to_u16(rxValue[3], rxValue[2])); logBufLength += snprintf(logBuf + logBufLength, kLogBufCapacity - logBufLength, "(%d)", rtConfig->getShifterPosition()); SS2K_LOG(CUSTOM_CHAR_LOG_TAG, "%s", logBuf); @@ -392,8 +414,8 @@ void ss2kCustomCharacteristicCallbacks::onWrite(BLECharacteristic *pCharacterist case BLE_targetPosition: // 0x19 logBufLength += snprintf(logBuf + logBufLength, kLogBufCapacity - logBufLength, "<-targetPosition"); - returnValue[0] = success; if (rxValue[0] == read) { + returnValue[0] = success; returnValue[2] = (uint8_t)(ss2k->targetPosition & 0xff); returnValue[3] = (uint8_t)(ss2k->targetPosition >> 8); returnValue[4] = (uint8_t)(ss2k->targetPosition >> 16); @@ -401,6 +423,7 @@ void ss2kCustomCharacteristicCallbacks::onWrite(BLECharacteristic *pCharacterist returnLength += 4; } if (rxValue[0] == write) { + returnValue[0] = success; ss2k->targetPosition = (int32_t((uint8_t)(rxValue[2]) << 0 | (uint8_t)(rxValue[3]) << 8 | (uint8_t)(rxValue[4]) << 16 | (uint8_t)(rxValue[5]) << 24)); logBufLength += snprintf(logBuf + logBufLength, kLogBufCapacity - logBufLength, " (%f)", ss2k->targetPosition); } @@ -408,12 +431,13 @@ void ss2kCustomCharacteristicCallbacks::onWrite(BLECharacteristic *pCharacterist case BLE_externalControl: // 0x1A logBufLength += snprintf(logBuf + logBufLength, kLogBufCapacity - logBufLength, "<-externalControl"); - returnValue[0] = success; if (rxValue[0] == read) { + returnValue[0] = success; returnValue[2] = (uint8_t)(ss2k->externalControl); returnLength += 1; } if (rxValue[0] == write) { + returnValue[0] = success; ss2k->externalControl = static_cast(rxValue[2]); logBufLength += snprintf(logBuf + logBufLength, kLogBufCapacity - logBufLength, "(%s)", ss2k->externalControl ? "On" : "Off"); } @@ -421,12 +445,13 @@ void ss2kCustomCharacteristicCallbacks::onWrite(BLECharacteristic *pCharacterist case BLE_syncMode: // 0x1B logBufLength += snprintf(logBuf + logBufLength, kLogBufCapacity - logBufLength, "<-syncMode"); - returnValue[0] = success; if (rxValue[0] == read) { + returnValue[0] = success; returnValue[2] = (uint8_t)(ss2k->syncMode); returnLength += 1; } if (rxValue[0] == write) { + returnValue[0] = success; ss2k->syncMode = static_cast(rxValue[2]); logBufLength += snprintf(logBuf + logBufLength, kLogBufCapacity - logBufLength, "(%s)", ss2k->syncMode ? "true" : "false"); } @@ -450,13 +475,14 @@ void ss2kCustomCharacteristicCallbacks::onWrite(BLECharacteristic *pCharacterist break; case BLE_stepperSpeed: // 0x1E logBufLength += snprintf(logBuf + logBufLength, kLogBufCapacity - logBufLength, "<-stepperSpeed"); - returnValue[0] = success; if (rxValue[0] == read) { + returnValue[0] = success; returnValue[2] = (uint8_t)(userConfig->getStepperSpeed() & 0xff); returnValue[3] = (uint8_t)(userConfig->getStepperSpeed() >> 8); returnLength += 2; } if (rxValue[0] == write) { + returnValue[0] = success; userConfig->setStepperSpeed(bytes_to_u16(rxValue[3], rxValue[2])); logBufLength += snprintf(logBuf + logBufLength, kLogBufCapacity - logBufLength, "(%d)", userConfig->getStepperSpeed()); ss2k->updateStepperSpeed(); @@ -466,14 +492,15 @@ void ss2kCustomCharacteristicCallbacks::onWrite(BLECharacteristic *pCharacterist case BLE_ERGSensitivity: { // 0x1F logBufLength += snprintf(logBuf + logBufLength, kLogBufCapacity - logBufLength, "<-ERGSensitivity"); - returnValue[0] = success; int pcf = userConfig->getERGSensitivity() * 10; if (rxValue[0] == read) { + returnValue[0] = success; returnValue[2] = (uint8_t)(pcf & 0xff); returnValue[3] = (uint8_t)(pcf >> 8); returnLength += 2; } if (rxValue[0] == write) { + returnValue[0] = success; userConfig->setERGSensitivity(bytes_to_u16(rxValue[3], rxValue[2]) / 10); logBufLength += snprintf(logBuf + logBufLength, kLogBufCapacity - logBufLength, "(%f)", userConfig->getERGSensitivity()); } @@ -481,12 +508,13 @@ void ss2kCustomCharacteristicCallbacks::onWrite(BLECharacteristic *pCharacterist case BLE_shiftDir: // 0x20 logBufLength += snprintf(logBuf + logBufLength, kLogBufCapacity - logBufLength, "<-ShiftDir"); - returnValue[0] = success; if (rxValue[0] == read) { + returnValue[0] = success; returnValue[2] = (uint8_t)(userConfig->getShifterDir()); returnLength += 1; } if (rxValue[0] == write) { + returnValue[0] = success; userConfig->setShifterDir(static_cast(rxValue[2])); logBufLength += snprintf(logBuf + logBufLength, kLogBufCapacity - logBufLength, "(%s)", userConfig->getShifterDir() ? "Normal" : "Reverse"); } @@ -494,13 +522,14 @@ void ss2kCustomCharacteristicCallbacks::onWrite(BLECharacteristic *pCharacterist /////////////// case BLE_minBrakeWatts: // 0x21 logBufLength += snprintf(logBuf + logBufLength, kLogBufCapacity - logBufLength, "<-MinWatts"); - returnValue[0] = success; if (rxValue[0] == read) { + returnValue[0] = success; returnValue[2] = (uint8_t)(userConfig->getMinWatts() & 0xff); returnValue[3] = (uint8_t)(userConfig->getMinWatts() >> 8); returnLength += 2; } if (rxValue[0] == write) { + returnValue[0] = success; userConfig->setMinWatts(bytes_to_u16(rxValue[3], rxValue[2])); logBufLength += snprintf(logBuf + logBufLength, kLogBufCapacity - logBufLength, "(%d)", userConfig->getMinWatts()); SS2K_LOG(CUSTOM_CHAR_LOG_TAG, "%s", logBuf); @@ -508,29 +537,30 @@ void ss2kCustomCharacteristicCallbacks::onWrite(BLECharacteristic *pCharacterist break; case BLE_maxBrakeWatts: // 0x22 logBufLength += snprintf(logBuf + logBufLength, kLogBufCapacity - logBufLength, "<-MaxWatts"); - returnValue[0] = success; if (rxValue[0] == read) { + returnValue[0] = success; returnValue[2] = (uint8_t)(userConfig->getMaxWatts() & 0xff); returnValue[3] = (uint8_t)(userConfig->getMaxWatts() >> 8); returnLength += 2; } if (rxValue[0] == write) { + returnValue[0] = success; userConfig->setMaxWatts(bytes_to_u16(rxValue[3], rxValue[2])); logBufLength += snprintf(logBuf + logBufLength, kLogBufCapacity - logBufLength, "(%d)", userConfig->getMaxWatts()); - SS2K_LOG(CUSTOM_CHAR_LOG_TAG, "%s", logBuf); + SS2K_LOG(CUSTOM_CHAR_LOG_TAG, "%s", logBuf); } break; case BLE_restartBLE: // 0x23 logBufLength += snprintf(logBuf + logBufLength, kLogBufCapacity - logBufLength, "<-restart BLE"); if (rxValue[0] == write) { + returnValue[0] = success; for (auto i : spinBLEClient.myBLEDevices) { if (NimBLEDevice::getClientByPeerAddress(i.peerAddress)->isConnected()) { NimBLEDevice::deleteClient(BLEDevice::getClientByPeerAddress(i.peerAddress)); i.reset(); - spinBLEClient.intentionalDisconnect ++; + spinBLEClient.intentionalDisconnect++; } - } - returnValue[0] = success; + } } break; case BLE_scanBLE: // 0x24 @@ -540,6 +570,13 @@ void ss2kCustomCharacteristicCallbacks::onWrite(BLECharacteristic *pCharacterist returnValue[0] = success; } break; + case BLE_firmwareVer: // 0x25 + logBufLength += snprintf(logBuf + logBufLength, kLogBufCapacity - logBufLength, "<-Firmware Version"); + if (rxValue[0] == read) { + returnValue[0] = success; + returnString = FIRMWARE_VERSION; + } + break; } SS2K_LOG(CUSTOM_CHAR_LOG_TAG, "%s", logBuf);