feat(lis2mdl): Add OLED digital compass example.#362
Conversation
There was a problem hiding this comment.
Pull request overview
Adds a new lis2mdl hardware example that combines the LIS2MDL magnetometer with an SSD1327 OLED to render a live digital compass display, including a startup calibration flow.
Changes:
- Added
lib/lis2mdl/examples/compass_display.pyexample integrating LIS2MDL heading + SSD1327 drawing loop. - Implements a startup 3D min/max calibration routine with on-screen prompts.
- Renders a compass UI (ring, cardinal points, needle, numeric heading + direction label).
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| # Draw the compass outer border (circle) | ||
| display.framebuf.ellipse(CENTER_X, CENTER_Y, COMPASS_RADIUS, COMPASS_RADIUS, 15) | ||
|
|
There was a problem hiding this comment.
framebuf.FrameBuffer on MicroPython typically doesn’t provide an ellipse() method, and the ssd1327 driver doesn’t add one either. Calling display.framebuf.ellipse(...) will raise AttributeError on most ports. Consider replacing this with a small helper that draws a circle using pixel()/line() (e.g., midpoint circle algorithm or a low-resolution polygon approximation), or drop the ring if you want to keep the example minimal.
There was a problem hiding this comment.
Good catch! I implemented the midpoint circle algorithm to keep the UI clean while avoiding the AttributeError.
975fd3c to
47398cf
Compare
|
💡 Suggestion: cette PR utilise le SSD1327 directement avec Tu pourrais simplifier ton code en utilisant : from steami_screen import Screen, SSD1327Display
display = SSD1327Display(ssd1327.WS_OLED_128X128_SPI(spi, dc, res, cs))
screen = Screen(display)
screen.clear()
screen.compass(heading)
screen.show()Cela remplacerait tout le code de dessin manuel (cercles, aiguille, labels cardinaux). Pas obligatoire pour cette PR, mais à considérer. |
Revue — LIS2MDL Compass Display ExampleBon ajout global L’exemple est visuellement intéressant, exploite bien le SSD1327 et démontre une vraie utilisation du LIS2MDL (heading + direction). 1. README non mis à jourL’exemple À faire : 2. PR description — issue non liéeLa PR ne contient pas : Obligatoire selon les conventions GitHub du projet. 3. Commentaire en français dans le codeDans le code : # Si tu préfères l'initiale française pour Ouest...À corriger : 4. Import inutiliséfrom math import cos, pi, sin
À corriger : Points positifs ✓
Résumé des corrections à faire
|
da04777 to
6e9171b
Compare
6e9171b to
a849196
Compare
|
I've updated the PR to address all your points and improve the code structure. Here is a summary of the changes: I added compass_display.py to the examples table in the lis2mdl README and included Closes #328 in the commit message to properly link the issue. I completely refactored the example to use the newly available steami_screen.compass() widget. It’s a great addition to the repo, and it significantly simplified the code. Just a quick clarification on the previous version! The cos function was actually being used in the manual drawing logic to calculate the Y-coordinate of the needle tip (end_y = CENTER_Y - int(NEEDLE_LENGTH * cos(heading_rad))). However, since I've now migrated to the steami_screen widget, all manual trigonometric calculations (sin, cos, pi) are completely obsolete and have been safely removed. I also removed the french comment that I wrote by mistake. Everything is pushed and tested. |
MatteoCnda1
left a comment
There was a problem hiding this comment.
Great example, the code is clean and well structured! The use of screen.compass() is exactly what we'd expect for this kind of example. A few small suggestions to align it perfectly with the project conventions:
- Calibration phase (Line 30 - 41)
The calibration still uses raw_display.text() directly. To stay consistent with the rest of the code that uses steami_screen, you could replace it with:
screen.clear()
screen.title("Calibrating...")
screen.subtitle("Move in 8-shape")
screen.show()
This would also allow removing the CALIB_MSG1_X etc. constants which would no longer be needed.
- Direction label
direction is computed but only printed to serial. It would be a nice touch to also display it on screen with screen.subtitle(direction) — it would make the example even more complete visually!
This is a really nice example, these are minor adjustments and the overall code is very solid 👍
Thank you for your review ! I tried to replace raw_display.text() like you stated it but for some reason I didn't manage to make it work so I kept it that way. In addition, I also thought that displaying the direction directly on the screen would be a nice addition, but we discussed it with Charly and the fact that I'm using the steami_screen library doesn't allow me to display both the compass and the direction on the screen. Moreover, as it is only an exemple, we decided that it would be enough to show the compass on the screen and to print the direction in the serial plot. Hope that helps ! |
Review & corrections appliedCorrections
This is a really nice example, it just lacked the depedency in the |
# [0.17.0](v0.16.9...v0.17.0) (2026-04-16) ### Features * **lis2mdl:** Add OLED digital compass example. ([#362](#362)) ([64ba19a](64ba19a))
|
🎉 This PR is included in version 0.17.0 🎉 The release is available on:
Your semantic-release bot 📦🚀 |
Summary
Closes #328
Adds a new hardware example demonstrating a digital compass by combining the LIS2MDL magnetometer and the SSD1327 OLED display.
Changes
lib/lis2mdl/examples/compass_display.py.calibrate_minmax_3d()) with on-screen user instructions.sin/cos).display.power_off()) in thefinallyblock according to project conventions.Checklist
ruff checkpassespython -m pytest tests/ -k mock -vpasses (no mock test broken)<scope>: <Description.>format