]> git.earman.xyz Git - sensor-watch.git/commitdiff
rewrite lis2dh logger for lis2dw
authorJoey Castillo <joeycastillo@utexas.edu>
Sun, 10 Apr 2022 22:08:37 +0000 (18:08 -0400)
committerJoey Castillo <joeycastillo@utexas.edu>
Sun, 10 Apr 2022 22:13:22 +0000 (18:13 -0400)
movement/make/Makefile
movement/movement_faces.h
movement/watch_faces/demo/lis2dh_logging_face.c [deleted file]
movement/watch_faces/demo/lis2dh_logging_face.h [deleted file]
movement/watch_faces/demo/lis2dw_logging_face.c [new file with mode: 0644]
movement/watch_faces/demo/lis2dw_logging_face.h [new file with mode: 0644]
watch-library/shared/driver/lis2dw.c
watch-library/shared/driver/lis2dw.h

index a773fee4809c93d9de0d4809085b1a8e7925dfac..03b18190259ee0fca32e042b3fa0777ba02be1e7 100755 (executable)
@@ -44,6 +44,7 @@ SRCS += \
   ../watch_faces/sensor/thermistor_testing_face.c \
   ../watch_faces/demo/character_set_face.c \
   ../watch_faces/demo/voltage_face.c \
+  ../watch_faces/demo/lis2dw_logging_face.c \
   ../watch_faces/demo/demo_face.c \
   ../watch_faces/demo/hello_there_face.c \
   ../watch_faces/complication/pulsometer_face.c \
index d90bf02ec8eb0c617af89c1b3169a60401412315..d2c57ed15ab8f21210c2404d8ca8ff8c8743a9a3 100644 (file)
@@ -39,6 +39,7 @@
 #include "voltage_face.h"
 #include "stopwatch_face.h"
 #include "totp_face.h"
+#include "lis2dw_logging_face.h"
 #include "demo_face.h"
 #include "hello_there_face.h"
 #include "sunrise_sunset_face.h"
diff --git a/movement/watch_faces/demo/lis2dh_logging_face.c b/movement/watch_faces/demo/lis2dh_logging_face.c
deleted file mode 100644 (file)
index 31d1cad..0000000
+++ /dev/null
@@ -1,215 +0,0 @@
-/*
- * 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 <stdlib.h>
-#include <string.h>
-#include "lis2dh_logging_face.h"
-#include "lis2dh.h"
-#include "watch.h"
-
-// This watch face is just for testing; if we want to build accelerometer support, it will likely have to be part of Movement itself.
-// The watch face only logs events when it is on screen and not in low energy mode, so you should set LE mode to Never when using it
-// and make it the first watch face in the list (so we come back to it from other modes).
-// On an interrupt, it flashes the Signal icon, and displays the axis or axes that were over the threshold.
-// The main display contains, from left to right, the number of interrupt events that were detected in each of the last three minutes.
-// Pressing the alarm button enters the log mode, where the main display shows the number of interrupts detected in each of the last
-// 24 hours (the hour is shown in the top right digit and AM/PM indicator, if the clock is set to 12 hour mode)
-
-static void _lis2dh_logging_face_update_display(movement_settings_t *settings, lis2dh_logger_state_t *logger_state, lis2dh_interrupt_state interrupt_state) {
-    char buf[14];
-    char time_indication_character;
-    int8_t pos;
-    watch_date_time date_time;
-
-    if (logger_state->log_ticks) {
-        pos = (logger_state->data_points - 1 - logger_state->display_index) % LIS2DH_LOGGING_NUM_DATA_POINTS;
-        if (pos < 0) {
-            watch_clear_colon();
-            sprintf(buf, "NO   data ");
-        } else {
-            date_time = logger_state->data[pos].timestamp;
-            watch_set_colon();
-            if (settings->bit.clock_mode_24h) {
-                watch_set_indicator(WATCH_INDICATOR_24H);
-            } else {
-                if (date_time.unit.hour > 11) watch_set_indicator(WATCH_INDICATOR_PM);
-                date_time.unit.hour %= 12;
-                if (date_time.unit.hour == 0) date_time.unit.hour = 12;
-            }
-            switch (logger_state->axis_index) {
-                case 0:
-                    sprintf(buf, "3A%2d%02d%4lu", date_time.unit.hour, date_time.unit.minute, logger_state->data[pos].x_interrupts + logger_state->data[pos].y_interrupts + logger_state->data[pos].z_interrupts);
-                    break;
-                case 1:
-                    sprintf(buf, "XA%2d%02d%4lu", date_time.unit.hour, date_time.unit.minute, logger_state->data[pos].x_interrupts);
-                    break;
-                case 2:
-                    sprintf(buf, "YA%2d%02d%4lu", date_time.unit.hour, date_time.unit.minute, logger_state->data[pos].y_interrupts);
-                    break;
-                case 3:
-                    sprintf(buf, "ZA%2d%02d%4lu", date_time.unit.hour, date_time.unit.minute, logger_state->data[pos].z_interrupts);
-                    break;
-            }
-        }
-    } else {
-        date_time = watch_rtc_get_date_time();
-        watch_clear_colon();
-        watch_clear_indicator(WATCH_INDICATOR_PM);
-        watch_clear_indicator(WATCH_INDICATOR_24H);
-        if ((59 - date_time.unit.second) < 10) time_indication_character = '0' + (59 - date_time.unit.second);
-        else time_indication_character = (date_time.unit.second % 2) ? 'i' : '_';
-        sprintf(buf, "%c%c%c%c%2d%2d%2d",
-            (interrupt_state & LIS2DH_INTERRUPT_STATE_Y_HIGH) ? 'Y' : ' ',
-            (interrupt_state & LIS2DH_INTERRUPT_STATE_X_HIGH) ? 'X' : ' ',
-            (interrupt_state & LIS2DH_INTERRUPT_STATE_Z_HIGH) ? 'Z' : ' ',
-            time_indication_character,
-            logger_state->interrupts[0],
-            logger_state->interrupts[1],
-            logger_state->interrupts[2]);
-    }
-    watch_display_string(buf, 0);
-}
-
-static void _lis2dh_logging_face_log_data(lis2dh_logger_state_t *logger_state) {
-    watch_date_time date_time = watch_rtc_get_date_time();
-    // we get this call 15 minutes late; i.e. at 6:15 we're logging events for 6:00.
-    // so: if we're at the top of the hour, roll the hour back too (7:00 task logs data for 6:45)
-    if (date_time.unit.minute == 0) date_time.unit.hour = (date_time.unit.hour + 23) % 24;
-
-    // // then roll the minute back.
-    date_time.unit.minute = (date_time.unit.minute + 45) % 60;
-
-    size_t pos = logger_state->data_points % LIS2DH_LOGGING_NUM_DATA_POINTS;
-    logger_state->data[pos].timestamp.reg = date_time.reg;
-    logger_state->data[pos].x_interrupts = logger_state->x_interrupts_this_hour;
-    logger_state->data[pos].y_interrupts = logger_state->y_interrupts_this_hour;
-    logger_state->data[pos].z_interrupts = logger_state->z_interrupts_this_hour;
-    logger_state->data_points++;
-    logger_state->x_interrupts_this_hour = 0;
-    logger_state->y_interrupts_this_hour = 0;
-    logger_state->z_interrupts_this_hour = 0;
-}
-
-void lis2dh_logging_face_setup(movement_settings_t *settings, uint8_t watch_face_index, void ** context_ptr) {
-    (void) settings;
-    (void) watch_face_index;
-    if (*context_ptr == NULL) {
-        *context_ptr = malloc(sizeof(lis2dh_logger_state_t));
-        memset(*context_ptr, 0, sizeof(lis2dh_logger_state_t));
-        gpio_set_pin_direction(A0, GPIO_DIRECTION_OUT);
-        gpio_set_pin_function(A0, GPIO_PIN_FUNCTION_OFF);
-        gpio_set_pin_level(A0, true);
-        watch_enable_i2c();
-        lis2dh_begin();
-        lis2dh_set_data_rate(LIS2DH_DATA_RATE_10_HZ);
-        lis2dh_configure_aoi_int1(
-            LIS2DH_INTERRUPT_CONFIGURATION_OR |
-            LIS2DH_INTERRUPT_CONFIGURATION_X_HIGH_ENABLE |
-            LIS2DH_INTERRUPT_CONFIGURATION_Y_HIGH_ENABLE |
-            LIS2DH_INTERRUPT_CONFIGURATION_Z_HIGH_ENABLE, 96, 0, true);
-    }
-}
-
-void lis2dh_logging_face_activate(movement_settings_t *settings, void *context) {
-    lis2dh_logger_state_t *logger_state = (lis2dh_logger_state_t *)context;
-    // force two settings: never enter low energy mode, and always snap back to screen 0.
-    // this assumes the accelerometer face is first in the watch_faces list.
-    settings->bit.le_interval = 0;
-    settings->bit.to_always = true;
-
-    logger_state->display_index = 0;
-    logger_state->log_ticks = 0;
-    watch_enable_digital_input(A1);
-}
-
-bool lis2dh_logging_face_loop(movement_event_t event, movement_settings_t *settings, void *context) {
-    lis2dh_logger_state_t *logger_state = (lis2dh_logger_state_t *)context;
-    lis2dh_interrupt_state interrupt_state = 0;
-
-    switch (event.event_type) {
-        case EVENT_MODE_BUTTON_UP:
-            movement_move_to_next_face();
-            break;
-        case EVENT_LIGHT_LONG_PRESS:
-            movement_illuminate_led();
-            break;
-        case EVENT_LIGHT_BUTTON_DOWN:
-            logger_state->axis_index = (logger_state->axis_index + 1) % 4;
-            logger_state->log_ticks = 255;
-            _lis2dh_logging_face_update_display(settings, logger_state, interrupt_state);
-            break;
-        case EVENT_ALARM_BUTTON_UP:
-            if (logger_state->log_ticks) logger_state->display_index = (logger_state->display_index + 1) % LIS2DH_LOGGING_NUM_DATA_POINTS;
-            logger_state->log_ticks = 255;
-            logger_state->axis_index = 0;
-            _lis2dh_logging_face_update_display(settings, logger_state, interrupt_state);
-            break;
-        case EVENT_ACTIVATE:
-        case EVENT_TICK:
-            if (logger_state->log_ticks > 0) {
-                logger_state->log_ticks--;
-            } else {
-                logger_state->display_index = 0;
-            }
-            if (watch_get_pin_level(A1)) {
-                watch_set_indicator(WATCH_INDICATOR_SIGNAL);
-                interrupt_state = lis2dh_get_int1_state();
-                logger_state->interrupts[0]++;
-                if (interrupt_state & LIS2DH_INTERRUPT_STATE_X_HIGH) logger_state->x_interrupts_this_hour++;
-                if (interrupt_state & LIS2DH_INTERRUPT_STATE_Y_HIGH) logger_state->y_interrupts_this_hour++;
-                if (interrupt_state & LIS2DH_INTERRUPT_STATE_Z_HIGH) logger_state->z_interrupts_this_hour++;
-            } else {
-                watch_clear_indicator(WATCH_INDICATOR_SIGNAL);
-            }
-            _lis2dh_logging_face_update_display(settings, logger_state, interrupt_state);
-            break;
-        case EVENT_BACKGROUND_TASK:
-            _lis2dh_logging_face_log_data(logger_state);
-            break;
-        default:
-            break;
-    }
-
-    return true;
-}
-
-void lis2dh_logging_face_resign(movement_settings_t *settings, void *context) {
-    (void) settings;
-    (void) context;
-    watch_disable_digital_input(A1);
-}
-
-bool lis2dh_logging_face_wants_background_task(movement_settings_t *settings, void *context) {
-    (void) settings;
-    lis2dh_logger_state_t *logger_state = (lis2dh_logger_state_t *)context;
-    watch_date_time date_time = watch_rtc_get_date_time();
-
-    // this is kind of an abuse of the API, but, let's use the 1 minute tick to shift all our data over.
-    logger_state->interrupts[2] = logger_state->interrupts[1];
-    logger_state->interrupts[1] = logger_state->interrupts[0];
-    logger_state->interrupts[0] = 0;
-
-    // and do our logging task every 15 minutes
-    return (date_time.unit.minute % 15) == 0;
-}
diff --git a/movement/watch_faces/demo/lis2dh_logging_face.h b/movement/watch_faces/demo/lis2dh_logging_face.h
deleted file mode 100644 (file)
index 4936654..0000000
+++ /dev/null
@@ -1,66 +0,0 @@
-/*
- * 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.
- */
-
-#ifndef LIS2DH_LOGGING_FACE_H_
-#define LIS2DH_LOGGING_FACE_H_
-
-#include "movement.h"
-#include "watch.h"
-
-#define LIS2DH_LOGGING_NUM_DATA_POINTS (96)
-
-typedef struct {
-    watch_date_time timestamp;
-    uint32_t x_interrupts;
-    uint32_t y_interrupts;
-    uint32_t z_interrupts;
-} lis2dh_logger_data_point_t;
-
-typedef struct {
-    uint8_t display_index;  // the index we are displaying on screen
-    uint8_t axis_index;     // the index we are displaying on screen
-    uint8_t log_ticks;      // when the user taps the ALARM button, we enter log mode
-    int32_t data_points;    // the absolute number of data points logged
-    uint8_t interrupts[3];  // the number of interrupts we have logged in each of the last 3 minutes
-    uint32_t x_interrupts_this_hour;  // the number of interrupts we have logged in the last hour
-    uint32_t y_interrupts_this_hour;  // the number of interrupts we have logged in the last hour
-    uint32_t z_interrupts_this_hour;  // the number of interrupts we have logged in the last hour
-    lis2dh_logger_data_point_t data[LIS2DH_LOGGING_NUM_DATA_POINTS];
-} lis2dh_logger_state_t;
-
-void lis2dh_logging_face_setup(movement_settings_t *settings, uint8_t watch_face_index, void ** context_ptr);
-void lis2dh_logging_face_activate(movement_settings_t *settings, void *context);
-bool lis2dh_logging_face_loop(movement_event_t event, movement_settings_t *settings, void *context);
-void lis2dh_logging_face_resign(movement_settings_t *settings, void *context);
-bool lis2dh_logging_face_wants_background_task(movement_settings_t *settings, void *context);
-
-#define lis2dh_logging_face ((const watch_face_t){ \
-    lis2dh_logging_face_setup, \
-    lis2dh_logging_face_activate, \
-    lis2dh_logging_face_loop, \
-    lis2dh_logging_face_resign, \
-    lis2dh_logging_face_wants_background_task, \
-})
-
-#endif // LIS2DH_LOGGING_FACE_H_
diff --git a/movement/watch_faces/demo/lis2dw_logging_face.c b/movement/watch_faces/demo/lis2dw_logging_face.c
new file mode 100644 (file)
index 0000000..0e63e41
--- /dev/null
@@ -0,0 +1,214 @@
+/*
+ * 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 <stdlib.h>
+#include <string.h>
+#include "lis2dw_logging_face.h"
+#include "lis2dw.h"
+#include "watch.h"
+
+// This watch face is just for testing; if we want to build accelerometer support, it will likely have to be part of Movement itself.
+// The watch face only logs events when it is on screen and not in low energy mode, so you should set LE mode to Never when using it
+// and make it the first watch face in the list (so we come back to it from other modes).
+// On an interrupt, it flashes the Signal icon, and displays the axis or axes that were over the threshold.
+// The main display contains, from left to right, the number of interrupt events that were detected in each of the last three minutes.
+// Pressing the alarm button enters the log mode, where the main display shows the number of interrupts detected in each of the last
+// 24 hours (the hour is shown in the top right digit and AM/PM indicator, if the clock is set to 12 hour mode)
+
+static void _lis2dw_logging_face_update_display(movement_settings_t *settings, lis2dw_logger_state_t *logger_state, lis2dw_wakeup_source wakeup_source) {
+    char buf[14];
+    char time_indication_character;
+    int8_t pos;
+    watch_date_time date_time;
+
+    if (logger_state->log_ticks) {
+        pos = (logger_state->data_points - 1 - logger_state->display_index) % LIS2DW_LOGGING_NUM_DATA_POINTS;
+        if (pos < 0) {
+            watch_clear_colon();
+            sprintf(buf, "NO   data ");
+        } else {
+            date_time = logger_state->data[pos].timestamp;
+            watch_set_colon();
+            if (settings->bit.clock_mode_24h) {
+                watch_set_indicator(WATCH_INDICATOR_24H);
+            } else {
+                if (date_time.unit.hour > 11) watch_set_indicator(WATCH_INDICATOR_PM);
+                date_time.unit.hour %= 12;
+                if (date_time.unit.hour == 0) date_time.unit.hour = 12;
+            }
+            switch (logger_state->axis_index) {
+                case 0:
+                    sprintf(buf, "3A%2d%02d%4lu", date_time.unit.hour, date_time.unit.minute, logger_state->data[pos].x_interrupts + logger_state->data[pos].y_interrupts + logger_state->data[pos].z_interrupts);
+                    break;
+                case 1:
+                    sprintf(buf, "XA%2d%02d%4lu", date_time.unit.hour, date_time.unit.minute, logger_state->data[pos].x_interrupts);
+                    break;
+                case 2:
+                    sprintf(buf, "YA%2d%02d%4lu", date_time.unit.hour, date_time.unit.minute, logger_state->data[pos].y_interrupts);
+                    break;
+                case 3:
+                    sprintf(buf, "ZA%2d%02d%4lu", date_time.unit.hour, date_time.unit.minute, logger_state->data[pos].z_interrupts);
+                    break;
+            }
+        }
+    } else {
+        date_time = watch_rtc_get_date_time();
+        watch_clear_colon();
+        watch_clear_indicator(WATCH_INDICATOR_PM);
+        watch_clear_indicator(WATCH_INDICATOR_24H);
+        if ((59 - date_time.unit.second) < 10) time_indication_character = '0' + (59 - date_time.unit.second);
+        else time_indication_character = (date_time.unit.second % 2) ? 'i' : '_';
+        sprintf(buf, "%c%c%c%c%2d%2d%2d",
+            (wakeup_source & LIS2DW_WAKEUP_SRC_WAKEUP_Y) ? 'Y' : ' ',
+            (wakeup_source & LIS2DW_WAKEUP_SRC_WAKEUP_X) ? 'X' : ' ',
+            (wakeup_source & LIS2DW_WAKEUP_SRC_WAKEUP_Z) ? 'Z' : ' ',
+            time_indication_character,
+            logger_state->interrupts[0],
+            logger_state->interrupts[1],
+            logger_state->interrupts[2]);
+    }
+    watch_display_string(buf, 0);
+}
+
+static void _lis2dw_logging_face_log_data(lis2dw_logger_state_t *logger_state) {
+    watch_date_time date_time = watch_rtc_get_date_time();
+    // we get this call 15 minutes late; i.e. at 6:15 we're logging events for 6:00.
+    // so: if we're at the top of the hour, roll the hour back too (7:00 task logs data for 6:45)
+    if (date_time.unit.minute == 0) date_time.unit.hour = (date_time.unit.hour + 23) % 24;
+
+    // // then roll the minute back.
+    date_time.unit.minute = (date_time.unit.minute + 45) % 60;
+
+    size_t pos = logger_state->data_points % LIS2DW_LOGGING_NUM_DATA_POINTS;
+    logger_state->data[pos].timestamp.reg = date_time.reg;
+    logger_state->data[pos].x_interrupts = logger_state->x_interrupts_this_hour;
+    logger_state->data[pos].y_interrupts = logger_state->y_interrupts_this_hour;
+    logger_state->data[pos].z_interrupts = logger_state->z_interrupts_this_hour;
+    logger_state->data_points++;
+    logger_state->x_interrupts_this_hour = 0;
+    logger_state->y_interrupts_this_hour = 0;
+    logger_state->z_interrupts_this_hour = 0;
+}
+
+void lis2dw_logging_face_setup(movement_settings_t *settings, uint8_t watch_face_index, void ** context_ptr) {
+    (void) settings;
+    (void) watch_face_index;
+    if (*context_ptr == NULL) {
+        *context_ptr = malloc(sizeof(lis2dw_logger_state_t));
+        memset(*context_ptr, 0, sizeof(lis2dw_logger_state_t));
+        watch_enable_i2c();
+        lis2dw_begin();
+        lis2dw_set_low_power_mode(LIS2DW_LP_MODE_2); // lowest power 14-bit mode, 25 Hz is 3.5 µA @ 1.8V w/ low noise, 3µA without
+        lis2dw_set_low_noise_mode(true); // consumes a little more power
+        lis2dw_set_range(LIS2DW_CTRL6_VAL_RANGE_4G);
+        lis2dw_set_data_rate(LIS2DW_DATA_RATE_25_HZ); // is this enough for training?
+        // threshold is 1/64th of full scale, so for a FS of ±4G this is 1.25G
+        lis2dw_configure_wakeup_int1(10, true, false);
+    }
+}
+
+void lis2dw_logging_face_activate(movement_settings_t *settings, void *context) {
+    lis2dw_logger_state_t *logger_state = (lis2dw_logger_state_t *)context;
+    // force two settings: never enter low energy mode, and always snap back to screen 0.
+    // this assumes the accelerometer face is first in the watch_faces list.
+    settings->bit.le_interval = 0;
+    settings->bit.to_always = true;
+
+    logger_state->display_index = 0;
+    logger_state->log_ticks = 0;
+    watch_enable_digital_input(A0);
+}
+
+bool lis2dw_logging_face_loop(movement_event_t event, movement_settings_t *settings, void *context) {
+    lis2dw_logger_state_t *logger_state = (lis2dw_logger_state_t *)context;
+    lis2dw_wakeup_source wakeup_source = 0;
+    lis2dw_interrupt_source interrupt_source = 0;
+
+    switch (event.event_type) {
+        case EVENT_MODE_BUTTON_UP:
+            movement_move_to_next_face();
+            break;
+        case EVENT_LIGHT_LONG_PRESS:
+            movement_illuminate_led();
+            break;
+        case EVENT_LIGHT_BUTTON_DOWN:
+            logger_state->axis_index = (logger_state->axis_index + 1) % 4;
+            logger_state->log_ticks = 255;
+            _lis2dw_logging_face_update_display(settings, logger_state, wakeup_source);
+            break;
+        case EVENT_ALARM_BUTTON_UP:
+            if (logger_state->log_ticks) logger_state->display_index = (logger_state->display_index + 1) % LIS2DW_LOGGING_NUM_DATA_POINTS;
+            logger_state->log_ticks = 255;
+            logger_state->axis_index = 0;
+            _lis2dw_logging_face_update_display(settings, logger_state, wakeup_source);
+            break;
+        case EVENT_ACTIVATE:
+        case EVENT_TICK:
+            if (logger_state->log_ticks > 0) {
+                logger_state->log_ticks--;
+            } else {
+                logger_state->display_index = 0;
+            }
+            interrupt_source = lis2dw_get_interrupt_source();
+            if (interrupt_source) {
+                watch_set_indicator(WATCH_INDICATOR_SIGNAL);
+                wakeup_source = lis2dw_get_wakeup_source();
+                logger_state->interrupts[0]++;
+                if (wakeup_source & LIS2DW_WAKEUP_SRC_WAKEUP_X) logger_state->x_interrupts_this_hour++;
+                if (wakeup_source & LIS2DW_WAKEUP_SRC_WAKEUP_Y) logger_state->y_interrupts_this_hour++;
+                if (wakeup_source & LIS2DW_WAKEUP_SRC_WAKEUP_Z) logger_state->z_interrupts_this_hour++;
+            } else {
+                watch_clear_indicator(WATCH_INDICATOR_SIGNAL);
+            }
+            _lis2dw_logging_face_update_display(settings, logger_state, wakeup_source);
+            break;
+        case EVENT_BACKGROUND_TASK:
+            _lis2dw_logging_face_log_data(logger_state);
+            break;
+        default:
+            break;
+    }
+
+    return true;
+}
+
+void lis2dw_logging_face_resign(movement_settings_t *settings, void *context) {
+    (void) settings;
+    (void) context;
+    watch_disable_digital_input(A0);
+}
+
+bool lis2dw_logging_face_wants_background_task(movement_settings_t *settings, void *context) {
+    (void) settings;
+    lis2dw_logger_state_t *logger_state = (lis2dw_logger_state_t *)context;
+    watch_date_time date_time = watch_rtc_get_date_time();
+
+    // this is kind of an abuse of the API, but, let's use the 1 minute tick to shift all our data over.
+    logger_state->interrupts[2] = logger_state->interrupts[1];
+    logger_state->interrupts[1] = logger_state->interrupts[0];
+    logger_state->interrupts[0] = 0;
+
+    // and do our logging task every 15 minutes
+    return (date_time.unit.minute % 15) == 0;
+}
diff --git a/movement/watch_faces/demo/lis2dw_logging_face.h b/movement/watch_faces/demo/lis2dw_logging_face.h
new file mode 100644 (file)
index 0000000..f6ea85e
--- /dev/null
@@ -0,0 +1,66 @@
+/*
+ * 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.
+ */
+
+#ifndef LIS2DW_LOGGING_FACE_H_
+#define LIS2DW_LOGGING_FACE_H_
+
+#include "movement.h"
+#include "watch.h"
+
+#define LIS2DW_LOGGING_NUM_DATA_POINTS (96)
+
+typedef struct {
+    watch_date_time timestamp;
+    uint32_t x_interrupts;
+    uint32_t y_interrupts;
+    uint32_t z_interrupts;
+} lis2dw_logger_data_point_t;
+
+typedef struct {
+    uint8_t display_index;  // the index we are displaying on screen
+    uint8_t axis_index;     // the index we are displaying on screen
+    uint8_t log_ticks;      // when the user taps the ALARM button, we enter log mode
+    int32_t data_points;    // the absolute number of data points logged
+    uint8_t interrupts[3];  // the number of interrupts we have logged in each of the last 3 minutes
+    uint32_t x_interrupts_this_hour;  // the number of interrupts we have logged in the last hour
+    uint32_t y_interrupts_this_hour;  // the number of interrupts we have logged in the last hour
+    uint32_t z_interrupts_this_hour;  // the number of interrupts we have logged in the last hour
+    lis2dw_logger_data_point_t data[LIS2DW_LOGGING_NUM_DATA_POINTS];
+} lis2dw_logger_state_t;
+
+void lis2dw_logging_face_setup(movement_settings_t *settings, uint8_t watch_face_index, void ** context_ptr);
+void lis2dw_logging_face_activate(movement_settings_t *settings, void *context);
+bool lis2dw_logging_face_loop(movement_event_t event, movement_settings_t *settings, void *context);
+void lis2dw_logging_face_resign(movement_settings_t *settings, void *context);
+bool lis2dw_logging_face_wants_background_task(movement_settings_t *settings, void *context);
+
+#define lis2dw_logging_face ((const watch_face_t){ \
+    lis2dw_logging_face_setup, \
+    lis2dw_logging_face_activate, \
+    lis2dw_logging_face_loop, \
+    lis2dw_logging_face_resign, \
+    lis2dw_logging_face_wants_background_task, \
+})
+
+#endif // LIS2DW_LOGGING_FACE_H_
index fce266b3380902a6a1efa8d056a45d38b1e68e5b..4d60fcff24f2f22f890b67e5def03d5cf52ea367 100644 (file)
@@ -164,3 +164,32 @@ void lis2dw_clear_fifo(void) {
     watch_i2c_write8(LIS2DW_ADDRESS, LIS2DW_REG_FIFO_CTRL, LIS2DW_FIFO_CTRL_MODE_OFF);
     watch_i2c_write8(LIS2DW_ADDRESS, LIS2DW_REG_FIFO_CTRL, LIS2DW_FIFO_CTRL_MODE_COLLECT_AND_STOP | LIS2DW_FIFO_CTRL_FTH);
 }
+
+void lis2dw_configure_wakeup_int1(uint8_t threshold, bool latch, bool active_state) {
+    uint8_t configuration;
+
+    // enable wakeup interrupt on INT1 pin
+    configuration = watch_i2c_read8(LIS2DW_ADDRESS, LIS2DW_REG_CTRL4_INT1);
+    watch_i2c_write8(LIS2DW_ADDRESS, LIS2DW_REG_CTRL4_INT1, configuration | LIS2DW_CTRL4_INT1_WU);
+
+    // set threshold
+    watch_i2c_write8(LIS2DW_ADDRESS, LIS2DW_REG_WAKE_UP_THS, threshold | LIS2DW_WAKE_UP_THS_VAL_SLEEP_ON);
+    watch_i2c_write8(LIS2DW_ADDRESS, LIS2DW_REG_INT1_DUR, 0b01111111);
+
+    configuration = watch_i2c_read8(LIS2DW_ADDRESS, LIS2DW_REG_CTRL3) & ~(LIS2DW_CTRL3_VAL_LIR);
+    if (!active_state) configuration |= LIS2DW_CTRL3_VAL_H_L_ACTIVE;
+    if (latch) configuration |= LIS2DW_CTRL3_VAL_LIR;
+    watch_i2c_write8(LIS2DW_ADDRESS, LIS2DW_REG_CTRL3, configuration);
+
+    // enable interrupts
+    configuration = watch_i2c_read8(LIS2DW_ADDRESS, LIS2DW_REG_CTRL7);
+    watch_i2c_write8(LIS2DW_ADDRESS, LIS2DW_REG_CTRL7, configuration | LIS2DW_CTRL7_VAL_INTERRUPTS_ENABLE);
+}
+
+lis2dw_wakeup_source lis2dw_get_wakeup_source() {
+    return (lis2dw_wakeup_source) watch_i2c_read8(LIS2DW_ADDRESS, LIS2DW_REG_WAKE_UP_SRC);
+}
+
+lis2dw_interrupt_source lis2dw_get_interrupt_source(void) {
+    return (lis2dw_interrupt_source) watch_i2c_read8(LIS2DW_ADDRESS, LIS2DW_REG_ALL_INT_SRC);
+}
index 41611172cc09c37949e5a3300af7889084f75c02..f9373e04481a433b65302dc3de7ad7dc2ef2483d 100644 (file)
@@ -94,6 +94,24 @@ typedef enum {
   LIS2DW_RANGE_2_G = 0b00   // +/- 2g (default value)
 } lis2dw_range_t;
 
+typedef enum {
+  LIS2DW_INTERRUPT_SRC_SLEEP_CHANGE = 0b00100000,
+  LIS2DW_INTERRUPT_SRC_6D           = 0b00010000,
+  LIS2DW_INTERRUPT_SRC_DOUBLE_TAP   = 0b00001000,
+  LIS2DW_INTERRUPT_SRC_SINGLE_TAP   = 0b00000100,
+  LIS2DW_INTERRUPT_SRC_WU           = 0b00000010,
+  LIS2DW_INTERRUPT_SRC_FF           = 0b00000001
+} lis2dw_interrupt_source;
+
+typedef enum {
+  LIS2DW_WAKEUP_SRC_FREEFALL    = 0b00100000,
+  LIS2DW_WAKEUP_SRC_SLEEP_STATE = 0b00010000,
+  LIS2DW_WAKEUP_SRC_WAKEUP      = 0b00001000,
+  LIS2DW_WAKEUP_SRC_WAKEUP_X    = 0b00000100,
+  LIS2DW_WAKEUP_SRC_WAKEUP_Y    = 0b00000010,
+  LIS2DW_WAKEUP_SRC_WAKEUP_Z    = 0b00000001
+} lis2dw_wakeup_source;
+
 // Assumes SA0 is high; if low, its 0x18
 #define LIS2DW_ADDRESS (0x19)
 
@@ -130,15 +148,15 @@ typedef enum {
 #define LIS2DW_CTRL2_VAL_IF_ADD_INC 0b00000100
 
 #define LIS2DW_REG_CTRL3 0x22
-#define LIS2DW_CTRL4_VAL_SELF_TEST_POS  0b10000000
-#define LIS2DW_CTRL4_VAL_SELF_TEST_NEG  0b01000000
+#define LIS2DW_CTRL3_VAL_SELF_TEST_POS  0b10000000
+#define LIS2DW_CTRL3_VAL_SELF_TEST_NEG  0b01000000
 #define LIS2DW_CTRL3_VAL_PP_OD          0b00100000
 #define LIS2DW_CTRL3_VAL_LIR            0b00010000
 #define LIS2DW_CTRL3_VAL_H_L_ACTIVE     0b00001000
 #define LIS2DW_CTRL3_VAL_SLP_MODE_SEL   0b00000010
 #define LIS2DW_CTRL3_VAL_SLP_MODE_1     0b00000001
 
-#define LIS2DW_REG_CTRL4 0x23
+#define LIS2DW_REG_CTRL4_INT1 0x23
 #define LIS2DW_CTRL4_INT1_6D          0b10000000
 #define LIS2DW_CTRL4_INT1_SINGLE_TAP  0b01000000
 #define LIS2DW_CTRL4_INT1_WU          0b00100000
@@ -148,7 +166,7 @@ typedef enum {
 #define LIS2DW_CTRL4_INT1_FTH         0b00000010
 #define LIS2DW_CTRL4_INT1_DRDY        0b00000001
 
-#define LIS2DW_REG_CTRL5 0x24
+#define LIS2DW_REG_CTRL5_INT2 0x24
 #define LIS2DW_CTRL5_INT2_SLEEP_STATE 0b10000000
 #define LIS2DW_CTRL5_INT2_SLEEP_CHG   0b01000000
 #define LIS2DW_CTRL5_INT2_BOOT        0b00100000
@@ -206,7 +224,11 @@ typedef enum {
 #define LIS2DW_REG_TAP_THS_Y 0x31
 #define LIS2DW_REG_TAP_THS_Z 0x32
 #define LIS2DW_REG_INT1_DUR 0x33
+
 #define LIS2DW_REG_WAKE_UP_THS 0x34
+#define LIS2DW_WAKE_UP_THS_VAL_TAP_EVENT_ENABLED 0b10000000
+#define LIS2DW_WAKE_UP_THS_VAL_SLEEP_ON          0b01000000
+
 #define LIS2DW_REG_WAKE_UP_DUR 0x35
 #define LIS2DW_REG_FREE_FALL 0x36
 #define LIS2DW_REG_STATUS_DUP 0x37
@@ -293,4 +315,10 @@ bool lis2dw_read_fifo(lis2dw_fifo_t *fifo_data);
 
 void lis2dw_clear_fifo(void);
 
+void lis2dw_configure_wakeup_int1(uint8_t threshold, bool latch, bool active_state);
+
+lis2dw_interrupt_source lis2dw_get_interrupt_source(void);
+
+lis2dw_wakeup_source lis2dw_get_wakeup_source(void);
+
 #endif // LIS2DW_H