Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Put accelerometer into sleep if necessary #64

Merged
merged 2 commits into from
Jun 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 23 additions & 9 deletions modules/accelerometer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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])
Expand Down
6 changes: 5 additions & 1 deletion modules/scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down