fix(lis2dw): Re-initialize accelerometer after waking from sleep (#16)
* fix(lis2dw): Re-initialize accelerometer after waking from sleep The LIS2DW accelerometer was not being re-initialized correctly after the device woke from sleep mode. When entering sleep, `watch_enter_sleep_mode` disables peripherals and pins, including those for I²C, to conserve power. Upon waking, the `app_setup` function was designed to re-initialize the hardware. However, the logic only performed the I²C device check once on the initial boot. On subsequent wakes, the `movement_state.has_lis2dw` flag would prevent the I²C bus and the LIS2DW driver from being set up again. This caused any watch face functionality that relied on the accelerometer, such as the countdown face's tap detection, to fail after a sleep/wake cycle. This commit refactors the initialization logic within `app_setup`. It now uses a static boolean `lis2dw_initialized` to ensure the sensor is detected only once at boot time. On subsequent wakes, if the sensor was originally found to be present, the I²C bus is explicitly re-enabled and the driver is re-initialized with `lis2dw_begin()` before its configuration is reapplied. This ensures the accelerometer is reliably available after every wake-up. * fix(lis2dw): only check for accelerometer presence once per boot Previously, devices without the LIS2DW sensor would re-enable I2C and attempt to detect the sensor on every wake from sleep, wasting power and CPU cycles. This change introduces a static `lis2dw_checked` flag that is set after the first detection attempt, regardless of whether the sensor is found. Now, the presence check and I2C enable/disable only occur once per boot, ensuring that devices without the sensor do not repeatedly attempt detection, improving efficiency and battery life.
This commit is contained in:
17
movement.c
17
movement.c
@@ -716,12 +716,22 @@ void app_setup(void) {
|
|||||||
watch_register_interrupt_callback(HAL_GPIO_BTN_ALARM_pin(), cb_alarm_btn_interrupt, INTERRUPT_TRIGGER_BOTH);
|
watch_register_interrupt_callback(HAL_GPIO_BTN_ALARM_pin(), cb_alarm_btn_interrupt, INTERRUPT_TRIGGER_BOTH);
|
||||||
|
|
||||||
#ifdef I2C_SERCOM
|
#ifdef I2C_SERCOM
|
||||||
// if accelerometer was already detected and set up, no need to set it up again.
|
static bool lis2dw_checked = false;
|
||||||
if (!movement_state.has_lis2dw) {
|
if (!lis2dw_checked) {
|
||||||
watch_enable_i2c();
|
watch_enable_i2c();
|
||||||
if (lis2dw_begin()) {
|
if (lis2dw_begin()) {
|
||||||
movement_state.has_lis2dw = true;
|
movement_state.has_lis2dw = true;
|
||||||
|
} else {
|
||||||
|
movement_state.has_lis2dw = false;
|
||||||
|
watch_disable_i2c();
|
||||||
|
}
|
||||||
|
lis2dw_checked = true;
|
||||||
|
} else if (movement_state.has_lis2dw) {
|
||||||
|
watch_enable_i2c();
|
||||||
|
lis2dw_begin();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (movement_state.has_lis2dw) {
|
||||||
lis2dw_set_mode(LIS2DW_MODE_LOW_POWER); // select low power (not high performance) 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_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_low_noise_mode(false); // low noise mode raises power consumption slightly; we don't need it
|
||||||
@@ -760,9 +770,6 @@ void app_setup(void) {
|
|||||||
// If a watch face wants to check in on the A4 pin, it can call movement_set_accelerometer_background_rate
|
// If a watch face wants to check in on the A4 pin, it can call movement_set_accelerometer_background_rate
|
||||||
lis2dw_set_data_rate(LIS2DW_DATA_RATE_POWERDOWN);
|
lis2dw_set_data_rate(LIS2DW_DATA_RATE_POWERDOWN);
|
||||||
movement_state.accelerometer_background_rate = LIS2DW_DATA_RATE_POWERDOWN;
|
movement_state.accelerometer_background_rate = LIS2DW_DATA_RATE_POWERDOWN;
|
||||||
} else {
|
|
||||||
watch_disable_i2c();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user