Écrans OLED avec Arduino : technologie et branchement
FunduinoPublié le
Drei Größen, zwei Bussysteme und die Bibliothek u8g2
Comment fonctionne un OLED
Les trois tailles et leur commande
1 / 3
Connecter un écran OLED de 0,96 pouce (I²C)
Connecter un écran OLED de 2,42 pouces (SPI)
Installer la bibliothèque u8g2
1 / 2
Le programme
Présenter toutes les fonctions de dessin de la bibliothèque u8g2
#include "U8g2lib.h"
// Initialiser l’écran OLED de 2,42 pouces
U8G2_SSD1309_128X64_NONAME2_1_4W_HW_SPI oled(U8G2_R0, 10, 9, 8);
// Déterminer les dimensions de l’écran
int BildschirmBreite = oled.getDisplayWidth();
int BildschirmHoehe = oled.getDisplayHeight();
// Smiley au format XBM, créé avec GIMP
#define SmileyBreite 46
#define SmileyHoehe 45
static unsigned char Smiley[] =
{
0x00, 0x00, 0xfe, 0x1f, 0x00, 0x00, 0x00, 0xc0, 0xff, 0xff, 0x00, 0x00,
0x00, 0xf0, 0x07, 0xf8, 0x03, 0x00, 0x00, 0xfc, 0x00, 0xc0, 0x0f, 0x00,
0x00, 0x3e, 0x00, 0x00, 0x1f, 0x00, 0x80, 0x0f, 0x00, 0x00, 0x7c, 0x00,
0xc0, 0x07, 0x00, 0x00, 0xf8, 0x00, 0xe0, 0x01, 0x00, 0x00, 0xe0, 0x01,
0xf0, 0x00, 0x00, 0x00, 0xc0, 0x03, 0x70, 0x00, 0x00, 0x00, 0x80, 0x03,
0x38, 0x7e, 0x00, 0x80, 0x1f, 0x07, 0x38, 0xff, 0x00, 0xc0, 0x3f, 0x07,
0x9c, 0xff, 0x01, 0xc0, 0x3f, 0x0e, 0x9c, 0xe7, 0x01, 0xc0, 0x39, 0x0e,
0x8e, 0xc3, 0x01, 0xc0, 0x30, 0x1c, 0x8e, 0xe3, 0x01, 0xc0, 0x31, 0x1c,
0x86, 0xf7, 0x01, 0xc0, 0x3b, 0x18, 0x87, 0xff, 0x01, 0xc0, 0x3f, 0x38,
0x07, 0xff, 0x00, 0x80, 0x3f, 0x38, 0x03, 0x7e, 0x00, 0x80, 0x1f, 0x30,
0x03, 0x00, 0x00, 0x00, 0x00, 0x30, 0x03, 0x00, 0x00, 0x00, 0x00, 0x30,
0x03, 0x00, 0x00, 0x00, 0x00, 0x30, 0x03, 0x00, 0x00, 0x00, 0x00, 0x30,
0x03, 0x00, 0x00, 0x00, 0x00, 0x30, 0x03, 0x00, 0x00, 0x00, 0x00, 0x30,
0x07, 0x00, 0x00, 0x00, 0x00, 0x38, 0x07, 0x20, 0x00, 0x00, 0x01, 0x38,
0x06, 0x70, 0x00, 0x80, 0x03, 0x18, 0x0e, 0xf0, 0x00, 0xc0, 0x01, 0x1c,
0x0e, 0xe0, 0x03, 0xf0, 0x01, 0x1c, 0x1c, 0xc0, 0x3f, 0xfc, 0x00, 0x0e,
0x1c, 0x80, 0xff, 0x7f, 0x00, 0x0e, 0x38, 0x00, 0xfc, 0x1f, 0x00, 0x07,
0x38, 0x00, 0xc0, 0x03, 0x00, 0x07, 0x70, 0x00, 0x00, 0x00, 0x80, 0x03,
0xf0, 0x00, 0x00, 0x00, 0xc0, 0x03, 0xe0, 0x01, 0x00, 0x00, 0xe0, 0x01,
0xc0, 0x07, 0x00, 0x00, 0xf8, 0x00, 0x80, 0x0f, 0x00, 0x00, 0x7c, 0x00,
0x00, 0x3e, 0x00, 0x00, 0x1f, 0x00, 0x00, 0xfc, 0x00, 0xc0, 0x0f, 0x00,
0x00, 0xf0, 0x07, 0xf8, 0x03, 0x00, 0x00, 0xc0, 0xff, 0xff, 0x00, 0x00,
0x00, 0x00, 0xfe, 0x1f, 0x00, 0x00
};
void setup()
{
// Démarrer le générateur de nombres aléatoires
randomSeed(A0);
// Démarrer l’écran
oled.begin();
// Contraste, maximum 255
oled.setContrast(200);
}
void loop()
{
// Couleur blanche
oled.setDrawColor(1);
// Smiley
oled.firstPage();
do
{
oled.drawXBM(40, 10, SmileyBreite, SmileyHoehe, Smiley);
}
while (oled.nextPage());
delay(2000);
oled.clearDisplay();
// Motif de pixels
oled.firstPage();
do
{
for (int i = 0; i < 500; i++)
{
int x = random(1, BildschirmBreite);
int y = random(1, BildschirmHoehe);
oled.drawPixel(x, y);
}
}
while (oled.nextPage());
delay(2000);
oled.clearDisplay();
// Afficher du texte
oled.firstPage();
do
{
oled.setFontDirection(0);
// Changer de police
oled.setFont(u8g2_font_unifont_t_symbols);
oled.setCursor(2, 10);
oled.print("Text");
// Changer de police
oled.setFont(u8g2_font_helvB14_tf);
oled.setCursor(2, 30);
oled.print("Text");
// Changer de police
oled.setFont(u8g2_font_courB24_tf);
oled.setCursor(2, 60);
oled.print("Text");
}
while (oled.nextPage());
delay(2000);
oled.clearDisplay();
// Cercles
oled.firstPage();
do
{
for (int i = 2; i < BildschirmHoehe / 2; i += 3)
{
oled.drawCircle(BildschirmBreite / 2, BildschirmHoehe / 2, i);
delay(10);
}
}
while (oled.nextPage());
delay(2000);
oled.clearDisplay();
// Cadres
oled.firstPage();
do
{
for (int i = 2; i < BildschirmHoehe; i += 10)
{
oled.drawFrame(0, 0, i, i);
}
}
while (oled.nextPage());
delay(2000);
oled.clearDisplay();
// Lignes verticales
oled.firstPage();
do
{
for (int i = 0; i < BildschirmBreite; i += 4)
{
oled.drawVLine(i, 0, BildschirmBreite - 1);
}
}
while (oled.nextPage());
delay(2000);
oled.clearDisplay();
// Lignes horizontales
oled.firstPage();
do
{
for (int i = 0; i < BildschirmHoehe; i += 4)
{
oled.drawHLine(0, i, BildschirmBreite - 1);
}
}
while (oled.nextPage());
delay(2000);
oled.clearDisplay();
// Cercles remplis
oled.firstPage();
do
{
int Radius = 5;
int StartX = 10;
int StartY = 10;
while (StartX < BildschirmBreite - Radius)
{
for (int i = StartY; i < BildschirmBreite - Radius; i += 20)
{
oled.drawDisc(StartX, i, Radius);
}
StartX += 10;
}
}
while (oled.nextPage());
delay(2000);
oled.clearDisplay();
// Lignes
oled.firstPage();
do
{
for (int i = 0; i < BildschirmBreite; i += 5)
{
oled.drawLine(0, i, 128, 64);
}
for (int i = BildschirmBreite; i > 0; i -= 5)
{
oled.drawLine(BildschirmBreite, i, 0, 0);
}
}
while (oled.nextPage());
delay(2000);
oled.clearDisplay();
}Évaluer le guide