|
| 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 | + raw_display.fill(0) |
| 38 | + raw_display.text("Calibrating...", CALIB_MSG1_X, CALIB_MSG1_Y, 15) |
| 39 | + raw_display.text("Move in 8-shape", CALIB_MSG2_X, CALIB_MSG2_Y, 15) |
| 40 | + raw_display.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