diff options
Diffstat (limited to 'src/components')
-rw-r--r-- | src/components/ble/NotificationManager.cpp | 145 | ||||
-rw-r--r-- | src/components/ble/NotificationManager.h | 31 | ||||
-rw-r--r-- | src/components/brightness/BrightnessController.cpp | 10 | ||||
-rw-r--r-- | src/components/brightness/BrightnessController.h | 4 | ||||
-rw-r--r-- | src/components/settings/Settings.h | 4 |
5 files changed, 118 insertions, 76 deletions
diff --git a/src/components/ble/NotificationManager.cpp b/src/components/ble/NotificationManager.cpp index ec99c4ed..223f6221 100644 --- a/src/components/ble/NotificationManager.cpp +++ b/src/components/ble/NotificationManager.cpp @@ -1,6 +1,7 @@ #include "components/ble/NotificationManager.h" #include <cstring> #include <algorithm> +#include <cassert> using namespace Pinetime::Controllers; @@ -9,73 +10,117 @@ constexpr uint8_t NotificationManager::MessageSize; void NotificationManager::Push(NotificationManager::Notification&& notif) { notif.id = GetNextId(); notif.valid = true; - notifications[writeIndex] = std::move(notif); - writeIndex = (writeIndex + 1 < TotalNbNotifications) ? writeIndex + 1 : 0; - if (!empty) - readIndex = (readIndex + 1 < TotalNbNotifications) ? readIndex + 1 : 0; - else - empty = false; - newNotification = true; -} - -NotificationManager::Notification NotificationManager::GetLastNotification() { - NotificationManager::Notification notification = notifications[readIndex]; - notification.index = 1; - return notification; + if (beginIdx > 0) { + --beginIdx; + } else { + beginIdx = notifications.size() - 1; + } + notifications[beginIdx] = std::move(notif); + if (size < notifications.size()) { + size++; + } } NotificationManager::Notification::Id NotificationManager::GetNextId() { return nextId++; } -NotificationManager::Notification NotificationManager::GetNext(NotificationManager::Notification::Id id) { - auto currentIterator = std::find_if(notifications.begin(), notifications.end(), [id](const Notification& n) { - return n.valid && n.id == id; - }); - if (currentIterator == notifications.end() || currentIterator->id != id) - return Notification {}; - - auto& lastNotification = notifications[readIndex]; - - NotificationManager::Notification result; - - if (currentIterator == (notifications.end() - 1)) - result = *(notifications.begin()); - else - result = *(currentIterator + 1); - - if (result.id <= id) +NotificationManager::Notification NotificationManager::GetLastNotification() const { + if (this->IsEmpty()) { return {}; + } + return this->At(0); +} - result.index = (lastNotification.id - result.id) + 1; - return result; +const NotificationManager::Notification& NotificationManager::At(NotificationManager::Notification::Idx idx) const { + if (idx >= notifications.size()) { + assert(false); + return notifications.at(beginIdx); // this should not happen + } + size_t read_idx = (beginIdx + idx) % notifications.size(); + return notifications.at(read_idx); } -NotificationManager::Notification NotificationManager::GetPrevious(NotificationManager::Notification::Id id) { - auto currentIterator = std::find_if(notifications.begin(), notifications.end(), [id](const Notification& n) { - return n.valid && n.id == id; - }); - if (currentIterator == notifications.end() || currentIterator->id != id) - return Notification {}; +NotificationManager::Notification& NotificationManager::At(NotificationManager::Notification::Idx idx) { + if (idx >= notifications.size()) { + assert(false); + return notifications.at(beginIdx); // this should not happen + } + size_t read_idx = (beginIdx + idx) % notifications.size(); + return notifications.at(read_idx); +} - auto& lastNotification = notifications[readIndex]; +NotificationManager::Notification::Idx NotificationManager::IndexOf(NotificationManager::Notification::Id id) const { + for (NotificationManager::Notification::Idx idx = 0; idx < this->size; idx++) { + const NotificationManager::Notification& notification = this->At(idx); + if (notification.id == id) { + return idx; + } + } + return size; +} - NotificationManager::Notification result; +NotificationManager::Notification NotificationManager::Get(NotificationManager::Notification::Id id) const { + NotificationManager::Notification::Idx idx = this->IndexOf(id); + if (idx == this->size) { + return {}; + } + return this->At(idx); +} - if (currentIterator == notifications.begin()) - result = *(notifications.end() - 1); - else - result = *(currentIterator - 1); +NotificationManager::Notification NotificationManager::GetNext(NotificationManager::Notification::Id id) const { + NotificationManager::Notification::Idx idx = this->IndexOf(id); + if (idx == this->size) { + return {}; + } + if (idx == 0 || idx > notifications.size()) { + return {}; + } + return this->At(idx - 1); +} - if (result.id >= id) +NotificationManager::Notification NotificationManager::GetPrevious(NotificationManager::Notification::Id id) const { + NotificationManager::Notification::Idx idx = this->IndexOf(id); + if (idx == this->size) { + return {}; + } + if (static_cast<size_t>(idx + 1) >= notifications.size()) { return {}; + } + return this->At(idx + 1); +} + +void NotificationManager::DismissIdx(NotificationManager::Notification::Idx idx) { + if (this->IsEmpty()) { + return; + } + if (idx >= size) { + assert(false); + return; // this should not happen + } + if (idx == 0) { // just remove the first element, don't need to change the other elements + notifications.at(beginIdx).valid = false; + beginIdx = (beginIdx + 1) % notifications.size(); + } else { + // overwrite the specified entry by moving all later messages one index to the front + for (size_t i = idx; i < size - 1; ++i) { + this->At(i) = this->At(i + 1); + } + this->At(size - 1).valid = false; + } + --size; +} - result.index = (lastNotification.id - result.id) + 1; - return result; +void NotificationManager::Dismiss(NotificationManager::Notification::Id id) { + NotificationManager::Notification::Idx idx = this->IndexOf(id); + if (idx == this->size) { + return; + } + this->DismissIdx(idx); } -bool NotificationManager::AreNewNotificationsAvailable() { +bool NotificationManager::AreNewNotificationsAvailable() const { return newNotification; } @@ -84,9 +129,7 @@ bool NotificationManager::ClearNewNotificationFlag() { } size_t NotificationManager::NbNotifications() const { - return std::count_if(notifications.begin(), notifications.end(), [](const Notification& n) { - return n.valid; - }); + return size; } const char* NotificationManager::Notification::Message() const { diff --git a/src/components/ble/NotificationManager.h b/src/components/ble/NotificationManager.h index 40f174ea..4c199dbf 100644 --- a/src/components/ble/NotificationManager.h +++ b/src/components/ble/NotificationManager.h @@ -26,9 +26,9 @@ namespace Pinetime { struct Notification { using Id = uint8_t; - Id id; + using Idx = uint8_t; + Id id = 0; bool valid = false; - uint8_t index; uint8_t size; std::array<char, MessageSize + 1> message; Categories category = Categories::Unknown; @@ -36,27 +36,38 @@ namespace Pinetime { const char* Message() const; const char* Title() const; }; - Notification::Id nextId {0}; void Push(Notification&& notif); - Notification GetLastNotification(); - Notification GetNext(Notification::Id id); - Notification GetPrevious(Notification::Id id); + Notification GetLastNotification() const; + Notification Get(Notification::Id id) const; + Notification GetNext(Notification::Id id) const; + Notification GetPrevious(Notification::Id id) const; + // Return the index of the notification with the specified id, if not found return NbNotifications() + Notification::Idx IndexOf(Notification::Id id) const; bool ClearNewNotificationFlag(); - bool AreNewNotificationsAvailable(); + bool AreNewNotificationsAvailable() const; + void Dismiss(Notification::Id id); static constexpr size_t MaximumMessageSize() { return MessageSize; }; + bool IsEmpty() const { + return size == 0; + } size_t NbNotifications() const; private: + Notification::Id nextId {0}; Notification::Id GetNextId(); + const Notification& At(Notification::Idx idx) const; + Notification& At(Notification::Idx idx); + void DismissIdx(Notification::Idx idx); + static constexpr uint8_t TotalNbNotifications = 5; std::array<Notification, TotalNbNotifications> notifications; - uint8_t readIndex = 0; - uint8_t writeIndex = 0; - bool empty = true; + size_t beginIdx = TotalNbNotifications - 1; // index of the newest notification + size_t size = 0; // number of valid notifications in buffer + std::atomic<bool> newNotification {false}; }; } diff --git a/src/components/brightness/BrightnessController.cpp b/src/components/brightness/BrightnessController.cpp index 2d9f980a..0392158c 100644 --- a/src/components/brightness/BrightnessController.cpp +++ b/src/components/brightness/BrightnessController.cpp @@ -74,14 +74,6 @@ BrightnessController::Levels BrightnessController::Level() const { return level; } -void BrightnessController::Backup() { - backupLevel = level; -} - -void BrightnessController::Restore() { - Set(backupLevel); -} - void BrightnessController::Step() { switch (level) { case Levels::Low: @@ -123,4 +115,4 @@ const char* BrightnessController::ToString() { default: return "???"; } -}
\ No newline at end of file +} diff --git a/src/components/brightness/BrightnessController.h b/src/components/brightness/BrightnessController.h index 0d7ac2ff..7f86759a 100644 --- a/src/components/brightness/BrightnessController.h +++ b/src/components/brightness/BrightnessController.h @@ -15,15 +15,11 @@ namespace Pinetime { void Higher(); void Step(); - void Backup(); - void Restore(); - const char* GetIcon(); const char* ToString(); private: Levels level = Levels::High; - Levels backupLevel = Levels::High; }; } } diff --git a/src/components/settings/Settings.h b/src/components/settings/Settings.h index 02d28353..8a246d11 100644 --- a/src/components/settings/Settings.h +++ b/src/components/settings/Settings.h @@ -9,7 +9,7 @@ namespace Pinetime { class Settings { public: enum class ClockType : uint8_t { H24, H12 }; - enum class Notification : uint8_t { ON, OFF }; + enum class Notification : uint8_t { On, Off, Sleep }; enum class ChimesOption : uint8_t { None, Hours, HalfHours }; enum class WakeUpMode : uint8_t { SingleTap = 0, @@ -251,7 +251,7 @@ namespace Pinetime { uint32_t screenTimeOut = 15000; ClockType clockType = ClockType::H24; - Notification notificationStatus = Notification::ON; + Notification notificationStatus = Notification::On; uint8_t clockFace = 0; ChimesOption chimesOption = ChimesOption::None; |