Skip to content

Commit

Permalink
Ported last watchface to OswAppV2
Browse files Browse the repository at this point in the history
Added minor crash protection
  • Loading branch information
simonmicro committed Nov 24, 2023
1 parent db1390a commit ded0980
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 32 deletions.
1 change: 0 additions & 1 deletion include/apps/watchfaces/OswAppWatchface.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ class OswAppWatchface : public OswAppV2 {
#ifdef OSW_FEATURE_STATS_STEPS
static void drawStepHistory(OswUI* ui, uint8_t x, uint8_t y, uint8_t w, uint8_t h, uint32_t max);
#endif
static void handleButtonDefaults();
static void addButtonDefaults(std::array<ButtonStateNames, BTN_NUMBER>& knownButtonStates);
static bool onButtonDefaults(OswAppV2& app, Button id, bool up, ButtonStateNames state);
private:
Expand Down
22 changes: 12 additions & 10 deletions include/apps/watchfaces/OswAppWatchfaceNumerals.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,21 @@
#include <osw_hal.h>
#include <osw_ui.h>

#include <OswAppV1.h>
#include <OswAppV2.h>

class OswAppWatchfaceNumerals : public OswApp {
class OswAppWatchfaceNumerals : public OswAppV2 {
public:
OswAppWatchfaceNumerals(void) {
ui = OswUI::getInstance();
};
virtual void setup() override;
virtual void loop() override;
virtual void stop() override;
~OswAppWatchfaceNumerals() {};
constexpr static const char* APP_ID = "osw.wf.nmrls";

const char* getAppId() override;
const char* getAppName() override;

void onStart() override;
void onLoop() override;
void onDraw() override;
void onButton(Button id, bool up, ButtonStateNames state) override;
private:
OswUI* ui;
time_t lastTime = 0;

void drawWatch();
};
3 changes: 3 additions & 0 deletions include/locales/en-US.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@
#ifndef LANG_MONO
#define LANG_MONO "Mono"
#endif
#ifndef LANG_NUMERALS
#define LANG_NUMERALS "Numerals"
#endif

// App: Time from web
#ifndef LANG_TFW_UPDATE
Expand Down
18 changes: 5 additions & 13 deletions src/apps/watchfaces/OswAppWatchface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,18 +137,6 @@ void OswAppWatchface::onStart() {
#endif
}

/**
* @brief Implements the default behavior - the same on all watchfaces!
*
*/
void OswAppWatchface::handleButtonDefaults() {
OSW_LOG_W("TODO remove this function!"); // TODO ;)
if (OswHal::getInstance()->btnHasGoneDown(BUTTON_3))
OswHal::getInstance()->increaseBrightness(25);
if (OswHal::getInstance()->btnHasGoneDown(BUTTON_2))
OswHal::getInstance()->decreaseBrightness(25);
}

void OswAppWatchface::onLoop() {
OswAppV2::onLoop();

Expand Down Expand Up @@ -195,7 +183,11 @@ bool OswAppWatchface::onButtonDefaults(OswAppV2& app, Button id, bool up, OswApp
} else if(state == OswAppV2::ButtonStateNames::LONG_PRESS and id == Button::BUTTON_DOWN and OswConfigAllKeys::settingDisplayDefaultWatchface.get() != app.getAppId()) {
OSW_LOG_I("Setting default watchface to: ", app.getAppId());
OswConfig::getInstance()->enableWrite();
OswConfigAllKeys::settingDisplayDefaultWatchface.set(app.getAppId());
try {
OswConfigAllKeys::settingDisplayDefaultWatchface.set(app.getAppId()); // if this app-id is not part of the list this may throw an exception
} catch(const std::invalid_argument& e) {
OSW_LOG_E("Failed to set default watchface: ", e.what());
}
OswConfig::getInstance()->disableWrite();
return true;
}
Expand Down
33 changes: 28 additions & 5 deletions src/apps/watchfaces/OswAppWatchfaceNumerals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,18 @@
#include "apps/watchfaces/OswAppWatchface.h"

#include <gfx_util.h>
#include <OswAppV1.h>
#include <osw_config.h>
#include <osw_config_keys.h>
#include <osw_hal.h>

const char* OswAppWatchfaceNumerals::getAppId() {
return OswAppWatchfaceNumerals::APP_ID;
}

const char* OswAppWatchfaceNumerals::getAppName() {
return LANG_NUMERALS;
}

void OswAppWatchfaceNumerals::drawWatch() {
OswHal* hal = OswHal::getInstance();

Expand Down Expand Up @@ -85,11 +92,27 @@ void OswAppWatchfaceNumerals::drawWatch() {
#endif
}

void OswAppWatchfaceNumerals::setup() {}
void OswAppWatchfaceNumerals::onStart() {
OswAppV2::onStart();
OswAppWatchface::addButtonDefaults(this->knownButtonStates);
}

void OswAppWatchfaceNumerals::onLoop() {
OswAppV2::onLoop();

this->needsRedraw = this->needsRedraw or time(nullptr) != this->lastTime; // redraw every second
}

void OswAppWatchfaceNumerals::onDraw() {
OswAppV2::onDraw();

void OswAppWatchfaceNumerals::loop() {
OswAppWatchface::handleButtonDefaults();
drawWatch();

this->lastTime = time(nullptr);
}

void OswAppWatchfaceNumerals::stop() {}
void OswAppWatchfaceNumerals::onButton(Button id, bool up, OswAppV2::ButtonStateNames state) {
OswAppV2::onButton(id, up, state);
if(OswAppWatchface::onButtonDefaults(*this, id, up, state))
return; // if the button was handled by the defaults, we are done here
}
3 changes: 1 addition & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,15 +113,14 @@ void setup() {
}

// TODO port all v1 watchfaces to v2, to allow for lazy loading
static OswAppWatchfaceNumerals watchfaceNumerals;
main_mainDrawer.registerAppLazy<OswAppWatchface>(LANG_WATCHFACES);
main_mainDrawer.registerAppLazy<OswAppWatchfaceDigital>(LANG_WATCHFACES);
main_mainDrawer.registerAppLazy<OswAppWatchfaceMix>(LANG_WATCHFACES);
main_mainDrawer.registerAppLazy<OswAppWatchfaceDual>(LANG_WATCHFACES);
main_mainDrawer.registerAppLazy<OswAppWatchfaceFitness>(LANG_WATCHFACES);
main_mainDrawer.registerAppLazy<OswAppWatchfaceBinary>(LANG_WATCHFACES);
main_mainDrawer.registerAppLazy<OswAppWatchfaceMonotimer>(LANG_WATCHFACES);
main_mainDrawer.registerApp(LANG_WATCHFACES, new OswAppV2Compat("osw.wf.nmrls", "Numerals", watchfaceNumerals));
main_mainDrawer.registerAppLazy<OswAppWatchfaceNumerals>(LANG_WATCHFACES);
try {
main_mainDrawer.startApp(OswConfigAllKeys::settingDisplayDefaultWatchface.get().c_str()); // if this id is invalid, the drawer will fall back to alternatives automatically
} catch(const std::runtime_error& e) {
Expand Down
4 changes: 3 additions & 1 deletion src/osw_config_keys.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "apps/watchfaces/OswAppWatchfaceFitness.h"
#include "apps/watchfaces/OswAppWatchfaceBinary.h"
#include "apps/watchfaces/OswAppWatchfaceMonotimer.h"
#include "apps/watchfaces/OswAppWatchfaceNumerals.h"

/**
* !!!WARNING!!!
Expand Down Expand Up @@ -61,7 +62,8 @@ OswConfigKeyDropDown settingDisplayDefaultWatchface("n", "Display", "Default Wat
OswAppWatchfaceDual::APP_ID,
OswAppWatchfaceFitness::APP_ID,
OswAppWatchfaceBinary::APP_ID,
OswAppWatchfaceMonotimer::APP_ID
OswAppWatchfaceMonotimer::APP_ID,
OswAppWatchfaceNumerals::APP_ID
}, CONFIG_DEFAULT_WATCHFACE_ID);
OswConfigKeyBool settingDisplayDualHourTick("h2", "Display", "Display Dual-Time Hour Tick", "Show dual time hour tick", false);
#if OSW_PLATFORM_ENVIRONMENT_ACCELEROMETER == 1
Expand Down

0 comments on commit ded0980

Please sign in to comment.