diff --git a/include/BLE_Custom_Characteristic.h b/include/BLE_Custom_Characteristic.h index ce91a0be..d95df029 100644 --- a/include/BLE_Custom_Characteristic.h +++ b/include/BLE_Custom_Characteristic.h @@ -60,6 +60,8 @@ const uint8_t BLE_simulatedTargetWatts = 0x28; // current target watts const uint8_t BLE_simulateTargetWatts = 0x29; // are we sending target watts const uint8_t BLE_hMin = 0x2A; // Minimum homing value const uint8_t BLE_hMax = 0x2B; // Maximum homing value +const uint8_t BLE_totalTravel = 0x2C; // Total travel +const uint8_t BLE_calculatedTotalTravel = 0x2D; // Calculated Total Travel class BLE_ss2kCustomCharacteristic { public: diff --git a/include/ERG_Mode.h b/include/ERG_Mode.h index afdba436..4880333a 100644 --- a/include/ERG_Mode.h +++ b/include/ERG_Mode.h @@ -19,6 +19,7 @@ class PowerEntry { public: int watts; + int resistance; int32_t targetPosition; int cad; int readings; @@ -28,6 +29,7 @@ class PowerEntry { this->targetPosition = 0; this->cad = 0; this->readings = 0; + this->resistance = 0; } }; diff --git a/include/SmartSpin_parameters.h b/include/SmartSpin_parameters.h index 7174ede9..0b88547e 100644 --- a/include/SmartSpin_parameters.h +++ b/include/SmartSpin_parameters.h @@ -65,6 +65,8 @@ class RuntimeParameters { int minResistance = -DEFAULT_RESISTANCE_RANGE; int maxResistance = DEFAULT_RESISTANCE_RANGE; bool simTargetWatts = false; + int32_t totalTravel = INT32_MIN; + int16_t calculatedTotalTravel = INT16_MIN; public: Measurement watts; @@ -104,6 +106,12 @@ class RuntimeParameters { void setMaxResistance(int max) { maxResistance = max; } int getMaxResistance() { return maxResistance; } + void setTotalTravel(int32_t tT) { totalTravel = tT; } + int32_t getTotalTravel() { return totalTravel; } + + void setCalculatedTotalTravel(int16_t calctT) { calculatedTotalTravel = calctT; } + int16_t getCalculatedTotalTravel() { return calculatedTotalTravel; } + String returnJSON(); }; diff --git a/src/BLE_Custom_Characteristic.cpp b/src/BLE_Custom_Characteristic.cpp index cce568ea..8dcab6d6 100644 --- a/src/BLE_Custom_Characteristic.cpp +++ b/src/BLE_Custom_Characteristic.cpp @@ -738,6 +738,39 @@ void BLE_ss2kCustomCharacteristic::process(std::string rxValue) { ss2k->setTargetPosition(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)", userConfig->getHMax()); } + + case BLE_totalTravel: // 0x2C + logBufLength += snprintf(logBuf + logBufLength, kLogBufCapacity - logBufLength, "<-totalTravel"); + if (rxValue[0] == cc_read) { + returnValue[0] = cc_success; + returnValue[2] = (uint8_t)(rtConfig->getTotalTravel() & 0xff); + returnValue[3] = (uint8_t)(rtConfig->getTotalTravel() >> 8); + returnValue[4] = (uint8_t)(rtConfig->getTotalTravel() >> 16); + returnValue[5] = (uint8_t)(rtConfig->getTotalTravel() >> 24); + returnLength += 4; + } + if (rxValue[0] == cc_write) { + returnValue[0] = cc_success; + ss2k->setTargetPosition(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)", rtConfig->getTotalTravel()); + } + + case BLE_calculatedTotalTravel: // 0x2D + logBufLength += snprintf(logBuf + logBufLength, kLogBufCapacity - logBufLength, "<-calculatedTotalTravel"); + if (rxValue[0] == cc_read) { + returnValue[0] = cc_success; + returnValue[2] = (uint8_t)(rtConfig->getCalculatedTotalTravel() & 0xff); + returnValue[3] = (uint8_t)(rtConfig->getCalculatedTotalTravel() >> 8); + returnValue[4] = (uint8_t)(rtConfig->getCalculatedTotalTravel() >> 16); + returnValue[5] = (uint8_t)(rtConfig->getCalculatedTotalTravel() >> 24); + returnLength += 4; + } + if (rxValue[0] == cc_write) { + returnValue[0] = cc_success; + ss2k->setTargetPosition(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)", rtConfig->getCalculatedTotalTravel()); + } + break; } @@ -900,4 +933,16 @@ void BLE_ss2kCustomCharacteristic::parseNemit() { BLE_ss2kCustomCharacteristic::notify(BLE_hMax); return; } + + if(rtConfig->getTotalTravel() != _oldRTParams.getTotalTravel()){ + _oldRTParams.setTotalTravel(rtConfig->getTotalTravel()); + BLE_ss2kCustomCharacteristic::notify(BLE_totalTravel); + return; + } + + if(rtConfig->getCalculatedTotalTravel() != _oldRTParams.getCalculatedTotalTravel()){ + _oldRTParams.setCalculatedTotalTravel(rtConfig->getCalculatedTotalTravel()); + BLE_ss2kCustomCharacteristic::notify(BLE_calculatedTotalTravel); + return; + } } \ No newline at end of file diff --git a/src/BLE_Fitness_Machine_Service.cpp b/src/BLE_Fitness_Machine_Service.cpp index e3c9bac1..5491dcb2 100644 --- a/src/BLE_Fitness_Machine_Service.cpp +++ b/src/BLE_Fitness_Machine_Service.cpp @@ -130,13 +130,13 @@ void BLE_Fitness_Machine_Service::processFTMSWrite() { switch ((uint8_t)rxValue[0]) { case FitnessMachineControlPointProcedure::RequestControl: returnValue[2] = FitnessMachineControlPointResultCode::Success; // 0x01; - rtConfig->watts.setTarget(0); - rtConfig->setSimTargetWatts(false); logBufLength += snprintf(logBuf + logBufLength, kLogBufCapacity - logBufLength, "-> Control Request"); ftmsTrainingStatus[1] = FitnessMachineTrainingStatus::Idle; // 0x01; fitnessMachineTrainingStatus->setValue(ftmsTrainingStatus, 2); ftmsStatus = {FitnessMachineStatus::StartedOrResumedByUser}; pCharacteristic->setValue(returnValue, 3); + rtConfig->watts.setTarget(0); + rtConfig->setSimTargetWatts(false); break; case FitnessMachineControlPointProcedure::Reset: { @@ -147,6 +147,8 @@ void BLE_Fitness_Machine_Service::processFTMSWrite() { ftmsStatus = {FitnessMachineStatus::Reset}; fitnessMachineTrainingStatus->setValue(ftmsTrainingStatus, 2); pCharacteristic->setValue(returnValue, 3); + rtConfig->watts.setTarget(0); + rtConfig->setSimTargetWatts(false); } break; case FitnessMachineControlPointProcedure::SetTargetInclination: { @@ -233,6 +235,8 @@ void BLE_Fitness_Machine_Service::processFTMSWrite() { ftmsStatus = {FitnessMachineStatus::StoppedOrPausedByUser}; ftmsTrainingStatus[1] = FitnessMachineTrainingStatus::Other; // 0x00; fitnessMachineTrainingStatus->setValue(ftmsTrainingStatus, 2); + rtConfig->watts.setTarget(0); + rtConfig->setSimTargetWatts(false); } break; diff --git a/src/Main.cpp b/src/Main.cpp index 477b2612..a3a968de 100644 --- a/src/Main.cpp +++ b/src/Main.cpp @@ -639,6 +639,8 @@ void SS2K::goHome(bool bothDirections) { if (bothDirections) { userConfig->setHMin(rtConfig->getMinStep()); userConfig->setHMax(rtConfig->getMaxStep()); + rtConfig->setTotalTravel(userConfig->getHMax() - userConfig->getHMin()); + rtConfig->setCalculatedTotalTravel(rtConfig->getTotalTravel()/100); } // In case this was only one direction homing. rtConfig->setMaxStep(userConfig->getHMax()); diff --git a/src/SensorCollector.cpp b/src/SensorCollector.cpp index d587b40d..09b007f4 100644 --- a/src/SensorCollector.cpp +++ b/src/SensorCollector.cpp @@ -64,6 +64,10 @@ void collectAndSet(NimBLEUUID charUUID, NimBLEUUID serviceUUID, NimBLEAddress ad if (sensorData->hasResistance()) { if ((rtConfig->getMaxResistance() == MAX_PELOTON_RESISTANCE) && (charUUID != PELOTON_DATA_UUID)) { // Peloton connected but using BLE Power Meter. So skip resistance for UUID's that aren't Peloton. + } else if (rtConfig->getHomed()) { + int resistance = rtConfig->getMaxResistance() *(ss2k->getCurrentPosition()/rtConfig->getCalculatedTotalTravel()); //might be total travel without dividing by 100 + rtConfig->resistance.setValue(resistance); + logBufLength += snprintf(logBuf + logBufLength, kLogBufMaxLength - logBufLength, " RS(%d)", resistance % 1000); } else { int resistance = sensorData->getResistance(); rtConfig->resistance.setValue(resistance);