Différences
Ci-dessous, les différences entre deux révisions de la page.
Les deux révisions précédentes Révision précédente Prochaine révision | Révision précédenteDernière révisionLes deux révisions suivantes | ||
python:micropython:materiel:ssd1306 [2021/07/08 19:20] – [2. Programmation] phil | python:micropython:materiel:ssd1306 [2023/06/04 11:30] – [1. Présentation] phil | ||
---|---|---|---|
Ligne 1: | Ligne 1: | ||
+ | [[: | ||
+ | |||
+ | ===== MicroPython - Afficheurs à circuit SSD1306 ===== | ||
+ | {{ : | ||
+ | |||
+ | [Mise à jour le : 19/ | ||
+ | |||
+ | * **Ressources** | ||
+ | * < | ||
+ | * < | ||
+ | * < | ||
+ | * < | ||
+ | |||
+ | * **Lectures connexes** | ||
+ | * **Installer MicroPython** - [[python: | ||
+ | * **Raspberry Pi Pico** - [[: | ||
+ | * **Programmez** ! Juillet/ | ||
+ | * **Elektor 489** Mai/Juin 2021 | ||
+ | |||
+ | ---- | ||
+ | |||
+ | |||
+ | {{ : | ||
+ | ==== 1. Présentation ==== | ||
+ | Les exemples de code de cette page ont été testés sur un afficheur < | ||
+ | |||
+ | <callout type=" | ||
+ | |||
+ | * **Organisation de l' | ||
+ | * **x** : position du point par rapport au côté gauche de l' | ||
+ | * **y** : position du point par rapport au dessus de l' | ||
+ | * **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** (hérite de framebuf) | ||
+ | |||
+ | ^ Prototype | ||
+ | | **fill**( c ) | Remplit l' | ||
+ | | **rect** ( x , y , w , h , c[, f]) | Dessine un rectangle de largeur w et de hauteur h dans la couleur c au point (x,y). Le paramètre facultatif f peut être défini sur True pour remplir le rectangle. Sinon, seul un contour d'un pixel est dessiné. | ||
+ | | **hline**(x, | ||
+ | | **vline**(x, | ||
+ | | **line**(x1, | ||
+ | | **pixels** ( x , y[, c]) | Affiche un pixel dans la couleur c au point (x,y). Si c n'est pas donné, obtient la valeur de couleur du pixel spécifié. Si c est donné, définit le pixel spécifié sur la couleur donnée. | ||
+ | | **scroll**(depl.horiz, | ||
+ | | **show**() | ||
+ | | **text**(" | ||
+ | | **poly** ( x , y , coordonnées , c[, f]) | Étant donné une liste de coordonnées, | ||
+ | | **ellipse**( x , y , xr , an , c[, f , m]) | Dessine une ellipse à l' | ||
+ | |||
+ | |||
+ | ==== 2. Programmation ==== | ||
+ | <callout type=" | ||
+ | |||
+ | === 2.1 Configurations === | ||
+ | |||
+ | <callout type=" | ||
+ | |||
+ | // 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' | ||
+ | i2c = SoftI2C(scl=Pin(22), | ||
+ | |||
+ | # Dimension de l' | ||
+ | oled_width = 128 # px | ||
+ | oled_height = 64 # px | ||
+ | |||
+ | # Construction de l' | ||
+ | oled = ssd1306.SSD1306_I2C(oled_width, | ||
+ | </ | ||
+ | |||
+ | // 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 | ||
+ | # Accès au connecteur I2C0 du shield Grove | ||
+ | i2c = I2C(0, | ||
+ | # Accès au connecteur I2C1 du shield Grove | ||
+ | #i2c = I2C(1, | ||
+ | ... | ||
+ | </ | ||
+ | |||
+ | Une fois créée, l' | ||
+ | |||
+ | === 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' | ||
+ | lin_hight = 9 | ||
+ | col_width = 8 | ||
+ | def text_write(text, | ||
+ | oled.text(text, | ||
+ | |||
+ | oled.fill(0) # Extinction de l' | ||
+ | text_write(" | ||
+ | text_write(" | ||
+ | text_write(" | ||
+ | oled.show() | ||
+ | |||
+ | time.sleep(1) | ||
+ | </ | ||
+ | |||
+ | * **Démo 2** : affichage de lignes | ||
+ | |||
+ | <code python main.py> | ||
+ | # A ajouter au code du §2.1 | ||
+ | # ligne horizontale : hline(pos_x1, | ||
+ | # ligne verticale : vline(pos_x1, | ||
+ | # c=0=> | ||
+ | |||
+ | oled.hline(0, | ||
+ | oled.hline(0, | ||
+ | oled.vline(0, | ||
+ | oled.vline(oled_width-1, | ||
+ | oled.show() | ||
+ | |||
+ | time.sleep(3) | ||
+ | </ | ||
+ | |||
+ | * **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, | ||
+ | pos_y = urandom.randint(1, | ||
+ | oled.pixel(pos_x, | ||
+ | oled.show() | ||
+ | |||
+ | time.sleep(3) | ||
+ | </ | ||
+ | |||
+ | * **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, | ||
+ | [0, | ||
+ | [0, | ||
+ | [0, | ||
+ | [0, | ||
+ | [0, | ||
+ | ] | ||
+ | |||
+ | # Démo 4a : affichage d'une icône au centre | ||
+ | oled.fill(0) # Extinction de l' | ||
+ | for y in range (11): | ||
+ | for x in range (11): | ||
+ | oled.pixel(x+64, | ||
+ | oled.show() | ||
+ | |||
+ | time.sleep(2) | ||
+ | |||
+ | # Démo 4b : affichage d' | ||
+ | for n in range(12): | ||
+ | pos_x = urandom.randint(1, | ||
+ | pos_y = urandom.randint(1, | ||
+ | for y, ligne in enumerate(ICON): | ||
+ | for x, c in enumerate(ligne): | ||
+ | oled.pixel(x+pos_x, | ||
+ | oled.show() | ||
+ | | ||
+ | time.sleep(3) | ||
+ | </ | ||
+ | |||
+ | {{ : | ||
+ | <callout type=" | ||
+ | |||
+ | * **Démo 5 ** : affichage d'une icône avec canal alpha \\ Modifier l' | ||
+ | |||