Skip to content

feat(lis2mdl): Add OLED digital compass example.#362

Open
Kaanoz-en wants to merge 1 commit intomainfrom
feat/lis2mdl-compass-display
Open

feat(lis2mdl): Add OLED digital compass example.#362
Kaanoz-en wants to merge 1 commit intomainfrom
feat/lis2mdl-compass-display

Conversation

@Kaanoz-en
Copy link
Copy Markdown
Contributor

@Kaanoz-en Kaanoz-en commented Apr 9, 2026

Summary

Closes #328

Adds a new hardware example demonstrating a digital compass by combining the LIS2MDL magnetometer and the SSD1327 OLED display.

Changes

  • Added lib/lis2mdl/examples/compass_display.py.
  • Implemented a 3D calibration sequence on startup (calibrate_minmax_3d()) with on-screen user instructions.
  • Created a dynamic, round-screen optimized UI featuring:
    • An outer compass ring with N, E, S, W cardinal points.
    • A real-time animated needle using trigonometric math (sin/cos).
    • Text readouts for both the exact degree heading and the cardinal direction label.
  • Ensured proper hardware cleanup (display.power_off()) in the finally block according to project conventions.
  • Kept UI math efficient and grouped layout constants cleanly at the top of the file.

Checklist

  • ruff check passes
  • python -m pytest tests/ -k mock -v passes (no mock test broken)
  • Tested on hardware (if applicable)
  • README updated (if adding/changing public API)
  • Examples added/updated (if applicable)
  • Commit messages follow <scope>: <Description.> format

Copilot AI review requested due to automatic review settings April 9, 2026 14:24
@Kaanoz-en Kaanoz-en requested a review from nedseb April 9, 2026 14:24
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.py example 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.

Comment on lines +75 to +77
# Draw the compass outer border (circle)
display.framebuf.ellipse(CENTER_X, CENTER_Y, COMPASS_RADIUS, COMPASS_RADIUS, 15)

Copy link

Copilot AI Apr 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copilot uses AI. Check for mistakes.
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! I implemented the midpoint circle algorithm to keep the UI clean while avoiding the AttributeError.

@Kaanoz-en Kaanoz-en force-pushed the feat/lis2mdl-compass-display branch from 975fd3c to 47398cf Compare April 9, 2026 14:35
@nedseb nedseb requested review from Charly-sketch and removed request for nedseb April 10, 2026 12:03
@nedseb
Copy link
Copy Markdown
Contributor

nedseb commented Apr 10, 2026

💡 Suggestion: cette PR utilise le SSD1327 directement avec framebuf. Le projet a maintenant steami_screen qui fournit un widget screen.compass(heading) prêt à l'emploi (voir lib/steami_screen/examples/compass_demo.py).

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.

@Charly-sketch
Copy link
Copy Markdown
Contributor

Revue — LIS2MDL Compass Display Example

Bon ajout global L’exemple est visuellement intéressant, exploite bien le SSD1327 et démontre une vraie utilisation du LIS2MDL (heading + direction).
Mais il y a plusieurs petit points à corriger pour être conforme aux conventions du repo et à la qualité attendue.

1. README non mis à jour

L’exemple compass_display.py n’est pas ajouté dans le README du driver lis2mdl.

À faire :
Ajouter une ligne dans le tableau :

compass_display.py | Graphical compass with OLED display

2. PR description — issue non liée

La PR ne contient pas :

Closes #328

Obligatoire selon les conventions GitHub du projet.

3. Commentaire en français dans le code

Dans le code :

# Si tu préfères l'initiale française pour Ouest...

À corriger :
Tu n'auras plus besoin de cet partie si tu utilise le widget compass

4. Import inutilisé

from math import cos, pi, sin

cos n’est jamais utilisé

À corriger :
Tu n'auras plus besoin de cet import si tu utilise le widget compass

Points positifs ✓

  • Très bon rendu visuel (compass + needle)
  • Utilisation correcte de heading_flat_only() ✔️
  • Bonne gestion du refresh (sleep_ms(50))
  • Affichage direction + degrés
  • Calibration intégrée (bonne idée UX)
  • Code globalement lisible et structuré

Résumé des corrections à faire

  • Ajouter l’exemple dans le README
  • Ajouter Closes #328 dans la PR
  • Supprimer le commentaire en français
  • Supprimer cos inutilisé

@Kaanoz-en Kaanoz-en force-pushed the feat/lis2mdl-compass-display branch 3 times, most recently from da04777 to 6e9171b Compare April 10, 2026 14:13
@Kaanoz-en Kaanoz-en force-pushed the feat/lis2mdl-compass-display branch from 6e9171b to a849196 Compare April 10, 2026 14:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(lis2mdl): Add compass display example with OLED.

4 participants