GxEPD2_WiFi_ParticleExample.ino 52 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297
  1. // GxEPD2_PP_WiFi_Example : Display Library example for SPI e-paper panels from Dalian Good Display and boards from Waveshare.
  2. // Requires HW SPI and Adafruit_GFX. Caution: these e-papers require 3.3V supply AND data lines!
  3. //
  4. // Display Library based on Demo Example from Good Display: http://www.good-display.com/download_list/downloadcategoryid=34&isMode=false.html
  5. //
  6. // BMP handling code extracts taken from: https://github.com/prenticedavid/MCUFRIEND_kbv/tree/master/examples/showBMP_kbv_Uno
  7. //
  8. // Author: Jean-Marc Zingg
  9. //
  10. // Version: see library.properties
  11. //
  12. // Library: https://github.com/ZinggJM/GxEPD2_PP
  13. //
  14. #include <Adafruit_GFX_RK.h>
  15. #include <GxEPD2.h>
  16. // Note: only bitmap download from http: works on Particle, would need help for https:
  17. // Supporting Arduino Forum Topics:
  18. // Waveshare e-paper displays with SPI: http://forum.arduino.cc/index.php?topic=487007.0
  19. // Good Display ePaper for Arduino : https://forum.arduino.cc/index.php?topic=436411.0
  20. // mapping suggestion from Waveshare SPI e-Paper to Particle Photon
  21. // A5 MOSI
  22. // A4 MISO
  23. // A3 SCK
  24. // A2 SS
  25. // BUSY -> D4, RST -> A0, DC -> A1, CS -> A2, CLK -> A3, DIN -> A5, GND -> GND, 3.3V -> 3.3V
  26. // NOTE: it looks like MISO can't be used as general input pin for BUSY.
  27. #include <GxEPD2_BW.h>
  28. #include <GxEPD2_3C.h>
  29. #if defined(PARTICLE)
  30. // select one and adapt to your mapping, for buffered drawing, can use full buffer size (full HEIGHT) for Photon
  31. GxEPD2_BW<GxEPD2_154, GxEPD2_154::HEIGHT> display(GxEPD2_154(/*CS=A2*/ SS, /*DC=*/ A1, /*RST=*/ A0, /*BUSY=*/ D4));
  32. //GxEPD2_BW<GxEPD2_213, GxEPD2_213::HEIGHT> display(GxEPD2_213(/*CS=A2*/ SS, /*DC=*/ A1, /*RST=*/ A0, /*BUSY=*/ D4)); // GDE0213B1, phased out
  33. //GxEPD2_BW<GxEPD2_213_B72, GxEPD2_213_B72::HEIGHT> display(GxEPD2_213_B72(/*CS=A2*/ SS, /*DC=*/ A1, /*RST=*/ A0, /*BUSY=*/ D4)); // GDEH0213B72
  34. //GxEPD2_BW<GxEPD2_213_B73, GxEPD2_213_B73::HEIGHT> display(GxEPD2_213_B73(/*CS=A2*/ SS, /*DC=*/ A1, /*RST=*/ A0, /*BUSY=*/ D4)); // GDEH0213B73
  35. //GxEPD2_BW<GxEPD2_213_flex, GxEPD2_213_flex::HEIGHT> display(GxEPD2_213_flex(/*CS=A2*/ SS, /*DC=*/ A1, /*RST=*/ A0, /*BUSY=*/ D4)); // GDEW0213I5F
  36. //GxEPD2_BW<GxEPD2_290, GxEPD2_290::HEIGHT> display(GxEPD2_290(/*CS=A2*/ SS, /*DC=*/ A1, /*RST=*/ A0, /*BUSY=*/ D4));
  37. //GxEPD2_BW<GxEPD2_290_T5, GxEPD2_290_T5::HEIGHT> display(GxEPD2_290_T5(/*CS=A2*/ SS, /*DC=*/ A1, /*RST=*/ A0, /*BUSY=*/ D4)); // GDEW029T5
  38. //GxEPD2_BW<GxEPD2_260, GxEPD2_260::HEIGHT> display(GxEPD2_260(/*CS=A2*/ SS, /*DC=*/ A1, /*RST=*/ A0, /*BUSY=*/ D4));
  39. //GxEPD2_BW<GxEPD2_270, GxEPD2_270::HEIGHT> display(GxEPD2_270(/*CS=A2*/ SS, /*DC=*/ A1, /*RST=*/ A0, /*BUSY=*/ D4));
  40. //GxEPD2_BW<GxEPD2_371, GxEPD2_371::HEIGHT> display(GxEPD2_371(/*CS=A2*/ SS, /*DC=*/ A1, /*RST=*/ A0, /*BUSY=*/ D4));
  41. //GxEPD2_BW<GxEPD2_420, GxEPD2_420::HEIGHT> display(GxEPD2_420(/*CS=A2*/ SS, /*DC=*/ A1, /*RST=*/ A0, /*BUSY=*/ D4));
  42. //GxEPD2_BW < GxEPD2_583, GxEPD2_583::HEIGHT / 2 > display(GxEPD2_583(/*CS=A2*/ SS, /*DC=*/ A1, /*RST=*/ A0, /*BUSY=*/ D4));
  43. //GxEPD2_BW < GxEPD2_750, GxEPD2_750::HEIGHT / 2 > display(GxEPD2_750(/*CS=A2*/ SS, /*DC=*/ A1, /*RST=*/ A0, /*BUSY=*/ D4));
  44. // 3-color e-papers
  45. //GxEPD2_3C<GxEPD2_154c, GxEPD2_154c::HEIGHT> display(GxEPD2_154c(/*CS=A2*/ SS, /*DC=*/ A1, /*RST=*/ A0, /*BUSY=*/ D4));
  46. //GxEPD2_3C<GxEPD2_213c, GxEPD2_213c::HEIGHT> display(GxEPD2_213c(/*CS=A2*/ SS, /*DC=*/ A1, /*RST=*/ A0, /*BUSY=*/ D4));
  47. //GxEPD2_3C<GxEPD2_290c, GxEPD2_290c::HEIGHT> display(GxEPD2_290c(/*CS=A2*/ SS, /*DC=*/ A1, /*RST=*/ A0, /*BUSY=*/ D4));
  48. //GxEPD2_3C<GxEPD2_270c, GxEPD2_270c::HEIGHT> display(GxEPD2_270c(/*CS=A2*/ SS, /*DC=*/ A1, /*RST=*/ A0, /*BUSY=*/ D4));
  49. //GxEPD2_3C<GxEPD2_420c, GxEPD2_420c::HEIGHT> display(GxEPD2_420c(/*CS=A2*/ SS, /*DC=*/ A1, /*RST=*/ A0, /*BUSY=*/ D4));
  50. //GxEPD2_3C<GxEPD2_583c, GxEPD2_583c::HEIGHT / 4 > display(GxEPD2_583c(/*CS=A2*/ SS, /*DC=*/ A1, /*RST=*/ A0, /*BUSY=*/ D4));
  51. //GxEPD2_3C<GxEPD2_750c, GxEPD2_750c::HEIGHT / 4 > display(GxEPD2_750c(/*CS=A2*/ SS, /*DC=*/ A1, /*RST=*/ A0, /*BUSY=*/ D4));
  52. //GxEPD2_3C<GxEPD2_750c_Z08, GxEPD2_750c_Z08::HEIGHT / 4 > display(GxEPD2_750c_Z08(/*CS=A2*/ SS, /*DC=*/ A1, /*RST=*/ A0, /*BUSY=*/ D4)); // GDEW075Z08 800x480
  53. #endif
  54. const char* ssid = "........";
  55. const char* password = "........";
  56. const int httpPort = 80;
  57. const int httpsPort = 443;
  58. const char* fp_api_github_com = "35 85 74 EF 67 35 A7 CE 40 69 50 F3 C0 F6 80 CF 80 3B 2E 19";
  59. const char* fp_github_com = "ca 06 f5 6b 25 8b 7a 0d 4f 2b 05 47 09 39 47 86 51 15 19 84";
  60. #if USE_BearSSL
  61. const char fp_rawcontent[20] = {0xcc, 0xaa, 0x48, 0x48, 0x66, 0x46, 0x0e, 0x91, 0x53, 0x2c, 0x9c, 0x7c, 0x23, 0x2a, 0xb1, 0x74, 0x4d, 0x29, 0x9d, 0x33};
  62. #else
  63. const char* fp_rawcontent = "cc aa 48 48 66 46 0e 91 53 2c 9c 7c 23 2a b1 74 4d 29 9d 33";
  64. #endif
  65. const char* host_rawcontent = "raw.githubusercontent.com";
  66. const char* path_rawcontent = "/ZinggJM/GxEPD2/master/extras/bitmaps/";
  67. const char* path_prenticedavid = "/prenticedavid/MCUFRIEND_kbv/master/extras/bitmaps/";
  68. void showBitmapFrom_HTTP(const char* host, const char* path, const char* filename, int16_t x, int16_t y, bool with_color = true);
  69. void showBitmapFrom_HTTPS(const char* host, const char* path, const char* filename, const char* fingerprint, int16_t x, int16_t y, bool with_color = true);
  70. void showBitmapFrom_HTTP_Buffered(const char* host, const char* path, const char* filename, int16_t x, int16_t y, bool with_color = true);
  71. void showBitmapFrom_HTTPS_Buffered(const char* host, const char* path, const char* filename, const char* fingerprint, int16_t x, int16_t y, bool with_color = true);
  72. void setup()
  73. {
  74. Serial.begin(115200);
  75. Serial.println();
  76. Serial.println("GxEPD2_PP_WiFi_Example");
  77. display.init(115200);
  78. if ((display.epd2.panel == GxEPD2::GDEW0154Z04) || false)
  79. {
  80. //drawBitmapsBuffered_200x200();
  81. //drawBitmapsBuffered_other();
  82. }
  83. else
  84. {
  85. //drawBitmaps_200x200();
  86. //drawBitmaps_other();
  87. drawTheOnlyBitmapFromHTTP();
  88. }
  89. //drawBitmaps_test();
  90. //drawBitmapsBuffered_test();
  91. Serial.println("GxEPD2_PP_WiFi_Example done");
  92. }
  93. void loop(void)
  94. {
  95. }
  96. void drawTheOnlyBitmapFromHTTP()
  97. {
  98. int16_t w2 = display.width() / 2;
  99. int16_t h2 = display.height() / 2;
  100. showBitmapFrom_HTTP("www.squix.org", "/blog/wunderground/", "chanceflurries.bmp", w2 - 50, h2 - 50, false);
  101. delay(2000);
  102. }
  103. void drawBitmaps_200x200()
  104. {
  105. int16_t x = (display.width() - 200) / 2;
  106. int16_t y = (display.height() - 200) / 2;
  107. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "logo200x200.bmp", fp_rawcontent, x, y);
  108. delay(2000);
  109. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "first200x200.bmp", fp_rawcontent, x, y);
  110. delay(2000);
  111. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "second200x200.bmp", fp_rawcontent, x, y);
  112. delay(2000);
  113. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "third200x200.bmp", fp_rawcontent, x, y);
  114. delay(2000);
  115. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "fourth200x200.bmp", fp_rawcontent, x, y);
  116. delay(2000);
  117. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "fifth200x200.bmp", fp_rawcontent, x, y);
  118. delay(2000);
  119. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "sixth200x200.bmp", fp_rawcontent, x, y);
  120. delay(2000);
  121. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "seventh200x200.bmp", fp_rawcontent, x, y);
  122. delay(2000);
  123. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "eighth200x200.bmp", fp_rawcontent, x, y);
  124. delay(2000);
  125. }
  126. void drawBitmaps_other()
  127. {
  128. int16_t w2 = display.width() / 2;
  129. int16_t h2 = display.height() / 2;
  130. showBitmapFrom_HTTP("www.squix.org", "/blog/wunderground/", "chanceflurries.bmp", w2 - 50, h2 - 50, false);
  131. delay(2000);
  132. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "betty_1.bmp", fp_rawcontent, w2 - 100, h2 - 160);
  133. delay(2000);
  134. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "betty_4.bmp", fp_rawcontent, w2 - 102, h2 - 126);
  135. delay(2000);
  136. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "marilyn_240x240x8.bmp", fp_rawcontent, w2 - 120, h2 - 120);
  137. delay(2000);
  138. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "miniwoof.bmp", fp_rawcontent, w2 - 60, h2 - 80);
  139. delay(2000);
  140. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "test.bmp", fp_rawcontent, w2 - 100, h2 - 100);
  141. delay(2000);
  142. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "tiger.bmp", fp_rawcontent, w2 - 160, h2 - 120);
  143. delay(2000);
  144. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "tiger_178x160x4.bmp", fp_rawcontent, w2 - 89, h2 - 80);
  145. delay(2000);
  146. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "tiger_240x317x4.bmp", fp_rawcontent, w2 - 120, h2 - 160);
  147. delay(2000);
  148. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "tiger_320x200x24.bmp", fp_rawcontent, w2 - 160, h2 - 100);
  149. delay(2000);
  150. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "tiger16T.bmp", fp_rawcontent, w2 - 160, h2 - 100);
  151. delay(2000);
  152. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "woof.bmp", fp_rawcontent, w2 - 100, h2 - 100);
  153. delay(2000);
  154. }
  155. void drawBitmaps_test()
  156. {
  157. int16_t w2 = display.width() / 2;
  158. int16_t h2 = display.height() / 2;
  159. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "betty_4.bmp", fp_rawcontent, w2 - 102, h2 - 126);
  160. delay(2000);
  161. }
  162. void drawBitmapsBuffered_200x200()
  163. {
  164. int16_t x = (display.width() - 200) / 2;
  165. int16_t y = (display.height() - 200) / 2;
  166. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_rawcontent, "logo200x200.bmp", fp_rawcontent, x, y);
  167. delay(2000);
  168. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_rawcontent, "first200x200.bmp", fp_rawcontent, x, y);
  169. delay(2000);
  170. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_rawcontent, "second200x200.bmp", fp_rawcontent, x, y);
  171. delay(2000);
  172. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_rawcontent, "third200x200.bmp", fp_rawcontent, x, y);
  173. delay(2000);
  174. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_rawcontent, "fourth200x200.bmp", fp_rawcontent, x, y);
  175. delay(2000);
  176. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_rawcontent, "fifth200x200.bmp", fp_rawcontent, x, y);
  177. delay(2000);
  178. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_rawcontent, "sixth200x200.bmp", fp_rawcontent, x, y);
  179. delay(2000);
  180. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_rawcontent, "seventh200x200.bmp", fp_rawcontent, x, y);
  181. delay(2000);
  182. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_rawcontent, "eighth200x200.bmp", fp_rawcontent, x, y);
  183. delay(2000);
  184. }
  185. void drawBitmapsBuffered_other()
  186. {
  187. int16_t w2 = display.width() / 2;
  188. int16_t h2 = display.height() / 2;
  189. showBitmapFrom_HTTP_Buffered("www.squix.org", "/blog/wunderground/", "chanceflurries.bmp", w2 - 50, h2 - 50, false);
  190. delay(2000);
  191. //showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "betty_1.bmp", fp_rawcontent, w2 - 100, h2 - 160);
  192. delay(2000);
  193. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "betty_4.bmp", fp_rawcontent, w2 - 102, h2 - 126);
  194. delay(2000);
  195. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "marilyn_240x240x8.bmp", fp_rawcontent, w2 - 120, h2 - 120);
  196. delay(2000);
  197. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "miniwoof.bmp", fp_rawcontent, w2 - 60, h2 - 80);
  198. delay(2000);
  199. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "test.bmp", fp_rawcontent, w2 - 100, h2 - 100);
  200. delay(2000);
  201. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "tiger.bmp", fp_rawcontent, w2 - 160, h2 - 120);
  202. delay(2000);
  203. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "tiger_178x160x4.bmp", fp_rawcontent, w2 - 89, h2 - 80);
  204. delay(2000);
  205. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "tiger_240x317x4.bmp", fp_rawcontent, w2 - 120, h2 - 160);
  206. delay(2000);
  207. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "tiger_320x200x24.bmp", fp_rawcontent, w2 - 160, h2 - 100);
  208. delay(2000);
  209. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "tiger16T.bmp", fp_rawcontent, w2 - 160, h2 - 100);
  210. delay(2000);
  211. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "woof.bmp", fp_rawcontent, w2 - 100, h2 - 100);
  212. delay(2000);
  213. }
  214. void drawBitmapsBuffered_test()
  215. {
  216. int16_t w2 = display.width() / 2;
  217. int16_t h2 = display.height() / 2;
  218. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "betty_4.bmp", fp_rawcontent, w2 - 102, h2 - 126);
  219. delay(2000);
  220. }
  221. static const uint16_t input_buffer_pixels = 640; // may affect performance
  222. static const uint16_t max_row_width = 640; // for up to 7.5" display
  223. static const uint16_t max_palette_pixels = 256; // for depth <= 8
  224. uint8_t input_buffer[3 * input_buffer_pixels]; // up to depth 24
  225. uint8_t output_row_mono_buffer[max_row_width / 8]; // buffer for at least one row of b/w bits
  226. uint8_t output_row_color_buffer[max_row_width / 8]; // buffer for at least one row of color bits
  227. uint8_t mono_palette_buffer[max_palette_pixels / 8]; // palette buffer for depth <= 8 b/w
  228. uint8_t color_palette_buffer[max_palette_pixels / 8]; // palette buffer for depth <= 8 c/w
  229. void showBitmapFrom_HTTP(const char* host, const char* path, const char* filename, int16_t x, int16_t y, bool with_color)
  230. {
  231. TCPClient client;
  232. bool connection_ok = false;
  233. bool valid = false; // valid format to be handled
  234. bool flip = true; // bitmap is stored bottom-to-top
  235. uint32_t startTime = millis();
  236. if ((x >= display.width()) || (y >= display.height())) return;
  237. Serial.println(); Serial.print("downloading file \""); Serial.print(filename); Serial.println("\"");
  238. Serial.print("connecting to "); Serial.println(host);
  239. if (!client.connect(host, httpPort))
  240. {
  241. Serial.println("connection failed");
  242. return;
  243. }
  244. Serial.print("requesting URL: ");
  245. Serial.println(String("http://") + host + path + filename);
  246. client.print(String("GET ") + path + filename + " HTTP/1.1\r\n" +
  247. "Host: " + host + "\r\n" +
  248. "User-Agent: GxEPD2_WiFi_Example\r\n" +
  249. "Connection: close\r\n\r\n");
  250. Serial.println("request sent");
  251. while (client.connected())
  252. {
  253. String line = client.readStringUntil('\n');
  254. if (!connection_ok)
  255. {
  256. connection_ok = line.startsWith("HTTP/1.1 200 OK");
  257. if (connection_ok) Serial.println(line);
  258. //if (!connection_ok) Serial.println(line);
  259. }
  260. if (!connection_ok) Serial.println(line);
  261. //Serial.println(line);
  262. if (line == "\r")
  263. {
  264. Serial.println("headers received");
  265. break;
  266. }
  267. }
  268. if (!connection_ok) return;
  269. // Parse BMP header
  270. if (read16(client) == 0x4D42) // BMP signature
  271. {
  272. uint32_t fileSize = read32(client);
  273. uint32_t creatorBytes = read32(client);
  274. uint32_t imageOffset = read32(client); // Start of image data
  275. uint32_t headerSize = read32(client);
  276. uint32_t width = read32(client);
  277. uint32_t height = read32(client);
  278. uint16_t planes = read16(client);
  279. uint16_t depth = read16(client); // bits per pixel
  280. uint32_t format = read32(client);
  281. uint32_t bytes_read = 7 * 4 + 3 * 2; // read so far
  282. if ((planes == 1) && ((format == 0) || (format == 3))) // uncompressed is handled, 565 also
  283. {
  284. Serial.print("File size: "); Serial.println(fileSize);
  285. Serial.print("Image Offset: "); Serial.println(imageOffset);
  286. Serial.print("Header size: "); Serial.println(headerSize);
  287. Serial.print("Bit Depth: "); Serial.println(depth);
  288. Serial.print("Image size: ");
  289. Serial.print(width);
  290. Serial.print('x');
  291. Serial.println(height);
  292. // BMP rows are padded (if needed) to 4-byte boundary
  293. uint32_t rowSize = (width * depth / 8 + 3) & ~3;
  294. if (depth < 8) rowSize = ((width * depth + 8 - depth) / 8 + 3) & ~3;
  295. if (height < 0)
  296. {
  297. height = -height;
  298. flip = false;
  299. }
  300. uint16_t w = width;
  301. uint16_t h = height;
  302. if ((x + w - 1) >= display.width()) w = display.width() - x;
  303. if ((y + h - 1) >= display.height()) h = display.height() - y;
  304. if (w <= max_row_width) // handle with direct drawing
  305. {
  306. valid = true;
  307. uint8_t bitmask = 0xFF;
  308. uint8_t bitshift = 8 - depth;
  309. uint16_t red, green, blue;
  310. bool whitish, colored;
  311. if (depth == 1) with_color = false;
  312. if (depth <= 8)
  313. {
  314. if (depth < 8) bitmask >>= depth;
  315. bytes_read += skip(client, 54 - bytes_read); //palette is always @ 54
  316. for (uint16_t pn = 0; pn < (1 << depth); pn++)
  317. {
  318. blue = client.read();
  319. green = client.read();
  320. red = client.read();
  321. client.read();
  322. bytes_read += 4;
  323. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  324. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  325. if (0 == pn % 8) mono_palette_buffer[pn / 8] = 0;
  326. mono_palette_buffer[pn / 8] |= whitish << pn % 8;
  327. if (0 == pn % 8) color_palette_buffer[pn / 8] = 0;
  328. color_palette_buffer[pn / 8] |= colored << pn % 8;
  329. }
  330. }
  331. display.clearScreen();
  332. uint32_t rowPosition = flip ? imageOffset + (height - h) * rowSize : imageOffset;
  333. //Serial.print("skip "); Serial.println(rowPosition - bytes_read);
  334. bytes_read += skip(client, rowPosition - bytes_read);
  335. for (uint16_t row = 0; row < h; row++, rowPosition += rowSize) // for each line
  336. {
  337. if (!connection_ok || !(client.connected() || client.available())) break;
  338. delay(1); // yield() to avoid WDT
  339. uint32_t in_remain = rowSize;
  340. uint32_t in_idx = 0;
  341. uint32_t in_bytes = 0;
  342. uint8_t in_byte = 0; // for depth <= 8
  343. uint8_t in_bits = 0; // for depth <= 8
  344. uint8_t out_byte = 0xFF; // white (for w%8!=0 boarder)
  345. uint8_t out_color_byte = 0xFF; // white (for w%8!=0 boarder)
  346. uint32_t out_idx = 0;
  347. for (uint16_t col = 0; col < w; col++) // for each pixel
  348. {
  349. yield();
  350. if (!connection_ok || !(client.connected() || client.available())) break;
  351. // Time to read more pixel data?
  352. if (in_idx >= in_bytes) // ok, exact match for 24bit also (size IS multiple of 3)
  353. {
  354. uint32_t get = in_remain > sizeof(input_buffer) ? sizeof(input_buffer) : in_remain;
  355. uint32_t got = read(client, input_buffer, get);
  356. while ((got < get) && connection_ok)
  357. {
  358. //Serial.print("got "); Serial.print(got); Serial.print(" < "); Serial.print(get); Serial.print(" @ "); Serial.println(bytes_read);
  359. uint32_t gotmore = read(client, input_buffer + got, get - got);
  360. got += gotmore;
  361. connection_ok = gotmore > 0;
  362. }
  363. in_bytes = got;
  364. in_remain -= got;
  365. bytes_read += got;
  366. }
  367. if (!connection_ok)
  368. {
  369. Serial.print("Error: got no more after "); Serial.print(bytes_read); Serial.println(" bytes read!");
  370. break;
  371. }
  372. switch (depth)
  373. {
  374. case 24:
  375. blue = input_buffer[in_idx++];
  376. green = input_buffer[in_idx++];
  377. red = input_buffer[in_idx++];
  378. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  379. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  380. break;
  381. case 16:
  382. {
  383. uint8_t lsb = input_buffer[in_idx++];
  384. uint8_t msb = input_buffer[in_idx++];
  385. if (format == 0) // 555
  386. {
  387. blue = (lsb & 0x1F) << 3;
  388. green = ((msb & 0x03) << 6) | ((lsb & 0xE0) >> 2);
  389. red = (msb & 0x7C) << 1;
  390. }
  391. else // 565
  392. {
  393. blue = (lsb & 0x1F) << 3;
  394. green = ((msb & 0x07) << 5) | ((lsb & 0xE0) >> 3);
  395. red = (msb & 0xF8);
  396. }
  397. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  398. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  399. }
  400. break;
  401. case 1:
  402. case 4:
  403. case 8:
  404. {
  405. if (0 == in_bits)
  406. {
  407. in_byte = input_buffer[in_idx++];
  408. in_bits = 8;
  409. }
  410. uint16_t pn = (in_byte >> bitshift) & bitmask;
  411. whitish = mono_palette_buffer[pn / 8] & (0x1 << pn % 8);
  412. colored = color_palette_buffer[pn / 8] & (0x1 << pn % 8);
  413. in_byte <<= depth;
  414. in_bits -= depth;
  415. }
  416. break;
  417. }
  418. if (whitish)
  419. {
  420. // keep white
  421. }
  422. else if (colored && with_color)
  423. {
  424. out_color_byte &= ~(0x80 >> col % 8); // colored
  425. }
  426. else
  427. {
  428. out_byte &= ~(0x80 >> col % 8); // black
  429. }
  430. if ((7 == col % 8) || (col == w - 1)) // write that last byte! (for w%8!=0 boarder)
  431. {
  432. output_row_color_buffer[out_idx] = out_color_byte;
  433. output_row_mono_buffer[out_idx++] = out_byte;
  434. out_byte = 0xFF; // white (for w%8!=0 boarder)
  435. out_color_byte = 0xFF; // white (for w%8!=0 boarder)
  436. }
  437. } // end pixel
  438. int16_t yrow = y + (flip ? h - row - 1 : row);
  439. display.writeImage(output_row_mono_buffer, output_row_color_buffer, x, yrow, w, 1);
  440. } // end line
  441. Serial.print("downloaded in ");
  442. Serial.print(millis() - startTime);
  443. Serial.println(" ms");
  444. display.refresh();
  445. }
  446. Serial.print("bytes read "); Serial.println(bytes_read);
  447. }
  448. }
  449. if (!valid)
  450. {
  451. Serial.println("bitmap format not handled.");
  452. }
  453. }
  454. void drawBitmapFrom_HTTP_ToBuffer(const char* host, const char* path, const char* filename, int16_t x, int16_t y, bool with_color)
  455. {
  456. TCPClient client;
  457. bool connection_ok = false;
  458. bool valid = false; // valid format to be handled
  459. bool flip = true; // bitmap is stored bottom-to-top
  460. uint32_t startTime = millis();
  461. if ((x >= display.width()) || (y >= display.height())) return;
  462. display.fillScreen(GxEPD_WHITE);
  463. Serial.print("connecting to "); Serial.println(host);
  464. if (!client.connect(host, httpPort))
  465. {
  466. Serial.println("connection failed");
  467. return;
  468. }
  469. Serial.print("requesting URL: ");
  470. Serial.println(String("http://") + host + path + filename);
  471. client.print(String("GET ") + path + filename + " HTTP/1.1\r\n" +
  472. "Host: " + host + "\r\n" +
  473. "User-Agent: GxEPD2_WiFi_Example\r\n" +
  474. "Connection: close\r\n\r\n");
  475. Serial.println("request sent");
  476. while (client.connected())
  477. {
  478. String line = client.readStringUntil('\n');
  479. if (!connection_ok)
  480. {
  481. connection_ok = line.startsWith("HTTP/1.1 200 OK");
  482. if (connection_ok) Serial.println(line);
  483. //if (!connection_ok) Serial.println(line);
  484. }
  485. if (!connection_ok) Serial.println(line);
  486. //Serial.println(line);
  487. if (line == "\r")
  488. {
  489. Serial.println("headers received");
  490. break;
  491. }
  492. }
  493. if (!connection_ok) return;
  494. // Parse BMP header
  495. if (read16(client) == 0x4D42) // BMP signature
  496. {
  497. uint32_t fileSize = read32(client);
  498. uint32_t creatorBytes = read32(client);
  499. uint32_t imageOffset = read32(client); // Start of image data
  500. uint32_t headerSize = read32(client);
  501. uint32_t width = read32(client);
  502. uint32_t height = read32(client);
  503. uint16_t planes = read16(client);
  504. uint16_t depth = read16(client); // bits per pixel
  505. uint32_t format = read32(client);
  506. uint32_t bytes_read = 7 * 4 + 3 * 2; // read so far
  507. if ((planes == 1) && ((format == 0) || (format == 3))) // uncompressed is handled, 565 also
  508. {
  509. Serial.print("File size: "); Serial.println(fileSize);
  510. Serial.print("Image Offset: "); Serial.println(imageOffset);
  511. Serial.print("Header size: "); Serial.println(headerSize);
  512. Serial.print("Bit Depth: "); Serial.println(depth);
  513. Serial.print("Image size: ");
  514. Serial.print(width);
  515. Serial.print('x');
  516. Serial.println(height);
  517. // BMP rows are padded (if needed) to 4-byte boundary
  518. uint32_t rowSize = (width * depth / 8 + 3) & ~3;
  519. if (depth < 8) rowSize = ((width * depth + 8 - depth) / 8 + 3) & ~3;
  520. if (height < 0)
  521. {
  522. height = -height;
  523. flip = false;
  524. }
  525. uint16_t w = width;
  526. uint16_t h = height;
  527. if ((x + w - 1) >= display.width()) w = display.width() - x;
  528. if ((y + h - 1) >= display.height()) h = display.height() - y;
  529. //if (w <= max_row_width) // handle with direct drawing
  530. {
  531. valid = true;
  532. uint8_t bitmask = 0xFF;
  533. uint8_t bitshift = 8 - depth;
  534. uint16_t red, green, blue;
  535. bool whitish, colored;
  536. if (depth == 1) with_color = false;
  537. if (depth <= 8)
  538. {
  539. if (depth < 8) bitmask >>= depth;
  540. bytes_read += skip(client, 54 - bytes_read); //palette is always @ 54
  541. for (uint16_t pn = 0; pn < (1 << depth); pn++)
  542. {
  543. blue = client.read();
  544. green = client.read();
  545. red = client.read();
  546. client.read();
  547. bytes_read += 4;
  548. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  549. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  550. if (0 == pn % 8) mono_palette_buffer[pn / 8] = 0;
  551. mono_palette_buffer[pn / 8] |= whitish << pn % 8;
  552. if (0 == pn % 8) color_palette_buffer[pn / 8] = 0;
  553. color_palette_buffer[pn / 8] |= colored << pn % 8;
  554. //Serial.print("0x00"); Serial.print(red, HEX); Serial.print(green, HEX); Serial.print(blue, HEX);
  555. //Serial.print(" : "); Serial.print(whitish); Serial.print(", "); Serial.println(colored);
  556. }
  557. }
  558. display.writeScreenBuffer();
  559. uint32_t rowPosition = flip ? imageOffset + (height - h) * rowSize : imageOffset;
  560. //Serial.print("skip "); Serial.println(rowPosition - bytes_read);
  561. bytes_read += skip(client, rowPosition - bytes_read);
  562. for (uint16_t row = 0; row < h; row++, rowPosition += rowSize) // for each line
  563. {
  564. if (!connection_ok || !(client.connected() || client.available())) break;
  565. delay(1); // yield() to avoid WDT
  566. uint32_t in_remain = rowSize;
  567. uint32_t in_idx = 0;
  568. uint32_t in_bytes = 0;
  569. uint8_t in_byte = 0; // for depth <= 8
  570. uint8_t in_bits = 0; // for depth <= 8
  571. uint16_t color = GxEPD_WHITE;
  572. for (uint16_t col = 0; col < w; col++) // for each pixel
  573. {
  574. yield();
  575. if (!connection_ok || !(client.connected() || client.available())) break;
  576. // Time to read more pixel data?
  577. if (in_idx >= in_bytes) // ok, exact match for 24bit also (size IS multiple of 3)
  578. {
  579. uint32_t get = in_remain > sizeof(input_buffer) ? sizeof(input_buffer) : in_remain;
  580. uint32_t got = read(client, input_buffer, get);
  581. while ((got < get) && connection_ok)
  582. {
  583. //Serial.print("got "); Serial.print(got); Serial.print(" < "); Serial.print(get); Serial.print(" @ "); Serial.println(bytes_read);
  584. uint32_t gotmore = read(client, input_buffer + got, get - got);
  585. got += gotmore;
  586. connection_ok = gotmore > 0;
  587. }
  588. in_bytes = got;
  589. in_remain -= got;
  590. bytes_read += got;
  591. }
  592. if (!connection_ok)
  593. {
  594. Serial.print("Error: got no more after "); Serial.print(bytes_read); Serial.println(" bytes read!");
  595. break;
  596. }
  597. switch (depth)
  598. {
  599. case 24:
  600. blue = input_buffer[in_idx++];
  601. green = input_buffer[in_idx++];
  602. red = input_buffer[in_idx++];
  603. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  604. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  605. break;
  606. case 16:
  607. {
  608. uint8_t lsb = input_buffer[in_idx++];
  609. uint8_t msb = input_buffer[in_idx++];
  610. if (format == 0) // 555
  611. {
  612. blue = (lsb & 0x1F) << 3;
  613. green = ((msb & 0x03) << 6) | ((lsb & 0xE0) >> 2);
  614. red = (msb & 0x7C) << 1;
  615. }
  616. else // 565
  617. {
  618. blue = (lsb & 0x1F) << 3;
  619. green = ((msb & 0x07) << 5) | ((lsb & 0xE0) >> 3);
  620. red = (msb & 0xF8);
  621. }
  622. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  623. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  624. }
  625. break;
  626. case 1:
  627. case 4:
  628. case 8:
  629. {
  630. if (0 == in_bits)
  631. {
  632. in_byte = input_buffer[in_idx++];
  633. in_bits = 8;
  634. }
  635. uint16_t pn = (in_byte >> bitshift) & bitmask;
  636. whitish = mono_palette_buffer[pn / 8] & (0x1 << pn % 8);
  637. colored = color_palette_buffer[pn / 8] & (0x1 << pn % 8);
  638. in_byte <<= depth;
  639. in_bits -= depth;
  640. }
  641. break;
  642. }
  643. if (whitish)
  644. {
  645. color = GxEPD_WHITE;
  646. }
  647. else if (colored && with_color)
  648. {
  649. color = GxEPD_COLORED;
  650. }
  651. else
  652. {
  653. color = GxEPD_BLACK;
  654. }
  655. uint16_t yrow = y + (flip ? h - row - 1 : row);
  656. display.drawPixel(x + col, yrow, color);
  657. } // end pixel
  658. } // end line
  659. }
  660. Serial.print("bytes read "); Serial.println(bytes_read);
  661. }
  662. }
  663. Serial.print("loaded in "); Serial.print(millis() - startTime); Serial.println(" ms");
  664. if (!valid)
  665. {
  666. Serial.println("bitmap format not handled.");
  667. }
  668. }
  669. void showBitmapFrom_HTTP_Buffered(const char* host, const char* path, const char* filename, int16_t x, int16_t y, bool with_color)
  670. {
  671. Serial.println(); Serial.print("downloading file \""); Serial.print(filename); Serial.println("\"");
  672. display.setFullWindow();
  673. display.firstPage();
  674. do
  675. {
  676. drawBitmapFrom_HTTP_ToBuffer(host, path, filename, x, y, with_color);
  677. }
  678. while (display.nextPage());
  679. }
  680. void showBitmapFrom_HTTPS(const char* host, const char* path, const char* filename, const char* fingerprint, int16_t x, int16_t y, bool with_color)
  681. {
  682. // Use TCPClient class to create TLS connection
  683. #if USE_BearSSL
  684. BearSSL::TCPClient client;
  685. #else
  686. TCPClient client;
  687. #endif
  688. bool connection_ok = false;
  689. bool valid = false; // valid format to be handled
  690. bool flip = true; // bitmap is stored bottom-to-top
  691. uint32_t startTime = millis();
  692. if ((x >= display.width()) || (y >= display.height())) return;
  693. Serial.println(); Serial.print("downloading file \""); Serial.print(filename); Serial.println("\"");
  694. Serial.print("connecting to "); Serial.println(host);
  695. #if USE_BearSSL
  696. if (fingerprint) client.setFingerprint((uint8_t*)fingerprint);
  697. #endif
  698. if (!client.connect(host, httpsPort))
  699. {
  700. Serial.println("connection failed");
  701. return;
  702. }
  703. #if defined (ESP8266) && !USE_BearSSL
  704. if (fingerprint)
  705. {
  706. if (client.verify(fingerprint, host))
  707. {
  708. Serial.println("certificate matches");
  709. }
  710. else
  711. {
  712. Serial.println("certificate doesn't match");
  713. return;
  714. }
  715. }
  716. #endif
  717. Serial.print("requesting URL: ");
  718. Serial.println(String("https://") + host + path + filename);
  719. client.print(String("GET ") + path + filename + " HTTP/1.1\r\n" +
  720. "Host: " + host + "\r\n" +
  721. "User-Agent: GxEPD2_WiFi_Example\r\n" +
  722. "Connection: close\r\n\r\n");
  723. Serial.println("request sent");
  724. while (client.connected())
  725. {
  726. String line = client.readStringUntil('\n');
  727. if (!connection_ok)
  728. {
  729. connection_ok = line.startsWith("HTTP/1.1 200 OK");
  730. if (connection_ok) Serial.println(line);
  731. //if (!connection_ok) Serial.println(line);
  732. }
  733. if (!connection_ok) Serial.println(line);
  734. //Serial.println(line);
  735. if (line == "\r")
  736. {
  737. Serial.println("headers received");
  738. break;
  739. }
  740. }
  741. if (!connection_ok) return;
  742. // Parse BMP header
  743. if (read16(client) == 0x4D42) // BMP signature
  744. {
  745. uint32_t fileSize = read32(client);
  746. uint32_t creatorBytes = read32(client);
  747. uint32_t imageOffset = read32(client); // Start of image data
  748. uint32_t headerSize = read32(client);
  749. uint32_t width = read32(client);
  750. uint32_t height = read32(client);
  751. uint16_t planes = read16(client);
  752. uint16_t depth = read16(client); // bits per pixel
  753. uint32_t format = read32(client);
  754. uint32_t bytes_read = 7 * 4 + 3 * 2; // read so far
  755. if ((planes == 1) && ((format == 0) || (format == 3))) // uncompressed is handled, 565 also
  756. {
  757. Serial.print("File size: "); Serial.println(fileSize);
  758. Serial.print("Image Offset: "); Serial.println(imageOffset);
  759. Serial.print("Header size: "); Serial.println(headerSize);
  760. Serial.print("Bit Depth: "); Serial.println(depth);
  761. Serial.print("Image size: ");
  762. Serial.print(width);
  763. Serial.print('x');
  764. Serial.println(height);
  765. // BMP rows are padded (if needed) to 4-byte boundary
  766. uint32_t rowSize = (width * depth / 8 + 3) & ~3;
  767. if (depth < 8) rowSize = ((width * depth + 8 - depth) / 8 + 3) & ~3;
  768. if (height < 0)
  769. {
  770. height = -height;
  771. flip = false;
  772. }
  773. uint16_t w = width;
  774. uint16_t h = height;
  775. if ((x + w - 1) >= display.width()) w = display.width() - x;
  776. if ((y + h - 1) >= display.height()) h = display.height() - y;
  777. if (w <= max_row_width) // handle with direct drawing
  778. {
  779. valid = true;
  780. uint8_t bitmask = 0xFF;
  781. uint8_t bitshift = 8 - depth;
  782. uint16_t red, green, blue;
  783. bool whitish, colored;
  784. if (depth == 1) with_color = false;
  785. if (depth <= 8)
  786. {
  787. if (depth < 8) bitmask >>= depth;
  788. bytes_read += skip(client, 54 - bytes_read); //palette is always @ 54
  789. for (uint16_t pn = 0; pn < (1 << depth); pn++)
  790. {
  791. blue = client.read();
  792. green = client.read();
  793. red = client.read();
  794. client.read();
  795. bytes_read += 4;
  796. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  797. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  798. if (0 == pn % 8) mono_palette_buffer[pn / 8] = 0;
  799. mono_palette_buffer[pn / 8] |= whitish << pn % 8;
  800. if (0 == pn % 8) color_palette_buffer[pn / 8] = 0;
  801. color_palette_buffer[pn / 8] |= colored << pn % 8;
  802. }
  803. }
  804. display.clearScreen();
  805. uint32_t rowPosition = flip ? imageOffset + (height - h) * rowSize : imageOffset;
  806. //Serial.print("skip "); Serial.println(rowPosition - bytes_read);
  807. bytes_read += skip(client, rowPosition - bytes_read);
  808. for (uint16_t row = 0; row < h; row++, rowPosition += rowSize) // for each line
  809. {
  810. if (!connection_ok || !(client.connected() || client.available())) break;
  811. delay(1); // yield() to avoid WDT
  812. uint32_t in_remain = rowSize;
  813. uint32_t in_idx = 0;
  814. uint32_t in_bytes = 0;
  815. uint8_t in_byte = 0; // for depth <= 8
  816. uint8_t in_bits = 0; // for depth <= 8
  817. uint8_t out_byte = 0xFF; // white (for w%8!=0 boarder)
  818. uint8_t out_color_byte = 0xFF; // white (for w%8!=0 boarder)
  819. uint32_t out_idx = 0;
  820. for (uint16_t col = 0; col < w; col++) // for each pixel
  821. {
  822. yield();
  823. if (!connection_ok || !(client.connected() || client.available())) break;
  824. // Time to read more pixel data?
  825. if (in_idx >= in_bytes) // ok, exact match for 24bit also (size IS multiple of 3)
  826. {
  827. uint32_t get = in_remain > sizeof(input_buffer) ? sizeof(input_buffer) : in_remain;
  828. uint32_t got = read(client, input_buffer, get);
  829. while ((got < get) && connection_ok)
  830. {
  831. //Serial.print("got "); Serial.print(got); Serial.print(" < "); Serial.print(get); Serial.print(" @ "); Serial.println(bytes_read);
  832. //if ((get - got) > client.available()) delay(200); // does improve? yes, if >= 200
  833. uint32_t gotmore = read(client, input_buffer + got, get - got);
  834. got += gotmore;
  835. connection_ok = gotmore > 0;
  836. }
  837. in_bytes = got;
  838. in_remain -= got;
  839. bytes_read += got;
  840. }
  841. if (!connection_ok)
  842. {
  843. Serial.print("Error: got no more after "); Serial.print(bytes_read); Serial.println(" bytes read!");
  844. break;
  845. }
  846. switch (depth)
  847. {
  848. case 24:
  849. blue = input_buffer[in_idx++];
  850. green = input_buffer[in_idx++];
  851. red = input_buffer[in_idx++];
  852. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  853. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  854. break;
  855. case 16:
  856. {
  857. uint8_t lsb = input_buffer[in_idx++];
  858. uint8_t msb = input_buffer[in_idx++];
  859. if (format == 0) // 555
  860. {
  861. blue = (lsb & 0x1F) << 3;
  862. green = ((msb & 0x03) << 6) | ((lsb & 0xE0) >> 2);
  863. red = (msb & 0x7C) << 1;
  864. }
  865. else // 565
  866. {
  867. blue = (lsb & 0x1F) << 3;
  868. green = ((msb & 0x07) << 5) | ((lsb & 0xE0) >> 3);
  869. red = (msb & 0xF8);
  870. }
  871. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  872. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  873. }
  874. break;
  875. case 1:
  876. case 4:
  877. case 8:
  878. {
  879. if (0 == in_bits)
  880. {
  881. in_byte = input_buffer[in_idx++];
  882. in_bits = 8;
  883. }
  884. uint16_t pn = (in_byte >> bitshift) & bitmask;
  885. whitish = mono_palette_buffer[pn / 8] & (0x1 << pn % 8);
  886. colored = color_palette_buffer[pn / 8] & (0x1 << pn % 8);
  887. in_byte <<= depth;
  888. in_bits -= depth;
  889. }
  890. break;
  891. }
  892. if (whitish)
  893. {
  894. // keep white
  895. }
  896. else if (colored && with_color)
  897. {
  898. out_color_byte &= ~(0x80 >> col % 8); // colored
  899. }
  900. else
  901. {
  902. out_byte &= ~(0x80 >> col % 8); // black
  903. }
  904. if ((7 == col % 8) || (col == w - 1)) // write that last byte! (for w%8!=0 boarder)
  905. {
  906. output_row_color_buffer[out_idx] = out_color_byte;
  907. output_row_mono_buffer[out_idx++] = out_byte;
  908. out_byte = 0xFF; // white (for w%8!=0 boarder)
  909. out_color_byte = 0xFF; // white (for w%8!=0 boarder)
  910. }
  911. } // end pixel
  912. int16_t yrow = y + (flip ? h - row - 1 : row);
  913. display.writeImage(output_row_mono_buffer, output_row_color_buffer, x, yrow, w, 1);
  914. } // end line
  915. Serial.print("downloaded in "); Serial.print(millis() - startTime); Serial.println(" ms");
  916. Serial.print("bytes read "); Serial.println(bytes_read);
  917. display.refresh();
  918. }
  919. }
  920. }
  921. if (!valid)
  922. {
  923. Serial.println("bitmap format not handled.");
  924. }
  925. }
  926. void drawBitmapFrom_HTTPS_ToBuffer(const char* host, const char* path, const char* filename, const char* fingerprint, int16_t x, int16_t y, bool with_color)
  927. {
  928. // Use TCPClient class to create TLS connection
  929. #if USE_BearSSL
  930. BearSSL::TCPClient client;
  931. #else
  932. TCPClient client;
  933. #endif
  934. bool connection_ok = false;
  935. bool valid = false; // valid format to be handled
  936. bool flip = true; // bitmap is stored bottom-to-top
  937. uint32_t startTime = millis();
  938. if ((x >= display.width()) || (y >= display.height())) return;
  939. display.fillScreen(GxEPD_WHITE);
  940. Serial.print("connecting to "); Serial.println(host);
  941. #if USE_BearSSL
  942. if (fingerprint) client.setFingerprint((uint8_t*)fingerprint);
  943. #endif
  944. if (!client.connect(host, httpsPort))
  945. {
  946. Serial.println("connection failed");
  947. return;
  948. }
  949. #if defined (ESP8266) && !USE_BearSSL
  950. if (fingerprint)
  951. {
  952. if (client.verify(fingerprint, host))
  953. {
  954. Serial.println("certificate matches");
  955. }
  956. else
  957. {
  958. Serial.println("certificate doesn't match");
  959. return;
  960. }
  961. }
  962. #endif
  963. Serial.print("requesting URL: ");
  964. Serial.println(String("https://") + host + path + filename);
  965. client.print(String("GET ") + path + filename + " HTTP/1.1\r\n" +
  966. "Host: " + host + "\r\n" +
  967. "User-Agent: GxEPD2_WiFi_Example\r\n" +
  968. "Connection: close\r\n\r\n");
  969. Serial.println("request sent");
  970. while (client.connected())
  971. {
  972. String line = client.readStringUntil('\n');
  973. if (!connection_ok)
  974. {
  975. connection_ok = line.startsWith("HTTP/1.1 200 OK");
  976. if (connection_ok) Serial.println(line);
  977. //if (!connection_ok) Serial.println(line);
  978. }
  979. if (!connection_ok) Serial.println(line);
  980. //Serial.println(line);
  981. if (line == "\r")
  982. {
  983. Serial.println("headers received");
  984. break;
  985. }
  986. }
  987. if (!connection_ok) return;
  988. // Parse BMP header
  989. if (read16(client) == 0x4D42) // BMP signature
  990. {
  991. uint32_t fileSize = read32(client);
  992. uint32_t creatorBytes = read32(client);
  993. uint32_t imageOffset = read32(client); // Start of image data
  994. uint32_t headerSize = read32(client);
  995. uint32_t width = read32(client);
  996. uint32_t height = read32(client);
  997. uint16_t planes = read16(client);
  998. uint16_t depth = read16(client); // bits per pixel
  999. uint32_t format = read32(client);
  1000. uint32_t bytes_read = 7 * 4 + 3 * 2; // read so far
  1001. if ((planes == 1) && ((format == 0) || (format == 3))) // uncompressed is handled, 565 also
  1002. {
  1003. Serial.print("File size: "); Serial.println(fileSize);
  1004. Serial.print("Image Offset: "); Serial.println(imageOffset);
  1005. Serial.print("Header size: "); Serial.println(headerSize);
  1006. Serial.print("Bit Depth: "); Serial.println(depth);
  1007. Serial.print("Image size: ");
  1008. Serial.print(width);
  1009. Serial.print('x');
  1010. Serial.println(height);
  1011. // BMP rows are padded (if needed) to 4-byte boundary
  1012. uint32_t rowSize = (width * depth / 8 + 3) & ~3;
  1013. if (depth < 8) rowSize = ((width * depth + 8 - depth) / 8 + 3) & ~3;
  1014. if (height < 0)
  1015. {
  1016. height = -height;
  1017. flip = false;
  1018. }
  1019. uint16_t w = width;
  1020. uint16_t h = height;
  1021. if ((x + w - 1) >= display.width()) w = display.width() - x;
  1022. if ((y + h - 1) >= display.height()) h = display.height() - y;
  1023. //if (w <= max_row_width) // handle with direct drawing
  1024. {
  1025. valid = true;
  1026. uint8_t bitmask = 0xFF;
  1027. uint8_t bitshift = 8 - depth;
  1028. uint16_t red, green, blue;
  1029. bool whitish, colored;
  1030. if (depth == 1) with_color = false;
  1031. if (depth <= 8)
  1032. {
  1033. if (depth < 8) bitmask >>= depth;
  1034. bytes_read += skip(client, 54 - bytes_read); //palette is always @ 54
  1035. for (uint16_t pn = 0; pn < (1 << depth); pn++)
  1036. {
  1037. blue = client.read();
  1038. green = client.read();
  1039. red = client.read();
  1040. client.read();
  1041. bytes_read += 4;
  1042. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  1043. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  1044. if (0 == pn % 8) mono_palette_buffer[pn / 8] = 0;
  1045. mono_palette_buffer[pn / 8] |= whitish << pn % 8;
  1046. if (0 == pn % 8) color_palette_buffer[pn / 8] = 0;
  1047. color_palette_buffer[pn / 8] |= colored << pn % 8;
  1048. //Serial.print("0x00"); Serial.print(red, HEX); Serial.print(green, HEX); Serial.print(blue, HEX);
  1049. //Serial.print(" : "); Serial.print(whitish); Serial.print(", "); Serial.println(colored);
  1050. }
  1051. }
  1052. display.writeScreenBuffer();
  1053. uint32_t rowPosition = flip ? imageOffset + (height - h) * rowSize : imageOffset;
  1054. //Serial.print("skip "); Serial.println(rowPosition - bytes_read);
  1055. bytes_read += skip(client, rowPosition - bytes_read);
  1056. for (uint16_t row = 0; row < h; row++, rowPosition += rowSize) // for each line
  1057. {
  1058. if (!connection_ok || !(client.connected() || client.available())) break;
  1059. delay(1); // yield() to avoid WDT
  1060. uint32_t in_remain = rowSize;
  1061. uint32_t in_idx = 0;
  1062. uint32_t in_bytes = 0;
  1063. uint8_t in_byte = 0; // for depth <= 8
  1064. uint8_t in_bits = 0; // for depth <= 8
  1065. uint16_t color = GxEPD_WHITE;
  1066. for (uint16_t col = 0; col < w; col++) // for each pixel
  1067. {
  1068. yield();
  1069. if (!connection_ok || !(client.connected() || client.available())) break;
  1070. // Time to read more pixel data?
  1071. if (in_idx >= in_bytes) // ok, exact match for 24bit also (size IS multiple of 3)
  1072. {
  1073. uint32_t get = in_remain > sizeof(input_buffer) ? sizeof(input_buffer) : in_remain;
  1074. uint32_t got = read(client, input_buffer, get);
  1075. while ((got < get) && connection_ok)
  1076. {
  1077. //Serial.print("got "); Serial.print(got); Serial.print(" < "); Serial.print(get); Serial.print(" @ "); Serial.println(bytes_read);
  1078. uint32_t gotmore = read(client, input_buffer + got, get - got);
  1079. got += gotmore;
  1080. connection_ok = gotmore > 0;
  1081. }
  1082. in_bytes = got;
  1083. in_remain -= got;
  1084. bytes_read += got;
  1085. }
  1086. if (!connection_ok)
  1087. {
  1088. Serial.print("Error: got no more after "); Serial.print(bytes_read); Serial.println(" bytes read!");
  1089. break;
  1090. }
  1091. switch (depth)
  1092. {
  1093. case 24:
  1094. blue = input_buffer[in_idx++];
  1095. green = input_buffer[in_idx++];
  1096. red = input_buffer[in_idx++];
  1097. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  1098. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  1099. break;
  1100. case 16:
  1101. {
  1102. uint8_t lsb = input_buffer[in_idx++];
  1103. uint8_t msb = input_buffer[in_idx++];
  1104. if (format == 0) // 555
  1105. {
  1106. blue = (lsb & 0x1F) << 3;
  1107. green = ((msb & 0x03) << 6) | ((lsb & 0xE0) >> 2);
  1108. red = (msb & 0x7C) << 1;
  1109. }
  1110. else // 565
  1111. {
  1112. blue = (lsb & 0x1F) << 3;
  1113. green = ((msb & 0x07) << 5) | ((lsb & 0xE0) >> 3);
  1114. red = (msb & 0xF8);
  1115. }
  1116. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  1117. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  1118. }
  1119. break;
  1120. case 1:
  1121. case 4:
  1122. case 8:
  1123. {
  1124. if (0 == in_bits)
  1125. {
  1126. in_byte = input_buffer[in_idx++];
  1127. in_bits = 8;
  1128. }
  1129. uint16_t pn = (in_byte >> bitshift) & bitmask;
  1130. whitish = mono_palette_buffer[pn / 8] & (0x1 << pn % 8);
  1131. colored = color_palette_buffer[pn / 8] & (0x1 << pn % 8);
  1132. in_byte <<= depth;
  1133. in_bits -= depth;
  1134. }
  1135. break;
  1136. }
  1137. if (whitish)
  1138. {
  1139. color = GxEPD_WHITE;
  1140. }
  1141. else if (colored && with_color)
  1142. {
  1143. color = GxEPD_COLORED;
  1144. }
  1145. else
  1146. {
  1147. color = GxEPD_BLACK;
  1148. }
  1149. uint16_t yrow = y + (flip ? h - row - 1 : row);
  1150. display.drawPixel(x + col, yrow, color);
  1151. } // end pixel
  1152. } // end line
  1153. }
  1154. Serial.print("bytes read "); Serial.println(bytes_read);
  1155. }
  1156. }
  1157. Serial.print("loaded in "); Serial.print(millis() - startTime); Serial.println(" ms");
  1158. if (!valid)
  1159. {
  1160. Serial.println("bitmap format not handled.");
  1161. }
  1162. }
  1163. void showBitmapFrom_HTTPS_Buffered(const char* host, const char* path, const char* filename, const char* fingerprint, int16_t x, int16_t y, bool with_color)
  1164. {
  1165. Serial.println(); Serial.print("downloading file \""); Serial.print(filename); Serial.println("\"");
  1166. display.setFullWindow();
  1167. display.firstPage();
  1168. do
  1169. {
  1170. drawBitmapFrom_HTTPS_ToBuffer(host, path, filename, fingerprint, x, y, with_color);
  1171. }
  1172. while (display.nextPage());
  1173. }
  1174. uint16_t read16(TCPClient& client)
  1175. {
  1176. // BMP data is stored little-endian, same as Arduino.
  1177. uint16_t result;
  1178. ((uint8_t *)&result)[0] = client.read(); // LSB
  1179. ((uint8_t *)&result)[1] = client.read(); // MSB
  1180. return result;
  1181. }
  1182. uint32_t read32(TCPClient& client)
  1183. {
  1184. // BMP data is stored little-endian, same as Arduino.
  1185. uint32_t result;
  1186. ((uint8_t *)&result)[0] = client.read(); // LSB
  1187. ((uint8_t *)&result)[1] = client.read();
  1188. ((uint8_t *)&result)[2] = client.read();
  1189. ((uint8_t *)&result)[3] = client.read(); // MSB
  1190. return result;
  1191. }
  1192. #if USE_BearSSL
  1193. uint32_t skip(BearSSL::TCPClient& client, int32_t bytes)
  1194. {
  1195. int32_t remain = bytes;
  1196. uint32_t start = millis();
  1197. while ((client.connected() || client.available()) && (remain > 0))
  1198. {
  1199. if (client.available())
  1200. {
  1201. int16_t v = client.read();
  1202. remain--;
  1203. }
  1204. else delay(1);
  1205. if (millis() - start > 2000) break; // don't hang forever
  1206. }
  1207. return bytes - remain;
  1208. }
  1209. uint32_t read(BearSSL::TCPClient& client, uint8_t* buffer, int32_t bytes)
  1210. {
  1211. int32_t remain = bytes;
  1212. uint32_t start = millis();
  1213. while ((client.connected() || client.available()) && (remain > 0))
  1214. {
  1215. if (client.available())
  1216. {
  1217. int16_t v = client.read();
  1218. *buffer++ = uint8_t(v);
  1219. remain--;
  1220. }
  1221. else delay(1);
  1222. if (millis() - start > 2000) break; // don't hang forever
  1223. }
  1224. return bytes - remain;
  1225. }
  1226. #endif
  1227. uint32_t skip(TCPClient& client, int32_t bytes)
  1228. {
  1229. int32_t remain = bytes;
  1230. uint32_t start = millis();
  1231. while ((client.connected() || client.available()) && (remain > 0))
  1232. {
  1233. if (client.available())
  1234. {
  1235. int16_t v = client.read();
  1236. remain--;
  1237. }
  1238. else delay(1);
  1239. if (millis() - start > 2000) break; // don't hang forever
  1240. }
  1241. return bytes - remain;
  1242. }
  1243. uint32_t read(TCPClient& client, uint8_t* buffer, int32_t bytes)
  1244. {
  1245. int32_t remain = bytes;
  1246. uint32_t start = millis();
  1247. while ((client.connected() || client.available()) && (remain > 0))
  1248. {
  1249. if (client.available())
  1250. {
  1251. int16_t v = client.read();
  1252. *buffer++ = uint8_t(v);
  1253. remain--;
  1254. }
  1255. else delay(1);
  1256. if (millis() - start > 2000) break; // don't hang forever
  1257. }
  1258. return bytes - remain;
  1259. }