]> git.earman.xyz Git - sensor-watch.git/commitdiff
rename stock_stopwatch -> fast_stopwatch
authorjoeycastillo <joeycastillo@utexas.edu>
Sun, 29 Sep 2024 17:27:35 +0000 (13:27 -0400)
committerjoeycastillo <joeycastillo@utexas.edu>
Sun, 29 Sep 2024 17:27:35 +0000 (13:27 -0400)
movement/movement_faces.h
movement/watch_faces/complication/dual_timer_face.c
movement/watch_faces/complication/dual_timer_face.h
movement/watch_faces/complication/stopwatch_face.h
movement_config.h
movement_faces.h
watch-faces.mk
watch-faces/complication/fast_stopwatch_face.c [new file with mode: 0644]
watch-faces/complication/fast_stopwatch_face.h [new file with mode: 0644]
watch-faces/complication/stock_stopwatch_face.c [deleted file]
watch-faces/complication/stock_stopwatch_face.h [deleted file]

index 630f264aafa95060f7086655d2ed3ce48e4e890c..d13a34fa158e89bb9f6b550c6f5459fb50da5d8c 100644 (file)
@@ -64,7 +64,7 @@
 #include "ratemeter_face.h"
 #include "rpn_calculator_alt_face.h"
 #include "weeknumber_clock_face.h"
-#include "stock_stopwatch_face.h"
+#include "fast_stopwatch_face.h"
 #include "tachymeter_face.h"
 #include "nanosec_face.h"
 #include "finetune_face.h"
index aa91c701480d2b0a0f1991661a57faa5367f92b4..e7bc5b3ff742be52bcf63cc846c53dc9184a13fc 100644 (file)
  * IMPORTANT: This watch face uses the same TC2 callback counter as the Stock Stopwatch
  * watch-face. It works through calling a global handler function. The two watch-faces
  * therefore can't coexist within the same firmware. If you want to compile this watch-face
- * then you need to remove the line <../watch_faces/complication/stock_stopwatch_face.c \>
+ * then you need to remove the line <../watch_faces/complication/fast_stopwatch_face.c \>
  * from the Makefile.
  */
 
-// FROM stock_stopwatch_face.c ////////////////////////////////////////////////
+// FROM fast_stopwatch_face.c ////////////////////////////////////////////////
 // Copyright (c) 2022 Andreas Nebinger
 
 #if __EMSCRIPTEN__
@@ -107,7 +107,7 @@ static void _dual_timer_cb_initialize() {
     NVIC_EnableIRQ (TC2_IRQn);
 }
 
-// you need to take stock_stopwatch.c out of the Makefile or this will create a conflict
+// you need to take fast_stopwatch.c out of the Makefile or this will create a conflict
 // you have to choose between one of the stopwatches
  void TC2_Handler(void) {
     // interrupt handler for TC2 (globally!)
index e1bd2d5ac6ca53d5b86625a03ecd2d22415e1f01..ea8d3c4b06a653d9564d6b48bdb6bd2308632242 100644 (file)
@@ -63,7 +63,7 @@
  * IMPORTANT: This watch face uses the same TC2 callback counter as the Stock Stopwatch
  * watch-face. It works through calling a global handler function. The two watch-faces
  * therefore can't coexist within the same firmware. If you want to compile this watch-face
- * then you need to remove the line <../watch_faces/complication/stock_stopwatch_face.c \>
+ * then you need to remove the line <../watch_faces/complication/fast_stopwatch_face.c \>
  * from the Makefile.
  */
 
index e30562832955b8205062bd2f6269bd4d2715a4f5..cf0d8fe6ac98ebb68cab57026e69a50ccadcdd05 100644 (file)
@@ -34,7 +34,7 @@
  * when the timer is stopped resets it.
  *
  * This face does not count sub-seconds.
- * See also: "stock_stopwatch_face.h"
+ * See also: "fast_stopwatch_face.h"
  */
 
 #include "movement.h"
index 632b4ee6a8d3abc5ac714334f2c3bbef2ee32167..2fcf292dbefdaee9901f6b3145949995796749d9 100644 (file)
@@ -29,7 +29,7 @@
 
 const watch_face_t watch_faces[] = {
     clock_face,
-    stock_stopwatch_face,
+    fast_stopwatch_face,
     beats_face,
     world_clock_face,
     countdown_face,
index 7c630a82c53abfe222128ad6eac5e0cca1062b90..49c80c6e4ae704c6dbbdfb85b03b39b4103b2a60 100644 (file)
@@ -29,7 +29,7 @@
 #include "beats_face.h"
 #include "world_clock_face.h"
 #include "countdown_face.h"
-#include "stock_stopwatch_face.h"
+#include "fast_stopwatch_face.h"
 #include "set_time_face.h"
 #include "preferences_face.h"
 // New includes go above this line.
index f5cb4cbe2174749bd5d82eb32155084f2062e1eb..65b775b6a59139409f44c0f76a5d6b781dab4fca 100644 (file)
@@ -4,6 +4,6 @@ SRCS += \
   ./watch-faces/clock/beats_face.c \
   ./watch-faces/clock/world_clock_face.c \
   ./watch-faces/complication/countdown_face.c \
-  ./watch-faces/complication/stock_stopwatch_face.c \
+  ./watch-faces/complication/fast_stopwatch_face.c \
   ./watch-faces/settings/set_time_face.c \
   ./watch-faces/settings/preferences_face.c \
diff --git a/watch-faces/complication/fast_stopwatch_face.c b/watch-faces/complication/fast_stopwatch_face.c
new file mode 100644 (file)
index 0000000..1116898
--- /dev/null
@@ -0,0 +1,328 @@
+/*
+ * MIT License
+ *
+ * Copyright (c) 2022 Andreas Nebinger
+ *
+ * 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 <stdlib.h>
+#include <string.h>
+#include "fast_stopwatch_face.h"
+#include "watch.h"
+#include "watch_common_display.h"
+#include "watch_utility.h"
+#include "watch_rtc.h"
+
+/*
+    This watch face implements the original F-91W stopwatch functionality
+    including counting hundredths of seconds and lap timing. There are two
+    improvements compared to the functionality of the original: 
+    1. When reaching 59:59 the counter does not simply jump back to zero,
+       but keeps track of hours in the upper right hand corner. (Up to 24h)
+    2. Long pressing the light button toggles the led behaviour: it either
+       turns on on each button press or it doesn't.
+*/
+
+#if __EMSCRIPTEN__
+#include <emscripten.h>
+#include <emscripten/html5.h>
+#else
+#include "tc.h"
+#endif
+
+// distant future for background task: January 1, 2083
+static const watch_date_time distant_future = {
+    .unit = {0, 0, 0, 1, 1, 63}
+};
+
+static uint32_t _ticks;
+static uint32_t _lap_ticks;
+static uint8_t _blink_ticks;
+static uint32_t _old_seconds;
+static uint8_t _old_minutes;
+static uint8_t _hours;
+static bool _colon;
+static bool _is_running;
+
+#if __EMSCRIPTEN__
+
+static long _em_interval_id = 0;
+
+void em_cb_handler(void *userData) {
+    // interrupt handler for emscripten 128 Hz callbacks
+    (void) userData;
+    _ticks++;
+}
+
+static void _cb_initialize() { }
+
+static inline void _cb_stop() {
+    emscripten_clear_interval(_em_interval_id);
+    _em_interval_id = 0;
+    _is_running = false;
+}
+
+static inline void _cb_start() {
+    // initiate 128 hz callback
+    _em_interval_id = emscripten_set_interval(em_cb_handler, (double)(1000/128), (void *)NULL);
+}
+
+#else
+
+static inline void _cb_start() {
+    // start the TC1 timer
+    tc_enable(1);
+    _is_running = true;
+}
+
+static inline void _cb_stop() {
+    // stop the TC1 timer
+    tc_disable(1);
+    _is_running = false;
+}
+
+static void _cb_initialize() {
+    tc_init(1, GENERIC_CLOCK_3, TC_PRESCALER_DIV4);
+    tc_set_counter_mode(1, TC_COUNTER_MODE_8BIT);
+    tc_set_run_in_standby(1, true);
+    _cb_stop();
+    tc_count8_set_period(1, 1); // 1024 Hz divided by 4 divided by 2 results in a 128 Hz interrupt
+    /// FIXME: #SecondMovement, we need a gossamer wrapper for interrupts.
+    TC1->COUNT8.INTENSET.bit.OVF = 1;
+    NVIC_ClearPendingIRQ(TC1_IRQn);
+    NVIC_EnableIRQ (TC1_IRQn);
+}
+
+void irq_handler_tc1(void);
+void irq_handler_tc1(void) {
+    // interrupt handler for TC1 (globally!)
+    _ticks++;
+    TC1->COUNT8.INTFLAG.reg |= TC_INTFLAG_OVF;
+}
+
+#endif
+
+static inline void _button_beep() {
+    // play a beep as confirmation for a button press (if applicable)
+    if (movement_button_should_sound()) watch_buzzer_play_note(BUZZER_NOTE_C7, 50);
+}
+
+/// @brief Display minutes, seconds and fractions derived from 128 Hz tick counter
+///        on the lcd.
+/// @param ticks
+static void _display_ticks(uint32_t ticks) {
+    char buf[14];
+    uint8_t sec_100 = (ticks & 0x7F) * 100 / 128;
+    uint32_t seconds = ticks >> 7;
+    uint32_t minutes = seconds / 60;
+    if (_hours) {
+        sprintf(buf, "%2u", _hours);
+        watch_display_text(WATCH_POSITION_TOP_RIGHT, buf);
+    } else {
+        watch_display_text(WATCH_POSITION_TOP_RIGHT, "  ");
+    }
+
+    sprintf(buf, "%02lu%02lu%02u", minutes, (seconds % 60), sec_100);
+    watch_display_text(WATCH_POSITION_BOTTOM, buf);
+}
+
+/// @brief Displays the current stopwatch time on the LCD (more optimized than _display_ticks())
+static void _draw() {
+    if (_lap_ticks == 0) {
+        char buf[14];
+        uint8_t sec_100 = (_ticks & 0x7F) * 100 / 128;
+        if (_is_running) {
+            uint32_t seconds = _ticks >> 7;
+            if (seconds != _old_seconds) {
+                // seconds have changed
+                _old_seconds = seconds;
+                uint8_t minutes = seconds / 60;
+                seconds %= 60;
+                if (minutes != _old_minutes) {
+                    // minutes have changed, draw everything
+                    _old_minutes = minutes;
+                    minutes %= 60;
+                    if (_hours) {
+                        // with hour indicator
+                        sprintf(buf, "%2u", _hours);
+                        watch_display_text(WATCH_POSITION_TOP_RIGHT, buf);
+                    } else {
+                        // no hour indicator
+                        watch_display_text(WATCH_POSITION_TOP_RIGHT, "  ");
+                    }
+                    sprintf(buf, "%02u%02lu%02u", minutes, seconds, sec_100);
+                    watch_display_text(WATCH_POSITION_BOTTOM, buf);
+                } else {
+                    // just draw seconds
+                    sprintf(buf, "%02lu", seconds);
+                    // note that we're drawing the seconds in the "minutes" position, since this
+                    // watch face uses the "seconds" position for hundredths of seconds
+                    watch_display_text(WATCH_POSITION_MINUTES, buf);
+                    watch_display_character_lp_seconds('0' + sec_100 / 10, 8);
+                    watch_display_character_lp_seconds('0' + sec_100 % 10, 9);
+                }
+            } else {
+                // only draw 100ths of seconds
+                watch_display_character_lp_seconds('0' + sec_100 / 10, 8);
+                watch_display_character_lp_seconds('0' + sec_100 % 10, 9);
+            }
+        } else {
+            _display_ticks(_ticks);
+        }
+    }
+    if (_is_running) {
+        // blink the colon every half second
+        uint8_t blink_ticks = ((_ticks >> 6) & 1);
+        if (blink_ticks != _blink_ticks) {
+            _blink_ticks = blink_ticks;
+            _colon = !_colon;
+            if (_colon) watch_set_colon();
+            else watch_clear_colon();
+        }
+    }
+}
+
+static inline void _update_lap_indicator() {
+    if (_lap_ticks) watch_set_indicator(WATCH_INDICATOR_LAP);
+    else  watch_clear_indicator(WATCH_INDICATOR_LAP);
+}
+
+static inline void _set_colon() {
+    watch_set_colon();
+    _colon = true;
+}
+
+void fast_stopwatch_face_setup(uint8_t watch_face_index, void ** context_ptr) {
+    (void) watch_face_index;
+    if (*context_ptr == NULL) {
+        *context_ptr = malloc(sizeof(fast_stopwatch_state_t));
+        memset(*context_ptr, 0, sizeof(fast_stopwatch_state_t));
+        fast_stopwatch_state_t *state = (fast_stopwatch_state_t *)*context_ptr;
+        _ticks = _lap_ticks = _blink_ticks = _old_minutes = _old_seconds = _hours = 0;
+    _is_running = _colon = false;
+        state->light_on_button = true;
+    }
+    if (!_is_running) {
+        // prepare the 128 Hz callback source
+        _cb_initialize();
+    }
+}
+
+void fast_stopwatch_face_activate(void *context) {
+    (void) context;
+    if (_is_running) {
+        // The background task will keep the watch from entering low energy mode while the stopwatch is on screen.
+        movement_schedule_background_task(distant_future);
+    }
+}
+
+bool fast_stopwatch_face_loop(movement_event_t event, void *context) {
+    fast_stopwatch_state_t *state = (fast_stopwatch_state_t *)context;
+
+    // handle overflow of fast ticks
+    while (_ticks >= (128 * 60 * 60)) {
+        _ticks -= (128 * 60 * 60);
+        _hours++;
+        if (_hours >= 24) _hours -= 24;
+        // initiate a re-draw
+        _old_minutes = 59;
+    }
+
+    switch (event.event_type) {
+        case EVENT_ACTIVATE:
+            _set_colon();
+            watch_display_text_with_fallback(WATCH_POSITION_TOP_LEFT, "STW", "ST");
+            _update_lap_indicator();
+            if (_is_running) movement_request_tick_frequency(16);
+            _display_ticks(_lap_ticks ? _lap_ticks : _ticks);
+            break;
+        case EVENT_TICK:
+            _draw();
+            break;
+        case EVENT_LIGHT_LONG_PRESS:
+            // kind od hidden feature: long press toggles light on or off
+            state->light_on_button = !state->light_on_button;
+            if (state->light_on_button) movement_illuminate_led();
+            else watch_set_led_off();
+            break;
+        case EVENT_ALARM_BUTTON_DOWN:
+            _is_running = !_is_running;
+            if (_is_running) {
+                // start or continue stopwatch
+                movement_request_tick_frequency(16);
+                // register 128 hz callback for time measuring
+                _cb_start();
+                // schedule the keepalive task when running
+                movement_schedule_background_task(distant_future);
+            } else {
+                // stop the stopwatch
+                _cb_stop();
+                movement_request_tick_frequency(1);
+                _set_colon();
+                // cancel the keepalive task
+                movement_cancel_background_task();
+            }
+            _draw();
+            _button_beep();
+            break;
+        case EVENT_LIGHT_BUTTON_DOWN:
+            if (state->light_on_button) movement_illuminate_led();
+            if (_is_running) {
+                if (_lap_ticks) {
+                    // clear lap and continue running
+                    _lap_ticks = 0;
+                    movement_request_tick_frequency(16);
+                } else {
+                    // set lap ticks and stop updating the display
+                    _lap_ticks = _ticks;
+                    movement_request_tick_frequency(2);
+                    _set_colon();
+                }
+            } else {
+                if (_lap_ticks) {
+                    // clear lap and show running stopwatch
+                    _lap_ticks = 0;
+                } else if (_ticks) {
+                    // reset stopwatch
+                    _ticks = _lap_ticks = _blink_ticks = _old_minutes = _old_seconds = _hours = 0;
+                    _button_beep();
+                }
+            }
+            _display_ticks(_ticks);
+            _update_lap_indicator();
+            break;
+        case EVENT_TIMEOUT:
+            if (!_is_running) movement_move_to_face(0);
+            break;
+        case EVENT_LOW_ENERGY_UPDATE:
+            _draw();
+            break;
+        default:
+            movement_default_loop_handler(event);
+            break;
+    }
+    return true;
+}
+
+void fast_stopwatch_face_resign(void *context) {
+    (void) context;
+    // cancel the keepalive task
+    movement_cancel_background_task();
+}
diff --git a/watch-faces/complication/fast_stopwatch_face.h b/watch-faces/complication/fast_stopwatch_face.h
new file mode 100644 (file)
index 0000000..eac5665
--- /dev/null
@@ -0,0 +1,80 @@
+/*
+ * MIT License
+ *
+ * Copyright (c) 2022 Andreas Nebinger
+ *
+ * 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.
+ */
+
+#ifndef FAST_STOPWATCH_FACE_H_
+#define FAST_STOPWATCH_FACE_H_
+
+/*
+ * STOCK STOPWATCH face
+ *
+ * The Stock Stopwatch face implements the original F-91W stopwatch
+ * functionality, including counting hundredths of seconds and lap timing.
+ *
+ * Use the ALARM button to start and stop the stopwatch.
+ * Press the LIGHT button while the stopwatch is running to view the lap time.
+ *  (The stopwatch continues running in the background, indicated by a blinking colon.)
+ * Press the LIGHT button again to switch back to the running stopwatch.
+ * Press the LIGHT button when the timekeeping is stopped to reset the stopwatch.
+ *
+ * There are two improvements compared to the original F-91W:
+ *  o When the stopwatch reaches 59:59, the counter does not simply jump back
+ *    to zero but keeps track of hours in the upper right-hand corner
+ *    (up to 24 hours).
+ *  o Long-press the light button to toggle the LED behavior.
+ *    It either turns on with each button press or remains off.
+ *
+ * NOTE:
+ * This watch face relies heavily on static vars in fast_stopwatch.c.
+ * The disadvantage is that you cannot use more than one instance of this
+ * watch face on your custom firmware - but then again, who would want that?
+ * The advantage is that accessing vars is more direct and faster, and we
+ * can save some precious cpu cycles.  :-)
+ */
+
+#include "movement.h"
+
+typedef struct {
+    bool light_on_button;   // determines whether the light button actually triggers the led
+} fast_stopwatch_state_t;
+
+void fast_stopwatch_face_setup(uint8_t watch_face_index, void ** context_ptr);
+void fast_stopwatch_face_activate(void *context);
+bool fast_stopwatch_face_loop(movement_event_t event, void *context);
+void fast_stopwatch_face_resign(void *context);
+
+#if __EMSCRIPTEN__
+void em_cb_handler(void *userData);
+#else
+void TC2_Handler(void);
+#endif
+
+#define fast_stopwatch_face ((const watch_face_t){ \
+    fast_stopwatch_face_setup, \
+    fast_stopwatch_face_activate, \
+    fast_stopwatch_face_loop, \
+    fast_stopwatch_face_resign, \
+    NULL, \
+})
+
+#endif // FAST_STOPWATCH_FACE_H_
diff --git a/watch-faces/complication/stock_stopwatch_face.c b/watch-faces/complication/stock_stopwatch_face.c
deleted file mode 100644 (file)
index 7c86ffc..0000000
+++ /dev/null
@@ -1,328 +0,0 @@
-/*
- * MIT License
- *
- * Copyright (c) 2022 Andreas Nebinger
- *
- * 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 <stdlib.h>
-#include <string.h>
-#include "stock_stopwatch_face.h"
-#include "watch.h"
-#include "watch_common_display.h"
-#include "watch_utility.h"
-#include "watch_rtc.h"
-
-/*
-    This watch face implements the original F-91W stopwatch functionality
-    including counting hundredths of seconds and lap timing. There are two
-    improvements compared to the functionality of the original: 
-    1. When reaching 59:59 the counter does not simply jump back to zero,
-       but keeps track of hours in the upper right hand corner. (Up to 24h)
-    2. Long pressing the light button toggles the led behaviour: it either
-       turns on on each button press or it doesn't.
-*/
-
-#if __EMSCRIPTEN__
-#include <emscripten.h>
-#include <emscripten/html5.h>
-#else
-#include "tc.h"
-#endif
-
-// distant future for background task: January 1, 2083
-static const watch_date_time distant_future = {
-    .unit = {0, 0, 0, 1, 1, 63}
-};
-
-static uint32_t _ticks;
-static uint32_t _lap_ticks;
-static uint8_t _blink_ticks;
-static uint32_t _old_seconds;
-static uint8_t _old_minutes;
-static uint8_t _hours;
-static bool _colon;
-static bool _is_running;
-
-#if __EMSCRIPTEN__
-
-static long _em_interval_id = 0;
-
-void em_cb_handler(void *userData) {
-    // interrupt handler for emscripten 128 Hz callbacks
-    (void) userData;
-    _ticks++;
-}
-
-static void _cb_initialize() { }
-
-static inline void _cb_stop() {
-    emscripten_clear_interval(_em_interval_id);
-    _em_interval_id = 0;
-    _is_running = false;
-}
-
-static inline void _cb_start() {
-    // initiate 128 hz callback
-    _em_interval_id = emscripten_set_interval(em_cb_handler, (double)(1000/128), (void *)NULL);
-}
-
-#else
-
-static inline void _cb_start() {
-    // start the TC1 timer
-    tc_enable(1);
-    _is_running = true;
-}
-
-static inline void _cb_stop() {
-    // stop the TC1 timer
-    tc_disable(1);
-    _is_running = false;
-}
-
-static void _cb_initialize() {
-    tc_init(1, GENERIC_CLOCK_3, TC_PRESCALER_DIV4);
-    tc_set_counter_mode(1, TC_COUNTER_MODE_8BIT);
-    tc_set_run_in_standby(1, true);
-    _cb_stop();
-    tc_count8_set_period(1, 1); // 1024 Hz divided by 4 divided by 2 results in a 128 Hz interrupt
-    /// FIXME: #SecondMovement, we need a gossamer wrapper for interrupts.
-    TC1->COUNT8.INTENSET.bit.OVF = 1;
-    NVIC_ClearPendingIRQ(TC1_IRQn);
-    NVIC_EnableIRQ (TC1_IRQn);
-}
-
-void irq_handler_tc1(void);
-void irq_handler_tc1(void) {
-    // interrupt handler for TC1 (globally!)
-    _ticks++;
-    TC1->COUNT8.INTFLAG.reg |= TC_INTFLAG_OVF;
-}
-
-#endif
-
-static inline void _button_beep() {
-    // play a beep as confirmation for a button press (if applicable)
-    if (movement_button_should_sound()) watch_buzzer_play_note(BUZZER_NOTE_C7, 50);
-}
-
-/// @brief Display minutes, seconds and fractions derived from 128 Hz tick counter
-///        on the lcd.
-/// @param ticks
-static void _display_ticks(uint32_t ticks) {
-    char buf[14];
-    uint8_t sec_100 = (ticks & 0x7F) * 100 / 128;
-    uint32_t seconds = ticks >> 7;
-    uint32_t minutes = seconds / 60;
-    if (_hours) {
-        sprintf(buf, "%2u", _hours);
-        watch_display_text(WATCH_POSITION_TOP_RIGHT, buf);
-    } else {
-        watch_display_text(WATCH_POSITION_TOP_RIGHT, "  ");
-    }
-
-    sprintf(buf, "%02lu%02lu%02u", minutes, (seconds % 60), sec_100);
-    watch_display_text(WATCH_POSITION_BOTTOM, buf);
-}
-
-/// @brief Displays the current stopwatch time on the LCD (more optimized than _display_ticks())
-static void _draw() {
-    if (_lap_ticks == 0) {
-        char buf[14];
-        uint8_t sec_100 = (_ticks & 0x7F) * 100 / 128;
-        if (_is_running) {
-            uint32_t seconds = _ticks >> 7;
-            if (seconds != _old_seconds) {
-                // seconds have changed
-                _old_seconds = seconds;
-                uint8_t minutes = seconds / 60;
-                seconds %= 60;
-                if (minutes != _old_minutes) {
-                    // minutes have changed, draw everything
-                    _old_minutes = minutes;
-                    minutes %= 60;
-                    if (_hours) {
-                        // with hour indicator
-                        sprintf(buf, "%2u", _hours);
-                        watch_display_text(WATCH_POSITION_TOP_RIGHT, buf);
-                    } else {
-                        // no hour indicator
-                        watch_display_text(WATCH_POSITION_TOP_RIGHT, "  ");
-                    }
-                    sprintf(buf, "%02u%02lu%02u", minutes, seconds, sec_100);
-                    watch_display_text(WATCH_POSITION_BOTTOM, buf);
-                } else {
-                    // just draw seconds
-                    sprintf(buf, "%02lu", seconds);
-                    // note that we're drawing the seconds in the "minutes" position, since this
-                    // watch face uses the "seconds" position for hundredths of seconds
-                    watch_display_text(WATCH_POSITION_MINUTES, buf);
-                    watch_display_character_lp_seconds('0' + sec_100 / 10, 8);
-                    watch_display_character_lp_seconds('0' + sec_100 % 10, 9);
-                }
-            } else {
-                // only draw 100ths of seconds
-                watch_display_character_lp_seconds('0' + sec_100 / 10, 8);
-                watch_display_character_lp_seconds('0' + sec_100 % 10, 9);
-            }
-        } else {
-            _display_ticks(_ticks);
-        }
-    }
-    if (_is_running) {
-        // blink the colon every half second
-        uint8_t blink_ticks = ((_ticks >> 6) & 1);
-        if (blink_ticks != _blink_ticks) {
-            _blink_ticks = blink_ticks;
-            _colon = !_colon;
-            if (_colon) watch_set_colon();
-            else watch_clear_colon();
-        }
-    }
-}
-
-static inline void _update_lap_indicator() {
-    if (_lap_ticks) watch_set_indicator(WATCH_INDICATOR_LAP);
-    else  watch_clear_indicator(WATCH_INDICATOR_LAP);
-}
-
-static inline void _set_colon() {
-    watch_set_colon();
-    _colon = true;
-}
-
-void stock_stopwatch_face_setup(uint8_t watch_face_index, void ** context_ptr) {
-    (void) watch_face_index;
-    if (*context_ptr == NULL) {
-        *context_ptr = malloc(sizeof(stock_stopwatch_state_t));
-        memset(*context_ptr, 0, sizeof(stock_stopwatch_state_t));
-        stock_stopwatch_state_t *state = (stock_stopwatch_state_t *)*context_ptr;
-        _ticks = _lap_ticks = _blink_ticks = _old_minutes = _old_seconds = _hours = 0;
-    _is_running = _colon = false;
-        state->light_on_button = true;
-    }
-    if (!_is_running) {
-        // prepare the 128 Hz callback source
-        _cb_initialize();
-    }
-}
-
-void stock_stopwatch_face_activate(void *context) {
-    (void) context;
-    if (_is_running) {
-        // The background task will keep the watch from entering low energy mode while the stopwatch is on screen.
-        movement_schedule_background_task(distant_future);
-    }
-}
-
-bool stock_stopwatch_face_loop(movement_event_t event, void *context) {
-    stock_stopwatch_state_t *state = (stock_stopwatch_state_t *)context;
-
-    // handle overflow of fast ticks
-    while (_ticks >= (128 * 60 * 60)) {
-        _ticks -= (128 * 60 * 60);
-        _hours++;
-        if (_hours >= 24) _hours -= 24;
-        // initiate a re-draw
-        _old_minutes = 59;
-    }
-
-    switch (event.event_type) {
-        case EVENT_ACTIVATE:
-            _set_colon();
-            watch_display_text_with_fallback(WATCH_POSITION_TOP_LEFT, "STW", "ST");
-            _update_lap_indicator();
-            if (_is_running) movement_request_tick_frequency(16);
-            _display_ticks(_lap_ticks ? _lap_ticks : _ticks);
-            break;
-        case EVENT_TICK:
-            _draw();
-            break;
-        case EVENT_LIGHT_LONG_PRESS:
-            // kind od hidden feature: long press toggles light on or off
-            state->light_on_button = !state->light_on_button;
-            if (state->light_on_button) movement_illuminate_led();
-            else watch_set_led_off();
-            break;
-        case EVENT_ALARM_BUTTON_DOWN:
-            _is_running = !_is_running;
-            if (_is_running) {
-                // start or continue stopwatch
-                movement_request_tick_frequency(16);
-                // register 128 hz callback for time measuring
-                _cb_start();
-                // schedule the keepalive task when running
-                movement_schedule_background_task(distant_future);
-            } else {
-                // stop the stopwatch
-                _cb_stop();
-                movement_request_tick_frequency(1);
-                _set_colon();
-                // cancel the keepalive task
-                movement_cancel_background_task();
-            }
-            _draw();
-            _button_beep();
-            break;
-        case EVENT_LIGHT_BUTTON_DOWN:
-            if (state->light_on_button) movement_illuminate_led();
-            if (_is_running) {
-                if (_lap_ticks) {
-                    // clear lap and continue running
-                    _lap_ticks = 0;
-                    movement_request_tick_frequency(16);
-                } else {
-                    // set lap ticks and stop updating the display
-                    _lap_ticks = _ticks;
-                    movement_request_tick_frequency(2);
-                    _set_colon();
-                }
-            } else {
-                if (_lap_ticks) {
-                    // clear lap and show running stopwatch
-                    _lap_ticks = 0;
-                } else if (_ticks) {
-                    // reset stopwatch
-                    _ticks = _lap_ticks = _blink_ticks = _old_minutes = _old_seconds = _hours = 0;
-                    _button_beep();
-                }
-            }
-            _display_ticks(_ticks);
-            _update_lap_indicator();
-            break;
-        case EVENT_TIMEOUT:
-            if (!_is_running) movement_move_to_face(0);
-            break;
-        case EVENT_LOW_ENERGY_UPDATE:
-            _draw();
-            break;
-        default:
-            movement_default_loop_handler(event);
-            break;
-    }
-    return true;
-}
-
-void stock_stopwatch_face_resign(void *context) {
-    (void) context;
-    // cancel the keepalive task
-    movement_cancel_background_task();
-}
diff --git a/watch-faces/complication/stock_stopwatch_face.h b/watch-faces/complication/stock_stopwatch_face.h
deleted file mode 100644 (file)
index 349c166..0000000
+++ /dev/null
@@ -1,80 +0,0 @@
-/*
- * MIT License
- *
- * Copyright (c) 2022 Andreas Nebinger
- *
- * 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.
- */
-
-#ifndef STOCK_STOPWATCH_FACE_H_
-#define STOCK_STOPWATCH_FACE_H_
-
-/*
- * STOCK STOPWATCH face
- *
- * The Stock Stopwatch face implements the original F-91W stopwatch
- * functionality, including counting hundredths of seconds and lap timing.
- *
- * Use the ALARM button to start and stop the stopwatch.
- * Press the LIGHT button while the stopwatch is running to view the lap time.
- *  (The stopwatch continues running in the background, indicated by a blinking colon.)
- * Press the LIGHT button again to switch back to the running stopwatch.
- * Press the LIGHT button when the timekeeping is stopped to reset the stopwatch.
- *
- * There are two improvements compared to the original F-91W:
- *  o When the stopwatch reaches 59:59, the counter does not simply jump back
- *    to zero but keeps track of hours in the upper right-hand corner
- *    (up to 24 hours).
- *  o Long-press the light button to toggle the LED behavior.
- *    It either turns on with each button press or remains off.
- *
- * NOTE:
- * This watch face relies heavily on static vars in stock_stopwatch.c.
- * The disadvantage is that you cannot use more than one instance of this
- * watch face on your custom firmware - but then again, who would want that?
- * The advantage is that accessing vars is more direct and faster, and we
- * can save some precious cpu cycles.  :-)
- */
-
-#include "movement.h"
-
-typedef struct {
-    bool light_on_button;   // determines whether the light button actually triggers the led
-} stock_stopwatch_state_t;
-
-void stock_stopwatch_face_setup(uint8_t watch_face_index, void ** context_ptr);
-void stock_stopwatch_face_activate(void *context);
-bool stock_stopwatch_face_loop(movement_event_t event, void *context);
-void stock_stopwatch_face_resign(void *context);
-
-#if __EMSCRIPTEN__
-void em_cb_handler(void *userData);
-#else
-void TC2_Handler(void);
-#endif
-
-#define stock_stopwatch_face ((const watch_face_t){ \
-    stock_stopwatch_face_setup, \
-    stock_stopwatch_face_activate, \
-    stock_stopwatch_face_loop, \
-    stock_stopwatch_face_resign, \
-    NULL, \
-})
-
-#endif // STOCK_STOPWATCH_FACE_H_