]> git.earman.xyz Git - sensor-watch.git/commitdiff
add movement_get_temperature, works across different sensor boards
authorJoey Castillo <joeycastillo@utexas.edu>
Sat, 17 May 2025 15:03:51 +0000 (11:03 -0400)
committerJoey Castillo <joeycastillo@utexas.edu>
Sat, 17 May 2025 15:03:51 +0000 (11:03 -0400)
movement.c
movement.h
movement_activity.c
watch-faces/sensor/temperature_display_face.c
watch-faces/sensor/temperature_display_face.h
watch-faces/sensor/temperature_logging_face.c
watch-faces/sensor/temperature_logging_face.h

index c7c0bfc1924484ce0bcb68597f255de64cf1a791..d3281c433f7dfd9c15a0676435c684040945efd1 100644 (file)
@@ -560,13 +560,27 @@ bool movement_disable_tap_detection_if_available(void) {
     return false;
 }
 
+float movement_get_temperature(void) {
+    float temperature_c = (float)0xFFFFFFFF;
+
+    if (movement_state.has_thermistor) {
+        thermistor_driver_enable();
+        temperature_c = thermistor_driver_get_temperature();
+        thermistor_driver_disable();
+    } else if (movement_state.has_lis2dw) {
+            int16_t val = lis2dw_get_temperature();
+            val = val >> 4;
+            temperature_c = 25 + (float)val / 16.0;
+    }
+
+    return temperature_c;
+}
+
 void app_init(void) {
     _watch_init();
 
     filesystem_init();
 
-    movement_state.has_thermistor = thermistor_driver_init();
-
     // check if we are plugged into USB power.
     HAL_GPIO_VBUS_DET_in();
     HAL_GPIO_VBUS_DET_pulldown();
@@ -578,6 +592,8 @@ void app_init(void) {
 
     memset(&movement_state, 0, sizeof(movement_state));
 
+    movement_state.has_thermistor = thermistor_driver_init();
+
     bool settings_file_exists = filesystem_file_exists("settings.u32");
     movement_settings_t maybe_settings;
     if (settings_file_exists && maybe_settings.bit.version == 0) {
index 7571b9479513a434b6220a4905d5ea04eda82643..817c28411982210b0f678094cb31d89f11b42c57 100644 (file)
@@ -370,3 +370,8 @@ void movement_set_alarm_enabled(bool value);
 // if the board has an accelerometer, these functions will enable or disable tap detection.
 bool movement_enable_tap_detection_if_available(void);
 bool movement_disable_tap_detection_if_available(void);
+
+// 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.
+float movement_get_temperature(void);
index fe91e29a7b242da40d3d48a3245b27d8a4d58657..cfad8fff4a36a257c97a9498917cd15d44c8bcd1 100644 (file)
@@ -51,9 +51,7 @@ void _movement_log_data(void) {
     }
 
     // log the temperature
-    thermistor_driver_enable();
-    float temperature_c = thermistor_driver_get_temperature();
-    thermistor_driver_disable();
+    float temperature_c = movement_get_temperature();
     // offset the temperature by 30, so -30°C is 0, and 72.3°C is 102.3
     temperature_c = temperature_c + 30;
     if (temperature_c < 0) temperature_c = 0;
index 1fe1dd13c9e80e4ca2d39a54139e85dda61cc949..af7202fb3f1c9c5cfeaa9f4fc77613de8d2fdd03 100644 (file)
 #include "thermistor_driver.h"
 #include "watch.h"
 
-#ifdef HAS_TEMPERATURE_SENSOR
-
 static void _temperature_display_face_update_display(bool in_fahrenheit) {
-    thermistor_driver_enable();
-    float temperature_c = thermistor_driver_get_temperature();
+    float temperature_c = movement_get_temperature();
     if (in_fahrenheit) {
         watch_display_float_with_best_effort(temperature_c * 1.8 + 32.0, "#F");
     } else {
         watch_display_float_with_best_effort(temperature_c, "#C");
     }
-    thermistor_driver_disable();
 }
 
 void temperature_display_face_setup(uint8_t watch_face_index, void ** context_ptr) {
@@ -96,5 +92,3 @@ bool temperature_display_face_loop(movement_event_t event, void *context) {
 void temperature_display_face_resign(void *context) {
     (void) context;
 }
-
-#endif
index 6dfdb53a970265699f0bf3590f0dbe7efaea34fd..d470e0902e5faa01f1764aa1b6cf11a617f6fa70 100644 (file)
@@ -26,8 +26,6 @@
 
 #include "pins.h"
 
-#ifdef HAS_TEMPERATURE_SENSOR
-
 /*
  * THERMISTOR READOUT (aka Temperature Display)
  *
@@ -65,5 +63,3 @@ void temperature_display_face_resign(void *context);
     temperature_display_face_resign, \
     NULL, \
 })
-
-#endif // HAS_TEMPERATURE_SENSOR
index 14ab6336501eaf4e44a93a68420d786273ac8d0b..cf15c7808814d5345485e2b03fe0fad7bb9686e1 100644 (file)
 #include "thermistor_driver.h"
 #include "watch.h"
 
-#ifdef HAS_TEMPERATURE_SENSOR
-
 static void _temperature_logging_face_log_data(thermistor_logger_state_t *logger_state) {
-    thermistor_driver_enable();
     watch_date_time_t date_time = watch_rtc_get_date_time();
     size_t pos = logger_state->data_points % THERMISTOR_LOGGING_NUM_DATA_POINTS;
 
     logger_state->data[pos].timestamp.reg = date_time.reg;
-    logger_state->data[pos].temperature_c = thermistor_driver_get_temperature();
+    logger_state->data[pos].temperature_c = movement_get_temperature();
     logger_state->data_points++;
-
-    thermistor_driver_disable();
 }
 
 static void _temperature_logging_face_update_display(thermistor_logger_state_t *logger_state, bool in_fahrenheit, bool clock_mode_24h) {
@@ -150,5 +145,3 @@ movement_watch_face_advisory_t temperature_logging_face_advise(void *context) {
 
     return retval;
 }
-
-#endif
index cda44bbc4127cee7e16e0dab3bd32287add07fe1..1c4f88f14eaf325f4db8a0f5529feb98eb459292 100644 (file)
@@ -26,8 +26,6 @@
 
 #include "pins.h"
 
-#ifdef HAS_TEMPERATURE_SENSOR
-
 /*
  * THERMISTOR LOGGING (aka Temperature Log)
  *
@@ -86,5 +84,3 @@ movement_watch_face_advisory_t temperature_logging_face_advise(void *context);
     temperature_logging_face_resign, \
     temperature_logging_face_advise, \
 })
-
-#endif // HAS_TEMPERATURE_SENSOR