diff options
Diffstat (limited to 'src/components')
-rw-r--r-- | src/components/ble/NimbleController.cpp | 23 | ||||
-rw-r--r-- | src/components/datetime/DateTimeController.cpp | 43 | ||||
-rw-r--r-- | src/components/datetime/DateTimeController.h | 7 | ||||
-rw-r--r-- | src/components/heartrate/Ppg.h | 2 | ||||
-rw-r--r-- | src/components/motion/MotionController.cpp | 47 | ||||
-rw-r--r-- | src/components/motion/MotionController.h | 20 | ||||
-rw-r--r-- | src/components/motor/MotorController.h | 2 | ||||
-rw-r--r-- | src/components/settings/Settings.h | 37 |
8 files changed, 164 insertions, 17 deletions
diff --git a/src/components/ble/NimbleController.cpp b/src/components/ble/NimbleController.cpp index d85ec5dc..94d2d155 100644 --- a/src/components/ble/NimbleController.cpp +++ b/src/components/ble/NimbleController.cpp @@ -282,7 +282,28 @@ int NimbleController::OnGAPEvent(ble_gap_event* event) { if (event->passkey.params.action == BLE_SM_IOACT_DISP) { struct ble_sm_io pkey = {0}; pkey.action = event->passkey.params.action; - pkey.passkey = ble_ll_rand() % 1000000; + + /* + * Passkey is a 6 digits code (1'000'000 possibilities). + * It is important every possible value has an equal probability + * of getting generated. Simply applying a modulo creates a bias + * since 2^32 is not a multiple of 1'000'000. + * To prevent that, we can reject values greater than 999'999. + * + * Rejecting values would happen a lot since 2^32-1 is way greater + * than 1'000'000. An optimisation is to use a multiple of 1'000'000. + * The greatest multiple of 1'000'000 lesser than 2^32-1 is + * 4'294'000'000. + * + * Great explanation at: + * https://research.kudelskisecurity.com/2020/07/28/the-definitive-guide-to-modulo-bias-and-how-to-avoid-it/ + */ + uint32_t passkey_rand; + do { + passkey_rand = ble_ll_rand(); + } while (passkey_rand > 4293999999); + pkey.passkey = passkey_rand % 1000000; + bleController.SetPairingKey(pkey.passkey); systemTask.PushMessage(Pinetime::System::Messages::OnPairing); ble_sm_inject_io(event->passkey.conn_handle, &pkey); diff --git a/src/components/datetime/DateTimeController.cpp b/src/components/datetime/DateTimeController.cpp index 4ac9e1f1..e0d12431 100644 --- a/src/components/datetime/DateTimeController.cpp +++ b/src/components/datetime/DateTimeController.cpp @@ -11,6 +11,9 @@ namespace { char const* MonthsStringLow[] = {"--", "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; } +DateTime::DateTime(Controllers::Settings& settingsController) : settingsController {settingsController} { +} + void DateTime::SetCurrentTime(std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds> t) { this->currentDateTime = t; UpdateTime(previousSystickCounter); // Update internal state without updating the time @@ -36,6 +39,8 @@ void DateTime::SetTime( UpdateTime(systickCounter); NRF_LOG_INFO("* %d %d %d ", this->hour, this->minute, this->second); NRF_LOG_INFO("* %d %d %d ", this->day, this->month, this->year); + + systemTask->PushMessage(System::Messages::OnNewTime); } void DateTime::UpdateTime(uint32_t systickCounter) { @@ -75,6 +80,24 @@ void DateTime::UpdateTime(uint32_t systickCounter) { minute = time.minutes().count(); second = time.seconds().count(); + if (minute == 0 && !isHourAlreadyNotified) { + isHourAlreadyNotified = true; + if (systemTask != nullptr) { + systemTask->PushMessage(System::Messages::OnNewHour); + } + } else if (minute != 0) { + isHourAlreadyNotified = false; + } + + if ((minute == 0 || minute == 30) && !isHalfHourAlreadyNotified) { + isHalfHourAlreadyNotified = true; + if (systemTask != nullptr) { + systemTask->PushMessage(System::Messages::OnNewHalfHour); + } + } else if (minute != 0 && minute != 30) { + isHalfHourAlreadyNotified = false; + } + // Notify new day to SystemTask if (hour == 0 and not isMidnightAlreadyNotified) { isMidnightAlreadyNotified = true; @@ -101,3 +124,23 @@ void DateTime::Register(Pinetime::System::SystemTask* systemTask) { this->systemTask = systemTask; } +using ClockType = Pinetime::Controllers::Settings::ClockType; +std::string DateTime::FormattedTime() { + // Return time as a string in 12- or 24-hour format + char buff[9]; + if (settingsController.GetClockType() == ClockType::H12) { + uint8_t hour12; + const char* amPmStr; + if (hour < 12) { + hour12 = (hour == 0) ? 12 : hour; + amPmStr = "AM"; + } else { + hour12 = (hour == 12) ? 12 : hour - 12; + amPmStr = "PM"; + } + sprintf(buff, "%i:%02i %s", hour12, minute, amPmStr); + } else { + sprintf(buff, "%02i:%02i", hour, minute); + } + return std::string(buff); +} diff --git a/src/components/datetime/DateTimeController.h b/src/components/datetime/DateTimeController.h index 77ed68e8..6e5ee3ca 100644 --- a/src/components/datetime/DateTimeController.h +++ b/src/components/datetime/DateTimeController.h @@ -2,6 +2,8 @@ #include <cstdint> #include <chrono> +#include <string> +#include "components/settings/Settings.h" namespace Pinetime { namespace System { @@ -10,6 +12,7 @@ namespace Pinetime { namespace Controllers { class DateTime { public: + DateTime(Controllers::Settings& settingsController); enum class Days : uint8_t { Unknown, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday }; enum class Months : uint8_t { Unknown, @@ -71,6 +74,7 @@ namespace Pinetime { void Register(System::SystemTask* systemTask); void SetCurrentTime(std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds> t); + std::string FormattedTime(); private: uint16_t year = 0; @@ -86,7 +90,10 @@ namespace Pinetime { std::chrono::seconds uptime {0}; bool isMidnightAlreadyNotified = false; + bool isHourAlreadyNotified = true; + bool isHalfHourAlreadyNotified = true; System::SystemTask* systemTask = nullptr; + Controllers::Settings& settingsController; }; } } diff --git a/src/components/heartrate/Ppg.h b/src/components/heartrate/Ppg.h index ed79b082..7000c871 100644 --- a/src/components/heartrate/Ppg.h +++ b/src/components/heartrate/Ppg.h @@ -1,6 +1,8 @@ #pragma once #include <array> +#include <cstddef> +#include <cstdint> #include "components/heartrate/Biquad.h" #include "components/heartrate/Ptagc.h" diff --git a/src/components/motion/MotionController.cpp b/src/components/motion/MotionController.cpp index cae49105..7dd32127 100644 --- a/src/components/motion/MotionController.cpp +++ b/src/components/motion/MotionController.cpp @@ -1,5 +1,5 @@ #include "components/motion/MotionController.h" - +#include "os/os_cputime.h" using namespace Pinetime::Controllers; void MotionController::Update(int16_t x, int16_t y, int16_t z, uint32_t nbSteps) { @@ -7,17 +7,21 @@ 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 || this->y != y || this->z != z)) { service->OnNewMotionValues(x, y, z); } this->x = x; this->y = y; this->z = z; + int32_t deltaSteps = nbSteps - this->nbSteps; this->nbSteps = nbSteps; + if (deltaSteps > 0) { + currentTripSteps += deltaSteps; + } } -bool MotionController::ShouldWakeUp(bool isSleeping) { +bool MotionController::Should_RaiseWake(bool isSleeping) { if ((x + 335) <= 670 && z < 0) { if (not isSleeping) { if (y <= 0) { @@ -39,14 +43,43 @@ bool MotionController::ShouldWakeUp(bool isSleeping) { } return false; } + +bool MotionController::Should_ShakeWake(uint16_t thresh) { + bool wake = false; + 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; + //(.2 * speed) + ((1 - .2) * accumulatedspeed); + // implemented without floats as .25Alpha + accumulatedspeed = (speed / 5) + ((accumulatedspeed / 5) * 4); + + if (accumulatedspeed > thresh) { + wake = true; + } + lastXForShake = x / 4; + lastYForShake = y / 2; + lastZForShake = z; + return wake; +} +int32_t MotionController::currentShakeSpeed() { + return accumulatedspeed; +} + void MotionController::IsSensorOk(bool isOk) { isSensorOk = isOk; } void MotionController::Init(Pinetime::Drivers::Bma421::DeviceTypes types) { - switch(types){ - case Drivers::Bma421::DeviceTypes::BMA421: this->deviceType = DeviceTypes::BMA421; break; - case Drivers::Bma421::DeviceTypes::BMA425: this->deviceType = DeviceTypes::BMA425; break; - default: this->deviceType = DeviceTypes::Unknown; break; + switch (types) { + case Drivers::Bma421::DeviceTypes::BMA421: + this->deviceType = DeviceTypes::BMA421; + break; + case Drivers::Bma421::DeviceTypes::BMA425: + this->deviceType = DeviceTypes::BMA425; + break; + default: + this->deviceType = DeviceTypes::Unknown; + break; } } void MotionController::SetService(Pinetime::Controllers::MotionService* service) { diff --git a/src/components/motion/MotionController.h b/src/components/motion/MotionController.h index c72d8a4a..f80b11b9 100644 --- a/src/components/motion/MotionController.h +++ b/src/components/motion/MotionController.h @@ -8,7 +8,7 @@ namespace Pinetime { namespace Controllers { class MotionController { public: - enum class DeviceTypes{ + enum class DeviceTypes { Unknown, BMA421, BMA425, @@ -28,8 +28,17 @@ namespace Pinetime { uint32_t NbSteps() const { return nbSteps; } - bool ShouldWakeUp(bool isSleeping); + void ResetTrip() { + currentTripSteps = 0; + } + uint32_t GetTripSteps() const { + return currentTripSteps; + } + + bool Should_ShakeWake(uint16_t thresh); + bool Should_RaiseWake(bool isSleeping); + int32_t currentShakeSpeed(); void IsSensorOk(bool isOk); bool IsSensorOk() const { return isSensorOk; @@ -44,6 +53,7 @@ namespace Pinetime { private: uint32_t nbSteps; + uint32_t currentTripSteps = 0; int16_t x; int16_t y; int16_t z; @@ -51,6 +61,12 @@ namespace Pinetime { bool isSensorOk = false; DeviceTypes deviceType = DeviceTypes::Unknown; Pinetime::Controllers::MotionService* service = nullptr; + + int16_t lastXForShake = 0; + int16_t lastYForShake = 0; + int16_t lastZForShake = 0; + int32_t accumulatedspeed = 0; + uint32_t lastShakeTime = 0; }; } }
\ No newline at end of file diff --git a/src/components/motor/MotorController.h b/src/components/motor/MotorController.h index c9326d57..b5a592b1 100644 --- a/src/components/motor/MotorController.h +++ b/src/components/motor/MotorController.h @@ -12,7 +12,7 @@ namespace Pinetime { void Init(); void RunForDuration(uint8_t motorDuration); void StartRinging(); - static void StopRinging(); + void StopRinging(); private: static void Ring(void* p_context); diff --git a/src/components/settings/Settings.h b/src/components/settings/Settings.h index a1529fec..9878aac5 100644 --- a/src/components/settings/Settings.h +++ b/src/components/settings/Settings.h @@ -1,7 +1,6 @@ #pragma once #include <cstdint> #include <bitset> -#include "components/datetime/DateTimeController.h" #include "components/brightness/BrightnessController.h" #include "components/fs/FS.h" @@ -11,10 +10,12 @@ namespace Pinetime { public: enum class ClockType : uint8_t { H24, H12 }; enum class Notification : uint8_t { ON, OFF }; + enum class ChimesOption : uint8_t { None, Hours, HalfHours }; enum class WakeUpMode : uint8_t { SingleTap = 0, DoubleTap = 1, RaiseWrist = 2, + Shake = 3, }; enum class Colors : uint8_t { White, @@ -56,6 +57,16 @@ namespace Pinetime { return settings.clockFace; }; + void SetChimeOption(ChimesOption chimeOption) { + if (chimeOption != settings.chimesOption) { + settingsChanged = true; + } + settings.chimesOption = chimeOption; + }; + ChimesOption GetChimeOption() const { + return settings.chimesOption; + }; + void SetPTSColorTime(Colors colorTime) { if (colorTime != settings.PTS.ColorTime) settingsChanged = true; @@ -124,10 +135,23 @@ namespace Pinetime { } settings.screenTimeOut = timeout; }; + uint32_t GetScreenTimeOut() const { return settings.screenTimeOut; }; + void SetShakeThreshold(uint16_t thresh){ + if(settings.shakeWakeThreshold != thresh){ + settings.shakeWakeThreshold = thresh; + settingsChanged = true; + } + + } + + int16_t GetShakeThreshold() const{ + return settings.shakeWakeThreshold; + } + void setWakeUpMode(WakeUpMode wakeUp, bool enabled) { if (enabled != isWakeUpModeOn(wakeUp)) { settingsChanged = true; @@ -142,13 +166,13 @@ namespace Pinetime { case WakeUpMode::DoubleTap: settings.wakeUpMode.set(static_cast<size_t>(WakeUpMode::SingleTap), false); break; - case WakeUpMode::RaiseWrist: + default: break; } } }; - std::bitset<3> getWakeUpModes() const { + std::bitset<4> getWakeUpModes() const { return settings.wakeUpMode; } @@ -189,7 +213,7 @@ namespace Pinetime { private: Pinetime::Controllers::FS& fs; - static constexpr uint32_t settingsVersion = 0x0002; + static constexpr uint32_t settingsVersion = 0x0003; struct SettingsData { uint32_t version = settingsVersion; uint32_t stepsGoal = 10000; @@ -199,11 +223,12 @@ namespace Pinetime { Notification notificationStatus = Notification::ON; uint8_t clockFace = 0; + ChimesOption chimesOption = ChimesOption::None; PineTimeStyle PTS; - std::bitset<3> wakeUpMode {0}; - + std::bitset<4> wakeUpMode {0}; + uint16_t shakeWakeThreshold = 150; Controllers::BrightnessController::Levels brightLevel = Controllers::BrightnessController::Levels::Medium; }; |