diff --git a/include/Custom_Characteristic.h b/include/Custom_Characteristic.h index 0c4c632f..ab63cd81 100644 --- a/include/Custom_Characteristic.h +++ b/include/Custom_Characteristic.h @@ -38,4 +38,5 @@ #define BLE_reboot 0x1C #define BLE_resetToDefaults 0x1D #define BLE_stepperSpeed 0x1E -#define BLE_ERGSensitivity 0x1F \ No newline at end of file +#define BLE_ERGSensitivity 0x1F +#define BLE_shiftDir 0x20 \ No newline at end of file diff --git a/src/BLE_Client.cpp b/src/BLE_Client.cpp index f2130357..2beea8cf 100644 --- a/src/BLE_Client.cpp +++ b/src/BLE_Client.cpp @@ -63,9 +63,9 @@ void bleClientTask(void *pvParameters) { long int scanDelay = millis(); for (;;) { vTaskDelay(BLE_CLIENT_DELAY / portTICK_PERIOD_MS); // Delay a second between loops. - if((millis()-scanDelay)*2 > (BLE_RECONNECT_SCAN_DURATION*1000)){ - spinBLEClient.checkBLEReconnect(); - scanDelay = millis(); + if ((millis() - scanDelay) * 2 > (BLE_RECONNECT_SCAN_DURATION * 1000)) { + spinBLEClient.checkBLEReconnect(); + scanDelay = millis(); } // if (spinBLEClient.doScan && (spinBLEClient.scanRetries > 0) && !NimBLEDevice::getScan()->isScanning()) { // spinBLEClient.scanRetries--; @@ -377,48 +377,43 @@ void MyClientCallback::onAuthenticationComplete(ble_gap_conn_desc desc) { SS2K_L */ void MyAdvertisedDeviceCallback::onResult(BLEAdvertisedDevice *advertisedDevice) { - auto advertisedDeviceInfo = advertisedDevice->toString(); - ss2k_remove_newlines(&advertisedDeviceInfo); - String aDevName; - if (advertisedDevice->haveName()) { - aDevName = String(advertisedDevice->getName().c_str()); - } else { - aDevName = ""; - } + char aDevName[40]; //40 should be enough for anybody! + (advertisedDevice->haveName()) ? strcpy(aDevName, advertisedDevice->getName().c_str()) : strcpy(aDevName, advertisedDevice->getAddress().toString().c_str()); + if ((advertisedDevice->haveServiceUUID()) && (advertisedDevice->isAdvertisingService(CYCLINGPOWERSERVICE_UUID) || (advertisedDevice->isAdvertisingService(FLYWHEEL_UART_SERVICE_UUID) && aDevName == FLYWHEEL_BLE_NAME) || advertisedDevice->isAdvertisingService(FITNESSMACHINESERVICE_UUID) || advertisedDevice->isAdvertisingService(HEARTSERVICE_UUID) || advertisedDevice->isAdvertisingService(ECHELON_DEVICE_UUID) || advertisedDevice->isAdvertisingService(HID_SERVICE_UUID))) { - SS2K_LOG(BLE_CLIENT_LOG_TAG, "Matching Device Name: %s", aDevName.c_str()); + SS2K_LOG(BLE_CLIENT_LOG_TAG, "Matching Device Name: %s", aDevName); if (advertisedDevice->getServiceUUID() == HID_SERVICE_UUID) { - if (String(userConfig->getConnectedRemote()) == "any") { + if (strcmp(userConfig->getConnectedRemote(), "any") == 0) { SS2K_LOG(BLE_CLIENT_LOG_TAG, "Remote String Matched Any"); // continue - } else if (aDevName != String(userConfig->getConnectedRemote()) || (String(userConfig->getConnectedRemote()) == "none")) { - SS2K_LOG(BLE_CLIENT_LOG_TAG, "Skipping non-selected Remote |%s|%s", aDevName.c_str(), userConfig->getConnectedRemote()); + } else if ((strcmp(aDevName, userConfig->getConnectedRemote()) == 0) || (strcmp(userConfig->getConnectedRemote(), "none") == 0)) { + SS2K_LOG(BLE_CLIENT_LOG_TAG, "Skipping non-selected Remote |%s|%s", aDevName, userConfig->getConnectedRemote()); return; - } else if (aDevName == String(userConfig->getConnectedRemote())) { - SS2K_LOG(BLE_CLIENT_LOG_TAG, "Remote String Matched %s", aDevName.c_str()); + } else if (strcmp(aDevName, userConfig->getConnectedRemote()) == 0) { + SS2K_LOG(BLE_CLIENT_LOG_TAG, "Remote String Matched %s", aDevName); } } else if (advertisedDevice->getServiceUUID() == HEARTSERVICE_UUID) { - if (String(userConfig->getConnectedHeartMonitor()) == "any") { + if (strcmp(userConfig->getConnectedHeartMonitor(), "any") == 0) { SS2K_LOG(BLE_CLIENT_LOG_TAG, "HR String Matched Any"); // continue - } else if (aDevName != String(userConfig->getConnectedHeartMonitor()) || (String(userConfig->getConnectedHeartMonitor()) == "none")) { - SS2K_LOG(BLE_CLIENT_LOG_TAG, "Skipping non-selected HRM |%s|%s", aDevName.c_str(), userConfig->getConnectedHeartMonitor()); + } else if ((strcmp(aDevName, userConfig->getConnectedHeartMonitor()) != 0) || (strcmp(userConfig->getConnectedHeartMonitor(), "none") == 0)) { + SS2K_LOG(BLE_CLIENT_LOG_TAG, "Skipping non-selected HRM |%s|%s", aDevName, userConfig->getConnectedHeartMonitor()); return; - } else if (aDevName == String(userConfig->getConnectedHeartMonitor())) { - SS2K_LOG(BLE_CLIENT_LOG_TAG, "HR String Matched %s", aDevName.c_str()); + } else if (strcmp(aDevName, userConfig->getConnectedHeartMonitor()) == 0) { + SS2K_LOG(BLE_CLIENT_LOG_TAG, "HR String Matched %s", aDevName); } } else { - if (String(userConfig->getConnectedPowerMeter()) == "any") { + if (strcmp(userConfig->getConnectedPowerMeter(), "any") == 0) { SS2K_LOG(BLE_CLIENT_LOG_TAG, "PM String Matched Any"); // continue - } else if (aDevName != String(userConfig->getConnectedPowerMeter()) || (String(userConfig->getConnectedPowerMeter()) == "none")) { - SS2K_LOG(BLE_CLIENT_LOG_TAG, "Skipping non-selected PM |%s|%s", aDevName.c_str(), userConfig->getConnectedPowerMeter()); + } else if ((strcmp(aDevName, userConfig->getConnectedPowerMeter()) != 0) || (strcmp(userConfig->getConnectedPowerMeter(), "none") == 0)) { + SS2K_LOG(BLE_CLIENT_LOG_TAG, "Skipping non-selected PM |%s|%s", aDevName, userConfig->getConnectedPowerMeter()); return; - } else if (aDevName == String(userConfig->getConnectedPowerMeter())) { - SS2K_LOG(BLE_CLIENT_LOG_TAG, "PM String Matched %s", aDevName.c_str()); + } else if (strcmp(aDevName, userConfig->getConnectedPowerMeter()) == 0) { + SS2K_LOG(BLE_CLIENT_LOG_TAG, "PM String Matched %s", aDevName); } } for (size_t i = 0; i < NUM_BLE_DEVICES; i++) { @@ -445,7 +440,7 @@ void SpinBLEClient::scanProcess(int duration) { pBLEScan->setInterval(49); // 97 pBLEScan->setWindow(33); // 67 pBLEScan->setDuplicateFilter(true); - pBLEScan->setActiveScan(true); //might cause memory leak if true - undetermined. We don't get device names without it. + pBLEScan->setActiveScan(true); // might cause memory leak if true - undetermined. We don't get device names without it. BLEScanResults foundDevices = pBLEScan->start(duration, true); this->dontBlockScan = false; // Load the scan into a Json String diff --git a/src/Custom_Characteristic.cpp b/src/Custom_Characteristic.cpp index 1b679ec8..571627e5 100644 --- a/src/Custom_Characteristic.cpp +++ b/src/Custom_Characteristic.cpp @@ -415,7 +415,7 @@ void ss2kCustomCharacteristicCallbacks::onWrite(BLECharacteristic *pCharacterist } if (rxValue[0] == write) { ss2k->externalControl = static_cast(rxValue[2]); - logBufLength += snprintf(logBuf + logBufLength, kLogBufCapacity - logBufLength, "(%B)", ss2k->externalControl); + logBufLength += snprintf(logBuf + logBufLength, kLogBufCapacity - logBufLength, "(%s)", ss2k->externalControl? "On" : "Off"); } break; @@ -436,7 +436,7 @@ void ss2kCustomCharacteristicCallbacks::onWrite(BLECharacteristic *pCharacterist logBufLength += snprintf(logBuf + logBufLength, kLogBufCapacity - logBufLength, "<-reboot"); if (rxValue[0] == write) { ss2k->rebootFlag = true; - returnValue[0] = success; + returnValue[0] = success; } break; @@ -479,6 +479,19 @@ void ss2kCustomCharacteristicCallbacks::onWrite(BLECharacteristic *pCharacterist logBufLength += snprintf(logBuf + logBufLength, kLogBufCapacity - logBufLength, "(%f)", userConfig->getERGSensitivity()); } } break; + + case BLE_shiftDir: // 0x20 + logBufLength += snprintf(logBuf + logBufLength, kLogBufCapacity - logBufLength, "<-ShiftDir"); + returnValue[0] = success; + if (rxValue[0] == read) { + returnValue[2] = (uint8_t)(userConfig->getShifterDir()); + returnLength += 1; + } + if (rxValue[0] == write) { + userConfig->setShifterDir(static_cast(rxValue[2])); + logBufLength += snprintf(logBuf + logBufLength, kLogBufCapacity - logBufLength, "(%s)", userConfig->getShifterDir() ? "Normal" : "Reverse"); + } + break; } SS2K_LOG(CUSTOM_CHAR_LOG_TAG, "%s", logBuf);