]> git.earman.xyz Git - sensor-watch.git/commitdiff
Port Breathing face to second movement, also add LED notification. General fixes...
authorDaniel Bergman <danber@gmail.com>
Sun, 6 Jul 2025 15:58:16 +0000 (17:58 +0200)
committerGitHub <noreply@github.com>
Sun, 6 Jul 2025 15:58:16 +0000 (11:58 -0400)
legacy/watch_faces/complication/breathing_face.c [deleted file]
legacy/watch_faces/complication/breathing_face.h [deleted file]
movement_faces.h
watch-faces.mk
watch-faces/complication/breathing_face.c [new file with mode: 0644]
watch-faces/complication/breathing_face.h [new file with mode: 0644]

diff --git a/legacy/watch_faces/complication/breathing_face.c b/legacy/watch_faces/complication/breathing_face.c
deleted file mode 100644 (file)
index c9ea67a..0000000
+++ /dev/null
@@ -1,204 +0,0 @@
-/*
- * MIT License
- *
- * Copyright (c) 2023 Bernd Plontsch
- *
- * 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 "breathing_face.h"
-#include "watch.h"
-
-typedef struct {
-    uint8_t current_stage;
-    bool sound_on;
-} breathing_state_t;
-
-static void beep_in (void);
-static void beep_in_hold (void);
-static void beep_out (void);
-static void beep_out_hold (void);
-
-void breathing_face_setup(uint8_t watch_face_index, void ** context_ptr) {
-    // These next two lines just silence the compiler warnings associated with unused parameters.
-    // We have no use for the settings or the watch_face_index, so we make that explicit here.
-    (void) watch_face_index;
-    // At boot, context_ptr will be NULL indicating that we don't have anyplace to store our context.
-    if (*context_ptr == NULL) {
-        // in this case, we allocate an area of memory sufficient to store the stuff we need to track.
-        *context_ptr = malloc(sizeof(breathing_state_t));
-    }
-}
-
-void breathing_face_activate(void *context) {
-    // same as above: silence the warning, we don't need to check the settings.
-    // we do however need to set some things in our context. Here we cast it to the correct type...
-    breathing_state_t *state = (breathing_state_t *)context;
-    // ...and set the initial state of our watch face.
-    state->current_stage = 0;
-    state->sound_on = true;
-}
-
-const int NOTE_LENGTH = 80;
-
-void beep_in (void) {
-        const watch_buzzer_note_t notes[] = {
-            BUZZER_NOTE_C4,
-            BUZZER_NOTE_D4,
-            BUZZER_NOTE_E4,
-        };
-        const uint16_t durations[] = {
-            NOTE_LENGTH,
-            NOTE_LENGTH,
-            NOTE_LENGTH
-        };
-        for(size_t i = 0, count = sizeof(notes) / sizeof(notes[0]); i < count; i++) {
-            watch_buzzer_play_note(notes[i], durations[i]);
-        }
-}
-
-void beep_in_hold (void) {
-        const watch_buzzer_note_t notes[] = {
-            BUZZER_NOTE_E4,
-            BUZZER_NOTE_REST,
-            BUZZER_NOTE_E4,
-        };
-        const uint16_t durations[] = {
-            NOTE_LENGTH,
-            NOTE_LENGTH * 2,
-            NOTE_LENGTH,
-        };
-        for(size_t i = 0, count = sizeof(notes) / sizeof(notes[0]); i < count; i++) {
-            watch_buzzer_play_note(notes[i], durations[i]);
-        }
-}
-
-void beep_out (void) {
-        const watch_buzzer_note_t notes[] = {
-            BUZZER_NOTE_E4,
-            BUZZER_NOTE_D4,
-            BUZZER_NOTE_C4,
-        };
-        const uint16_t durations[] = {
-            NOTE_LENGTH,
-            NOTE_LENGTH,
-            NOTE_LENGTH,
-        };
-        for(size_t i = 0, count = sizeof(notes) / sizeof(notes[0]); i < count; i++) {
-            watch_buzzer_play_note(notes[i], durations[i]);
-        }
-}
-
-void beep_out_hold (void) {
-        const watch_buzzer_note_t notes[] = {
-            BUZZER_NOTE_C4,
-            BUZZER_NOTE_REST * 2,
-            BUZZER_NOTE_C4,
-        };
-        const uint16_t durations[] = {
-            NOTE_LENGTH,
-            NOTE_LENGTH,
-            NOTE_LENGTH,
-        };
-        for(size_t i = 0, count = sizeof(notes) / sizeof(notes[0]); i < count; i++) {
-            watch_buzzer_play_note(notes[i], durations[i]);
-        }
-}
-
-bool breathing_face_loop(movement_event_t event, void *context) {
-    breathing_state_t *state = (breathing_state_t *)context;
-
-    switch (event.event_type) {
-        case EVENT_ACTIVATE:
-        case EVENT_TICK:
-
-            if (state->sound_on == true) {
-                watch_set_indicator(WATCH_INDICATOR_BELL); 
-            } else {
-                watch_clear_indicator(WATCH_INDICATOR_BELL); 
-            }
-
-            switch (state->current_stage)
-            {
-            case 0: { watch_display_string("Breath", 4); if (state->sound_on) beep_in(); } break;
-            case 1: watch_display_string("In   3", 4); break;
-            case 2: watch_display_string("In   2", 4); break;
-            case 3: watch_display_string("In   1", 4); break;
-            
-            case 4: { watch_display_string("Hold 4", 4); if (state->sound_on) beep_in_hold(); } break;
-            case 5: watch_display_string("Hold 3", 4); break;
-            case 6: watch_display_string("Hold 2", 4); break;               
-            case 7:  watch_display_string("Hold 1", 4); break;
-
-            case 8: { watch_display_string("Ou t 4", 4); if (state->sound_on) beep_out(); } break;
-            case 9: watch_display_string("Ou t 3", 4); break;
-            case 10: watch_display_string("Ou t 2", 4); break;
-            case 11: watch_display_string("Ou t 1", 4); break;         
-            
-            case 12: { watch_display_string("Hold 4", 4); if (state->sound_on) beep_out_hold(); } break;
-            case 13: watch_display_string("Hold 3", 4); break;
-            case 14: watch_display_string("Hold 2", 4); break;     
-            case 15: watch_display_string("Hold 1", 4); break;     
-            default:
-                break;
-            }
-
-            // and increment it so that it will update on the next tick.
-            state->current_stage = (state->current_stage + 1) % 16;
-
-            break;
-        case EVENT_ALARM_BUTTON_UP:
-            state->sound_on = !state->sound_on;            
-             if (state->sound_on == true) {
-                watch_set_indicator(WATCH_INDICATOR_BELL); 
-            } else {
-                watch_clear_indicator(WATCH_INDICATOR_BELL); 
-            }
-            break;
-        case EVENT_LOW_ENERGY_UPDATE:
-            // This low energy mode update occurs once a minute, if the watch face is in the
-            // foreground when Movement enters low energy mode. We have the option of supporting
-            // this mode, but since our watch face animates once a second, the "Hello there" face
-            // isn't very useful in this mode. So we choose not to support it. (continued below)
-            break;
-        case EVENT_TIMEOUT:
-            // ... Instead, we respond to the timeout event. This event happens after a configurable
-            // interval on screen (1-30 minutes). The watch will give us this event as a chance to
-            // resign control if we want to, and in this case, we do.
-            // This function will return the watch to the first screen (usually a simple clock),
-            // and it will do it long before the watch enters low energy mode. This ensures we
-            // won't be on screen, and thus opts us out of getting the EVENT_LOW_ENERGY_UPDATE above.
-            
-            // movement_move_to_face(0);
-            break;
-        default:
-            movement_default_loop_handler(event);
-            break;
-    }
-
-    return true;
-}
-
-void breathing_face_resign(void *context) {
-    // our watch face, like most watch faces, has nothing special to do when resigning.
-    // watch faces that enable a peripheral or interact with a sensor may want to turn it off here.
-    (void) context;
-}
diff --git a/legacy/watch_faces/complication/breathing_face.h b/legacy/watch_faces/complication/breathing_face.h
deleted file mode 100644 (file)
index 91ef2a6..0000000
+++ /dev/null
@@ -1,54 +0,0 @@
-/*
- * MIT License
- *
- * Copyright (c) 2023 Bernd Plontsch
- *
- * 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 BREATHING_FACE_H_
-#define BREATHING_FACE_H_
-
-/*
- * BOXED BREATHING face
- *
- * Breathing is a complication for guiding boxed breathing sessions.
- * Boxed breathing is a technique to help you stay calm and improve
- * concentration in stressful situations.
- *
- * Usage: Timed messages will cycle as long as this face is active.
- * Press ALARM to toggle sound.
- */
-
-#include "movement.h"
-
-void breathing_face_setup(uint8_t watch_face_index, void ** context_ptr);
-void breathing_face_activate(void *context);
-bool breathing_face_loop(movement_event_t event, void *context);
-void breathing_face_resign(void *context);
-
-#define breathing_face ((const watch_face_t){ \
-    breathing_face_setup, \
-    breathing_face_activate, \
-    breathing_face_loop, \
-    breathing_face_resign, \
-    NULL, \
-})
-
-#endif // BREATHING_FACE_H_
index 145d6eabc07649a178e7cb7dc57f6a73eafce182..ab02b915ec948488a4ce9fc91f63bb1a36434763 100644 (file)
@@ -51,6 +51,7 @@
 #include "nanosec_face.h"
 #include "mars_time_face.h"
 #include "peek_memory_face.h"
+#include "breathing_face.h"
 #include "close_enough_face.h"
 #include "tarot_face.h"
 #include "kitchen_conversions_face.h"
index 06a81231e210e391326f3302ccec2dce10333347..15b1bf1795b4695d1808ab9098627a008ce79b2e 100644 (file)
@@ -11,6 +11,7 @@ SRCS += \
   ./watch-faces/complication/sunrise_sunset_face.c \
   ./watch-faces/complication/moon_phase_face.c \
   ./watch-faces/complication/days_since_face.c \
+  ./watch-faces/complication/breathing_face.c \
   ./watch-faces/complication/squash_face.c \
   ./watch-faces/complication/totp_face.c \
   ./watch-faces/demo/all_segments_face.c \
diff --git a/watch-faces/complication/breathing_face.c b/watch-faces/complication/breathing_face.c
new file mode 100644 (file)
index 0000000..dd6297e
--- /dev/null
@@ -0,0 +1,202 @@
+/*
+ * MIT License
+ *
+ * Copyright (c) 2023 Bernd Plontsch
+ * Copyright (c) 2025 Daniel Bergman
+ *
+ * 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 "breathing_face.h"
+#include "watch.h"
+
+typedef struct {
+    uint8_t current_stage;
+    uint8_t indication_mode; // 0 = sound only, 1 = LED only, 2 = all off
+    uint8_t led_on_state; // 0 = LED off, 1 = LED on
+} breathing_state_t;
+
+static void update_indicators(breathing_state_t *state);
+
+const int NOTE_LENGTH = 80;
+static const watch_buzzer_note_t IN_NOTES[] = { BUZZER_NOTE_C4, BUZZER_NOTE_D4, BUZZER_NOTE_E4 };
+static const uint16_t IN_DUR[] = { NOTE_LENGTH, NOTE_LENGTH, NOTE_LENGTH };
+static const watch_buzzer_note_t IN_HOLD_NOTES[] = { BUZZER_NOTE_E4, BUZZER_NOTE_REST, BUZZER_NOTE_E4 };
+static const uint16_t IN_HOLD_DUR[] = { NOTE_LENGTH, NOTE_LENGTH * 2, NOTE_LENGTH };
+static const watch_buzzer_note_t OUT_NOTES[] = { BUZZER_NOTE_E4, BUZZER_NOTE_D4, BUZZER_NOTE_C4 };
+static const uint16_t OUT_DUR[] = { NOTE_LENGTH, NOTE_LENGTH, NOTE_LENGTH };
+static const watch_buzzer_note_t OUT_HOLD_NOTES[] = { BUZZER_NOTE_C4, BUZZER_NOTE_REST * 2, BUZZER_NOTE_C4 };
+static const uint16_t OUT_HOLD_DUR[] = { NOTE_LENGTH, NOTE_LENGTH, NOTE_LENGTH };
+
+void breathing_face_setup(uint8_t watch_face_index, void ** context_ptr) {
+    (void) watch_face_index; // Unused parameter
+    if (*context_ptr == NULL) {
+        breathing_state_t *state = malloc(sizeof(breathing_state_t));
+        state->current_stage = 0;
+        state->indication_mode = 0; // Start with sound only
+        state->led_on_state = 0;
+        *context_ptr = state;
+    }
+}
+
+void breathing_face_activate(void *context) {
+    breathing_state_t *state = (breathing_state_t *)context;
+    state->current_stage = 0;
+    update_indicators(state);
+}
+
+static void update_indicators(breathing_state_t *state) {
+    switch (state->indication_mode) {
+        case 0: // sound only
+            watch_set_indicator(WATCH_INDICATOR_BELL);
+            watch_clear_indicator(WATCH_INDICATOR_SIGNAL);
+            break;
+        case 1: // LED only
+            watch_clear_indicator(WATCH_INDICATOR_BELL);
+            watch_set_indicator(WATCH_INDICATOR_SIGNAL);
+            break;
+        case 2: // all off
+        default:
+            watch_clear_indicator(WATCH_INDICATOR_BELL);
+            watch_clear_indicator(WATCH_INDICATOR_SIGNAL);
+            break;
+    }
+}
+
+static void breathe_notify(breathing_state_t *state, const watch_buzzer_note_t *notes, const uint16_t *durations, size_t count, bool use_red_led) {
+    if (state->indication_mode == 2) return;
+    for (size_t i = 0; i < count; i++) {
+        if (state->indication_mode == 1 && notes[i] != BUZZER_NOTE_REST) {
+            if (use_red_led) watch_set_led_red();
+            else watch_set_led_green();
+            state->led_on_state = 1;
+        } else if (state->indication_mode == 0) {
+            watch_buzzer_play_note(notes[i], durations[i]);
+        }
+    }
+}
+
+bool breathing_face_loop(movement_event_t event, void *context) {
+    breathing_state_t *state = (breathing_state_t *)context;
+
+    switch (event.event_type) {
+        case EVENT_ACTIVATE:
+        case EVENT_TICK:
+            if (state->led_on_state == 1) {
+                watch_set_led_off();
+                state->led_on_state = 0;
+            }
+
+            switch (state->current_stage) {
+              case 0: {
+                watch_display_text_with_fallback(WATCH_POSITION_BOTTOM, "Breath", "Breath");
+                if (state->indication_mode != 2)
+                  breathe_notify(state, IN_NOTES, IN_DUR, 3, false);
+                break;
+              }
+              case 1:
+                watch_display_text_with_fallback(WATCH_POSITION_BOTTOM, "ln   3", "In   3");
+                break;
+              case 2:
+                watch_display_text_with_fallback(WATCH_POSITION_BOTTOM, "ln   2", "In   2");
+                break;
+              case 3:
+                watch_display_text_with_fallback(WATCH_POSITION_BOTTOM, "ln   1", "In   1");
+                break;
+
+              case 4: {
+                watch_display_text_with_fallback(WATCH_POSITION_BOTTOM, "Hold 4", "Hold 4");
+                if (state->indication_mode != 2)
+                  breathe_notify(state, IN_HOLD_NOTES, IN_HOLD_DUR, 3, true);
+                break;
+              }
+              case 5:
+                watch_display_text_with_fallback(WATCH_POSITION_BOTTOM, "Hold 3", "Hold 3");
+                break;
+              case 6:
+                watch_display_text_with_fallback(WATCH_POSITION_BOTTOM, "Hold 2", "Hold 2");
+                break;
+              case 7:
+                watch_display_text_with_fallback(WATCH_POSITION_BOTTOM, "Hold 1", "Hold 1");
+                break;
+
+              case 8: {
+                watch_display_text_with_fallback(WATCH_POSITION_BOTTOM, "Out  4", "Ou t 4");
+                if (state->indication_mode != 2)
+                  breathe_notify(state, OUT_NOTES, OUT_DUR, 3, false);
+                break;
+              }
+              case 9:
+                watch_display_text_with_fallback(WATCH_POSITION_BOTTOM, "Out  3", "Ou t 3");
+                break;
+              case 10:
+                watch_display_text_with_fallback(WATCH_POSITION_BOTTOM, "Out  2", "Ou t 2");
+                break;
+              case 11:
+                watch_display_text_with_fallback(WATCH_POSITION_BOTTOM, "Out  1", "Ou t 1");
+                break;
+
+              case 12: {
+                watch_display_text_with_fallback(WATCH_POSITION_BOTTOM, "Hold 4", "Hold 4");
+                if (state->indication_mode != 2)
+                  breathe_notify(state, OUT_HOLD_NOTES, OUT_HOLD_DUR, 3, true);
+                break;
+              }
+              case 13:
+                watch_display_text_with_fallback(WATCH_POSITION_BOTTOM, "Hold 3", "Hold 3");
+                break;
+              case 14:
+                watch_display_text_with_fallback(WATCH_POSITION_BOTTOM, "Hold 2", "Hold 2");
+                break;
+              case 15:
+                watch_display_text_with_fallback(WATCH_POSITION_BOTTOM, "Hold 1", "Hold 1");
+                break;
+              default:
+                break;
+            }
+
+            // and increment it so that it will update on the next tick.
+            state->current_stage = (state->current_stage + 1) % 16;
+
+            break;
+        case EVENT_ALARM_BUTTON_UP:
+            // Cycle through the indication modes
+            state->indication_mode = (state->indication_mode + 1) % 3;
+            update_indicators(state);
+            break;
+        case EVENT_LOW_ENERGY_UPDATE:
+            // We don't want to go to sleep while we're breathing.
+            movement_request_wake();
+            break;
+        case EVENT_TIMEOUT:
+            // We stay in this face until the user chooses to exit
+            break;
+        default:
+            movement_default_loop_handler(event);
+            break;
+    }
+
+    return true;
+}
+
+void breathing_face_resign(void *context) {
+    (void) context; // Silence unused parameter warning
+    watch_set_led_off();
+}
diff --git a/watch-faces/complication/breathing_face.h b/watch-faces/complication/breathing_face.h
new file mode 100644 (file)
index 0000000..a33f467
--- /dev/null
@@ -0,0 +1,54 @@
+/*
+ * MIT License
+ *
+ * Copyright (c) 2023 Bernd Plontsch
+ *
+ * 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 BREATHING_FACE_H_
+#define BREATHING_FACE_H_
+
+/*
+ * BOXED BREATHING face
+ *
+ * Breathing is a complication for guiding boxed breathing sessions.
+ * Boxed breathing is a technique to help you stay calm and improve
+ * concentration in stressful situations.
+ *
+ * Usage: Timed messages will cycle as long as this face is active.
+ * Press ALARM to toggle between sound and LED indication for phases.
+ */
+
+#include "movement.h"
+
+void breathing_face_setup(uint8_t watch_face_index, void ** context_ptr);
+void breathing_face_activate(void *context);
+bool breathing_face_loop(movement_event_t event, void *context);
+void breathing_face_resign(void *context);
+
+#define breathing_face ((const watch_face_t){ \
+    breathing_face_setup, \
+    breathing_face_activate, \
+    breathing_face_loop, \
+    breathing_face_resign, \
+    NULL, \
+})
+
+#endif // BREATHING_FACE_H_