]> git.earman.xyz Git - sensor-watch.git/commitdiff
accelerometer: keep threshold across sleeps by tracking it in movement
authorJoey Castillo <joeycastillo@utexas.edu>
Wed, 25 Jun 2025 21:22:17 +0000 (17:22 -0400)
committerJoey Castillo <joeycastillo@utexas.edu>
Wed, 25 Jun 2025 21:22:17 +0000 (17:22 -0400)
movement.c
movement.h
watch-faces/sensor/accelerometer_status_face.c

index adda773b0d4b034c989ddc0bf17337be51ecaece..33eb231fb94f44600091eb8976bf24fa5b6bf842 100644 (file)
@@ -567,6 +567,24 @@ bool movement_set_accelerometer_background_rate(lis2dw_data_rate_t new_rate) {
     return false;
 }
 
+uint8_t movement_get_accelerometer_motion_threshold(void) {
+    if (movement_state.has_lis2dw) return movement_state.accelerometer_motion_threshold;
+    else return 0;
+}
+
+bool movement_set_accelerometer_motion_threshold(uint8_t new_threshold) {
+    if (movement_state.has_lis2dw) {
+        if (movement_state.accelerometer_motion_threshold != new_threshold) {
+            lis2dw_configure_wakeup_threshold(new_threshold);
+            movement_state.accelerometer_motion_threshold = new_threshold;
+
+            return true;
+        }
+    }
+
+    return false;
+}
+
 float movement_get_temperature(void) {
     float temperature_c = (float)0xFFFFFFFF;
 
@@ -660,6 +678,7 @@ void app_init(void) {
         watch_rtc_set_date_time(date_time);
     }
 
+    if (movement_state.accelerometer_motion_threshold == 0) movement_state.accelerometer_motion_threshold = 32;
 
     movement_state.light_ticks = -1;
     movement_state.alarm_ticks = -1;
@@ -738,7 +757,7 @@ void app_setup(void) {
             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_enable_sleep();                          // allow acceleromter to sleep and wake on activity
-            lis2dw_configure_wakeup_threshold(32);          // g threshold to wake up: (THS * FS / 64) where FS is "full scale" of ±2g.
+            lis2dw_configure_wakeup_threshold(movement_state.accelerometer_motion_threshold); // g threshold to wake up: (THS * 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?
 
             // set up interrupts:
index dbee3830d3c501f6bc0c3ded57afd8be98fcb7ad..56f67a49ed4b379f945d9125ace1f9f76c35a516 100644 (file)
@@ -294,6 +294,8 @@ typedef struct {
     bool has_lis2dw;
     // data rate for background accelerometer sensing
     lis2dw_data_rate_t accelerometer_background_rate;
+    // threshold for considering the wearer is in motion
+    uint8_t accelerometer_motion_threshold;
 } movement_state_t;
 
 void movement_move_to_face(uint8_t watch_face_index);
@@ -380,6 +382,10 @@ bool movement_disable_tap_detection_if_available(void);
 lis2dw_data_rate_t movement_get_accelerometer_background_rate(void);
 bool movement_set_accelerometer_background_rate(lis2dw_data_rate_t new_rate);
 
+// gets and sets the accelerometer motion threshold
+uint8_t movement_get_accelerometer_motion_threshold(void);
+bool movement_set_accelerometer_motion_threshold(uint8_t new_threshold);
+
 // If the board has a temperature sensor, this function will give you the temperature in degrees celsius.
 // If the board has multiple temperature sensors, it will use the most accurate one available.
 // If the board has no temperature sensors, it will return 0xFFFFFFFF.
index 8aa07361cae941aa33d711370ef243361868f7e6..0dbbfdcbe3a27e53b2a4e004e28634bb74cf3068 100644 (file)
@@ -61,7 +61,7 @@ void accelerometer_status_face_activate(void *context) {
     movement_request_tick_frequency(4);
 
     // fetch current threshold from accelerometer
-    state->threshold = lis2dw_get_wakeup_threshold();
+    state->threshold = movement_get_accelerometer_motion_threshold();
 }
 
 bool accelerometer_status_face_loop(movement_event_t event, void *context) {
@@ -87,7 +87,7 @@ bool accelerometer_status_face_loop(movement_event_t event, void *context) {
                 }
                 break;
             case EVENT_ALARM_BUTTON_UP:
-                lis2dw_configure_wakeup_threshold(state->new_threshold);
+                movement_set_accelerometer_motion_threshold(state->new_threshold);
                 state->threshold = state->new_threshold;
                 watch_clear_decimal_if_available();
                 state->is_setting = false;