]> git.earman.xyz Git - sensor-watch.git/commitdiff
activity tracking proof of concept
authorjoeycastillo <joeycastillo@utexas.edu>
Mon, 21 Oct 2024 01:13:34 +0000 (21:13 -0400)
committerjoeycastillo <joeycastillo@utexas.edu>
Mon, 21 Oct 2024 01:13:34 +0000 (21:13 -0400)
movement.c
watch-faces/demo/accel_interrupt_count_face.c
watch-faces/sensor/activity_logging_face.c
watch-faces/sensor/activity_logging_face.h

index f0d08c2f63b59ce8ca938f1550fb2dbb7dfa1c13..80693673e8a52e73050f1c4681b561ad5208c399 100644 (file)
@@ -79,6 +79,7 @@ void cb_tick(void);
 void cb_motion_interrupt_1(void);
 void cb_motion_interrupt_2(void);
 uint32_t orientation_changes = 0;
+uint8_t active_minutes = 0;
 #endif
 
 #if __EMSCRIPTEN__
@@ -157,6 +158,11 @@ static inline void _movement_disable_fast_tick_if_possible(void) {
 static void _movement_handle_top_of_minute(void) {
     watch_date_time_t date_time = watch_rtc_get_date_time();
 
+#ifdef HAS_ACCELEROMETER
+    // every minute, we want to log whether the accelerometer is asleep or awake.
+    if (!HAL_GPIO_A3_read()) active_minutes++;
+#endif
+
     // update the DST offset cache every 30 minutes, since someplace in the world could change.
     if (date_time.unit.minute % 30 == 0) {
         _movement_update_dst_offset_cache();
@@ -511,8 +517,8 @@ void app_init(void) {
     if (date_time.reg == 0) {
         // at first boot, set year to 2024
         date_time.unit.year = 2024 - WATCH_RTC_REFERENCE_YEAR;
-        date_time.unit.month = 10;
-        date_time.unit.day = 16;
+        date_time.unit.month = 1;
+        date_time.unit.day = 1;
         watch_rtc_set_date_time(date_time);
     }
 
@@ -628,12 +634,12 @@ void app_setup(void) {
 #endif
         watch_enable_i2c();
         if (lis2dw_begin()) {
-            lis2dw_set_mode(LIS2DW_MODE_LOW_POWER);         // select low power (not high performance)
-            lis2dw_set_low_power_mode(LIS2DW_LP_MODE_1);    // lowest power mode, 12-bit, up this if needed
-            lis2dw_set_low_noise_mode(true);                // only marginally raises power consumption
-            lis2dw_enable_sleep();                          // enable sleep mode
+            lis2dw_set_mode(LIS2DW_MODE_LOW_POWER);         // select low power (not high performance) mode
+            lis2dw_set_low_power_mode(LIS2DW_LP_MODE_1);    // lowest power mode, 12-bit
+            lis2dw_set_low_noise_mode(false);               // low noise mode raises power consumption slightly; we don't need it
+            lis2dw_set_data_rate(LIS2DW_DATA_RATE_LOWEST);  // sample at 1.6 Hz, lowest rate available
+            lis2dw_enable_stationary_motion_detection();    // stationary/motion detection mode keeps the data rate at 1.6 Hz even in sleep
             lis2dw_set_range(LIS2DW_RANGE_2_G);             // Application note AN5038 recommends 2g range
-            lis2dw_set_data_rate(LIS2DW_DATA_RATE_LOWEST);  // 1.6Hz in low power mode
             lis2dw_enable_sleep();                          // allow acceleromter to sleep and wake on activity
             lis2dw_configure_wakeup_threshold(24);          // g threshold to wake up: (2 * FS / 64) where FS is "full scale" of ±2g.
             lis2dw_configure_6d_threshold(3);               // 0-3 is 80, 70, 60, or 50 degrees. 50 is least precise, hopefully most sensitive?
@@ -936,6 +942,7 @@ void cb_motion_interrupt_1(void) {
     if (int_src & LIS2DW_REG_ALL_INT_SRC_6D_IA) {
         event.event_type = EVENT_ORIENTATION_CHANGE;
         orientation_changes++;
+        printf("Orientation change\n");
     }
     if (int_src & LIS2DW_REG_ALL_INT_SRC_DOUBLE_TAP) event.event_type = EVENT_DOUBLE_TAP;
     if (int_src & LIS2DW_REG_ALL_INT_SRC_SINGLE_TAP) event.event_type = EVENT_SINGLE_TAP;
index 3417282c695947c318767ab699ad8391a1771031..fae99b9fc67e4be5cdefefef4413893956d3d3f6 100644 (file)
 
 #ifdef HAS_ACCELEROMETER
 
-// hacky: we're just tapping into Movement's orientation changes.
+// hacky: we're just tapping into Movement's global state.
 // we should make better API for this.
 extern uint32_t orientation_changes;
+extern uint8_t active_minutes;
 
 static void _accel_interrupt_count_face_update_display(accel_interrupt_count_state_t *state) {
     (void) state;
-    char buf[7];
+    char buf[8];
 
-    // "AC"celerometer "IN"terrupts
+    // Accelerometer title
     watch_display_text(WATCH_POSITION_TOP_LEFT, "AC");
-    watch_display_text(WATCH_POSITION_TOP_RIGHT, "1N");
-    snprintf(buf, 7, "%6lu", orientation_changes);
+
+    // Sleep/active state
+    if (HAL_GPIO_A3_read()) watch_display_text(WATCH_POSITION_TOP_RIGHT, " S");
+    else watch_display_text(WATCH_POSITION_TOP_RIGHT, " A");
+
+    // Orientation changes / active minutes
+    sprintf(buf, "%-3lu/%2d", orientation_changes > 999 ? 999 : orientation_changes, active_minutes);
     watch_display_text(WATCH_POSITION_BOTTOM, buf);
-    printf("%s\n", buf);
 }
 
 void accel_interrupt_count_face_setup(uint8_t watch_face_index, void ** context_ptr) {
@@ -63,6 +68,9 @@ void accel_interrupt_count_face_activate(void *context) {
 
     // never in settings mode at the start
     state->is_setting = false;
+
+    // update more quickly to catch changes, also to blink setting
+    movement_request_tick_frequency(4);
 }
 
 bool accel_interrupt_count_face_loop(movement_event_t event, void *context) {
@@ -76,10 +84,14 @@ bool accel_interrupt_count_face_loop(movement_event_t event, void *context) {
             case EVENT_TICK:
                 {
                     char buf[11];
-                    watch_display_text(WATCH_POSITION_TOP_RIGHT, "  ");
-                    watch_display_text_with_fallback(WATCH_POSITION_TOP, "W_THS", "TH");
-                    watch_display_float_with_best_effort(state->new_threshold * 0.03125, " G");
-                    printf("%s\n", buf);
+                    if (event.subsecond % 2) {
+                        watch_display_text(WATCH_POSITION_BOTTOM, "      ");
+                    } else {
+                        watch_display_text(WATCH_POSITION_TOP_RIGHT, "  ");
+                        watch_display_text_with_fallback(WATCH_POSITION_TOP, "W_THS", "TH");
+                        watch_display_float_with_best_effort(state->new_threshold * 0.03125, " G");
+                        printf("%s\n", buf);
+                    }
                 }
                 break;
             case EVENT_ALARM_BUTTON_UP:
index a4fcab7f11156ea915bd02ca5b8bd99e9889212a..05e8fcc75d3536313019460607975bd8edc1b4d3 100644 (file)
 
 #ifdef HAS_ACCELEROMETER
 
-// hacky: we're just tapping into Movement's orientation changes.
+// hacky: we're just tapping into Movement's global state.
 // we should make better API for this.
 extern uint32_t orientation_changes;
+extern uint8_t active_minutes;
 
 static void _activity_logging_face_log_data(activity_logging_state_t *state) {
-    watch_date_time_t date_time = watch_rtc_get_date_time();
+    watch_date_time_t date_time = movement_get_local_date_time();
     size_t pos = state->data_points % ACTIVITY_LOGGING_NUM_DATA_POINTS;
 
     state->data[pos].timestamp.reg = date_time.reg;
-    state->data[pos].active_minutes = state->active_minutes;
+    state->data[pos].active_minutes = active_minutes;
     state->data[pos].orientation_changes = orientation_changes;
-    state->active_minutes = 0;    
+    active_minutes = 0;
     orientation_changes = 0;
 
     state->data_points++;
@@ -48,7 +49,7 @@ static void _activity_logging_face_log_data(activity_logging_state_t *state) {
 
 static void _activity_logging_face_update_display(activity_logging_state_t *state, bool clock_mode_24h) {
     int8_t pos = (state->data_points - 1 - state->display_index) % ACTIVITY_LOGGING_NUM_DATA_POINTS;
-    char buf[16];
+    char buf[8];
 
     watch_clear_indicator(WATCH_INDICATOR_24H);
     watch_clear_indicator(WATCH_INDICATOR_PM);
@@ -78,10 +79,10 @@ static void _activity_logging_face_update_display(activity_logging_state_t *stat
         watch_display_text(WATCH_POSITION_BOTTOM, buf);
     } else {
         // we are displaying the number of accelerometer wakeups and orientation changes
-        watch_display_text(WATCH_POSITION_TOP, "WO");
+        watch_display_text(WATCH_POSITION_TOP, "AC");
         sprintf(buf, "%2d", state->display_index);
         watch_display_text(WATCH_POSITION_TOP_RIGHT, buf);
-        sprintf(buf, "%2d=%lu", state->data[pos].active_minutes, state->data[pos].orientation_changes);
+        sprintf(buf, "%-3lu/%2d", state->data[pos].orientation_changes > 999 ? 999 : state->data[pos].orientation_changes, state->data[pos].active_minutes);
         watch_display_text(WATCH_POSITION_BOTTOM, buf);
     }
 }
@@ -142,14 +143,9 @@ void activity_logging_face_resign(void *context) {
 }
 
 movement_watch_face_advisory_t activity_logging_face_advise(void *context) {
-    activity_logging_state_t *state = (activity_logging_state_t *)context;
+    (void) context;
     movement_watch_face_advisory_t retval = { 0 };
 
-    // every minute, we want to log whether the accelerometer is asleep or awake.
-    if (!HAL_GPIO_A3_read()) {
-        state->active_minutes++;
-    }
-
     // this will get called at the top of each minute, so all we check is if we're at the top of the hour as well.
     // if we are, we ask for a background task.
     retval.wants_background_task = watch_rtc_get_date_time().unit.minute == 0;
index 39747a3188ace5994896979d4351b6ae446f0f93..4848e637b264a20bc6c37d261e0cda7f7984c5bc 100644 (file)
@@ -50,7 +50,6 @@ typedef struct {
 typedef struct {
     uint8_t display_index;  // the index we are displaying on screen
     uint8_t ts_ticks;       // when the user taps the LIGHT button, we show the timestamp for a few ticks.
-    uint8_t active_minutes; // Active minutes this hour
     int32_t data_points;    // the absolute number of data points logged
     activity_logging_data_point_t data[ACTIVITY_LOGGING_NUM_DATA_POINTS];
 } activity_logging_state_t;