diff options
Diffstat (limited to 'src/components')
| -rw-r--r-- | src/components/alarm/AlarmController.cpp | 16 | ||||
| -rw-r--r-- | src/components/alarm/AlarmController.h | 1 | ||||
| -rw-r--r-- | src/components/ble/BleController.h | 5 | ||||
| -rw-r--r-- | src/components/ble/NimbleController.cpp | 47 | ||||
| -rw-r--r-- | src/components/brightness/BrightnessController.h | 2 | ||||
| -rw-r--r-- | src/components/datetime/DateTimeController.cpp | 43 | ||||
| -rw-r--r-- | src/components/datetime/DateTimeController.h | 19 | ||||
| -rw-r--r-- | src/components/motion/MotionController.cpp | 28 | ||||
| -rw-r--r-- | src/components/motion/MotionController.h | 15 | ||||
| -rw-r--r-- | src/components/settings/Settings.h | 19 | ||||
| -rw-r--r-- | src/components/timer/TimerController.cpp | 32 | ||||
| -rw-r--r-- | src/components/timer/TimerController.h | 19 |
12 files changed, 143 insertions, 103 deletions
diff --git a/src/components/alarm/AlarmController.cpp b/src/components/alarm/AlarmController.cpp index a08bfbd..6e93f6d 100644 --- a/src/components/alarm/AlarmController.cpp +++ b/src/components/alarm/AlarmController.cpp @@ -44,6 +44,22 @@ void AlarmController::SetAlarmTime(uint8_t alarmHr, uint8_t alarmMin) { minutes = alarmMin; } +void AlarmController::RescheduleAlarm() { + + if (state != AlarmState::Set) { return; } + + xTimerStop(alarmAppTimer, 0); + auto now = dateTimeController.CurrentDateTime(); + alarmTime = now; + auto mSecToAlarm = std::chrono::duration_cast<std::chrono::milliseconds>(alarmTime - now).count(); + + if (mSecToAlarm < 0) { mSecToAlarm = 0; } + + xTimerChangePeriod(alarmAppTimer, APP_TIMER_TICKS(mSecToAlarm), 0); + xTimerStart(alarmAppTimer, 0); + +} + void AlarmController::ScheduleAlarm() { // Determine the next time the alarm needs to go off and set the app_timer xTimerStop(alarmAppTimer, 0); diff --git a/src/components/alarm/AlarmController.h b/src/components/alarm/AlarmController.h index 3c66775..df65c63 100644 --- a/src/components/alarm/AlarmController.h +++ b/src/components/alarm/AlarmController.h @@ -34,6 +34,7 @@ namespace Pinetime { void Init(System::SystemTask* systemTask); void SetAlarmTime(uint8_t alarmHr, uint8_t alarmMin); void ScheduleAlarm(); + void RescheduleAlarm(); void DisableAlarm(); void SetOffAlarmNow(); uint32_t SecondsToAlarm(); diff --git a/src/components/ble/BleController.h b/src/components/ble/BleController.h index 675ede2..b15d688 100644 --- a/src/components/ble/BleController.h +++ b/src/components/ble/BleController.h @@ -20,6 +20,10 @@ namespace Pinetime { void EnableRadio(); void DisableRadio(); + void EnablePasskey() { isPasskeyEnabled = true; } + void DisablePasskey() { isPasskeyEnabled = false; } + bool IsPasskeyEnabled() { return isPasskeyEnabled; } + void StartFirmwareUpdate(); void StopFirmwareUpdate(); void FirmwareUpdateTotalBytes(uint32_t totalBytes); @@ -60,6 +64,7 @@ namespace Pinetime { private: bool isConnected = false; bool isRadioEnabled = true; + bool isPasskeyEnabled = false; bool isFirmwareUpdating = false; uint32_t firmwareUpdateTotalBytes = 0; uint32_t firmwareUpdateCurrentBytes = 0; diff --git a/src/components/ble/NimbleController.cpp b/src/components/ble/NimbleController.cpp index 10eb429..4dee63e 100644 --- a/src/components/ble/NimbleController.cpp +++ b/src/components/ble/NimbleController.cpp @@ -116,25 +116,38 @@ void NimbleController::Init() { ASSERT(rc == 0); bleController.Address(std::move(address)); - switch (addrType) { - case BLE_OWN_ADDR_PUBLIC: - bleController.AddressType(Ble::AddressTypes::Public); - break; - case BLE_OWN_ADDR_RANDOM: - bleController.AddressType(Ble::AddressTypes::Random); - break; - case BLE_OWN_ADDR_RPA_PUBLIC_DEFAULT: - bleController.AddressType(Ble::AddressTypes::RPA_Public); - break; - case BLE_OWN_ADDR_RPA_RANDOM_DEFAULT: - bleController.AddressType(Ble::AddressTypes::RPA_Random); - break; + if (bleController.IsPasskeyEnabled()) { + switch (addrType) { + case BLE_OWN_ADDR_PUBLIC: + bleController.AddressType(Ble::AddressTypes::Public); + break; + case BLE_OWN_ADDR_RANDOM: + bleController.AddressType(Ble::AddressTypes::Random); + break; + case BLE_OWN_ADDR_RPA_PUBLIC_DEFAULT: + bleController.AddressType(Ble::AddressTypes::RPA_Public); + break; + case BLE_OWN_ADDR_RPA_RANDOM_DEFAULT: + bleController.AddressType(Ble::AddressTypes::RPA_Random); + break; + } + } else { + switch (addrType) { + case BLE_OWN_ADDR_PUBLIC: + bleController.AddressType(Ble::AddressTypes::Public); + break; + default: + bleController.AddressType(Ble::AddressTypes::Random); + break; + } } rc = ble_gatts_start(); ASSERT(rc == 0); - RestoreBond(); + if (bleController.IsPasskeyEnabled()) { + RestoreBond(); + } StartAdvertising(); } @@ -217,7 +230,7 @@ int NimbleController::OnGAPEvent(ble_gap_event* event) { NRF_LOG_INFO("Disconnect event : BLE_GAP_EVENT_DISCONNECT"); NRF_LOG_INFO("disconnect reason=%d", event->disconnect.reason); - if (event->disconnect.conn.sec_state.bonded) { + if (bleController.IsPasskeyEnabled() && event->disconnect.conn.sec_state.bonded) { PersistBond(event->disconnect.conn); } @@ -252,7 +265,7 @@ int NimbleController::OnGAPEvent(ble_gap_event* event) { NRF_LOG_INFO("Security event : BLE_GAP_EVENT_ENC_CHANGE"); NRF_LOG_INFO("encryption change event; status=%0X ", event->enc_change.status); - if (event->enc_change.status == 0) { + if (bleController.IsPasskeyEnabled() && event->enc_change.status == 0) { struct ble_gap_conn_desc desc; ble_gap_conn_find(event->enc_change.conn_handle, &desc); if (desc.sec_state.bonded) { @@ -281,7 +294,7 @@ int NimbleController::OnGAPEvent(ble_gap_event* event) { * Use the tinycrypt prng here since rand() is predictable. */ NRF_LOG_INFO("Security event : BLE_GAP_EVENT_PASSKEY_ACTION"); - if (event->passkey.params.action == BLE_SM_IOACT_DISP) { + if (bleController.IsPasskeyEnabled() && event->passkey.params.action == BLE_SM_IOACT_DISP) { struct ble_sm_io pkey = {0}; pkey.action = event->passkey.params.action; diff --git a/src/components/brightness/BrightnessController.h b/src/components/brightness/BrightnessController.h index 0d7ac2f..1ee5603 100644 --- a/src/components/brightness/BrightnessController.h +++ b/src/components/brightness/BrightnessController.h @@ -6,7 +6,7 @@ namespace Pinetime { namespace Controllers { class BrightnessController { public: - enum class Levels { Off, Low, Medium, High }; + enum class Levels : uint8_t { Off = 0, Low = 1, Medium = 2, High = 3 }; void Init(); void Set(Levels level); diff --git a/src/components/datetime/DateTimeController.cpp b/src/components/datetime/DateTimeController.cpp index 3bfbdc7..01e2859 100644 --- a/src/components/datetime/DateTimeController.cpp +++ b/src/components/datetime/DateTimeController.cpp @@ -14,7 +14,7 @@ namespace { DateTime::DateTime(Controllers::Settings& settingsController) : settingsController {settingsController} { } -void DateTime::SetCurrentTime(std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds> t) { +void DateTime::SetCurrentTime(std::chrono::time_point<std::chrono::system_clock, std::chrono::seconds> t) { this->currentDateTime = t; UpdateTime(previousSystickCounter); // Update internal state without updating the time } @@ -30,7 +30,7 @@ void DateTime::SetTime( /* .tm_year = */ year - 1900, }; tm.tm_isdst = -1; // Use DST value from local time zone - currentDateTime = std::chrono::system_clock::from_time_t(std::mktime(&tm)); + currentDateTime = std::chrono::time_point_cast<std::chrono::seconds>(std::chrono::system_clock::from_time_t(std::mktime(&tm))); NRF_LOG_INFO("%d %d %d ", day, month, year); NRF_LOG_INFO("%d %d %d ", hour, minute, second); @@ -38,34 +38,27 @@ 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); + NRF_LOG_INFO("* %d %d %d ", this->day, ((int)this->month), this->year); systemTask->PushMessage(System::Messages::OnNewTime); } void DateTime::UpdateTime(uint32_t systickCounter) { // Handle systick counter overflow - uint32_t systickDelta = 0; - if (systickCounter < previousSystickCounter) { - systickDelta = 0xffffff - previousSystickCounter; - systickDelta += systickCounter + 1; - } else { - systickDelta = systickCounter - previousSystickCounter; - } + uint32_t systickDelta = (systickCounter - previousSystickCounter) & 0xffffff; + previousSystickCounter = systickCounter; /* * 1000 ms = 1024 ticks */ - auto correctedDelta = systickDelta / 1024; - auto rest = (systickDelta - (correctedDelta * 1024)); - if (systickCounter >= rest) { - previousSystickCounter = systickCounter - rest; - } else { - previousSystickCounter = 0xffffff - (rest - systickCounter); - } + // auto newSeconds = systickDelta >> 10; + // auto rest = systickDelta & ((1 << 10)-1); + subsecondTicks += systickDelta & ((1 << 10)-1); + systickDelta = (systickDelta >> 10) + (subsecondTicks >> 10); + subsecondTicks &= ((1 << 10)-1); - currentDateTime += std::chrono::seconds(correctedDelta); - uptime += std::chrono::seconds(correctedDelta); + currentDateTime += std::chrono::seconds(systickDelta); + uptime += std::chrono::seconds(systickDelta); auto dp = date::floor<date::days>(currentDateTime); auto time = date::make_time(currentDateTime - dp); @@ -89,15 +82,15 @@ void DateTime::UpdateTime(uint32_t systickCounter) { isHourAlreadyNotified = false; } - if ((minute == 0 || minute == 30) && !isHalfHourAlreadyNotified) { - isHalfHourAlreadyNotified = true; + if (minute%chimePeriod) { + hasChimed = false; + } else if (!hasChimed) { if (systemTask != nullptr) { - systemTask->PushMessage(System::Messages::OnNewHalfHour); + systemTask->PushMessage(System::Messages::OnChime); + hasChimed = true; } - } else if (minute != 0 && minute != 30) { - isHalfHourAlreadyNotified = false; } - + // Notify new day to SystemTask if (hour == 0 and not isMidnightAlreadyNotified) { isMidnightAlreadyNotified = true; diff --git a/src/components/datetime/DateTimeController.h b/src/components/datetime/DateTimeController.h index 00bbc2e..ca7320c 100644 --- a/src/components/datetime/DateTimeController.h +++ b/src/components/datetime/DateTimeController.h @@ -60,12 +60,17 @@ namespace Pinetime { uint8_t Seconds() const { return second; } + uint8_t Deciseconds() const { + uint16_t x = subsecondTicks; + x *= 10; + return x >> 10; + } const char* MonthShortToString() const; const char* DayOfWeekShortToString() const; static const char* MonthShortToStringLow(Months month); - std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds> CurrentDateTime() const { + std::chrono::time_point<std::chrono::system_clock, std::chrono::seconds> CurrentDateTime() const { return currentDateTime; } std::chrono::seconds Uptime() const { @@ -73,7 +78,7 @@ namespace Pinetime { } void Register(System::SystemTask* systemTask); - void SetCurrentTime(std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds> t); + void SetCurrentTime(std::chrono::time_point<std::chrono::system_clock, std::chrono::seconds> t); std::string FormattedTime(); private: @@ -84,14 +89,18 @@ namespace Pinetime { uint8_t hour = 0; uint8_t minute = 0; uint8_t second = 0; + uint16_t subsecondTicks = 0; - uint32_t previousSystickCounter = 0; - std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds> currentDateTime; + uint32_t previousSystickCounter = 0; // FIXME: This could probably be 16 bits? + std::chrono::time_point<std::chrono::system_clock, std::chrono::seconds> currentDateTime; std::chrono::seconds uptime {0}; bool isMidnightAlreadyNotified = false; bool isHourAlreadyNotified = true; - bool isHalfHourAlreadyNotified = true; + bool hasChimed = true; + public: + uint8_t chimePeriod = 5; + private: System::SystemTask* systemTask = nullptr; Controllers::Settings& settingsController; }; diff --git a/src/components/motion/MotionController.cpp b/src/components/motion/MotionController.cpp index 7dd3212..c57d8aa 100644 --- a/src/components/motion/MotionController.cpp +++ b/src/components/motion/MotionController.cpp @@ -11,6 +11,7 @@ void MotionController::Update(int16_t x, int16_t y, int16_t z, uint32_t nbSteps) service->OnNewMotionValues(x, y, z); } + lastY = this->y; this->x = x; this->y = y; this->z = z; @@ -21,27 +22,8 @@ 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) { - return false; - } else { - lastYForWakeUp = 0; - return false; - } - } - - if (y >= 0) { - lastYForWakeUp = 0; - return false; - } - if (y + 230 < lastYForWakeUp) { - lastYForWakeUp = y; - return true; - } - } - return false; +bool MotionController::ShouldRaiseWake() const { + return x >= -384 && x <= 384 && z <= 0 && y <= -160 && y <= lastY - 160; } bool MotionController::Should_ShakeWake(uint16_t thresh) { @@ -66,6 +48,10 @@ int32_t MotionController::currentShakeSpeed() { return accumulatedspeed; } +bool MotionController::ShouldLowerSleep() const { + return y >= 512 && y >= lastY + 192; +} + void MotionController::IsSensorOk(bool isOk) { isSensorOk = isOk; } diff --git a/src/components/motion/MotionController.h b/src/components/motion/MotionController.h index f80b11b..297db59 100644 --- a/src/components/motion/MotionController.h +++ b/src/components/motion/MotionController.h @@ -25,6 +25,9 @@ namespace Pinetime { int16_t Z() const { return z; } + int16_t LastY() const { + return lastY; + } uint32_t NbSteps() const { return nbSteps; } @@ -36,9 +39,10 @@ namespace Pinetime { return currentTripSteps; } + bool ShouldRaiseWake() const; 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 +58,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 y = 0; + int16_t z = 0; + int16_t lastY = 0; int16_t lastYForWakeUp = 0; bool isSensorOk = false; DeviceTypes deviceType = DeviceTypes::Unknown; @@ -69,4 +74,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 44a1a85..b85b5b4 100644 --- a/src/components/settings/Settings.h +++ b/src/components/settings/Settings.h @@ -10,12 +10,13 @@ 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 ChimesOption : uint8_t { None, Hours, HalfHours, FiveMinutes }; enum class WakeUpMode : uint8_t { SingleTap = 0, DoubleTap = 1, RaiseWrist = 2, Shake = 3, + LowerWrist = 4 }; enum class Colors : uint8_t { White, @@ -172,7 +173,7 @@ namespace Pinetime { } }; - std::bitset<4> getWakeUpModes() const { + std::bitset<5> getWakeUpModes() const { return settings.wakeUpMode; } @@ -210,10 +211,18 @@ namespace Pinetime { return bleRadioEnabled; }; + void SetBluetoothPasskeyEnabled(bool enabled) { + settings.bluetoothPasskeyEnabled = enabled; + }; + + bool GetBluetoothPasskeyEnabled() const { + return settings.bluetoothPasskeyEnabled; + }; + private: Pinetime::Controllers::FS& fs; - static constexpr uint32_t settingsVersion = 0x0003; + static constexpr uint32_t settingsVersion = 0x3505; struct SettingsData { uint32_t version = settingsVersion; uint32_t stepsGoal = 10000; @@ -227,9 +236,11 @@ namespace Pinetime { PineTimeStyle PTS; - std::bitset<4> wakeUpMode {0}; + std::bitset<5> wakeUpMode {0}; uint16_t shakeWakeThreshold = 150; + Controllers::BrightnessController::Levels brightLevel = Controllers::BrightnessController::Levels::Medium; + bool bluetoothPasskeyEnabled = false; }; SettingsData settings; diff --git a/src/components/timer/TimerController.cpp b/src/components/timer/TimerController.cpp index 115aa27..35e3512 100644 --- a/src/components/timer/TimerController.cpp +++ b/src/components/timer/TimerController.cpp @@ -8,7 +8,6 @@ using namespace Pinetime::Controllers; - namespace { void TimerEnd(TimerHandle_t xTimer) { auto controller = static_cast<Pinetime::Controllers::TimerController*>(pvTimerGetTimerID(xTimer)); @@ -16,7 +15,6 @@ namespace { } } - void TimerController::Init() { timerAppTimer = xTimerCreate("timerAppTm", 1, pdFALSE, this, TimerEnd); } @@ -29,37 +27,37 @@ void TimerController::StartTimer(uint32_t duration) { xTimerStart(timerAppTimer, 0); endTicks = currentTicks + durationTicks; timerRunning = true; + overtime = false; } -uint32_t TimerController::GetTimeRemaining() { +int32_t TimerController::GetSecondsRemaining() { if (!timerRunning) { return 0; } auto currentTicks = xTaskGetTickCount(); - - TickType_t deltaTicks = 0; - if (currentTicks > endTicks) { - deltaTicks = 0xffffffff - currentTicks; - deltaTicks += (endTicks + 1); - } else { - deltaTicks = endTicks - currentTicks; - } - - return (static_cast<TickType_t>(deltaTicks) / static_cast<TickType_t>(configTICK_RATE_HZ)) * 1000; + + int32_t deltaTicks = static_cast<int32_t>(endTicks) - static_cast<int32_t>(currentTicks); + + return (deltaTicks / static_cast<int32_t>(configTICK_RATE_HZ)); } void TimerController::StopTimer() { xTimerStop(timerAppTimer, 0); timerRunning = false; + overtime = false; } -bool TimerController::IsRunning() { - return timerRunning; +void TimerController::StopAlerting() { + if (systemTask != nullptr) { + systemTask->PushMessage(System::Messages::StopRinging); + } } + void TimerController::OnTimerEnd() { - timerRunning = false; - if(systemTask != nullptr) + overtime = true; + if (systemTask != nullptr) { systemTask->PushMessage(System::Messages::OnTimerDone); + } } void TimerController::Register(Pinetime::System::SystemTask* systemTask) { diff --git a/src/components/timer/TimerController.h b/src/components/timer/TimerController.h index e44d3d9..8542987 100644 --- a/src/components/timer/TimerController.h +++ b/src/components/timer/TimerController.h @@ -11,20 +11,22 @@ namespace Pinetime { class SystemTask; } namespace Controllers { - + class TimerController { public: TimerController() = default; - + void Init(); - void StartTimer(uint32_t duration); - void StopTimer(); - - uint32_t GetTimeRemaining(); - - bool IsRunning(); + void StopAlerting(); + int32_t GetSecondsRemaining(); + bool IsOvertime() { + return overtime; + } + bool IsRunning() { + return timerRunning; + } void OnTimerEnd(); @@ -35,6 +37,7 @@ namespace Pinetime { TimerHandle_t timerAppTimer; TickType_t endTicks; bool timerRunning = false; + bool overtime = false; }; } }
\ No newline at end of file |
