Skip to content

Commit 6e9171b

Browse files
Kaan OzenKaan Ozen
authored andcommitted
feat(lis2mdl): Add OLED compass example using steami_screen widget.
1 parent bf8a482 commit 6e9171b

2 files changed

Lines changed: 70 additions & 0 deletions

File tree

lib/lis2mdl/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ print("Register dump:", regs)
213213
| `power_on()` / `power_off()` | Power management |
214214
| `soft_reset()` / `reboot()` | Sensor reset functions |
215215

216+
216217
---
217218

218219
## Examples
@@ -229,6 +230,7 @@ print("Register dump:", regs)
229230
| low_power_one_shot.py | Energy-efficient sampling example. Uses `power_off()` between readings and `read_one_shot()` every 10 seconds, then prints values and free memory. |
230231
| magnet_compass.py | Flat compass example that computes heading and cardinal direction from the LIS2MDL magnetic field. Useful for basic orientation demos. |
231232
| magnet_fieldForce.py | Magnetic field magnitude example that shows total field strength in microtesla. Useful for observing magnetic disturbances and relative field changes. |
233+
| compass_display.py | Graphical compass with OLED display |
232234

233235
---
234236

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
"""Digital compass example using LIS2MDL and Steami Screen OLED widget.
2+
3+
Reads the magnetic heading using the built-in flat heading method,
4+
and draws a dynamic compass on the display using the Screen widget.
5+
Includes a preliminary 3D calibration step.
6+
"""
7+
8+
from time import sleep_ms
9+
10+
import ssd1327
11+
from lis2mdl import LIS2MDL
12+
from machine import I2C, SPI, Pin
13+
from steami_screen import Screen, SSD1327Display
14+
15+
# Layout Constants for Calibration Screen
16+
CALIB_MSG1_X = 12
17+
CALIB_MSG1_Y = 50
18+
CALIB_MSG2_X = 4
19+
CALIB_MSG2_Y = 70
20+
21+
# Hardware Initialization
22+
i2c = I2C(1)
23+
mag = LIS2MDL(i2c)
24+
25+
spi = SPI(1)
26+
dc = Pin("DATA_COMMAND_DISPLAY")
27+
res = Pin("RST_DISPLAY")
28+
cs = Pin("CS_DISPLAY")
29+
30+
# Initialize the raw display, then wrap it in the high-level Screen widget system
31+
raw_display = ssd1327.WS_OLED_128X128_SPI(spi, dc, res, cs)
32+
display = SSD1327Display(raw_display)
33+
screen = Screen(display)
34+
35+
try:
36+
# Calibration Phase
37+
screen.clear()
38+
screen.text("Calibrating...", CALIB_MSG1_X, CALIB_MSG1_Y, 15)
39+
screen.text("Move in 8-shape", CALIB_MSG2_X, CALIB_MSG2_Y, 15)
40+
screen.show()
41+
sleep_ms(100)
42+
43+
print("Calibrate the magnetometer by moving it in a figure-eight pattern.")
44+
mag.calibrate_minmax_3d()
45+
print("Calibration complete.")
46+
47+
# Main Compass Loop
48+
while True:
49+
heading_deg = mag.heading_flat_only()
50+
direction = mag.direction_label(heading_deg)
51+
52+
# Drawing Phase (Delegated entirely to the steami_screen widget)
53+
screen.clear()
54+
screen.compass(heading_deg)
55+
screen.show()
56+
57+
print("Angle: {:3.0f}° | Direction: {}".format(heading_deg, direction))
58+
59+
sleep_ms(50)
60+
61+
except KeyboardInterrupt:
62+
pass
63+
finally:
64+
# Clean up and power off display on exit
65+
screen.clear()
66+
screen.show()
67+
sleep_ms(100)
68+
raw_display.power_off()

0 commit comments

Comments
 (0)