Skip to content
Snippets Groups Projects
Commit ce6c1258 authored by schneider's avatar schneider
Browse files

imu: make it part of InputState

parent a109bf32
Branches
Tags
No related merge requests found
# flow3r imports
from st3m import InputState
from st3m.application import Application
from ctx import Context
from st3m.utils import tau
class IMUDemo(Application):
def __init__(self, name: str) -> None:
super().__init__(name)
self.v_x = 0.0
self.v_y = 0.0
self.p_x = 0.0
self.p_y = 0.0
def draw(self, ctx: Context) -> None:
ctx.rectangle(-120, -120, 240, 240)
ctx.rgb(0, 0, 0)
ctx.fill()
ctx.arc(self.p_x, self.p_y, 10, 0, tau, 0)
ctx.rgb(117 / 255, 255 / 255, 226 / 255)
ctx.fill()
def think(self, ins: InputState, delta_ms: int) -> None:
super().think(ins, delta_ms)
self.v_y += ins.acc[0] * delta_ms / 1000.0 * 10
self.v_x += ins.acc[1] * delta_ms / 1000.0 * 10
x = self.p_x + self.v_x * delta_ms / 1000.0
y = self.p_y + self.v_y * delta_ms / 1000.0
if x**2 + y**2 < (120 - 10) ** 2:
self.p_x = x
self.p_y = y
else:
self.v_x = 0
self.v_y = 0
[app]
name = "IMU Demo"
menu = "Apps"
[entry]
class = "IMUDemo"
[metadata]
author = "Flow3r Badge Authors"
license = "LGPL-3.0-only"
url = "https://git.flow3r.garden/flow3r/flow3r-firmware"
...@@ -3,8 +3,9 @@ from st3m.goose import List, Optional, Enum, Tuple ...@@ -3,8 +3,9 @@ from st3m.goose import List, Optional, Enum, Tuple
import hardware import hardware
import captouch import captouch
import imu import imu
from st3m.power import Power
import machine power = Power()
class InputState: class InputState:
...@@ -20,11 +21,21 @@ class InputState: ...@@ -20,11 +21,21 @@ class InputState:
captouch: captouch.CaptouchState, captouch: captouch.CaptouchState,
left_button: int, left_button: int,
right_button: int, right_button: int,
acc: Tuple[float, float, float],
gyro: Tuple[float, float, float],
pressure: float,
temperature: float,
battery_voltage: float,
) -> None: ) -> None:
# self.petal_pads = petal_pads # self.petal_pads = petal_pads
self.captouch = captouch self.captouch = captouch
self.left_button = left_button self.left_button = left_button
self.right_button = right_button self.right_button = right_button
self.acc = acc
self.gyro = gyro
self.pressure = pressure
self.temperature = temperature
self.battery_voltage = battery_voltage
@classmethod @classmethod
def gather(cls, swapped_buttons: bool = False) -> "InputState": def gather(cls, swapped_buttons: bool = False) -> "InputState":
...@@ -38,7 +49,20 @@ class InputState: ...@@ -38,7 +49,20 @@ class InputState:
if swapped_buttons: if swapped_buttons:
left_button, right_button = right_button, left_button left_button, right_button = right_button, left_button
return InputState(cts, left_button, right_button) acc = imu.acc_read()
gyro = imu.gyro_read()
pressure, temperature = imu.pressure_read()
battery_voltage = power.battery_voltage
return InputState(
cts,
left_button,
right_button,
acc,
gyro,
pressure,
temperature,
battery_voltage,
)
class RepeatSettings: class RepeatSettings:
...@@ -475,24 +499,6 @@ class IMUState: ...@@ -475,24 +499,6 @@ class IMUState:
self.pressure, self.temperature = imu.pressure_read() self.pressure, self.temperature = imu.pressure_read()
class PowerState:
__slots__ = ("_ts", "_adc_pin", "_adc", "battery_voltage")
def __init__(self) -> None:
self._adc_pin = machine.Pin(9, machine.Pin.IN)
self._adc = machine.ADC(self._adc_pin, atten=machine.ADC.ATTN_11DB)
self.battery_voltage = self._battery_voltage_sample()
self._ts = 0
def _battery_voltage_sample(self) -> float:
return self._adc.read_uv() * 2 / 1e6
def _update(self, ts: int, hr: InputState) -> None:
if ts >= self._ts + 1000:
self.battery_voltage = self._battery_voltage_sample()
self._ts = ts
class InputController: class InputController:
""" """
A stateful input controller. It accepts InputState updates from the Reactor A stateful input controller. It accepts InputState updates from the Reactor
...@@ -509,8 +515,6 @@ class InputController: ...@@ -509,8 +515,6 @@ class InputController:
"captouch", "captouch",
"left_shoulder", "left_shoulder",
"right_shoulder", "right_shoulder",
"imu",
"power",
"_ts", "_ts",
) )
...@@ -518,8 +522,6 @@ class InputController: ...@@ -518,8 +522,6 @@ class InputController:
self.captouch = CaptouchState() self.captouch = CaptouchState()
self.left_shoulder = TriSwitchState(TriSwitchHandedness.left) self.left_shoulder = TriSwitchState(TriSwitchHandedness.left)
self.right_shoulder = TriSwitchState(TriSwitchHandedness.right) self.right_shoulder = TriSwitchState(TriSwitchHandedness.right)
self.imu = IMUState()
self.power = PowerState()
self._ts = 0 self._ts = 0
def think(self, hr: InputState, delta_ms: int) -> None: def think(self, hr: InputState, delta_ms: int) -> None:
...@@ -527,8 +529,6 @@ class InputController: ...@@ -527,8 +529,6 @@ class InputController:
self.captouch._update(self._ts, hr) self.captouch._update(self._ts, hr)
self.left_shoulder._update(self._ts, hr) self.left_shoulder._update(self._ts, hr)
self.right_shoulder._update(self._ts, hr) self.right_shoulder._update(self._ts, hr)
self.imu._update(self._ts, hr)
self.power._update(self._ts, hr)
def _ignore_pressed(self) -> None: def _ignore_pressed(self) -> None:
""" """
......
import machine
import time
class Power:
"""
Collects information about the power state (e.g. battery voltage) of the badge.
Member of st3m.input.InputState, should be acquired from there, not instantiated manually.
"""
def __init__(self) -> None:
self._adc_pin = machine.Pin(9, machine.Pin.IN)
self._adc = machine.ADC(self._adc_pin, atten=machine.ADC.ATTN_11DB)
self._battery_voltage = self._battery_voltage_sample()
self._ts = time.ticks_ms()
def _battery_voltage_sample(self) -> float:
return self._adc.read_uv() * 2 / 1e6
def _update(self) -> None:
ts = time.ticks_ms()
if ts >= self._ts + 1000:
# Sampling takes time, don't do it too often
self._battery_voltage = self._battery_voltage_sample()
self._ts = ts
@property
def battery_voltage(self) -> float:
self._update()
return self._battery_voltage
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment