From 07fc4444bba1a5fcde2c10ebf3903dd7a8a9f268 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1ndor=20R=C3=A1tkai?= Date: Sun, 8 Sep 2024 01:41:41 +0200 Subject: [PATCH 01/16] Introduce an alias for the step counter's type --- src/components/motion/MotionController.cpp | 2 +- src/components/motion/MotionController.h | 12 +++++++----- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/components/motion/MotionController.cpp b/src/components/motion/MotionController.cpp index 72507ac5cd..62801ce7c9 100644 --- a/src/components/motion/MotionController.cpp +++ b/src/components/motion/MotionController.cpp @@ -35,7 +35,7 @@ namespace { } } -void MotionController::Update(int16_t x, int16_t y, int16_t z, uint32_t nbSteps) { +void MotionController::Update(int16_t x, int16_t y, int16_t z, MotionController::step_t nbSteps) { if (this->nbSteps != nbSteps && service != nullptr) { service->OnNewStepCountValue(nbSteps); } diff --git a/src/components/motion/MotionController.h b/src/components/motion/MotionController.h index be0241d32e..181c595b7d 100644 --- a/src/components/motion/MotionController.h +++ b/src/components/motion/MotionController.h @@ -18,7 +18,9 @@ namespace Pinetime { BMA425, }; - void Update(int16_t x, int16_t y, int16_t z, uint32_t nbSteps); + using step_t = uint32_t; + + void Update(int16_t x, int16_t y, int16_t z, step_t nbSteps); int16_t X() const { return xHistory[0]; @@ -32,7 +34,7 @@ namespace Pinetime { return zHistory[0]; } - uint32_t NbSteps() const { + step_t NbSteps() const { return nbSteps; } @@ -40,7 +42,7 @@ namespace Pinetime { currentTripSteps = 0; } - uint32_t GetTripSteps() const { + step_t GetTripSteps() const { return currentTripSteps; } @@ -67,8 +69,8 @@ namespace Pinetime { } private: - uint32_t nbSteps = 0; - uint32_t currentTripSteps = 0; + step_t nbSteps = 0; + step_t currentTripSteps = 0; TickType_t lastTime = 0; TickType_t time = 0; From a1a45df2fc3351309877dc22e2190b762907c75d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1ndor=20R=C3=A1tkai?= Date: Sun, 8 Sep 2024 20:51:29 +0200 Subject: [PATCH 02/16] Inverted the circular dependency of MotionService and MotionController --- src/components/ble/MotionService.cpp | 10 +++++----- src/components/ble/MotionService.h | 5 +++-- src/components/motion/MotionController.cpp | 1 + src/components/motion/MotionController.h | 3 ++- 4 files changed, 11 insertions(+), 8 deletions(-) diff --git a/src/components/ble/MotionService.cpp b/src/components/ble/MotionService.cpp index 1626a5bf4d..018382a778 100644 --- a/src/components/ble/MotionService.cpp +++ b/src/components/ble/MotionService.cpp @@ -62,9 +62,9 @@ void MotionService::Init() { int MotionService::OnStepCountRequested(uint16_t attributeHandle, ble_gatt_access_ctxt* context) { if (attributeHandle == stepCountHandle) { NRF_LOG_INFO("Motion-stepcount : handle = %d", stepCountHandle); - uint32_t buffer = motionController.NbSteps(); + MotionController::step_t buffer = motionController.NbSteps(); - int res = os_mbuf_append(context->om, &buffer, 4); + int res = os_mbuf_append(context->om, &buffer, sizeof(buffer)); return (res == 0) ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES; } else if (attributeHandle == motionValuesHandle) { int16_t buffer[3] = {motionController.X(), motionController.Y(), motionController.Z()}; @@ -75,12 +75,12 @@ int MotionService::OnStepCountRequested(uint16_t attributeHandle, ble_gatt_acces return 0; } -void MotionService::OnNewStepCountValue(uint32_t stepCount) { +void MotionService::OnNewStepCountValue(MotionController::step_t stepCount) { if (!stepCountNoficationEnabled) return; - uint32_t buffer = stepCount; - auto* om = ble_hs_mbuf_from_flat(&buffer, 4); + MotionController::step_t buffer = stepCount; + auto* om = ble_hs_mbuf_from_flat(&buffer, sizeof(buffer)); uint16_t connectionHandle = nimble.connHandle(); diff --git a/src/components/ble/MotionService.h b/src/components/ble/MotionService.h index acc91e8d59..beeec479d5 100644 --- a/src/components/ble/MotionService.h +++ b/src/components/ble/MotionService.h @@ -6,17 +6,18 @@ #undef max #undef min +#include "components/motion/MotionController.h" + namespace Pinetime { namespace Controllers { class NimbleController; - class MotionController; class MotionService { public: MotionService(NimbleController& nimble, Controllers::MotionController& motionController); void Init(); int OnStepCountRequested(uint16_t attributeHandle, ble_gatt_access_ctxt* context); - void OnNewStepCountValue(uint32_t stepCount); + void OnNewStepCountValue(MotionController::step_t stepCount); void OnNewMotionValues(int16_t x, int16_t y, int16_t z); void SubscribeNotification(uint16_t attributeHandle); diff --git a/src/components/motion/MotionController.cpp b/src/components/motion/MotionController.cpp index 62801ce7c9..82fde12265 100644 --- a/src/components/motion/MotionController.cpp +++ b/src/components/motion/MotionController.cpp @@ -2,6 +2,7 @@ #include +#include "components/ble/MotionService.h" #include "utility/Math.h" using namespace Pinetime::Controllers; diff --git a/src/components/motion/MotionController.h b/src/components/motion/MotionController.h index 181c595b7d..d47e1043d8 100644 --- a/src/components/motion/MotionController.h +++ b/src/components/motion/MotionController.h @@ -5,11 +5,12 @@ #include #include "drivers/Bma421.h" -#include "components/ble/MotionService.h" #include "utility/CircularBuffer.h" namespace Pinetime { namespace Controllers { + class MotionService; + class MotionController { public: enum class DeviceTypes { From cd1815b267e71200276142db3818a570ef02b9ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1ndor=20R=C3=A1tkai?= Date: Sun, 8 Sep 2024 22:13:08 +0200 Subject: [PATCH 03/16] Converted MotionController::nbSteps to CircularBuffer internally --- src/components/motion/MotionController.cpp | 6 +++--- src/components/motion/MotionController.h | 5 +++-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/components/motion/MotionController.cpp b/src/components/motion/MotionController.cpp index 82fde12265..22b2fa67e0 100644 --- a/src/components/motion/MotionController.cpp +++ b/src/components/motion/MotionController.cpp @@ -37,7 +37,7 @@ namespace { } void MotionController::Update(int16_t x, int16_t y, int16_t z, MotionController::step_t nbSteps) { - if (this->nbSteps != nbSteps && service != nullptr) { + if (this->nbSteps[0] != nbSteps && service != nullptr) { service->OnNewStepCountValue(nbSteps); } @@ -57,11 +57,11 @@ void MotionController::Update(int16_t x, int16_t y, int16_t z, MotionController: stats = GetAccelStats(); - int32_t deltaSteps = nbSteps - this->nbSteps; + int32_t deltaSteps = nbSteps - this->nbSteps[0]; if (deltaSteps > 0) { currentTripSteps += deltaSteps; } - this->nbSteps = nbSteps; + this->nbSteps[0] = nbSteps; } MotionController::AccelStats MotionController::GetAccelStats() const { diff --git a/src/components/motion/MotionController.h b/src/components/motion/MotionController.h index d47e1043d8..274645975c 100644 --- a/src/components/motion/MotionController.h +++ b/src/components/motion/MotionController.h @@ -20,6 +20,7 @@ namespace Pinetime { }; using step_t = uint32_t; + static constexpr size_t stepHistorySize = 2; // Store this many day's step counter void Update(int16_t x, int16_t y, int16_t z, step_t nbSteps); @@ -36,7 +37,7 @@ namespace Pinetime { } step_t NbSteps() const { - return nbSteps; + return nbSteps[0]; } void ResetTrip() { @@ -70,7 +71,7 @@ namespace Pinetime { } private: - step_t nbSteps = 0; + Utility::CircularBuffer nbSteps = {0}; step_t currentTripSteps = 0; TickType_t lastTime = 0; From 71cef5175060404da2dde9a0f0e169ecf841517d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1ndor=20R=C3=A1tkai?= Date: Mon, 9 Sep 2024 00:06:19 +0200 Subject: [PATCH 04/16] Added an enum for the index of today and yesterday --- src/components/motion/MotionController.cpp | 6 +++--- src/components/motion/MotionController.h | 9 +++++++-- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/src/components/motion/MotionController.cpp b/src/components/motion/MotionController.cpp index 22b2fa67e0..367a388430 100644 --- a/src/components/motion/MotionController.cpp +++ b/src/components/motion/MotionController.cpp @@ -37,7 +37,7 @@ namespace { } void MotionController::Update(int16_t x, int16_t y, int16_t z, MotionController::step_t nbSteps) { - if (this->nbSteps[0] != nbSteps && service != nullptr) { + if (this->nbSteps[today] != nbSteps && service != nullptr) { service->OnNewStepCountValue(nbSteps); } @@ -57,11 +57,11 @@ void MotionController::Update(int16_t x, int16_t y, int16_t z, MotionController: stats = GetAccelStats(); - int32_t deltaSteps = nbSteps - this->nbSteps[0]; + int32_t deltaSteps = nbSteps - this->nbSteps[today]; if (deltaSteps > 0) { currentTripSteps += deltaSteps; } - this->nbSteps[0] = nbSteps; + this->nbSteps[today] = nbSteps; } MotionController::AccelStats MotionController::GetAccelStats() const { diff --git a/src/components/motion/MotionController.h b/src/components/motion/MotionController.h index 274645975c..274ab87bda 100644 --- a/src/components/motion/MotionController.h +++ b/src/components/motion/MotionController.h @@ -10,7 +10,7 @@ namespace Pinetime { namespace Controllers { class MotionService; - + class MotionController { public: enum class DeviceTypes { @@ -19,6 +19,11 @@ namespace Pinetime { BMA425, }; + enum Days { + today = 0, + yesterday = 1, + }; + using step_t = uint32_t; static constexpr size_t stepHistorySize = 2; // Store this many day's step counter @@ -37,7 +42,7 @@ namespace Pinetime { } step_t NbSteps() const { - return nbSteps[0]; + return nbSteps[today]; } void ResetTrip() { From f07a16fdf6c5424d5b7d633ee8334501d7b75af3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1ndor=20R=C3=A1tkai?= Date: Mon, 9 Sep 2024 00:08:04 +0200 Subject: [PATCH 05/16] Display yesterday's step counter on the Steps screen --- src/components/motion/MotionController.h | 4 ++++ src/displayapp/screens/Steps.cpp | 26 +++++++++++++++--------- src/displayapp/screens/Steps.h | 5 ++++- 3 files changed, 24 insertions(+), 11 deletions(-) diff --git a/src/components/motion/MotionController.h b/src/components/motion/MotionController.h index 274ab87bda..619c52da89 100644 --- a/src/components/motion/MotionController.h +++ b/src/components/motion/MotionController.h @@ -45,6 +45,10 @@ namespace Pinetime { return nbSteps[today]; } + const Utility::CircularBuffer &StepCounterHistory() const { + return nbSteps; + } + void ResetTrip() { currentTripSteps = 0; } diff --git a/src/displayapp/screens/Steps.cpp b/src/displayapp/screens/Steps.cpp index c5faaf050a..0e865c0081 100644 --- a/src/displayapp/screens/Steps.cpp +++ b/src/displayapp/screens/Steps.cpp @@ -11,7 +11,7 @@ static void lap_event_handler(lv_obj_t* obj, lv_event_t event) { } Steps::Steps(Controllers::MotionController& motionController, Controllers::Settings& settingsController) - : motionController {motionController}, settingsController {settingsController} { + : motionController {motionController}, settingsController {settingsController}, yesterdayStr {"Yest: %5lu"}, stepsCount {motionController.StepCounterHistory()} { stepsArc = lv_arc_create(lv_scr_act(), nullptr); @@ -25,21 +25,26 @@ Steps::Steps(Controllers::MotionController& motionController, Controllers::Setti lv_arc_set_range(stepsArc, 0, 500); lv_obj_align(stepsArc, nullptr, LV_ALIGN_CENTER, 0, 0); - stepsCount = motionController.NbSteps(); - currentTripSteps = stepsCount - motionController.GetTripSteps(); + currentTripSteps = stepsCount[Controllers::MotionController::Days::today] - motionController.GetTripSteps(); - lv_arc_set_value(stepsArc, int16_t(500 * stepsCount / settingsController.GetStepsGoal())); + lv_arc_set_value(stepsArc, int16_t(500 * stepsCount[Controllers::MotionController::Days::today] / settingsController.GetStepsGoal())); lSteps = lv_label_create(lv_scr_act(), nullptr); lv_obj_set_style_local_text_color(lSteps, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_LIME); lv_obj_set_style_local_text_font(lSteps, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, &jetbrains_mono_42); - lv_label_set_text_fmt(lSteps, "%li", stepsCount); + lv_label_set_text_fmt(lSteps, "%lu", stepsCount[Controllers::MotionController::Days::today]); lv_obj_align(lSteps, nullptr, LV_ALIGN_CENTER, 0, -40); lv_obj_t* lstepsL = lv_label_create(lv_scr_act(), nullptr); lv_obj_set_style_local_text_color(lstepsL, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, Colors::lightGray); lv_label_set_text_static(lstepsL, "Steps"); - lv_obj_align(lstepsL, lSteps, LV_ALIGN_OUT_BOTTOM_MID, 0, 5); + lv_obj_align(lstepsL, lSteps, LV_ALIGN_OUT_BOTTOM_MID, 0, 0); + + lStepsYesterday = lv_label_create(lv_scr_act(), nullptr); + lv_obj_set_style_local_text_color(lStepsYesterday, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, Colors::lightGray); + lv_label_set_text_fmt(lStepsYesterday, yesterdayStr, stepsCount[Controllers::MotionController::Days::yesterday]); + lv_label_set_align(lStepsYesterday, LV_LABEL_ALIGN_CENTER); + lv_obj_align(lStepsYesterday, lSteps, LV_ALIGN_OUT_BOTTOM_MID, 0, 20); lv_obj_t* lstepsGoal = lv_label_create(lv_scr_act(), nullptr); lv_obj_set_style_local_text_color(lstepsGoal, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_CYAN); @@ -73,10 +78,12 @@ Steps::~Steps() { } void Steps::Refresh() { - stepsCount = motionController.NbSteps(); currentTripSteps = motionController.GetTripSteps(); - lv_label_set_text_fmt(lSteps, "%li", stepsCount); + lv_label_set_text_fmt(lSteps, "%lu", stepsCount[Controllers::MotionController::Days::today]); + lv_obj_align(lSteps, nullptr, LV_ALIGN_CENTER, 0, -40); + + lv_label_set_text_fmt(lStepsYesterday, yesterdayStr, stepsCount[Controllers::MotionController::Days::yesterday]); lv_obj_align(lSteps, nullptr, LV_ALIGN_CENTER, 0, -40); if (currentTripSteps < 100000) { @@ -84,14 +91,13 @@ void Steps::Refresh() { } else { lv_label_set_text_fmt(tripLabel, "Trip: 99999+"); } - lv_arc_set_value(stepsArc, int16_t(500 * stepsCount / settingsController.GetStepsGoal())); + lv_arc_set_value(stepsArc, int16_t(500 * stepsCount[Controllers::MotionController::Days::today] / settingsController.GetStepsGoal())); } void Steps::lapBtnEventHandler(lv_event_t event) { if (event != LV_EVENT_CLICKED) { return; } - stepsCount = motionController.NbSteps(); motionController.ResetTrip(); Refresh(); } diff --git a/src/displayapp/screens/Steps.h b/src/displayapp/screens/Steps.h index 6443582f5e..589db6a499 100644 --- a/src/displayapp/screens/Steps.h +++ b/src/displayapp/screens/Steps.h @@ -32,12 +32,15 @@ namespace Pinetime { uint32_t currentTripSteps = 0; lv_obj_t* lSteps; + lv_obj_t* lStepsYesterday; lv_obj_t* stepsArc; lv_obj_t* resetBtn; lv_obj_t* resetButtonLabel; lv_obj_t* tripLabel; - uint32_t stepsCount; + const char *yesterdayStr; + + const Utility::CircularBuffer &stepsCount; lv_task_t* taskRefresh; }; From 35129e9549ca217a5d64e3a6c328d1a77257e8e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1ndor=20R=C3=A1tkai?= Date: Mon, 9 Sep 2024 00:11:22 +0200 Subject: [PATCH 06/16] At midnight, "shift" the step counter history --- src/components/motion/MotionController.cpp | 5 +++++ src/components/motion/MotionController.h | 2 ++ src/systemtask/SystemTask.cpp | 1 + 3 files changed, 8 insertions(+) diff --git a/src/components/motion/MotionController.cpp b/src/components/motion/MotionController.cpp index 367a388430..566b495adf 100644 --- a/src/components/motion/MotionController.cpp +++ b/src/components/motion/MotionController.cpp @@ -36,6 +36,11 @@ namespace { } } +void MotionController::AdvanceDay() { + --nbSteps; // Higher index = further in the past + nbSteps[today] = -1; // Ensure that the `this->nbSteps[today] != nbSteps` condition in `Update()` will be FALSE +} + void MotionController::Update(int16_t x, int16_t y, int16_t z, MotionController::step_t nbSteps) { if (this->nbSteps[today] != nbSteps && service != nullptr) { service->OnNewStepCountValue(nbSteps); diff --git a/src/components/motion/MotionController.h b/src/components/motion/MotionController.h index 619c52da89..00db09dc63 100644 --- a/src/components/motion/MotionController.h +++ b/src/components/motion/MotionController.h @@ -27,6 +27,8 @@ namespace Pinetime { using step_t = uint32_t; static constexpr size_t stepHistorySize = 2; // Store this many day's step counter + void AdvanceDay(); + void Update(int16_t x, int16_t y, int16_t z, step_t nbSteps); int16_t X() const { diff --git a/src/systemtask/SystemTask.cpp b/src/systemtask/SystemTask.cpp index eb013d6d1a..8b1c91b909 100644 --- a/src/systemtask/SystemTask.cpp +++ b/src/systemtask/SystemTask.cpp @@ -440,6 +440,7 @@ void SystemTask::UpdateMotion() { if (stepCounterMustBeReset) { motionSensor.ResetStepCounter(); + motionController.AdvanceDay(); stepCounterMustBeReset = false; } From 24eab6f0e35b0d5e9498bcdb6f74efd19d302160 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1ndor=20R=C3=A1tkai?= Date: Mon, 9 Sep 2024 00:38:02 +0200 Subject: [PATCH 07/16] Clang-format changes --- src/components/motion/MotionController.cpp | 2 +- src/components/motion/MotionController.h | 2 +- src/displayapp/screens/Steps.cpp | 5 ++++- src/displayapp/screens/Steps.h | 4 ++-- 4 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/components/motion/MotionController.cpp b/src/components/motion/MotionController.cpp index 566b495adf..0a00abac8c 100644 --- a/src/components/motion/MotionController.cpp +++ b/src/components/motion/MotionController.cpp @@ -37,7 +37,7 @@ namespace { } void MotionController::AdvanceDay() { - --nbSteps; // Higher index = further in the past + --nbSteps; // Higher index = further in the past nbSteps[today] = -1; // Ensure that the `this->nbSteps[today] != nbSteps` condition in `Update()` will be FALSE } diff --git a/src/components/motion/MotionController.h b/src/components/motion/MotionController.h index 00db09dc63..8fada3fd3b 100644 --- a/src/components/motion/MotionController.h +++ b/src/components/motion/MotionController.h @@ -47,7 +47,7 @@ namespace Pinetime { return nbSteps[today]; } - const Utility::CircularBuffer &StepCounterHistory() const { + const Utility::CircularBuffer& StepCounterHistory() const { return nbSteps; } diff --git a/src/displayapp/screens/Steps.cpp b/src/displayapp/screens/Steps.cpp index 0e865c0081..5ad54c79f6 100644 --- a/src/displayapp/screens/Steps.cpp +++ b/src/displayapp/screens/Steps.cpp @@ -11,7 +11,10 @@ static void lap_event_handler(lv_obj_t* obj, lv_event_t event) { } Steps::Steps(Controllers::MotionController& motionController, Controllers::Settings& settingsController) - : motionController {motionController}, settingsController {settingsController}, yesterdayStr {"Yest: %5lu"}, stepsCount {motionController.StepCounterHistory()} { + : motionController {motionController}, + settingsController {settingsController}, + yesterdayStr {"Yest: %5lu"}, + stepsCount {motionController.StepCounterHistory()} { stepsArc = lv_arc_create(lv_scr_act(), nullptr); diff --git a/src/displayapp/screens/Steps.h b/src/displayapp/screens/Steps.h index 589db6a499..7bcdbc59db 100644 --- a/src/displayapp/screens/Steps.h +++ b/src/displayapp/screens/Steps.h @@ -38,9 +38,9 @@ namespace Pinetime { lv_obj_t* resetButtonLabel; lv_obj_t* tripLabel; - const char *yesterdayStr; + const char* yesterdayStr; - const Utility::CircularBuffer &stepsCount; + const Utility::CircularBuffer& stepsCount; lv_task_t* taskRefresh; }; From cc8a381e2eae84a032bc790288a9f1e8965f4fa2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1ndor=20R=C3=A1tkai?= Date: Wed, 11 Sep 2024 08:35:28 +0200 Subject: [PATCH 08/16] Do not set unsigned step count to -1 --- src/components/motion/MotionController.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/components/motion/MotionController.cpp b/src/components/motion/MotionController.cpp index 0a00abac8c..abe70ca054 100644 --- a/src/components/motion/MotionController.cpp +++ b/src/components/motion/MotionController.cpp @@ -37,8 +37,11 @@ namespace { } void MotionController::AdvanceDay() { - --nbSteps; // Higher index = further in the past - nbSteps[today] = -1; // Ensure that the `this->nbSteps[today] != nbSteps` condition in `Update()` will be FALSE + --nbSteps; // Higher index = further in the past + nbSteps[today] = 0; + if (service != nullptr) { + service->OnNewStepCountValue(nbSteps[today]); + } } void MotionController::Update(int16_t x, int16_t y, int16_t z, MotionController::step_t nbSteps) { From a0b468d38faef8e4d97bbf32b844bd4d2e5e63fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1ndor=20R=C3=A1tkai?= Date: Wed, 11 Sep 2024 08:40:38 +0200 Subject: [PATCH 09/16] Capitalising Day enum's members "Today" and "Yesterday" --- src/components/motion/MotionController.cpp | 10 +++++----- src/components/motion/MotionController.h | 6 +++--- src/displayapp/screens/Steps.cpp | 14 +++++++------- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/components/motion/MotionController.cpp b/src/components/motion/MotionController.cpp index abe70ca054..3755808ecc 100644 --- a/src/components/motion/MotionController.cpp +++ b/src/components/motion/MotionController.cpp @@ -38,14 +38,14 @@ namespace { void MotionController::AdvanceDay() { --nbSteps; // Higher index = further in the past - nbSteps[today] = 0; + nbSteps[Today] = 0; if (service != nullptr) { - service->OnNewStepCountValue(nbSteps[today]); + service->OnNewStepCountValue(nbSteps[Today]); } } void MotionController::Update(int16_t x, int16_t y, int16_t z, MotionController::step_t nbSteps) { - if (this->nbSteps[today] != nbSteps && service != nullptr) { + if (this->nbSteps[Today] != nbSteps && service != nullptr) { service->OnNewStepCountValue(nbSteps); } @@ -65,11 +65,11 @@ void MotionController::Update(int16_t x, int16_t y, int16_t z, MotionController: stats = GetAccelStats(); - int32_t deltaSteps = nbSteps - this->nbSteps[today]; + int32_t deltaSteps = nbSteps - this->nbSteps[Today]; if (deltaSteps > 0) { currentTripSteps += deltaSteps; } - this->nbSteps[today] = nbSteps; + this->nbSteps[Today] = nbSteps; } MotionController::AccelStats MotionController::GetAccelStats() const { diff --git a/src/components/motion/MotionController.h b/src/components/motion/MotionController.h index 8fada3fd3b..10fc2a9bfc 100644 --- a/src/components/motion/MotionController.h +++ b/src/components/motion/MotionController.h @@ -20,8 +20,8 @@ namespace Pinetime { }; enum Days { - today = 0, - yesterday = 1, + Today = 0, + Yesterday = 1, }; using step_t = uint32_t; @@ -44,7 +44,7 @@ namespace Pinetime { } step_t NbSteps() const { - return nbSteps[today]; + return nbSteps[Today]; } const Utility::CircularBuffer& StepCounterHistory() const { diff --git a/src/displayapp/screens/Steps.cpp b/src/displayapp/screens/Steps.cpp index 5ad54c79f6..6130ceea25 100644 --- a/src/displayapp/screens/Steps.cpp +++ b/src/displayapp/screens/Steps.cpp @@ -28,14 +28,14 @@ Steps::Steps(Controllers::MotionController& motionController, Controllers::Setti lv_arc_set_range(stepsArc, 0, 500); lv_obj_align(stepsArc, nullptr, LV_ALIGN_CENTER, 0, 0); - currentTripSteps = stepsCount[Controllers::MotionController::Days::today] - motionController.GetTripSteps(); + currentTripSteps = stepsCount[Controllers::MotionController::Days::Today] - motionController.GetTripSteps(); - lv_arc_set_value(stepsArc, int16_t(500 * stepsCount[Controllers::MotionController::Days::today] / settingsController.GetStepsGoal())); + lv_arc_set_value(stepsArc, int16_t(500 * stepsCount[Controllers::MotionController::Days::Today] / settingsController.GetStepsGoal())); lSteps = lv_label_create(lv_scr_act(), nullptr); lv_obj_set_style_local_text_color(lSteps, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_LIME); lv_obj_set_style_local_text_font(lSteps, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, &jetbrains_mono_42); - lv_label_set_text_fmt(lSteps, "%lu", stepsCount[Controllers::MotionController::Days::today]); + lv_label_set_text_fmt(lSteps, "%lu", stepsCount[Controllers::MotionController::Days::Today]); lv_obj_align(lSteps, nullptr, LV_ALIGN_CENTER, 0, -40); lv_obj_t* lstepsL = lv_label_create(lv_scr_act(), nullptr); @@ -45,7 +45,7 @@ Steps::Steps(Controllers::MotionController& motionController, Controllers::Setti lStepsYesterday = lv_label_create(lv_scr_act(), nullptr); lv_obj_set_style_local_text_color(lStepsYesterday, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, Colors::lightGray); - lv_label_set_text_fmt(lStepsYesterday, yesterdayStr, stepsCount[Controllers::MotionController::Days::yesterday]); + lv_label_set_text_fmt(lStepsYesterday, yesterdayStr, stepsCount[Controllers::MotionController::Days::Yesterday]); lv_label_set_align(lStepsYesterday, LV_LABEL_ALIGN_CENTER); lv_obj_align(lStepsYesterday, lSteps, LV_ALIGN_OUT_BOTTOM_MID, 0, 20); @@ -83,10 +83,10 @@ Steps::~Steps() { void Steps::Refresh() { currentTripSteps = motionController.GetTripSteps(); - lv_label_set_text_fmt(lSteps, "%lu", stepsCount[Controllers::MotionController::Days::today]); + lv_label_set_text_fmt(lSteps, "%lu", stepsCount[Controllers::MotionController::Days::Today]); lv_obj_align(lSteps, nullptr, LV_ALIGN_CENTER, 0, -40); - lv_label_set_text_fmt(lStepsYesterday, yesterdayStr, stepsCount[Controllers::MotionController::Days::yesterday]); + lv_label_set_text_fmt(lStepsYesterday, yesterdayStr, stepsCount[Controllers::MotionController::Days::Yesterday]); lv_obj_align(lSteps, nullptr, LV_ALIGN_CENTER, 0, -40); if (currentTripSteps < 100000) { @@ -94,7 +94,7 @@ void Steps::Refresh() { } else { lv_label_set_text_fmt(tripLabel, "Trip: 99999+"); } - lv_arc_set_value(stepsArc, int16_t(500 * stepsCount[Controllers::MotionController::Days::today] / settingsController.GetStepsGoal())); + lv_arc_set_value(stepsArc, int16_t(500 * stepsCount[Controllers::MotionController::Days::Today] / settingsController.GetStepsGoal())); } void Steps::lapBtnEventHandler(lv_event_t event) { From 275c70e25d374e9f52a6a8336e67aff94d68a724 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1ndor=20R=C3=A1tkai?= Date: Wed, 11 Sep 2024 13:17:26 +0200 Subject: [PATCH 10/16] MotionController::NbSteps should take a Days enum as parameter Since most if not all calls expect today, I set it as default value --- src/components/motion/MotionController.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/components/motion/MotionController.h b/src/components/motion/MotionController.h index 10fc2a9bfc..1637363350 100644 --- a/src/components/motion/MotionController.h +++ b/src/components/motion/MotionController.h @@ -43,8 +43,8 @@ namespace Pinetime { return zHistory[0]; } - step_t NbSteps() const { - return nbSteps[Today]; + step_t NbSteps(Days day = Today) const { + return nbSteps[day]; } const Utility::CircularBuffer& StepCounterHistory() const { From 144c7d04a6e14d2a931dc8f942afa9b9741d3e63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1ndor=20R=C3=A1tkai?= Date: Thu, 12 Sep 2024 10:09:47 +0200 Subject: [PATCH 11/16] Removed step counter history getter --- src/components/motion/MotionController.h | 4 ---- src/displayapp/screens/Steps.cpp | 23 +++++++++++++---------- src/displayapp/screens/Steps.h | 4 ++-- 3 files changed, 15 insertions(+), 16 deletions(-) diff --git a/src/components/motion/MotionController.h b/src/components/motion/MotionController.h index 1637363350..b46e57d4cb 100644 --- a/src/components/motion/MotionController.h +++ b/src/components/motion/MotionController.h @@ -47,10 +47,6 @@ namespace Pinetime { return nbSteps[day]; } - const Utility::CircularBuffer& StepCounterHistory() const { - return nbSteps; - } - void ResetTrip() { currentTripSteps = 0; } diff --git a/src/displayapp/screens/Steps.cpp b/src/displayapp/screens/Steps.cpp index 6130ceea25..70cd3be2fc 100644 --- a/src/displayapp/screens/Steps.cpp +++ b/src/displayapp/screens/Steps.cpp @@ -5,6 +5,8 @@ using namespace Pinetime::Applications::Screens; +using Days = Pinetime::Controllers::MotionController::Days; + static void lap_event_handler(lv_obj_t* obj, lv_event_t event) { auto* steps = static_cast(obj->user_data); steps->lapBtnEventHandler(event); @@ -12,9 +14,7 @@ static void lap_event_handler(lv_obj_t* obj, lv_event_t event) { Steps::Steps(Controllers::MotionController& motionController, Controllers::Settings& settingsController) : motionController {motionController}, - settingsController {settingsController}, - yesterdayStr {"Yest: %5lu"}, - stepsCount {motionController.StepCounterHistory()} { + settingsController {settingsController} { stepsArc = lv_arc_create(lv_scr_act(), nullptr); @@ -28,14 +28,15 @@ Steps::Steps(Controllers::MotionController& motionController, Controllers::Setti lv_arc_set_range(stepsArc, 0, 500); lv_obj_align(stepsArc, nullptr, LV_ALIGN_CENTER, 0, 0); - currentTripSteps = stepsCount[Controllers::MotionController::Days::Today] - motionController.GetTripSteps(); + stepsCount = motionController.NbSteps(); + currentTripSteps = stepsCount - motionController.GetTripSteps(); - lv_arc_set_value(stepsArc, int16_t(500 * stepsCount[Controllers::MotionController::Days::Today] / settingsController.GetStepsGoal())); + lv_arc_set_value(stepsArc, int16_t(500 * stepsCount / settingsController.GetStepsGoal())); lSteps = lv_label_create(lv_scr_act(), nullptr); lv_obj_set_style_local_text_color(lSteps, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_LIME); lv_obj_set_style_local_text_font(lSteps, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, &jetbrains_mono_42); - lv_label_set_text_fmt(lSteps, "%lu", stepsCount[Controllers::MotionController::Days::Today]); + lv_label_set_text_fmt(lSteps, "%lu", stepsCount); lv_obj_align(lSteps, nullptr, LV_ALIGN_CENTER, 0, -40); lv_obj_t* lstepsL = lv_label_create(lv_scr_act(), nullptr); @@ -45,7 +46,7 @@ Steps::Steps(Controllers::MotionController& motionController, Controllers::Setti lStepsYesterday = lv_label_create(lv_scr_act(), nullptr); lv_obj_set_style_local_text_color(lStepsYesterday, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, Colors::lightGray); - lv_label_set_text_fmt(lStepsYesterday, yesterdayStr, stepsCount[Controllers::MotionController::Days::Yesterday]); + lv_label_set_text_fmt(lStepsYesterday, yesterdayStr, motionController.NbSteps(Days::Yesterday)); lv_label_set_align(lStepsYesterday, LV_LABEL_ALIGN_CENTER); lv_obj_align(lStepsYesterday, lSteps, LV_ALIGN_OUT_BOTTOM_MID, 0, 20); @@ -81,12 +82,13 @@ Steps::~Steps() { } void Steps::Refresh() { + stepsCount = motionController.NbSteps(); currentTripSteps = motionController.GetTripSteps(); - lv_label_set_text_fmt(lSteps, "%lu", stepsCount[Controllers::MotionController::Days::Today]); + lv_label_set_text_fmt(lSteps, "%lu", stepsCount); lv_obj_align(lSteps, nullptr, LV_ALIGN_CENTER, 0, -40); - lv_label_set_text_fmt(lStepsYesterday, yesterdayStr, stepsCount[Controllers::MotionController::Days::Yesterday]); + lv_label_set_text_fmt(lStepsYesterday, yesterdayStr, motionController.NbSteps(Days::Yesterday)); lv_obj_align(lSteps, nullptr, LV_ALIGN_CENTER, 0, -40); if (currentTripSteps < 100000) { @@ -94,13 +96,14 @@ void Steps::Refresh() { } else { lv_label_set_text_fmt(tripLabel, "Trip: 99999+"); } - lv_arc_set_value(stepsArc, int16_t(500 * stepsCount[Controllers::MotionController::Days::Today] / settingsController.GetStepsGoal())); + lv_arc_set_value(stepsArc, int16_t(500 * stepsCount / settingsController.GetStepsGoal())); } void Steps::lapBtnEventHandler(lv_event_t event) { if (event != LV_EVENT_CLICKED) { return; } + stepsCount = motionController.NbSteps(); motionController.ResetTrip(); Refresh(); } diff --git a/src/displayapp/screens/Steps.h b/src/displayapp/screens/Steps.h index 7bcdbc59db..301ae9b0cb 100644 --- a/src/displayapp/screens/Steps.h +++ b/src/displayapp/screens/Steps.h @@ -38,9 +38,9 @@ namespace Pinetime { lv_obj_t* resetButtonLabel; lv_obj_t* tripLabel; - const char* yesterdayStr; + static constexpr const char* yesterdayStr = "Yest: %5lu"; - const Utility::CircularBuffer& stepsCount; + uint32_t stepsCount; lv_task_t* taskRefresh; }; From fcfdea733ef0c6761fd303baa07cea08fa134116 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1ndor=20R=C3=A1tkai?= Date: Thu, 12 Sep 2024 10:20:00 +0200 Subject: [PATCH 12/16] Removed step_t --- src/components/ble/MotionService.cpp | 6 +++--- src/components/ble/MotionService.h | 2 +- src/components/motion/MotionController.cpp | 2 +- src/components/motion/MotionController.h | 11 +++++------ 4 files changed, 10 insertions(+), 11 deletions(-) diff --git a/src/components/ble/MotionService.cpp b/src/components/ble/MotionService.cpp index 018382a778..93f5150802 100644 --- a/src/components/ble/MotionService.cpp +++ b/src/components/ble/MotionService.cpp @@ -62,7 +62,7 @@ void MotionService::Init() { int MotionService::OnStepCountRequested(uint16_t attributeHandle, ble_gatt_access_ctxt* context) { if (attributeHandle == stepCountHandle) { NRF_LOG_INFO("Motion-stepcount : handle = %d", stepCountHandle); - MotionController::step_t buffer = motionController.NbSteps(); + uint32_t buffer = motionController.NbSteps(); int res = os_mbuf_append(context->om, &buffer, sizeof(buffer)); return (res == 0) ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES; @@ -75,11 +75,11 @@ int MotionService::OnStepCountRequested(uint16_t attributeHandle, ble_gatt_acces return 0; } -void MotionService::OnNewStepCountValue(MotionController::step_t stepCount) { +void MotionService::OnNewStepCountValue(uint32_t stepCount) { if (!stepCountNoficationEnabled) return; - MotionController::step_t buffer = stepCount; + uint32_t buffer = stepCount; auto* om = ble_hs_mbuf_from_flat(&buffer, sizeof(buffer)); uint16_t connectionHandle = nimble.connHandle(); diff --git a/src/components/ble/MotionService.h b/src/components/ble/MotionService.h index beeec479d5..277cb49876 100644 --- a/src/components/ble/MotionService.h +++ b/src/components/ble/MotionService.h @@ -17,7 +17,7 @@ namespace Pinetime { MotionService(NimbleController& nimble, Controllers::MotionController& motionController); void Init(); int OnStepCountRequested(uint16_t attributeHandle, ble_gatt_access_ctxt* context); - void OnNewStepCountValue(MotionController::step_t stepCount); + void OnNewStepCountValue(uint32_t stepCount); void OnNewMotionValues(int16_t x, int16_t y, int16_t z); void SubscribeNotification(uint16_t attributeHandle); diff --git a/src/components/motion/MotionController.cpp b/src/components/motion/MotionController.cpp index 3755808ecc..3634ca2890 100644 --- a/src/components/motion/MotionController.cpp +++ b/src/components/motion/MotionController.cpp @@ -44,7 +44,7 @@ void MotionController::AdvanceDay() { } } -void MotionController::Update(int16_t x, int16_t y, int16_t z, MotionController::step_t nbSteps) { +void MotionController::Update(int16_t x, int16_t y, int16_t z, uint32_t nbSteps) { if (this->nbSteps[Today] != nbSteps && service != nullptr) { service->OnNewStepCountValue(nbSteps); } diff --git a/src/components/motion/MotionController.h b/src/components/motion/MotionController.h index b46e57d4cb..2108b6eaaf 100644 --- a/src/components/motion/MotionController.h +++ b/src/components/motion/MotionController.h @@ -24,12 +24,11 @@ namespace Pinetime { Yesterday = 1, }; - using step_t = uint32_t; static constexpr size_t stepHistorySize = 2; // Store this many day's step counter void AdvanceDay(); - void Update(int16_t x, int16_t y, int16_t z, step_t nbSteps); + void Update(int16_t x, int16_t y, int16_t z, uint32_t nbSteps); int16_t X() const { return xHistory[0]; @@ -43,7 +42,7 @@ namespace Pinetime { return zHistory[0]; } - step_t NbSteps(Days day = Today) const { + uint32_t NbSteps(Days day = Today) const { return nbSteps[day]; } @@ -51,7 +50,7 @@ namespace Pinetime { currentTripSteps = 0; } - step_t GetTripSteps() const { + uint32_t GetTripSteps() const { return currentTripSteps; } @@ -78,8 +77,8 @@ namespace Pinetime { } private: - Utility::CircularBuffer nbSteps = {0}; - step_t currentTripSteps = 0; + Utility::CircularBuffer nbSteps = {0}; + uint32_t currentTripSteps = 0; TickType_t lastTime = 0; TickType_t time = 0; From 0f2c2ffb21f51953f689ccd22a394990f978974f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1ndor=20R=C3=A1tkai?= Date: Thu, 12 Sep 2024 10:25:42 +0200 Subject: [PATCH 13/16] Reverted to the original MotionController and MotionService dependency --- src/components/ble/MotionService.h | 3 +-- src/components/motion/MotionController.cpp | 1 - src/components/motion/MotionController.h | 3 +-- 3 files changed, 2 insertions(+), 5 deletions(-) diff --git a/src/components/ble/MotionService.h b/src/components/ble/MotionService.h index 277cb49876..acc91e8d59 100644 --- a/src/components/ble/MotionService.h +++ b/src/components/ble/MotionService.h @@ -6,11 +6,10 @@ #undef max #undef min -#include "components/motion/MotionController.h" - namespace Pinetime { namespace Controllers { class NimbleController; + class MotionController; class MotionService { public: diff --git a/src/components/motion/MotionController.cpp b/src/components/motion/MotionController.cpp index 3634ca2890..b290520bda 100644 --- a/src/components/motion/MotionController.cpp +++ b/src/components/motion/MotionController.cpp @@ -2,7 +2,6 @@ #include -#include "components/ble/MotionService.h" #include "utility/Math.h" using namespace Pinetime::Controllers; diff --git a/src/components/motion/MotionController.h b/src/components/motion/MotionController.h index 2108b6eaaf..f40a21316f 100644 --- a/src/components/motion/MotionController.h +++ b/src/components/motion/MotionController.h @@ -5,12 +5,11 @@ #include #include "drivers/Bma421.h" +#include "components/ble/MotionService.h" #include "utility/CircularBuffer.h" namespace Pinetime { namespace Controllers { - class MotionService; - class MotionController { public: enum class DeviceTypes { From a5f2d2c7b907aad242472682cdfd2c946ab66ac6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1ndor=20R=C3=A1tkai?= Date: Thu, 12 Sep 2024 11:00:10 +0200 Subject: [PATCH 14/16] Changed MotionController::Days from enum to enum class --- src/components/motion/MotionController.cpp | 11 ++++++----- src/components/motion/MotionController.h | 15 ++++++++++----- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/src/components/motion/MotionController.cpp b/src/components/motion/MotionController.cpp index b290520bda..a04a62f9e0 100644 --- a/src/components/motion/MotionController.cpp +++ b/src/components/motion/MotionController.cpp @@ -37,14 +37,15 @@ namespace { void MotionController::AdvanceDay() { --nbSteps; // Higher index = further in the past - nbSteps[Today] = 0; + NbStepsRef(Days::Today) = 0; if (service != nullptr) { - service->OnNewStepCountValue(nbSteps[Today]); + service->OnNewStepCountValue(NbSteps(Days::Today)); } } void MotionController::Update(int16_t x, int16_t y, int16_t z, uint32_t nbSteps) { - if (this->nbSteps[Today] != nbSteps && service != nullptr) { + uint32_t& oldSteps = NbStepsRef(Days::Today); + if (oldSteps != nbSteps && service != nullptr) { service->OnNewStepCountValue(nbSteps); } @@ -64,11 +65,11 @@ void MotionController::Update(int16_t x, int16_t y, int16_t z, uint32_t nbSteps) stats = GetAccelStats(); - int32_t deltaSteps = nbSteps - this->nbSteps[Today]; + int32_t deltaSteps = nbSteps - oldSteps; if (deltaSteps > 0) { currentTripSteps += deltaSteps; } - this->nbSteps[Today] = nbSteps; + oldSteps = nbSteps; } MotionController::AccelStats MotionController::GetAccelStats() const { diff --git a/src/components/motion/MotionController.h b/src/components/motion/MotionController.h index f40a21316f..33e6516fdc 100644 --- a/src/components/motion/MotionController.h +++ b/src/components/motion/MotionController.h @@ -18,12 +18,13 @@ namespace Pinetime { BMA425, }; - enum Days { + enum class Days : uint8_t { Today = 0, - Yesterday = 1, + Yesterday, + Last, }; - static constexpr size_t stepHistorySize = 2; // Store this many day's step counter + static constexpr size_t stepHistorySize = static_cast>(Days::Last); // Store this many day's step counter void AdvanceDay(); @@ -41,8 +42,8 @@ namespace Pinetime { return zHistory[0]; } - uint32_t NbSteps(Days day = Today) const { - return nbSteps[day]; + uint32_t NbSteps(Days day = Days::Today) const { + return nbSteps[static_cast>(day)]; } void ResetTrip() { @@ -79,6 +80,10 @@ namespace Pinetime { Utility::CircularBuffer nbSteps = {0}; uint32_t currentTripSteps = 0; + uint32_t& NbStepsRef(Days day = Days::Today) { + return nbSteps[static_cast>(day)]; + } + TickType_t lastTime = 0; TickType_t time = 0; From e973125639b098df8bc44198fba9a145de5f5ac9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1ndor=20R=C3=A1tkai?= Date: Thu, 12 Sep 2024 22:45:34 +0200 Subject: [PATCH 15/16] Changed MotionController::stepHistorySize to hard-coded value again --- src/components/motion/MotionController.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/components/motion/MotionController.h b/src/components/motion/MotionController.h index 33e6516fdc..10b91b105d 100644 --- a/src/components/motion/MotionController.h +++ b/src/components/motion/MotionController.h @@ -21,10 +21,9 @@ namespace Pinetime { enum class Days : uint8_t { Today = 0, Yesterday, - Last, }; - static constexpr size_t stepHistorySize = static_cast>(Days::Last); // Store this many day's step counter + static constexpr size_t stepHistorySize = 2; // Store this many day's step counter void AdvanceDay(); From b3695c6c1b355d9822dd71a0ff1ceca8e466a992 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A1ndor=20R=C3=A1tkai?= Date: Wed, 20 Nov 2024 00:26:00 +0100 Subject: [PATCH 16/16] Clang-format fix --- src/displayapp/screens/Steps.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/displayapp/screens/Steps.cpp b/src/displayapp/screens/Steps.cpp index 70cd3be2fc..d25ff57522 100644 --- a/src/displayapp/screens/Steps.cpp +++ b/src/displayapp/screens/Steps.cpp @@ -13,8 +13,7 @@ static void lap_event_handler(lv_obj_t* obj, lv_event_t event) { } Steps::Steps(Controllers::MotionController& motionController, Controllers::Settings& settingsController) - : motionController {motionController}, - settingsController {settingsController} { + : motionController {motionController}, settingsController {settingsController} { stepsArc = lv_arc_create(lv_scr_act(), nullptr);