summaryrefslogtreecommitdiffstats
path: root/watch-library
diff options
context:
space:
mode:
authorJoey Castillo <joeycastillo@utexas.edu>2022-01-27 14:28:46 -0500
committerJoey Castillo <joeycastillo@utexas.edu>2022-01-27 14:28:46 -0500
commit5fb8dabdd2b557450886fa447c081209b2a3b74d (patch)
tree694472c653ff081233db69cdfdbc393938b48f5c /watch-library
parent45aa04f4acda9d4e67eedef84bb2b8e87994f999 (diff)
parentd3e484dc989a6e3a040b5b1092340973d9b4e22f (diff)
downloadSensor-Watch-5fb8dabdd2b557450886fa447c081209b2a3b74d.tar.gz
Sensor-Watch-5fb8dabdd2b557450886fa447c081209b2a3b74d.tar.bz2
Sensor-Watch-5fb8dabdd2b557450886fa447c081209b2a3b74d.zip
Merge branch 'main' of github.com:joeycastillo/Sensor-Watch into main
Diffstat (limited to 'watch-library')
-rw-r--r--watch-library/hardware/watch/watch_buzzer.c4
-rw-r--r--watch-library/shared/watch/watch_private_buzzer.c28
-rw-r--r--watch-library/simulator/main.c58
-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
9 files changed, 213 insertions, 64 deletions
diff --git a/watch-library/hardware/watch/watch_buzzer.c b/watch-library/hardware/watch/watch_buzzer.c
index a275b00d..c06242ff 100644
--- a/watch-library/hardware/watch/watch_buzzer.c
+++ b/watch-library/hardware/watch/watch_buzzer.c
@@ -47,10 +47,6 @@ inline void watch_set_buzzer_off(void) {
gpio_set_pin_function(BUZZER, GPIO_PIN_FUNCTION_OFF);
}
-// 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] = {18182,17161,16197,15288,14430,13620,12857,12134,11453,10811,10204,9631,9091,8581,8099,7645,7216,6811,6428,6068,5727,5405,5102,4816,4545,4290,4050,3822,3608,3405,3214,3034,2863,2703,2551,2408,2273,2145,2025,1911,1804,1703,1607,1517,1432,1351,1276,1204,1136,1073,1012,956,902,851,804,758,716,676,638,602,568,536,506,478,451,426,402,379,358,338,319,301,284,268,253,239,225,213,201,190,179,169,159,150,142,134,127};
-
void watch_buzzer_play_note(BuzzerNote note, uint16_t duration_ms) {
if (note == BUZZER_NOTE_REST) {
watch_set_buzzer_off();
diff --git a/watch-library/shared/watch/watch_private_buzzer.c b/watch-library/shared/watch/watch_private_buzzer.c
new file mode 100644
index 00000000..0618f425
--- /dev/null
+++ b/watch-library/shared/watch/watch_private_buzzer.c
@@ -0,0 +1,28 @@
+/*
+ * 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"
+
+// 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] = {18182,17161,16197,15288,14430,13620,12857,12134,11453,10811,10204,9631,9091,8581,8099,7645,7216,6811,6428,6068,5727,5405,5102,4816,4545,4290,4050,3822,3608,3405,3214,3034,2863,2703,2551,2408,2273,2145,2025,1911,1804,1703,1607,1517,1432,1351,1276,1204,1136,1073,1012,956,902,851,804,758,716,676,638,602,568,536,506,478,451,426,402,379,358,338,319,301,284,268,253,239,225,213,201,190,179,169,159,150,142,134,127};
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 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);