GxEPD2_U8G2_Fonts_Example.ino 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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: http://www.e-paper-display.com/download_list/downloadcategoryid=34&isMode=false.html
  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:
  19. // Waveshare e-paper displays with SPI: http://forum.arduino.cc/index.php?topic=487007.0
  20. // Good Display ePaper for Arduino: https://forum.arduino.cc/index.php?topic=436411.0
  21. // see GxEPD2_wiring_examples.h for wiring suggestions and examples
  22. // base class GxEPD2_GFX can be used to pass references or pointers to the display instance as parameter, uses ~1.2k more code
  23. // enable or disable GxEPD2_GFX base class
  24. #define ENABLE_GxEPD2_GFX 0
  25. #include <GxEPD2_BW.h>
  26. #include <GxEPD2_3C.h>
  27. #include <GxEPD2_7C.h>
  28. #include <Fonts/FreeMonoBold9pt7b.h>
  29. #include <U8g2_for_Adafruit_GFX.h>
  30. // select the display class and display driver class in the following file (new style):
  31. #include "GxEPD2_display_selection_new_style.h"
  32. U8G2_FOR_ADAFRUIT_GFX u8g2Fonts;
  33. void setup()
  34. {
  35. Serial.begin(115200);
  36. Serial.println();
  37. Serial.println("setup");
  38. display.init();
  39. u8g2Fonts.begin(display); // connect u8g2 procedures to Adafruit GFX
  40. helloWorld();
  41. delay(1000);
  42. helloArduino();
  43. delay(1000);
  44. helloEpaper();
  45. delay(1000);
  46. showFont("u8g2_font_helvR14_tf", u8g2_font_helvR14_tf); // select u8g2 font from here: https://github.com/olikraus/u8g2/wiki/fntlistall
  47. delay(2000);
  48. showFont("u8g2_font_profont22_mr", u8g2_font_profont22_mr); // select u8g2 font from here: https://github.com/olikraus/u8g2/wiki/fntlistall
  49. delay(1000);
  50. Serial.println("setup done");
  51. }
  52. void loop()
  53. {
  54. }
  55. const char HelloWorld[] = "Hello World!";
  56. const char HelloArduino[] = "Hello Arduino!";
  57. const char HelloEpaper[] = "Hello E-Paper!";
  58. void helloWorld()
  59. {
  60. //Serial.println("helloWorld");
  61. uint16_t bg = GxEPD_WHITE;
  62. uint16_t fg = GxEPD_BLACK;
  63. u8g2Fonts.setFontMode(1); // use u8g2 transparent mode (this is default)
  64. u8g2Fonts.setFontDirection(0); // left to right (this is default)
  65. u8g2Fonts.setForegroundColor(fg); // apply Adafruit GFX color
  66. u8g2Fonts.setBackgroundColor(bg); // apply Adafruit GFX color
  67. u8g2Fonts.setFont(u8g2_font_helvR14_tf); // select u8g2 font from here: https://github.com/olikraus/u8g2/wiki/fntlistall
  68. int16_t tw = u8g2Fonts.getUTF8Width(HelloWorld); // text box width
  69. int16_t ta = u8g2Fonts.getFontAscent(); // positive
  70. int16_t td = u8g2Fonts.getFontDescent(); // negative; in mathematicians view
  71. int16_t th = ta - td; // text box height
  72. //Serial.print("ascent, descent ("); Serial.print(u8g2Fonts.getFontAscent()); Serial.print(", "); Serial.print(u8g2Fonts.getFontDescent()); Serial.println(")");
  73. // center bounding box by transposition of origin:
  74. // y is base line for u8g2Fonts, like for Adafruit_GFX True Type fonts
  75. uint16_t x = (display.width() - tw) / 2;
  76. uint16_t y = (display.height() - th) / 2 + ta;
  77. //Serial.print("bounding box ("); Serial.print(x); Serial.print(", "); Serial.print(y); Serial.print(", "); Serial.print(tw); Serial.print(", "); Serial.print(th); Serial.println(")");
  78. display.firstPage();
  79. do
  80. {
  81. display.fillScreen(bg);
  82. u8g2Fonts.setCursor(x, y); // start writing at this position
  83. u8g2Fonts.print(HelloWorld);
  84. }
  85. while (display.nextPage());
  86. //Serial.println("helloWorld done");
  87. }
  88. void helloArduino()
  89. {
  90. //Serial.println("helloArduino");
  91. uint16_t bg = GxEPD_WHITE;
  92. uint16_t fg = GxEPD_BLACK;
  93. u8g2Fonts.setFontMode(1); // use u8g2 transparent mode (this is default)
  94. u8g2Fonts.setFontDirection(0); // left to right (this is default)
  95. u8g2Fonts.setForegroundColor(fg); // apply Adafruit GFX color
  96. u8g2Fonts.setBackgroundColor(bg); // apply Adafruit GFX color
  97. u8g2Fonts.setFont(u8g2_font_helvR14_tf); // select u8g2 font from here: https://github.com/olikraus/u8g2/wiki/fntlistall
  98. int16_t tw = u8g2Fonts.getUTF8Width(HelloArduino); // text box width
  99. int16_t ta = u8g2Fonts.getFontAscent(); // positive
  100. int16_t td = u8g2Fonts.getFontDescent(); // negative; in mathematicians view
  101. int16_t th = ta - td; // text box height
  102. uint16_t x = (display.width() - tw) / 2;
  103. uint16_t y = display.height() / 4 - th / 2 + ta;
  104. display.setPartialWindow(0, y - 14, display.width(), 20);
  105. display.firstPage();
  106. do
  107. {
  108. display.fillScreen(bg);
  109. u8g2Fonts.setCursor(x, y);
  110. u8g2Fonts.print(HelloArduino);
  111. }
  112. while (display.nextPage());
  113. //Serial.println("helloArduino done");
  114. }
  115. void helloEpaper()
  116. {
  117. //Serial.println("helloEpaper");
  118. uint16_t bg = GxEPD_WHITE;
  119. uint16_t fg = GxEPD_BLACK;
  120. u8g2Fonts.setFontMode(1); // use u8g2 transparent mode (this is default)
  121. u8g2Fonts.setFontDirection(0); // left to right (this is default)
  122. u8g2Fonts.setForegroundColor(fg); // apply Adafruit GFX color
  123. u8g2Fonts.setBackgroundColor(bg); // apply Adafruit GFX color
  124. u8g2Fonts.setFont(u8g2_font_helvR14_tf); // select u8g2 font from here: https://github.com/olikraus/u8g2/wiki/fntlistall
  125. int16_t tw = u8g2Fonts.getUTF8Width(HelloArduino); // text box width
  126. int16_t ta = u8g2Fonts.getFontAscent(); // positive
  127. int16_t td = u8g2Fonts.getFontDescent(); // negative; in mathematicians view
  128. int16_t th = ta - td; // text box height
  129. uint16_t x = (display.width() - tw) / 2;
  130. uint16_t y = display.height() * 3 / 4 - th / 2 + ta;
  131. display.setPartialWindow(0, y - 14, display.width(), 20);
  132. display.firstPage();
  133. do
  134. {
  135. display.fillScreen(bg);
  136. u8g2Fonts.setCursor(x, y);
  137. u8g2Fonts.print(HelloEpaper);
  138. }
  139. while (display.nextPage());
  140. //Serial.println("helloEpaper done");
  141. }
  142. void showFont(const char name[], const uint8_t *font)
  143. {
  144. display.setFullWindow();
  145. display.setRotation(0);
  146. u8g2Fonts.setFontMode(1); // use u8g2 transparent mode (this is default)
  147. u8g2Fonts.setFontDirection(0); // left to right (this is default)
  148. u8g2Fonts.setForegroundColor(GxEPD_BLACK); // apply Adafruit GFX color
  149. u8g2Fonts.setBackgroundColor(GxEPD_WHITE); // apply Adafruit GFX color
  150. u8g2Fonts.setFont(font); // select u8g2 font from here: https://github.com/olikraus/u8g2/wiki/fntlistall
  151. display.firstPage();
  152. do
  153. {
  154. drawFont(name);
  155. }
  156. while (display.nextPage());
  157. }
  158. void drawFont(const char name[])
  159. {
  160. //display.setRotation(0);
  161. display.fillScreen(GxEPD_WHITE);
  162. u8g2Fonts.setCursor(0, 0);
  163. u8g2Fonts.println();
  164. u8g2Fonts.println(name);
  165. u8g2Fonts.println(" !\"#$%&'()*+,-./");
  166. u8g2Fonts.println("0123456789:;<=>?");
  167. u8g2Fonts.println("@ABCDEFGHIJKLMNO");
  168. u8g2Fonts.println("PQRSTUVWXYZ[\\]^_");
  169. u8g2Fonts.println("`abcdefghijklmno");
  170. u8g2Fonts.println("pqrstuvwxyz{|}~ ");
  171. u8g2Fonts.println("Umlaut ÄÖÜäéöü");
  172. }