launcher: first crack at low power 'screensaver' mode
This commit is contained in:
@@ -1,19 +1,27 @@
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <limits.h>
|
||||
#include "watch.h"
|
||||
#include "launcher.h"
|
||||
#include "launcher_config.h"
|
||||
|
||||
LauncherState launcher_state;
|
||||
void * widget_contexts[LAUNCHER_NUM_WIDGETS];
|
||||
const int32_t launcher_screensaver_deadlines[8] = {INT_MAX, 3600, 7200, 21600, 43200, 86400, 172800, 604800};
|
||||
|
||||
void cb_mode_pressed();
|
||||
void cb_light_pressed();
|
||||
void cb_alarm_pressed();
|
||||
void cb_mode_btn_interrupt();
|
||||
void cb_light_btn_interrupt();
|
||||
void cb_alarm_btn_interrupt();
|
||||
void cb_alarm_btn_extwake();
|
||||
void cb_alarm_fired();
|
||||
void cb_tick();
|
||||
|
||||
static inline void _launcher_reset_screensaver_countdown() {
|
||||
// for testing, make the timeout happen 60x faster.
|
||||
launcher_state.screensaver_ticks = launcher_screensaver_deadlines[launcher_state.launcher_settings.bit.screensaver_interval] / 60;
|
||||
}
|
||||
|
||||
void launcher_request_tick_frequency(uint8_t freq) {
|
||||
// FIXME: there is an issue where after changing tick frequencies on a widget switch, something glitchy happens on the next one.
|
||||
watch_rtc_disable_all_periodic_callbacks();
|
||||
launcher_state.subsecond = 0;
|
||||
launcher_state.tick_frequency = freq;
|
||||
@@ -39,10 +47,11 @@ void launcher_move_to_next_widget() {
|
||||
|
||||
void app_init() {
|
||||
memset(&launcher_state, 0, sizeof(launcher_state));
|
||||
|
||||
launcher_state.launcher_settings.bit.led_green_color = 0xF;
|
||||
launcher_state.launcher_settings.bit.button_should_sound = true;
|
||||
watch_date_time date_time = watch_rtc_get_date_time();
|
||||
watch_rtc_set_date_time(date_time);
|
||||
launcher_state.launcher_settings.bit.screensaver_interval = 1;
|
||||
_launcher_reset_screensaver_countdown();
|
||||
}
|
||||
|
||||
void app_wake_from_deep_sleep() {
|
||||
@@ -50,23 +59,27 @@ void app_wake_from_deep_sleep() {
|
||||
}
|
||||
|
||||
void app_setup() {
|
||||
watch_enable_external_interrupts();
|
||||
watch_register_interrupt_callback(BTN_MODE, cb_mode_pressed, INTERRUPT_TRIGGER_BOTH);
|
||||
watch_register_interrupt_callback(BTN_LIGHT, cb_light_pressed, INTERRUPT_TRIGGER_BOTH);
|
||||
watch_register_interrupt_callback(BTN_ALARM, cb_alarm_pressed, INTERRUPT_TRIGGER_BOTH);
|
||||
if (launcher_state.screensaver_ticks != -1) {
|
||||
watch_disable_extwake_interrupt(BTN_ALARM);
|
||||
|
||||
watch_enable_buzzer();
|
||||
watch_enable_leds();
|
||||
watch_enable_display();
|
||||
watch_enable_external_interrupts();
|
||||
watch_register_interrupt_callback(BTN_MODE, cb_mode_btn_interrupt, INTERRUPT_TRIGGER_BOTH);
|
||||
watch_register_interrupt_callback(BTN_LIGHT, cb_light_btn_interrupt, INTERRUPT_TRIGGER_BOTH);
|
||||
watch_register_interrupt_callback(BTN_ALARM, cb_alarm_btn_interrupt, INTERRUPT_TRIGGER_BOTH);
|
||||
|
||||
launcher_request_tick_frequency(1);
|
||||
watch_enable_buzzer();
|
||||
watch_enable_leds();
|
||||
watch_enable_display();
|
||||
|
||||
for(uint8_t i = 0; i < LAUNCHER_NUM_WIDGETS; i++) {
|
||||
widgets[i].setup(&launcher_state.launcher_settings, &widget_contexts[i]);
|
||||
launcher_request_tick_frequency(1);
|
||||
|
||||
for(uint8_t i = 0; i < LAUNCHER_NUM_WIDGETS; i++) {
|
||||
widgets[i].setup(&launcher_state.launcher_settings, &widget_contexts[i]);
|
||||
}
|
||||
|
||||
widgets[0].activate(&launcher_state.launcher_settings, widget_contexts[launcher_state.current_widget]);
|
||||
widgets[0].loop(EVENT_ACTIVATE, &launcher_state.launcher_settings, 0, widget_contexts[launcher_state.current_widget]);
|
||||
}
|
||||
|
||||
widgets[0].activate(&launcher_state.launcher_settings, widget_contexts[launcher_state.current_widget]);
|
||||
widgets[0].loop(EVENT_ACTIVATE, &launcher_state.launcher_settings, 0, widget_contexts[launcher_state.current_widget]);
|
||||
}
|
||||
|
||||
void app_prepare_for_sleep() {
|
||||
@@ -106,6 +119,19 @@ bool app_loop() {
|
||||
}
|
||||
}
|
||||
|
||||
// if we have timed out of our screensaver countdown, enter screensaver mode.
|
||||
if (launcher_state.screensaver_ticks == 0) {
|
||||
launcher_state.screensaver_ticks = -1;
|
||||
watch_date_time alarm_time;
|
||||
alarm_time.reg = 0;
|
||||
alarm_time.unit.second = 59; // after a match, the alarm fires at the next rising edge of CLK_RTC_CNT, so 59 seconds lets us update at :00
|
||||
watch_rtc_register_alarm_callback(cb_alarm_fired, alarm_time, ALARM_MATCH_SS);
|
||||
watch_register_extwake_callback(BTN_ALARM, cb_alarm_btn_extwake, true);
|
||||
widgets[launcher_state.current_widget].loop(EVENT_SCREENSAVER, &launcher_state.launcher_settings, 0, widget_contexts[launcher_state.current_widget]);
|
||||
event = EVENT_SCREENSAVER;
|
||||
watch_enter_shallow_sleep(true);
|
||||
}
|
||||
|
||||
if (event) {
|
||||
widgets[launcher_state.current_widget].loop(event, &launcher_state.launcher_settings, launcher_state.subsecond, widget_contexts[launcher_state.current_widget]);
|
||||
event = 0;
|
||||
@@ -130,23 +156,38 @@ LauncherEvent _figure_out_button_event(LauncherEvent button_down_event, uint8_t
|
||||
}
|
||||
}
|
||||
|
||||
void cb_light_pressed() {
|
||||
void cb_light_btn_interrupt() {
|
||||
_launcher_reset_screensaver_countdown();
|
||||
event = _figure_out_button_event(EVENT_LIGHT_BUTTON_DOWN, &launcher_state.light_down_timestamp);
|
||||
}
|
||||
|
||||
void cb_mode_pressed() {
|
||||
void cb_mode_btn_interrupt() {
|
||||
_launcher_reset_screensaver_countdown();
|
||||
event = _figure_out_button_event(EVENT_MODE_BUTTON_DOWN, &launcher_state.mode_down_timestamp);
|
||||
}
|
||||
|
||||
void cb_alarm_pressed() {
|
||||
void cb_alarm_btn_interrupt() {
|
||||
_launcher_reset_screensaver_countdown();
|
||||
event = _figure_out_button_event(EVENT_ALARM_BUTTON_DOWN, &launcher_state.alarm_down_timestamp);
|
||||
}
|
||||
|
||||
void cb_alarm_btn_extwake() {
|
||||
_launcher_reset_screensaver_countdown();
|
||||
// this is a hack: waking from shallow sleep, app_setup does get called, but it happens before we reset our ticks.
|
||||
// need to figure out if there's a better heuristic for determining how we woke up.
|
||||
app_setup();
|
||||
}
|
||||
|
||||
void cb_alarm_fired() {
|
||||
event = EVENT_SCREENSAVER;
|
||||
}
|
||||
|
||||
void cb_tick() {
|
||||
event = EVENT_TICK;
|
||||
watch_date_time date_time = watch_rtc_get_date_time();
|
||||
if (date_time.unit.second != launcher_state.last_second) {
|
||||
if (launcher_state.light_ticks) launcher_state.light_ticks--;
|
||||
if (launcher_state.launcher_settings.bit.screensaver_interval && launcher_state.screensaver_ticks > 0) launcher_state.screensaver_ticks--;
|
||||
|
||||
launcher_state.last_second = date_time.unit.second;
|
||||
launcher_state.subsecond = 0;
|
||||
|
||||
@@ -25,6 +25,7 @@ typedef enum LauncherEvent {
|
||||
EVENT_NONE = 0, // There is no event to report.
|
||||
EVENT_ACTIVATE, // Your widget is entering the foreground.
|
||||
EVENT_TICK, // Most common event type. Your widget is being called from the tick callback.
|
||||
EVENT_SCREENSAVER, // Your widget is being asked to display its output for screensaver mode.
|
||||
EVENT_LIGHT_BUTTON_DOWN, // The light button has been pressed, but not yet released.
|
||||
EVENT_LIGHT_BUTTON_UP, // The light button was pressed and released.
|
||||
EVENT_LIGHT_LONG_PRESS, // The light button was held for >2 seconds, and released.
|
||||
@@ -65,6 +66,9 @@ typedef struct LauncherState {
|
||||
uint8_t mode_down_timestamp;
|
||||
uint8_t alarm_down_timestamp;
|
||||
|
||||
// screensaver countdown
|
||||
int32_t screensaver_ticks;
|
||||
|
||||
// stuff for subsecond tracking
|
||||
uint8_t tick_frequency;
|
||||
uint8_t last_second;
|
||||
|
||||
@@ -27,17 +27,18 @@ void simple_clock_widget_loop(LauncherEvent event, LauncherSettings *settings, u
|
||||
watch_date_time date_time;
|
||||
uint32_t previous_date_time;
|
||||
switch (event) {
|
||||
case EVENT_TICK:
|
||||
case EVENT_ACTIVATE:
|
||||
case EVENT_TICK:
|
||||
case EVENT_SCREENSAVER:
|
||||
date_time = watch_rtc_get_date_time();
|
||||
previous_date_time = *((uint32_t *)context);
|
||||
*((uint32_t *)context) = date_time.reg;
|
||||
|
||||
if (date_time.reg >> 6 == previous_date_time >> 6) {
|
||||
if (date_time.reg >> 6 == previous_date_time >> 6 && event != EVENT_SCREENSAVER) {
|
||||
// everything before seconds is the same, don't waste cycles setting those segments.
|
||||
pos = 8;
|
||||
sprintf(buf, "%02d", date_time.unit.second);
|
||||
} else if (date_time.reg >> 12 == previous_date_time >> 12) {
|
||||
} else if (date_time.reg >> 12 == previous_date_time >> 12 && event != EVENT_SCREENSAVER) {
|
||||
// everything before minutes is the same.
|
||||
pos = 6;
|
||||
sprintf(buf, "%02d%02d", date_time.unit.minute, date_time.unit.second);
|
||||
@@ -54,7 +55,11 @@ void simple_clock_widget_loop(LauncherEvent event, LauncherSettings *settings, u
|
||||
if (date_time.unit.hour == 0) date_time.unit.hour = 12;
|
||||
}
|
||||
pos = 0;
|
||||
sprintf(buf, "%s%2d%2d%02d%02d", weekdays[simple_clock_widget_get_weekday(date_time.unit.year, date_time.unit.month, date_time.unit.day)], date_time.unit.day, date_time.unit.hour, date_time.unit.minute, date_time.unit.second);
|
||||
if (event == EVENT_SCREENSAVER) {
|
||||
sprintf(buf, "%s%2d%2d%02d ", weekdays[simple_clock_widget_get_weekday(date_time.unit.year, date_time.unit.month, date_time.unit.day)], date_time.unit.day, date_time.unit.hour, date_time.unit.minute);
|
||||
} else {
|
||||
sprintf(buf, "%s%2d%2d%02d%02d", weekdays[simple_clock_widget_get_weekday(date_time.unit.year, date_time.unit.month, date_time.unit.day)], date_time.unit.day, date_time.unit.hour, date_time.unit.minute, date_time.unit.second);
|
||||
}
|
||||
}
|
||||
watch_display_string(buf, pos);
|
||||
break;
|
||||
|
||||
Reference in New Issue
Block a user