Skip to content

Commit 716deff

Browse files
authored
Step counter history
Store 2 days steps history and display yesterday's steps on the Steps screen
1 parent 9093d18 commit 716deff

File tree

6 files changed

+50
-11
lines changed

6 files changed

+50
-11
lines changed

src/components/ble/MotionService.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ int MotionService::OnStepCountRequested(uint16_t attributeHandle, ble_gatt_acces
6464
NRF_LOG_INFO("Motion-stepcount : handle = %d", stepCountHandle);
6565
uint32_t buffer = motionController.NbSteps();
6666

67-
int res = os_mbuf_append(context->om, &buffer, 4);
67+
int res = os_mbuf_append(context->om, &buffer, sizeof(buffer));
6868
return (res == 0) ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
6969
}
7070
if (attributeHandle == motionValuesHandle) {
@@ -82,7 +82,7 @@ void MotionService::OnNewStepCountValue(uint32_t stepCount) {
8282
}
8383

8484
uint32_t buffer = stepCount;
85-
auto* om = ble_hs_mbuf_from_flat(&buffer, 4);
85+
auto* om = ble_hs_mbuf_from_flat(&buffer, sizeof(buffer));
8686

8787
uint16_t connectionHandle = nimble.connHandle();
8888

src/components/motion/MotionController.cpp

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,17 @@ namespace {
3535
}
3636
}
3737

38+
void MotionController::AdvanceDay() {
39+
--nbSteps; // Higher index = further in the past
40+
SetSteps(Days::Today, 0);
41+
if (service != nullptr) {
42+
service->OnNewStepCountValue(NbSteps(Days::Today));
43+
}
44+
}
45+
3846
void MotionController::Update(int16_t x, int16_t y, int16_t z, uint32_t nbSteps) {
39-
if (this->nbSteps != nbSteps && service != nullptr) {
47+
uint32_t oldSteps = NbSteps(Days::Today);
48+
if (oldSteps != nbSteps && service != nullptr) {
4049
service->OnNewStepCountValue(nbSteps);
4150
}
4251

@@ -64,11 +73,11 @@ void MotionController::Update(int16_t x, int16_t y, int16_t z, uint32_t nbSteps)
6473

6574
stats = GetAccelStats();
6675

67-
int32_t deltaSteps = nbSteps - this->nbSteps;
76+
int32_t deltaSteps = nbSteps - oldSteps;
6877
if (deltaSteps > 0) {
6978
currentTripSteps += deltaSteps;
7079
}
71-
this->nbSteps = nbSteps;
80+
SetSteps(Days::Today, nbSteps);
7281
}
7382

7483
MotionController::AccelStats MotionController::GetAccelStats() const {

src/components/motion/MotionController.h

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@ namespace Pinetime {
1818
BMA425,
1919
};
2020

21+
enum class Days : uint8_t {
22+
Today = 0,
23+
Yesterday,
24+
};
25+
26+
static constexpr size_t stepHistorySize = 2; // Store this many day's step counter
27+
28+
void AdvanceDay();
29+
2130
void Update(int16_t x, int16_t y, int16_t z, uint32_t nbSteps);
2231

2332
int16_t X() const {
@@ -32,8 +41,8 @@ namespace Pinetime {
3241
return zHistory[0];
3342
}
3443

35-
uint32_t NbSteps() const {
36-
return nbSteps;
44+
uint32_t NbSteps(Days day = Days::Today) const {
45+
return nbSteps[static_cast<std::underlying_type_t<Days>>(day)];
3746
}
3847

3948
void ResetTrip() {
@@ -66,9 +75,13 @@ namespace Pinetime {
6675
}
6776

6877
private:
69-
uint32_t nbSteps = 0;
78+
Utility::CircularBuffer<uint32_t, stepHistorySize> nbSteps = {0};
7079
uint32_t currentTripSteps = 0;
7180

81+
void SetSteps(Days day, uint32_t steps) {
82+
nbSteps[static_cast<std::underlying_type_t<Days>>(day)] = steps;
83+
}
84+
7285
TickType_t lastTime = 0;
7386
TickType_t time = 0;
7487

src/displayapp/screens/Steps.cpp

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55

66
using namespace Pinetime::Applications::Screens;
77

8+
using Days = Pinetime::Controllers::MotionController::Days;
9+
10+
namespace {
11+
constexpr const char* yesterdayStr = "Yest: %5lu";
12+
}
13+
814
static void lap_event_handler(lv_obj_t* obj, lv_event_t event) {
915
auto* steps = static_cast<Steps*>(obj->user_data);
1016
steps->lapBtnEventHandler(event);
@@ -33,13 +39,19 @@ Steps::Steps(Controllers::MotionController& motionController, Controllers::Setti
3339
lSteps = lv_label_create(lv_scr_act(), nullptr);
3440
lv_obj_set_style_local_text_color(lSteps, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_LIME);
3541
lv_obj_set_style_local_text_font(lSteps, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, &jetbrains_mono_42);
36-
lv_label_set_text_fmt(lSteps, "%li", stepsCount);
42+
lv_label_set_text_fmt(lSteps, "%lu", stepsCount);
3743
lv_obj_align(lSteps, nullptr, LV_ALIGN_CENTER, 0, -40);
3844

3945
lv_obj_t* lstepsL = lv_label_create(lv_scr_act(), nullptr);
4046
lv_obj_set_style_local_text_color(lstepsL, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, Colors::lightGray);
4147
lv_label_set_text_static(lstepsL, "Steps");
42-
lv_obj_align(lstepsL, lSteps, LV_ALIGN_OUT_BOTTOM_MID, 0, 5);
48+
lv_obj_align(lstepsL, lSteps, LV_ALIGN_OUT_BOTTOM_MID, 0, 0);
49+
50+
lStepsYesterday = lv_label_create(lv_scr_act(), nullptr);
51+
lv_obj_set_style_local_text_color(lStepsYesterday, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, Colors::lightGray);
52+
lv_label_set_text_fmt(lStepsYesterday, yesterdayStr, motionController.NbSteps(Days::Yesterday));
53+
lv_label_set_align(lStepsYesterday, LV_LABEL_ALIGN_CENTER);
54+
lv_obj_align(lStepsYesterday, lSteps, LV_ALIGN_OUT_BOTTOM_MID, 0, 20);
4355

4456
lv_obj_t* lstepsGoal = lv_label_create(lv_scr_act(), nullptr);
4557
lv_obj_set_style_local_text_color(lstepsGoal, LV_LABEL_PART_MAIN, LV_STATE_DEFAULT, LV_COLOR_CYAN);
@@ -76,7 +88,10 @@ void Steps::Refresh() {
7688
stepsCount = motionController.NbSteps();
7789
currentTripSteps = motionController.GetTripSteps();
7890

79-
lv_label_set_text_fmt(lSteps, "%li", stepsCount);
91+
lv_label_set_text_fmt(lSteps, "%lu", stepsCount);
92+
lv_obj_align(lSteps, nullptr, LV_ALIGN_CENTER, 0, -40);
93+
94+
lv_label_set_text_fmt(lStepsYesterday, yesterdayStr, motionController.NbSteps(Days::Yesterday));
8095
lv_obj_align(lSteps, nullptr, LV_ALIGN_CENTER, 0, -40);
8196

8297
if (currentTripSteps < 100000) {

src/displayapp/screens/Steps.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ namespace Pinetime {
3232
uint32_t currentTripSteps = 0;
3333

3434
lv_obj_t* lSteps;
35+
lv_obj_t* lStepsYesterday;
3536
lv_obj_t* stepsArc;
3637
lv_obj_t* resetBtn;
3738
lv_obj_t* resetButtonLabel;

src/systemtask/SystemTask.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -334,6 +334,7 @@ void SystemTask::Work() {
334334
break;
335335
case Messages::OnNewDay:
336336
motionSensor.ResetStepCounter();
337+
motionController.AdvanceDay();
337338
break;
338339
case Messages::OnNewHour:
339340
using Pinetime::Controllers::AlarmController;

0 commit comments

Comments
 (0)