| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302 |
- // Display Library example for SPI e-paper panels from Dalian Good Display and boards from Waveshare.
- // Requires HW SPI and Adafruit_GFX. Caution: the e-paper panels require 3.3V supply AND data lines!
- //
- // Display Library based on Demo Example from Good Display: https://www.good-display.com/companyfile/32/
- //
- // Author: Jean-Marc Zingg
- //
- // Version: see library.properties
- //
- // Library: https://github.com/ZinggJM/GxEPD2
- //
- // Purpose: show uses of GxEPD2_GFX base class for references to a display instance
- //
- // TextDisplay shows the use of the display instance reference as a function parameter
- // note for partial update window and setPartialWindow() method:
- // partial update window size and position is on byte boundary in physical x direction
- // the size is increased in setPartialWindow() if x or w are not multiple of 8 for even rotation, y or h for odd rotation
- // see also comment in GxEPD2_BW.h, GxEPD2_3C.h or GxEPD2_GFX.h for method setPartialWindow()
- #include "TextDisplay.h"
- #include <Fonts/FreeMonoBold9pt7b.h>
- const char HelloWorld[] = "Hello World!";
- const char HelloArduino[] = "Hello Arduino!";
- const char HelloEpaper[] = "Hello E-Paper!";
- void helloWorld(GxEPD2_GFX& display)
- {
- //Serial.println("helloWorld");
- display.setRotation(1);
- display.setFont(&FreeMonoBold9pt7b);
- display.setTextColor(GxEPD_BLACK);
- int16_t tbx, tby; uint16_t tbw, tbh;
- display.getTextBounds(HelloWorld, 0, 0, &tbx, &tby, &tbw, &tbh);
- // center bounding box by transposition of origin:
- uint16_t x = ((display.width() - tbw) / 2) - tbx;
- uint16_t y = ((display.height() - tbh) / 2) - tby;
- display.setFullWindow();
- display.firstPage();
- do
- {
- display.fillScreen(GxEPD_WHITE);
- display.setCursor(x, y);
- display.print(HelloWorld);
- }
- while (display.nextPage());
- //Serial.println("helloWorld done");
- }
- void helloWorldForDummies(GxEPD2_GFX& display)
- {
- //Serial.println("helloWorld");
- const char text[] = "Hello World!";
- // most e-papers have width < height (portrait) as native orientation, especially the small ones
- // in GxEPD2 rotation 0 is used for native orientation (most TFT libraries use 0 fix for portrait orientation)
- // set rotation to 1 (rotate right 90 degrees) to have enough space on small displays (landscape)
- display.setRotation(1);
- // select a suitable font in Adafruit_GFX
- display.setFont(&FreeMonoBold9pt7b);
- // on e-papers black on white is more pleasant to read
- display.setTextColor(GxEPD_BLACK);
- // Adafruit_GFX has a handy method getTextBounds() to determine the boundary box for a text for the actual font
- int16_t tbx, tby; uint16_t tbw, tbh; // boundary box window
- display.getTextBounds(text, 0, 0, &tbx, &tby, &tbw, &tbh); // it works for origin 0, 0, fortunately (negative tby!)
- // center bounding box by transposition of origin:
- uint16_t x = ((display.width() - tbw) / 2) - tbx;
- uint16_t y = ((display.height() - tbh) / 2) - tby;
- // full window mode is the initial mode, set it anyway
- display.setFullWindow();
- // here we use paged drawing, even if the processor has enough RAM for full buffer
- // so this can be used with any supported processor board.
- // the cost in code overhead and execution time penalty is marginal
- // tell the graphics class to use paged drawing mode
- display.firstPage();
- do
- {
- // this part of code is executed multiple times, as many as needed,
- // in case of full buffer it is executed once
- // IMPORTANT: each iteration needs to draw the same, to avoid strange effects
- // use a copy of values that might change, don't read e.g. from analog or pins in the loop!
- display.fillScreen(GxEPD_WHITE); // set the background to white (fill the buffer with value for white)
- display.setCursor(x, y); // set the postition to start printing text
- display.print(text); // print some text
- // end of part executed multiple times
- }
- // tell the graphics class to transfer the buffer content (page) to the controller buffer
- // the graphics class will command the controller to refresh to the screen when the last page has been transferred
- // returns true if more pages need be drawn and transferred
- // returns false if the last page has been transferred and the screen refreshed for panels without fast partial update
- // returns false for panels with fast partial update when the controller buffer has been written once more, to make the differential buffers equal
- // (for full buffered with fast partial update the (full) buffer is just transferred again, and false returned)
- while (display.nextPage());
- //Serial.println("helloWorld done");
- }
- void helloFullScreenPartialMode(GxEPD2_GFX& display)
- {
- //Serial.println("helloFullScreenPartialMode");
- const char fullscreen[] = "full screen update";
- const char fpm[] = "fast partial mode";
- const char spm[] = "slow partial mode";
- const char npm[] = "no partial mode";
- display.setPartialWindow(0, 0, display.width(), display.height());
- display.setRotation(1);
- display.setFont(&FreeMonoBold9pt7b);
- display.setTextColor(GxEPD_BLACK);
- const char* updatemode;
- if (display.epd2.hasFastPartialUpdate)
- {
- updatemode = fpm;
- }
- else if (display.epd2.hasPartialUpdate)
- {
- updatemode = spm;
- }
- else
- {
- updatemode = npm;
- }
- // do this outside of the loop
- int16_t tbx, tby; uint16_t tbw, tbh;
- // center update text
- display.getTextBounds(fullscreen, 0, 0, &tbx, &tby, &tbw, &tbh);
- uint16_t utx = ((display.width() - tbw) / 2) - tbx;
- uint16_t uty = ((display.height() / 4) - tbh / 2) - tby;
- // center update mode
- display.getTextBounds(updatemode, 0, 0, &tbx, &tby, &tbw, &tbh);
- uint16_t umx = ((display.width() - tbw) / 2) - tbx;
- uint16_t umy = ((display.height() * 3 / 4) - tbh / 2) - tby;
- // center HelloWorld
- display.getTextBounds(HelloWorld, 0, 0, &tbx, &tby, &tbw, &tbh);
- uint16_t hwx = ((display.width() - tbw) / 2) - tbx;
- uint16_t hwy = ((display.height() - tbh) / 2) - tby;
- display.firstPage();
- do
- {
- display.fillScreen(GxEPD_WHITE);
- display.setCursor(hwx, hwy);
- display.print(HelloWorld);
- display.setCursor(utx, uty);
- display.print(fullscreen);
- display.setCursor(umx, umy);
- display.print(updatemode);
- }
- while (display.nextPage());
- //Serial.println("helloFullScreenPartialMode done");
- }
- void helloArduino(GxEPD2_GFX& display)
- {
- //Serial.println("helloArduino");
- display.setRotation(1);
- display.setFont(&FreeMonoBold9pt7b);
- display.setTextColor(display.epd2.hasColor ? GxEPD_RED : GxEPD_BLACK);
- int16_t tbx, tby; uint16_t tbw, tbh;
- // align with centered HelloWorld
- display.getTextBounds(HelloWorld, 0, 0, &tbx, &tby, &tbw, &tbh);
- uint16_t x = ((display.width() - tbw) / 2) - tbx;
- // height might be different
- display.getTextBounds(HelloArduino, 0, 0, &tbx, &tby, &tbw, &tbh);
- uint16_t y = ((display.height() / 4) - tbh / 2) - tby; // y is base line!
- // make the window big enough to cover (overwrite) descenders of previous text
- uint16_t wh = FreeMonoBold9pt7b.yAdvance;
- uint16_t wy = (display.height() / 4) - wh / 2;
- display.setPartialWindow(0, wy, display.width(), wh);
- display.firstPage();
- do
- {
- display.fillScreen(GxEPD_WHITE);
- //display.drawRect(x, y - tbh, tbw, tbh, GxEPD_BLACK);
- display.setCursor(x, y);
- display.print(HelloArduino);
- }
- while (display.nextPage());
- delay(1000);
- //Serial.println("helloArduino done");
- }
- void helloEpaper(GxEPD2_GFX& display)
- {
- //Serial.println("helloEpaper");
- display.setRotation(1);
- display.setFont(&FreeMonoBold9pt7b);
- display.setTextColor(display.epd2.hasColor ? GxEPD_RED : GxEPD_BLACK);
- int16_t tbx, tby; uint16_t tbw, tbh;
- // align with centered HelloWorld
- display.getTextBounds(HelloWorld, 0, 0, &tbx, &tby, &tbw, &tbh);
- uint16_t x = ((display.width() - tbw) / 2) - tbx;
- // height might be different
- display.getTextBounds(HelloEpaper, 0, 0, &tbx, &tby, &tbw, &tbh);
- uint16_t y = (display.height() * 3 / 4) + tbh / 2; // y is base line!
- // make the window big enough to cover (overwrite) descenders of previous text
- uint16_t wh = FreeMonoBold9pt7b.yAdvance;
- uint16_t wy = (display.height() * 3 / 4) - wh / 2;
- display.setPartialWindow(0, wy, display.width(), wh);
- display.firstPage();
- do
- {
- display.fillScreen(GxEPD_WHITE);
- display.setCursor(x, y);
- display.print(HelloEpaper);
- }
- while (display.nextPage());
- //Serial.println("helloEpaper done");
- }
- #if defined(ESP8266) || defined(ESP32)
- #include <StreamString.h>
- #define PrintString StreamString
- #else
- class PrintString : public Print, public String
- {
- public:
- size_t write(uint8_t data) override
- {
- return concat(char(data));
- };
- };
- #endif
- void helloValue(GxEPD2_GFX& display, double v, int digits)
- {
- //Serial.println("helloValue");
- display.setRotation(1);
- display.setFont(&FreeMonoBold9pt7b);
- display.setTextColor(display.epd2.hasColor ? GxEPD_RED : GxEPD_BLACK);
- PrintString valueString;
- valueString.print(v, digits);
- int16_t tbx, tby; uint16_t tbw, tbh;
- display.getTextBounds(valueString, 0, 0, &tbx, &tby, &tbw, &tbh);
- uint16_t x = ((display.width() - tbw) / 2) - tbx;
- uint16_t y = (display.height() * 3 / 4) + tbh / 2; // y is base line!
- // show what happens, if we use the bounding box for partial window
- uint16_t wx = (display.width() - tbw) / 2;
- uint16_t wy = (display.height() * 3 / 4) - tbh / 2;
- display.setPartialWindow(wx, wy, tbw, tbh);
- display.firstPage();
- do
- {
- display.fillScreen(GxEPD_WHITE);
- display.setCursor(x, y);
- display.print(valueString);
- }
- while (display.nextPage());
- delay(2000);
- // make the partial window big enough to cover the previous text
- uint16_t ww = tbw; // remember window width
- display.getTextBounds(HelloEpaper, 0, 0, &tbx, &tby, &tbw, &tbh);
- // adjust, because HelloEpaper was aligned, not centered (could calculate this to be precise)
- ww = max(ww, uint16_t(tbw + 12)); // 12 seems ok
- wx = (display.width() - tbw) / 2;
- // make the window big enough to cover (overwrite) descenders of previous text
- uint16_t wh = FreeMonoBold9pt7b.yAdvance;
- wy = (display.height() * 3 / 4) - wh / 2;
- display.setPartialWindow(wx, wy, ww, wh);
- // alternately use the whole width for partial window
- //display.setPartialWindow(0, wy, display.width(), wh);
- display.firstPage();
- do
- {
- display.fillScreen(GxEPD_WHITE);
- display.setCursor(x, y);
- display.print(valueString);
- }
- while (display.nextPage());
- //Serial.println("helloValue done");
- }
- void showFont(GxEPD2_GFX& display, const char name[], const GFXfont* f)
- {
- display.setFullWindow();
- display.setRotation(0);
- display.setTextColor(GxEPD_BLACK);
- display.firstPage();
- do
- {
- drawFont(display, name, f);
- }
- while (display.nextPage());
- }
- void drawFont(GxEPD2_GFX& display, const char name[], const GFXfont* f)
- {
- //display.setRotation(0);
- display.fillScreen(GxEPD_WHITE);
- display.setTextColor(GxEPD_BLACK);
- display.setFont(f);
- display.setCursor(0, 0);
- display.println();
- display.println(name);
- display.println(" !\"#$%&'()*+,-./");
- display.println("0123456789:;<=>?");
- display.println("@ABCDEFGHIJKLMNO");
- display.println("PQRSTUVWXYZ[\\]^_");
- if (display.epd2.hasColor)
- {
- display.setTextColor(GxEPD_RED);
- }
- display.println("`abcdefghijklmno");
- display.println("pqrstuvwxyz{|}~ ");
- }
|