event type / subsecond refactor
This commit is contained in:
parent
a6115de94d
commit
51e2f0d77d
@ -8,6 +8,7 @@
|
||||
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};
|
||||
LauncherEvent event;
|
||||
|
||||
void cb_mode_btn_interrupt();
|
||||
void cb_light_btn_interrupt();
|
||||
@ -38,7 +39,9 @@ void launcher_move_to_widget(uint8_t widget_index) {
|
||||
launcher_state.current_widget = widget_index;
|
||||
watch_clear_display();
|
||||
widgets[launcher_state.current_widget].activate(&launcher_state.launcher_settings, widget_contexts[launcher_state.current_widget]);
|
||||
widgets[launcher_state.current_widget].loop(EVENT_ACTIVATE, &launcher_state.launcher_settings, launcher_state.subsecond, widget_contexts[launcher_state.current_widget]);
|
||||
event.value = 0;
|
||||
event.bit.event_type = EVENT_ACTIVATE;
|
||||
widgets[launcher_state.current_widget].loop(event, &launcher_state.launcher_settings, widget_contexts[launcher_state.current_widget]);
|
||||
}
|
||||
|
||||
void launcher_move_to_next_widget() {
|
||||
@ -79,7 +82,8 @@ void app_setup() {
|
||||
}
|
||||
|
||||
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]);
|
||||
event.value = 0;
|
||||
event.bit.event_type = EVENT_ACTIVATE;
|
||||
}
|
||||
}
|
||||
|
||||
@ -89,8 +93,6 @@ void app_prepare_for_sleep() {
|
||||
void app_wake_from_sleep() {
|
||||
}
|
||||
|
||||
LauncherEvent event;
|
||||
|
||||
bool app_loop() {
|
||||
// play a beep if the widget has changed in response to a user's press of the MODE button
|
||||
if (launcher_state.widget_changed) {
|
||||
@ -128,14 +130,16 @@ bool app_loop() {
|
||||
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;
|
||||
event.value = 0;
|
||||
event.bit.event_type = EVENT_SCREENSAVER;
|
||||
widgets[launcher_state.current_widget].loop(event, &launcher_state.launcher_settings, widget_contexts[launcher_state.current_widget]);
|
||||
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;
|
||||
if (event.bit.event_type) {
|
||||
event.bit.subsecond = launcher_state.subsecond;
|
||||
widgets[launcher_state.current_widget].loop(event, &launcher_state.launcher_settings, widget_contexts[launcher_state.current_widget]);
|
||||
event.value = 0;
|
||||
}
|
||||
|
||||
|
||||
@ -144,7 +148,7 @@ bool app_loop() {
|
||||
return true;
|
||||
}
|
||||
|
||||
LauncherEvent _figure_out_button_event(LauncherEvent button_down_event, uint8_t *down_timestamp) {
|
||||
LauncherEventType _figure_out_button_event(LauncherEventType button_down_event, uint8_t *down_timestamp) {
|
||||
watch_date_time date_time = watch_rtc_get_date_time();
|
||||
if (*down_timestamp) {
|
||||
uint8_t diff = ((61 + date_time.unit.second) - *down_timestamp) % 60;
|
||||
@ -159,17 +163,17 @@ LauncherEvent _figure_out_button_event(LauncherEvent button_down_event, uint8_t
|
||||
|
||||
void cb_light_btn_interrupt() {
|
||||
_launcher_reset_screensaver_countdown();
|
||||
event = _figure_out_button_event(EVENT_LIGHT_BUTTON_DOWN, &launcher_state.light_down_timestamp);
|
||||
event.bit.event_type = _figure_out_button_event(EVENT_LIGHT_BUTTON_DOWN, &launcher_state.light_down_timestamp);
|
||||
}
|
||||
|
||||
void cb_mode_btn_interrupt() {
|
||||
_launcher_reset_screensaver_countdown();
|
||||
event = _figure_out_button_event(EVENT_MODE_BUTTON_DOWN, &launcher_state.mode_down_timestamp);
|
||||
event.bit.event_type = _figure_out_button_event(EVENT_MODE_BUTTON_DOWN, &launcher_state.mode_down_timestamp);
|
||||
}
|
||||
|
||||
void cb_alarm_btn_interrupt() {
|
||||
_launcher_reset_screensaver_countdown();
|
||||
event = _figure_out_button_event(EVENT_ALARM_BUTTON_DOWN, &launcher_state.alarm_down_timestamp);
|
||||
event.bit.event_type = _figure_out_button_event(EVENT_ALARM_BUTTON_DOWN, &launcher_state.alarm_down_timestamp);
|
||||
}
|
||||
|
||||
void cb_alarm_btn_extwake() {
|
||||
@ -180,11 +184,11 @@ void cb_alarm_btn_extwake() {
|
||||
}
|
||||
|
||||
void cb_alarm_fired() {
|
||||
event = EVENT_SCREENSAVER;
|
||||
event.bit.event_type = EVENT_SCREENSAVER;
|
||||
}
|
||||
|
||||
void cb_tick() {
|
||||
event = EVENT_TICK;
|
||||
event.bit.event_type = 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--;
|
||||
|
@ -21,7 +21,7 @@ typedef union {
|
||||
uint32_t value;
|
||||
} LauncherSettings;
|
||||
|
||||
typedef enum LauncherEvent {
|
||||
typedef enum {
|
||||
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.
|
||||
@ -35,11 +35,21 @@ typedef enum LauncherEvent {
|
||||
EVENT_ALARM_BUTTON_DOWN, // The alarm button has been pressed, but not yet released.
|
||||
EVENT_ALARM_BUTTON_UP, // The alarm button was pressed and released.
|
||||
EVENT_ALARM_LONG_PRESS, // The alarm button was held for >2 seconds, and released.
|
||||
} LauncherEventType;
|
||||
|
||||
typedef union {
|
||||
struct {
|
||||
uint32_t event_type : 8;
|
||||
uint32_t subsecond : 8;
|
||||
uint32_t reserved : 16;
|
||||
} bit;
|
||||
uint32_t value;
|
||||
} LauncherEvent;
|
||||
|
||||
|
||||
typedef void (*launcher_widget_setup)(LauncherSettings *settings, void ** context_ptr);
|
||||
typedef void (*launcher_widget_activate)(LauncherSettings *settings, void *context);
|
||||
typedef void (*launcher_widget_loop)(LauncherEvent event, LauncherSettings *settings, uint8_t subsecond, void *context);
|
||||
typedef void (*launcher_widget_loop)(LauncherEvent event, LauncherSettings *settings, void *context);
|
||||
typedef void (*launcher_widget_resign)(LauncherSettings *settings, void *context);
|
||||
|
||||
typedef struct WatchWidget {
|
||||
|
@ -17,16 +17,15 @@ void simple_clock_widget_activate(LauncherSettings *settings, void *context) {
|
||||
*((uint32_t *)context) = 0xFFFFFFFF;
|
||||
}
|
||||
|
||||
void simple_clock_widget_loop(LauncherEvent event, LauncherSettings *settings, uint8_t subsecond, void *context) {
|
||||
void simple_clock_widget_loop(LauncherEvent event, LauncherSettings *settings, void *context) {
|
||||
printf("simple_clock_widget_loop\n");
|
||||
(void) subsecond;
|
||||
const char weekdays[7][3] = {"SA", "SU", "MO", "TU", "WE", "TH", "FR"};
|
||||
char buf[11];
|
||||
uint8_t pos;
|
||||
|
||||
watch_date_time date_time;
|
||||
uint32_t previous_date_time;
|
||||
switch (event) {
|
||||
switch (event.bit.event_type) {
|
||||
case EVENT_ACTIVATE:
|
||||
case EVENT_TICK:
|
||||
case EVENT_SCREENSAVER:
|
||||
@ -34,11 +33,11 @@ void simple_clock_widget_loop(LauncherEvent event, LauncherSettings *settings, u
|
||||
previous_date_time = *((uint32_t *)context);
|
||||
*((uint32_t *)context) = date_time.reg;
|
||||
|
||||
if (date_time.reg >> 6 == previous_date_time >> 6 && event != EVENT_SCREENSAVER) {
|
||||
if (date_time.reg >> 6 == previous_date_time >> 6 && event.bit.event_type != 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 && event != EVENT_SCREENSAVER) {
|
||||
} else if (date_time.reg >> 12 == previous_date_time >> 12 && event.bit.event_type != EVENT_SCREENSAVER) {
|
||||
// everything before minutes is the same.
|
||||
pos = 6;
|
||||
sprintf(buf, "%02d%02d", date_time.unit.minute, date_time.unit.second);
|
||||
@ -55,7 +54,7 @@ void simple_clock_widget_loop(LauncherEvent event, LauncherSettings *settings, u
|
||||
if (date_time.unit.hour == 0) date_time.unit.hour = 12;
|
||||
}
|
||||
pos = 0;
|
||||
if (event == EVENT_SCREENSAVER) {
|
||||
if (event.bit.event_type == 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);
|
||||
|
@ -5,7 +5,7 @@
|
||||
|
||||
void simple_clock_widget_setup(LauncherSettings *settings, void ** context_ptr);
|
||||
void simple_clock_widget_activate(LauncherSettings *settings, void *context);
|
||||
void simple_clock_widget_loop(LauncherEvent event, LauncherSettings *settings, uint8_t subsecond, void *context);
|
||||
void simple_clock_widget_loop(LauncherEvent event, LauncherSettings *settings, void *context);
|
||||
void simple_clock_widget_resign(LauncherSettings *settings, void *context);
|
||||
|
||||
uint8_t simple_clock_widget_get_weekday(uint16_t day, uint16_t month, uint16_t year);
|
||||
|
@ -11,14 +11,13 @@ void fake_widget_1_activate(LauncherSettings *settings, void *context) {
|
||||
(void) context;
|
||||
}
|
||||
|
||||
void fake_widget_1_loop(LauncherEvent event, LauncherSettings *settings, uint8_t subsecond, void *context) {
|
||||
void fake_widget_1_loop(LauncherEvent event, LauncherSettings *settings, void *context) {
|
||||
printf("fake_widget_1_loop\n");
|
||||
(void) settings;
|
||||
(void) subsecond;
|
||||
(void) context;
|
||||
watch_display_string("W1 d get01", 0);
|
||||
|
||||
switch (event) {
|
||||
switch (event.bit.event_type) {
|
||||
case EVENT_MODE_BUTTON_UP:
|
||||
launcher_move_to_next_widget();
|
||||
return;
|
||||
|
@ -5,7 +5,7 @@
|
||||
|
||||
void fake_widget_1_setup(LauncherSettings *settings, void ** context_ptr);
|
||||
void fake_widget_1_activate(LauncherSettings *settings, void *context);
|
||||
void fake_widget_1_loop(LauncherEvent event, LauncherSettings *settings, uint8_t subsecond, void *context);
|
||||
void fake_widget_1_loop(LauncherEvent event, LauncherSettings *settings, void *context);
|
||||
void fake_widget_1_resign(LauncherSettings *settings, void *context);
|
||||
|
||||
#define fake_widget_1 { \
|
||||
|
@ -11,14 +11,13 @@ void fake_widget_2_activate(LauncherSettings *settings, void *context) {
|
||||
(void) context;
|
||||
}
|
||||
|
||||
void fake_widget_2_loop(LauncherEvent event, LauncherSettings *settings, uint8_t subsecond, void *context) {
|
||||
void fake_widget_2_loop(LauncherEvent event, LauncherSettings *settings, void *context) {
|
||||
printf("fake_widget_2_loop\n");
|
||||
(void) settings;
|
||||
(void) subsecond;
|
||||
(void) context;
|
||||
watch_display_string("W1 d get02", 0);
|
||||
|
||||
switch (event) {
|
||||
switch (event.bit.event_type) {
|
||||
case EVENT_MODE_BUTTON_UP:
|
||||
launcher_move_to_next_widget();
|
||||
return;
|
||||
|
@ -5,7 +5,7 @@
|
||||
|
||||
void fake_widget_2_setup(LauncherSettings *settings, void ** context_ptr);
|
||||
void fake_widget_2_activate(LauncherSettings *settings, void *context);
|
||||
void fake_widget_2_loop(LauncherEvent event, LauncherSettings *settings, uint8_t subsecond, void *context);
|
||||
void fake_widget_2_loop(LauncherEvent event, LauncherSettings *settings, void *context);
|
||||
void fake_widget_2_resign(LauncherSettings *settings, void *context);
|
||||
|
||||
#define fake_widget_2 { \
|
||||
|
@ -16,12 +16,12 @@ void preferences_widget_activate(LauncherSettings *settings, void *context) {
|
||||
launcher_request_tick_frequency(4); // we need to manually blink some pixels
|
||||
}
|
||||
|
||||
void preferences_widget_loop(LauncherEvent event, LauncherSettings *settings, uint8_t subsecond, void *context) {
|
||||
void preferences_widget_loop(LauncherEvent event, LauncherSettings *settings, void *context) {
|
||||
(void) settings;
|
||||
(void) context;
|
||||
printf("preferences_widget_loop\n");
|
||||
uint8_t current_page = *((uint8_t *)context);
|
||||
switch (event) {
|
||||
switch (event.bit.event_type) {
|
||||
case EVENT_MODE_BUTTON_UP:
|
||||
launcher_move_to_next_widget();
|
||||
return;
|
||||
@ -63,7 +63,7 @@ void preferences_widget_loop(LauncherEvent event, LauncherSettings *settings, ui
|
||||
watch_set_led_off();
|
||||
}
|
||||
|
||||
if (subsecond % 2) return;
|
||||
if (event.bit.subsecond % 2) return;
|
||||
char buf[3];
|
||||
switch (current_page) {
|
||||
case 0:
|
||||
|
@ -5,7 +5,7 @@
|
||||
|
||||
void preferences_widget_setup(LauncherSettings *settings, void ** context_ptr);
|
||||
void preferences_widget_activate(LauncherSettings *settings, void *context);
|
||||
void preferences_widget_loop(LauncherEvent event, LauncherSettings *settings, uint8_t subsecond, void *context);
|
||||
void preferences_widget_loop(LauncherEvent event, LauncherSettings *settings, void *context);
|
||||
void preferences_widget_resign(LauncherSettings *settings, void *context);
|
||||
|
||||
#define preferences_widget { \
|
||||
|
Loading…
x
Reference in New Issue
Block a user