|
|
@@ -0,0 +1,333 @@
|
|
|
+// GxEPD2_SD_Example : Display Library example for SPI e-paper panels from Dalian Good Display and boards from Waveshare.
|
|
|
+// Requires HW SPI and Adafruit_GFX. Caution: these e-papers require 3.3V supply AND data lines!
|
|
|
+//
|
|
|
+// Display Library based on Demo Example from Good Display: http://www.good-display.com/download_list/downloadcategoryid=34&isMode=false.html
|
|
|
+//
|
|
|
+// Author: Jean-Marc Zingg
|
|
|
+//
|
|
|
+// Version: see library.properties
|
|
|
+//
|
|
|
+// Library: https://github.com/ZinggJM/GxEPD2
|
|
|
+
|
|
|
+// Supporting Arduino Forum Topics:
|
|
|
+// Waveshare e-paper displays with SPI: http://forum.arduino.cc/index.php?topic=487007.0
|
|
|
+// Good Dispay ePaper for Arduino : https://forum.arduino.cc/index.php?topic=436411.0
|
|
|
+
|
|
|
+// mapping suggestion from Waveshare SPI e-Paper to Wemos D1 mini
|
|
|
+// BUSY -> D2, RST -> D4, DC -> D3, CS -> D8, CLK -> D5, DIN -> D7, GND -> GND, 3.3V -> 3.3V
|
|
|
+
|
|
|
+// mapping suggestion from Waveshare SPI e-Paper to generic ESP8266
|
|
|
+// BUSY -> GPIO4, RST -> GPIO2, DC -> GPIO0, CS -> GPIO15, CLK -> GPIO14, DIN -> GPIO13, GND -> GND, 3.3V -> 3.3V
|
|
|
+
|
|
|
+// mapping of Waveshare e-Paper ESP8266 Driver Board
|
|
|
+// BUSY -> GPIO16, RST -> GPIO5, DC -> GPIO4, CS -> GPIO15, CLK -> GPIO14, DIN -> GPIO13, GND -> GND, 3.3V -> 3.3V
|
|
|
+
|
|
|
+// mapping suggestion for ESP32, e.g. LOLIN32, see .../variants/.../pins_arduino.h for your board
|
|
|
+// NOTE: there are variants with different pins for SPI ! CHECK SPI PINS OF YOUR BOARD
|
|
|
+// BUSY -> 4, RST -> 16, DC -> 17, CS -> SS(5), CLK -> SCK(18), DIN -> MOSI(23), GND -> GND, 3.3V -> 3.3V
|
|
|
+
|
|
|
+// new mapping suggestion for STM32F1, e.g. STM32F103C8T6 "BluePill"
|
|
|
+// BUSY -> A1, RST -> A2, DC -> A3, CS-> A4, CLK -> A5, DIN -> A7
|
|
|
+
|
|
|
+// mapping suggestion for AVR, UNO, NANO etc.
|
|
|
+// BUSY -> 7, RST -> 9, DC -> 8, CS-> 10, CLK -> 13, DIN -> 11
|
|
|
+//
|
|
|
+// **** NOTE that the mapping suggestion may need modification depending on SD board used! ****
|
|
|
+// ********************************************************************************************
|
|
|
+//
|
|
|
+//#define SD_CS SS // e.g. for RobotDyn Wemos D1 mini SD board
|
|
|
+//#define EPD_CS D1 // alternative I use with RobotDyn Wemos D1 mini SD board
|
|
|
+
|
|
|
+#include <GxEPD2_BW.h>
|
|
|
+#include <GxEPD2_3C.h>
|
|
|
+#include <Fonts/FreeMonoBold9pt7b.h>
|
|
|
+
|
|
|
+#include <SPI.h>
|
|
|
+#include <SD.h>
|
|
|
+
|
|
|
+#if defined (ESP8266)
|
|
|
+#define SD_CS SS // e.g. for RobotDyn Wemos D1 mini SD board
|
|
|
+#define EPD_CS D1 // alternative I use with RobotDyn Wemos D1 mini SD board
|
|
|
+// select one and adapt to your mapping, can use full buffer size (full HEIGHT)
|
|
|
+//GxEPD2_BW<GxEPD2_154, GxEPD2_154::HEIGHT> display(GxEPD2_154(/*CS=D8*/ EPD_CS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4));
|
|
|
+//GxEPD2_BW<GxEPD2_213, GxEPD2_213::HEIGHT> display(GxEPD2_213(/*CS=D8*/ EPD_CS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4));
|
|
|
+//GxEPD2_BW<GxEPD2_290, GxEPD2_290::HEIGHT> display(GxEPD2_290(/*CS=D8*/ EPD_CS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4));
|
|
|
+//GxEPD2_BW<GxEPD2_270, GxEPD2_270::HEIGHT> display(GxEPD2_270(/*CS=D8*/ EPD_CS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4));
|
|
|
+//GxEPD2_BW<GxEPD2_420, GxEPD2_420::HEIGHT> display(GxEPD2_420(/*CS=D8*/ EPD_CS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4));
|
|
|
+// can use only half buffer size
|
|
|
+//GxEPD2_BW < GxEPD2_583, GxEPD2_583::HEIGHT / 2 > display(GxEPD2_583(/*CS=D8*/ EPD_CS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4));
|
|
|
+//GxEPD2_BW < GxEPD2_750, GxEPD2_750::HEIGHT / 2 > display(GxEPD2_750(/*CS=D8*/ EPD_CS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4));
|
|
|
+// 3-color e-papers
|
|
|
+//GxEPD2_3C<GxEPD2_154c, GxEPD2_154c::HEIGHT> display(GxEPD2_154c(/*CS=D8*/ EPD_CS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4));
|
|
|
+//GxEPD2_3C<GxEPD2_213c, GxEPD2_213c::HEIGHT> display(GxEPD2_213c(/*CS=D8*/ EPD_CS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4));
|
|
|
+//GxEPD2_3C<GxEPD2_290c, GxEPD2_290c::HEIGHT> display(GxEPD2_290c(/*CS=D8*/ EPD_CS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4));
|
|
|
+//GxEPD2_3C<GxEPD2_270c, GxEPD2_270c::HEIGHT> display(GxEPD2_270c(/*CS=D8*/ EPD_CS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4));
|
|
|
+// can use only half buffer size
|
|
|
+//GxEPD2_3C<GxEPD2_420c, GxEPD2_420c::HEIGHT / 2> display(GxEPD2_420c(/*CS=D8*/ EPD_CS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4));
|
|
|
+// can use only quarter buffer size
|
|
|
+//GxEPD2_3C<GxEPD2_583c, GxEPD2_583c::HEIGHT / 4> display(GxEPD2_583c(/*CS=D8*/ EPD_CS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4));
|
|
|
+//GxEPD2_3C<GxEPD2_750c, GxEPD2_750c::HEIGHT / 4> display(GxEPD2_750c(/*CS=D8*/ EPD_CS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4));
|
|
|
+
|
|
|
+// ***** for mapping of Waveshare e-Paper ESP8266 Driver Board *****
|
|
|
+// select one , can use full buffer size (full HEIGHT)
|
|
|
+//GxEPD2_BW<GxEPD2_154, GxEPD2_154::HEIGHT> display(GxEPD2_154(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=5*/ 5, /*BUSY=16*/ 16));
|
|
|
+//GxEPD2_BW<GxEPD2_213, GxEPD2_213::HEIGHT> display(GxEPD2_213(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=5*/ 5, /*BUSY=16*/ 16));
|
|
|
+//GxEPD2_BW<GxEPD2_290, GxEPD2_290::HEIGHT> display(GxEPD2_290(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=5*/ 5, /*BUSY=16*/ 16));
|
|
|
+//GxEPD2_BW<GxEPD2_270, GxEPD2_270::HEIGHT> display(GxEPD2_270(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=5*/ 5, /*BUSY=16*/ 16));
|
|
|
+//GxEPD2_BW<GxEPD2_420, GxEPD2_420::HEIGHT> display(GxEPD2_420(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=5*/ 5, /*BUSY=16*/ 16));
|
|
|
+// can use only half buffer size
|
|
|
+//GxEPD2_BW < GxEPD2_583, GxEPD2_583::HEIGHT / 2 > display(GxEPD2_583(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=5*/ 5, /*BUSY=16*/ 16));
|
|
|
+//GxEPD2_BW < GxEPD2_750, GxEPD2_750::HEIGHT / 2 > display(GxEPD2_750(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=5*/ 5, /*BUSY=16*/ 16));
|
|
|
+// 3-color e-papers
|
|
|
+//GxEPD2_3C<GxEPD2_154c, GxEPD2_154c::HEIGHT> display(GxEPD2_154c(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=5*/ 5, /*BUSY=16*/ 16));
|
|
|
+//GxEPD2_3C<GxEPD2_213c, GxEPD2_213c::HEIGHT> display(GxEPD2_213c(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=5*/ 5, /*BUSY=16*/ 16));
|
|
|
+//GxEPD2_3C<GxEPD2_290c, GxEPD2_290c::HEIGHT> display(GxEPD2_290c(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=5*/ 5, /*BUSY=16*/ 16));
|
|
|
+//GxEPD2_3C<GxEPD2_270c, GxEPD2_270c::HEIGHT> display(GxEPD2_270c(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=5*/ 5, /*BUSY=16*/ 16));
|
|
|
+// can use only half buffer size
|
|
|
+//GxEPD2_3C<GxEPD2_420c, GxEPD2_420c::HEIGHT / 2> display(GxEPD2_420c(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=5*/ 5, /*BUSY=16*/ 16));
|
|
|
+// can use only quarter buffer size
|
|
|
+//GxEPD2_3C<GxEPD2_583c, GxEPD2_583c::HEIGHT / 4> display(GxEPD2_583c(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=5*/ 5, /*BUSY=16*/ 16));
|
|
|
+//GxEPD2_3C<GxEPD2_750c, GxEPD2_750c::HEIGHT / 4> display(GxEPD2_750c(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=5*/ 5, /*BUSY=16*/ 16));
|
|
|
+#endif
|
|
|
+
|
|
|
+#if defined(ESP32)
|
|
|
+// select one and adapt to your mapping, can use full buffer size (full HEIGHT)
|
|
|
+//GxEPD2_BW<GxEPD2_154, GxEPD2_154::HEIGHT> display(GxEPD2_154(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4));
|
|
|
+//GxEPD2_BW<GxEPD2_213, GxEPD2_213::HEIGHT> display(GxEPD2_213(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4));
|
|
|
+//GxEPD2_BW<GxEPD2_290, GxEPD2_290::HEIGHT> display(GxEPD2_290(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4));
|
|
|
+//GxEPD2_BW<GxEPD2_270, GxEPD2_270::HEIGHT> display(GxEPD2_270(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4));
|
|
|
+//GxEPD2_BW<GxEPD2_420, GxEPD2_420::HEIGHT> display(GxEPD2_420(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4));
|
|
|
+//GxEPD2_BW<GxEPD2_750, GxEPD2_750::HEIGHT> display(GxEPD2_750(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4));
|
|
|
+// 3-color e-papers
|
|
|
+//GxEPD2_3C<GxEPD2_154c, GxEPD2_154c::HEIGHT> display(GxEPD2_154c(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4));
|
|
|
+//GxEPD2_3C<GxEPD2_213c, GxEPD2_213c::HEIGHT> display(GxEPD2_213c(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4));
|
|
|
+//GxEPD2_3C<GxEPD2_290c, GxEPD2_290c::HEIGHT> display(GxEPD2_290c(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4));
|
|
|
+//GxEPD2_3C<GxEPD2_270c, GxEPD2_270c::HEIGHT> display(GxEPD2_270c(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4));
|
|
|
+//GxEPD2_3C<GxEPD2_420c, GxEPD2_420c::HEIGHT> display(GxEPD2_420c(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4));
|
|
|
+//GxEPD2_3C<GxEPD2_750c, GxEPD2_750c::HEIGHT> display(GxEPD2_750c(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4));
|
|
|
+#endif
|
|
|
+
|
|
|
+#if defined(_BOARD_GENERIC_STM32F103C_H_)
|
|
|
+#define MAX_DISPAY_BUFFER_SIZE (400 * 300) // ~5k is a good compromise
|
|
|
+#define MAX_HEIGHT(EPD) (EPD::HEIGHT <= MAX_DISPAY_BUFFER_SIZE / 8 / EPD::WIDTH ? EPD::HEIGHT : MAX_DISPAY_BUFFER_SIZE / 8 / EPD::WIDTH)
|
|
|
+// select one and adapt to your mapping
|
|
|
+//GxEPD2_BW<GxEPD2_154, MAX_HEIGHT(GxEPD2_154)> display(GxEPD2_154(/*CS=4*/ SS, /*DC=*/ 3, /*RST=*/ 2, /*BUSY=*/ 1));
|
|
|
+//GxEPD2_BW<GxEPD2_213, MAX_HEIGHT(GxEPD2_213)> display(GxEPD2_213(/*CS=4*/ SS, /*DC=*/ 3, /*RST=*/ 2, /*BUSY=*/ 1));
|
|
|
+//GxEPD2_BW<GxEPD2_290, MAX_HEIGHT(GxEPD2_290)> display(GxEPD2_290(/*CS=4*/ SS, /*DC=*/ 3, /*RST=*/ 2, /*BUSY=*/ 1));
|
|
|
+//GxEPD2_BW<GxEPD2_270, MAX_HEIGHT(GxEPD2_270)> display(GxEPD2_270(/*CS=4*/ SS, /*DC=*/ 3, /*RST=*/ 2, /*BUSY=*/ 1));
|
|
|
+//GxEPD2_BW<GxEPD2_420, MAX_HEIGHT(GxEPD2_420)> display(GxEPD2_420(/*CS=4*/ SS, /*DC=*/ 3, /*RST=*/ 2, /*BUSY=*/ 1));
|
|
|
+//GxEPD2_BW<GxEPD2_750, MAX_HEIGHT(GxEPD2_750)> display(GxEPD2_750(/*CS=4*/ SS, /*DC=*/ 3, /*RST=*/ 2, /*BUSY=*/ 1));
|
|
|
+// 3-color e-papers
|
|
|
+//GxEPD2_3C<GxEPD2_154c, MAX_HEIGHT(GxEPD2_154c)> display(GxEPD2_154c(/*CS=4*/ SS, /*DC=*/ 3, /*RST=*/ 2, /*BUSY=*/ 1));
|
|
|
+//GxEPD2_3C<GxEPD2_213c, MAX_HEIGHT(GxEPD2_213c)> display(GxEPD2_213c(/*CS=4*/ SS, /*DC=*/ 3, /*RST=*/ 2, /*BUSY=*/ 1));
|
|
|
+//GxEPD2_3C<GxEPD2_290c, MAX_HEIGHT(GxEPD2_290c)> display(GxEPD2_290c(/*CS=4*/ SS, /*DC=*/ 3, /*RST=*/ 2, /*BUSY=*/ 1));
|
|
|
+//GxEPD2_3C<GxEPD2_270c, MAX_HEIGHT(GxEPD2_270c)> display(GxEPD2_270c(/*CS=4*/ SS, /*DC=*/ 3, /*RST=*/ 2, /*BUSY=*/ 1));
|
|
|
+//GxEPD2_3C<GxEPD2_420c, MAX_HEIGHT(GxEPD2_420c)> display(GxEPD2_420c(/*CS=4*/ SS, /*DC=*/ 3, /*RST=*/ 2, /*BUSY=*/ 1));
|
|
|
+//GxEPD2_3C<GxEPD2_750c, MAX_HEIGHT(GxEPD2_750c)> display(GxEPD2_750c(/*CS=4*/ SS, /*DC=*/ 3, /*RST=*/ 2, /*BUSY=*/ 1));
|
|
|
+#endif
|
|
|
+
|
|
|
+#if defined(__AVR)
|
|
|
+#define MAX_DISPAY_BUFFER_SIZE 1000 // half of available RAM as a compromise
|
|
|
+#define MAX_HEIGHT(EPD) (EPD::HEIGHT <= MAX_DISPAY_BUFFER_SIZE / 8 / EPD::WIDTH ? EPD::HEIGHT : MAX_DISPAY_BUFFER_SIZE / 8 / EPD::WIDTH)
|
|
|
+// select one and adapt to your mapping
|
|
|
+//GxEPD2_BW<GxEPD2_154, MAX_HEIGHT(GxEPD2_154)> display(GxEPD2_154(/*CS=10*/ SS, /*DC=*/ 8, /*RST=*/ 9, /*BUSY=*/ 7));
|
|
|
+//GxEPD2_BW<GxEPD2_213, MAX_HEIGHT(GxEPD2_213)> display(GxEPD2_213(/*CS=10*/ SS, /*DC=*/ 8, /*RST=*/ 9, /*BUSY=*/ 7));
|
|
|
+//GxEPD2_BW<GxEPD2_290, MAX_HEIGHT(GxEPD2_290)> display(GxEPD2_290(/*CS=10*/ SS, /*DC=*/ 8, /*RST=*/ 9, /*BUSY=*/ 7));
|
|
|
+//GxEPD2_BW<GxEPD2_270, MAX_HEIGHT(GxEPD2_270)> display(GxEPD2_270(/*CS=10*/ SS, /*DC=*/ 8, /*RST=*/ 9, /*BUSY=*/ 7));
|
|
|
+//GxEPD2_BW<GxEPD2_420, MAX_HEIGHT(GxEPD2_420)> display(GxEPD2_420(/*CS=10*/ SS, /*DC=*/ 8, /*RST=*/ 9, /*BUSY=*/ 7));
|
|
|
+//GxEPD2_BW<GxEPD2_750, MAX_HEIGHT(GxEPD2_750)> display(GxEPD2_750(/*CS=10*/ SS, /*DC=*/ 8, /*RST=*/ 9, /*BUSY=*/ 7));
|
|
|
+// 3-color e-papers
|
|
|
+//GxEPD2_3C<GxEPD2_154c, MAX_HEIGHT(GxEPD2_154c)> display(GxEPD2_154c(/*CS=10*/ SS, /*DC=*/ 8, /*RST=*/ 9, /*BUSY=*/ 7));
|
|
|
+//GxEPD2_3C<GxEPD2_213c, MAX_HEIGHT(GxEPD2_213c)> display(GxEPD2_213c(/*CS=10*/ SS, /*DC=*/ 8, /*RST=*/ 9, /*BUSY=*/ 7));
|
|
|
+//GxEPD2_3C<GxEPD2_290c, MAX_HEIGHT(GxEPD2_290c)> display(GxEPD2_290c(/*CS=10*/ SS, /*DC=*/ 8, /*RST=*/ 9, /*BUSY=*/ 7));
|
|
|
+//GxEPD2_3C<GxEPD2_270c, MAX_HEIGHT(GxEPD2_270c)> display(GxEPD2_270c(/*CS=10*/ SS, /*DC=*/ 8, /*RST=*/ 9, /*BUSY=*/ 7));
|
|
|
+//GxEPD2_3C<GxEPD2_420c, MAX_HEIGHT(GxEPD2_420c)> display(GxEPD2_420c(/*CS=10*/ SS, /*DC=*/ 8, /*RST=*/ 9, /*BUSY=*/ 7));
|
|
|
+//GxEPD2_3C<GxEPD2_750c, MAX_HEIGHT(GxEPD2_750c)> display(GxEPD2_750c(/*CS=10*/ SS, /*DC=*/ 8, /*RST=*/ 9, /*BUSY=*/ 7));
|
|
|
+#endif
|
|
|
+
|
|
|
+
|
|
|
+void setup()
|
|
|
+{
|
|
|
+ Serial.begin(115200);
|
|
|
+ Serial.println();
|
|
|
+ Serial.println("setup");
|
|
|
+
|
|
|
+ display.init();
|
|
|
+
|
|
|
+ Serial.print("Initializing SD card...");
|
|
|
+ if (!SD.begin(SD_CS))
|
|
|
+ {
|
|
|
+ Serial.println("SD failed!");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ Serial.println("SD OK!");
|
|
|
+
|
|
|
+ drawBitmapFromSD("parrot.bmp", 0, 0);
|
|
|
+ delay(5000);
|
|
|
+ drawBitmapFromSD("betty_1.bmp", 0, 0);
|
|
|
+ delay(5000);
|
|
|
+}
|
|
|
+
|
|
|
+void loop(void)
|
|
|
+{
|
|
|
+}
|
|
|
+
|
|
|
+#define SD_BUFFER_PIXELS 20
|
|
|
+
|
|
|
+void drawBitmapFromSD(const char *filename, uint8_t x, uint8_t y)
|
|
|
+{
|
|
|
+ File file;
|
|
|
+ uint8_t buffer[3 * SD_BUFFER_PIXELS]; // pixel buffer, size for r,g,b
|
|
|
+ bool valid = false; // valid format to be handled
|
|
|
+ bool flip = true; // bitmap is stored bottom-to-top
|
|
|
+ uint32_t pos = 0;
|
|
|
+ uint32_t startTime = millis();
|
|
|
+ if ((x >= display.width()) || (y >= display.height())) return;
|
|
|
+ Serial.println();
|
|
|
+ Serial.print("Loading image '");
|
|
|
+ Serial.print(filename);
|
|
|
+ Serial.println('\'');
|
|
|
+ file = SD.open(filename);
|
|
|
+ if (!file)
|
|
|
+ {
|
|
|
+ Serial.print("File not found");
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ // Parse BMP header
|
|
|
+ if (read16(file) == 0x4D42) // BMP signature
|
|
|
+ {
|
|
|
+ uint32_t fileSize = read32(file);
|
|
|
+ uint32_t creatorBytes = read32(file);
|
|
|
+ uint32_t imageOffset = read32(file); // Start of image data
|
|
|
+ uint32_t headerSize = read32(file);
|
|
|
+ uint32_t width = read32(file);
|
|
|
+ uint32_t height = read32(file);
|
|
|
+ uint16_t planes = read16(file);
|
|
|
+ uint16_t depth = read16(file); // bits per pixel
|
|
|
+ uint32_t format = read32(file);
|
|
|
+ if ((planes == 1) && (format == 0)) // uncompressed is handled
|
|
|
+ {
|
|
|
+ Serial.print("File size: "); Serial.println(fileSize);
|
|
|
+ Serial.print("Image Offset: "); Serial.println(imageOffset);
|
|
|
+ Serial.print("Header size: "); Serial.println(headerSize);
|
|
|
+ Serial.print("Bit Depth: "); Serial.println(depth);
|
|
|
+ Serial.print("Image size: ");
|
|
|
+ Serial.print(width);
|
|
|
+ Serial.print('x');
|
|
|
+ Serial.println(height);
|
|
|
+ // BMP rows are padded (if needed) to 4-byte boundary
|
|
|
+ uint32_t rowSize = (width * depth / 8 + 3) & ~3;
|
|
|
+ if (height < 0)
|
|
|
+ {
|
|
|
+ height = -height;
|
|
|
+ flip = false;
|
|
|
+ }
|
|
|
+ uint16_t w = width;
|
|
|
+ uint16_t h = height;
|
|
|
+ if ((x + w - 1) >= display.width()) w = display.width() - x;
|
|
|
+ if ((y + h - 1) >= display.height()) h = display.height() - y;
|
|
|
+ if ((depth == 1) && (rowSize < sizeof(buffer))) // handle with direct drawing
|
|
|
+ {
|
|
|
+ valid = true;
|
|
|
+ display.writeScreenBuffer();
|
|
|
+ if (flip) file.seek(imageOffset + (height - h) * rowSize);
|
|
|
+ else file.seek(imageOffset);
|
|
|
+ for (uint16_t row = 0; row < h; row++) // for each line
|
|
|
+ {
|
|
|
+ file.read(buffer, rowSize);
|
|
|
+ uint16_t y = flip ? h - row - 1 : row;
|
|
|
+ display.writeImage(buffer, 0, y, w, 1);
|
|
|
+ }
|
|
|
+ display.refresh();
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ display.firstPage();
|
|
|
+ do
|
|
|
+ {
|
|
|
+ size_t buffidx = sizeof(buffer); // force buffer load
|
|
|
+ for (uint16_t row = 0; row < h; row++) // for each line
|
|
|
+ {
|
|
|
+ if (flip) // Bitmap is stored bottom-to-top order (normal BMP)
|
|
|
+ pos = imageOffset + (height - 1 - row) * rowSize;
|
|
|
+ else // Bitmap is stored top-to-bottom
|
|
|
+ pos = imageOffset + row * rowSize;
|
|
|
+ if (file.position() != pos)
|
|
|
+ { // Need seek?
|
|
|
+ file.seek(pos);
|
|
|
+ buffidx = sizeof(buffer); // force buffer reload
|
|
|
+ }
|
|
|
+ uint8_t bits;
|
|
|
+ for (uint16_t col = 0; col < w; col++) // for each pixel
|
|
|
+ {
|
|
|
+ // Time to read more pixel data?
|
|
|
+ if (buffidx >= sizeof(buffer))
|
|
|
+ {
|
|
|
+ file.read(buffer, sizeof(buffer));
|
|
|
+ buffidx = 0; // Set index to beginning
|
|
|
+ }
|
|
|
+ switch (depth)
|
|
|
+ {
|
|
|
+ case 1: // one bit per pixel b/w format
|
|
|
+ {
|
|
|
+ valid = true;
|
|
|
+ if (0 == col % 8)
|
|
|
+ {
|
|
|
+ bits = buffer[buffidx++];
|
|
|
+ }
|
|
|
+ uint16_t bw_color = bits & 0x80 ? GxEPD_WHITE : GxEPD_BLACK;
|
|
|
+ display.drawPixel(col, row, bw_color);
|
|
|
+ bits <<= 1;
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case 24: // standard BMP format
|
|
|
+ {
|
|
|
+ valid = true;
|
|
|
+ uint16_t b = buffer[buffidx++];
|
|
|
+ uint16_t g = buffer[buffidx++];
|
|
|
+ uint16_t r = buffer[buffidx++];
|
|
|
+ uint16_t bw_color = ((r + g + b) / 3 > 0xFF / 2) ? GxEPD_WHITE : GxEPD_BLACK;
|
|
|
+ display.drawPixel(col, row, bw_color);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ } // end pixel
|
|
|
+ } // end line
|
|
|
+ } // end do
|
|
|
+ while (display.nextPage());
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ file.close();
|
|
|
+ if (valid)
|
|
|
+ {
|
|
|
+ Serial.println("bitmap format not handled.");
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ Serial.print("loaded in ");
|
|
|
+ Serial.print(millis() - startTime);
|
|
|
+ Serial.println(" ms");
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+uint16_t read16(File f)
|
|
|
+{
|
|
|
+ // BMP data is stored little-endian, same as Arduino.
|
|
|
+ uint16_t result;
|
|
|
+ ((uint8_t *)&result)[0] = f.read(); // LSB
|
|
|
+ ((uint8_t *)&result)[1] = f.read(); // MSB
|
|
|
+ return result;
|
|
|
+}
|
|
|
+
|
|
|
+uint32_t read32(File f)
|
|
|
+{
|
|
|
+ // BMP data is stored little-endian, same as Arduino.
|
|
|
+ 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;
|
|
|
+}
|
|
|
+
|