Les deux révisions précédentes Révision précédente Prochaine révision | Révision précédente |
python:micropython:materiel:ssd1306 [2023/04/28 17:18] – [1. Présentation] phil | python:micropython:materiel:ssd1306 [Date inconnue] (Version actuelle) – supprimée - modification externe (Date inconnue) 127.0.0.1 |
---|
[[:python:micropython:accueil|{{ :iconemaison.jpg?nolink&30|Sommaire MicroPython, CircuitPython}}]] | |
| |
===== MicroPython - Afficheurs à circuit SSD1306 ===== | |
{{ :micropython:logomicropython.png?nolink&100|}} | |
| |
[Mise à jour le : 28/4/2023] | |
| |
* **Ressources** | |
* <html><a href="https://docs.micropython.org/en/latest/esp32/tutorial/intro.html" target="_blank">Getting started with MicroPython on the ESP32, RP2, etc.</a></html> | |
* <html><a href="https://micropython.org/" target="_blank">MicroPython.org</a></html> | |
* <html><a href="https://docs.micropython.org/en/latest/index.html#" target="_blank">MicroPython documentation</a></html> | |
* <html><a href="https://thonny.org/" target="_blank">IDE Thonny</a></html> | |
| |
* **Lectures connexes** | |
* **Installer MicroPython** - [[python:micropython:materiel:espressif|MicroPython - Les modules Espressif ESP32 et ESP8266]] | |
* **Raspberry Pi Pico** - [[:python:micropython:raspypico2040|Les modules Raspberry Pi Pico et Pico W]] | |
* **Programmez** ! Juillet/Août 2019 | |
* **Elektor 489** Mai/Juin 2021 | |
| |
---- | |
| |
| |
{{ :arduino:gravity-i2c-oled-128x64-display.jpg?nolink&150|Gravity OLED-2864 Display}} | |
==== 1. Présentation ==== | |
Les exemples de code de cette page ont été testés sur un afficheur <html><a href="https://www.gotronic.fr/art-module-oled-i2c-gravity-dfr0486-26805.htm" target="_blank">Gravity OLED-2864 (128x64) de DFROBOT</a></html> et un <html><a href="https://www.gotronic.fr/art-module-afficheur-oled-0-96-tf052-28511.htm" target="_blank">Module afficheur OLED 0,96'' TF052</a></html> connectés à un **ESP32** ou à un **Raspberry Pi Pico**. Ces afficheurs sont pilotés par un circuit SOLOMON SYSTECH <html><a href="https://cdn-shop.adafruit.com/datasheets/SSD1306.pdf" target="_blank">SSD1306</a></html>. Sa mise en oeuvre nécessite une bibliothèque (téléchargeable à partir du lien ci-dessous). | |
| |
<callout type="warning" icon="true"><html><a href="https://github.com/stlehmann/micropython-ssd1306" target="_blank"><strong>Télécharger</strong></a></html> la bibliothèque SSD1306 pour MicroPython et la copier dans un fichier nommé ssd1306.py à installer dans la carte à µC. Celle bibliothèque hérite de <html><a href="https://docs.micropython.org/en/latest/pyboard/library/framebuf.html" target="_blank">framebuf</a></html>. On dispose donc des méthodes ci-dessous pour dessiner des formes sur le buffer d'impression.</callout> | |
| |
* **Organisation de l'écran**{{ :python:micropython:materiel:350px-feather-micropython-oled-position.png?nolink |}} | |
* **x** : position du point par rapport au côté gauche de l'écran. \\ | |
* **y** : position du point par rapport au dessus de l'écran. \\ | |
* **w** : largeur (du mot Width). \\ | |
* **h** : hauteur (du mot Height). \\ | |
* **c** : couleur (1=point allumé, 0=point éteint) \\ | |
| |
* **Méthodes de la bibliothèque SSD1306** | |
| |
^ Prototype ^ Description ^ | |
| **fill**( c ) | Remplit l'écran en noir (c=1) ou en blanc (c=0) | | |
| **rect**(x,y,w,h,c) | Dessine un rectangle de largeur w et de hauteur h dans la couleur c au point (x,y) | | |
| **hline**(x,y,w,c) | Dessine une ligne horizontale de longueur w dans la couleur c au point (x,y) | | |
| **line**(x1,y1,x2,y2,c) | Dessine une ligne dans la couleur c entre les points (x1,y1) et (x2,y2) | | |
| **pixel**(x,y,c) | Affiche un pixel dans la couleur c au point (x,y) | | |
| **scroll**(depl.horiz, depl. vert.) | Déplace le contenu de l'écran de n points | | |
| **show**() | Transfert le contenu du buffer d'affichage sur l'écran | | |
| **text**("text",x,y,c) | Affiche le texte text dans la couleur c au point (x,y) | | |
| **vline**(x,y,h,c) | Dessine une ligne verticale de longueur h dans la couleur c au point (x,y) | | |
| |
| |
==== 2. Programmation ==== | |
<callout type="warning" icon="true">A l'exception de scroll(), les méthodes ci-dessus "écrivent" dans le tampon d'affichage.</callout> | |
| |
=== 2.1 Configurations === | |
| |
<callout type="primary" icon="true">Dans les exemples de cette page, les microcontrôleurs accèdent à l'afficheur via le bus I2C. Il faut au préalable le configurer et créer une instance de la classe SSD1306 comme ci-dessous.</callout> | |
| |
// Exemple pour un **ESP32 Feather Huzzah**// | |
| |
<code python main.py> | |
from machine import Pin, SoftI2C | |
import ssd1306 | |
import time | |
import urandom | |
| |
# Configuration du bus i2c sur l'ESP32 Feather Huzzah | |
i2c = SoftI2C(scl=Pin(22), sda=Pin(23),freq=100000) | |
| |
# Dimension de l'afficheur oled (ssd1306) | |
oled_width = 128 # px | |
oled_height = 64 # px | |
| |
# Construction de l'objet oled | |
oled = ssd1306.SSD1306_I2C(oled_width, oled_height, i2c) | |
</code> | |
| |
// Exemple pour un **Raspberry Pi Pico**// | |
<code python main.py> | |
# Modifications à apporter au code ci-dessus | |
from machine import Pin, I2C | |
... | |
| |
# Configuration du bus i2c sur le Raspberry Pi Pico | |
i2c = I2C(0) | |
... | |
| |
</code> | |
| |
Une fois créée, l'instance de la classe SSD1306 est utilisable pour afficher du texte et des graphiques sur l'écran. | |
| |
=== 2.2 Démos === | |
* **Démo 1** : affichage d'un texte | |
| |
<code python main.py> | |
# A ajouter au code du §2.1 | |
# Division de l'afficheur en 8 lignes et 9 colonnes | |
lin_hight = 9 | |
col_width = 8 | |
def text_write(text, lin, col): | |
oled.text(text,col*col_width,lin*lin_hight) | |
| |
oled.fill(0) # Extinction de l'afficheur | |
text_write("MicroPython",1,2); | |
text_write("sur",3,6) | |
text_write("ESP32",5,5) | |
oled.show() | |
| |
time.sleep(1) | |
</code> | |
| |
* **Démo 2** : affichage de lignes | |
| |
<code python main.py> | |
# A ajouter au code du §2.1 | |
# ligne horizontale : hline(pos_x1,pos_y1,pos_x2,pos_y2,c) | |
# ligne verticale : vline(pos_x1,pos_y1,pos_x2,pos_y2,c) | |
# c=0=>noir, c=1=>blanc | |
| |
oled.hline(0,0,oled_width-1,1) | |
oled.hline(0,oled_height-1,oled_width-1,1) | |
oled.vline(0,0,oled_height,1) | |
oled.vline(oled_width-1,0,oled_height,1) | |
oled.show() | |
| |
time.sleep(3) | |
</code> | |
| |
* **Démo 3** : affichage aléatoire de pixels | |
| |
<code python main.py> | |
# A ajouter au code du §2.1 | |
oled.fill(0) # Ecran noir | |
| |
for n in range(50): | |
pos_x = urandom.randint(1,oled_width) | |
pos_y = urandom.randint(1,oled_height) | |
oled.pixel(pos_x,pos_y,1) | |
oled.show() | |
| |
time.sleep(3) | |
</code> | |
| |
* **Démo 4** : affichage d'une icône | |
| |
<code python main.py> | |
# A ajouter au code du §2.1 | |
ICON = [ | |
[0,0,0,0,1,1,1,0,0,0,0], | |
[0,0,0,1,0,0,0,1,0,0,0], | |
[0,0,1,0,0,0,0,0,1,0,0], | |
[0,1,0,0,0,0,0,0,0,1,0], | |
[1,0,0,0,0,0,0,0,0,0,1], | |
[1,1,1,1,1,1,1,1,1,1,1], | |
[0,0,0,0,1,1,1,0,0,0,0], | |
[0,0,0,1,0,1,0,1,0,0,0], | |
[0,0,1,0,0,1,0,0,1,0,0], | |
[0,1,0,0,0,1,0,0,0,1,0], | |
[0,0,0,0,1,1,1,0,0,0,0], | |
] | |
| |
# Démo 4a : affichage d'une icône au centre | |
oled.fill(0) # Extinction de l'afficheur | |
for y in range (11): | |
for x in range (11): | |
oled.pixel(x+64,y+32,ICON[y][x]) | |
oled.show() | |
| |
time.sleep(2) | |
| |
# Démo 4b : affichage d'icônes au hazard | |
for n in range(12): | |
pos_x = urandom.randint(1,oled_width-12) | |
pos_y = urandom.randint(1,oled_height-12) | |
for y, ligne in enumerate(ICON): | |
for x, c in enumerate(ligne): | |
oled.pixel(x+pos_x,y+pos_y,c) | |
oled.show() | |
| |
time.sleep(3) | |
</code> | |
| |
{{ :python:micropython:materiel:thonny.png?nolink&70|}} | |
<callout type="tip" icon="true"><html><strong><a href="https://webge.fr/doc/wikis/code/micropython/MICROPYTHON_ESP32_SSD1306_DEMO.zip" target="_blank">Télécharger</a></strong></html> le projet MICROPYTHON_ESP32_SSD1306_DEMO pour Thonny et la <html><strong><a href="https://webge.fr/doc/wikis/videos/VIDEO_MICROPYTHON_ESP32_DEMO_ SSD1306.zip" target="_blank">vidéo</a></strong></html> des démos.</callout> | |
| |
* **Démo 5 ** : affichage d'une icône avec canal alpha \\ Modifier l'exemple du site <html><a href="https://wiki.mchobby.be/index.php?title=FEATHER-MICROPYTHON-OLED" target="_blank">MCHobby</a></html> en prenant en compte le code du §2.1 pour un ESP32 ou un Raspberry Pi Pico. | |
| |
| |