]> git.earman.xyz Git - sensor-watch.git/commitdiff
pulseometer: support variable update frequency
authorJoey Castillo <jose.castillo@gmail.com>
Wed, 6 Oct 2021 16:03:02 +0000 (12:03 -0400)
committerJoey Castillo <jose.castillo@gmail.com>
Wed, 6 Oct 2021 16:09:54 +0000 (12:09 -0400)
launcher/widgets/complications/pulseometer_widget.c

index a7272dcb60d571e5aa99d5d81c6846cca0579207..7b3f3efe28feb5629920b51ef40e342593a733e7 100644 (file)
@@ -3,6 +3,9 @@
 #include "pulseometer_widget.h"
 #include "watch.h"
 
+#define PULSOMETER_WIDGET_FREQUENCY_FACTOR (4ul) // refresh rate will be 2 to this power Hz (0 for 1 Hz, 2 for 4 Hz, etc.)
+#define PULSOMETER_WIDGET_FREQUENCY (1 << PULSOMETER_WIDGET_FREQUENCY_FACTOR)
+
 void pulseometer_widget_setup(LauncherSettings *settings, void ** context_ptr) {
     (void) settings;
     if (*context_ptr == NULL) *context_ptr = malloc(sizeof(PulsometerState));
@@ -18,8 +21,6 @@ bool pulseometer_widget_loop(LauncherEvent event, LauncherSettings *settings, vo
     (void) settings;
     PulsometerState *pulsometer_state = (PulsometerState *)context;
     char buf[14];
-    // starts at index 15
-    const uint8_t pulse_lookup[] = {240, 225, 212, 200, 189, 180, 171, 164, 157, 150, 144, 138, 133, 129, 124, 120, 116, 113, 109, 106, 103, 100, 97, 95, 92, 90, 88, 86, 84, 82, 80, 78, 77, 75, 73, 72, 71, 69, 68, 67, 65, 64, 63, 62, 61, 60, 59, 58, 57, 56, 55, 55, 54, 53, 52, 51, 51, 50, 49, 49, 48, 47, 47, 46, 46, 45, 44, 44, 43, 43, 42, 42, 41, 41, 40, 40, 40};
     switch (event.event_type) {
         case EVENT_TICK:
             if (pulsometer_state->pulse == 0 && !pulsometer_state->measuring) {
@@ -40,14 +41,16 @@ bool pulseometer_widget_loop(LauncherEvent event, LauncherSettings *settings, vo
                         watch_clear_display();
                         break;
                 }
-                pulsometer_state->ticks++;
+                pulsometer_state->ticks = (pulsometer_state->ticks + 1) % 5;
             } else {
-                if (pulsometer_state->ticks < 15) {
-                    watch_display_string("        Lo", 0);
-                } else if (pulsometer_state->ticks > 91) {
+                if (pulsometer_state->measuring && pulsometer_state->ticks) {
+                    pulsometer_state->pulse = (int16_t)((30.0 * ((float)(60 << PULSOMETER_WIDGET_FREQUENCY_FACTOR) / (float)pulsometer_state->ticks)) + 0.5);
+                }
+                if (pulsometer_state->pulse > 240) {
                     watch_display_string("        Hi", 0);
+                } else if (pulsometer_state->pulse < 40) {
+                    watch_display_string("        Lo", 0);
                 } else {
-                    if (pulsometer_state->measuring) pulsometer_state->pulse = pulse_lookup[pulsometer_state->ticks - 15];
                     sprintf(buf, "    %-3dbpn", pulsometer_state->pulse);
                     watch_display_string(buf, 0);
                 }
@@ -62,8 +65,9 @@ bool pulseometer_widget_loop(LauncherEvent event, LauncherSettings *settings, vo
             break;
         case EVENT_ALARM_BUTTON_DOWN:
             pulsometer_state->ticks = 0;
+            pulsometer_state->pulse = 0xFFFF;
             pulsometer_state->measuring = true;
-            launcher_request_tick_frequency(2);
+            launcher_request_tick_frequency(PULSOMETER_WIDGET_FREQUENCY);
             break;
         case EVENT_ALARM_BUTTON_UP:
         case EVENT_ALARM_LONG_PRESS: