summaryrefslogtreecommitdiffstats
path: root/watch-library
diff options
context:
space:
mode:
authorAlexsander Akers <me@a2.io>2022-01-26 20:24:15 -0500
committerAlexsander Akers <me@a2.io>2022-01-27 11:12:01 -0500
commitcb8223217b9fbcb705677dad890b35a467a44e75 (patch)
tree0897cde67b16c6c3e33345b98381164499416c1a /watch-library
parenta0f8e9c8bc458b0a34b34864703fd97d9835fd86 (diff)
downloadSensor-Watch-cb8223217b9fbcb705677dad890b35a467a44e75.tar.gz
Sensor-Watch-cb8223217b9fbcb705677dad890b35a467a44e75.tar.bz2
Sensor-Watch-cb8223217b9fbcb705677dad890b35a467a44e75.zip
Update main loop to fix reentrancy runtime errors
Diffstat (limited to 'watch-library')
-rw-r--r--watch-library/simulator/main.c58
-rw-r--r--watch-library/simulator/watch/watch_buzzer.c4
-rw-r--r--watch-library/simulator/watch/watch_extint.c26
-rw-r--r--watch-library/simulator/watch/watch_main_loop.h33
-rw-r--r--watch-library/simulator/watch/watch_rtc.c31
-rw-r--r--watch-library/simulator/watch/watch_slcd.c12
6 files changed, 123 insertions, 41 deletions
diff --git a/watch-library/simulator/main.c b/watch-library/simulator/main.c
index 5596bf82..14bf44e6 100644
--- a/watch-library/simulator/main.c
+++ b/watch-library/simulator/main.c
@@ -24,37 +24,81 @@
#include <stdio.h>
#include "watch.h"
+#include "watch_main_loop.h"
#include <emscripten.h>
#include <emscripten/html5.h>
+#define ANIMATION_FRAME_ID_IS_VALID(id) ((id) >= 0)
+#define ANIMATION_FRAME_ID_INVALID (-1)
+#define ANIMATION_FRAME_ID_SUSPENDED (-2)
+
static bool sleeping = true;
+static volatile long animation_frame_id = ANIMATION_FRAME_ID_INVALID;
+
+// make compiler happy
+static void main_loop_set_sleeping(bool sleeping);
+static EM_BOOL main_loop(double time, void *userData);
+
+static inline void request_next_frame(void) {
+ if (animation_frame_id == ANIMATION_FRAME_ID_INVALID) {
+ animation_frame_id = emscripten_request_animation_frame(main_loop, NULL);
+ }
+}
static EM_BOOL main_loop(double time, void *userData) {
+ if (main_loop_is_sleeping()) {
+ request_next_frame();
+ return EM_FALSE;
+ }
+
if (sleeping) {
sleeping = false;
app_wake_from_standby();
}
+ animation_frame_id = ANIMATION_FRAME_ID_INVALID;
bool can_sleep = app_loop();
if (can_sleep) {
app_prepare_for_standby();
sleeping = true;
+ animation_frame_id = ANIMATION_FRAME_ID_INVALID;
return EM_FALSE;
}
- return EM_TRUE;
+ request_next_frame();
+ return EM_FALSE;
}
-// make compiler happy
-void resume_main_loop(void);
-
-EMSCRIPTEN_KEEPALIVE
void resume_main_loop(void) {
- if (sleeping) {
- emscripten_request_animation_frame_loop(main_loop, NULL);
+ if (!ANIMATION_FRAME_ID_IS_VALID(animation_frame_id)) {
+ animation_frame_id = emscripten_request_animation_frame(main_loop, NULL);
+ }
+}
+
+void suspend_main_loop(void) {
+ if (ANIMATION_FRAME_ID_IS_VALID(animation_frame_id)) {
+ emscripten_cancel_animation_frame(animation_frame_id);
}
+
+ animation_frame_id = ANIMATION_FRAME_ID_SUSPENDED;
+}
+
+void main_loop_sleep(uint32_t ms) {
+ main_loop_set_sleeping(true);
+ emscripten_sleep(ms);
+ main_loop_set_sleeping(false);
+}
+
+bool main_loop_is_sleeping(void) {
+ return EM_ASM_INT({ return Module['suspended']; }) != 0;
+}
+
+static void main_loop_set_sleeping(bool sleeping) {
+ EM_ASM({
+ Module['suspended'] = $0;
+ }, sleeping);
}
int main(void) {
diff --git a/watch-library/simulator/watch/watch_buzzer.c b/watch-library/simulator/watch/watch_buzzer.c
index c5191de2..1c95a96d 100644
--- a/watch-library/simulator/watch/watch_buzzer.c
+++ b/watch-library/simulator/watch/watch_buzzer.c
@@ -23,6 +23,7 @@
*/
#include "watch_buzzer.h"
+#include "watch_main_loop.h"
#include <emscripten.h>
@@ -97,6 +98,7 @@ void watch_buzzer_play_note(BuzzerNote note, uint16_t duration_ms) {
watch_set_buzzer_period(NotePeriods[note]);
watch_set_buzzer_on();
}
- emscripten_sleep(duration_ms);
+
+ main_loop_sleep(duration_ms);
watch_set_buzzer_off();
}
diff --git a/watch-library/simulator/watch/watch_extint.c b/watch-library/simulator/watch/watch_extint.c
index d37059bf..03abe42c 100644
--- a/watch-library/simulator/watch/watch_extint.c
+++ b/watch-library/simulator/watch/watch_extint.c
@@ -23,6 +23,7 @@
*/
#include "watch_extint.h"
+#include "watch_main_loop.h"
#include <emscripten.h>
#include <emscripten/html5.h>
@@ -41,7 +42,7 @@ static watch_interrupt_trigger external_interrupt_alarm_trigger = INTERRUPT_TRIG
#define BTN_ID_LIGHT 1
#define BTN_ID_MODE 2
static const uint8_t BTN_IDS[] = { BTN_ID_ALARM, BTN_ID_LIGHT, BTN_ID_MODE };
-static void watch_invoke_interrupt_callback(const uint8_t button_id, watch_interrupt_trigger trigger);
+static EM_BOOL watch_invoke_interrupt_callback(const uint8_t button_id, watch_interrupt_trigger trigger);
static EM_BOOL watch_invoke_key_callback(int eventType, const EmscriptenKeyboardEvent *keyEvent, void *userData) {
if (output_focused || keyEvent->repeat) return EM_FALSE;
@@ -68,23 +69,20 @@ static EM_BOOL watch_invoke_key_callback(int eventType, const EmscriptenKeyboard
}
watch_interrupt_trigger trigger = eventType == EMSCRIPTEN_EVENT_KEYDOWN ? INTERRUPT_TRIGGER_RISING : INTERRUPT_TRIGGER_FALLING;
- watch_invoke_interrupt_callback(button_id, trigger);
- return EM_TRUE;
+ return watch_invoke_interrupt_callback(button_id, trigger);
}
static EM_BOOL watch_invoke_mouse_callback(int eventType, const EmscriptenMouseEvent *mouseEvent, void *userData) {
if (eventType == EMSCRIPTEN_EVENT_MOUSEOUT && mouseEvent->buttons == 0) return EM_FALSE;
uint8_t button_id = *(const char *)userData;
watch_interrupt_trigger trigger = eventType == EMSCRIPTEN_EVENT_MOUSEDOWN ? INTERRUPT_TRIGGER_RISING : INTERRUPT_TRIGGER_FALLING;
- watch_invoke_interrupt_callback(button_id, trigger);
- return EM_TRUE;
+ return watch_invoke_interrupt_callback(button_id, trigger);
}
static EM_BOOL watch_invoke_touch_callback(int eventType, const EmscriptenTouchEvent *touchEvent, void *userData) {
uint8_t button_id = *(const char *)userData;
watch_interrupt_trigger trigger = eventType == EMSCRIPTEN_EVENT_TOUCHSTART ? INTERRUPT_TRIGGER_RISING : INTERRUPT_TRIGGER_FALLING;
- watch_invoke_interrupt_callback(button_id, trigger);
- return EM_TRUE;
+ return watch_invoke_interrupt_callback(button_id, trigger);
}
static EM_BOOL watch_invoke_focus_callback(int eventType, const EmscriptenFocusEvent *focusEvent, void *userData) {
@@ -126,9 +124,7 @@ void watch_disable_external_interrupts(void) {
external_interrupt_enabled = false;
}
-static void watch_invoke_interrupt_callback(const uint8_t button_id, watch_interrupt_trigger event) {
- if (!external_interrupt_enabled) return;
-
+static EM_BOOL watch_invoke_interrupt_callback(const uint8_t button_id, watch_interrupt_trigger event) {
ext_irq_cb_t callback;
watch_interrupt_trigger trigger;
uint8_t pin;
@@ -149,7 +145,7 @@ static void watch_invoke_interrupt_callback(const uint8_t button_id, watch_inter
trigger = external_interrupt_alarm_trigger;
break;
default:
- return;
+ return EM_FALSE;
}
const bool level = (event & INTERRUPT_TRIGGER_RISING) != 0;
@@ -159,14 +155,18 @@ static void watch_invoke_interrupt_callback(const uint8_t button_id, watch_inter
$1 ? classList.add(highlight) : classList.remove(highlight);
}, button_id, level);
+ if (!external_interrupt_enabled || main_loop_is_sleeping()) {
+ return EM_FALSE;
+ }
+
watch_set_pin_level(pin, level);
if (callback && (event & trigger) != 0) {
callback();
-
- void resume_main_loop(void);
resume_main_loop();
}
+
+ return EM_TRUE;
}
void watch_register_interrupt_callback(const uint8_t pin, ext_irq_cb_t callback, watch_interrupt_trigger trigger) {
diff --git a/watch-library/simulator/watch/watch_main_loop.h b/watch-library/simulator/watch/watch_main_loop.h
new file mode 100644
index 00000000..82351919
--- /dev/null
+++ b/watch-library/simulator/watch/watch_main_loop.h
@@ -0,0 +1,33 @@
+/*
+ * MIT License
+ *
+ * Copyright (c) 2022 Joey Castillo
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in all
+ * copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+ * SOFTWARE.
+ */
+
+#include "driver_init.h"
+
+void suspend_main_loop(void);
+
+void resume_main_loop(void);
+
+void main_loop_sleep(uint32_t ms);
+
+bool main_loop_is_sleeping(void);
diff --git a/watch-library/simulator/watch/watch_rtc.c b/watch-library/simulator/watch/watch_rtc.c
index 573c0ff2..1fe6a78b 100644
--- a/watch-library/simulator/watch/watch_rtc.c
+++ b/watch-library/simulator/watch/watch_rtc.c
@@ -23,15 +23,16 @@
*/
#include "watch_rtc.h"
+#include "watch_main_loop.h"
#include <emscripten.h>
#include <emscripten/html5.h>
static double time_offset = 0;
-static long tick_callbacks[8];
+static long tick_callbacks[8] = { -1, -1, -1, -1, -1, -1, -1, -1 };
-static long alarm_interval_id;
-static long alarm_timeout_id;
+static long alarm_interval_id = -1;
+static long alarm_timeout_id = -1;
static double alarm_interval;
ext_irq_cb_t alarm_callback;
ext_irq_cb_t btn_alarm_callback;
@@ -83,8 +84,6 @@ void watch_rtc_disable_tick_callback(void) {
static void watch_invoke_periodic_callback(void *userData) {
ext_irq_cb_t callback = userData;
callback();
-
- void resume_main_loop(void);
resume_main_loop();
}
@@ -100,32 +99,34 @@ void watch_rtc_register_periodic_callback(ext_irq_cb_t callback, uint8_t frequen
// this also maps nicely to an index for our list of tick callbacks.
double interval = 1000 / frequency; // in msec
+
+ if (tick_callbacks[per_n] != -1) emscripten_clear_interval(tick_callbacks[per_n]);
tick_callbacks[per_n] = emscripten_set_interval(watch_invoke_periodic_callback, interval, (void *)callback);
}
void watch_rtc_disable_periodic_callback(uint8_t frequency) {
if (__builtin_popcount(frequency) != 1) return;
uint8_t per_n = __builtin_clz(frequency << 24);
- emscripten_clear_interval(tick_callbacks[per_n]);
- tick_callbacks[per_n] = 0;
+ if (tick_callbacks[per_n] != -1) {
+ emscripten_clear_interval(tick_callbacks[per_n]);
+ tick_callbacks[per_n] = -1;
+ }
}
void watch_rtc_disable_all_periodic_callbacks(void) {
for (int i = 0; i < 8; i++) {
- if (tick_callbacks[i] != 0) {
+ if (tick_callbacks[i] != -1) {
emscripten_clear_interval(tick_callbacks[i]);
- tick_callbacks[i] = 0;
+ tick_callbacks[i] = -1;
}
}
}
static void watch_invoke_alarm_interval_callback(void *userData) {
- (void)userData;
if (alarm_callback) alarm_callback();
}
static void watch_invoke_alarm_callback(void *userData) {
- (void)userData;
if (alarm_callback) alarm_callback();
alarm_interval_id = emscripten_set_interval(watch_invoke_alarm_interval_callback, alarm_interval, NULL);
}
@@ -182,14 +183,14 @@ void watch_rtc_disable_alarm_callback(void) {
alarm_callback = NULL;
alarm_interval = 0;
- if (alarm_timeout_id) {
+ if (alarm_timeout_id != -1) {
emscripten_clear_timeout(alarm_timeout_id);
- alarm_timeout_id = 0;
+ alarm_timeout_id = -1;
}
- if (alarm_interval_id) {
+ if (alarm_interval_id != -1) {
emscripten_clear_interval(alarm_interval_id);
- alarm_interval_id = 0;
+ alarm_interval_id = -1;
}
}
diff --git a/watch-library/simulator/watch/watch_slcd.c b/watch-library/simulator/watch/watch_slcd.c
index 2af96847..5c5a936f 100644
--- a/watch-library/simulator/watch/watch_slcd.c
+++ b/watch-library/simulator/watch/watch_slcd.c
@@ -34,9 +34,9 @@
static char blink_character;
static bool blink_state;
-static long blink_interval_id;
+static long blink_interval_id = - 1;
static bool tick_state;
-static long tick_interval_id;
+static long tick_interval_id = -1;
void watch_enable_display(void) {
watch_clear_display();
@@ -70,6 +70,7 @@ static void watch_invoke_blink_callback(void *userData) {
}
void watch_start_character_blink(char character, uint32_t duration) {
+ if (blink_interval_id != -1) return;
watch_display_character(character, 7);
watch_clear_pixel(2, 10); // clear segment B of position 7 since it can't blink
@@ -80,7 +81,7 @@ void watch_start_character_blink(char character, uint32_t duration) {
void watch_stop_blink(void) {
emscripten_clear_timeout(blink_interval_id);
- blink_interval_id = 0;
+ blink_interval_id = -1;
blink_state = false;
}
@@ -96,6 +97,7 @@ static void watch_invoke_tick_callback(void *userData) {
}
void watch_start_tick_animation(uint32_t duration) {
+ if (tick_interval_id != -1) return;
watch_display_character(' ', 8);
tick_state = true;
@@ -103,12 +105,12 @@ void watch_start_tick_animation(uint32_t duration) {
}
bool watch_tick_animation_is_running(void) {
- return tick_interval_id != 0;
+ return tick_interval_id != -1;
}
void watch_stop_tick_animation(void) {
emscripten_clear_timeout(tick_interval_id);
- tick_interval_id = 0;
+ tick_interval_id = -1;
tick_state = false;
watch_display_character(' ', 8);