Skip to content

Commit

Permalink
Replace lastY with circular buffer to prevent turning off when shaken
Browse files Browse the repository at this point in the history
  • Loading branch information
FintasticMan committed May 9, 2022
1 parent 2feb17c commit 9c375be
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 15 deletions.
17 changes: 15 additions & 2 deletions src/components/motion/MotionController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ void MotionController::Update(int16_t x, int16_t y, int16_t z, uint32_t nbSteps)
service->OnNewMotionValues(x, y, z);
}

lastY = this->y;
lastYIndex++;
lastYIndex %= lastYs.size();
lastYs.at(lastYIndex) = this->y;
this->x = x;
this->y = y;
this->z = z;
Expand Down Expand Up @@ -68,7 +70,18 @@ int32_t MotionController::currentShakeSpeed() {
}

bool MotionController::ShouldLowerSleep() const {
return y >= 512 && y >= lastY + 192;
if (lastYs.at((lastYIndex + 2) % lastYs.size()) < lastYs.at((lastYIndex + 1) % lastYs.size()) + 192 ||
lastYs.at((lastYIndex + 2) % lastYs.size()) < 512) {
return false;
}

for (uint8_t i = 3; i < lastYs.size(); i++) {
if (lastYs.at((lastYIndex + i) % lastYs.size()) < 0) {
return false;
}
}

return true;
}

void MotionController::IsSensorOk(bool isOk) {
Expand Down
7 changes: 3 additions & 4 deletions src/components/motion/MotionController.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <cstdint>
#include <array>
#include <drivers/Bma421.h>
#include <components/ble/MotionService.h>

Expand All @@ -25,9 +26,6 @@ namespace Pinetime {
int16_t Z() const {
return z;
}
int16_t LastY() const {
return lastY;
}
uint32_t NbSteps() const {
return nbSteps;
}
Expand Down Expand Up @@ -61,7 +59,8 @@ namespace Pinetime {
int16_t x = 0;
int16_t y = 0;
int16_t z = 0;
int16_t lastY = 0;
std::array<int16_t, 8> lastYs = {};
uint8_t lastYIndex = 0;
int16_t lastYForWakeUp = 0;
bool isSensorOk = false;
DeviceTypes deviceType = DeviceTypes::Unknown;
Expand Down
8 changes: 0 additions & 8 deletions src/displayapp/screens/Motion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,6 @@ Motion::Motion(Pinetime::Applications::DisplayApp* app, Controllers::MotionContr
lv_obj_align(labelStep, chart, LV_ALIGN_IN_BOTTOM_LEFT, 0, 0);
lv_label_set_text_static(labelStep, "Steps ---");

labelLastY = lv_label_create(lv_scr_act(), NULL);
lv_label_set_text(labelLastY, "LastY 0");
lv_label_set_align(labelLastY, LV_LABEL_ALIGN_RIGHT);
lv_obj_align(labelLastY, chart, LV_ALIGN_IN_BOTTOM_RIGHT, 0, 0);

taskRefresh = lv_task_create(RefreshTaskCallback, LV_DISP_DEF_REFR_PERIOD, LV_TASK_PRIO_MID, this);
}

Expand All @@ -63,7 +58,4 @@ void Motion::Refresh() {
motionController.Y() / 0x10,
motionController.Z() / 0x10);
lv_obj_align(label, NULL, LV_ALIGN_IN_TOP_MID, 0, 10);

lv_label_set_text_fmt(labelLastY, "LastY %d", motionController.LastY() / 0x10);
lv_obj_align(labelLastY, chart, LV_ALIGN_IN_BOTTOM_RIGHT, 0, 0);
}
1 change: 0 additions & 1 deletion src/displayapp/screens/Motion.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ namespace Pinetime {
lv_obj_t* label;

lv_obj_t* labelStep;
lv_obj_t* labelLastY;
lv_task_t* taskRefresh;
};
}
Expand Down

0 comments on commit 9c375be

Please sign in to comment.