From ff7c2fa9ccadd0919db3541ee83e8228f88f0719 Mon Sep 17 00:00:00 2001 From: hubmartin Date: Thu, 5 Aug 2021 22:20:00 +0200 Subject: [PATCH 01/25] Echo example working --- src/CMakeLists.txt | 2 + src/components/ble/BleNus.cpp | 70 +++++++++++++++++++++++++ src/components/ble/BleNus.h | 52 ++++++++++++++++++ src/components/ble/NimbleController.cpp | 1 + src/components/ble/NimbleController.h | 2 + 5 files changed, 127 insertions(+) create mode 100644 src/components/ble/BleNus.cpp create mode 100644 src/components/ble/BleNus.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 40e1f2a554..9c4b211c53 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -456,6 +456,7 @@ list(APPEND SOURCE_FILES components/motion/MotionController.cpp components/ble/NimbleController.cpp components/ble/DeviceInformationService.cpp + components/ble/BleNus.cpp components/ble/CurrentTimeClient.cpp components/ble/AlertNotificationClient.cpp components/ble/DfuService.cpp @@ -522,6 +523,7 @@ list(APPEND RECOVERY_SOURCE_FILES components/motion/MotionController.cpp components/ble/NimbleController.cpp components/ble/DeviceInformationService.cpp + components/ble/BleNus.cpp components/ble/CurrentTimeClient.cpp components/ble/AlertNotificationClient.cpp components/ble/DfuService.cpp diff --git a/src/components/ble/BleNus.cpp b/src/components/ble/BleNus.cpp new file mode 100644 index 0000000000..d8c21df09f --- /dev/null +++ b/src/components/ble/BleNus.cpp @@ -0,0 +1,70 @@ +#include "BleNus.h" + +using namespace Pinetime::Controllers; + +constexpr ble_uuid128_t BleNus::nusServiceUuid; +constexpr ble_uuid128_t BleNus::rxCharacteristicUuid; +constexpr ble_uuid128_t BleNus::txCharacteristicUuid; +uint16_t BleNus::attr_read_handle; + + +int BleNusCallback(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg) { + auto deviceInformationService = static_cast(arg); + return deviceInformationService->OnDeviceInfoRequested(conn_handle, attr_handle, ctxt); +} + +void BleNus::Init() { + int res = 0; + res = ble_gatts_count_cfg(serviceDefinition); + ASSERT(res == 0); + + res = ble_gatts_add_svcs(serviceDefinition); + ASSERT(res == 0); +} + +int BleNus::OnDeviceInfoRequested(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt) { + + struct os_mbuf *om = ctxt->om; + switch (ctxt->op) { + case BLE_GATT_ACCESS_OP_WRITE_CHR: + while(om) { + //console_write((char *)om->om_data, om->om_len); + + om = ble_hs_mbuf_from_flat((char *)om->om_data, om->om_len); + if (om) { + ble_gattc_notify_custom(conn_handle, attr_read_handle, om); + } + + om = SLIST_NEXT(om, om_next); + } + //console_write("\n", 1); + return 0; + default: + assert(0); + return BLE_ATT_ERR_UNLIKELY; + } +} + +BleNus::BleNus() + : characteristicDefinition {{ + .uuid = (ble_uuid_t*) &rxCharacteristicUuid, + .access_cb = BleNusCallback, + .arg = this, + .flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_WRITE_NO_RSP, + }, + { + .uuid = (ble_uuid_t*) &txCharacteristicUuid, + .access_cb = BleNusCallback, + .arg = this, + .flags = BLE_GATT_CHR_F_NOTIFY, + .val_handle = &attr_read_handle + }, + {0}}, + serviceDefinition { + {/* Device Information Service */ + .type = BLE_GATT_SVC_TYPE_PRIMARY, + .uuid = (ble_uuid_t*) &nusServiceUuid, + .characteristics = characteristicDefinition}, + {0}, + } { +} diff --git a/src/components/ble/BleNus.h b/src/components/ble/BleNus.h new file mode 100644 index 0000000000..ea396fa795 --- /dev/null +++ b/src/components/ble/BleNus.h @@ -0,0 +1,52 @@ +#pragma once +#define min // workaround: nimble's min/max macros conflict with libstdc++ +#define max +#include +#undef max +#undef min +#include "Version.h" + +/* c7e60000-78fc-48fe-8e23-433b3a1942d0 +#define NAVIGATION_SERVICE_UUID_BASE \ + { 0xd0, 0x42, 0x19, 0x3a, 0x3b, 0x43, 0x23, 0x8e, 0xfe, 0x48, 0xfc, 0x78, 0x00, 0x00, 0x00, 0x00 } +*/ + +// Service UUID +// 6E400001-B5A3-F393-E0A9-E50E24DCCA9E +#define NUS_SERVICE_UUID_BASE \ + { 0x9e, 0xca, 0xdc, 0x24, 0x0e, 0xe5, 0xa9, 0xe0, 0x93, 0xf3, 0xa3, 0xb5, 0x01, 0x00, 0x40, 0x6e } + +//6E400002-B5A3-F393-E0A9-E50E24DCCA9E +#define NUS_CHARACTERTISTIC_RX_UUID \ + { 0x9e, 0xca, 0xdc, 0x24, 0x0e, 0xe5, 0xa9, 0xe0, 0x93, 0xf3, 0xa3, 0xb5, 0x02, 0x00, 0x40, 0x6e } + +//6E400003-B5A3-F393-E0A9-E50E24DCCA9E +#define NUS_CHARACTERTISTIC_TX_UUID \ + { 0x9e, 0xca, 0xdc, 0x24, 0x0e, 0xe5, 0xa9, 0xe0, 0x93, 0xf3, 0xa3, 0xb5, 0x03, 0x00, 0x40, 0x6e } + + +namespace Pinetime { + namespace Controllers { + class BleNus { + public: + BleNus(); + void Init(); + + int OnDeviceInfoRequested(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt); + + private: + + static uint16_t attr_read_handle; + + + static constexpr ble_uuid128_t nusServiceUuid {.u {.type = BLE_UUID_TYPE_128}, .value = NUS_SERVICE_UUID_BASE}; + + static constexpr ble_uuid128_t rxCharacteristicUuid {.u {.type = BLE_UUID_TYPE_128}, .value = NUS_CHARACTERTISTIC_RX_UUID}; + + static constexpr ble_uuid128_t txCharacteristicUuid {.u {.type = BLE_UUID_TYPE_128}, .value = NUS_CHARACTERTISTIC_TX_UUID}; + + struct ble_gatt_chr_def characteristicDefinition[3]; + struct ble_gatt_svc_def serviceDefinition[2]; + }; + } +} \ No newline at end of file diff --git a/src/components/ble/NimbleController.cpp b/src/components/ble/NimbleController.cpp index 5eb227bf00..d134915c5d 100644 --- a/src/components/ble/NimbleController.cpp +++ b/src/components/ble/NimbleController.cpp @@ -55,6 +55,7 @@ void NimbleController::Init() { ble_svc_gatt_init(); deviceInformationService.Init(); + bleNus.Init(); currentTimeClient.Init(); currentTimeService.Init(); musicService.Init(); diff --git a/src/components/ble/NimbleController.h b/src/components/ble/NimbleController.h index 0cfe983c6b..ca1ee6a906 100644 --- a/src/components/ble/NimbleController.h +++ b/src/components/ble/NimbleController.h @@ -13,6 +13,7 @@ #include "CurrentTimeClient.h" #include "CurrentTimeService.h" #include "DeviceInformationService.h" +#include "BleNus.h" #include "DfuService.h" #include "ImmediateAlertService.h" #include "MusicService.h" @@ -82,6 +83,7 @@ namespace Pinetime { Pinetime::Controllers::DfuService dfuService; DeviceInformationService deviceInformationService; + BleNus bleNus; CurrentTimeClient currentTimeClient; AlertNotificationService anService; AlertNotificationClient alertNotificationClient; From 5620ab8aced761bbde396cdab3fec21247e9c93a Mon Sep 17 00:00:00 2001 From: hubmartin Date: Thu, 5 Aug 2021 22:52:35 +0200 Subject: [PATCH 02/25] WIP: Save conn handle, add print --- src/components/ble/BleNus.cpp | 30 +++++++++++++++++++++---- src/components/ble/BleNus.h | 5 ++++- src/components/ble/NimbleController.cpp | 3 +++ 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/src/components/ble/BleNus.cpp b/src/components/ble/BleNus.cpp index d8c21df09f..a81285fbe8 100644 --- a/src/components/ble/BleNus.cpp +++ b/src/components/ble/BleNus.cpp @@ -6,7 +6,7 @@ constexpr ble_uuid128_t BleNus::nusServiceUuid; constexpr ble_uuid128_t BleNus::rxCharacteristicUuid; constexpr ble_uuid128_t BleNus::txCharacteristicUuid; uint16_t BleNus::attr_read_handle; - +//static uint16_t conn_handle; int BleNusCallback(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg) { auto deviceInformationService = static_cast(arg); @@ -22,17 +22,39 @@ void BleNus::Init() { ASSERT(res == 0); } +void BleNus::SetConnectionHandle(uint16_t connection_handle) { + conn_handle = connection_handle; + +} + +void BleNus::Print(char *str) +{ + struct os_mbuf *om; + om = ble_hs_mbuf_from_flat(str, strlen(str)); + + if (om) { + ble_gattc_notify_custom(conn_handle, attr_read_handle, om); + } +} + + int BleNus::OnDeviceInfoRequested(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt) { struct os_mbuf *om = ctxt->om; + struct os_mbuf *om_tx; + switch (ctxt->op) { case BLE_GATT_ACCESS_OP_WRITE_CHR: while(om) { //console_write((char *)om->om_data, om->om_len); - om = ble_hs_mbuf_from_flat((char *)om->om_data, om->om_len); - if (om) { - ble_gattc_notify_custom(conn_handle, attr_read_handle, om); + // reply the received data + // test it with Bluefruit, NRF Connect on computer or phone, or in web browser + // https://wiki.makerdiary.com/web-device-cli/ + + om_tx = ble_hs_mbuf_from_flat((char *)om->om_data, om->om_len); + if (om_tx) { + ble_gattc_notify_custom(conn_handle, attr_read_handle, om_tx); } om = SLIST_NEXT(om, om_next); diff --git a/src/components/ble/BleNus.h b/src/components/ble/BleNus.h index ea396fa795..839adea8de 100644 --- a/src/components/ble/BleNus.h +++ b/src/components/ble/BleNus.h @@ -31,13 +31,16 @@ namespace Pinetime { public: BleNus(); void Init(); + void SetConnectionHandle(uint16_t conn_handle); + void Print(char *str); + int OnDeviceInfoRequested(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt); private: static uint16_t attr_read_handle; - + uint16_t conn_handle; static constexpr ble_uuid128_t nusServiceUuid {.u {.type = BLE_UUID_TYPE_128}, .value = NUS_SERVICE_UUID_BASE}; diff --git a/src/components/ble/NimbleController.cpp b/src/components/ble/NimbleController.cpp index d134915c5d..fa291f105e 100644 --- a/src/components/ble/NimbleController.cpp +++ b/src/components/ble/NimbleController.cpp @@ -14,6 +14,7 @@ #include "components/ble/NotificationManager.h" #include "components/datetime/DateTimeController.h" #include "systemtask/SystemTask.h" +#include "components/ble/BleNus.h" using namespace Pinetime::Controllers; @@ -144,6 +145,8 @@ int NimbleController::OnGAPEvent(ble_gap_event* event) { /* A new connection was established or a connection attempt failed. */ NRF_LOG_INFO("connection %s; status=%d ", event->connect.status == 0 ? "established" : "failed", event->connect.status); + bleNus.SetConnectionHandle(event->connect.conn_handle); + if (event->connect.status != 0) { /* Connection failed; resume advertising. */ StartAdvertising(); From 1a5de5e5b08d2bd85730dc0a915c38de46fd2eb0 Mon Sep 17 00:00:00 2001 From: hubmartin Date: Sat, 7 Aug 2021 21:32:05 +0200 Subject: [PATCH 03/25] Trying to add Console class --- src/CMakeLists.txt | 4 ++++ src/components/ble/BleNus.h | 11 +++-------- src/components/console/Console.cpp | 13 +++++++++++++ src/components/console/Console.h | 23 +++++++++++++++++++++++ src/systemtask/SystemTask.cpp | 8 +++++++- src/systemtask/SystemTask.h | 2 ++ 6 files changed, 52 insertions(+), 9 deletions(-) create mode 100644 src/components/console/Console.cpp create mode 100644 src/components/console/Console.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 9c4b211c53..de9e5e0b16 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -496,6 +496,9 @@ list(APPEND SOURCE_FILES components/heartrate/Biquad.cpp components/heartrate/Ptagc.cpp components/heartrate/HeartRateController.cpp + + components/console/Console.cpp + ) list(APPEND RECOVERY_SOURCE_FILES @@ -554,6 +557,7 @@ list(APPEND RECOVERY_SOURCE_FILES components/heartrate/Ptagc.cpp components/motor/MotorController.cpp components/fs/FS.cpp + components/console/Console.cpp ) list(APPEND RECOVERYLOADER_SOURCE_FILES diff --git a/src/components/ble/BleNus.h b/src/components/ble/BleNus.h index 839adea8de..a89e3c194d 100644 --- a/src/components/ble/BleNus.h +++ b/src/components/ble/BleNus.h @@ -6,22 +6,17 @@ #undef min #include "Version.h" -/* c7e60000-78fc-48fe-8e23-433b3a1942d0 -#define NAVIGATION_SERVICE_UUID_BASE \ - { 0xd0, 0x42, 0x19, 0x3a, 0x3b, 0x43, 0x23, 0x8e, 0xfe, 0x48, 0xfc, 0x78, 0x00, 0x00, 0x00, 0x00 } -*/ - // Service UUID // 6E400001-B5A3-F393-E0A9-E50E24DCCA9E -#define NUS_SERVICE_UUID_BASE \ +#define NUS_SERVICE_UUID_BASE \ { 0x9e, 0xca, 0xdc, 0x24, 0x0e, 0xe5, 0xa9, 0xe0, 0x93, 0xf3, 0xa3, 0xb5, 0x01, 0x00, 0x40, 0x6e } //6E400002-B5A3-F393-E0A9-E50E24DCCA9E -#define NUS_CHARACTERTISTIC_RX_UUID \ +#define NUS_CHARACTERTISTIC_RX_UUID \ { 0x9e, 0xca, 0xdc, 0x24, 0x0e, 0xe5, 0xa9, 0xe0, 0x93, 0xf3, 0xa3, 0xb5, 0x02, 0x00, 0x40, 0x6e } //6E400003-B5A3-F393-E0A9-E50E24DCCA9E -#define NUS_CHARACTERTISTIC_TX_UUID \ +#define NUS_CHARACTERTISTIC_TX_UUID \ { 0x9e, 0xca, 0xdc, 0x24, 0x0e, 0xe5, 0xa9, 0xe0, 0x93, 0xf3, 0xa3, 0xb5, 0x03, 0x00, 0x40, 0x6e } diff --git a/src/components/console/Console.cpp b/src/components/console/Console.cpp new file mode 100644 index 0000000000..bc6368bac4 --- /dev/null +++ b/src/components/console/Console.cpp @@ -0,0 +1,13 @@ + +#include "Console.h" +#include "systemtask/SystemTask.h" +#include "components/ble/BleNus.h" + + +using namespace Pinetime; + + +Console::Console(Pinetime::System::SystemTask& systemTask): systemTask {systemTask} +{ + +} diff --git a/src/components/console/Console.h b/src/components/console/Console.h new file mode 100644 index 0000000000..d0d4d517ae --- /dev/null +++ b/src/components/console/Console.h @@ -0,0 +1,23 @@ +#pragma once + + +#include +#include + +#include "systemtask/SystemTask.h" + +namespace Pinetime { + + namespace System { + class SystemTask; + } + class Console { + public: + Console(Pinetime::System::SystemTask& systemTask); + + private: + Pinetime::System::SystemTask& systemTask; + + }; + +} diff --git a/src/systemtask/SystemTask.cpp b/src/systemtask/SystemTask.cpp index 8915ce7448..393c8df10c 100644 --- a/src/systemtask/SystemTask.cpp +++ b/src/systemtask/SystemTask.cpp @@ -89,7 +89,9 @@ SystemTask::SystemTask(Drivers::SpiMaster& spi, displayApp{displayApp}, heartRateApp(heartRateApp), fs{fs}, - nimbleController(*this, bleController, dateTimeController, notificationManager, batteryController, spiNorFlash, heartRateController) { + + nimbleController(*this, bleController, dateTimeController, notificationManager, batteryController, spiNorFlash, heartRateController), + console(*this) { } @@ -296,6 +298,7 @@ void SystemTask::Work() { break; case Messages::OnTouchEvent: ReloadIdleTimer(); + console.Process(); displayApp.PushMessage(Pinetime::Applications::Display::Messages::TouchEvent); break; case Messages::OnButtonEvent: @@ -351,6 +354,9 @@ void SystemTask::Work() { batteryNotificationTick = xTaskGetTickCount(); } + + + monitor.Process(); uint32_t systick_counter = nrf_rtc_counter_get(portNRF_RTC_REG); dateTimeController.UpdateTime(systick_counter); diff --git a/src/systemtask/SystemTask.h b/src/systemtask/SystemTask.h index ba4342987b..f6dce77b01 100644 --- a/src/systemtask/SystemTask.h +++ b/src/systemtask/SystemTask.h @@ -17,6 +17,7 @@ #include "components/motor/MotorController.h" #include "components/timer/TimerController.h" #include "components/fs/FS.h" +#include "components/console/Console.h" #ifdef PINETIME_IS_RECOVERY #include "displayapp/DisplayAppRecovery.h" @@ -114,6 +115,7 @@ namespace Pinetime { Pinetime::Applications::HeartRateTask& heartRateApp; Pinetime::Controllers::FS& fs; Pinetime::Controllers::NimbleController nimbleController; + Pinetime::Console console; static constexpr uint8_t pinSpiSck = 2; static constexpr uint8_t pinSpiMosi = 3; From ae4b46aea24ab0da79bd0de0d4e94d3e02ebc2d1 Mon Sep 17 00:00:00 2001 From: hubmartin Date: Sun, 8 Aug 2021 16:16:35 +0200 Subject: [PATCH 04/25] Move Console to Components --- src/components/console/Console.cpp | 3 +-- src/components/console/Console.h | 7 ++++++- src/systemtask/SystemTask.cpp | 2 -- src/systemtask/SystemTask.h | 2 +- 4 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/components/console/Console.cpp b/src/components/console/Console.cpp index bc6368bac4..ebc9810ff3 100644 --- a/src/components/console/Console.cpp +++ b/src/components/console/Console.cpp @@ -4,8 +4,7 @@ #include "components/ble/BleNus.h" -using namespace Pinetime; - +using namespace Pinetime::Components; Console::Console(Pinetime::System::SystemTask& systemTask): systemTask {systemTask} { diff --git a/src/components/console/Console.h b/src/components/console/Console.h index d0d4d517ae..b475e82ddf 100644 --- a/src/components/console/Console.h +++ b/src/components/console/Console.h @@ -11,6 +11,11 @@ namespace Pinetime { namespace System { class SystemTask; } + +namespace Components +{ + + class Console { public: Console(Pinetime::System::SystemTask& systemTask); @@ -19,5 +24,5 @@ namespace Pinetime { Pinetime::System::SystemTask& systemTask; }; - +} } diff --git a/src/systemtask/SystemTask.cpp b/src/systemtask/SystemTask.cpp index 393c8df10c..f346e058b3 100644 --- a/src/systemtask/SystemTask.cpp +++ b/src/systemtask/SystemTask.cpp @@ -89,7 +89,6 @@ SystemTask::SystemTask(Drivers::SpiMaster& spi, displayApp{displayApp}, heartRateApp(heartRateApp), fs{fs}, - nimbleController(*this, bleController, dateTimeController, notificationManager, batteryController, spiNorFlash, heartRateController), console(*this) { @@ -298,7 +297,6 @@ void SystemTask::Work() { break; case Messages::OnTouchEvent: ReloadIdleTimer(); - console.Process(); displayApp.PushMessage(Pinetime::Applications::Display::Messages::TouchEvent); break; case Messages::OnButtonEvent: diff --git a/src/systemtask/SystemTask.h b/src/systemtask/SystemTask.h index f6dce77b01..4155efc899 100644 --- a/src/systemtask/SystemTask.h +++ b/src/systemtask/SystemTask.h @@ -115,7 +115,7 @@ namespace Pinetime { Pinetime::Applications::HeartRateTask& heartRateApp; Pinetime::Controllers::FS& fs; Pinetime::Controllers::NimbleController nimbleController; - Pinetime::Console console; + Pinetime::Components::Console console; static constexpr uint8_t pinSpiSck = 2; static constexpr uint8_t pinSpiMosi = 3; From bc0f7c18f73842c7d493d67b4d4aa2675fadc116 Mon Sep 17 00:00:00 2001 From: hubmartin Date: Sun, 8 Aug 2021 17:33:29 +0200 Subject: [PATCH 05/25] Working console.Print concept --- src/components/ble/NimbleController.cpp | 6 ++++++ src/components/ble/NimbleController.h | 1 + src/components/console/Console.cpp | 9 ++++++++- src/components/console/Console.h | 27 ++++++++++++++++--------- src/systemtask/SystemTask.cpp | 5 ++++- 5 files changed, 36 insertions(+), 12 deletions(-) diff --git a/src/components/ble/NimbleController.cpp b/src/components/ble/NimbleController.cpp index fa291f105e..cf275af51c 100644 --- a/src/components/ble/NimbleController.cpp +++ b/src/components/ble/NimbleController.cpp @@ -245,3 +245,9 @@ void NimbleController::NotifyBatteryLevel(uint8_t level) { batteryInformationService.NotifyBatteryLevel(connectionHandle, level); } } + + +void NimbleController::Print(char *str) +{ + bleNus.Print(str); +} diff --git a/src/components/ble/NimbleController.h b/src/components/ble/NimbleController.h index ca1ee6a906..055eac48f6 100644 --- a/src/components/ble/NimbleController.h +++ b/src/components/ble/NimbleController.h @@ -72,6 +72,7 @@ namespace Pinetime { uint16_t connHandle(); void NotifyBatteryLevel(uint8_t level); + void Print(char *str); private: static constexpr const char* deviceName = "InfiniTime"; diff --git a/src/components/console/Console.cpp b/src/components/console/Console.cpp index ebc9810ff3..ca24f9779e 100644 --- a/src/components/console/Console.cpp +++ b/src/components/console/Console.cpp @@ -1,12 +1,19 @@ #include "Console.h" #include "systemtask/SystemTask.h" +#include "components/ble/NimbleController.h" #include "components/ble/BleNus.h" using namespace Pinetime::Components; -Console::Console(Pinetime::System::SystemTask& systemTask): systemTask {systemTask} +Console::Console(Pinetime::System::SystemTask& systemTask, Pinetime::Controllers::NimbleController& nimbleController): systemTask {systemTask}, nimbleController{nimbleController} { } + + +void Console::Print(char *str) +{ + nimbleController.Print(str); +} \ No newline at end of file diff --git a/src/components/console/Console.h b/src/components/console/Console.h index b475e82ddf..caa4b34024 100644 --- a/src/components/console/Console.h +++ b/src/components/console/Console.h @@ -4,7 +4,7 @@ #include #include -#include "systemtask/SystemTask.h" +//#include "systemtask/SystemTask.h" namespace Pinetime { @@ -12,17 +12,24 @@ namespace Pinetime { class SystemTask; } -namespace Components -{ + namespace Controllers + { + class NimbleController; + } + namespace Components + { - class Console { - public: - Console(Pinetime::System::SystemTask& systemTask); + class Console { + public: + Console(Pinetime::System::SystemTask& systemTask, Pinetime::Controllers::NimbleController& nimbleController); - private: - Pinetime::System::SystemTask& systemTask; + void Print(char *str); - }; -} + private: + Pinetime::System::SystemTask& systemTask; + Pinetime::Controllers::NimbleController& nimbleController; + + }; + } } diff --git a/src/systemtask/SystemTask.cpp b/src/systemtask/SystemTask.cpp index f346e058b3..861de828c5 100644 --- a/src/systemtask/SystemTask.cpp +++ b/src/systemtask/SystemTask.cpp @@ -90,7 +90,7 @@ SystemTask::SystemTask(Drivers::SpiMaster& spi, heartRateApp(heartRateApp), fs{fs}, nimbleController(*this, bleController, dateTimeController, notificationManager, batteryController, spiNorFlash, heartRateController), - console(*this) { + console(*this, nimbleController) { } @@ -296,10 +296,12 @@ void SystemTask::Work() { xTimerStart(dimTimer, 0); break; case Messages::OnTouchEvent: + console.Print("Touch event\r\n"); ReloadIdleTimer(); displayApp.PushMessage(Pinetime::Applications::Display::Messages::TouchEvent); break; case Messages::OnButtonEvent: + console.Print("Button event\r\n"); ReloadIdleTimer(); displayApp.PushMessage(Pinetime::Applications::Display::Messages::ButtonPushed); break; @@ -456,6 +458,7 @@ void SystemTask::OnIdle() { if (doNotGoToSleep) return; NRF_LOG_INFO("Idle timeout -> Going to sleep") + console.Print("Idle timeout -> Going to sleep\r\n"); PushMessage(Messages::GoToSleep); } From 2a0754b902980465ec5db3b70d6a767b14567d36 Mon Sep 17 00:00:00 2001 From: hubmartin Date: Mon, 9 Aug 2021 21:02:23 +0200 Subject: [PATCH 06/25] WIP --- src/components/ble/BleNus.cpp | 6 +++ src/components/ble/BleNus.h | 6 ++- src/components/ble/NimbleController.cpp | 2 + src/components/console/Console.cpp | 5 +++ src/components/console/Console.h | 4 ++ src/systemtask/SystemTask.cpp | 49 +++++++++++++++++++++++++ 6 files changed, 71 insertions(+), 1 deletion(-) diff --git a/src/components/ble/BleNus.cpp b/src/components/ble/BleNus.cpp index a81285fbe8..84851dd018 100644 --- a/src/components/ble/BleNus.cpp +++ b/src/components/ble/BleNus.cpp @@ -1,4 +1,5 @@ #include "BleNus.h" +#include "components/console/Console.h" using namespace Pinetime::Controllers; @@ -37,6 +38,11 @@ void BleNus::Print(char *str) } } +void BleNus::ConsoleRegister(Pinetime::Components::Console* console) +{ + this->console = console; +} + int BleNus::OnDeviceInfoRequested(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt) { diff --git a/src/components/ble/BleNus.h b/src/components/ble/BleNus.h index a89e3c194d..bacc8cfffe 100644 --- a/src/components/ble/BleNus.h +++ b/src/components/ble/BleNus.h @@ -21,6 +21,9 @@ namespace Pinetime { + namespace Components { + class Console; + } namespace Controllers { class BleNus { public: @@ -28,7 +31,7 @@ namespace Pinetime { void Init(); void SetConnectionHandle(uint16_t conn_handle); void Print(char *str); - + void ConsoleRegister(Pinetime::Components::Console* console); int OnDeviceInfoRequested(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt); @@ -36,6 +39,7 @@ namespace Pinetime { static uint16_t attr_read_handle; uint16_t conn_handle; + Pinetime::Components::Console* console = nullptr; static constexpr ble_uuid128_t nusServiceUuid {.u {.type = BLE_UUID_TYPE_128}, .value = NUS_SERVICE_UUID_BASE}; diff --git a/src/components/ble/NimbleController.cpp b/src/components/ble/NimbleController.cpp index cf275af51c..e729fa5dd5 100644 --- a/src/components/ble/NimbleController.cpp +++ b/src/components/ble/NimbleController.cpp @@ -79,6 +79,8 @@ void NimbleController::Init() { bleController.AddressType((addrType == 0) ? Ble::AddressTypes::Public : Ble::AddressTypes::Random); bleController.Address(std::move(address)); + bleNus.ConsoleRegister(&systemTask.console); + res = ble_gatts_start(); ASSERT(res == 0); } diff --git a/src/components/console/Console.cpp b/src/components/console/Console.cpp index ca24f9779e..5824ce0337 100644 --- a/src/components/console/Console.cpp +++ b/src/components/console/Console.cpp @@ -14,6 +14,11 @@ Console::Console(Pinetime::System::SystemTask& systemTask, Pinetime::Controllers void Console::Print(char *str) +{ + nimbleController.Print(str); +} + +void Console::Received(char *str) { nimbleController.Print(str); } \ No newline at end of file diff --git a/src/components/console/Console.h b/src/components/console/Console.h index caa4b34024..7533ce50fc 100644 --- a/src/components/console/Console.h +++ b/src/components/console/Console.h @@ -25,11 +25,15 @@ namespace Pinetime { Console(Pinetime::System::SystemTask& systemTask, Pinetime::Controllers::NimbleController& nimbleController); void Print(char *str); + void Received(char *str); private: Pinetime::System::SystemTask& systemTask; Pinetime::Controllers::NimbleController& nimbleController; + static constexpr int bufferSize = 256; + char rxBuffer[bufferSize]; + }; } } diff --git a/src/systemtask/SystemTask.cpp b/src/systemtask/SystemTask.cpp index 861de828c5..6fce748cc6 100644 --- a/src/systemtask/SystemTask.cpp +++ b/src/systemtask/SystemTask.cpp @@ -25,6 +25,13 @@ #include +#include +#include +#include +#include +#include + + using namespace Pinetime::System; namespace { @@ -105,6 +112,42 @@ void SystemTask::Process(void* instance) { NRF_LOG_INFO("systemtask task started!"); app->Work(); } +/* +#define NRF_LOG_BACKEND_BLENUS_TEMP_BUFFER_SIZE 128 +static uint8_t m_string_buff[NRF_LOG_BACKEND_BLENUS_TEMP_BUFFER_SIZE]; + +static void blenus_tx(void const * p_context, char const * p_buffer, size_t len) +{ + //console.Print(p_buffer); +} + +static void nrf_log_backend_blenus_put(nrf_log_backend_t const * p_backend, nrf_log_entry_t * p_msg) +{ + nrf_log_backend_serial_put(p_backend, p_msg, m_string_buff, NRF_LOG_BACKEND_BLENUS_TEMP_BUFFER_SIZE, blenus_tx); +} + +static void nrf_log_backend_blenus_flush(nrf_log_backend_t const * p_backend) +{ + +} + +static void nrf_log_backend_blenus_panic_set(nrf_log_backend_t const * p_backend) +{ + //nrf_drv_uart_uninit(&m_uart); + + // uart_init(false); +} + + + static const nrf_log_backend_api_t nrf_log_backend_blenus_api = { + .put = nrf_log_backend_blenus_put, + .panic_set = nrf_log_backend_blenus_panic_set, + .flush = nrf_log_backend_blenus_flush, + }; + + + NRF_LOG_BACKEND_DEF(blenus_backend, nrf_log_backend_blenus_api, NULL); +*/ void SystemTask::Work() { watchdog.Setup(7); @@ -150,6 +193,12 @@ void SystemTask::Work() { heartRateSensor.Disable(); heartRateApp.Start(); + /* + int32_t backend_id = nrf_log_backend_add(&blenus_backend, NRF_LOG_SEVERITY_DEBUG); + ASSERT(backend_id >= 0); + nrf_log_backend_enable(&blenus_backend); +*/ + nrf_gpio_cfg_sense_input(pinButton, (nrf_gpio_pin_pull_t) GPIO_PIN_CNF_PULL_Pulldown, (nrf_gpio_pin_sense_t) GPIO_PIN_CNF_SENSE_High); nrf_gpio_cfg_output(15); nrf_gpio_pin_set(15); From 8d128061cacedce10dfe5768c05de9cb554c3f3b Mon Sep 17 00:00:00 2001 From: hubmartin Date: Tue, 10 Aug 2021 20:58:54 +0200 Subject: [PATCH 07/25] RX callback registration --- src/components/ble/BleNus.cpp | 16 +++++++------ src/components/ble/BleNus.h | 8 +++++-- src/components/ble/NimbleController.cpp | 11 +++------ src/components/ble/NimbleController.h | 7 ++++-- src/components/console/Console.cpp | 30 ++++++++++++++++++++++--- src/components/console/Console.h | 4 +++- src/systemtask/SystemTask.cpp | 2 ++ 7 files changed, 55 insertions(+), 23 deletions(-) diff --git a/src/components/ble/BleNus.cpp b/src/components/ble/BleNus.cpp index 84851dd018..e0e38f8351 100644 --- a/src/components/ble/BleNus.cpp +++ b/src/components/ble/BleNus.cpp @@ -38,16 +38,16 @@ void BleNus::Print(char *str) } } -void BleNus::ConsoleRegister(Pinetime::Components::Console* console) +void BleNus::ConsoleRegister(std::function f) { - this->console = console; + this->rxDataFunction = f; } int BleNus::OnDeviceInfoRequested(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt) { struct os_mbuf *om = ctxt->om; - struct os_mbuf *om_tx; + //struct os_mbuf *om_tx; switch (ctxt->op) { case BLE_GATT_ACCESS_OP_WRITE_CHR: @@ -58,10 +58,12 @@ int BleNus::OnDeviceInfoRequested(uint16_t conn_handle, uint16_t attr_handle, st // test it with Bluefruit, NRF Connect on computer or phone, or in web browser // https://wiki.makerdiary.com/web-device-cli/ - om_tx = ble_hs_mbuf_from_flat((char *)om->om_data, om->om_len); - if (om_tx) { - ble_gattc_notify_custom(conn_handle, attr_read_handle, om_tx); - } + rxDataFunction((char *)om->om_data, (int)om->om_len); + + // om_tx = ble_hs_mbuf_from_flat((char *)om->om_data, om->om_len); + // if (om_tx) { + // ble_gattc_notify_custom(conn_handle, attr_read_handle, om_tx); + // } om = SLIST_NEXT(om, om_next); } diff --git a/src/components/ble/BleNus.h b/src/components/ble/BleNus.h index bacc8cfffe..2760f2f0cf 100644 --- a/src/components/ble/BleNus.h +++ b/src/components/ble/BleNus.h @@ -5,6 +5,8 @@ #undef max #undef min #include "Version.h" +#include + // Service UUID // 6E400001-B5A3-F393-E0A9-E50E24DCCA9E @@ -31,7 +33,8 @@ namespace Pinetime { void Init(); void SetConnectionHandle(uint16_t conn_handle); void Print(char *str); - void ConsoleRegister(Pinetime::Components::Console* console); + + void ConsoleRegister(std::function f); int OnDeviceInfoRequested(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt); @@ -39,7 +42,8 @@ namespace Pinetime { static uint16_t attr_read_handle; uint16_t conn_handle; - Pinetime::Components::Console* console = nullptr; + + std::function rxDataFunction; static constexpr ble_uuid128_t nusServiceUuid {.u {.type = BLE_UUID_TYPE_128}, .value = NUS_SERVICE_UUID_BASE}; diff --git a/src/components/ble/NimbleController.cpp b/src/components/ble/NimbleController.cpp index e729fa5dd5..1c2521ce58 100644 --- a/src/components/ble/NimbleController.cpp +++ b/src/components/ble/NimbleController.cpp @@ -56,7 +56,7 @@ void NimbleController::Init() { ble_svc_gatt_init(); deviceInformationService.Init(); - bleNus.Init(); + bleNusService.Init(); currentTimeClient.Init(); currentTimeService.Init(); musicService.Init(); @@ -79,7 +79,7 @@ void NimbleController::Init() { bleController.AddressType((addrType == 0) ? Ble::AddressTypes::Public : Ble::AddressTypes::Random); bleController.Address(std::move(address)); - bleNus.ConsoleRegister(&systemTask.console); + //bleNus.ConsoleRegister(&systemTask.console); res = ble_gatts_start(); ASSERT(res == 0); @@ -147,7 +147,7 @@ int NimbleController::OnGAPEvent(ble_gap_event* event) { /* A new connection was established or a connection attempt failed. */ NRF_LOG_INFO("connection %s; status=%d ", event->connect.status == 0 ? "established" : "failed", event->connect.status); - bleNus.SetConnectionHandle(event->connect.conn_handle); + bleNusService.SetConnectionHandle(event->connect.conn_handle); if (event->connect.status != 0) { /* Connection failed; resume advertising. */ @@ -248,8 +248,3 @@ void NimbleController::NotifyBatteryLevel(uint8_t level) { } } - -void NimbleController::Print(char *str) -{ - bleNus.Print(str); -} diff --git a/src/components/ble/NimbleController.h b/src/components/ble/NimbleController.h index 055eac48f6..8bcf07511c 100644 --- a/src/components/ble/NimbleController.h +++ b/src/components/ble/NimbleController.h @@ -70,9 +70,12 @@ namespace Pinetime { return anService; }; + Pinetime::Controllers::BleNus& bleNus() { + return bleNusService; + }; + uint16_t connHandle(); void NotifyBatteryLevel(uint8_t level); - void Print(char *str); private: static constexpr const char* deviceName = "InfiniTime"; @@ -84,7 +87,7 @@ namespace Pinetime { Pinetime::Controllers::DfuService dfuService; DeviceInformationService deviceInformationService; - BleNus bleNus; + BleNus bleNusService; CurrentTimeClient currentTimeClient; AlertNotificationService anService; AlertNotificationClient alertNotificationClient; diff --git a/src/components/console/Console.cpp b/src/components/console/Console.cpp index 5824ce0337..28c163d364 100644 --- a/src/components/console/Console.cpp +++ b/src/components/console/Console.cpp @@ -12,13 +12,37 @@ Console::Console(Pinetime::System::SystemTask& systemTask, Pinetime::Controllers } +void Console::Init() +{ + auto rxCallback = [this](char *str, int length) { + this->Received(str, length); + }; + + nimbleController.bleNus().ConsoleRegister(rxCallback); +} void Console::Print(char *str) { - nimbleController.Print(str); + //nimbleController.Print(str); + nimbleController.bleNus().Print(str); } -void Console::Received(char *str) +void Console::Received(char* str, int length) { - nimbleController.Print(str); + + char b[32]; + + for(int i = 0; i < length, i++) + { + rxBuffer[rxPos++] = str[i]; + + if(str[i] == 13 || str[i] == 10) + { + break; + } + } + + sprintf(b, "rx: %s, len: %d", str, length); + + nimbleController.bleNus().Print(b); } \ No newline at end of file diff --git a/src/components/console/Console.h b/src/components/console/Console.h index 7533ce50fc..2141f3dec6 100644 --- a/src/components/console/Console.h +++ b/src/components/console/Console.h @@ -24,8 +24,9 @@ namespace Pinetime { public: Console(Pinetime::System::SystemTask& systemTask, Pinetime::Controllers::NimbleController& nimbleController); + void Init(); void Print(char *str); - void Received(char *str); + void Received(char* str, int length); private: Pinetime::System::SystemTask& systemTask; @@ -33,6 +34,7 @@ namespace Pinetime { static constexpr int bufferSize = 256; char rxBuffer[bufferSize]; + uint16_t rxPos; }; } diff --git a/src/systemtask/SystemTask.cpp b/src/systemtask/SystemTask.cpp index 6fce748cc6..ab040dbeb4 100644 --- a/src/systemtask/SystemTask.cpp +++ b/src/systemtask/SystemTask.cpp @@ -193,6 +193,8 @@ void SystemTask::Work() { heartRateSensor.Disable(); heartRateApp.Start(); + console.Init(); + /* int32_t backend_id = nrf_log_backend_add(&blenus_backend, NRF_LOG_SEVERITY_DEBUG); ASSERT(backend_id >= 0); From 7ebe928f94db50210abab00b7a0ca57cb9ab9d5c Mon Sep 17 00:00:00 2001 From: hubmartin Date: Mon, 9 Aug 2021 21:14:47 +0200 Subject: [PATCH 08/25] Add VSCode ST-link debug config --- .vscode/launch.json | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 .vscode/launch.json diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000000..b7a8dd076e --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,25 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "cwd": "${workspaceRoot}", + "executable": "./build/src/pinetime-app-1.3.0.out", + "name": "Debug OpenOCD ST-LINK", + "request": "launch", + "type": "cortex-debug", + "showDevDebugOutput": false, + "servertype": "openocd", + "runToMain": true, + // Only use armToolchainPath if your arm-none-eabi-gdb is not in your path (some GCC packages does not contain arm-none-eabi-gdb) + "armToolchainPath": "${workspaceRoot}/../gcc-arm-none-eabi-9-2020-q2-update/bin", + "svdFile": "${workspaceRoot}/nrf52.svd", + "configFiles": [ + "interface/stlink.cfg", + "target/nrf52.cfg" + ], + } + ] +} From 8c95f55e412686381d968d997445f573c7256de5 Mon Sep 17 00:00:00 2001 From: hubmartin Date: Tue, 10 Aug 2021 22:28:38 +0200 Subject: [PATCH 09/25] First commands working --- src/components/console/Console.cpp | 103 +++++++++++++++++++++++++++-- src/components/console/Console.h | 26 +++++++- src/systemtask/SystemTask.cpp | 2 +- 3 files changed, 123 insertions(+), 8 deletions(-) diff --git a/src/components/console/Console.cpp b/src/components/console/Console.cpp index 28c163d364..bd9e1c16d5 100644 --- a/src/components/console/Console.cpp +++ b/src/components/console/Console.cpp @@ -7,7 +7,22 @@ using namespace Pinetime::Components; -Console::Console(Pinetime::System::SystemTask& systemTask, Pinetime::Controllers::NimbleController& nimbleController): systemTask {systemTask}, nimbleController{nimbleController} +Console::Console(Pinetime::System::SystemTask& systemTask, + Pinetime::Controllers::NimbleController& nimbleController, + Pinetime::Controllers::FS& fs, + Pinetime::Components::LittleVgl& lvgl, + Pinetime::Controllers::MotorController& motorController, + Pinetime::Drivers::Cst816S& touchPanel, + Pinetime::Drivers::SpiNorFlash& spiNorFlash, + Pinetime::Drivers::TwiMaster& twiMaster): + systemTask {systemTask}, + nimbleController{nimbleController}, + fs{fs}, + lvgl{lvgl}, + motorController{motorController}, + touchPanel{touchPanel}, + spiNorFlash{spiNorFlash}, + twiMaster{twiMaster} { } @@ -29,20 +44,96 @@ void Console::Print(char *str) void Console::Received(char* str, int length) { + bool hasCommand = false; + //char b[128]; - char b[32]; - - for(int i = 0; i < length, i++) + for(int i = 0; i < length; i++) { rxBuffer[rxPos++] = str[i]; + rxBuffer[rxPos] = '\0'; // terminate for debug out if(str[i] == 13 || str[i] == 10) { + rxPos = 0; + hasCommand = true; break; } } - sprintf(b, "rx: %s, len: %d", str, length); + //sprintf(b, "rx: %s, len: %d, buffer: %s\r\n", str, length, rxBuffer); + //nimbleController.bleNus().Print(b); + + + // Simple stupid comparison, later would be nice to add commands lookup table with argument parsing + if(hasCommand) + { + //sprintf(b, "cmd: %s\r\n", rxBuffer); + //nimbleController.bleNus().Print(b); + + // This AT > OK needs to be there, because https://terminal.hardwario.com/ waits for the answer + // When we use or create better webpage terminal, this can go out + if(strncmp(rxBuffer, "AT", 2) == 0) + { + nimbleController.bleNus().Print((char*)"OK\r\n"); + } + else if(strncmp(rxBuffer, "LVGL", 4) == 0) + { + // TODO: list of objects, changing position, size & color would be great + + char lvbuf[128]; + lv_mem_monitor_t mon; + lv_mem_monitor(&mon); + snprintf(lvbuf, sizeof(lvbuf), "used: %6d (%3d %%), frag: %3d %%, biggest free: %6d\n", (int)(mon.total_size - mon.free_size), + mon.used_pct, + mon.frag_pct, + (int)mon.free_biggest_size); + nimbleController.bleNus().Print(lvbuf); + } + else if(strncmp(rxBuffer, "VIBRATE", 7) == 0) + { + motorController.SetDuration(100); + } + else if(strncmp(rxBuffer, "FS", 2) == 0) + { + // TODO: add directory listings etc. + /* + lfs_file_t settingsFile; + + if(fs.FileOpen(&settingsFile, "/settings.dat", LFS_O_RDONLY) != LFS_ERR_OK) + { + return; + }*/ - nimbleController.bleNus().Print(b); + } + else if(strncmp(rxBuffer, "WKUP", 4) == 0) + { + systemTask.PushMessage(Pinetime::System::Messages::GoToRunning); + } + else if(strncmp(rxBuffer, "SLEEP", 5) == 0) + { + systemTask.PushMessage(Pinetime::System::Messages::GoToSleep); + } + else if(strncmp(rxBuffer, "SPINOR", 6) == 0) + { + // Not working yet + /* + uint8_t flashBuffer[64]; + char lineBuffer[64]; + spiNorFlash.Read(0x0, flashBuffer, sizeof(flashBuffer)); + + lineBuffer[0] = '\0'; + + uint8_t *ptr = flashBuffer; + + for(uint32_t i = 0; i < sizeof(flashBuffer) / 8; i++) + { + snprintf(lineBuffer, sizeof(lineBuffer), "%02X %02X %02X %02X %02X %02X %02X %02X\r\n", ptr[0], ptr[1], ptr[2], ptr[3], ptr[4], ptr[5], ptr[6], ptr[7]); + ptr += 8; + + nimbleController.bleNus().Print(lineBuffer); + vTaskDelay(50); // not sure if this has to be, needs to be inspected how BLE communicates and if buffers are ok + } + */ + } + } } \ No newline at end of file diff --git a/src/components/console/Console.h b/src/components/console/Console.h index 2141f3dec6..5ae31ce796 100644 --- a/src/components/console/Console.h +++ b/src/components/console/Console.h @@ -15,14 +15,32 @@ namespace Pinetime { namespace Controllers { class NimbleController; + class FS; + class MotorController; } + namespace Drivers + { + class Cst816S; + class SpiNorFlash; + class TwiMaster; + } + + namespace Components { + class LittleVgl; class Console { public: - Console(Pinetime::System::SystemTask& systemTask, Pinetime::Controllers::NimbleController& nimbleController); + Console(Pinetime::System::SystemTask& systemTask, + Pinetime::Controllers::NimbleController& nimbleController, + Pinetime::Controllers::FS& fs, + Pinetime::Components::LittleVgl& lvgl, + Pinetime::Controllers::MotorController& motorController, + Pinetime::Drivers::Cst816S& touchPanel, + Pinetime::Drivers::SpiNorFlash& spiNorFlash, + Pinetime::Drivers::TwiMaster& twiMaster); void Init(); void Print(char *str); @@ -31,6 +49,12 @@ namespace Pinetime { private: Pinetime::System::SystemTask& systemTask; Pinetime::Controllers::NimbleController& nimbleController; + Pinetime::Controllers::FS& fs; + Pinetime::Components::LittleVgl& lvgl; + Pinetime::Controllers::MotorController& motorController; + Pinetime::Drivers::Cst816S& touchPanel; + Pinetime::Drivers::SpiNorFlash& spiNorFlash; + Pinetime::Drivers::TwiMaster& twiMaster; static constexpr int bufferSize = 256; char rxBuffer[bufferSize]; diff --git a/src/systemtask/SystemTask.cpp b/src/systemtask/SystemTask.cpp index ab040dbeb4..c075dbb7b9 100644 --- a/src/systemtask/SystemTask.cpp +++ b/src/systemtask/SystemTask.cpp @@ -97,7 +97,7 @@ SystemTask::SystemTask(Drivers::SpiMaster& spi, heartRateApp(heartRateApp), fs{fs}, nimbleController(*this, bleController, dateTimeController, notificationManager, batteryController, spiNorFlash, heartRateController), - console(*this, nimbleController) { + console(*this, nimbleController, fs, lvgl, motorController, touchPanel, spiNorFlash, twiMaster) { } From 18c0f46e93f77cdde508b374906fed449d86bf92 Mon Sep 17 00:00:00 2001 From: hubmartin Date: Wed, 11 Aug 2021 15:02:05 +0200 Subject: [PATCH 10/25] Move console processing to SystemTask --- src/components/console/Console.cpp | 91 +++++++++++++----------------- src/components/console/Console.h | 2 + src/systemtask/SystemTask.cpp | 8 +-- 3 files changed, 45 insertions(+), 56 deletions(-) diff --git a/src/components/console/Console.cpp b/src/components/console/Console.cpp index bd9e1c16d5..3fd31970bd 100644 --- a/src/components/console/Console.cpp +++ b/src/components/console/Console.cpp @@ -4,7 +4,6 @@ #include "components/ble/NimbleController.h" #include "components/ble/BleNus.h" - using namespace Pinetime::Components; Console::Console(Pinetime::System::SystemTask& systemTask, @@ -24,7 +23,6 @@ Console::Console(Pinetime::System::SystemTask& systemTask, spiNorFlash{spiNorFlash}, twiMaster{twiMaster} { - } void Console::Init() @@ -38,37 +36,15 @@ void Console::Init() void Console::Print(char *str) { - //nimbleController.Print(str); nimbleController.bleNus().Print(str); } -void Console::Received(char* str, int length) +void Console::Process() { - bool hasCommand = false; - //char b[128]; - - for(int i = 0; i < length; i++) - { - rxBuffer[rxPos++] = str[i]; - rxBuffer[rxPos] = '\0'; // terminate for debug out - - if(str[i] == 13 || str[i] == 10) - { - rxPos = 0; - hasCommand = true; - break; - } - } - - //sprintf(b, "rx: %s, len: %d, buffer: %s\r\n", str, length, rxBuffer); - //nimbleController.bleNus().Print(b); - - // Simple stupid comparison, later would be nice to add commands lookup table with argument parsing - if(hasCommand) + if(hasCommandFlag) { - //sprintf(b, "cmd: %s\r\n", rxBuffer); - //nimbleController.bleNus().Print(b); + hasCommandFlag = false; // This AT > OK needs to be there, because https://terminal.hardwario.com/ waits for the answer // When we use or create better webpage terminal, this can go out @@ -79,7 +55,6 @@ void Console::Received(char* str, int length) else if(strncmp(rxBuffer, "LVGL", 4) == 0) { // TODO: list of objects, changing position, size & color would be great - char lvbuf[128]; lv_mem_monitor_t mon; lv_mem_monitor(&mon); @@ -88,6 +63,23 @@ void Console::Received(char* str, int length) mon.frag_pct, (int)mon.free_biggest_size); nimbleController.bleNus().Print(lvbuf); + + // List active screen objects + lv_obj_t* actStrc = lv_scr_act(); + uint16_t childCount = lv_obj_count_children(actStrc); + snprintf(lvbuf, sizeof(lvbuf), "children: %d\n", childCount); + nimbleController.bleNus().Print(lvbuf); + + lv_obj_t * child; + uint16_t i = 0; + child = lv_obj_get_child(actStrc, NULL); + while(child) { + snprintf(lvbuf, sizeof(lvbuf), "#%d, x: %d, y: %d, w: %d, h: %d\n", i++, lv_obj_get_x(child), lv_obj_get_y(child), lv_obj_get_width(child), lv_obj_get_height(child)); + nimbleController.bleNus().Print(lvbuf); + vTaskDelay(50); // Add small delay for each item, so the print buffer has time to be send over BLE + + child = lv_obj_get_child(actStrc, child); + } } else if(strncmp(rxBuffer, "VIBRATE", 7) == 0) { @@ -96,14 +88,6 @@ void Console::Received(char* str, int length) else if(strncmp(rxBuffer, "FS", 2) == 0) { // TODO: add directory listings etc. - /* - lfs_file_t settingsFile; - - if(fs.FileOpen(&settingsFile, "/settings.dat", LFS_O_RDONLY) != LFS_ERR_OK) - { - return; - }*/ - } else if(strncmp(rxBuffer, "WKUP", 4) == 0) { @@ -115,25 +99,28 @@ void Console::Received(char* str, int length) } else if(strncmp(rxBuffer, "SPINOR", 6) == 0) { - // Not working yet - /* - uint8_t flashBuffer[64]; - char lineBuffer[64]; - spiNorFlash.Read(0x0, flashBuffer, sizeof(flashBuffer)); - - lineBuffer[0] = '\0'; + // TODO: print RAW data from FLASH + } + } +} - uint8_t *ptr = flashBuffer; +void Console::Received(char* str, int length) +{ + //char b[128]; - for(uint32_t i = 0; i < sizeof(flashBuffer) / 8; i++) - { - snprintf(lineBuffer, sizeof(lineBuffer), "%02X %02X %02X %02X %02X %02X %02X %02X\r\n", ptr[0], ptr[1], ptr[2], ptr[3], ptr[4], ptr[5], ptr[6], ptr[7]); - ptr += 8; + for(int i = 0; i < length; i++) + { + rxBuffer[rxPos++] = str[i]; + rxBuffer[rxPos] = '\0'; // terminate for debug print - nimbleController.bleNus().Print(lineBuffer); - vTaskDelay(50); // not sure if this has to be, needs to be inspected how BLE communicates and if buffers are ok - } - */ + if(str[i] == 13 || str[i] == 10) + { + rxPos = 0; + hasCommandFlag = true; + break; } } + + //sprintf(b, "rx: %s, len: %d, buffer: %s\r\n", str, length, rxBuffer); + //nimbleController.bleNus().Print(b); } \ No newline at end of file diff --git a/src/components/console/Console.h b/src/components/console/Console.h index 5ae31ce796..941744817d 100644 --- a/src/components/console/Console.h +++ b/src/components/console/Console.h @@ -43,6 +43,7 @@ namespace Pinetime { Pinetime::Drivers::TwiMaster& twiMaster); void Init(); + void Process(); void Print(char *str); void Received(char* str, int length); @@ -59,6 +60,7 @@ namespace Pinetime { static constexpr int bufferSize = 256; char rxBuffer[bufferSize]; uint16_t rxPos; + bool hasCommandFlag; }; } diff --git a/src/systemtask/SystemTask.cpp b/src/systemtask/SystemTask.cpp index c075dbb7b9..bfb001236a 100644 --- a/src/systemtask/SystemTask.cpp +++ b/src/systemtask/SystemTask.cpp @@ -347,12 +347,12 @@ void SystemTask::Work() { xTimerStart(dimTimer, 0); break; case Messages::OnTouchEvent: - console.Print("Touch event\r\n"); + console.Print((char*)"Touch event\r\n"); ReloadIdleTimer(); displayApp.PushMessage(Pinetime::Applications::Display::Messages::TouchEvent); break; case Messages::OnButtonEvent: - console.Print("Button event\r\n"); + console.Print((char*)"Button event\r\n"); ReloadIdleTimer(); displayApp.PushMessage(Pinetime::Applications::Display::Messages::ButtonPushed); break; @@ -406,7 +406,7 @@ void SystemTask::Work() { } - + console.Process(); monitor.Process(); uint32_t systick_counter = nrf_rtc_counter_get(portNRF_RTC_REG); @@ -509,7 +509,7 @@ void SystemTask::OnIdle() { if (doNotGoToSleep) return; NRF_LOG_INFO("Idle timeout -> Going to sleep") - console.Print("Idle timeout -> Going to sleep\r\n"); + console.Print((char*)"Idle timeout -> Going to sleep\r\n"); PushMessage(Messages::GoToSleep); } From 774e2f8f3cb6d537b34ea27d5d8bdfef2534b83c Mon Sep 17 00:00:00 2001 From: hubmartin Date: Wed, 11 Aug 2021 15:14:58 +0200 Subject: [PATCH 11/25] Add MotionController debug output --- src/components/console/Console.cpp | 23 +++++++++++++++++++++-- src/components/console/Console.h | 5 ++++- src/systemtask/SystemTask.cpp | 2 +- 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/src/components/console/Console.cpp b/src/components/console/Console.cpp index 3fd31970bd..bd8dee5c3e 100644 --- a/src/components/console/Console.cpp +++ b/src/components/console/Console.cpp @@ -13,7 +13,8 @@ Console::Console(Pinetime::System::SystemTask& systemTask, Pinetime::Controllers::MotorController& motorController, Pinetime::Drivers::Cst816S& touchPanel, Pinetime::Drivers::SpiNorFlash& spiNorFlash, - Pinetime::Drivers::TwiMaster& twiMaster): + Pinetime::Drivers::TwiMaster& twiMaster, + Pinetime::Controllers::MotionController& motionController): systemTask {systemTask}, nimbleController{nimbleController}, fs{fs}, @@ -21,7 +22,8 @@ Console::Console(Pinetime::System::SystemTask& systemTask, motorController{motorController}, touchPanel{touchPanel}, spiNorFlash{spiNorFlash}, - twiMaster{twiMaster} + twiMaster{twiMaster}, + motionController{motionController} { } @@ -41,6 +43,8 @@ void Console::Print(char *str) void Console::Process() { + static uint32_t accCount = 0; + // Simple stupid comparison, later would be nice to add commands lookup table with argument parsing if(hasCommandFlag) { @@ -101,6 +105,21 @@ void Console::Process() { // TODO: print RAW data from FLASH } + else if(strncmp(rxBuffer, "ACC", 3) == 0) + { + // Print 50 accelerometer measurements + accCount = 50; + } + } + + // Debug print accelerometer values + if(accCount) + { + accCount--; + char accBuf[32]; + + snprintf(accBuf, sizeof(accBuf), "%d, %d, %d\n", motionController.X(), motionController.Y(), motionController.Z()); + nimbleController.bleNus().Print(accBuf); } } diff --git a/src/components/console/Console.h b/src/components/console/Console.h index 941744817d..0ce5faf42a 100644 --- a/src/components/console/Console.h +++ b/src/components/console/Console.h @@ -17,6 +17,7 @@ namespace Pinetime { class NimbleController; class FS; class MotorController; + class MotionController; } namespace Drivers @@ -40,7 +41,8 @@ namespace Pinetime { Pinetime::Controllers::MotorController& motorController, Pinetime::Drivers::Cst816S& touchPanel, Pinetime::Drivers::SpiNorFlash& spiNorFlash, - Pinetime::Drivers::TwiMaster& twiMaster); + Pinetime::Drivers::TwiMaster& twiMaster, + Pinetime::Controllers::MotionController& motionController); void Init(); void Process(); @@ -56,6 +58,7 @@ namespace Pinetime { Pinetime::Drivers::Cst816S& touchPanel; Pinetime::Drivers::SpiNorFlash& spiNorFlash; Pinetime::Drivers::TwiMaster& twiMaster; + Pinetime::Controllers::MotionController& motionController; static constexpr int bufferSize = 256; char rxBuffer[bufferSize]; diff --git a/src/systemtask/SystemTask.cpp b/src/systemtask/SystemTask.cpp index bfb001236a..a21739f76b 100644 --- a/src/systemtask/SystemTask.cpp +++ b/src/systemtask/SystemTask.cpp @@ -97,7 +97,7 @@ SystemTask::SystemTask(Drivers::SpiMaster& spi, heartRateApp(heartRateApp), fs{fs}, nimbleController(*this, bleController, dateTimeController, notificationManager, batteryController, spiNorFlash, heartRateController), - console(*this, nimbleController, fs, lvgl, motorController, touchPanel, spiNorFlash, twiMaster) { + console(*this, nimbleController, fs, lvgl, motorController, touchPanel, spiNorFlash, twiMaster, motionController) { } From e560bc975c4d9e3f92ecc1f0be3556e00e4b4ec1 Mon Sep 17 00:00:00 2001 From: hubmartin Date: Wed, 11 Aug 2021 15:21:08 +0200 Subject: [PATCH 12/25] Cleanup --- src/components/ble/BleNus.cpp | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) diff --git a/src/components/ble/BleNus.cpp b/src/components/ble/BleNus.cpp index e0e38f8351..691a229c8e 100644 --- a/src/components/ble/BleNus.cpp +++ b/src/components/ble/BleNus.cpp @@ -43,31 +43,22 @@ void BleNus::ConsoleRegister(std::function f) this->rxDataFunction = f; } - int BleNus::OnDeviceInfoRequested(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt) { struct os_mbuf *om = ctxt->om; - //struct os_mbuf *om_tx; switch (ctxt->op) { case BLE_GATT_ACCESS_OP_WRITE_CHR: while(om) { - //console_write((char *)om->om_data, om->om_len); // reply the received data // test it with Bluefruit, NRF Connect on computer or phone, or in web browser - // https://wiki.makerdiary.com/web-device-cli/ + // https://terminal.hardwario.com/ rxDataFunction((char *)om->om_data, (int)om->om_len); - // om_tx = ble_hs_mbuf_from_flat((char *)om->om_data, om->om_len); - // if (om_tx) { - // ble_gattc_notify_custom(conn_handle, attr_read_handle, om_tx); - // } - om = SLIST_NEXT(om, om_next); } - //console_write("\n", 1); return 0; default: assert(0); @@ -77,13 +68,13 @@ int BleNus::OnDeviceInfoRequested(uint16_t conn_handle, uint16_t attr_handle, st BleNus::BleNus() : characteristicDefinition {{ - .uuid = (ble_uuid_t*) &rxCharacteristicUuid, + .uuid = &rxCharacteristicUuid.u, .access_cb = BleNusCallback, .arg = this, .flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_WRITE_NO_RSP, }, { - .uuid = (ble_uuid_t*) &txCharacteristicUuid, + .uuid = &txCharacteristicUuid.u, .access_cb = BleNusCallback, .arg = this, .flags = BLE_GATT_CHR_F_NOTIFY, @@ -93,7 +84,7 @@ BleNus::BleNus() serviceDefinition { {/* Device Information Service */ .type = BLE_GATT_SVC_TYPE_PRIMARY, - .uuid = (ble_uuid_t*) &nusServiceUuid, + .uuid = &nusServiceUuid.u, .characteristics = characteristicDefinition}, {0}, } { From 7191bb5a655d570c9b8454fab16268e327e37e1f Mon Sep 17 00:00:00 2001 From: hubmartin Date: Wed, 11 Aug 2021 15:31:33 +0200 Subject: [PATCH 13/25] Add rxPos len check --- src/components/console/Console.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/components/console/Console.cpp b/src/components/console/Console.cpp index bd8dee5c3e..a7e3a3435a 100644 --- a/src/components/console/Console.cpp +++ b/src/components/console/Console.cpp @@ -127,6 +127,12 @@ void Console::Received(char* str, int length) { //char b[128]; + // Wrap if input is too long without CR/LN + if(rxPos == bufferSize - 1) + { + rxPos = 0; + } + for(int i = 0; i < length; i++) { rxBuffer[rxPos++] = str[i]; From 6ec3aa457b16be844b19599cd085eaad8c687659 Mon Sep 17 00:00:00 2001 From: hubmartin Date: Wed, 11 Aug 2021 15:33:24 +0200 Subject: [PATCH 14/25] Cleanup --- src/components/ble/BleNus.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/components/ble/BleNus.cpp b/src/components/ble/BleNus.cpp index 691a229c8e..2580616899 100644 --- a/src/components/ble/BleNus.cpp +++ b/src/components/ble/BleNus.cpp @@ -7,7 +7,6 @@ constexpr ble_uuid128_t BleNus::nusServiceUuid; constexpr ble_uuid128_t BleNus::rxCharacteristicUuid; constexpr ble_uuid128_t BleNus::txCharacteristicUuid; uint16_t BleNus::attr_read_handle; -//static uint16_t conn_handle; int BleNusCallback(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg) { auto deviceInformationService = static_cast(arg); @@ -25,7 +24,6 @@ void BleNus::Init() { void BleNus::SetConnectionHandle(uint16_t connection_handle) { conn_handle = connection_handle; - } void BleNus::Print(char *str) From 0170cfe7a5ee891a019006f6bf0e56c91760644f Mon Sep 17 00:00:00 2001 From: hubmartin Date: Wed, 11 Aug 2021 21:50:19 +0200 Subject: [PATCH 15/25] Add hatmajster suggestions --- src/components/ble/BleNus.cpp | 28 ++--- src/components/ble/BleNus.h | 38 +++---- src/components/ble/NimbleController.cpp | 5 +- src/components/console/Console.cpp | 137 +++++++++++++++++++----- src/components/console/Console.h | 4 +- src/systemtask/SystemTask.cpp | 6 +- 6 files changed, 142 insertions(+), 76 deletions(-) diff --git a/src/components/ble/BleNus.cpp b/src/components/ble/BleNus.cpp index 2580616899..656207b18a 100644 --- a/src/components/ble/BleNus.cpp +++ b/src/components/ble/BleNus.cpp @@ -6,11 +6,11 @@ using namespace Pinetime::Controllers; constexpr ble_uuid128_t BleNus::nusServiceUuid; constexpr ble_uuid128_t BleNus::rxCharacteristicUuid; constexpr ble_uuid128_t BleNus::txCharacteristicUuid; -uint16_t BleNus::attr_read_handle; +uint16_t BleNus::attributeReadHandle; -int BleNusCallback(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg) { +int BleNusCallback(uint16_t connectionHandle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg) { auto deviceInformationService = static_cast(arg); - return deviceInformationService->OnDeviceInfoRequested(conn_handle, attr_handle, ctxt); + return deviceInformationService->OnDeviceInfoRequested(connectionHandle, attr_handle, ctxt); } void BleNus::Init() { @@ -23,34 +23,34 @@ void BleNus::Init() { } void BleNus::SetConnectionHandle(uint16_t connection_handle) { - conn_handle = connection_handle; + connectionHandle = connection_handle; } -void BleNus::Print(char *str) +void BleNus::Print(const std::string str) { - struct os_mbuf *om; - om = ble_hs_mbuf_from_flat(str, strlen(str)); + os_mbuf *om; + om = ble_hs_mbuf_from_flat(str.c_str(), str.length()); if (om) { - ble_gattc_notify_custom(conn_handle, attr_read_handle, om); + ble_gattc_notify_custom(connectionHandle, attributeReadHandle, om); } } -void BleNus::ConsoleRegister(std::function f) +void BleNus::RegisterRxCallback(std::function f) { this->rxDataFunction = f; } -int BleNus::OnDeviceInfoRequested(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt) { +int BleNus::OnDeviceInfoRequested(uint16_t connectionHandle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt) { - struct os_mbuf *om = ctxt->om; + os_mbuf *om = ctxt->om; switch (ctxt->op) { case BLE_GATT_ACCESS_OP_WRITE_CHR: while(om) { - // reply the received data - // test it with Bluefruit, NRF Connect on computer or phone, or in web browser + // Test BLE console it with Bluefruit, NRF Toolbox (you must add enter before hitting send! https://devzone.nordicsemi.com/f/nordic-q-a/33687/nrf-toolbox-2-6-0-uart-does-not-send-lf-cr-or-cr-lf-as-eol) + // on the phone, or in any Chromium-based web browser // https://terminal.hardwario.com/ rxDataFunction((char *)om->om_data, (int)om->om_len); @@ -76,7 +76,7 @@ BleNus::BleNus() .access_cb = BleNusCallback, .arg = this, .flags = BLE_GATT_CHR_F_NOTIFY, - .val_handle = &attr_read_handle + .val_handle = &attributeReadHandle }, {0}}, serviceDefinition { diff --git a/src/components/ble/BleNus.h b/src/components/ble/BleNus.h index 2760f2f0cf..8455037494 100644 --- a/src/components/ble/BleNus.h +++ b/src/components/ble/BleNus.h @@ -7,21 +7,6 @@ #include "Version.h" #include - -// Service UUID -// 6E400001-B5A3-F393-E0A9-E50E24DCCA9E -#define NUS_SERVICE_UUID_BASE \ - { 0x9e, 0xca, 0xdc, 0x24, 0x0e, 0xe5, 0xa9, 0xe0, 0x93, 0xf3, 0xa3, 0xb5, 0x01, 0x00, 0x40, 0x6e } - -//6E400002-B5A3-F393-E0A9-E50E24DCCA9E -#define NUS_CHARACTERTISTIC_RX_UUID \ - { 0x9e, 0xca, 0xdc, 0x24, 0x0e, 0xe5, 0xa9, 0xe0, 0x93, 0xf3, 0xa3, 0xb5, 0x02, 0x00, 0x40, 0x6e } - -//6E400003-B5A3-F393-E0A9-E50E24DCCA9E -#define NUS_CHARACTERTISTIC_TX_UUID \ - { 0x9e, 0xca, 0xdc, 0x24, 0x0e, 0xe5, 0xa9, 0xe0, 0x93, 0xf3, 0xa3, 0xb5, 0x03, 0x00, 0x40, 0x6e } - - namespace Pinetime { namespace Components { class Console; @@ -31,25 +16,26 @@ namespace Pinetime { public: BleNus(); void Init(); - void SetConnectionHandle(uint16_t conn_handle); - void Print(char *str); + void SetConnectionHandle(uint16_t connectionHandle); + void Print(const std::string str); - void ConsoleRegister(std::function f); + void RegisterRxCallback(std::function f); - int OnDeviceInfoRequested(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt); + int OnDeviceInfoRequested(uint16_t connectionHandle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt); private: - static uint16_t attr_read_handle; - uint16_t conn_handle; + static uint16_t attributeReadHandle; + uint16_t connectionHandle; std::function rxDataFunction; - static constexpr ble_uuid128_t nusServiceUuid {.u {.type = BLE_UUID_TYPE_128}, .value = NUS_SERVICE_UUID_BASE}; - - static constexpr ble_uuid128_t rxCharacteristicUuid {.u {.type = BLE_UUID_TYPE_128}, .value = NUS_CHARACTERTISTIC_RX_UUID}; - - static constexpr ble_uuid128_t txCharacteristicUuid {.u {.type = BLE_UUID_TYPE_128}, .value = NUS_CHARACTERTISTIC_TX_UUID}; + // 6E400001-B5A3-F393-E0A9-E50E24DCCA9E + static constexpr ble_uuid128_t nusServiceUuid {.u {.type = BLE_UUID_TYPE_128}, .value = { 0x9e, 0xca, 0xdc, 0x24, 0x0e, 0xe5, 0xa9, 0xe0, 0x93, 0xf3, 0xa3, 0xb5, 0x01, 0x00, 0x40, 0x6e }}; + //6E400002-B5A3-F393-E0A9-E50E24DCCA9E + static constexpr ble_uuid128_t rxCharacteristicUuid {.u {.type = BLE_UUID_TYPE_128}, .value = { 0x9e, 0xca, 0xdc, 0x24, 0x0e, 0xe5, 0xa9, 0xe0, 0x93, 0xf3, 0xa3, 0xb5, 0x02, 0x00, 0x40, 0x6e }}; + //6E400003-B5A3-F393-E0A9-E50E24DCCA9E + static constexpr ble_uuid128_t txCharacteristicUuid {.u {.type = BLE_UUID_TYPE_128}, .value = { 0x9e, 0xca, 0xdc, 0x24, 0x0e, 0xe5, 0xa9, 0xe0, 0x93, 0xf3, 0xa3, 0xb5, 0x03, 0x00, 0x40, 0x6e }}; struct ble_gatt_chr_def characteristicDefinition[3]; struct ble_gatt_svc_def serviceDefinition[2]; diff --git a/src/components/ble/NimbleController.cpp b/src/components/ble/NimbleController.cpp index 1c2521ce58..f64024413e 100644 --- a/src/components/ble/NimbleController.cpp +++ b/src/components/ble/NimbleController.cpp @@ -79,8 +79,6 @@ void NimbleController::Init() { bleController.AddressType((addrType == 0) ? Ble::AddressTypes::Public : Ble::AddressTypes::Random); bleController.Address(std::move(address)); - //bleNus.ConsoleRegister(&systemTask.console); - res = ble_gatts_start(); ASSERT(res == 0); } @@ -147,8 +145,6 @@ int NimbleController::OnGAPEvent(ble_gap_event* event) { /* A new connection was established or a connection attempt failed. */ NRF_LOG_INFO("connection %s; status=%d ", event->connect.status == 0 ? "established" : "failed", event->connect.status); - bleNusService.SetConnectionHandle(event->connect.conn_handle); - if (event->connect.status != 0) { /* Connection failed; resume advertising. */ StartAdvertising(); @@ -157,6 +153,7 @@ int NimbleController::OnGAPEvent(ble_gap_event* event) { bleController.Connect(); systemTask.PushMessage(Pinetime::System::Messages::BleConnected); connectionHandle = event->connect.conn_handle; + bleNusService.SetConnectionHandle(event->connect.conn_handle); // Service discovery is deffered via systemtask } } break; diff --git a/src/components/console/Console.cpp b/src/components/console/Console.cpp index a7e3a3435a..0f293364c1 100644 --- a/src/components/console/Console.cpp +++ b/src/components/console/Console.cpp @@ -1,4 +1,3 @@ - #include "Console.h" #include "systemtask/SystemTask.h" #include "components/ble/NimbleController.h" @@ -33,14 +32,56 @@ void Console::Init() this->Received(str, length); }; - nimbleController.bleNus().ConsoleRegister(rxCallback); + nimbleController.bleNus().RegisterRxCallback(rxCallback); } -void Console::Print(char *str) +void Console::Print(const std::string str) { nimbleController.bleNus().Print(str); } +static bool cmdCmp(char *buffer, const std::string search) +{ + return strncmp(buffer, search.c_str(), search.length()) == 0; +} + +static lv_color_t parseHexColorString(const char *str) +{ + char tmp[3]; + + // Skip index 0 with '#' + tmp[0] = str[1]; + tmp[1] = str[2]; + tmp[2] = '\0'; + int red = strtol(tmp, NULL, 16); + + tmp[0] = str[3]; + tmp[1] = str[4]; + tmp[2] = '\0'; + int green = strtol(tmp, NULL, 16); + + tmp[0] = str[5]; + tmp[1] = str[6]; + tmp[2] = '\0'; + int blue = strtol(tmp, NULL, 16); + + return lv_color_make(red, green, blue); +} + +// Example showing how you can use console to change LVGL properties temporarily for development purpose +// This might be improved for position and width of elements +static void setObjectsColor(const char *hexColor) +{ + lv_obj_t* actStrc = lv_scr_act(); + + lv_obj_t * child; + child = lv_obj_get_child(actStrc, NULL); + while(child) { + lv_obj_set_style_local_text_color(child, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, parseHexColorString(hexColor)); + child = lv_obj_get_child(actStrc, child); + } +} + void Console::Process() { static uint32_t accCount = 0; @@ -50,62 +91,107 @@ void Console::Process() { hasCommandFlag = false; + static constexpr int maxArgumentsCount = 20; + static constexpr int maxBufferLength = 128; + + char arg_buffer[maxBufferLength]; + const char *args[maxArgumentsCount]; + + // Copy string, becase we replace ' ' with '\0' for proper string termination + strncpy(arg_buffer, rxBuffer, sizeof(arg_buffer)); + + // First argument is always command name itself + int argc = 1; + args[0] = arg_buffer; + + int param_len = strlen(rxBuffer); + + for (uint8_t i = 0; i < param_len; i++) + { + if (rxBuffer[i] == ' ' && param_len > (i + 1)) + { + arg_buffer[i] = '\0'; + args[argc++] = &arg_buffer[i+1]; + } + + if (argc == maxArgumentsCount) + { + // Max argument count reached + break; + } + } + // This AT > OK needs to be there, because https://terminal.hardwario.com/ waits for the answer // When we use or create better webpage terminal, this can go out - if(strncmp(rxBuffer, "AT", 2) == 0) + if(cmdCmp(rxBuffer, "AT")) { - nimbleController.bleNus().Print((char*)"OK\r\n"); + Print((char*)"OK\r\n"); } - else if(strncmp(rxBuffer, "LVGL", 4) == 0) + else if(cmdCmp(rxBuffer, "COLOR")) + { + if(argc == 2) + { + setObjectsColor(args[1]); + } + else + { + Print("Expects 1 parameter"); + } + } + else if(cmdCmp(rxBuffer, "LVGL")) { - // TODO: list of objects, changing position, size & color would be great char lvbuf[128]; + snprintf(lvbuf, sizeof(lvbuf), "argc: %d, cmd: %s, 1:%s, 2:%s, 3:%s", argc, args[0], args[1], args[2], args[3]); + Print(lvbuf); + + // TODO: list of objects, changing position, size & color would be great + lv_mem_monitor_t mon; lv_mem_monitor(&mon); snprintf(lvbuf, sizeof(lvbuf), "used: %6d (%3d %%), frag: %3d %%, biggest free: %6d\n", (int)(mon.total_size - mon.free_size), mon.used_pct, mon.frag_pct, (int)mon.free_biggest_size); - nimbleController.bleNus().Print(lvbuf); + Print(lvbuf); // List active screen objects lv_obj_t* actStrc = lv_scr_act(); uint16_t childCount = lv_obj_count_children(actStrc); snprintf(lvbuf, sizeof(lvbuf), "children: %d\n", childCount); - nimbleController.bleNus().Print(lvbuf); + Print(lvbuf); lv_obj_t * child; uint16_t i = 0; child = lv_obj_get_child(actStrc, NULL); while(child) { snprintf(lvbuf, sizeof(lvbuf), "#%d, x: %d, y: %d, w: %d, h: %d\n", i++, lv_obj_get_x(child), lv_obj_get_y(child), lv_obj_get_width(child), lv_obj_get_height(child)); - nimbleController.bleNus().Print(lvbuf); + Print(lvbuf); vTaskDelay(50); // Add small delay for each item, so the print buffer has time to be send over BLE child = lv_obj_get_child(actStrc, child); } } - else if(strncmp(rxBuffer, "VIBRATE", 7) == 0) + else if(cmdCmp(rxBuffer, "VIBRATE")) { motorController.SetDuration(100); } - else if(strncmp(rxBuffer, "FS", 2) == 0) + else if(cmdCmp(rxBuffer, "FS")) { // TODO: add directory listings etc. } - else if(strncmp(rxBuffer, "WKUP", 4) == 0) + else if(cmdCmp(rxBuffer, "WKUP")) { systemTask.PushMessage(Pinetime::System::Messages::GoToRunning); } - else if(strncmp(rxBuffer, "SLEEP", 5) == 0) + else if(cmdCmp(rxBuffer, "SLEEP")) { systemTask.PushMessage(Pinetime::System::Messages::GoToSleep); } - else if(strncmp(rxBuffer, "SPINOR", 6) == 0) + else if(cmdCmp(rxBuffer, "SPINOR")) { // TODO: print RAW data from FLASH } - else if(strncmp(rxBuffer, "ACC", 3) == 0) + else if(cmdCmp(rxBuffer, "ACC")) { // Print 50 accelerometer measurements accCount = 50; @@ -119,22 +205,20 @@ void Console::Process() char accBuf[32]; snprintf(accBuf, sizeof(accBuf), "%d, %d, %d\n", motionController.X(), motionController.Y(), motionController.Z()); - nimbleController.bleNus().Print(accBuf); + Print(accBuf); } } void Console::Received(char* str, int length) { - //char b[128]; - - // Wrap if input is too long without CR/LN - if(rxPos == bufferSize - 1) - { - rxPos = 0; - } - for(int i = 0; i < length; i++) { + // Wrap if input is too long without CR/LN + if(rxPos == bufferSize - 1) + { + rxPos = 0; + } + rxBuffer[rxPos++] = str[i]; rxBuffer[rxPos] = '\0'; // terminate for debug print @@ -145,7 +229,4 @@ void Console::Received(char* str, int length) break; } } - - //sprintf(b, "rx: %s, len: %d, buffer: %s\r\n", str, length, rxBuffer); - //nimbleController.bleNus().Print(b); } \ No newline at end of file diff --git a/src/components/console/Console.h b/src/components/console/Console.h index 0ce5faf42a..7e4d6462e5 100644 --- a/src/components/console/Console.h +++ b/src/components/console/Console.h @@ -3,6 +3,8 @@ #include #include +#include + //#include "systemtask/SystemTask.h" @@ -46,7 +48,7 @@ namespace Pinetime { void Init(); void Process(); - void Print(char *str); + void Print(std::string str); void Received(char* str, int length); private: diff --git a/src/systemtask/SystemTask.cpp b/src/systemtask/SystemTask.cpp index a21739f76b..2103d09fa4 100644 --- a/src/systemtask/SystemTask.cpp +++ b/src/systemtask/SystemTask.cpp @@ -347,12 +347,12 @@ void SystemTask::Work() { xTimerStart(dimTimer, 0); break; case Messages::OnTouchEvent: - console.Print((char*)"Touch event\r\n"); + console.Print("Touch event\r\n"); ReloadIdleTimer(); displayApp.PushMessage(Pinetime::Applications::Display::Messages::TouchEvent); break; case Messages::OnButtonEvent: - console.Print((char*)"Button event\r\n"); + console.Print("Button event\r\n"); ReloadIdleTimer(); displayApp.PushMessage(Pinetime::Applications::Display::Messages::ButtonPushed); break; @@ -509,7 +509,7 @@ void SystemTask::OnIdle() { if (doNotGoToSleep) return; NRF_LOG_INFO("Idle timeout -> Going to sleep") - console.Print((char*)"Idle timeout -> Going to sleep\r\n"); + console.Print("Idle timeout -> Going to sleep\r\n"); PushMessage(Messages::GoToSleep); } From eeda7adb3ead9a3774e57787bc940c766b672ff9 Mon Sep 17 00:00:00 2001 From: hubmartin Date: Wed, 11 Aug 2021 22:07:48 +0200 Subject: [PATCH 16/25] Clean commented NRF log block --- src/systemtask/SystemTask.cpp | 42 ----------------------------------- 1 file changed, 42 deletions(-) diff --git a/src/systemtask/SystemTask.cpp b/src/systemtask/SystemTask.cpp index 2103d09fa4..ef1ec202be 100644 --- a/src/systemtask/SystemTask.cpp +++ b/src/systemtask/SystemTask.cpp @@ -112,42 +112,6 @@ void SystemTask::Process(void* instance) { NRF_LOG_INFO("systemtask task started!"); app->Work(); } -/* -#define NRF_LOG_BACKEND_BLENUS_TEMP_BUFFER_SIZE 128 -static uint8_t m_string_buff[NRF_LOG_BACKEND_BLENUS_TEMP_BUFFER_SIZE]; - -static void blenus_tx(void const * p_context, char const * p_buffer, size_t len) -{ - //console.Print(p_buffer); -} - -static void nrf_log_backend_blenus_put(nrf_log_backend_t const * p_backend, nrf_log_entry_t * p_msg) -{ - nrf_log_backend_serial_put(p_backend, p_msg, m_string_buff, NRF_LOG_BACKEND_BLENUS_TEMP_BUFFER_SIZE, blenus_tx); -} - -static void nrf_log_backend_blenus_flush(nrf_log_backend_t const * p_backend) -{ - -} - -static void nrf_log_backend_blenus_panic_set(nrf_log_backend_t const * p_backend) -{ - //nrf_drv_uart_uninit(&m_uart); - - // uart_init(false); -} - - - static const nrf_log_backend_api_t nrf_log_backend_blenus_api = { - .put = nrf_log_backend_blenus_put, - .panic_set = nrf_log_backend_blenus_panic_set, - .flush = nrf_log_backend_blenus_flush, - }; - - - NRF_LOG_BACKEND_DEF(blenus_backend, nrf_log_backend_blenus_api, NULL); -*/ void SystemTask::Work() { watchdog.Setup(7); @@ -195,12 +159,6 @@ void SystemTask::Work() { console.Init(); - /* - int32_t backend_id = nrf_log_backend_add(&blenus_backend, NRF_LOG_SEVERITY_DEBUG); - ASSERT(backend_id >= 0); - nrf_log_backend_enable(&blenus_backend); -*/ - nrf_gpio_cfg_sense_input(pinButton, (nrf_gpio_pin_pull_t) GPIO_PIN_CNF_PULL_Pulldown, (nrf_gpio_pin_sense_t) GPIO_PIN_CNF_SENSE_High); nrf_gpio_cfg_output(15); nrf_gpio_pin_set(15); From f15ce019c1ec7539d42039e02ecba3f6dfbfab57 Mon Sep 17 00:00:00 2001 From: hubmartin Date: Fri, 13 Aug 2021 15:02:40 +0200 Subject: [PATCH 17/25] Move acc debug cmd & lvgl info to funtions --- src/components/console/Console.cpp | 81 +++++++++++++++++------------- src/components/console/Console.h | 3 ++ 2 files changed, 48 insertions(+), 36 deletions(-) diff --git a/src/components/console/Console.cpp b/src/components/console/Console.cpp index 0f293364c1..46105315a7 100644 --- a/src/components/console/Console.cpp +++ b/src/components/console/Console.cpp @@ -82,6 +82,47 @@ static void setObjectsColor(const char *hexColor) } } +void Console::AccelerometerDebug() +{ + char accBuf[32]; + snprintf(accBuf, sizeof(accBuf), "%d, %d, %d\n", motionController.X(), motionController.Y(), motionController.Z()); + Print(accBuf); +} + +void Console::CommandLvgl(const char *args[], uint16_t argc) +{ + char lvbuf[128]; + snprintf(lvbuf, sizeof(lvbuf), "argc: %d, cmd: %s, 1:%s, 2:%s, 3:%s", argc, args[0], args[1], args[2], args[3]); + Print(lvbuf); + + // TODO: list of objects, changing position, size & color would be great + + lv_mem_monitor_t mon; + lv_mem_monitor(&mon); + snprintf(lvbuf, sizeof(lvbuf), "used: %6d (%3d %%), frag: %3d %%, biggest free: %6d\n", (int)(mon.total_size - mon.free_size), + mon.used_pct, + mon.frag_pct, + (int)mon.free_biggest_size); + Print(lvbuf); + + // List active screen objects + lv_obj_t* actStrc = lv_scr_act(); + uint16_t childCount = lv_obj_count_children(actStrc); + snprintf(lvbuf, sizeof(lvbuf), "children: %d\n", childCount); + Print(lvbuf); + + lv_obj_t * child; + uint16_t i = 0; + child = lv_obj_get_child(actStrc, NULL); + while(child) { + snprintf(lvbuf, sizeof(lvbuf), "#%d, x: %d, y: %d, w: %d, h: %d\n", i++, lv_obj_get_x(child), lv_obj_get_y(child), lv_obj_get_width(child), lv_obj_get_height(child)); + Print(lvbuf); + vTaskDelay(50); // Add small delay for each item, so the print buffer has time to be send over BLE + + child = lv_obj_get_child(actStrc, child); + } +} + void Console::Process() { static uint32_t accCount = 0; @@ -101,10 +142,10 @@ void Console::Process() strncpy(arg_buffer, rxBuffer, sizeof(arg_buffer)); // First argument is always command name itself - int argc = 1; + uint16_t argc = 1; args[0] = arg_buffer; - int param_len = strlen(rxBuffer); + uint16_t param_len = strlen(rxBuffer); for (uint8_t i = 0; i < param_len; i++) { @@ -140,36 +181,7 @@ void Console::Process() } else if(cmdCmp(rxBuffer, "LVGL")) { - char lvbuf[128]; - snprintf(lvbuf, sizeof(lvbuf), "argc: %d, cmd: %s, 1:%s, 2:%s, 3:%s", argc, args[0], args[1], args[2], args[3]); - Print(lvbuf); - - // TODO: list of objects, changing position, size & color would be great - - lv_mem_monitor_t mon; - lv_mem_monitor(&mon); - snprintf(lvbuf, sizeof(lvbuf), "used: %6d (%3d %%), frag: %3d %%, biggest free: %6d\n", (int)(mon.total_size - mon.free_size), - mon.used_pct, - mon.frag_pct, - (int)mon.free_biggest_size); - Print(lvbuf); - - // List active screen objects - lv_obj_t* actStrc = lv_scr_act(); - uint16_t childCount = lv_obj_count_children(actStrc); - snprintf(lvbuf, sizeof(lvbuf), "children: %d\n", childCount); - Print(lvbuf); - - lv_obj_t * child; - uint16_t i = 0; - child = lv_obj_get_child(actStrc, NULL); - while(child) { - snprintf(lvbuf, sizeof(lvbuf), "#%d, x: %d, y: %d, w: %d, h: %d\n", i++, lv_obj_get_x(child), lv_obj_get_y(child), lv_obj_get_width(child), lv_obj_get_height(child)); - Print(lvbuf); - vTaskDelay(50); // Add small delay for each item, so the print buffer has time to be send over BLE - - child = lv_obj_get_child(actStrc, child); - } + CommandLvgl(args, argc); } else if(cmdCmp(rxBuffer, "VIBRATE")) { @@ -202,10 +214,7 @@ void Console::Process() if(accCount) { accCount--; - char accBuf[32]; - - snprintf(accBuf, sizeof(accBuf), "%d, %d, %d\n", motionController.X(), motionController.Y(), motionController.Z()); - Print(accBuf); + AccelerometerDebug(); } } diff --git a/src/components/console/Console.h b/src/components/console/Console.h index 7e4d6462e5..27195045ce 100644 --- a/src/components/console/Console.h +++ b/src/components/console/Console.h @@ -67,6 +67,9 @@ namespace Pinetime { uint16_t rxPos; bool hasCommandFlag; + void CommandLvgl(const char *args[], uint16_t argc); + void AccelerometerDebug(); + }; } } From a3317d892b0979652a298d298e68ac757763e553 Mon Sep 17 00:00:00 2001 From: hubmartin Date: Fri, 13 Aug 2021 18:33:04 +0200 Subject: [PATCH 18/25] Compare with \r & \n --- src/components/console/Console.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/console/Console.cpp b/src/components/console/Console.cpp index 46105315a7..97cea53270 100644 --- a/src/components/console/Console.cpp +++ b/src/components/console/Console.cpp @@ -231,7 +231,7 @@ void Console::Received(char* str, int length) rxBuffer[rxPos++] = str[i]; rxBuffer[rxPos] = '\0'; // terminate for debug print - if(str[i] == 13 || str[i] == 10) + if(str[i] == '\n' || str[i] == '\r') { rxPos = 0; hasCommandFlag = true; From e94d0fdcc1db3da4f7fd0e8c90f9fe7b385ca715 Mon Sep 17 00:00:00 2001 From: hubmartin Date: Fri, 13 Aug 2021 18:52:34 +0200 Subject: [PATCH 19/25] Handle console by event Messages::ConsoleProcess --- src/components/console/Console.cpp | 136 ++++++++++++----------------- src/components/console/Console.h | 1 - src/systemtask/Messages.h | 3 +- src/systemtask/SystemTask.cpp | 7 +- 4 files changed, 64 insertions(+), 83 deletions(-) diff --git a/src/components/console/Console.cpp b/src/components/console/Console.cpp index 97cea53270..6225fa200d 100644 --- a/src/components/console/Console.cpp +++ b/src/components/console/Console.cpp @@ -92,8 +92,6 @@ void Console::AccelerometerDebug() void Console::CommandLvgl(const char *args[], uint16_t argc) { char lvbuf[128]; - snprintf(lvbuf, sizeof(lvbuf), "argc: %d, cmd: %s, 1:%s, 2:%s, 3:%s", argc, args[0], args[1], args[2], args[3]); - Print(lvbuf); // TODO: list of objects, changing position, size & color would be great @@ -125,96 +123,78 @@ void Console::CommandLvgl(const char *args[], uint16_t argc) void Console::Process() { - static uint32_t accCount = 0; - - // Simple stupid comparison, later would be nice to add commands lookup table with argument parsing - if(hasCommandFlag) - { - hasCommandFlag = false; + static constexpr int maxArgumentsCount = 20; + static constexpr int maxBufferLength = 128; - static constexpr int maxArgumentsCount = 20; - static constexpr int maxBufferLength = 128; - - char arg_buffer[maxBufferLength]; - const char *args[maxArgumentsCount]; - - // Copy string, becase we replace ' ' with '\0' for proper string termination - strncpy(arg_buffer, rxBuffer, sizeof(arg_buffer)); + char arg_buffer[maxBufferLength]; + const char *args[maxArgumentsCount]; + + // Copy string, becase we replace ' ' with '\0' for proper string termination + strncpy(arg_buffer, rxBuffer, sizeof(arg_buffer)); - // First argument is always command name itself - uint16_t argc = 1; - args[0] = arg_buffer; + // First argument is always command name itself + uint16_t argc = 1; + args[0] = arg_buffer; - uint16_t param_len = strlen(rxBuffer); + uint16_t param_len = strlen(rxBuffer); - for (uint8_t i = 0; i < param_len; i++) + for (uint8_t i = 0; i < param_len; i++) + { + if (rxBuffer[i] == ' ' && param_len > (i + 1)) { - if (rxBuffer[i] == ' ' && param_len > (i + 1)) - { - arg_buffer[i] = '\0'; - args[argc++] = &arg_buffer[i+1]; - } - - if (argc == maxArgumentsCount) - { - // Max argument count reached - break; - } + arg_buffer[i] = '\0'; + args[argc++] = &arg_buffer[i+1]; } - // This AT > OK needs to be there, because https://terminal.hardwario.com/ waits for the answer - // When we use or create better webpage terminal, this can go out - if(cmdCmp(rxBuffer, "AT")) - { - Print((char*)"OK\r\n"); - } - else if(cmdCmp(rxBuffer, "COLOR")) - { - if(argc == 2) - { - setObjectsColor(args[1]); - } - else - { - Print("Expects 1 parameter"); - } - } - else if(cmdCmp(rxBuffer, "LVGL")) - { - CommandLvgl(args, argc); - } - else if(cmdCmp(rxBuffer, "VIBRATE")) - { - motorController.SetDuration(100); - } - else if(cmdCmp(rxBuffer, "FS")) - { - // TODO: add directory listings etc. - } - else if(cmdCmp(rxBuffer, "WKUP")) - { - systemTask.PushMessage(Pinetime::System::Messages::GoToRunning); - } - else if(cmdCmp(rxBuffer, "SLEEP")) + if (argc == maxArgumentsCount) { - systemTask.PushMessage(Pinetime::System::Messages::GoToSleep); + // Max argument count reached + break; } - else if(cmdCmp(rxBuffer, "SPINOR")) + } + + // Simple stupid command comparison, later would be nice to add commands lookup table with argument parsing + + // This AT > OK needs to be there, because https://terminal.hardwario.com/ waits for the answer + // When we use or create better webpage terminal, this can go out + if(cmdCmp(rxBuffer, "AT")) + { + Print((char*)"OK\r\n"); + } + else if(cmdCmp(rxBuffer, "COLOR")) + { + if(argc == 2) { - // TODO: print RAW data from FLASH + setObjectsColor(args[1]); } - else if(cmdCmp(rxBuffer, "ACC")) + else { - // Print 50 accelerometer measurements - accCount = 50; + Print("Expects 1 parameter"); } } - - // Debug print accelerometer values - if(accCount) + else if(cmdCmp(rxBuffer, "LVGL")) + { + CommandLvgl(args, argc); + } + else if(cmdCmp(rxBuffer, "VIBRATE")) + { + motorController.SetDuration(100); + } + else if(cmdCmp(rxBuffer, "FS")) + { + // TODO: add directory listings etc. + } + else if(cmdCmp(rxBuffer, "WKUP")) + { + systemTask.PushMessage(Pinetime::System::Messages::GoToRunning); + } + else if(cmdCmp(rxBuffer, "SLEEP")) + { + systemTask.PushMessage(Pinetime::System::Messages::GoToSleep); + } + else if(cmdCmp(rxBuffer, "SPINOR")) { - accCount--; - AccelerometerDebug(); + // TODO: print RAW data from FLASH } } @@ -234,7 +214,7 @@ void Console::Received(char* str, int length) if(str[i] == '\n' || str[i] == '\r') { rxPos = 0; - hasCommandFlag = true; + systemTask.PushMessage(System::Messages::ConsoleProcess); break; } } diff --git a/src/components/console/Console.h b/src/components/console/Console.h index 27195045ce..173c919635 100644 --- a/src/components/console/Console.h +++ b/src/components/console/Console.h @@ -65,7 +65,6 @@ namespace Pinetime { static constexpr int bufferSize = 256; char rxBuffer[bufferSize]; uint16_t rxPos; - bool hasCommandFlag; void CommandLvgl(const char *args[], uint16_t argc); void AccelerometerDebug(); diff --git a/src/systemtask/Messages.h b/src/systemtask/Messages.h index 3a195e2d41..2309fc6f25 100644 --- a/src/systemtask/Messages.h +++ b/src/systemtask/Messages.h @@ -20,7 +20,8 @@ namespace Pinetime { EnableSleeping, DisableSleeping, OnNewDay, - OnChargingEvent + OnChargingEvent, + ConsoleProcess }; } } diff --git a/src/systemtask/SystemTask.cpp b/src/systemtask/SystemTask.cpp index ef1ec202be..bc92709e28 100644 --- a/src/systemtask/SystemTask.cpp +++ b/src/systemtask/SystemTask.cpp @@ -342,6 +342,10 @@ void SystemTask::Work() { // Battery level is updated on every message - there's no need to do anything break; + case Messages::ConsoleProcess: + console.Process(); + break; + default: break; } @@ -363,9 +367,6 @@ void SystemTask::Work() { batteryNotificationTick = xTaskGetTickCount(); } - - console.Process(); - monitor.Process(); uint32_t systick_counter = nrf_rtc_counter_get(portNRF_RTC_REG); dateTimeController.UpdateTime(systick_counter); From b1304e95515fac67f9b07fc164c146ae0176ee58 Mon Sep 17 00:00:00 2001 From: hubmartin Date: Sat, 21 Aug 2021 21:01:02 +0200 Subject: [PATCH 20/25] Comment LVGL commands because of recovery FW does not compiled --- src/components/console/Console.cpp | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/components/console/Console.cpp b/src/components/console/Console.cpp index 6225fa200d..4b7ba5b062 100644 --- a/src/components/console/Console.cpp +++ b/src/components/console/Console.cpp @@ -44,7 +44,7 @@ static bool cmdCmp(char *buffer, const std::string search) { return strncmp(buffer, search.c_str(), search.length()) == 0; } - +/* static lv_color_t parseHexColorString(const char *str) { char tmp[3]; @@ -81,7 +81,7 @@ static void setObjectsColor(const char *hexColor) child = lv_obj_get_child(actStrc, child); } } - +*/ void Console::AccelerometerDebug() { char accBuf[32]; @@ -89,6 +89,7 @@ void Console::AccelerometerDebug() Print(accBuf); } +/* void Console::CommandLvgl(const char *args[], uint16_t argc) { char lvbuf[128]; @@ -120,6 +121,7 @@ void Console::CommandLvgl(const char *args[], uint16_t argc) child = lv_obj_get_child(actStrc, child); } } +*/ void Console::Process() { @@ -153,6 +155,9 @@ void Console::Process() } } + (void) args; + (void) argc; + // Simple stupid command comparison, later would be nice to add commands lookup table with argument parsing // This AT > OK needs to be there, because https://terminal.hardwario.com/ waits for the answer @@ -163,18 +168,18 @@ void Console::Process() } else if(cmdCmp(rxBuffer, "COLOR")) { - if(argc == 2) + /*if(argc == 2) { setObjectsColor(args[1]); } else { Print("Expects 1 parameter"); - } + }*/ } else if(cmdCmp(rxBuffer, "LVGL")) { - CommandLvgl(args, argc); + //CommandLvgl(args, argc); } else if(cmdCmp(rxBuffer, "VIBRATE")) { From 0de7822241754934a26b7d61443f7848ff20b702 Mon Sep 17 00:00:00 2001 From: hubmartin Date: Sat, 11 Sep 2021 17:09:18 +0200 Subject: [PATCH 21/25] Update vibrations to RunForDuration --- src/components/console/Console.cpp | 2 +- src/systemtask/SystemTask.cpp | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/components/console/Console.cpp b/src/components/console/Console.cpp index 4b7ba5b062..ea4088d973 100644 --- a/src/components/console/Console.cpp +++ b/src/components/console/Console.cpp @@ -183,7 +183,7 @@ void Console::Process() } else if(cmdCmp(rxBuffer, "VIBRATE")) { - motorController.SetDuration(100); + motorController.RunForDuration(100); } else if(cmdCmp(rxBuffer, "FS")) { diff --git a/src/systemtask/SystemTask.cpp b/src/systemtask/SystemTask.cpp index 89014df807..9c19da8c54 100644 --- a/src/systemtask/SystemTask.cpp +++ b/src/systemtask/SystemTask.cpp @@ -99,8 +99,8 @@ SystemTask::SystemTask(Drivers::SpiMaster& spi, fs{fs}, touchHandler {touchHandler}, nimbleController(*this, bleController, dateTimeController, notificationManager, batteryController, spiNorFlash, heartRateController), - console(*this, nimbleController, fs, lvgl, motorController, touchPanel, spiNorFlash, twiMaster, motionController) - + console(*this, nimbleController, fs, lvgl, motorController, touchPanel, spiNorFlash, twiMaster, motionController) { + } void SystemTask::Start() { From 2ad36adadb03ca524fd374dc0dcba18a4e8df584 Mon Sep 17 00:00:00 2001 From: hubmartin Date: Fri, 22 Oct 2021 21:56:35 +0200 Subject: [PATCH 22/25] Clean code, lower rx buffer --- src/components/console/Console.cpp | 114 +---------------------------- src/components/console/Console.h | 7 +- 2 files changed, 4 insertions(+), 117 deletions(-) diff --git a/src/components/console/Console.cpp b/src/components/console/Console.cpp index ea4088d973..ada8fe84b5 100644 --- a/src/components/console/Console.cpp +++ b/src/components/console/Console.cpp @@ -44,89 +44,11 @@ static bool cmdCmp(char *buffer, const std::string search) { return strncmp(buffer, search.c_str(), search.length()) == 0; } -/* -static lv_color_t parseHexColorString(const char *str) -{ - char tmp[3]; - - // Skip index 0 with '#' - tmp[0] = str[1]; - tmp[1] = str[2]; - tmp[2] = '\0'; - int red = strtol(tmp, NULL, 16); - - tmp[0] = str[3]; - tmp[1] = str[4]; - tmp[2] = '\0'; - int green = strtol(tmp, NULL, 16); - - tmp[0] = str[5]; - tmp[1] = str[6]; - tmp[2] = '\0'; - int blue = strtol(tmp, NULL, 16); - - return lv_color_make(red, green, blue); -} - -// Example showing how you can use console to change LVGL properties temporarily for development purpose -// This might be improved for position and width of elements -static void setObjectsColor(const char *hexColor) -{ - lv_obj_t* actStrc = lv_scr_act(); - - lv_obj_t * child; - child = lv_obj_get_child(actStrc, NULL); - while(child) { - lv_obj_set_style_local_text_color(child, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, parseHexColorString(hexColor)); - child = lv_obj_get_child(actStrc, child); - } -} -*/ -void Console::AccelerometerDebug() -{ - char accBuf[32]; - snprintf(accBuf, sizeof(accBuf), "%d, %d, %d\n", motionController.X(), motionController.Y(), motionController.Z()); - Print(accBuf); -} - -/* -void Console::CommandLvgl(const char *args[], uint16_t argc) -{ - char lvbuf[128]; - - // TODO: list of objects, changing position, size & color would be great - - lv_mem_monitor_t mon; - lv_mem_monitor(&mon); - snprintf(lvbuf, sizeof(lvbuf), "used: %6d (%3d %%), frag: %3d %%, biggest free: %6d\n", (int)(mon.total_size - mon.free_size), - mon.used_pct, - mon.frag_pct, - (int)mon.free_biggest_size); - Print(lvbuf); - - // List active screen objects - lv_obj_t* actStrc = lv_scr_act(); - uint16_t childCount = lv_obj_count_children(actStrc); - snprintf(lvbuf, sizeof(lvbuf), "children: %d\n", childCount); - Print(lvbuf); - - lv_obj_t * child; - uint16_t i = 0; - child = lv_obj_get_child(actStrc, NULL); - while(child) { - snprintf(lvbuf, sizeof(lvbuf), "#%d, x: %d, y: %d, w: %d, h: %d\n", i++, lv_obj_get_x(child), lv_obj_get_y(child), lv_obj_get_width(child), lv_obj_get_height(child)); - Print(lvbuf); - vTaskDelay(50); // Add small delay for each item, so the print buffer has time to be send over BLE - - child = lv_obj_get_child(actStrc, child); - } -} -*/ void Console::Process() { - static constexpr int maxArgumentsCount = 20; - static constexpr int maxBufferLength = 128; + static constexpr int maxArgumentsCount = 4; + static constexpr int maxBufferLength = 64; char arg_buffer[maxBufferLength]; const char *args[maxArgumentsCount]; @@ -159,36 +81,10 @@ void Console::Process() (void) argc; // Simple stupid command comparison, later would be nice to add commands lookup table with argument parsing - - // This AT > OK needs to be there, because https://terminal.hardwario.com/ waits for the answer - // When we use or create better webpage terminal, this can go out - if(cmdCmp(rxBuffer, "AT")) - { - Print((char*)"OK\r\n"); - } - else if(cmdCmp(rxBuffer, "COLOR")) - { - /*if(argc == 2) - { - setObjectsColor(args[1]); - } - else - { - Print("Expects 1 parameter"); - }*/ - } - else if(cmdCmp(rxBuffer, "LVGL")) - { - //CommandLvgl(args, argc); - } - else if(cmdCmp(rxBuffer, "VIBRATE")) + if(cmdCmp(rxBuffer, "VIBRATE")) { motorController.RunForDuration(100); } - else if(cmdCmp(rxBuffer, "FS")) - { - // TODO: add directory listings etc. - } else if(cmdCmp(rxBuffer, "WKUP")) { systemTask.PushMessage(Pinetime::System::Messages::GoToRunning); @@ -197,10 +93,6 @@ void Console::Process() { systemTask.PushMessage(Pinetime::System::Messages::GoToSleep); } - else if(cmdCmp(rxBuffer, "SPINOR")) - { - // TODO: print RAW data from FLASH - } } void Console::Received(char* str, int length) diff --git a/src/components/console/Console.h b/src/components/console/Console.h index 173c919635..febaa0c255 100644 --- a/src/components/console/Console.h +++ b/src/components/console/Console.h @@ -1,13 +1,9 @@ #pragma once - #include #include #include - -//#include "systemtask/SystemTask.h" - namespace Pinetime { namespace System { @@ -29,7 +25,6 @@ namespace Pinetime { class TwiMaster; } - namespace Components { class LittleVgl; @@ -62,7 +57,7 @@ namespace Pinetime { Pinetime::Drivers::TwiMaster& twiMaster; Pinetime::Controllers::MotionController& motionController; - static constexpr int bufferSize = 256; + static constexpr int bufferSize = 32; char rxBuffer[bufferSize]; uint16_t rxPos; From f667aabe816c967c28e596f1c0193f3934b93faa Mon Sep 17 00:00:00 2001 From: hubmartin Date: Fri, 22 Oct 2021 21:56:51 +0200 Subject: [PATCH 23/25] Remove console.Print examples --- src/systemtask/SystemTask.cpp | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/systemtask/SystemTask.cpp b/src/systemtask/SystemTask.cpp index 73d2e77535..be2f38efcb 100644 --- a/src/systemtask/SystemTask.cpp +++ b/src/systemtask/SystemTask.cpp @@ -323,7 +323,6 @@ void SystemTask::Work() { xTimerStart(dimTimer, 0); break; case Messages::OnTouchEvent: - console.Print("Touch event\r\n"); if (touchHandler.GetNewTouchInfo()) { touchHandler.UpdateLvglTouchPoint(); } @@ -331,7 +330,6 @@ void SystemTask::Work() { displayApp.PushMessage(Pinetime::Applications::Display::Messages::TouchEvent); break; case Messages::OnButtonEvent: - console.Print("Button event\r\n"); ReloadIdleTimer(); displayApp.PushMessage(Pinetime::Applications::Display::Messages::ButtonPushed); break; @@ -488,7 +486,6 @@ void SystemTask::OnIdle() { if (doNotGoToSleep) return; NRF_LOG_INFO("Idle timeout -> Going to sleep") - console.Print("Idle timeout -> Going to sleep\r\n"); PushMessage(Messages::GoToSleep); } From 16736d5924649511b1e6c97e11103af112d98eb2 Mon Sep 17 00:00:00 2001 From: hubmartin Date: Fri, 22 Oct 2021 22:48:03 +0200 Subject: [PATCH 24/25] clang-format --- src/components/ble/BleNus.cpp | 54 +++++---- src/components/ble/BleNus.h | 19 ++-- src/components/console/Console.cpp | 169 +++++++++++++---------------- src/components/console/Console.h | 72 ++++++------ 4 files changed, 147 insertions(+), 167 deletions(-) diff --git a/src/components/ble/BleNus.cpp b/src/components/ble/BleNus.cpp index 656207b18a..6237820524 100644 --- a/src/components/ble/BleNus.cpp +++ b/src/components/ble/BleNus.cpp @@ -23,45 +23,43 @@ void BleNus::Init() { } void BleNus::SetConnectionHandle(uint16_t connection_handle) { - connectionHandle = connection_handle; + connectionHandle = connection_handle; } -void BleNus::Print(const std::string str) -{ - os_mbuf *om; +void BleNus::Print(const std::string str) { + os_mbuf* om; om = ble_hs_mbuf_from_flat(str.c_str(), str.length()); if (om) { - ble_gattc_notify_custom(connectionHandle, attributeReadHandle, om); + ble_gattc_notify_custom(connectionHandle, attributeReadHandle, om); } } -void BleNus::RegisterRxCallback(std::function f) -{ +void BleNus::RegisterRxCallback(std::function f) { this->rxDataFunction = f; } int BleNus::OnDeviceInfoRequested(uint16_t connectionHandle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt) { - os_mbuf *om = ctxt->om; + os_mbuf* om = ctxt->om; - switch (ctxt->op) { - case BLE_GATT_ACCESS_OP_WRITE_CHR: - while(om) { + switch (ctxt->op) { + case BLE_GATT_ACCESS_OP_WRITE_CHR: + while (om) { - // Test BLE console it with Bluefruit, NRF Toolbox (you must add enter before hitting send! https://devzone.nordicsemi.com/f/nordic-q-a/33687/nrf-toolbox-2-6-0-uart-does-not-send-lf-cr-or-cr-lf-as-eol) - // on the phone, or in any Chromium-based web browser - // https://terminal.hardwario.com/ + // Test BLE console it with Bluefruit, NRF Toolbox (you must add enter before hitting send! + // https://devzone.nordicsemi.com/f/nordic-q-a/33687/nrf-toolbox-2-6-0-uart-does-not-send-lf-cr-or-cr-lf-as-eol) on the phone, or in + // any Chromium-based web browser https://terminal.hardwario.com/ - rxDataFunction((char *)om->om_data, (int)om->om_len); + rxDataFunction((char*) om->om_data, (int) om->om_len); - om = SLIST_NEXT(om, om_next); - } - return 0; - default: - assert(0); - return BLE_ATT_ERR_UNLIKELY; - } + om = SLIST_NEXT(om, om_next); + } + return 0; + default: + assert(0); + return BLE_ATT_ERR_UNLIKELY; + } } BleNus::BleNus() @@ -71,13 +69,11 @@ BleNus::BleNus() .arg = this, .flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_WRITE_NO_RSP, }, - { - .uuid = &txCharacteristicUuid.u, - .access_cb = BleNusCallback, - .arg = this, - .flags = BLE_GATT_CHR_F_NOTIFY, - .val_handle = &attributeReadHandle - }, + {.uuid = &txCharacteristicUuid.u, + .access_cb = BleNusCallback, + .arg = this, + .flags = BLE_GATT_CHR_F_NOTIFY, + .val_handle = &attributeReadHandle}, {0}}, serviceDefinition { {/* Device Information Service */ diff --git a/src/components/ble/BleNus.h b/src/components/ble/BleNus.h index 8455037494..6300d2a0e5 100644 --- a/src/components/ble/BleNus.h +++ b/src/components/ble/BleNus.h @@ -24,18 +24,23 @@ namespace Pinetime { int OnDeviceInfoRequested(uint16_t connectionHandle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt); private: - static uint16_t attributeReadHandle; uint16_t connectionHandle; - + std::function rxDataFunction; // 6E400001-B5A3-F393-E0A9-E50E24DCCA9E - static constexpr ble_uuid128_t nusServiceUuid {.u {.type = BLE_UUID_TYPE_128}, .value = { 0x9e, 0xca, 0xdc, 0x24, 0x0e, 0xe5, 0xa9, 0xe0, 0x93, 0xf3, 0xa3, 0xb5, 0x01, 0x00, 0x40, 0x6e }}; - //6E400002-B5A3-F393-E0A9-E50E24DCCA9E - static constexpr ble_uuid128_t rxCharacteristicUuid {.u {.type = BLE_UUID_TYPE_128}, .value = { 0x9e, 0xca, 0xdc, 0x24, 0x0e, 0xe5, 0xa9, 0xe0, 0x93, 0xf3, 0xa3, 0xb5, 0x02, 0x00, 0x40, 0x6e }}; - //6E400003-B5A3-F393-E0A9-E50E24DCCA9E - static constexpr ble_uuid128_t txCharacteristicUuid {.u {.type = BLE_UUID_TYPE_128}, .value = { 0x9e, 0xca, 0xdc, 0x24, 0x0e, 0xe5, 0xa9, 0xe0, 0x93, 0xf3, 0xa3, 0xb5, 0x03, 0x00, 0x40, 0x6e }}; + static constexpr ble_uuid128_t nusServiceUuid { + .u {.type = BLE_UUID_TYPE_128}, + .value = {0x9e, 0xca, 0xdc, 0x24, 0x0e, 0xe5, 0xa9, 0xe0, 0x93, 0xf3, 0xa3, 0xb5, 0x01, 0x00, 0x40, 0x6e}}; + // 6E400002-B5A3-F393-E0A9-E50E24DCCA9E + static constexpr ble_uuid128_t rxCharacteristicUuid { + .u {.type = BLE_UUID_TYPE_128}, + .value = {0x9e, 0xca, 0xdc, 0x24, 0x0e, 0xe5, 0xa9, 0xe0, 0x93, 0xf3, 0xa3, 0xb5, 0x02, 0x00, 0x40, 0x6e}}; + // 6E400003-B5A3-F393-E0A9-E50E24DCCA9E + static constexpr ble_uuid128_t txCharacteristicUuid { + .u {.type = BLE_UUID_TYPE_128}, + .value = {0x9e, 0xca, 0xdc, 0x24, 0x0e, 0xe5, 0xa9, 0xe0, 0x93, 0xf3, 0xa3, 0xb5, 0x03, 0x00, 0x40, 0x6e}}; struct ble_gatt_chr_def characteristicDefinition[3]; struct ble_gatt_svc_def serviceDefinition[2]; diff --git a/src/components/console/Console.cpp b/src/components/console/Console.cpp index ada8fe84b5..11875f36de 100644 --- a/src/components/console/Console.cpp +++ b/src/components/console/Console.cpp @@ -6,113 +6,96 @@ using namespace Pinetime::Components; Console::Console(Pinetime::System::SystemTask& systemTask, - Pinetime::Controllers::NimbleController& nimbleController, - Pinetime::Controllers::FS& fs, - Pinetime::Components::LittleVgl& lvgl, - Pinetime::Controllers::MotorController& motorController, - Pinetime::Drivers::Cst816S& touchPanel, - Pinetime::Drivers::SpiNorFlash& spiNorFlash, - Pinetime::Drivers::TwiMaster& twiMaster, - Pinetime::Controllers::MotionController& motionController): - systemTask {systemTask}, - nimbleController{nimbleController}, - fs{fs}, - lvgl{lvgl}, - motorController{motorController}, - touchPanel{touchPanel}, - spiNorFlash{spiNorFlash}, - twiMaster{twiMaster}, - motionController{motionController} -{ + Pinetime::Controllers::NimbleController& nimbleController, + Pinetime::Controllers::FS& fs, + Pinetime::Components::LittleVgl& lvgl, + Pinetime::Controllers::MotorController& motorController, + Pinetime::Drivers::Cst816S& touchPanel, + Pinetime::Drivers::SpiNorFlash& spiNorFlash, + Pinetime::Drivers::TwiMaster& twiMaster, + Pinetime::Controllers::MotionController& motionController) + : systemTask {systemTask}, + nimbleController {nimbleController}, + fs {fs}, + lvgl {lvgl}, + motorController {motorController}, + touchPanel {touchPanel}, + spiNorFlash {spiNorFlash}, + twiMaster {twiMaster}, + motionController {motionController} { } -void Console::Init() -{ - auto rxCallback = [this](char *str, int length) { - this->Received(str, length); - }; +void Console::Init() { + auto rxCallback = [this](char* str, int length) { + this->Received(str, length); + }; - nimbleController.bleNus().RegisterRxCallback(rxCallback); + nimbleController.bleNus().RegisterRxCallback(rxCallback); } -void Console::Print(const std::string str) -{ - nimbleController.bleNus().Print(str); +void Console::Print(const std::string str) { + nimbleController.bleNus().Print(str); } -static bool cmdCmp(char *buffer, const std::string search) -{ - return strncmp(buffer, search.c_str(), search.length()) == 0; +static bool cmdCmp(char* buffer, const std::string search) { + return strncmp(buffer, search.c_str(), search.length()) == 0; } -void Console::Process() -{ - static constexpr int maxArgumentsCount = 4; - static constexpr int maxBufferLength = 64; - - char arg_buffer[maxBufferLength]; - const char *args[maxArgumentsCount]; - - // Copy string, becase we replace ' ' with '\0' for proper string termination - strncpy(arg_buffer, rxBuffer, sizeof(arg_buffer)); - - // First argument is always command name itself - uint16_t argc = 1; - args[0] = arg_buffer; - - uint16_t param_len = strlen(rxBuffer); - - for (uint8_t i = 0; i < param_len; i++) - { - if (rxBuffer[i] == ' ' && param_len > (i + 1)) - { - arg_buffer[i] = '\0'; - args[argc++] = &arg_buffer[i+1]; - } - - if (argc == maxArgumentsCount) - { - // Max argument count reached - break; - } - } +void Console::Process() { + static constexpr int maxArgumentsCount = 4; + static constexpr int maxBufferLength = 64; - (void) args; - (void) argc; + char arg_buffer[maxBufferLength]; + const char* args[maxArgumentsCount]; - // Simple stupid command comparison, later would be nice to add commands lookup table with argument parsing - if(cmdCmp(rxBuffer, "VIBRATE")) - { - motorController.RunForDuration(100); - } - else if(cmdCmp(rxBuffer, "WKUP")) - { - systemTask.PushMessage(Pinetime::System::Messages::GoToRunning); + // Copy string, becase we replace ' ' with '\0' for proper string termination + strncpy(arg_buffer, rxBuffer, sizeof(arg_buffer)); + + // First argument is always command name itself + uint16_t argc = 1; + args[0] = arg_buffer; + + uint16_t param_len = strlen(rxBuffer); + + for (uint8_t i = 0; i < param_len; i++) { + if (rxBuffer[i] == ' ' && param_len > (i + 1)) { + arg_buffer[i] = '\0'; + args[argc++] = &arg_buffer[i + 1]; } - else if(cmdCmp(rxBuffer, "SLEEP")) - { - systemTask.PushMessage(Pinetime::System::Messages::GoToSleep); + + if (argc == maxArgumentsCount) { + // Max argument count reached + break; } + } + + (void) args; + (void) argc; + + // Simple stupid command comparison, later would be nice to add commands lookup table with argument parsing + if (cmdCmp(rxBuffer, "VIBRATE")) { + motorController.RunForDuration(100); + } else if (cmdCmp(rxBuffer, "WKUP")) { + systemTask.PushMessage(Pinetime::System::Messages::GoToRunning); + } else if (cmdCmp(rxBuffer, "SLEEP")) { + systemTask.PushMessage(Pinetime::System::Messages::GoToSleep); + } } -void Console::Received(char* str, int length) -{ - for(int i = 0; i < length; i++) - { - // Wrap if input is too long without CR/LN - if(rxPos == bufferSize - 1) - { - rxPos = 0; - } - - rxBuffer[rxPos++] = str[i]; - rxBuffer[rxPos] = '\0'; // terminate for debug print - - if(str[i] == '\n' || str[i] == '\r') - { - rxPos = 0; - systemTask.PushMessage(System::Messages::ConsoleProcess); - break; - } +void Console::Received(char* str, int length) { + for (int i = 0; i < length; i++) { + // Wrap if input is too long without CR/LN + if (rxPos == bufferSize - 1) { + rxPos = 0; + } + + rxBuffer[rxPos++] = str[i]; + rxBuffer[rxPos] = '\0'; // terminate for debug print + + if (str[i] == '\n' || str[i] == '\r') { + rxPos = 0; + systemTask.PushMessage(System::Messages::ConsoleProcess); + break; } + } } \ No newline at end of file diff --git a/src/components/console/Console.h b/src/components/console/Console.h index febaa0c255..138f497cc3 100644 --- a/src/components/console/Console.h +++ b/src/components/console/Console.h @@ -10,60 +10,56 @@ namespace Pinetime { class SystemTask; } - namespace Controllers - { + namespace Controllers { class NimbleController; class FS; class MotorController; class MotionController; } - namespace Drivers - { + namespace Drivers { class Cst816S; class SpiNorFlash; class TwiMaster; } - - namespace Components - { + + namespace Components { class LittleVgl; class Console { - public: - Console(Pinetime::System::SystemTask& systemTask, - Pinetime::Controllers::NimbleController& nimbleController, - Pinetime::Controllers::FS& fs, - Pinetime::Components::LittleVgl& lvgl, - Pinetime::Controllers::MotorController& motorController, - Pinetime::Drivers::Cst816S& touchPanel, - Pinetime::Drivers::SpiNorFlash& spiNorFlash, - Pinetime::Drivers::TwiMaster& twiMaster, - Pinetime::Controllers::MotionController& motionController); - - void Init(); - void Process(); - void Print(std::string str); - void Received(char* str, int length); + public: + Console(Pinetime::System::SystemTask& systemTask, + Pinetime::Controllers::NimbleController& nimbleController, + Pinetime::Controllers::FS& fs, + Pinetime::Components::LittleVgl& lvgl, + Pinetime::Controllers::MotorController& motorController, + Pinetime::Drivers::Cst816S& touchPanel, + Pinetime::Drivers::SpiNorFlash& spiNorFlash, + Pinetime::Drivers::TwiMaster& twiMaster, + Pinetime::Controllers::MotionController& motionController); - private: - Pinetime::System::SystemTask& systemTask; - Pinetime::Controllers::NimbleController& nimbleController; - Pinetime::Controllers::FS& fs; - Pinetime::Components::LittleVgl& lvgl; - Pinetime::Controllers::MotorController& motorController; - Pinetime::Drivers::Cst816S& touchPanel; - Pinetime::Drivers::SpiNorFlash& spiNorFlash; - Pinetime::Drivers::TwiMaster& twiMaster; - Pinetime::Controllers::MotionController& motionController; + void Init(); + void Process(); + void Print(std::string str); + void Received(char* str, int length); - static constexpr int bufferSize = 32; - char rxBuffer[bufferSize]; - uint16_t rxPos; + private: + Pinetime::System::SystemTask& systemTask; + Pinetime::Controllers::NimbleController& nimbleController; + Pinetime::Controllers::FS& fs; + Pinetime::Components::LittleVgl& lvgl; + Pinetime::Controllers::MotorController& motorController; + Pinetime::Drivers::Cst816S& touchPanel; + Pinetime::Drivers::SpiNorFlash& spiNorFlash; + Pinetime::Drivers::TwiMaster& twiMaster; + Pinetime::Controllers::MotionController& motionController; - void CommandLvgl(const char *args[], uint16_t argc); - void AccelerometerDebug(); + static constexpr int bufferSize = 32; + char rxBuffer[bufferSize]; + uint16_t rxPos; - }; + void CommandLvgl(const char* args[], uint16_t argc); + void AccelerometerDebug(); + }; } } From 94215c75e6d45df0a2b448ecfc99740b18be6e25 Mon Sep 17 00:00:00 2001 From: hubmartin Date: Thu, 28 Oct 2021 18:55:09 +0200 Subject: [PATCH 25/25] Run clang-tidy --- src/components/ble/BleNus.cpp | 4 +++- src/components/ble/BleNus.h | 2 +- src/components/console/Console.cpp | 6 +++--- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/components/ble/BleNus.cpp b/src/components/ble/BleNus.cpp index 6237820524..e091a526df 100644 --- a/src/components/ble/BleNus.cpp +++ b/src/components/ble/BleNus.cpp @@ -9,7 +9,7 @@ constexpr ble_uuid128_t BleNus::txCharacteristicUuid; uint16_t BleNus::attributeReadHandle; int BleNusCallback(uint16_t connectionHandle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg) { - auto deviceInformationService = static_cast(arg); + auto *deviceInformationService = static_cast(arg); return deviceInformationService->OnDeviceInfoRequested(connectionHandle, attr_handle, ctxt); } @@ -40,6 +40,8 @@ void BleNus::RegisterRxCallback(std::function f) { } int BleNus::OnDeviceInfoRequested(uint16_t connectionHandle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt) { + (void) connectionHandle; + (void) attr_handle; os_mbuf* om = ctxt->om; diff --git a/src/components/ble/BleNus.h b/src/components/ble/BleNus.h index 6300d2a0e5..e18c0acb47 100644 --- a/src/components/ble/BleNus.h +++ b/src/components/ble/BleNus.h @@ -16,7 +16,7 @@ namespace Pinetime { public: BleNus(); void Init(); - void SetConnectionHandle(uint16_t connectionHandle); + void SetConnectionHandle(uint16_t connection_handle); void Print(const std::string str); void RegisterRxCallback(std::function f); diff --git a/src/components/console/Console.cpp b/src/components/console/Console.cpp index 11875f36de..10b297c547 100644 --- a/src/components/console/Console.cpp +++ b/src/components/console/Console.cpp @@ -1,7 +1,7 @@ #include "Console.h" -#include "systemtask/SystemTask.h" -#include "components/ble/NimbleController.h" #include "components/ble/BleNus.h" +#include "components/ble/NimbleController.h" +#include "systemtask/SystemTask.h" using namespace Pinetime::Components; @@ -57,7 +57,7 @@ void Console::Process() { uint16_t param_len = strlen(rxBuffer); - for (uint8_t i = 0; i < param_len; i++) { + for (uint16_t i = 0; i < param_len; i++) { if (rxBuffer[i] == ' ' && param_len > (i + 1)) { arg_buffer[i] = '\0'; args[argc++] = &arg_buffer[i + 1];