Skip to content

Commit

Permalink
Implement Lower to Sleep functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
FintasticMan committed Jan 7, 2023
1 parent 09db67e commit 7469157
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 26 deletions.
33 changes: 25 additions & 8 deletions src/components/motion/MotionController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}
}
Expand All @@ -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);
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down
13 changes: 8 additions & 5 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 @@ -20,7 +21,7 @@ namespace Pinetime {
return x;
}
int16_t Y() const {
return y;
return lastYs[lastYIndex];
}
int16_t Z() const {
return z;
Expand All @@ -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;
Expand All @@ -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<int16_t, 8> lastYs = {};
uint8_t lastYIndex = 0;
int16_t lastYForWakeUp = 0;
bool isSensorOk = false;
DeviceTypes deviceType = DeviceTypes::Unknown;
Expand All @@ -69,4 +72,4 @@ namespace Pinetime {
uint32_t lastShakeTime = 0;
};
}
}
}
14 changes: 5 additions & 9 deletions src/components/settings/Settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -213,7 +208,7 @@ namespace Pinetime {
}
};

std::bitset<4> getWakeUpModes() const {
std::bitset<5> getWakeUpModes() const {
return settings.wakeUpMode;
}

Expand Down Expand Up @@ -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;
Expand All @@ -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;
};

Expand Down
6 changes: 3 additions & 3 deletions src/displayapp/screens/settings/SettingWakeUp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

using namespace Pinetime::Applications::Screens;

constexpr std::array<SettingWakeUp::Option, 4> SettingWakeUp::options;
constexpr std::array<SettingWakeUp::Option, 5> SettingWakeUp::options;

namespace {
void event_handler(lv_obj_t* obj, lv_event_t event) {
Expand All @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion src/displayapp/screens/settings/SettingWakeUp.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ namespace Pinetime {
const char* name;
};
Controllers::Settings& settingsController;
static constexpr std::array<Option, 4> options = {{
static constexpr std::array<Option, 5> 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()];
Expand Down
4 changes: 4 additions & 0 deletions src/systemtask/SystemTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 7469157

Please sign in to comment.