summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--buttons.cpp18
-rw-r--r--buttons.hpp17
-rw-r--r--pico-watch.cpp37
3 files changed, 37 insertions, 35 deletions
diff --git a/buttons.cpp b/buttons.cpp
index b86ac5d..c919562 100644
--- a/buttons.cpp
+++ b/buttons.cpp
@@ -6,28 +6,22 @@
// From pico-watch.c:
extern int app_btnpressed(int app_id, uint gpio);
extern void app_switch(int old_appid, int new_appid);
-extern int current_app;
extern Api app_api;
-// Debounce control
-// See https://www.raspberrypi.org/forums/viewtopic.php?f=145&t=301522#p1812063
-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_last_pressed_time)>button_delay_time) {
+ if ((to_ms_since_boot(get_absolute_time())-g_s.button_last_pressed_time)>g_s.button_delay_time) {
if (app_api.m_interpret_button_press) {
- if (gpio == BUTTON_HOME && (current_app != 0)) // Home app
- app_switch(current_app, 0);
+ if (gpio == BUTTON_HOME && (g_s.current_app != 0)) // Home app
+ app_switch(g_s.current_app, 0);
else
- app_btnpressed(current_app, gpio);
+ app_btnpressed(g_s.current_app, gpio);
}
app_api.button_last_set(gpio);
- button_last_pressed_time = to_ms_since_boot(get_absolute_time());
+ g_s.button_last_pressed_time = to_ms_since_boot(get_absolute_time());
}
}
@@ -56,5 +50,5 @@ void init_buttons() {
gpio_set_irq_enabled_with_callback(button, GPIO_IRQ_EDGE_FALL , true, &gpio_interrupt_cb);
}
*/
- button_last_pressed_time = to_ms_since_boot(get_absolute_time());
+ g_s.button_last_pressed_time = to_ms_since_boot(get_absolute_time());
}
diff --git a/buttons.hpp b/buttons.hpp
index 3899127..b8ecbc3 100644
--- a/buttons.hpp
+++ b/buttons.hpp
@@ -12,9 +12,20 @@
#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;
+struct global_status {
+ int current_app = 0;
+ bool is_sleeping = false;
+ bool app_ready = true;
+ bool app_rendering = false;
+
+ // 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_last_pressed_time;
+ const int button_delay_time = 50;
+};
+
+extern global_status g_s;
void init_buttons();
void gpio_interrupt_cb(uint gpio, uint32_t events);
diff --git a/pico-watch.cpp b/pico-watch.cpp
index ce76016..f3b8e4a 100644
--- a/pico-watch.cpp
+++ b/pico-watch.cpp
@@ -11,10 +11,7 @@
#include "apps/main_clock.hpp"
#include "apps/home_menu.hpp"
-int current_app = 0;
-bool is_sleeping = false;
-bool app_ready = true;
-bool app_rendering = false;
+global_status g_s;
Api app_api;
#define NUMBER_OF_APPS 2
@@ -79,18 +76,18 @@ int app_destroy(int app_id) {
int app_bgrefresh(int app_id) {
if (APPS_IS_INIT[app_id])
- return (*APPS_FUNC_BGREFRESH[app_id])(&app_api, app_id==current_app);
+ return (*APPS_FUNC_BGREFRESH[app_id])(&app_api, app_id==g_s.current_app);
}
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;
+ uint32_t time_since_last_press = to_ms_since_boot(get_absolute_time())-g_s.button_last_pressed_time;
+ if (!g_s.is_sleeping && time_since_last_press > ENTER_SLEEP_DELAY) {
+ g_s.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;
+ } else if (g_s.is_sleeping && time_since_last_press < ENTER_SLEEP_DELAY) {
+ g_s.is_sleeping = false;
app_api.performance_set(Api::perf_modes::EXIT_SHALLOW_SLEEP);
app_api.display_power(true);
}
@@ -102,14 +99,14 @@ bool repeating_callback(struct repeating_timer *t) {
}
void app_switch(int old_appid, int new_appid) {
- app_ready = false;
+ g_s.app_ready = false;
// FIXME: race condition when pressing on HOME while app is rendering!
// The system is blocked waiting for the app to finish rendering, which will never happen. To fix the problem, app switching has to be a flag (c.f struct) that is set, and checked before rendering app. "if (app_switching.requested) app_switch(...);" We will not need anymore the app_rendering flag, as the check is done while the app is not rendering.
- while (app_rendering); // Wait for the app to finish rendering cycle
+ while (g_s.app_rendering); // Wait for the app to finish rendering cycle
if (APPS_DESTROY_ON_EXIT[old_appid])
app_destroy(old_appid);
- current_app = app_init(new_appid);
- app_ready = true;
+ g_s.current_app = app_init(new_appid);
+ g_s.app_ready = true;
}
int main() {
@@ -120,16 +117,16 @@ int main() {
struct repeating_timer timer;
add_repeating_timer_ms(250, repeating_callback, NULL, &timer); // TODO: Execute on core1
- app_init(current_app);
+ app_init(g_s.current_app);
while (1) {
- if (app_ready && !is_sleeping) {
- app_rendering = true;
- app_render(current_app);
+ if (g_s.app_ready && !g_s.is_sleeping) {
+ g_s.app_rendering = true;
+ app_render(g_s.current_app);
app_api.display_write_backbuffer();
- app_rendering = false;
+ g_s.app_rendering = false;
}
- if (is_sleeping) __wfi();
+ if (g_s.is_sleeping) __wfi();
else sleep_ms(app_api.performance_render_interval_get());
}
return 0;