Skip to content

Instantly share code, notes, and snippets.

@ypelletier
Created April 25, 2020 16:44
Show Gist options
  • Save ypelletier/1be72484715ec895736a89504c4389c3 to your computer and use it in GitHub Desktop.
Save ypelletier/1be72484715ec895736a89504c4389c3 to your computer and use it in GitHub Desktop.
Affichage d'images en format .bmp sur un écran ST7735. Testé sur STM32 Bluepill.
/*
Affichage d'images en format .bmp sur un
écran ST7735. Testé sur STM32 Bluepill.
Très très très basé sur spitftbitmap.ino par Adafruit:
https://github.com/PaulStoffregen/Adafruit_ST7735/blob/master/examples/spitftbitmap/spitftbitmap.ino
Plus de détails:
https://electroniqueamateur.blogspot.com/2020/04/aficher-sur-un-ecran-st7735-les.html
*/
// inclusion des bibliothèques
#include <Adafruit_GFX.h> // graphics library
#include <Adafruit_ST7735.h> // spécifique à l'écran
#include <SPI.h> // communication SPI (écran et carte SD)
#include <SD.h> // carte SD
// définition des broches utilisées
#define ECRAN_CS PA8
#define ECRAN_RST PA3
#define ECRAN_DC PA2
#define SD_CS PA4
Adafruit_ST7735 tft = Adafruit_ST7735(ECRAN_CS, ECRAN_DC, ECRAN_RST);
void setup(void) {
Serial.begin(9600); // utile pour débogage
tft.initR(INITR_BLACKTAB); // intialisation de l'écran
tft.setRotation(1); // mode paysage
Serial.print("Initialisation de la carte SD...");
if (!SD.begin(SD_CS)) {
Serial.println("Echec!");
return;
}
Serial.println("Reussie!");
}
void loop() {
// on regarde le contenu de la carte SD, et on tente
// d'afficher chaque fichier, un après l'autre.
File root = SD.open("/");
while (true) {
File entry = root.openNextFile();
if (! entry) {
root.close();
return;
}
bmpDraw(entry.name(), 0, 0);
entry.close();
}
}
#define BUFFPIXEL 20 // nombre de pixels traités à la fois.
// la fonction d'Adafruit qui affiche un fichier .bmp:
void bmpDraw(char *filename, uint8_t x, uint16_t y) {
File bmpFile;
int bmpWidth, bmpHeight; // largeur et hauteur en pixels
uint8_t bmpDepth; // bits par pixels (doit être 24)
uint32_t bmpImageoffset; // Début de la description de l'image dans le fichier
uint32_t rowSize;
uint8_t sdbuffer[3 * BUFFPIXEL];
uint8_t buffidx = sizeof(sdbuffer);
boolean goodBmp = false;
boolean flip = true;
int w, h, row, col;
uint8_t r, g, b;
uint32_t pos = 0, startTime = millis();
if ((x >= tft.width()) || (y >= tft.height())) return;
Serial.print(F("Chargement de l'image "));
Serial.println(filename);
// ouverture du fichier
if ((bmpFile = SD.open(filename)) == NULL) {
Serial.print(F("Fichier non trouve"));
return;
}
// Analyse du fichier
if (read16(bmpFile) == 0x4D42) { // lecture des 16 premiers bits du fichier <
read32(bmpFile); // lecture des 32 bits suivants (pas utiles)
read32(bmpFile);
bmpImageoffset = read32(bmpFile);
read32(bmpFile);
bmpWidth = read32(bmpFile);
bmpHeight = read32(bmpFile);
if (read16(bmpFile) == 1) {
bmpDepth = read16(bmpFile); // bits par pixel
if ((bmpDepth == 24) && (read32(bmpFile) == 0)) { // 0 = non compressé
goodBmp = true; // Supported BMP format -- proceed!
rowSize = (bmpWidth * 3 + 3) & ~3;
if (bmpHeight < 0) {
bmpHeight = -bmpHeight;
flip = false;
}
w = bmpWidth;
h = bmpHeight;
if ((x + w - 1) >= tft.width()) w = tft.width() - x;
if ((y + h - 1) >= tft.height()) h = tft.height() - y;
tft.startWrite();
tft.setAddrWindow(x, y, w, h);
for (row = 0; row < h; row++) { // pour chaque ligne
if (flip)
pos = bmpImageoffset + (bmpHeight - 1 - row) * rowSize;
else
pos = bmpImageoffset + row * rowSize;
if (bmpFile.position() != pos) {
tft.endWrite();
bmpFile.seek(pos);
buffidx = sizeof(sdbuffer);
}
for (col = 0; col < w; col++) { // pour chaque pixel
if (buffidx >= sizeof(sdbuffer)) { // Indeed
bmpFile.read(sdbuffer, sizeof(sdbuffer));
buffidx = 0; // Set index to beginning
tft.startWrite();
}
b = sdbuffer[buffidx++];
g = sdbuffer[buffidx++];
r = sdbuffer[buffidx++];
tft.pushColor(tft.color565(r, g, b));
} // fin pixel
} // fin ligne
tft.endWrite();
delay(2000); // on laisse l'image à l'écran 2 secondes
} // fin goodBmp
}
}
bmpFile.close();
if (!goodBmp) Serial.println(F("Format de BMP non reconnu"));
}
uint16_t read16(File f) {
uint16_t result;
((uint8_t *)&result)[0] = f.read(); // LSB
((uint8_t *)&result)[1] = f.read(); // MSB
return result;
}
uint32_t read32(File f) {
uint32_t result;
((uint8_t *)&result)[0] = f.read(); // LSB
((uint8_t *)&result)[1] = f.read();
((uint8_t *)&result)[2] = f.read();
((uint8_t *)&result)[3] = f.read(); // MSB
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment