Skip to content

Commit

Permalink
dirtyvalue: Move to src/utility
Browse files Browse the repository at this point in the history
  • Loading branch information
FintasticMan authored and Riksu9000 committed Mar 27, 2023
1 parent 47931f4 commit 616aa91
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 92 deletions.
35 changes: 0 additions & 35 deletions src/displayapp/screens/Screen.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,41 +9,6 @@ namespace Pinetime {
class DisplayApp;

namespace Screens {

template <class T>
class DirtyValue {
public:
DirtyValue() = default; // Use NSDMI

explicit DirtyValue(T const& v) : value {v} {
} // Use MIL and const-lvalue-ref

bool IsUpdated() {
if (this->isUpdated) {
this->isUpdated = false;
return true;
}
return false;
}

T const& Get() {
this->isUpdated = false;
return value;
} // never expose a non-const lvalue-ref

DirtyValue& operator=(const T& other) {
if (this->value != other) {
this->value = other;
this->isUpdated = true;
}
return *this;
}

private:
T value {}; // NSDMI - default initialise type
bool isUpdated {true}; // NSDMI - use brace initialisation
};

class Screen {
private:
virtual void Refresh() {
Expand Down
15 changes: 8 additions & 7 deletions src/displayapp/screens/WatchFaceAnalog.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
#include "components/battery/BatteryController.h"
#include "components/ble/BleController.h"
#include "components/ble/NotificationManager.h"
#include <displayapp/screens/BatteryIcon.h>
#include "displayapp/screens/BatteryIcon.h"
#include "utility/DirtyValue.h"

namespace Pinetime {
namespace Controllers {
Expand Down Expand Up @@ -37,13 +38,13 @@ namespace Pinetime {
private:
uint8_t sHour, sMinute, sSecond;

DirtyValue<uint8_t> batteryPercentRemaining {0};
DirtyValue<bool> isCharging {};
DirtyValue<bool> bleState {};
DirtyValue<std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds>> currentDateTime;
DirtyValue<bool> notificationState {false};
Utility::DirtyValue<uint8_t> batteryPercentRemaining {0};
Utility::DirtyValue<bool> isCharging {};
Utility::DirtyValue<bool> bleState {};
Utility::DirtyValue<std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds>> currentDateTime;
Utility::DirtyValue<bool> notificationState {false};
using days = std::chrono::duration<int32_t, std::ratio<86400>>; // TODO: days is standard in c++20
DirtyValue<std::chrono::time_point<std::chrono::system_clock, days>> currentDate;
Utility::DirtyValue<std::chrono::time_point<std::chrono::system_clock, days>> currentDate;

lv_obj_t* hour_body;
lv_obj_t* hour_body_trace;
Expand Down
21 changes: 11 additions & 10 deletions src/displayapp/screens/WatchFaceCasioStyleG7710.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "displayapp/screens/Screen.h"
#include "components/datetime/DateTimeController.h"
#include "components/ble/BleController.h"
#include "utility/DirtyValue.h"

namespace Pinetime {
namespace Controllers {
Expand Down Expand Up @@ -47,16 +48,16 @@ namespace Pinetime {
Controllers::DateTime::Days currentDayOfWeek = Pinetime::Controllers::DateTime::Days::Unknown;
uint8_t currentDay = 0;

DirtyValue<uint8_t> batteryPercentRemaining {};
DirtyValue<bool> powerPresent {};
DirtyValue<bool> bleState {};
DirtyValue<bool> bleRadioEnabled {};
DirtyValue<std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds>> currentDateTime {};
DirtyValue<bool> motionSensorOk {};
DirtyValue<uint32_t> stepCount {};
DirtyValue<uint8_t> heartbeat {};
DirtyValue<bool> heartbeatRunning {};
DirtyValue<bool> notificationState {};
Utility::DirtyValue<uint8_t> batteryPercentRemaining {};
Utility::DirtyValue<bool> powerPresent {};
Utility::DirtyValue<bool> bleState {};
Utility::DirtyValue<bool> bleRadioEnabled {};
Utility::DirtyValue<std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds>> currentDateTime {};
Utility::DirtyValue<bool> motionSensorOk {};
Utility::DirtyValue<uint32_t> stepCount {};
Utility::DirtyValue<uint8_t> heartbeat {};
Utility::DirtyValue<bool> heartbeatRunning {};
Utility::DirtyValue<bool> notificationState {};

lv_point_t line_icons_points[3] {{0, 5}, {117, 5}, {122, 0}};
lv_point_t line_day_of_week_number_points[4] {{0, 0}, {100, 0}, {95, 95}, {0, 95}};
Expand Down
21 changes: 11 additions & 10 deletions src/displayapp/screens/WatchFaceDigital.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "components/datetime/DateTimeController.h"
#include "components/ble/BleController.h"
#include "displayapp/widgets/StatusIcons.h"
#include "utility/DirtyValue.h"

namespace Pinetime {
namespace Controllers {
Expand Down Expand Up @@ -44,16 +45,16 @@ namespace Pinetime {
Controllers::DateTime::Days currentDayOfWeek = Pinetime::Controllers::DateTime::Days::Unknown;
uint8_t currentDay = 0;

DirtyValue<uint8_t> batteryPercentRemaining {};
DirtyValue<bool> powerPresent {};
DirtyValue<bool> bleState {};
DirtyValue<bool> bleRadioEnabled {};
DirtyValue<std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds>> currentDateTime {};
DirtyValue<bool> motionSensorOk {};
DirtyValue<uint32_t> stepCount {};
DirtyValue<uint8_t> heartbeat {};
DirtyValue<bool> heartbeatRunning {};
DirtyValue<bool> notificationState {};
Utility::DirtyValue<uint8_t> batteryPercentRemaining {};
Utility::DirtyValue<bool> powerPresent {};
Utility::DirtyValue<bool> bleState {};
Utility::DirtyValue<bool> bleRadioEnabled {};
Utility::DirtyValue<std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds>> currentDateTime {};
Utility::DirtyValue<bool> motionSensorOk {};
Utility::DirtyValue<uint32_t> stepCount {};
Utility::DirtyValue<uint8_t> heartbeat {};
Utility::DirtyValue<bool> heartbeatRunning {};
Utility::DirtyValue<bool> notificationState {};

lv_obj_t* label_time;
lv_obj_t* label_time_ampm;
Expand Down
17 changes: 9 additions & 8 deletions src/displayapp/screens/WatchFaceInfineat.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <memory>
#include "displayapp/screens/Screen.h"
#include "components/datetime/DateTimeController.h"
#include "utility/DirtyValue.h"

namespace Pinetime {
namespace Controllers {
Expand Down Expand Up @@ -51,14 +52,14 @@ namespace Pinetime {
uint32_t savedTick = 0;
uint8_t chargingBatteryPercent = 101; // not a mistake ;)

DirtyValue<uint8_t> batteryPercentRemaining {};
DirtyValue<bool> isCharging {};
DirtyValue<bool> bleState {};
DirtyValue<bool> bleRadioEnabled {};
DirtyValue<std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds>> currentDateTime {};
DirtyValue<bool> motionSensorOk {};
DirtyValue<uint32_t> stepCount {};
DirtyValue<bool> notificationState {};
Utility::DirtyValue<uint8_t> batteryPercentRemaining {};
Utility::DirtyValue<bool> isCharging {};
Utility::DirtyValue<bool> bleState {};
Utility::DirtyValue<bool> bleRadioEnabled {};
Utility::DirtyValue<std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds>> currentDateTime {};
Utility::DirtyValue<bool> motionSensorOk {};
Utility::DirtyValue<uint32_t> stepCount {};
Utility::DirtyValue<bool> notificationState {};

// Lines making up the side cover
lv_obj_t* lineBattery;
Expand Down
17 changes: 9 additions & 8 deletions src/displayapp/screens/WatchFacePineTimeStyle.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "displayapp/Colors.h"
#include "components/datetime/DateTimeController.h"
#include "components/ble/BleController.h"
#include "utility/DirtyValue.h"

namespace Pinetime {
namespace Controllers {
Expand Down Expand Up @@ -50,14 +51,14 @@ namespace Pinetime {
uint8_t currentDay = 0;
uint32_t savedTick = 0;

DirtyValue<uint8_t> batteryPercentRemaining {};
DirtyValue<bool> isCharging {};
DirtyValue<bool> bleState {};
DirtyValue<bool> bleRadioEnabled {};
DirtyValue<std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds>> currentDateTime {};
DirtyValue<bool> motionSensorOk {};
DirtyValue<uint32_t> stepCount {};
DirtyValue<bool> notificationState {};
Utility::DirtyValue<uint8_t> batteryPercentRemaining {};
Utility::DirtyValue<bool> isCharging {};
Utility::DirtyValue<bool> bleState {};
Utility::DirtyValue<bool> bleRadioEnabled {};
Utility::DirtyValue<std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds>> currentDateTime {};
Utility::DirtyValue<bool> motionSensorOk {};
Utility::DirtyValue<uint32_t> stepCount {};
Utility::DirtyValue<bool> notificationState {};

static Pinetime::Controllers::Settings::Colors GetNext(Controllers::Settings::Colors color);
static Pinetime::Controllers::Settings::Colors GetPrevious(Controllers::Settings::Colors color);
Expand Down
21 changes: 11 additions & 10 deletions src/displayapp/screens/WatchFaceTerminal.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <memory>
#include "displayapp/screens/Screen.h"
#include "components/datetime/DateTimeController.h"
#include "utility/DirtyValue.h"

namespace Pinetime {
namespace Controllers {
Expand Down Expand Up @@ -43,16 +44,16 @@ namespace Pinetime {
Pinetime::Controllers::DateTime::Days currentDayOfWeek = Pinetime::Controllers::DateTime::Days::Unknown;
uint8_t currentDay = 0;

DirtyValue<int> batteryPercentRemaining {};
DirtyValue<bool> powerPresent {};
DirtyValue<bool> bleState {};
DirtyValue<bool> bleRadioEnabled {};
DirtyValue<std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds>> currentDateTime {};
DirtyValue<bool> motionSensorOk {};
DirtyValue<uint32_t> stepCount {};
DirtyValue<uint8_t> heartbeat {};
DirtyValue<bool> heartbeatRunning {};
DirtyValue<bool> notificationState {};
Utility::DirtyValue<int> batteryPercentRemaining {};
Utility::DirtyValue<bool> powerPresent {};
Utility::DirtyValue<bool> bleState {};
Utility::DirtyValue<bool> bleRadioEnabled {};
Utility::DirtyValue<std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds>> currentDateTime {};
Utility::DirtyValue<bool> motionSensorOk {};
Utility::DirtyValue<uint32_t> stepCount {};
Utility::DirtyValue<uint8_t> heartbeat {};
Utility::DirtyValue<bool> heartbeatRunning {};
Utility::DirtyValue<bool> notificationState {};

lv_obj_t* label_time;
lv_obj_t* label_date;
Expand Down
9 changes: 5 additions & 4 deletions src/displayapp/widgets/StatusIcons.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include "components/battery/BatteryController.h"
#include "components/ble/BleController.h"
#include "displayapp/screens/BatteryIcon.h"
#include "utility/DirtyValue.h"

namespace Pinetime {
namespace Applications {
Expand All @@ -27,10 +28,10 @@ namespace Pinetime {
const Pinetime::Controllers::Battery& batteryController;
const Controllers::Ble& bleController;

Screens::DirtyValue<uint8_t> batteryPercentRemaining {};
Screens::DirtyValue<bool> powerPresent {};
Screens::DirtyValue<bool> bleState {};
Screens::DirtyValue<bool> bleRadioEnabled {};
Utility::DirtyValue<uint8_t> batteryPercentRemaining {};
Utility::DirtyValue<bool> powerPresent {};
Utility::DirtyValue<bool> bleState {};
Utility::DirtyValue<bool> bleRadioEnabled {};

lv_obj_t* bleIcon;
lv_obj_t* batteryPlug;
Expand Down
39 changes: 39 additions & 0 deletions src/utility/DirtyValue.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#pragma once

namespace Pinetime {
namespace Utility {
template <class T>
class DirtyValue {
public:
DirtyValue() = default; // Use NSDMI

explicit DirtyValue(T const& v) : value {v} {
} // Use MIL and const-lvalue-ref

bool IsUpdated() {
if (this->isUpdated) {
this->isUpdated = false;
return true;
}
return false;
}

T const& Get() {
this->isUpdated = false;
return value;
} // never expose a non-const lvalue-ref

DirtyValue& operator=(const T& other) {
if (this->value != other) {
this->value = other;
this->isUpdated = true;
}
return *this;
}

private:
T value {}; // NSDMI - default initialise type
bool isUpdated {true}; // NSDMI - use brace initialisation
};
}
}

0 comments on commit 616aa91

Please sign in to comment.