From 7469157a111c7459fd469fd51057f8fdab9d1687 Mon Sep 17 00:00:00 2001 From: Finlay Davidson Date: Thu, 11 Nov 2021 13:42:25 +0100 Subject: [PATCH] Implement Lower to Sleep functionality --- src/components/motion/MotionController.cpp | 33 ++++++++++++++----- src/components/motion/MotionController.h | 13 +++++--- src/components/settings/Settings.h | 14 +++----- .../screens/settings/SettingWakeUp.cpp | 6 ++-- .../screens/settings/SettingWakeUp.h | 3 +- src/systemtask/SystemTask.cpp | 4 +++ 6 files changed, 47 insertions(+), 26 deletions(-) diff --git a/src/components/motion/MotionController.cpp b/src/components/motion/MotionController.cpp index 8ba468149c..d21503d151 100644 --- a/src/components/motion/MotionController.cpp +++ b/src/components/motion/MotionController.cpp @@ -7,12 +7,14 @@ void MotionController::Update(int16_t x, int16_t y, int16_t z, uint32_t nbSteps) service->OnNewStepCountValue(nbSteps); } - if (service != nullptr && (this->x != x || this->y != y || this->z != z)) { + if (service != nullptr && (this->x != x || lastYs[lastYIndex] != y || this->z != z)) { service->OnNewMotionValues(x, y, z); } + lastYIndex++; + lastYIndex %= lastYs.size(); + lastYs[lastYIndex] = y; this->x = x; - this->y = y; this->z = z; int32_t deltaSteps = nbSteps - this->nbSteps; this->nbSteps = nbSteps; @@ -24,19 +26,19 @@ void MotionController::Update(int16_t x, int16_t y, int16_t z, uint32_t nbSteps) bool MotionController::Should_RaiseWake(bool isSleeping) { if ((x + 335) <= 670 && z < 0) { if (not isSleeping) { - if (y <= 0) { + if (lastYs[lastYIndex] <= 0) { return false; } lastYForWakeUp = 0; return false; } - if (y >= 0) { + if (lastYs[lastYIndex] >= 0) { lastYForWakeUp = 0; return false; } - if (y + 230 < lastYForWakeUp) { - lastYForWakeUp = y; + if (lastYs[lastYIndex] + 230 < lastYForWakeUp) { + lastYForWakeUp = lastYs[lastYIndex]; return true; } } @@ -48,7 +50,7 @@ bool MotionController::Should_ShakeWake(uint16_t thresh) { auto diff = xTaskGetTickCount() - lastShakeTime; lastShakeTime = xTaskGetTickCount(); /* Currently Polling at 10hz, If this ever goes faster scalar and EMA might need adjusting */ - int32_t speed = std::abs(z + (y / 2) + (x / 4) - lastYForShake - lastZForShake) / diff * 100; + int32_t speed = std::abs(z + (lastYs[lastYIndex] / 2) + (x / 4) - lastYForShake - lastZForShake) / diff * 100; //(.2 * speed) + ((1 - .2) * accumulatedspeed); // implemented without floats as .25Alpha accumulatedspeed = (speed / 5) + ((accumulatedspeed / 5) * 4); @@ -57,7 +59,7 @@ bool MotionController::Should_ShakeWake(uint16_t thresh) { wake = true; } lastXForShake = x / 4; - lastYForShake = y / 2; + lastYForShake = lastYs[lastYIndex] / 2; lastZForShake = z; return wake; } @@ -66,6 +68,21 @@ int32_t MotionController::currentShakeSpeed() { return accumulatedspeed; } +bool MotionController::ShouldLowerSleep() const { + if (lastYs[(lastYIndex + 2) % lastYs.size()] < lastYs[(lastYIndex + 1) % lastYs.size()] + 192 || + lastYs[(lastYIndex + 2) % lastYs.size()] < 512) { + return false; + } + + for (uint8_t i = 3; i < lastYs.size(); i++) { + if (lastYs[(lastYIndex + i) % lastYs.size()] < 0) { + return false; + } + } + + return true; +} + void MotionController::IsSensorOk(bool isOk) { isSensorOk = isOk; } diff --git a/src/components/motion/MotionController.h b/src/components/motion/MotionController.h index f80b11b994..5771e019bf 100644 --- a/src/components/motion/MotionController.h +++ b/src/components/motion/MotionController.h @@ -1,6 +1,7 @@ #pragma once #include +#include #include #include @@ -20,7 +21,7 @@ namespace Pinetime { return x; } int16_t Y() const { - return y; + return lastYs[lastYIndex]; } int16_t Z() const { return z; @@ -39,6 +40,7 @@ namespace Pinetime { bool Should_ShakeWake(uint16_t thresh); bool Should_RaiseWake(bool isSleeping); int32_t currentShakeSpeed(); + bool ShouldLowerSleep() const; void IsSensorOk(bool isOk); bool IsSensorOk() const { return isSensorOk; @@ -54,9 +56,10 @@ namespace Pinetime { private: uint32_t nbSteps; uint32_t currentTripSteps = 0; - int16_t x; - int16_t y; - int16_t z; + int16_t x = 0; + int16_t z = 0; + std::array lastYs = {}; + uint8_t lastYIndex = 0; int16_t lastYForWakeUp = 0; bool isSensorOk = false; DeviceTypes deviceType = DeviceTypes::Unknown; @@ -69,4 +72,4 @@ namespace Pinetime { uint32_t lastShakeTime = 0; }; } -} \ No newline at end of file +} diff --git a/src/components/settings/Settings.h b/src/components/settings/Settings.h index 93f861f381..414f7ffd9c 100644 --- a/src/components/settings/Settings.h +++ b/src/components/settings/Settings.h @@ -11,12 +11,7 @@ namespace Pinetime { enum class ClockType : uint8_t { H24, H12 }; enum class Notification : uint8_t { On, Off, Sleep }; enum class ChimesOption : uint8_t { None, Hours, HalfHours }; - enum class WakeUpMode : uint8_t { - SingleTap = 0, - DoubleTap = 1, - RaiseWrist = 2, - Shake = 3, - }; + enum class WakeUpMode : uint8_t { SingleTap = 0, DoubleTap = 1, RaiseWrist = 2, Shake = 3, LowerWrist = 4 }; enum class Colors : uint8_t { White, Silver, @@ -213,7 +208,7 @@ namespace Pinetime { } }; - std::bitset<4> getWakeUpModes() const { + std::bitset<5> getWakeUpModes() const { return settings.wakeUpMode; } @@ -254,7 +249,7 @@ namespace Pinetime { private: Pinetime::Controllers::FS& fs; - static constexpr uint32_t settingsVersion = 0x0004; + static constexpr uint32_t settingsVersion = 0x0005; struct SettingsData { uint32_t version = settingsVersion; uint32_t stepsGoal = 10000; @@ -270,8 +265,9 @@ namespace Pinetime { WatchFaceInfineat watchFaceInfineat; - std::bitset<4> wakeUpMode {0}; + std::bitset<5> wakeUpMode {0}; uint16_t shakeWakeThreshold = 150; + Controllers::BrightnessController::Levels brightLevel = Controllers::BrightnessController::Levels::Medium; }; diff --git a/src/displayapp/screens/settings/SettingWakeUp.cpp b/src/displayapp/screens/settings/SettingWakeUp.cpp index 620fe6e80a..a651cc932d 100644 --- a/src/displayapp/screens/settings/SettingWakeUp.cpp +++ b/src/displayapp/screens/settings/SettingWakeUp.cpp @@ -8,7 +8,7 @@ using namespace Pinetime::Applications::Screens; -constexpr std::array SettingWakeUp::options; +constexpr std::array SettingWakeUp::options; namespace { void event_handler(lv_obj_t* obj, lv_event_t event) { @@ -28,9 +28,9 @@ SettingWakeUp::SettingWakeUp(Pinetime::Applications::DisplayApp* app, Pinetime:: lv_obj_set_style_local_pad_inner(container1, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 5); lv_obj_set_style_local_border_width(container1, LV_CONT_PART_MAIN, LV_STATE_DEFAULT, 0); - lv_obj_set_pos(container1, 10, 60); + lv_obj_set_pos(container1, 10, 35); lv_obj_set_width(container1, LV_HOR_RES - 20); - lv_obj_set_height(container1, LV_VER_RES - 50); + lv_obj_set_height(container1, LV_VER_RES - 20); lv_cont_set_layout(container1, LV_LAYOUT_COLUMN_LEFT); lv_obj_t* title = lv_label_create(lv_scr_act(), nullptr); diff --git a/src/displayapp/screens/settings/SettingWakeUp.h b/src/displayapp/screens/settings/SettingWakeUp.h index 54282f5da2..35371d5a68 100644 --- a/src/displayapp/screens/settings/SettingWakeUp.h +++ b/src/displayapp/screens/settings/SettingWakeUp.h @@ -24,11 +24,12 @@ namespace Pinetime { const char* name; }; Controllers::Settings& settingsController; - static constexpr std::array options = {{ + static constexpr std::array options = {{ {Controllers::Settings::WakeUpMode::SingleTap, "Single Tap"}, {Controllers::Settings::WakeUpMode::DoubleTap, "Double Tap"}, {Controllers::Settings::WakeUpMode::RaiseWrist, "Raise Wrist"}, {Controllers::Settings::WakeUpMode::Shake, "Shake Wake"}, + {Controllers::Settings::WakeUpMode::LowerWrist, "Lower Wrist"}, }}; lv_obj_t* cbOption[options.size()]; diff --git a/src/systemtask/SystemTask.cpp b/src/systemtask/SystemTask.cpp index 73f573fa0e..40e93555b6 100644 --- a/src/systemtask/SystemTask.cpp +++ b/src/systemtask/SystemTask.cpp @@ -505,6 +505,10 @@ void SystemTask::UpdateMotion() { GoToRunning(); } } + if (settingsController.isWakeUpModeOn(Pinetime::Controllers::Settings::WakeUpMode::LowerWrist) && state == SystemTaskState::Running && + motionController.ShouldLowerSleep()) { + PushMessage(Messages::GoToSleep); + } } void SystemTask::HandleButtonAction(Controllers::ButtonActions action) {