Skip to content

Commit

Permalink
activity: Switch to CircularBuffer utility
Browse files Browse the repository at this point in the history
  • Loading branch information
FintasticMan committed Mar 17, 2023
1 parent 6d75922 commit e355baa
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 46 deletions.
39 changes: 11 additions & 28 deletions src/components/activity/ActivityController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,24 @@

using namespace Pinetime::Controllers;

ActivityController::ActivityController(Controllers::DateTime& dateTimeController,
Controllers::Settings& settingsController,
Controllers::MotionController& motionController)
: dateTimeController {dateTimeController}, settingsController {settingsController}, motionController {motionController} {
}

void ActivityController::UpdateSteps() {
int8_t utcMinutes = (dateTimeController.Minutes() - dateTimeController.UtcOffset() * 15) % 60;
if (utcMinutes < 0) {
utcMinutes += 60;
}

idx = utcMinutes * stepHistory.size() / 60;
if (prevIdx != idx && prevIdx != UINT8_MAX) {
oldestSteps = olderSteps;
olderSteps = stepHistory[idx];
void ActivityController::UpdateSteps(uint32_t nbSteps, uint8_t minutes) {
uint8_t hourSegment = minutes * (stepHistory.Size() - 2) / 60;
if (hourSegment != prevHourSegment && prevHourSegment != UINT8_MAX) {
stepHistory++;
canNotify = true;
}
prevIdx = idx;
stepHistory[idx] = motionController.NbSteps();
prevHourSegment = hourSegment;
stepHistory[0] = nbSteps;

if (stepHistory[idx] < stepHistory[(idx + stepHistory.size() - 1) % stepHistory.size()]) {
for (uint8_t i = 0; i < stepHistory.size(); i++) {
if (i != idx) {
stepHistory[i] = 0;
}
if (stepHistory[0] < stepHistory[stepHistory.Size() - 1]) {
for (uint8_t i = 1; i < stepHistory.Size(); i++) {
stepHistory[i] = 0;
}
oldestSteps = 0;
olderSteps = 0;
}
}

bool ActivityController::ShouldNotify() {
if (canNotify &&
stepHistory[(idx + stepHistory.size() - 1) % stepHistory.size()] < oldestSteps + settingsController.GetActivityThresh()) {
bool ActivityController::ShouldNotify(uint16_t thresh) {
if (canNotify && stepHistory[stepHistory.Size() - 1] - stepHistory[1] < thresh) {
canNotify = false;
return true;
}
Expand Down
20 changes: 5 additions & 15 deletions src/components/activity/ActivityController.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,18 @@
#include "components/datetime/DateTimeController.h"
#include "components/settings/Settings.h"
#include "components/motion/MotionController.h"
#include "utility/CircularBuffer.h"

namespace Pinetime {
namespace Controllers {
class ActivityController {
public:
ActivityController(Controllers::DateTime& dateTimeController,
Controllers::Settings& settingsController,
Controllers::MotionController& motionController);

void UpdateSteps();
bool ShouldNotify();
void UpdateSteps(uint32_t nbSteps, uint8_t minutes);
bool ShouldNotify(uint16_t thresh);

private:
Controllers::DateTime& dateTimeController;
Controllers::Settings& settingsController;
Controllers::MotionController& motionController;

std::array<uint32_t, 2> stepHistory = {};
uint32_t olderSteps = 0;
uint32_t oldestSteps = 0;
uint8_t prevIdx = UINT8_MAX;
uint8_t idx = 0;
Utility::CircularBuffer<uint32_t, 4> stepHistory = {};
uint8_t prevHourSegment = UINT8_MAX;
bool canNotify = false;
};
}
Expand Down
2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ Pinetime::Controllers::AlarmController alarmController {dateTimeController};
Pinetime::Controllers::TouchHandler touchHandler;
Pinetime::Controllers::ButtonHandler buttonHandler;
Pinetime::Controllers::BrightnessController brightnessController {};
Pinetime::Controllers::ActivityController activityController(dateTimeController, settingsController, motionController);
Pinetime::Controllers::ActivityController activityController;

Pinetime::Applications::DisplayApp displayApp(lcd,
touchPanel,
Expand Down
5 changes: 3 additions & 2 deletions src/systemtask/SystemTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,10 @@ void SystemTask::Work() {
while (true) {
UpdateMotion();

activityController.UpdateSteps();
activityController.UpdateSteps(motionController.NbSteps(), dateTimeController.Minutes());
if (settingsController.GetNotificationStatus() == Controllers::Settings::Notification::On &&
settingsController.GetActivity() == Controllers::Settings::Activity::On && activityController.ShouldNotify()) {
settingsController.GetActivity() == Controllers::Settings::Activity::On &&
activityController.ShouldNotify(settingsController.GetActivityThresh())) {
NRF_LOG_INFO("activity");
Controllers::NotificationManager::Notification notif;
constexpr char message[] = "Low activity\0Fewer steps than threshold in last hour. Try walking around.";
Expand Down

0 comments on commit e355baa

Please sign in to comment.