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

Add magnetometer driver & example app #70

Merged
merged 2 commits into from
Jun 4, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion modules/accel_app/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import tidal
import accelerometer

class Accel(TextApp):
class Accel(app.TextApp):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TextApp was explicitly imported from app, so does this actually work? If you were wanting to make it consistent can you update the import as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, don't know how that crept in. Have pushed a fixup commit.

TITLE = "Accelerometer"
timer = None

Expand Down
1 change: 1 addition & 0 deletions modules/app_launcher/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ def list_core_apps(self):
("Sponsors", "sponsors", "Sponsors"),
("Battery", "battery", "Battery"),
("Accelerometer", "accel_app", "Accel"),
("Magnetometer", "magnet_app", "Magnetometer"),
("Settings", "settings_app", "SettingsApp"),
# ("Swatch", "swatch", "Swatch"),
# ("uGUI Demo", "ugui_demo", "uGUIDemo")
Expand Down
29 changes: 29 additions & 0 deletions modules/magnet_app/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from app import TextApp
import magnetometer

class Magnetometer(TextApp):
TITLE = "Magnetometer"
timer = None

def on_start(self):
super().on_start()

def on_activate(self):
super().on_activate()
self.update_screen()
self.timer = self.periodic(500, self.update_screen)

def on_deactivate(self):
self.timer.cancel()
super().on_deactivate()

def update_screen(self):
win = self.window
win.set_next_line(0)
win.println()

(x, y, z) = magnetometer.get_xyz()

win.println(f"X: {x:0.3g}")
win.println(f"Y: {y:0.3g}")
win.println(f"Z: {z:0.3g}")
73 changes: 73 additions & 0 deletions modules/magnetometer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
from tidal import i2c
import ustruct
import time

_inited = False

# See https://github.com/emfcamp/TiDAL-Hardware/blob/main/datasheets/QMC7983%20Datasheet%20-NCP-Rev1.0.pdf

DEVICE_ADDR = 0x2C


REG_XYZ = 0x0
REG_STATUS = 0x6
REG_TOUT_0 = 0x7
REG_TOUT_1 = 0x8
REG_CFG = 0x9
REG_RST = 0xA
REG_FBR = 0xB
REG_OTP_RDY = 0xC
REG_CHIPID = 0xD

_scale = 8

def write(reg, val):
i2c.writeto_mem(DEVICE_ADDR, reg, bytes((val,)))

def read(reg, count):
return i2c.readfrom_mem(DEVICE_ADDR, reg, count)

def read_val(bytes):
return (ustruct.unpack("<h", bytes)[0] / 32768) * _scale

def init():
global _inited
write(REG_RST, 0x80)
time.sleep(0.1)

write(REG_FBR, 0x0F)

if (_scale == 16):
write(REG_CFG, 0x3D)
if (_scale == 12):
write(REG_CFG, 0x2D)
if (_scale == 8):
write(REG_CFG, 0x1D)
if (_scale == 2):
write(REG_CFG, 0x0D)

_inited = True

def sleep():
global _inited
if _inited:
print("Magnetometer sleep")
write(REG_CFG, 0x00)
_inited = False

def check_ready():
ready = read(REG_STATUS, 1)[0] & 1
if not _inited or not ready:
init()

def get_xyz():
try:
check_ready()
rawdata = read(REG_XYZ, 6)
except OSError:
return (0, 0, 0) # is there something more sensible?

x = read_val(rawdata[0:2])
y = read_val(rawdata[2:4])
z = read_val(rawdata[4:6])
return (x, y, z)
2 changes: 2 additions & 0 deletions modules/scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ def main(self, initial_app):

def enter(self):
import accelerometer
import magnetometer
first_time = True
self._level += 1
enter_level = self._level
Expand Down Expand Up @@ -152,6 +153,7 @@ def enter(self):

# Other power management actions we want to do before display off
accelerometer.sleep()
magnetometer.sleep()

# Make sure any debug prints show up on the UART
tidal_helpers.uart_tx_flush(0)
Expand Down