Update main loop to fix reentrancy runtime errors
This commit is contained in:
		
							parent
							
								
									a0f8e9c8bc
								
							
						
					
					
						commit
						cb8223217b
					
				
							
								
								
									
										1
									
								
								make.mk
									
									
									
									
									
								
							
							
						
						
									
										1
									
								
								make.mk
									
									
									
									
									
								
							@ -138,6 +138,7 @@ INCLUDES += \
 | 
			
		||||
  -I$(TOP)/watch-library/shared/driver/ \
 | 
			
		||||
  -I$(TOP)/watch-library/shared/config/ \
 | 
			
		||||
  -I$(TOP)/watch-library/shared/watch/ \
 | 
			
		||||
  -I$(TOP)/watch-library/simulator/watch/ \
 | 
			
		||||
  -I$(TOP)/watch-library/simulator/hpl/port/ \
 | 
			
		||||
  -I$(TOP)/watch-library/hardware/include/component \
 | 
			
		||||
  -I$(TOP)/watch-library/hardware/hal/include/ \
 | 
			
		||||
 | 
			
		||||
@ -24,39 +24,83 @@
 | 
			
		||||
 | 
			
		||||
#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) {
 | 
			
		||||
    printf("Hello, world!\n");
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -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();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -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) {
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										33
									
								
								watch-library/simulator/watch/watch_main_loop.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										33
									
								
								watch-library/simulator/watch/watch_main_loop.h
									
									
									
									
									
										Normal file
									
								
							@ -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);
 | 
			
		||||
@ -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;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -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);
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user