diff --git a/modules/accelerometer.py b/modules/accelerometer.py index 66fc915..f916620 100644 --- a/modules/accelerometer.py +++ b/modules/accelerometer.py @@ -3,6 +3,7 @@ import ustruct _inited = False +_active = False # See https://github.com/emfcamp/TiDAL-Hardware/blob/main/datasheets/2004281102_QST-QMA7981_C457290.pdf @@ -34,7 +35,7 @@ def write(reg, val): i2c.writeto_mem(DEVICE_ADDR, reg, bytes((val,))) def init(): - global _inited + global _inited, _active write(REG_RESET, 0xB6) time.sleep(0.2) write(REG_RESET, 0) @@ -45,18 +46,32 @@ def init(): write(REG_BW, 0xE0 | DIV_1935) # None of these actually seem to work... - write(REG_STEP_CONF_0, 0x82) # STEP_SAMPLE_CNT=? - write(REG_STEP_CONF_1, 0x80) # STEP_PRECISION=? - write(REG_STEP_CONF_2, 1) # STEP_TIME_LOW=? - write(REG_STEP_CONF_3, 0xFF) # STEP_TIME_UP=? - write(REG_STEP_CONF_4, 0) # STEP_START_CNT, STEP_COUNT_PEAK, STEP_COUNT_P2P + # write(REG_STEP_CONF_0, 0x82) # STEP_SAMPLE_CNT=? + # write(REG_STEP_CONF_1, 0x80) # STEP_PRECISION=? + # write(REG_STEP_CONF_2, 1) # STEP_TIME_LOW=? + # write(REG_STEP_CONF_3, 0xFF) # STEP_TIME_UP=? + # write(REG_STEP_CONF_4, 0) # STEP_START_CNT, STEP_COUNT_PEAK, STEP_COUNT_P2P _inited = True + _active = True -def get_step_count(): +def sleep(): + global _active + if _inited and _active: + print("Accelerometer sleep") + write(REG_PM, MCLK_50KHZ) + _active = False + +def check_active(): + global _active if not _inited: init() + if not _active: + write(REG_PM, 0xC0 | MCLK_50KHZ) + _active = True +def get_step_count(): + check_active() data = i2c.readfrom_mem(DEVICE_ADDR, REG_STEP_CNT_0, 2) data2 = i2c.readfrom_mem(DEVICE_ADDR, REG_STEP_CNT_2, 1) return data[0] + (data[1] << 8) + (data2[0] << 16) @@ -69,8 +84,7 @@ def _read_val(bytes): return (raw * _scale)/ (1 << 13); def get_xyz(): - if not _inited: - init() + check_active() rawdata = i2c.readfrom_mem(DEVICE_ADDR, REG_DXL, 6) x = _read_val(rawdata[0:2]) diff --git a/modules/scheduler.py b/modules/scheduler.py index 8f866cd..0cf9aef 100644 --- a/modules/scheduler.py +++ b/modules/scheduler.py @@ -90,7 +90,8 @@ def main(self, initial_app): self.reset_inactivity() self.enter() - def enter(self): + def enter(self): + import accelerometer first_time = True self._level += 1 enter_level = self._level @@ -149,6 +150,9 @@ def enter(self): # interrupt for. self.wake_lcd_buttons.activate() + # Other power management actions we want to do before display off + accelerometer.sleep() + # Make sure any debug prints show up on the UART tidal_helpers.uart_tx_flush(0)