Skip to content

Commit f08fbe1

Browse files
committed
Use named tuple for Modulino Movement
1 parent 5be8367 commit f08fbe1

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

examples/movement.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@
1111
movement = ModulinoMovement()
1212

1313
while True:
14-
print("🏃 Accelerometer: x:{:>8.3f} y:{:>8.3f} z:{:>8.3f}".format(*movement.accelerometer))
15-
print("🌐 Gyroscope: x:{:>8.3f} y:{:>8.3f} z:{:>8.3f}".format(*movement.gyro))
14+
acc = movement.accelerometer
15+
gyro = movement.gyro
16+
17+
print(f"🏃 Accelerometer: x:{acc.x:>8.3f} y:{acc.y:>8.3f} z:{acc.z:>8.3f}")
18+
print(f"🌐 Gyroscope: x:{gyro.x:>8.3f} y:{gyro.y:>8.3f} z:{gyro.z:>8.3f}")
1619
print("")
17-
sleep_ms(100)
20+
sleep_ms(100)

src/modulino/movement.py

+16-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
from .modulino import Modulino
22
from lsm6dsox import LSM6DSOX
3+
from collections import namedtuple
4+
5+
MovementValues = namedtuple('MovementValues', ['x', 'y', 'z'])
6+
"""A named tuple to store the x, y, and z values of the movement sensors."""
37

48
class ModulinoMovement(Modulino):
59
"""
@@ -23,17 +27,23 @@ def __init__(self, i2c_bus = None, address: int | None = None) -> None:
2327
self.sensor = LSM6DSOX(self.i2c_bus, address=self.address)
2428

2529
@property
26-
def accelerometer(self) -> tuple[float, float, float]:
30+
def accelerometer(self) -> MovementValues:
2731
"""
2832
Returns:
29-
tuple[float, float, float]: The acceleration values in the x, y, and z axes.
33+
MovementValues: The acceleration values in the x, y, and z axes.
34+
These values can be accessed as .x, .y, and .z properties
35+
or by using the index operator for tuple unpacking.
3036
"""
31-
return self.sensor.accel()
37+
sensor_values = self.sensor.accel()
38+
return MovementValues(sensor_values[0], sensor_values[1], sensor_values[2])
3239

3340
@property
34-
def gyro(self) -> tuple[float, float, float]:
41+
def gyro(self) -> MovementValues:
3542
"""
3643
Returns:
37-
tuple[float, float, float]: The angular velocity values in the x, y, and z axes.
44+
MovementValues: The gyroscope values in the x, y, and z axes.
45+
These values can be accessed as .x, .y, and .z properties
46+
or by using the index operator for tuple unpacking.
3847
"""
39-
return self.sensor.gyro()
48+
sensor_values = self.sensor.gyro()
49+
return MovementValues(sensor_values[0], sensor_values[1], sensor_values[2])

0 commit comments

Comments
 (0)