diff options
author | ConfuSomu | 2021-03-02 21:24:15 -0500 |
---|---|---|
committer | ConfuSomu | 2021-03-02 22:51:55 -0500 |
commit | eb01ae68e4ffc5850b1b1182844ae4e60611fe0e (patch) | |
tree | f8454457238dc8d318dd5f8d19823e6882fa2d28 | |
parent | a304e37c5fb70bb465165105f484a9b2f2c5393f (diff) | |
download | pico-watch-eb01ae68e4ffc5850b1b1182844ae4e60611fe0e.tar pico-watch-eb01ae68e4ffc5850b1b1182844ae4e60611fe0e.tar.gz pico-watch-eb01ae68e4ffc5850b1b1182844ae4e60611fe0e.zip |
Enter shallow sleep when there are no interactions
Current app stops getting refreshed and display is turned off.
-rw-r--r-- | api.cpp | 4 | ||||
-rw-r--r-- | api.hpp | 6 | ||||
-rw-r--r-- | buttons.cpp | 11 | ||||
-rw-r--r-- | buttons.hpp | 4 | ||||
-rw-r--r-- | init.hpp | 3 | ||||
-rw-r--r-- | pico-watch.cpp | 19 |
6 files changed, 37 insertions, 10 deletions
@@ -22,6 +22,10 @@ void Api::init_display() { //oledSetTextWrap(&oled, true); } +void Api::display_power(bool mode) { + oledPower(&m_oled, mode); +} + int Api::display_write_string(int iScrollX, int x, int y, char *szMsg, int iSize, int bInvert, int bRender) { return oledWriteString(&m_oled, iScrollX, x, y, szMsg, iSize, bInvert, bRender); } @@ -20,9 +20,13 @@ class Api { enum perf_modes { LOW_POWER, NORMAL_PERF, - HIGH_PERF + HIGH_PERF, + ENTER_SHALLOW_SLEEP, + EXIT_SHALLOW_SLEEP // Restore perf setting }; void init(); + // Control the display's power (on or off) + void display_power(bool mode); int display_write_string(int iScrollX, int x, int y, char *szMsg, int iSize, int bInvert, int bRender); void display_fill(unsigned char ucData, int bRender); void display_draw_line(int x1, int y1, int x2, int y2, int bRender); diff --git a/buttons.cpp b/buttons.cpp index cd94504..7ecc8a4 100644 --- a/buttons.cpp +++ b/buttons.cpp @@ -11,20 +11,19 @@ extern Api app_api; // Debounce control // See https://www.raspberrypi.org/forums/viewtopic.php?f=145&t=301522#p1812063 -// time is currently shared between all buttons. -unsigned long button_time; -const int button_delayTime = 50; // 50ms worked fine for me .... change it to your needs/specs. +unsigned long button_last_pressed_time; +const int button_delay_time = 50; // 50ms worked fine for me .... change it to your needs/specs. //const uint BUTTON_PINS[] = {BUTTON_HOME, BUTTON_SELECT, BUTTON_MODE, BUTTON_UP, BUTTON_DOWN}; void gpio_interrupt_cb(uint gpio, uint32_t events) { - if ((to_ms_since_boot(get_absolute_time())-button_time)>button_delayTime) { + if ((to_ms_since_boot(get_absolute_time())-button_last_pressed_time)>button_delay_time) { if (gpio == BUTTON_HOME && (current_app != 0)) // Home app app_switch(current_app, 0); else app_btnpressed(current_app, gpio); app_api.button_last_set(gpio); - button_time = to_ms_since_boot(get_absolute_time()); + button_last_pressed_time = to_ms_since_boot(get_absolute_time()); } } @@ -53,5 +52,5 @@ void init_buttons() { gpio_set_irq_enabled_with_callback(button, GPIO_IRQ_EDGE_FALL , true, &gpio_interrupt_cb); } */ - button_time = to_ms_since_boot(get_absolute_time()); + button_last_pressed_time = to_ms_since_boot(get_absolute_time()); } diff --git a/buttons.hpp b/buttons.hpp index 43d2d25..3136a26 100644 --- a/buttons.hpp +++ b/buttons.hpp @@ -11,6 +11,10 @@ #define BUTTON_DOWN 20 #define BUTTON_UP 21 +// time is currently shared between all buttons. +extern unsigned long button_last_pressed_time; +extern const int button_delay_time; + void init_buttons(); void gpio_interrupt_cb(uint gpio, uint32_t events); @@ -19,6 +19,9 @@ #define INIT_DATETIME_MIN 45 #define INIT_DATETIME_SEC 00 +// Time (ms) before entering sleep +#define ENTER_SLEEP_DELAY 10000 + // Init every componement void init_all(); diff --git a/pico-watch.cpp b/pico-watch.cpp index c6a3aa8..9e22687 100644 --- a/pico-watch.cpp +++ b/pico-watch.cpp @@ -11,6 +11,7 @@ #include "apps/home_menu.hpp" int current_app = 0; +bool is_sleeping = false; bool app_ready = true; Api app_api; @@ -55,7 +56,19 @@ int app_bgrefresh(int app_id) { return (*APPS_FUNC_BGREFRESH[app_id])(&app_api, app_id==current_app); } -bool apps_bgrefresh(struct repeating_timer *t) { // TODO: Refresh done on core1 +bool repeating_callback(struct repeating_timer *t) { + // Enter shallow sleep mode when needed + uint32_t time_since_last_press = to_ms_since_boot(get_absolute_time())-button_last_pressed_time; + if (!is_sleeping && time_since_last_press > ENTER_SLEEP_DELAY) { + is_sleeping = true; + app_api.performance_set(Api::perf_modes::ENTER_SHALLOW_SLEEP); + app_api.display_power(false); + } else if (is_sleeping && time_since_last_press < ENTER_SLEEP_DELAY) { + is_sleeping = false; + app_api.performance_set(Api::perf_modes::EXIT_SHALLOW_SLEEP); + app_api.display_power(true); + } + // Refresh each app, but should it be done when sleeping? for (int i=0; i < NUMBER_OF_APPS; i++) { app_bgrefresh(i); } @@ -77,12 +90,12 @@ int main() { init_buttons(); app_api.init(); struct repeating_timer timer; - add_repeating_timer_ms(250, apps_bgrefresh, NULL, &timer); + add_repeating_timer_ms(250, repeating_callback, NULL, &timer); // TODO: Execute on core1 app_init(current_app); while (1) { - if (app_ready) + if (app_ready && !is_sleeping) app_render(current_app); // FIXME: This may cause race conditions when switching app sleep_ms(app_api.performance_render_interval_get()); } |