diff --git a/include/config_defaults.h b/include/config_defaults.h index 2e8b0d1be..03be9c8ea 100644 --- a/include/config_defaults.h +++ b/include/config_defaults.h @@ -85,7 +85,7 @@ #endif #ifndef BLE_ON_BOOT -#define BLE_ON_BOOT false +#define BLE_ON_BOOT true #endif #ifndef DISPLAY_BRIGHTNESS diff --git a/include/services/OswServiceTaskBLEServer.h b/include/services/OswServiceTaskBLEServer.h index 2e3afe03d..cd1b607fd 100644 --- a/include/services/OswServiceTaskBLEServer.h +++ b/include/services/OswServiceTaskBLEServer.h @@ -44,7 +44,7 @@ class OswServiceTaskBLEServer : public OswServiceTask { }; class CurrentTimeCharacteristicCallbacks: public NimBLECharacteristicCallbacks { void onRead(NimBLECharacteristic* pCharacteristic); - uint8_t bytes[9+1]; // will be read from (9 exact-time-256, 1 reason) + uint8_t bytesCurrentTime[9+1]; // will be read from (9 exact-time-256, 1 reason) }; class FirmwareRevisionCharacteristicCallbacks: public NimBLECharacteristicCallbacks { void onRead(NimBLECharacteristic* pCharacteristic); @@ -67,6 +67,75 @@ class OswServiceTaskBLEServer : public OswServiceTask { OswServiceTaskBLEServer* task; }; + class StepsTodayCharacteristicCallbacks: public NimBLECharacteristicCallbacks { + void onRead(NimBLECharacteristic* pCharacteristic); + /* + uint32_t value; + this->bytes[0] = (uint8_t) value; + this->bytes[1] = (uint8_t) (value >> 8); + this->bytes[2] = (uint8_t) (value >> 16); + this->bytes[3] = (uint8_t) (value >> 24); + */ + uint8_t bytesStepsToday[4]; + }; + class StepsTotalWeekCharacteristicCallbacks: public NimBLECharacteristicCallbacks { + void onRead(NimBLECharacteristic* pCharacteristic); + /* + uint32_t value; + this->bytes[0] = (uint8_t) value; + this->bytes[1] = (uint8_t) (value >> 8); + this->bytes[2] = (uint8_t) (value >> 16); + this->bytes[3] = (uint8_t) (value >> 24); + */ + uint8_t bytesStepsTotalWeek[4]; + }; + class StepsTotalCharacteristicCallbacks: public NimBLECharacteristicCallbacks { + void onRead(NimBLECharacteristic* pCharacteristic); + /* + uint32_t value; + this->bytes[0] = (uint8_t) value; + this->bytes[1] = (uint8_t) (value >> 8); + this->bytes[2] = (uint8_t) (value >> 16); + this->bytes[3] = (uint8_t) (value >> 24); + */ + uint8_t bytesStepsTotal[4]; + }; + class StepsAverageCharacteristicCallbacks: public NimBLECharacteristicCallbacks { + void onRead(NimBLECharacteristic* pCharacteristic); + /* + uint32_t value; + this->bytes[0] = (uint8_t) value; + this->bytes[1] = (uint8_t) (value >> 8); + this->bytes[2] = (uint8_t) (value >> 16); + this->bytes[3] = (uint8_t) (value >> 24); + */ + uint8_t bytesStepsAverage[4]; + }; + class StepsDayHistoryCharacteristicCallbacks: public NimBLECharacteristicCallbacks { + void onRead(NimBLECharacteristic* pCharacteristic); + /* + uint32_t value[7]; + this->bytes[0] = (uint8_t) value[0]; + this->bytes[1] = (uint8_t) (value[0] >> 8); + this->bytes[2] = (uint8_t) (value[0] >> 16); + this->bytes[3] = (uint8_t) (value[0] >> 24); + + this->bytes[4] = (uint8_t) value[1]; + this->bytes[5] = (uint8_t) (value[1] >> 8); + this->bytes[6] = (uint8_t) (value[1] >> 16); + this->bytes[7] = (uint8_t) (value[1] >> 24); + + this->bytes[8] = (uint8_t) value[2]; + this->bytes[9] = (uint8_t) (value[2] >> 8); + this->bytes[10] = (uint8_t) (value[2] >> 16); + this->bytes[11] = (uint8_t) (value[2] >> 24); + + ... + + */ + uint8_t bytesStepsDayHistory[4 * 7]; + }; + /// apply the desired BLE state void updateBLEConfig(); @@ -84,6 +153,15 @@ class OswServiceTaskBLEServer : public OswServiceTask { NimBLECharacteristic* characteristicSoftRev = nullptr; NimBLEService* serviceOsw = nullptr; NimBLECharacteristic* characteristicToast = nullptr; +#if OSW_PLATFORM_ENVIRONMENT_ACCELEROMETER == 1 + NimBLECharacteristic* characteristicStepCountTotal = nullptr; + NimBLECharacteristic* characteristicStepCountTotalWeek = nullptr; + NimBLECharacteristic* characteristicStepCountToday = nullptr; +#ifdef OSW_FEATURE_STATS_STEPS + NimBLECharacteristic* characteristicStepCountAverage = nullptr; + NimBLECharacteristic* characteristicStepCountHistory = nullptr; +#endif +#endif // ↓ our own stuff NotifierClient notify = NotifierClient("osw.ble.server"); diff --git a/platformio.ini b/platformio.ini index dc28c9c25..b3fc97c3b 100755 --- a/platformio.ini +++ b/platformio.ini @@ -46,7 +46,7 @@ build_flags = -D OSW_TARGET_PLATFORM_HEADER='"platform/LIGHT_EDITION_V3_3.h"' -D OSW_FEATURE_STATS_STEPS -D OSW_SERVICE_CONSOLE - -D OSW_FEATURE_WIFI + -D OSW_FEATURE_BLE_SERVER build_type = debug [env:LIGHT_EDITION_V4_0] diff --git a/src/services/OswServiceTaskBLEServer.cpp b/src/services/OswServiceTaskBLEServer.cpp index a0721e18c..55373bd2f 100644 --- a/src/services/OswServiceTaskBLEServer.cpp +++ b/src/services/OswServiceTaskBLEServer.cpp @@ -2,17 +2,23 @@ #include "./services/OswServiceTaskBLEServer.h" #include "osw_hal.h" -#define BATTERY_SERVICE_UUID "0000180F-0000-1000-8000-00805f9b34fb" -#define BATTERY_LEVEL_CHARACTERISTIC_UUID "00002A19-0000-1000-8000-00805f9b34fb" -#define BATTERY_LEVEL_STATUS_CHARACTERISTIC_UUID "00002bed-0000-1000-8000-00805f9b34fb" -#define TIME_SERVICE_UUID "00001805-0000-1000-8000-00805f9b34fb" -#define CURRENT_TIME_CHARACTERISTIC_UUID "00002a0c-0000-1000-8000-00805f9b34fb" -#define DEVICE_INFORMATION_SERVICE_UUID "0000180a-0000-1000-8000-00805f9b34fb" -#define FIRMWARE_REVISION_CHARACTERISTIC_UUID "00002a26-0000-1000-8000-00805f9b34fb" -#define HARDWARE_REVISION_CHARACTERISTIC_UUID "00002a27-0000-1000-8000-00805f9b34fb" -#define SOFTWARE_REVISION_CHARACTERISTIC_UUID "00002a28-0000-1000-8000-00805f9b34fb" -#define OSW_SERVICE_UUID "6ab9b834-3e0b-4770-a938-ebaa58d6b852" -#define TOAST_CHARACTERISTIC_UUID "9cadf570-4ec8-40b0-a8d3-cfdb4a90940b" +#define BATTERY_SERVICE_UUID "0000180F-0000-1000-8000-00805f9b34fb" +#define BATTERY_LEVEL_CHARACTERISTIC_UUID "00002A19-0000-1000-8000-00805f9b34fb" +#define BATTERY_LEVEL_STATUS_CHARACTERISTIC_UUID "00002bed-0000-1000-8000-00805f9b34fb" +#define TIME_SERVICE_UUID "00001805-0000-1000-8000-00805f9b34fb" +#define CURRENT_TIME_CHARACTERISTIC_UUID "00002a0c-0000-1000-8000-00805f9b34fb" +#define DEVICE_INFORMATION_SERVICE_UUID "0000180a-0000-1000-8000-00805f9b34fb" +#define FIRMWARE_REVISION_CHARACTERISTIC_UUID "00002a26-0000-1000-8000-00805f9b34fb" +#define HARDWARE_REVISION_CHARACTERISTIC_UUID "00002a27-0000-1000-8000-00805f9b34fb" +#define SOFTWARE_REVISION_CHARACTERISTIC_UUID "00002a28-0000-1000-8000-00805f9b34fb" + +#define OSW_SERVICE_UUID "6ab9b834-3e0b-4770-a938-ebaa58d6b852" +#define TOAST_CHARACTERISTIC_UUID "9cadf570-4ec8-40b0-a8d3-cfdb4a90940b" +#define STEP_COUNT_AVERAGE_CHARACTERISTIC_UUID "3d27dd91-b714-4fe4-8067-620ff7cf13c0" +#define STEP_COUNT_TOTAL_CHARACTERISTIC_UUID "6567e61b-41df-4416-96fc-4e068628bd37" +#define STEP_COUNT_TOTAL_WEEK_CHARACTERISTIC_UUID "0b97315f-883b-4e1e-a745-bc2dd14aedb4" +#define STEP_COUNT_TODAY_CHARACTERISTIC_UUID "143a6279-67ce-43c2-8db2-082a9fbca140" +#define STEP_COUNT_DAY_HISTORY_CHARACTERISTIC_UUID "6b078d24-79ae-4fff-bf3a-2b71dce2b2bb" void OswServiceTaskBLEServer::setup() { OswServiceTask::setup(); @@ -143,6 +149,38 @@ void OswServiceTaskBLEServer::updateBLEConfig() { ); this->characteristicToast->setCallbacks(new ToastCharacteristicCallbacks(this)); +#if OSW_PLATFORM_ENVIRONMENT_ACCELEROMETER == 1 + this->characteristicStepCountToday = this->serviceOsw->createCharacteristic( + STEP_COUNT_TODAY_CHARACTERISTIC_UUID, + NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::READ_ENC | NIMBLE_PROPERTY::READ_AUTHEN + ); + this->characteristicStepCountToday->setCallbacks(new StepsTodayCharacteristicCallbacks()); + + this->characteristicStepCountTotal = this->serviceOsw->createCharacteristic( + STEP_COUNT_TOTAL_CHARACTERISTIC_UUID, + NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::READ_ENC | NIMBLE_PROPERTY::READ_AUTHEN + ); + this->characteristicStepCountTotal->setCallbacks(new StepsTotalCharacteristicCallbacks()); + + this->characteristicStepCountTotalWeek = this->serviceOsw->createCharacteristic( + STEP_COUNT_TOTAL_WEEK_CHARACTERISTIC_UUID, + NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::READ_ENC | NIMBLE_PROPERTY::READ_AUTHEN + ); + this->characteristicStepCountTotalWeek->setCallbacks(new StepsTotalWeekCharacteristicCallbacks()); +#ifdef OSW_FEATURE_STATS_STEPS + this->characteristicStepCountAverage = this->serviceOsw->createCharacteristic( + STEP_COUNT_AVERAGE_CHARACTERISTIC_UUID, + NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::READ_ENC | NIMBLE_PROPERTY::READ_AUTHEN + ); + this->characteristicStepCountAverage->setCallbacks(new StepsAverageCharacteristicCallbacks()); + + this->characteristicStepCountHistory = this->serviceOsw->createCharacteristic( + STEP_COUNT_DAY_HISTORY_CHARACTERISTIC_UUID, + NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::READ_ENC | NIMBLE_PROPERTY::READ_AUTHEN + ); + this->characteristicStepCountHistory->setCallbacks(new StepsDayHistoryCharacteristicCallbacks()); // Create a BLE Characteristic: "Hardware Revision String" +#endif +#endif // Start the service this->serviceOsw->start(); } @@ -172,6 +210,15 @@ void OswServiceTaskBLEServer::updateBLEConfig() { this->serviceDevice->removeCharacteristic(this->characteristicSoftRev, true); this->server->removeService(this->serviceDevice, true); this->serviceOsw->removeCharacteristic(this->characteristicToast, true); +#if OSW_PLATFORM_ENVIRONMENT_ACCELEROMETER == 1 + this->serviceOsw->removeCharacteristic(this->characteristicStepCountToday, true); + this->serviceOsw->removeCharacteristic(this->characteristicStepCountTotal, true); + this->serviceOsw->removeCharacteristic(this->characteristicStepCountTotalWeek, true); +#ifdef OSW_FEATURE_STATS_STEPS + this->serviceOsw->removeCharacteristic(this->characteristicStepCountAverage, true); + this->serviceOsw->removeCharacteristic(this->characteristicStepCountHistory, true); +#endif +#endif this->server->removeService(this->serviceOsw, true); this->server = nullptr; NimBLEDevice::deinit(true); @@ -265,17 +312,17 @@ void OswServiceTaskBLEServer::CurrentTimeCharacteristicCallbacks::onRead(NimBLEC uint32_t dow; OswHal::getInstance()->getDate(offset, &day, &dow); - this->bytes[0] = (uint8_t) year; // Exact Time 256 -> Day Date Time -> Date Time -> Year #1 - this->bytes[1] = (uint8_t) (year >> 8); // Exact Time 256 -> Day Date Time -> Date Time -> Year #2 - this->bytes[2] = (uint8_t) month; // Exact Time 256 -> Day Date Time -> Date Time -> Month - this->bytes[3] = (uint8_t) day; // Exact Time 256 -> Day Date Time -> Date Time -> Day - this->bytes[4] = (uint8_t) hour; // Exact Time 256 -> Day Date Time -> Date Time -> Hours - this->bytes[5] = (uint8_t) minute; // Exact Time 256 -> Day Date Time -> Date Time -> Minutes - this->bytes[6] = (uint8_t) second; // Exact Time 256 -> Day Date Time -> Date Time -> Seconds - this->bytes[7] = (uint8_t) (dow == 0 ? 7 : dow); // Exact Time 256 -> Day Date Time -> Day of Week - this->bytes[8] = 0b00000000; // Exact Time 256 -> Fractions256 - this->bytes[9] = 0b00000001; // Adjust Reason: External Reference Time Update - pCharacteristic->setValue(this->bytes, sizeof(this->bytes)); + this->bytesCurrentTime[0] = (uint8_t) year; // Exact Time 256 -> Day Date Time -> Date Time -> Year #1 + this->bytesCurrentTime[1] = (uint8_t) (year >> 8); // Exact Time 256 -> Day Date Time -> Date Time -> Year #2 + this->bytesCurrentTime[2] = (uint8_t) month; // Exact Time 256 -> Day Date Time -> Date Time -> Month + this->bytesCurrentTime[3] = (uint8_t) day; // Exact Time 256 -> Day Date Time -> Date Time -> Day + this->bytesCurrentTime[4] = (uint8_t) hour; // Exact Time 256 -> Day Date Time -> Date Time -> Hours + this->bytesCurrentTime[5] = (uint8_t) minute; // Exact Time 256 -> Day Date Time -> Date Time -> Minutes + this->bytesCurrentTime[6] = (uint8_t) second; // Exact Time 256 -> Day Date Time -> Date Time -> Seconds + this->bytesCurrentTime[7] = (uint8_t) (dow == 0 ? 7 : dow); // Exact Time 256 -> Day Date Time -> Day of Week + this->bytesCurrentTime[8] = 0b00000000; // Exact Time 256 -> Fractions256 + this->bytesCurrentTime[9] = 0b00000001; // Adjust Reason: External Reference Time Update + pCharacteristic->setValue(this->bytesCurrentTime, sizeof(this->bytesCurrentTime)); } void OswServiceTaskBLEServer::FirmwareRevisionCharacteristicCallbacks::onRead(NimBLECharacteristic* pCharacteristic) { @@ -286,7 +333,66 @@ void OswServiceTaskBLEServer::HardwareRevisionCharacteristicCallbacks::onRead(Ni this->value = String(PIO_ENV_NAME); pCharacteristic->setValue((uint8_t*) this->value.c_str(), this->value.length()); } +#if OSW_PLATFORM_ENVIRONMENT_ACCELEROMETER == 1 +void OswServiceTaskBLEServer::StepsTodayCharacteristicCallbacks::onRead(NimBLECharacteristic* pCharacteristic) { + uint32_t stepsToday = OswHal::getInstance()->environment()->getStepsToday(); + + this->bytesStepsToday[0] = (uint8_t) stepsToday; + this->bytesStepsToday[1] = (uint8_t) (stepsToday >> 8); + this->bytesStepsToday[2] = (uint8_t) (stepsToday >> 16); + this->bytesStepsToday[3] = (uint8_t) (stepsToday >> 24); + + pCharacteristic->setValue(this->bytesStepsToday, sizeof(this->bytesStepsToday)); +} + +void OswServiceTaskBLEServer::StepsTotalCharacteristicCallbacks::onRead(NimBLECharacteristic* pCharacteristic) { + uint32_t stepsTotal = OswHal::getInstance()->environment()->getStepsTotal(); + + this->bytesStepsTotal[0] = (uint8_t) stepsTotal; + this->bytesStepsTotal[1] = (uint8_t) (stepsTotal >> 8); + this->bytesStepsTotal[2] = (uint8_t) (stepsTotal >> 16); + this->bytesStepsTotal[3] = (uint8_t) (stepsTotal >> 24); + + pCharacteristic->setValue(this->bytesStepsTotal, sizeof(this->bytesStepsTotal)); +} + +void OswServiceTaskBLEServer::StepsTotalWeekCharacteristicCallbacks::onRead(NimBLECharacteristic* pCharacteristic) { + uint32_t stepsTotalWeek = OswHal::getInstance()->environment()->getStepsTotalWeek(); + + this->bytesStepsTotalWeek[0] = (uint8_t) stepsTotalWeek; + this->bytesStepsTotalWeek[1] = (uint8_t) (stepsTotalWeek >> 8); + this->bytesStepsTotalWeek[2] = (uint8_t) (stepsTotalWeek >> 16); + this->bytesStepsTotalWeek[3] = (uint8_t) (stepsTotalWeek >> 24); + + pCharacteristic->setValue(this->bytesStepsTotalWeek, sizeof(this->bytesStepsTotalWeek)); +} +#ifdef OSW_FEATURE_STATS_STEPS +void OswServiceTaskBLEServer::StepsAverageCharacteristicCallbacks::onRead(NimBLECharacteristic* pCharacteristic) { + uint32_t stepsAverage = OswHal::getInstance()->environment()->getStepsAverage(); + + this->bytesStepsAverage[0] = (uint8_t) stepsAverage; + this->bytesStepsAverage[1] = (uint8_t) (stepsAverage >> 8); + this->bytesStepsAverage[2] = (uint8_t) (stepsAverage >> 16); + this->bytesStepsAverage[3] = (uint8_t) (stepsAverage >> 24); + pCharacteristic->setValue(this->bytesStepsAverage, sizeof(this->bytesStepsAverage)); +} + +void OswServiceTaskBLEServer::StepsDayHistoryCharacteristicCallbacks::onRead(NimBLECharacteristic* pCharacteristic) { + + for (uint8_t indexOfWeek = 0; indexOfWeek < 7; indexOfWeek++) + { + uint32_t value = OswHal::getInstance()->environment()->getStepsOnDay(indexOfWeek, false); + this->bytesStepsDayHistory[0 + indexOfWeek * 4] = (uint8_t) value; + this->bytesStepsDayHistory[1 + indexOfWeek * 4] = (uint8_t) (value >> 8); + this->bytesStepsDayHistory[2 + indexOfWeek * 4] = (uint8_t) (value >> 16); + this->bytesStepsDayHistory[3 + indexOfWeek * 4] = (uint8_t) (value >> 24); + } + + pCharacteristic->setValue(this->bytesStepsDayHistory, sizeof(this->bytesStepsDayHistory)); +} +#endif +#endif void OswServiceTaskBLEServer::SoftwareRevisionCharacteristicCallbacks::onRead(NimBLECharacteristic* pCharacteristic) { this->value = String(GIT_COMMIT_HASH) + " (" + String(GIT_BRANCH_NAME) + ")"; pCharacteristic->setValue((uint8_t*) this->value.c_str(), this->value.length()); diff --git a/src/services/OswServiceTaskConsole.cpp b/src/services/OswServiceTaskConsole.cpp index a0956b023..62b7bcea3 100644 --- a/src/services/OswServiceTaskConsole.cpp +++ b/src/services/OswServiceTaskConsole.cpp @@ -126,6 +126,10 @@ void OswServiceTaskConsole::runPrompt() { NotifierClient notify("osw.console"); notify.showToast(toast); #endif + // Test + } else if (this->m_inputBuffer.find("notify ") == 0 and this->m_inputBuffer.length() > 7) { + auto value = this->m_inputBuffer.substr(7); + OswUI::getInstance()->showNotification(value, false); } else if (this->m_inputBuffer == "time") { Serial.println(OswHal::getInstance()->getUTCTime()); } else if (this->m_inputBuffer == "wipe") {