GxEPD2_U8G2_Fonts_Example.ino 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. // Display Library example for SPI e-paper panels from Dalian Good Display and boards from Waveshare.
  2. // Requires HW SPI and Adafruit_GFX. Caution: the e-paper panels require 3.3V supply AND data lines!
  3. //
  4. // Display Library based on Demo Example from Good Display: https://www.good-display.com/companyfile/32/
  5. //
  6. // Author: Jean-Marc Zingg
  7. //
  8. // Version: see library.properties
  9. //
  10. // Library: https://github.com/ZinggJM/GxEPD2
  11. // GxEPD2_U8G2_Fonts_Example : show use of U8G2_FOR_ADAFRUIT_GFX with GxEPD2
  12. //
  13. // Library: https://github.com/olikraus/U8g2_for_Adafruit_GFX
  14. // see: https://github.com/olikraus/U8g2_for_Adafruit_GFX/blob/master/README.md
  15. //
  16. // NOTE: you need to SAVE the modified example to a saveable location for UTF-8 characters to work
  17. // e.g. for Umlauts
  18. // Supporting Arduino Forum Topics (closed, read only):
  19. // Good Display ePaper for Arduino: https://forum.arduino.cc/t/good-display-epaper-for-arduino/419657
  20. // Waveshare e-paper displays with SPI: https://forum.arduino.cc/t/waveshare-e-paper-displays-with-spi/467865
  21. //
  22. // Add new topics in https://forum.arduino.cc/c/using-arduino/displays/23 for new questions and issues
  23. // see GxEPD2_wiring_examples.h for wiring suggestions and examples
  24. // base class GxEPD2_GFX can be used to pass references or pointers to the display instance as parameter, uses ~1.2k more code
  25. // enable or disable GxEPD2_GFX base class
  26. #define ENABLE_GxEPD2_GFX 0
  27. #include <GxEPD2_BW.h>
  28. #include <GxEPD2_3C.h>
  29. #include <GxEPD2_4C.h>
  30. #include <GxEPD2_7C.h>
  31. #include <Fonts/FreeMonoBold9pt7b.h>
  32. #include <U8g2_for_Adafruit_GFX.h>
  33. // select the display class and display driver class in the following file (new style):
  34. #include "GxEPD2_display_selection_new_style.h"
  35. U8G2_FOR_ADAFRUIT_GFX u8g2Fonts;
  36. // for handling alternative SPI pins (ESP32, RP2040) see example GxEPD2_Example.ino
  37. void setup()
  38. {
  39. Serial.begin(115200);
  40. Serial.println();
  41. Serial.println("setup");
  42. //display.init(115200); // default 10ms reset pulse, e.g. for bare panels with DESPI-C02
  43. display.init(115200, true, 2, false); // USE THIS for Waveshare boards with "clever" reset circuit, 2ms reset pulse
  44. u8g2Fonts.begin(display); // connect u8g2 procedures to Adafruit GFX
  45. helloWorld();
  46. delay(1000);
  47. helloArduino();
  48. delay(1000);
  49. helloEpaper();
  50. delay(1000);
  51. showFont("u8g2_font_helvR14_tf", u8g2_font_helvR14_tf); // select u8g2 font from here: https://github.com/olikraus/u8g2/wiki/fntlistall
  52. delay(2000);
  53. showFont("u8g2_font_profont22_mr", u8g2_font_profont22_mr); // select u8g2 font from here: https://github.com/olikraus/u8g2/wiki/fntlistall
  54. delay(1000);
  55. Serial.println("setup done");
  56. }
  57. void loop()
  58. {
  59. }
  60. const char HelloWorld[] = "Hello World!";
  61. const char HelloArduino[] = "Hello Arduino!";
  62. const char HelloEpaper[] = "Hello E-Paper!";
  63. void helloWorld()
  64. {
  65. //Serial.println("helloWorld");
  66. uint16_t bg = GxEPD_WHITE;
  67. uint16_t fg = GxEPD_BLACK;
  68. u8g2Fonts.setFontMode(1); // use u8g2 transparent mode (this is default)
  69. u8g2Fonts.setFontDirection(0); // left to right (this is default)
  70. u8g2Fonts.setForegroundColor(fg); // apply Adafruit GFX color
  71. u8g2Fonts.setBackgroundColor(bg); // apply Adafruit GFX color
  72. u8g2Fonts.setFont(u8g2_font_helvR14_tf); // select u8g2 font from here: https://github.com/olikraus/u8g2/wiki/fntlistall
  73. int16_t tw = u8g2Fonts.getUTF8Width(HelloWorld); // text box width
  74. int16_t ta = u8g2Fonts.getFontAscent(); // positive
  75. int16_t td = u8g2Fonts.getFontDescent(); // negative; in mathematicians view
  76. int16_t th = ta - td; // text box height
  77. //Serial.print("ascent, descent ("); Serial.print(u8g2Fonts.getFontAscent()); Serial.print(", "); Serial.print(u8g2Fonts.getFontDescent()); Serial.println(")");
  78. // center bounding box by transposition of origin:
  79. // y is base line for u8g2Fonts, like for Adafruit_GFX True Type fonts
  80. uint16_t x = (display.width() - tw) / 2;
  81. uint16_t y = (display.height() - th) / 2 + ta;
  82. //Serial.print("bounding box ("); Serial.print(x); Serial.print(", "); Serial.print(y); Serial.print(", "); Serial.print(tw); Serial.print(", "); Serial.print(th); Serial.println(")");
  83. display.firstPage();
  84. do
  85. {
  86. display.fillScreen(bg);
  87. u8g2Fonts.setCursor(x, y); // start writing at this position
  88. u8g2Fonts.print(HelloWorld);
  89. }
  90. while (display.nextPage());
  91. //Serial.println("helloWorld done");
  92. }
  93. void helloArduino()
  94. {
  95. //Serial.println("helloArduino");
  96. uint16_t bg = GxEPD_WHITE;
  97. uint16_t fg = GxEPD_BLACK;
  98. u8g2Fonts.setFontMode(1); // use u8g2 transparent mode (this is default)
  99. u8g2Fonts.setFontDirection(0); // left to right (this is default)
  100. u8g2Fonts.setForegroundColor(fg); // apply Adafruit GFX color
  101. u8g2Fonts.setBackgroundColor(bg); // apply Adafruit GFX color
  102. u8g2Fonts.setFont(u8g2_font_helvR14_tf); // select u8g2 font from here: https://github.com/olikraus/u8g2/wiki/fntlistall
  103. int16_t tw = u8g2Fonts.getUTF8Width(HelloArduino); // text box width
  104. int16_t ta = u8g2Fonts.getFontAscent(); // positive
  105. int16_t td = u8g2Fonts.getFontDescent(); // negative; in mathematicians view
  106. int16_t th = ta - td; // text box height
  107. uint16_t x = (display.width() - tw) / 2;
  108. uint16_t y = display.height() / 4 - th / 2 + ta;
  109. display.setPartialWindow(0, y - 14, display.width(), 20);
  110. display.firstPage();
  111. do
  112. {
  113. display.fillScreen(bg);
  114. u8g2Fonts.setCursor(x, y);
  115. u8g2Fonts.print(HelloArduino);
  116. }
  117. while (display.nextPage());
  118. //Serial.println("helloArduino done");
  119. }
  120. void helloEpaper()
  121. {
  122. //Serial.println("helloEpaper");
  123. uint16_t bg = GxEPD_WHITE;
  124. uint16_t fg = GxEPD_BLACK;
  125. u8g2Fonts.setFontMode(1); // use u8g2 transparent mode (this is default)
  126. u8g2Fonts.setFontDirection(0); // left to right (this is default)
  127. u8g2Fonts.setForegroundColor(fg); // apply Adafruit GFX color
  128. u8g2Fonts.setBackgroundColor(bg); // apply Adafruit GFX color
  129. u8g2Fonts.setFont(u8g2_font_helvR14_tf); // select u8g2 font from here: https://github.com/olikraus/u8g2/wiki/fntlistall
  130. int16_t tw = u8g2Fonts.getUTF8Width(HelloArduino); // text box width
  131. int16_t ta = u8g2Fonts.getFontAscent(); // positive
  132. int16_t td = u8g2Fonts.getFontDescent(); // negative; in mathematicians view
  133. int16_t th = ta - td; // text box height
  134. uint16_t x = (display.width() - tw) / 2;
  135. uint16_t y = display.height() * 3 / 4 - th / 2 + ta;
  136. display.setPartialWindow(0, y - 14, display.width(), 20);
  137. display.firstPage();
  138. do
  139. {
  140. display.fillScreen(bg);
  141. u8g2Fonts.setCursor(x, y);
  142. u8g2Fonts.print(HelloEpaper);
  143. }
  144. while (display.nextPage());
  145. //Serial.println("helloEpaper done");
  146. }
  147. void showFont(const char name[], const uint8_t *font)
  148. {
  149. display.setFullWindow();
  150. display.setRotation(0);
  151. u8g2Fonts.setFontMode(1); // use u8g2 transparent mode (this is default)
  152. u8g2Fonts.setFontDirection(0); // left to right (this is default)
  153. u8g2Fonts.setForegroundColor(GxEPD_BLACK); // apply Adafruit GFX color
  154. u8g2Fonts.setBackgroundColor(GxEPD_WHITE); // apply Adafruit GFX color
  155. u8g2Fonts.setFont(font); // select u8g2 font from here: https://github.com/olikraus/u8g2/wiki/fntlistall
  156. display.firstPage();
  157. do
  158. {
  159. drawFont(name);
  160. }
  161. while (display.nextPage());
  162. }
  163. void drawFont(const char name[])
  164. {
  165. //display.setRotation(0);
  166. display.fillScreen(GxEPD_WHITE);
  167. u8g2Fonts.setCursor(0, 0);
  168. u8g2Fonts.println();
  169. u8g2Fonts.println(name);
  170. u8g2Fonts.println(" !\"#$%&'()*+,-./");
  171. u8g2Fonts.println("0123456789:;<=>?");
  172. u8g2Fonts.println("@ABCDEFGHIJKLMNO");
  173. u8g2Fonts.println("PQRSTUVWXYZ[\\]^_");
  174. u8g2Fonts.println("`abcdefghijklmno");
  175. u8g2Fonts.println("pqrstuvwxyz{|}~ ");
  176. u8g2Fonts.println("Umlaut ÄÖÜäéöü");
  177. }