]> git.earman.xyz Git - sensor-watch.git/commitdiff
movement: prevent invalid tick frequency from breaking scheduled tasks
authorJoey Castillo <joeycastillo@utexas.edu>
Sun, 13 Feb 2022 03:19:01 +0000 (22:19 -0500)
committerJoey Castillo <joeycastillo@utexas.edu>
Sun, 13 Feb 2022 03:19:01 +0000 (22:19 -0500)
movement/movement.c
movement/movement.h

index aa95f2f1f711e2a2e2b269a2ac5c075466b48798..42b7352666125ee06addee09e8a02664ca2c5527 100644 (file)
@@ -152,14 +152,19 @@ static void _movement_handle_scheduled_tasks(void) {
 }
 
 void movement_request_tick_frequency(uint8_t freq) {
-    if (freq == 128) return; // Movement uses the 128 Hz tick internally
+    // Movement uses the 128 Hz tick internally
+    if (freq == 128) return;
+
+    // Movement requires at least a 1 Hz tick.
+    // If we are asked for an invalid frequency, default back to 1 Hz.
+    if (freq == 0 || __builtin_popcount(freq) != 1) freq = 1;
 
     // disable all callbacks except the 128 Hz one
     watch_rtc_disable_matching_periodic_callbacks(0xFE);
 
     movement_state.subsecond = 0;
     movement_state.tick_frequency = freq;
-    if (freq) watch_rtc_register_periodic_callback(cb_tick, freq);
+    watch_rtc_register_periodic_callback(cb_tick, freq);
 }
 
 void movement_illuminate_led(void) {
index 8949d0264bcff1bf6eec039daab7e1a13d7f4a55..cb2c9ca2bad0d82674e270a4a84a738cb525ccb3 100644 (file)
@@ -274,8 +274,6 @@ void movement_move_to_face(uint8_t watch_face_index);
 void movement_move_to_next_face(void);
 void movement_illuminate_led(void);
 
-// note: requesting a tick frequency of 0 will break any scheduled background tasks.
-// this will be fixed in a future refactor of the tick mechanism.
 void movement_request_tick_frequency(uint8_t freq);
 
 // note: watch faces can only schedule a background task when in the foreground, since