summaryrefslogtreecommitdiffstats
path: root/watch-library/simulator/watch
diff options
context:
space:
mode:
Diffstat (limited to 'watch-library/simulator/watch')
-rw-r--r--watch-library/simulator/watch/watch_buzzer.c84
-rw-r--r--watch-library/simulator/watch/watch_deepsleep.c1
-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, 134 insertions, 53 deletions
diff --git a/watch-library/simulator/watch/watch_buzzer.c b/watch-library/simulator/watch/watch_buzzer.c
index f19e1928..1c95a96d 100644
--- a/watch-library/simulator/watch/watch_buzzer.c
+++ b/watch-library/simulator/watch/watch_buzzer.c
@@ -23,38 +23,82 @@
*/
#include "watch_buzzer.h"
+#include "watch_main_loop.h"
-inline void watch_enable_buzzer(void) {
- // TODO: (a2) hook to UI
+#include <emscripten.h>
+
+static bool buzzer_enabled = false;
+static uint32_t buzzer_period;
+
+void watch_enable_buzzer(void) {
+ buzzer_enabled = true;
+ buzzer_period = NotePeriods[BUZZER_NOTE_A4];
+
+ EM_ASM({
+ Module['audioContext'] = new (window.AudioContext || window.webkitAudioContext)();
+ });
}
-inline void watch_set_buzzer_period(uint32_t period) {
- // TODO: (a2) hook to UI
+
+void watch_set_buzzer_period(uint32_t period) {
+ if (!buzzer_enabled) return;
+ buzzer_period = period;
}
void watch_disable_buzzer(void) {
- _watch_disable_tcc();
-}
+ buzzer_enabled = false;
+ buzzer_period = NotePeriods[BUZZER_NOTE_A4];
-inline void watch_set_buzzer_on(void) {
- // TODO: (a2) hook to UI
+ EM_ASM({
+ if (Module['audioContext']) {
+ Module['audioContext'].close();
+ Module['audioContext'] = null;
+ }
+ });
}
-inline void watch_set_buzzer_off(void) {
- // TODO: (a2) hook to UI
+void watch_set_buzzer_on(void) {
+ if (!buzzer_enabled) return;
+
+ EM_ASM({
+ const audioContext = Module['audioContext'];
+ if (!audioContext) return;
+
+ if (!(audioContext._oscillator && audioContext._gain)) {
+ const oscillator = audioContext.createOscillator();
+ const gain = audioContext.createGain();
+ oscillator.type = 'triangle';
+ oscillator.connect(gain);
+ gain.connect(audioContext.destination);
+ oscillator.start(0);
+
+ audioContext._oscillator = oscillator;
+ audioContext._gain = gain;
+ }
+
+ audioContext._oscillator.frequency.value = 1e6/$0;
+ audioContext._gain.gain.value = 1;
+ }, buzzer_period);
}
-// note: the buzzer uses a 1 MHz clock. these values were determined by dividing 1,000,000 by the target frequency.
-// i.e. for a 440 Hz tone (A4 on the piano), 1MHz/440Hz = 2273
-const uint16_t NotePeriods[108] = {0};
+void watch_set_buzzer_off(void) {
+ if (!buzzer_enabled) return;
+
+ EM_ASM({
+ const audioContext = Module['audioContext'];
+ if (audioContext && audioContext._gain) {
+ audioContext._gain.gain.value = 0;
+ }
+ });
+}
void watch_buzzer_play_note(BuzzerNote note, uint16_t duration_ms) {
if (note == BUZZER_NOTE_REST) {
watch_set_buzzer_off();
- } // else {
- // hri_tcc_write_PERBUF_reg(TCC0, NotePeriods[note]);
- // hri_tcc_write_CCBUF_reg(TCC0, WATCH_BUZZER_TCC_CHANNEL, NotePeriods[note] / 2);
- // watch_set_buzzer_on();
- // }
- // delay_ms(duration_ms);
- // watch_set_buzzer_off();
+ } else {
+ watch_set_buzzer_period(NotePeriods[note]);
+ watch_set_buzzer_on();
+ }
+
+ main_loop_sleep(duration_ms);
+ watch_set_buzzer_off();
}
diff --git a/watch-library/simulator/watch/watch_deepsleep.c b/watch-library/simulator/watch/watch_deepsleep.c
index 9f409570..a12cf2a6 100644
--- a/watch-library/simulator/watch/watch_deepsleep.c
+++ b/watch-library/simulator/watch/watch_deepsleep.c
@@ -35,6 +35,7 @@ static uint32_t watch_backup_data[8];
void watch_register_extwake_callback(uint8_t pin, ext_irq_cb_t callback, bool level) {
if (pin == BTN_ALARM) {
+ watch_enable_external_interrupts();
watch_register_interrupt_callback(pin, callback, level ? INTERRUPT_TRIGGER_RISING : INTERRUPT_TRIGGER_FALLING);
}
}
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);