GxEPD2_WiFi_Example.ino 74 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544
  1. // GxEPD2_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: 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. // 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
  13. //
  14. // note that BMP bitmaps are drawn at physical position in physical orientation of the screen
  15. // Supporting Arduino Forum Topics:
  16. // Waveshare e-paper displays with SPI: http://forum.arduino.cc/index.php?topic=487007.0
  17. // Good Display ePaper for Arduino: https://forum.arduino.cc/index.php?topic=436411.0
  18. // mapping suggestion from Waveshare SPI e-Paper to Wemos D1 mini
  19. // BUSY -> D2, RST -> D4, DC -> D3, CS -> D8, CLK -> D5, DIN -> D7, GND -> GND, 3.3V -> 3.3V
  20. // NOTE: connect 3.3k pull-down from D8 to GND if your board or shield has level converters
  21. // NOTE for ESP8266: using SS (GPIO15) for CS may cause boot mode problems, use different pin in case, or 4k7 pull-down
  22. // mapping suggestion from Waveshare SPI e-Paper to generic ESP8266
  23. // BUSY -> GPIO4, RST -> GPIO2, DC -> GPIO0, CS -> GPIO15, CLK -> GPIO14, DIN -> GPIO13, GND -> GND, 3.3V -> 3.3V
  24. // NOTE: connect 3.3k pull-down from GPIO15 to GND if your board or shield has level converters
  25. // NOTE for ESP8266: using SS (GPIO15) for CS may cause boot mode problems, use different pin in case, or 3.3k pull-down
  26. // mapping of Waveshare e-Paper ESP8266 Driver Board, new version (see GxEPD2_boards_added.h for old version)
  27. // BUSY -> GPIO5, RST -> GPIO2, DC -> GPIO4, CS -> GPIO15, CLK -> GPIO14, DIN -> GPIO13, GND -> GND, 3.3V -> 3.3V
  28. // NOTE for ESP8266: using SS (GPIO15) for CS may cause boot mode problems, add a 3.3k pull-down in case
  29. // the e-Paper ESP8266 Driver Board should have no boot mode issue, as it doesn't use level converters
  30. // mapping suggestion for ESP32, e.g. LOLIN32, see .../variants/.../pins_arduino.h for your board
  31. // NOTE: there are variants with different pins for SPI ! CHECK SPI PINS OF YOUR BOARD
  32. // BUSY -> 4, RST -> 16, DC -> 17, CS -> SS(5), CLK -> SCK(18), DIN -> MOSI(23), GND -> GND, 3.3V -> 3.3V
  33. // mapping of Waveshare ESP32 Driver Board
  34. // BUSY -> 25, RST -> 26, DC -> 27, CS-> 15, CLK -> 13, DIN -> 14
  35. // NOTE: this board uses "unusual" SPI pins and requires re-mapping of HW SPI to these pins in SPIClass
  36. // see example GxEPD2_WS_ESP32_Driver.ino, it shows how this can be done easily
  37. // new mapping suggestion for STM32F1, e.g. STM32F103C8T6 "BluePill"
  38. // BUSY -> A1, RST -> A2, DC -> A3, CS-> A4, CLK -> A5, DIN -> A7
  39. // mapping suggestion for AVR, UNO, NANO etc.
  40. // BUSY -> 7, RST -> 9, DC -> 8, CS-> 10, CLK -> 13, DIN -> 11
  41. // mapping of Waveshare Universal e-Paper Raw Panel Driver Shield for Arduino / NUCLEO
  42. // BUSY -> 7, RST -> 8, DC -> 9, CS-> 10, CLK -> 13, DIN -> 11
  43. // mapping suggestion for Arduino MEGA
  44. // BUSY -> 7, RST -> 9, DC -> 8, CS-> 53, CLK -> 52, DIN -> 51
  45. // base class GxEPD2_GFX can be used to pass references or pointers to the display instance as parameter, uses ~1.2k more code
  46. // enable or disable GxEPD2_GFX base class
  47. #define ENABLE_GxEPD2_GFX 0
  48. // uncomment next line to use class GFX of library GFX_Root instead of Adafruit_GFX
  49. //#include <GFX.h>
  50. // Note: if you use this with ENABLE_GxEPD2_GFX 1:
  51. // uncomment it in GxEPD2_GFX.h too, or add #include <GFX.h> before any #include <GxEPD2_GFX.h>
  52. #include <GxEPD2_BW.h>
  53. #include <GxEPD2_3C.h>
  54. #include <GxEPD2_7C.h>
  55. #if defined (ESP8266)
  56. #define MAX_DISPLAY_BUFFER_SIZE (81920ul-34000ul-34000ul) // ~34000 base use, WiFiClientSecure seems to need about 34k more to work
  57. #define MAX_HEIGHT(EPD) (EPD::HEIGHT <= MAX_DISPLAY_BUFFER_SIZE / (EPD::WIDTH / 8) ? EPD::HEIGHT : MAX_DISPLAY_BUFFER_SIZE / (EPD::WIDTH / 8))
  58. #define MAX_HEIGHT_3C(EPD) (EPD::HEIGHT <= (MAX_DISPLAY_BUFFER_SIZE / 2) / (EPD::WIDTH / 8) ? EPD::HEIGHT : (MAX_DISPLAY_BUFFER_SIZE / 2) / (EPD::WIDTH / 8))
  59. #define MAX_HEIGHT_7C(EPD) (EPD::HEIGHT <= (MAX_DISPLAY_BUFFER_SIZE) / (EPD::WIDTH / 2) ? EPD::HEIGHT : (MAX_DISPLAY_BUFFER_SIZE) / (EPD::WIDTH / 2))
  60. // select one and adapt to your mapping, can use full buffer size (full HEIGHT)
  61. //GxEPD2_BW<GxEPD2_154, MAX_HEIGHT(GxEPD2_154)> display(GxEPD2_154(/*CS=D8*/ SS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4)); // GDEP015OC1 no longer available
  62. //GxEPD2_BW<GxEPD2_154_D67, MAX_HEIGHT(GxEPD2_154_D67)> display(GxEPD2_154_D67(/*CS=D8*/ SS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4)); // GDEH0154D67
  63. //GxEPD2_BW<GxEPD2_154_T8, MAX_HEIGHT(GxEPD2_154_T8)> display(GxEPD2_154_T8(/*CS=D8*/ SS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4)); // GDEW0154T8 152x152
  64. //GxEPD2_BW<GxEPD2_154_M09, MAX_HEIGHT(GxEPD2_154_M09)> display(GxEPD2_154_M09(/*CS=D8*/ SS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4)); // GDEW0154M09 200x200
  65. //GxEPD2_BW<GxEPD2_154_M10, MAX_HEIGHT(GxEPD2_154_M10)> display(GxEPD2_154_M10(/*CS=D8*/ SS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4)); // GDEW0154M10 152x152
  66. //GxEPD2_BW<GxEPD2_213, MAX_HEIGHT(GxEPD2_213)> display(GxEPD2_213(/*CS=D8*/ SS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4)); // GDE0213B1, phased out
  67. //GxEPD2_BW<GxEPD2_213_B72, MAX_HEIGHT(GxEPD2_213_B72)> display(GxEPD2_213_B72(/*CS=D8*/ SS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4)); // GDEH0213B72
  68. //GxEPD2_BW<GxEPD2_213_B73, MAX_HEIGHT(GxEPD2_213_B73)> display(GxEPD2_213_B73(/*CS=D8*/ SS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4)); // GDEH0213B73
  69. //GxEPD2_BW<GxEPD2_213_flex, MAX_HEIGHT(GxEPD2_213_flex)> display(GxEPD2_213_flex(/*CS=D8*/ SS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4)); // GDEW0213I5F
  70. //GxEPD2_BW<GxEPD2_290, MAX_HEIGHT(GxEPD2_290)> display(GxEPD2_290(/*CS=D8*/ SS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4));
  71. //GxEPD2_BW<GxEPD2_290_T5, MAX_HEIGHT(GxEPD2_290_T5)> display(GxEPD2_290_T5(/*CS=D8*/ SS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4)); // GDEW029T5
  72. //GxEPD2_BW<GxEPD2_260, MAX_HEIGHT(GxEPD2_260)> display(GxEPD2_260(/*CS=D8*/ SS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4));
  73. //GxEPD2_BW<GxEPD2_270, MAX_HEIGHT(GxEPD2_270)> display(GxEPD2_270(/*CS=D8*/ SS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4));
  74. //GxEPD2_BW<GxEPD2_371, MAX_HEIGHT(GxEPD2_371)> display(GxEPD2_371(/*CS=D8*/ SS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4));
  75. //GxEPD2_BW<GxEPD2_420, MAX_HEIGHT(GxEPD2_420)> display(GxEPD2_420(/*CS=D8*/ SS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4));
  76. //GxEPD2_BW<GxEPD2_583, MAX_HEIGHT(GxEPD2_583)> display(GxEPD2_583(/*CS=D8*/ SS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4));
  77. //GxEPD2_BW<GxEPD2_583_T8, MAX_HEIGHT(GxEPD2_583_T8)> display(GxEPD2_583_T8(/*CS=D8*/ SS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4));
  78. //GxEPD2_BW<GxEPD2_750, MAX_HEIGHT(GxEPD2_750)> display(GxEPD2_750(/*CS=D8*/ SS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4));
  79. //GxEPD2_BW<GxEPD2_750_T7, MAX_HEIGHT(GxEPD2_750_T7)> display(GxEPD2_750_T7(/*CS=D8*/ SS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4)); // GDEW075T7 800x480
  80. // 3-color e-papers
  81. //GxEPD2_3C<GxEPD2_154c, MAX_HEIGHT_3C(GxEPD2_154c)> display(GxEPD2_154c(/*CS=D8*/ SS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4)); // GDEW0154Z04 no longer available
  82. //GxEPD2_3C<GxEPD2_154_Z90c, MAX_HEIGHT_3C(GxEPD2_154_Z90c)> display(GxEPD2_154_Z90c(/*CS=D8*/ SS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4)); // GDEH0154Z90
  83. //GxEPD2_3C<GxEPD2_213c, MAX_HEIGHT_3C(GxEPD2_213c)> display(GxEPD2_213c(/*CS=D8*/ SS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4));
  84. //GxEPD2_3C<GxEPD2_290c, MAX_HEIGHT_3C(GxEPD2_290c)> display(GxEPD2_290c(/*CS=D8*/ SS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4));
  85. //GxEPD2_3C<GxEPD2_270c, MAX_HEIGHT_3C(GxEPD2_270c)> display(GxEPD2_270c(/*CS=D8*/ SS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4));
  86. //GxEPD2_3C<GxEPD2_420c, MAX_HEIGHT_3C(GxEPD2_420c)> display(GxEPD2_420c(/*CS=D8*/ SS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4));
  87. //GxEPD2_3C<GxEPD2_583c, MAX_HEIGHT_3C(GxEPD2_583c)> display(GxEPD2_583c(/*CS=D8*/ SS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4));
  88. //GxEPD2_3C<GxEPD2_750c, MAX_HEIGHT_3C(GxEPD2_750c)> display(GxEPD2_750c(/*CS=D8*/ SS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4));
  89. //GxEPD2_3C<GxEPD2_750c_Z08, MAX_HEIGHT_3C(GxEPD2_750c_Z08)> display(GxEPD2_750c_Z08(/*CS=D8*/ SS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4)); // GDEW075Z08 800x480
  90. //GxEPD2_3C<GxEPD2_750c_Z90, MAX_HEIGHT_3C(GxEPD2_750c_Z90)> display(GxEPD2_750c_Z90(/*CS=D8*/ SS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4)); // GDEH075Z90 880x528
  91. // 7-color e-paper
  92. //GxEPD2_3C<GxEPD2_565c, MAX_HEIGHT_7C(GxEPD2_565c)> display(GxEPD2_565c(/*CS=D8*/ SS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4)); // Waveshare 5.65" 7-color (3C graphics)
  93. //GxEPD2_7C<GxEPD2_565c, MAX_HEIGHT_7C(GxEPD2_565c)> display(GxEPD2_565c(/*CS=D8*/ SS, /*DC=D3*/ 0, /*RST=D4*/ 2, /*BUSY=D2*/ 4)); // Waveshare 5.65" 7-color
  94. // grey levels parallel IF e-papers on Waveshare e-Paper IT8951 Driver HAT
  95. // HRDY -> 4, RST -> 2, CS -> SS(15), SCK -> SCK(14), MOSI -> MOSI(D7(13)), MISO -> MISO(D6(12)), GND -> GND, 5V -> 5V
  96. // note: 5V supply needs to be exact and strong; 5V pin of USB powered Wemos D1 mini doesn't work!
  97. //GxEPD2_BW<GxEPD2_it60, MAX_HEIGHT(GxEPD2_it60)> display(GxEPD2_it60(/*CS=5*/ SS, /*DC=*/ 0, /*RST=*/ 2, /*BUSY=*/ 4));
  98. //GxEPD2_BW<GxEPD2_it60_1448x1072, MAX_HEIGHT(GxEPD2_it60_1448x1072)> display(GxEPD2_it60_1448x1072(/*CS=5*/ SS, /*DC=*/ 0, /*RST=*/ 2, /*BUSY=*/ 4));
  99. // ***** for mapping of Waveshare e-Paper ESP8266 Driver Board *****
  100. // select one , can use full buffer size (full HEIGHT)
  101. //GxEPD2_BW<GxEPD2_154, MAX_HEIGHT(GxEPD2_154)> display(GxEPD2_154(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=2*/ 2, /*BUSY=5*/ 5)); // GDEP015OC1 no longer available
  102. //GxEPD2_BW<GxEPD2_154_D67, MAX_HEIGHT(GxEPD2_154_D67)> display(GxEPD2_154_D67(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=2*/ 2, /*BUSY=5*/ 5)); // GDEH0154D67
  103. //GxEPD2_BW<GxEPD2_154_T8, MAX_HEIGHT(GxEPD2_154_T8)> display(GxEPD2_154_T8(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=2*/ 2, /*BUSY=5*/ 5)); // GDEW0154T8 152x152
  104. //GxEPD2_BW<GxEPD2_154_M09, MAX_HEIGHT(GxEPD2_154_M09)> display(GxEPD2_154_M09(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=2*/ 2, /*BUSY=5*/ 5)); // GDEW0154M09 200x200
  105. //GxEPD2_BW<GxEPD2_154_M10, MAX_HEIGHT(GxEPD2_154_M10)> display(GxEPD2_154_M10(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=2*/ 2, /*BUSY=5*/ 5)); // GDEW0154M10 152x152
  106. //GxEPD2_BW<GxEPD2_213, MAX_HEIGHT(GxEPD2_213)> display(GxEPD2_213(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=2*/ 2, /*BUSY=5*/ 5)); // GDE0213B1, phased out
  107. //GxEPD2_BW<GxEPD2_213_B72, MAX_HEIGHT(GxEPD2_213_B72)> display(GxEPD2_213_B72(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=2*/ 2, /*BUSY=5*/ 5)); // GDEH0213B72
  108. //GxEPD2_BW<GxEPD2_213_B73, MAX_HEIGHT(GxEPD2_213_B73)> display(GxEPD2_213_B73(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=2*/ 2, /*BUSY=5*/ 5)); // GDEH0213B73
  109. //GxEPD2_BW<GxEPD2_213_flex, MAX_HEIGHT(GxEPD2_213_flex)> display(GxEPD2_213_flex(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=2*/ 2, /*BUSY=5*/ 5)); // GDEW0213I5F
  110. //GxEPD2_BW<GxEPD2_290, MAX_HEIGHT(GxEPD2_290)> display(GxEPD2_290(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=2*/ 2, /*BUSY=5*/ 5));
  111. //GxEPD2_BW<GxEPD2_290_T5, MAX_HEIGHT(GxEPD2_290_T5)> display(GxEPD2_290_T5(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=2*/ 2, /*BUSY=5*/ 5)); // GDEW029T5
  112. //GxEPD2_BW<GxEPD2_260, MAX_HEIGHT(GxEPD2_260)> display(GxEPD2_260(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=2*/ 2, /*BUSY=5*/ 5));
  113. //GxEPD2_BW<GxEPD2_270, MAX_HEIGHT(GxEPD2_270)> display(GxEPD2_270(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=2*/ 2, /*BUSY=5*/ 5));
  114. //GxEPD2_BW<GxEPD2_371, MAX_HEIGHT(GxEPD2_371)> display(GxEPD2_371(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=2*/ 2, /*BUSY=5*/ 5));
  115. //GxEPD2_BW<GxEPD2_420, MAX_HEIGHT(GxEPD2_420)> display(GxEPD2_420(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=2*/ 2, /*BUSY=5*/ 5));
  116. //GxEPD2_BW<GxEPD2_583, MAX_HEIGHT(GxEPD2_583)> display(GxEPD2_583(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=2*/ 2, /*BUSY=5*/ 5));
  117. //GxEPD2_BW<GxEPD2_583_T8, MAX_HEIGHT(GxEPD2_583_T8)> display(GxEPD2_583_T8(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=2*/ 2, /*BUSY=5*/ 5));
  118. //GxEPD2_BW<GxEPD2_750, MAX_HEIGHT(GxEPD2_750)> display(GxEPD2_750(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=2*/ 2, /*BUSY=5*/ 5));
  119. //GxEPD2_BW<GxEPD2_750_T7, MAX_HEIGHT(GxEPD2_750_T7)> display(GxEPD2_750_T7(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=2*/ 2, /*BUSY=5*/ 5)); // GDEW075T7 800x480
  120. // 3-color e-papers
  121. //GxEPD2_3C<GxEPD2_154c, MAX_HEIGHT_3C(GxEPD2_154c)> display(GxEPD2_154c(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=2*/ 2, /*BUSY=5*/ 5)); // GDEW0154Z04 no longer available
  122. //GxEPD2_3C<GxEPD2_154_Z90c, MAX_HEIGHT_3C(GxEPD2_154_Z90c)> display(GxEPD2_154_Z90c(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=2*/ 2, /*BUSY=5*/ 5)); // GDEH0154Z90
  123. //GxEPD2_3C<GxEPD2_213c, MAX_HEIGHT_3C(GxEPD2_213c)> display(GxEPD2_213c(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=2*/ 2, /*BUSY=5*/ 5));
  124. //GxEPD2_3C<GxEPD2_290c, MAX_HEIGHT_3C(GxEPD2_290c)> display(GxEPD2_290c(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=2*/ 2, /*BUSY=5*/ 5));
  125. //GxEPD2_3C<GxEPD2_270c, MAX_HEIGHT_3C(GxEPD2_270c)> display(GxEPD2_270c(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=2*/ 2, /*BUSY=5*/ 5));
  126. //GxEPD2_3C<GxEPD2_420c, MAX_HEIGHT_3C(GxEPD2_420c)> display(GxEPD2_420c(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=2*/ 2, /*BUSY=5*/ 5));
  127. //GxEPD2_3C<GxEPD2_583c, MAX_HEIGHT_3C(GxEPD2_583c)> display(GxEPD2_583c(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=2*/ 2, /*BUSY=5*/ 5));
  128. //GxEPD2_3C<GxEPD2_750c, MAX_HEIGHT_3C(GxEPD2_750c)> display(GxEPD2_750c(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=2*/ 2, /*BUSY=5*/ 5));
  129. //GxEPD2_3C<GxEPD2_750c_Z08, MAX_HEIGHT_3C(GxEPD2_750c_Z08)> display(GxEPD2_750c_Z08(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=2*/ 2, /*BUSY=5*/ 5)); // GDEW075Z08 800x480
  130. //GxEPD2_3C<GxEPD2_750c_Z90, MAX_HEIGHT_3C(GxEPD2_750c_Z90)> display(GxEPD2_750c_Z90(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=2*/ 2, /*BUSY=5*/ 5)); // GDEH075Z90 880x528
  131. // 7-color e-paper
  132. //GxEPD2_7C<GxEPD2_565c, MAX_HEIGHT_7C(GxEPD2_565c)> display(GxEPD2_565c(/*CS=15*/ SS, /*DC=4*/ 4, /*RST=2*/ 2, /*BUSY=5*/ 5)); // Waveshare 5.65" 7-color
  133. #endif
  134. #if defined(ESP32)
  135. // select one and adapt to your mapping, can use full buffer size (full HEIGHT)
  136. //GxEPD2_BW<GxEPD2_154, GxEPD2_154::HEIGHT> display(GxEPD2_154(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4)); // GDEP015OC1 no longer available
  137. //GxEPD2_BW<GxEPD2_154_D67, GxEPD2_154_D67::HEIGHT> display(GxEPD2_154_D67(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4)); // GDEH0154D67
  138. //GxEPD2_BW<GxEPD2_154_T8, GxEPD2_154_T8::HEIGHT> display(GxEPD2_154_T8(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4)); // GDEW0154T8 152x152
  139. //GxEPD2_BW<GxEPD2_154_M09, GxEPD2_154_M09::HEIGHT> display(GxEPD2_154_M09(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4)); // GDEW0154M09 200x200
  140. //GxEPD2_BW<GxEPD2_154_M10, GxEPD2_154_M10::HEIGHT> display(GxEPD2_154_M10(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4)); // GDEW0154M10 152x152
  141. //GxEPD2_BW<GxEPD2_213, GxEPD2_213::HEIGHT> display(GxEPD2_213(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4)); // GDE0213B1, phased out
  142. //GxEPD2_BW<GxEPD2_213_B72, GxEPD2_213_B72::HEIGHT> display(GxEPD2_213_B72(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4)); // GDEH0213B72
  143. //GxEPD2_BW<GxEPD2_213_B73, GxEPD2_213_B73::HEIGHT> display(GxEPD2_213_B73(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4)); // GDEH0213B73
  144. //GxEPD2_BW<GxEPD2_213_flex, GxEPD2_213_flex::HEIGHT> display(GxEPD2_213_flex(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4)); // GDEW0213I5F
  145. //GxEPD2_BW<GxEPD2_290, GxEPD2_290::HEIGHT> display(GxEPD2_290(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4));
  146. //GxEPD2_BW<GxEPD2_290_T5, GxEPD2_290_T5::HEIGHT> display(GxEPD2_290_T5(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4)); // GDEW029T5
  147. //GxEPD2_BW<GxEPD2_260, GxEPD2_260::HEIGHT> display(GxEPD2_260(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4));
  148. //GxEPD2_BW<GxEPD2_270, GxEPD2_270::HEIGHT> display(GxEPD2_270(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4));
  149. //GxEPD2_BW<GxEPD2_371, GxEPD2_371::HEIGHT> display(GxEPD2_371(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4));
  150. //GxEPD2_BW<GxEPD2_420, GxEPD2_420::HEIGHT> display(GxEPD2_420(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4));
  151. //GxEPD2_BW<GxEPD2_583, GxEPD2_583::HEIGHT> display(GxEPD2_583(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4));
  152. //GxEPD2_BW<GxEPD2_583_T8, GxEPD2_583_T8::HEIGHT> display(GxEPD2_583_T8(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4));
  153. //GxEPD2_BW<GxEPD2_750, GxEPD2_750::HEIGHT> display(GxEPD2_750(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4));
  154. //GxEPD2_BW<GxEPD2_750_T7, GxEPD2_750_T7::HEIGHT> display(GxEPD2_750_T7(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4)); // GDEW075T7 800x480
  155. // 3-color e-papers
  156. //GxEPD2_3C<GxEPD2_154c, GxEPD2_154c::HEIGHT> display(GxEPD2_154c(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4)); // GDEW0154Z04 no longer available
  157. //GxEPD2_3C<GxEPD2_154_Z90c, GxEPD2_154_Z90c::HEIGHT> display(GxEPD2_154_Z90c(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4)); // GDEH0154Z90
  158. //GxEPD2_3C<GxEPD2_213c, GxEPD2_213c::HEIGHT> display(GxEPD2_213c(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4));
  159. //GxEPD2_3C<GxEPD2_290c, GxEPD2_290c::HEIGHT> display(GxEPD2_290c(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4));
  160. //GxEPD2_3C<GxEPD2_270c, GxEPD2_270c::HEIGHT> display(GxEPD2_270c(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4));
  161. //GxEPD2_3C<GxEPD2_420c, GxEPD2_420c::HEIGHT> display(GxEPD2_420c(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4));
  162. //GxEPD2_3C<GxEPD2_583c, GxEPD2_583c::HEIGHT> display(GxEPD2_583c(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4));
  163. //GxEPD2_3C<GxEPD2_750c, GxEPD2_750c::HEIGHT> display(GxEPD2_750c(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4));
  164. //GxEPD2_3C<GxEPD2_750c_Z08, GxEPD2_750c_Z08::HEIGHT> display(GxEPD2_750c_Z08(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4)); // GDEW075Z08 800x480
  165. //GxEPD2_3C<GxEPD2_750c_Z90, GxEPD2_750c_Z90::HEIGHT / 2> display(GxEPD2_750c_Z90(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4)); // GDEH075Z90 880x528
  166. // 7-color e-paper
  167. //GxEPD2_3C < GxEPD2_565c, GxEPD2_565c::HEIGHT / 2 > display(GxEPD2_565c(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4)); // Waveshare 5.65" 7-color (3C graphics)
  168. //GxEPD2_7C < GxEPD2_565c, GxEPD2_565c::HEIGHT / 2 > display(GxEPD2_565c(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4)); // Waveshare 5.65" 7-color
  169. // grey levels parallel IF e-papers on Waveshare e-Paper IT8951 Driver HAT
  170. // HRDY -> 4, RST -> 16, CS -> SS(5), SCK -> SCK(18), MOSI -> MOSI(23), MISO -> MISO(19), GND -> GND, 5V -> 5V
  171. // note: 5V supply needs to be exact and strong; 5V over diode from USB (e.g. Wemos D1 mini) doesn't work!
  172. //GxEPD2_BW<GxEPD2_it60, GxEPD2_it60::HEIGHT> display(GxEPD2_it60(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4));
  173. //GxEPD2_BW < GxEPD2_it60_1448x1072, GxEPD2_it60_1448x1072::HEIGHT / 4 > display(GxEPD2_it60_1448x1072(/*CS=5*/ SS, /*DC=*/ 17, /*RST=*/ 16, /*BUSY=*/ 4));
  174. // Waveshare 12.48 b/w SPI display board and frame or Good Display 12.48 b/w panel GDEW1248T3
  175. // general constructor for use with all parameters, e.g. for Waveshare ESP32 driver board mounted on connection board
  176. //GxEPD2_BW < GxEPD2_1248, GxEPD2_1248::HEIGHT / 4 >
  177. //display(GxEPD2_1248(/*sck=*/ 13, /*miso=*/ 12, /*mosi=*/ 14, /*cs_m1=*/ 23, /*cs_s1=*/ 22, /*cs_m2=*/ 16, /*cs_s2=*/ 19,
  178. // /*dc1=*/ 25, /*dc2=*/ 17, /*rst1=*/ 33, /*rst2=*/ 5, /*busy_m1=*/ 32, /*busy_s1=*/ 26, /*busy_m2=*/ 18, /*busy_s2=*/ 4));
  179. #endif
  180. #if defined (ESP8266)
  181. #include <ESP8266WiFi.h>
  182. #define USE_BearSSL true
  183. #endif
  184. #include <WiFiClient.h>
  185. #include <WiFiClientSecure.h>
  186. const char* ssid = "........";
  187. const char* password = "........";
  188. const int httpPort = 80;
  189. const int httpsPort = 443;
  190. const char* fp_api_github_com = "df b2 29 c6 a6 38 1a 59 9d c9 ad 92 2d 26 f5 3c 83 8f a5 87"; // as of 25.11.2020
  191. const char* fp_github_com = "5f 3f 7a c2 56 9f 50 a4 66 76 47 c6 a1 8c a0 07 aa ed bb 8e"; // as of 25.11.2020
  192. const char* fp_rawcontent = "70 94 de dd e6 c4 69 48 3a 92 70 a1 48 56 78 2d 18 64 e0 b7"; // as of 25.11.2020
  193. // how to find the certificate was not easy. finally I found it using Mozilla Firefox.
  194. // opened one of the bitmaps, e.g. https://raw.githubusercontent.com/ZinggJM/GxEPD2/master/extras/bitmaps/logo200x200.bmp
  195. // clicked the lock symbol, Connection secure clicked >, show connection details, clicked More Information, clicked View Certificate, clicked Download PEM (chain),
  196. // selected Open with Notepad. Copied the middle certificate and editted to the following format:
  197. // the number of characters per line is of no importance, but the 3 \n are needed
  198. const char* certificate_rawcontent =
  199. "-----BEGIN CERTIFICATE-----\n"
  200. "MIIEsTCCA5mgAwIBAgIQBOHnpNxc8vNtwCtCuF0VnzANBgkqhkiG9w0BAQsFADBsMQswCQYDVQQGEwJVUzEVMBMGA1UEChMMRGln"
  201. "aUNlcnQgSW5jMRkwFwYDVQQLExB3d3cuZGlnaWNlcnQuY29tMSswKQYDVQQDEyJEaWdpQ2VydCBIaWdoIEFzc3VyYW5jZSBFViBS"
  202. "b290IENBMB4XDTEzMTAyMjEyMDAwMFoXDTI4MTAyMjEyMDAwMFowcDELMAkGA1UEBhMCVVMxFTATBgNVBAoTDERpZ2lDZXJ0IElu"
  203. "YzEZMBcGA1UECxMQd3d3LmRpZ2ljZXJ0LmNvbTEvMC0GA1UEAxMmRGlnaUNlcnQgU0hBMiBIaWdoIEFzc3VyYW5jZSBTZXJ2ZXIg"
  204. "Q0EwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQC24C/CJAbIbQRf1+8KZAayfSImZRauQkCbztyfn3YHPsMwVYcZuU+U"
  205. "DlqUH1VWtMICKq/QmO4LQNfE0DtyyBSe75CxEamu0si4QzrZCwvV1ZX1QK/IHe1NnF9Xt4ZQaJn1itrSxwUfqJfJ3KSxgoQtxq2l"
  206. "nMcZgqaFD15EWCo3j/018QsIJzJa9buLnqS9UdAn4t07QjOjBSjEuyjMmqwrIw14xnvmXnG3Sj4I+4G3FhahnSMSTeXXkgisdaSc"
  207. "us0Xsh5ENWV/UyU50RwKmmMbGZJ0aAo3wsJSSMs5WqK24V3B3aAguCGikyZvFEohQcftbZvySC/zA/WiaJJTL17jAgMBAAGjggFJ"
  208. "MIIBRTASBgNVHRMBAf8ECDAGAQH/AgEAMA4GA1UdDwEB/wQEAwIBhjAdBgNVHSUEFjAUBggrBgEFBQcDAQYIKwYBBQUHAwIwNAYI"
  209. "KwYBBQUHAQEEKDAmMCQGCCsGAQUFBzABhhhodHRwOi8vb2NzcC5kaWdpY2VydC5jb20wSwYDVR0fBEQwQjBAoD6gPIY6aHR0cDov"
  210. "L2NybDQuZGlnaWNlcnQuY29tL0RpZ2lDZXJ0SGlnaEFzc3VyYW5jZUVWUm9vdENBLmNybDA9BgNVHSAENjA0MDIGBFUdIAAwKjAo"
  211. "BggrBgEFBQcCARYcaHR0cHM6Ly93d3cuZGlnaWNlcnQuY29tL0NQUzAdBgNVHQ4EFgQUUWj/kK8CB3U8zNllZGKiErhZcjswHwYD"
  212. "VR0jBBgwFoAUsT7DaQP4v0cB1JgmGggC72NkK8MwDQYJKoZIhvcNAQELBQADggEBABiKlYkD5m3fXPwdaOpKj4PWUS+Na0QWnqxj"
  213. "9dJubISZi6qBcYRb7TROsLd5kinMLYBq8I4g4Xmk/gNHE+r1hspZcX30BJZr01lYPf7TMSVcGDiEo+afgv2MW5gxTs14nhr9hctJ"
  214. "qvIni5ly/D6q1UEL2tU2ob8cbkdJf17ZSHwD2f2LSaCYJkJA69aSEaRkCldUxPUd1gJea6zuxICaEnL6VpPX/78whQYwvwt/Tv9X"
  215. "BZ0k7YXDK/umdaisLRbvfXknsuvCnQsH6qqF0wGjIChBWUMo0oHjqvbsezt3tkBigAVBRQHvFwY+3sAzm2fTYS5yh+Rp/BIAV0AecPUeybQ=\n"
  216. "-----END CERTIFICATE-----\n";
  217. const char* host_rawcontent = "raw.githubusercontent.com";
  218. const char* path_rawcontent = "/ZinggJM/GxEPD2/master/extras/bitmaps/";
  219. const char* path_prenticedavid = "/prenticedavid/MCUFRIEND_kbv/master/extras/bitmaps/";
  220. const char* path_waveshare_c = "/waveshare/e-Paper/master/RaspberryPi%26JetsonNano/c/pic/";
  221. const char* path_waveshare_py = "/waveshare/e-Paper/master/RaspberryPi%26JetsonNano/python/pic/";
  222. // note that BMP bitmaps are drawn at physical position in physical orientation of the screen
  223. void showBitmapFrom_HTTP(const char* host, const char* path, const char* filename, int16_t x, int16_t y, bool with_color = true);
  224. 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,
  225. const char* certificate = certificate_rawcontent);
  226. // draws BMP bitmap according to set orientation
  227. void showBitmapFrom_HTTP_Buffered(const char* host, const char* path, const char* filename, int16_t x, int16_t y, bool with_color = true);
  228. 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,
  229. const char* certificate = certificate_rawcontent);
  230. void setup()
  231. {
  232. Serial.begin(115200);
  233. Serial.println();
  234. Serial.println("GxEPD2_WiFi_Example");
  235. display.init(115200);
  236. #ifdef RE_INIT_NEEDED
  237. WiFi.persistent(true);
  238. WiFi.mode(WIFI_STA); // switch off AP
  239. WiFi.setAutoConnect(true);
  240. WiFi.setAutoReconnect(true);
  241. WiFi.disconnect();
  242. #endif
  243. if (!WiFi.getAutoConnect() || ( WiFi.getMode() != WIFI_STA) || ((WiFi.SSID() != ssid) && String(ssid) != "........"))
  244. {
  245. Serial.println();
  246. Serial.print("WiFi.getAutoConnect()=");
  247. Serial.println(WiFi.getAutoConnect());
  248. Serial.print("WiFi.SSID()=");
  249. Serial.println(WiFi.SSID());
  250. WiFi.mode(WIFI_STA); // switch off AP
  251. Serial.print("Connecting to ");
  252. Serial.println(ssid);
  253. WiFi.begin(ssid, password);
  254. }
  255. int ConnectTimeout = 30; // 15 seconds
  256. while (WiFi.status() != WL_CONNECTED)
  257. {
  258. delay(500);
  259. Serial.print(".");
  260. Serial.print(WiFi.status());
  261. if (--ConnectTimeout <= 0)
  262. {
  263. Serial.println();
  264. Serial.println("WiFi connect timeout");
  265. return;
  266. }
  267. }
  268. Serial.println();
  269. Serial.println("WiFi connected");
  270. // Print the IP address
  271. Serial.println(WiFi.localIP());
  272. if ((display.epd2.panel == GxEPD2::GDEW0154Z04) || (display.epd2.panel == GxEPD2::ACeP565) || false)
  273. {
  274. drawBitmapsBuffered_200x200();
  275. drawBitmapsBuffered_other();
  276. drawBitmapsBuffered_7C();
  277. }
  278. else
  279. {
  280. drawBitmaps_200x200();
  281. drawBitmaps_other();
  282. }
  283. //drawBitmaps_test();
  284. //drawBitmapsBuffered_test();
  285. Serial.println("GxEPD2_WiFi_Example done");
  286. }
  287. void loop(void)
  288. {
  289. }
  290. void drawBitmaps_200x200()
  291. {
  292. int16_t x = (display.width() - 200) / 2;
  293. int16_t y = (display.height() - 200) / 2;
  294. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "logo200x200.bmp", fp_rawcontent, x, y);
  295. delay(2000);
  296. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "first200x200.bmp", fp_rawcontent, x, y);
  297. delay(2000);
  298. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "second200x200.bmp", fp_rawcontent, x, y);
  299. delay(2000);
  300. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "third200x200.bmp", fp_rawcontent, x, y);
  301. delay(2000);
  302. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "fourth200x200.bmp", fp_rawcontent, x, y);
  303. delay(2000);
  304. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "fifth200x200.bmp", fp_rawcontent, x, y);
  305. delay(2000);
  306. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "sixth200x200.bmp", fp_rawcontent, x, y);
  307. delay(2000);
  308. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "seventh200x200.bmp", fp_rawcontent, x, y);
  309. delay(2000);
  310. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "eighth200x200.bmp", fp_rawcontent, x, y);
  311. delay(2000);
  312. }
  313. void drawBitmaps_other()
  314. {
  315. int16_t w2 = display.width() / 2;
  316. int16_t h2 = display.height() / 2;
  317. showBitmapFrom_HTTP("www.squix.org", "/blog/wunderground/", "chanceflurries.bmp", w2 - 50, h2 - 50, false);
  318. delay(2000);
  319. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "betty_1.bmp", fp_rawcontent, w2 - 100, h2 - 160);
  320. delay(2000);
  321. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "betty_4.bmp", fp_rawcontent, w2 - 102, h2 - 126);
  322. delay(2000);
  323. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "marilyn_240x240x8.bmp", fp_rawcontent, w2 - 120, h2 - 120);
  324. delay(2000);
  325. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "miniwoof.bmp", fp_rawcontent, w2 - 60, h2 - 80);
  326. delay(2000);
  327. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "test.bmp", fp_rawcontent, w2 - 120, h2 - 160);
  328. delay(2000);
  329. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "tiger.bmp", fp_rawcontent, w2 - 160, h2 - 120);
  330. delay(2000);
  331. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "tiger_178x160x4.bmp", fp_rawcontent, w2 - 89, h2 - 80);
  332. delay(2000);
  333. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "tiger_240x317x4.bmp", fp_rawcontent, w2 - 120, h2 - 160);
  334. delay(2000);
  335. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "tiger_320x200x24.bmp", fp_rawcontent, w2 - 160, h2 - 100);
  336. delay(2000);
  337. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "tiger16T.bmp", fp_rawcontent, w2 - 160, h2 - 100);
  338. delay(2000);
  339. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "woof.bmp", fp_rawcontent, w2 - 100, h2 - 100);
  340. delay(2000);
  341. }
  342. void drawBitmaps_test()
  343. {
  344. int16_t w2 = display.width() / 2;
  345. int16_t h2 = display.height() / 2;
  346. showBitmapFrom_HTTPS(host_rawcontent, path_prenticedavid, "betty_4.bmp", fp_rawcontent, w2 - 102, h2 - 126);
  347. delay(2000);
  348. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "output5.bmp", fp_rawcontent, 0, 0);
  349. delay(2000);
  350. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "output6.bmp", fp_rawcontent, 0, 0);
  351. delay(2000);
  352. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "tractor_1.bmp", fp_rawcontent, 0, 0);
  353. delay(2000);
  354. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "tractor_4.bmp", fp_rawcontent, 0, 0);
  355. delay(2000);
  356. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "tractor_8.bmp", fp_rawcontent, 0, 0);
  357. delay(2000);
  358. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "tractor_11.bmp", fp_rawcontent, 0, 0);
  359. delay(2000);
  360. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "tractor_44.bmp", fp_rawcontent, 0, 0);
  361. delay(2000);
  362. showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "tractor_88.bmp", fp_rawcontent, 0, 0);
  363. delay(2000);
  364. //showBitmapFrom_HTTPS(host_rawcontent, path_rawcontent, "liberato640x384.bmp", fp_rawcontent, 0, 0);
  365. //delay(2000);
  366. }
  367. void drawBitmapsBuffered_200x200()
  368. {
  369. int16_t x = (display.width() - 200) / 2;
  370. int16_t y = (display.height() - 200) / 2;
  371. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_rawcontent, "logo200x200.bmp", fp_rawcontent, x, y);
  372. delay(2000);
  373. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_rawcontent, "first200x200.bmp", fp_rawcontent, x, y);
  374. delay(2000);
  375. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_rawcontent, "second200x200.bmp", fp_rawcontent, x, y);
  376. delay(2000);
  377. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_rawcontent, "third200x200.bmp", fp_rawcontent, x, y);
  378. delay(2000);
  379. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_rawcontent, "fourth200x200.bmp", fp_rawcontent, x, y);
  380. delay(2000);
  381. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_rawcontent, "fifth200x200.bmp", fp_rawcontent, x, y);
  382. delay(2000);
  383. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_rawcontent, "sixth200x200.bmp", fp_rawcontent, x, y);
  384. delay(2000);
  385. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_rawcontent, "seventh200x200.bmp", fp_rawcontent, x, y);
  386. delay(2000);
  387. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_rawcontent, "eighth200x200.bmp", fp_rawcontent, x, y);
  388. delay(2000);
  389. }
  390. void drawBitmapsBuffered_other()
  391. {
  392. int16_t w2 = display.width() / 2;
  393. int16_t h2 = display.height() / 2;
  394. showBitmapFrom_HTTP_Buffered("www.squix.org", "/blog/wunderground/", "chanceflurries.bmp", w2 - 50, h2 - 50, false);
  395. delay(2000);
  396. //showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "betty_1.bmp", fp_rawcontent, w2 - 100, h2 - 160);
  397. delay(2000);
  398. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "betty_4.bmp", fp_rawcontent, w2 - 102, h2 - 126);
  399. delay(2000);
  400. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "marilyn_240x240x8.bmp", fp_rawcontent, w2 - 120, h2 - 120);
  401. delay(2000);
  402. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "miniwoof.bmp", fp_rawcontent, w2 - 60, h2 - 80);
  403. delay(2000);
  404. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "test.bmp", fp_rawcontent, w2 - 120, h2 - 160);
  405. delay(2000);
  406. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "tiger.bmp", fp_rawcontent, w2 - 160, h2 - 120);
  407. delay(2000);
  408. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "tiger_178x160x4.bmp", fp_rawcontent, w2 - 89, h2 - 80);
  409. delay(2000);
  410. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "tiger_240x317x4.bmp", fp_rawcontent, w2 - 120, h2 - 160);
  411. delay(2000);
  412. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "tiger_320x200x24.bmp", fp_rawcontent, w2 - 160, h2 - 100);
  413. delay(2000);
  414. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "tiger16T.bmp", fp_rawcontent, w2 - 160, h2 - 100);
  415. delay(2000);
  416. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "woof.bmp", fp_rawcontent, w2 - 100, h2 - 100);
  417. delay(2000);
  418. }
  419. void drawBitmapsBuffered_7C()
  420. {
  421. if (display.epd2.panel == GxEPD2::ACeP565)
  422. {
  423. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_waveshare_c, "5in65f.bmp", fp_rawcontent, 0, 0);
  424. delay(2000);
  425. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_waveshare_c, "5in65f2.bmp", fp_rawcontent, 0, 0);
  426. delay(2000);
  427. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_waveshare_c, "5in65f3.bmp", fp_rawcontent, 0, 0);
  428. delay(2000);
  429. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_waveshare_c, "5in65f4.bmp", fp_rawcontent, 0, 0);
  430. delay(2000);
  431. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_waveshare_py, "5in65f4.bmp", fp_rawcontent, 0, 0);
  432. delay(2000);
  433. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_waveshare_py, "N-Color1.bmp", fp_rawcontent, 0, 0);
  434. delay(2000);
  435. }
  436. }
  437. void drawBitmapsBuffered_test()
  438. {
  439. int16_t w2 = display.width() / 2;
  440. int16_t h2 = display.height() / 2;
  441. showBitmapFrom_HTTPS_Buffered(host_rawcontent, path_prenticedavid, "betty_4.bmp", fp_rawcontent, w2 - 102, h2 - 126);
  442. delay(2000);
  443. }
  444. static const uint16_t input_buffer_pixels = 800; // may affect performance
  445. static const uint16_t max_row_width = 1448; // for up to 6" display 1448x1072
  446. static const uint16_t max_palette_pixels = 256; // for depth <= 8
  447. uint8_t input_buffer[3 * input_buffer_pixels]; // up to depth 24
  448. uint8_t output_row_mono_buffer[max_row_width / 8]; // buffer for at least one row of b/w bits
  449. uint8_t output_row_color_buffer[max_row_width / 8]; // buffer for at least one row of color bits
  450. uint8_t mono_palette_buffer[max_palette_pixels / 8]; // palette buffer for depth <= 8 b/w
  451. uint8_t color_palette_buffer[max_palette_pixels / 8]; // palette buffer for depth <= 8 c/w
  452. uint16_t rgb_palette_buffer[max_palette_pixels]; // palette buffer for depth <= 8 for buffered graphics, needed for 7-color display
  453. void showBitmapFrom_HTTP(const char* host, const char* path, const char* filename, int16_t x, int16_t y, bool with_color)
  454. {
  455. WiFiClient client;
  456. bool connection_ok = false;
  457. bool valid = false; // valid format to be handled
  458. bool flip = true; // bitmap is stored bottom-to-top
  459. uint32_t startTime = millis();
  460. if ((x >= display.epd2.WIDTH) || (y >= display.epd2.HEIGHT)) return;
  461. Serial.println(); Serial.print("downloading file \""); Serial.print(filename); Serial.println("\"");
  462. Serial.print("connecting to "); Serial.println(host);
  463. if (!client.connect(host, httpPort))
  464. {
  465. Serial.println("connection failed");
  466. return;
  467. }
  468. Serial.print("requesting URL: ");
  469. Serial.println(String("http://") + host + path + filename);
  470. client.print(String("GET ") + path + filename + " HTTP/1.1\r\n" +
  471. "Host: " + host + "\r\n" +
  472. "User-Agent: GxEPD2_WiFi_Example\r\n" +
  473. "Connection: close\r\n\r\n");
  474. Serial.println("request sent");
  475. while (client.connected())
  476. {
  477. String line = client.readStringUntil('\n');
  478. if (!connection_ok)
  479. {
  480. connection_ok = line.startsWith("HTTP/1.1 200 OK");
  481. if (connection_ok) Serial.println(line);
  482. //if (!connection_ok) Serial.println(line);
  483. }
  484. if (!connection_ok) Serial.println(line);
  485. //Serial.println(line);
  486. if (line == "\r")
  487. {
  488. Serial.println("headers received");
  489. break;
  490. }
  491. }
  492. if (!connection_ok) return;
  493. // Parse BMP header
  494. if (read16(client) == 0x4D42) // BMP signature
  495. {
  496. uint32_t fileSize = read32(client);
  497. uint32_t creatorBytes = read32(client);
  498. uint32_t imageOffset = read32(client); // Start of image data
  499. uint32_t headerSize = read32(client);
  500. uint32_t width = read32(client);
  501. uint32_t height = read32(client);
  502. uint16_t planes = read16(client);
  503. uint16_t depth = read16(client); // bits per pixel
  504. uint32_t format = read32(client);
  505. uint32_t bytes_read = 7 * 4 + 3 * 2; // read so far
  506. if ((planes == 1) && ((format == 0) || (format == 3))) // uncompressed is handled, 565 also
  507. {
  508. Serial.print("File size: "); Serial.println(fileSize);
  509. Serial.print("Image Offset: "); Serial.println(imageOffset);
  510. Serial.print("Header size: "); Serial.println(headerSize);
  511. Serial.print("Bit Depth: "); Serial.println(depth);
  512. Serial.print("Image size: ");
  513. Serial.print(width);
  514. Serial.print('x');
  515. Serial.println(height);
  516. // BMP rows are padded (if needed) to 4-byte boundary
  517. uint32_t rowSize = (width * depth / 8 + 3) & ~3;
  518. if (depth < 8) rowSize = ((width * depth + 8 - depth) / 8 + 3) & ~3;
  519. if (height < 0)
  520. {
  521. height = -height;
  522. flip = false;
  523. }
  524. uint16_t w = width;
  525. uint16_t h = height;
  526. if ((x + w - 1) >= display.epd2.WIDTH) w = display.epd2.WIDTH - x;
  527. if ((y + h - 1) >= display.epd2.HEIGHT) h = display.epd2.HEIGHT - y;
  528. if (w <= max_row_width) // handle with direct drawing
  529. {
  530. valid = true;
  531. uint8_t bitmask = 0xFF;
  532. uint8_t bitshift = 8 - depth;
  533. uint16_t red, green, blue;
  534. bool whitish, colored;
  535. if (depth == 1) with_color = false;
  536. if (depth <= 8)
  537. {
  538. if (depth < 8) bitmask >>= depth;
  539. //bytes_read += skip(client, 54 - bytes_read); //palette is always @ 54
  540. bytes_read += skip(client, imageOffset - (4 << depth) - bytes_read); // 54 for regular, diff for colorsimportant
  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. }
  555. }
  556. display.clearScreen();
  557. uint32_t rowPosition = flip ? imageOffset + (height - h) * rowSize : imageOffset;
  558. //Serial.print("skip "); Serial.println(rowPosition - bytes_read);
  559. bytes_read += skip(client, rowPosition - bytes_read);
  560. for (uint16_t row = 0; row < h; row++, rowPosition += rowSize) // for each line
  561. {
  562. if (!connection_ok || !(client.connected() || client.available())) break;
  563. delay(1); // yield() to avoid WDT
  564. uint32_t in_remain = rowSize;
  565. uint32_t in_idx = 0;
  566. uint32_t in_bytes = 0;
  567. uint8_t in_byte = 0; // for depth <= 8
  568. uint8_t in_bits = 0; // for depth <= 8
  569. uint8_t out_byte = 0xFF; // white (for w%8!=0 border)
  570. uint8_t out_color_byte = 0xFF; // white (for w%8!=0 border)
  571. uint32_t out_idx = 0;
  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 = read8n(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 = read8n(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. // keep white
  646. }
  647. else if (colored && with_color)
  648. {
  649. out_color_byte &= ~(0x80 >> col % 8); // colored
  650. }
  651. else
  652. {
  653. out_byte &= ~(0x80 >> col % 8); // black
  654. }
  655. if ((7 == col % 8) || (col == w - 1)) // write that last byte! (for w%8!=0 border)
  656. {
  657. output_row_color_buffer[out_idx] = out_color_byte;
  658. output_row_mono_buffer[out_idx++] = out_byte;
  659. out_byte = 0xFF; // white (for w%8!=0 border)
  660. out_color_byte = 0xFF; // white (for w%8!=0 border)
  661. }
  662. } // end pixel
  663. int16_t yrow = y + (flip ? h - row - 1 : row);
  664. display.writeImage(output_row_mono_buffer, output_row_color_buffer, x, yrow, w, 1);
  665. } // end line
  666. Serial.print("downloaded in ");
  667. Serial.print(millis() - startTime);
  668. Serial.println(" ms");
  669. display.refresh();
  670. }
  671. Serial.print("bytes read "); Serial.println(bytes_read);
  672. }
  673. }
  674. client.stop();
  675. if (!valid)
  676. {
  677. Serial.println("bitmap format not handled.");
  678. }
  679. }
  680. void drawBitmapFrom_HTTP_ToBuffer(const char* host, const char* path, const char* filename, int16_t x, int16_t y, bool with_color)
  681. {
  682. WiFiClient client;
  683. bool connection_ok = false;
  684. bool valid = false; // valid format to be handled
  685. bool flip = true; // bitmap is stored bottom-to-top
  686. bool has_multicolors = display.epd2.panel == GxEPD2::ACeP565;
  687. uint32_t startTime = millis();
  688. if ((x >= display.width()) || (y >= display.height())) return;
  689. display.fillScreen(GxEPD_WHITE);
  690. Serial.print("connecting to "); Serial.println(host);
  691. if (!client.connect(host, httpPort))
  692. {
  693. Serial.println("connection failed");
  694. return;
  695. }
  696. Serial.print("requesting URL: ");
  697. Serial.println(String("http://") + host + path + filename);
  698. client.print(String("GET ") + path + filename + " HTTP/1.1\r\n" +
  699. "Host: " + host + "\r\n" +
  700. "User-Agent: GxEPD2_WiFi_Example\r\n" +
  701. "Connection: close\r\n\r\n");
  702. Serial.println("request sent");
  703. while (client.connected())
  704. {
  705. String line = client.readStringUntil('\n');
  706. if (!connection_ok)
  707. {
  708. connection_ok = line.startsWith("HTTP/1.1 200 OK");
  709. if (connection_ok) Serial.println(line);
  710. //if (!connection_ok) Serial.println(line);
  711. }
  712. if (!connection_ok) Serial.println(line);
  713. //Serial.println(line);
  714. if (line == "\r")
  715. {
  716. Serial.println("headers received");
  717. break;
  718. }
  719. }
  720. if (!connection_ok) return;
  721. // Parse BMP header
  722. if (read16(client) == 0x4D42) // BMP signature
  723. {
  724. uint32_t fileSize = read32(client);
  725. uint32_t creatorBytes = read32(client);
  726. uint32_t imageOffset = read32(client); // Start of image data
  727. uint32_t headerSize = read32(client);
  728. uint32_t width = read32(client);
  729. uint32_t height = read32(client);
  730. uint16_t planes = read16(client);
  731. uint16_t depth = read16(client); // bits per pixel
  732. uint32_t format = read32(client);
  733. uint32_t bytes_read = 7 * 4 + 3 * 2; // read so far
  734. if ((planes == 1) && ((format == 0) || (format == 3))) // uncompressed is handled, 565 also
  735. {
  736. Serial.print("File size: "); Serial.println(fileSize);
  737. Serial.print("Image Offset: "); Serial.println(imageOffset);
  738. Serial.print("Header size: "); Serial.println(headerSize);
  739. Serial.print("Bit Depth: "); Serial.println(depth);
  740. Serial.print("Image size: ");
  741. Serial.print(width);
  742. Serial.print('x');
  743. Serial.println(height);
  744. // BMP rows are padded (if needed) to 4-byte boundary
  745. uint32_t rowSize = (width * depth / 8 + 3) & ~3;
  746. if (depth < 8) rowSize = ((width * depth + 8 - depth) / 8 + 3) & ~3;
  747. if (height < 0)
  748. {
  749. height = -height;
  750. flip = false;
  751. }
  752. uint16_t w = width;
  753. uint16_t h = height;
  754. if ((x + w - 1) >= display.width()) w = display.width() - x;
  755. if ((y + h - 1) >= display.height()) h = display.height() - y;
  756. //if (w <= max_row_width) // handle with direct drawing
  757. {
  758. valid = true;
  759. uint8_t bitmask = 0xFF;
  760. uint8_t bitshift = 8 - depth;
  761. uint16_t red, green, blue;
  762. bool whitish, colored;
  763. if (depth == 1) with_color = false;
  764. if (depth <= 8)
  765. {
  766. if (depth < 8) bitmask >>= depth;
  767. //bytes_read += skip(client, 54 - bytes_read); //palette is always @ 54
  768. bytes_read += skip(client, imageOffset - (4 << depth) - bytes_read); // 54 for regular, diff for colorsimportant
  769. for (uint16_t pn = 0; pn < (1 << depth); pn++)
  770. {
  771. blue = client.read();
  772. green = client.read();
  773. red = client.read();
  774. client.read();
  775. bytes_read += 4;
  776. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  777. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  778. if (0 == pn % 8) mono_palette_buffer[pn / 8] = 0;
  779. mono_palette_buffer[pn / 8] |= whitish << pn % 8;
  780. if (0 == pn % 8) color_palette_buffer[pn / 8] = 0;
  781. color_palette_buffer[pn / 8] |= colored << pn % 8;
  782. //Serial.print("0x00"); Serial.print(red, HEX); Serial.print(green, HEX); Serial.print(blue, HEX);
  783. //Serial.print(" : "); Serial.print(whitish); Serial.print(", "); Serial.println(colored);
  784. rgb_palette_buffer[pn] = ((red & 0xF8) << 8) | ((green & 0xFC) << 3) | ((blue & 0xF8) >> 3);
  785. }
  786. }
  787. uint32_t rowPosition = flip ? imageOffset + (height - h) * rowSize : imageOffset;
  788. //Serial.print("skip "); Serial.println(rowPosition - bytes_read);
  789. bytes_read += skip(client, rowPosition - bytes_read);
  790. for (uint16_t row = 0; row < h; row++, rowPosition += rowSize) // for each line
  791. {
  792. if (!connection_ok || !(client.connected() || client.available())) break;
  793. delay(1); // yield() to avoid WDT
  794. uint32_t in_remain = rowSize;
  795. uint32_t in_idx = 0;
  796. uint32_t in_bytes = 0;
  797. uint8_t in_byte = 0; // for depth <= 8
  798. uint8_t in_bits = 0; // for depth <= 8
  799. uint16_t color = GxEPD_WHITE;
  800. for (uint16_t col = 0; col < w; col++) // for each pixel
  801. {
  802. yield();
  803. if (!connection_ok || !(client.connected() || client.available())) break;
  804. // Time to read more pixel data?
  805. if (in_idx >= in_bytes) // ok, exact match for 24bit also (size IS multiple of 3)
  806. {
  807. uint32_t get = in_remain > sizeof(input_buffer) ? sizeof(input_buffer) : in_remain;
  808. uint32_t got = read8n(client, input_buffer, get);
  809. while ((got < get) && connection_ok)
  810. {
  811. //Serial.print("got "); Serial.print(got); Serial.print(" < "); Serial.print(get); Serial.print(" @ "); Serial.println(bytes_read);
  812. uint32_t gotmore = read8n(client, input_buffer + got, get - got);
  813. got += gotmore;
  814. connection_ok = gotmore > 0;
  815. }
  816. in_bytes = got;
  817. in_remain -= got;
  818. bytes_read += got;
  819. }
  820. if (!connection_ok)
  821. {
  822. Serial.print("Error: got no more after "); Serial.print(bytes_read); Serial.println(" bytes read!");
  823. break;
  824. }
  825. switch (depth)
  826. {
  827. case 24:
  828. blue = input_buffer[in_idx++];
  829. green = input_buffer[in_idx++];
  830. red = input_buffer[in_idx++];
  831. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  832. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  833. color = ((red & 0xF8) << 8) | ((green & 0xFC) << 3) | ((blue & 0xF8) >> 3);
  834. break;
  835. case 16:
  836. {
  837. uint8_t lsb = input_buffer[in_idx++];
  838. uint8_t msb = input_buffer[in_idx++];
  839. if (format == 0) // 555
  840. {
  841. blue = (lsb & 0x1F) << 3;
  842. green = ((msb & 0x03) << 6) | ((lsb & 0xE0) >> 2);
  843. red = (msb & 0x7C) << 1;
  844. color = ((red & 0xF8) << 8) | ((green & 0xFC) << 3) | ((blue & 0xF8) >> 3);
  845. }
  846. else // 565
  847. {
  848. blue = (lsb & 0x1F) << 3;
  849. green = ((msb & 0x07) << 5) | ((lsb & 0xE0) >> 3);
  850. red = (msb & 0xF8);
  851. color = (msb << 8) | lsb;
  852. }
  853. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  854. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  855. }
  856. break;
  857. case 1:
  858. case 4:
  859. case 8:
  860. {
  861. if (0 == in_bits)
  862. {
  863. in_byte = input_buffer[in_idx++];
  864. in_bits = 8;
  865. }
  866. uint16_t pn = (in_byte >> bitshift) & bitmask;
  867. whitish = mono_palette_buffer[pn / 8] & (0x1 << pn % 8);
  868. colored = color_palette_buffer[pn / 8] & (0x1 << pn % 8);
  869. in_byte <<= depth;
  870. in_bits -= depth;
  871. color = rgb_palette_buffer[pn];
  872. }
  873. break;
  874. }
  875. if (with_color && has_multicolors)
  876. {
  877. // keep color
  878. }
  879. else if (whitish)
  880. {
  881. color = GxEPD_WHITE;
  882. }
  883. else if (colored && with_color)
  884. {
  885. color = GxEPD_COLORED;
  886. }
  887. else
  888. {
  889. color = GxEPD_BLACK;
  890. }
  891. uint16_t yrow = y + (flip ? h - row - 1 : row);
  892. display.drawPixel(x + col, yrow, color);
  893. } // end pixel
  894. } // end line
  895. }
  896. Serial.print("bytes read "); Serial.println(bytes_read);
  897. }
  898. }
  899. Serial.print("loaded in "); Serial.print(millis() - startTime); Serial.println(" ms");
  900. client.stop();
  901. if (!valid)
  902. {
  903. Serial.println("bitmap format not handled.");
  904. }
  905. }
  906. void showBitmapFrom_HTTP_Buffered(const char* host, const char* path, const char* filename, int16_t x, int16_t y, bool with_color)
  907. {
  908. Serial.println(); Serial.print("downloading file \""); Serial.print(filename); Serial.println("\"");
  909. display.setFullWindow();
  910. display.firstPage();
  911. do
  912. {
  913. drawBitmapFrom_HTTP_ToBuffer(host, path, filename, x, y, with_color);
  914. }
  915. while (display.nextPage());
  916. }
  917. void showBitmapFrom_HTTPS(const char* host, const char* path, const char* filename, const char* fingerprint, int16_t x, int16_t y, bool with_color, const char* certificate)
  918. {
  919. // Use WiFiClientSecure class to create TLS connection
  920. #if USE_BearSSL
  921. BearSSL::WiFiClientSecure client;
  922. #else
  923. WiFiClientSecure client;
  924. #endif
  925. bool connection_ok = false;
  926. bool valid = false; // valid format to be handled
  927. bool flip = true; // bitmap is stored bottom-to-top
  928. uint32_t startTime = millis();
  929. if ((x >= display.epd2.WIDTH) || (y >= display.epd2.HEIGHT)) return;
  930. Serial.println(); Serial.print("downloading file \""); Serial.print(filename); Serial.println("\"");
  931. Serial.print("connecting to "); Serial.println(host);
  932. #if defined (ESP8266)
  933. client.setBufferSizes(4096, 4096); // doesn't help
  934. if (fingerprint) client.setFingerprint(fingerprint);
  935. #elif defined (ESP32)
  936. if (certificate) client.setCACert(certificate);
  937. #endif
  938. if (!client.connect(host, httpsPort))
  939. {
  940. Serial.println("connection failed");
  941. return;
  942. }
  943. Serial.print("requesting URL: ");
  944. Serial.println(String("https://") + host + path + filename);
  945. client.print(String("GET ") + path + filename + " HTTP/1.1\r\n" +
  946. "Host: " + host + "\r\n" +
  947. "User-Agent: GxEPD2_WiFi_Example\r\n" +
  948. "Connection: close\r\n\r\n");
  949. Serial.println("request sent");
  950. while (client.connected())
  951. {
  952. String line = client.readStringUntil('\n');
  953. if (!connection_ok)
  954. {
  955. connection_ok = line.startsWith("HTTP/1.1 200 OK");
  956. if (connection_ok) Serial.println(line);
  957. //if (!connection_ok) Serial.println(line);
  958. }
  959. if (!connection_ok) Serial.println(line);
  960. //Serial.println(line);
  961. if (line == "\r")
  962. {
  963. Serial.println("headers received");
  964. break;
  965. }
  966. }
  967. if (!connection_ok) return;
  968. // Parse BMP header
  969. if (read16(client) == 0x4D42) // BMP signature
  970. {
  971. uint32_t fileSize = read32(client);
  972. uint32_t creatorBytes = read32(client);
  973. uint32_t imageOffset = read32(client); // Start of image data
  974. uint32_t headerSize = read32(client);
  975. uint32_t width = read32(client);
  976. uint32_t height = read32(client);
  977. uint16_t planes = read16(client);
  978. uint16_t depth = read16(client); // bits per pixel
  979. uint32_t format = read32(client);
  980. uint32_t bytes_read = 7 * 4 + 3 * 2; // read so far
  981. if ((planes == 1) && ((format == 0) || (format == 3))) // uncompressed is handled, 565 also
  982. {
  983. Serial.print("File size: "); Serial.println(fileSize);
  984. Serial.print("Image Offset: "); Serial.println(imageOffset);
  985. Serial.print("Header size: "); Serial.println(headerSize);
  986. Serial.print("Bit Depth: "); Serial.println(depth);
  987. Serial.print("Image size: ");
  988. Serial.print(width);
  989. Serial.print('x');
  990. Serial.println(height);
  991. // BMP rows are padded (if needed) to 4-byte boundary
  992. uint32_t rowSize = (width * depth / 8 + 3) & ~3;
  993. if (depth < 8) rowSize = ((width * depth + 8 - depth) / 8 + 3) & ~3;
  994. if (height < 0)
  995. {
  996. height = -height;
  997. flip = false;
  998. }
  999. uint16_t w = width;
  1000. uint16_t h = height;
  1001. if ((x + w - 1) >= display.epd2.WIDTH) w = display.epd2.WIDTH - x;
  1002. if ((y + h - 1) >= display.epd2.HEIGHT) h = display.epd2.HEIGHT - y;
  1003. if (w <= max_row_width) // handle with direct drawing
  1004. {
  1005. valid = true;
  1006. uint8_t bitmask = 0xFF;
  1007. uint8_t bitshift = 8 - depth;
  1008. uint16_t red, green, blue;
  1009. bool whitish, colored;
  1010. if (depth == 1) with_color = false;
  1011. if (depth <= 8)
  1012. {
  1013. if (depth < 8) bitmask >>= depth;
  1014. //bytes_read += skip(client, 54 - bytes_read); //palette is always @ 54
  1015. bytes_read += skip(client, imageOffset - (4 << depth) - bytes_read); // 54 for regular, diff for colorsimportant
  1016. for (uint16_t pn = 0; pn < (1 << depth); pn++)
  1017. {
  1018. blue = client.read();
  1019. green = client.read();
  1020. red = client.read();
  1021. client.read();
  1022. bytes_read += 4;
  1023. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  1024. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  1025. if (0 == pn % 8) mono_palette_buffer[pn / 8] = 0;
  1026. mono_palette_buffer[pn / 8] |= whitish << pn % 8;
  1027. if (0 == pn % 8) color_palette_buffer[pn / 8] = 0;
  1028. color_palette_buffer[pn / 8] |= colored << pn % 8;
  1029. }
  1030. }
  1031. display.clearScreen();
  1032. uint32_t rowPosition = flip ? imageOffset + (height - h) * rowSize : imageOffset;
  1033. //Serial.print("skip "); Serial.println(rowPosition - bytes_read);
  1034. bytes_read += skip(client, rowPosition - bytes_read);
  1035. for (uint16_t row = 0; row < h; row++, rowPosition += rowSize) // for each line
  1036. {
  1037. if (!connection_ok || !(client.connected() || client.available())) break;
  1038. delay(1); // yield() to avoid WDT
  1039. uint32_t in_remain = rowSize;
  1040. uint32_t in_idx = 0;
  1041. uint32_t in_bytes = 0;
  1042. uint8_t in_byte = 0; // for depth <= 8
  1043. uint8_t in_bits = 0; // for depth <= 8
  1044. uint8_t out_byte = 0xFF; // white (for w%8!=0 border)
  1045. uint8_t out_color_byte = 0xFF; // white (for w%8!=0 border)
  1046. uint32_t out_idx = 0;
  1047. for (uint16_t col = 0; col < w; col++) // for each pixel
  1048. {
  1049. yield();
  1050. if (!connection_ok || !(client.connected() || client.available())) break;
  1051. // Time to read more pixel data?
  1052. if (in_idx >= in_bytes) // ok, exact match for 24bit also (size IS multiple of 3)
  1053. {
  1054. uint32_t get = in_remain > sizeof(input_buffer) ? sizeof(input_buffer) : in_remain;
  1055. uint32_t got = read8n(client, input_buffer, get);
  1056. while ((got < get) && connection_ok)
  1057. {
  1058. //Serial.print("got "); Serial.print(got); Serial.print(" < "); Serial.print(get); Serial.print(" @ "); Serial.println(bytes_read);
  1059. //if ((get - got) > client.available()) delay(200); // does improve? yes, if >= 200
  1060. uint32_t gotmore = read8n(client, input_buffer + got, get - got);
  1061. got += gotmore;
  1062. connection_ok = gotmore > 0;
  1063. }
  1064. in_bytes = got;
  1065. in_remain -= got;
  1066. bytes_read += got;
  1067. }
  1068. if (!connection_ok)
  1069. {
  1070. Serial.print("Error: got no more after "); Serial.print(bytes_read); Serial.println(" bytes read!");
  1071. break;
  1072. }
  1073. switch (depth)
  1074. {
  1075. case 24:
  1076. blue = input_buffer[in_idx++];
  1077. green = input_buffer[in_idx++];
  1078. red = input_buffer[in_idx++];
  1079. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  1080. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  1081. break;
  1082. case 16:
  1083. {
  1084. uint8_t lsb = input_buffer[in_idx++];
  1085. uint8_t msb = input_buffer[in_idx++];
  1086. if (format == 0) // 555
  1087. {
  1088. blue = (lsb & 0x1F) << 3;
  1089. green = ((msb & 0x03) << 6) | ((lsb & 0xE0) >> 2);
  1090. red = (msb & 0x7C) << 1;
  1091. }
  1092. else // 565
  1093. {
  1094. blue = (lsb & 0x1F) << 3;
  1095. green = ((msb & 0x07) << 5) | ((lsb & 0xE0) >> 3);
  1096. red = (msb & 0xF8);
  1097. }
  1098. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  1099. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  1100. }
  1101. break;
  1102. case 1:
  1103. case 4:
  1104. case 8:
  1105. {
  1106. if (0 == in_bits)
  1107. {
  1108. in_byte = input_buffer[in_idx++];
  1109. in_bits = 8;
  1110. }
  1111. uint16_t pn = (in_byte >> bitshift) & bitmask;
  1112. whitish = mono_palette_buffer[pn / 8] & (0x1 << pn % 8);
  1113. colored = color_palette_buffer[pn / 8] & (0x1 << pn % 8);
  1114. in_byte <<= depth;
  1115. in_bits -= depth;
  1116. }
  1117. break;
  1118. }
  1119. if (whitish)
  1120. {
  1121. // keep white
  1122. }
  1123. else if (colored && with_color)
  1124. {
  1125. out_color_byte &= ~(0x80 >> col % 8); // colored
  1126. }
  1127. else
  1128. {
  1129. out_byte &= ~(0x80 >> col % 8); // black
  1130. }
  1131. if ((7 == col % 8) || (col == w - 1)) // write that last byte! (for w%8!=0 border)
  1132. {
  1133. output_row_color_buffer[out_idx] = out_color_byte;
  1134. output_row_mono_buffer[out_idx++] = out_byte;
  1135. out_byte = 0xFF; // white (for w%8!=0 border)
  1136. out_color_byte = 0xFF; // white (for w%8!=0 border)
  1137. }
  1138. } // end pixel
  1139. int16_t yrow = y + (flip ? h - row - 1 : row);
  1140. display.writeImage(output_row_mono_buffer, output_row_color_buffer, x, yrow, w, 1);
  1141. } // end line
  1142. Serial.print("downloaded in "); Serial.print(millis() - startTime); Serial.println(" ms");
  1143. Serial.print("bytes read "); Serial.println(bytes_read);
  1144. display.refresh();
  1145. }
  1146. }
  1147. }
  1148. client.stop();
  1149. if (!valid)
  1150. {
  1151. Serial.println("bitmap format not handled.");
  1152. }
  1153. }
  1154. 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, const char* certificate)
  1155. {
  1156. // Use WiFiClientSecure class to create TLS connection
  1157. #if USE_BearSSL
  1158. BearSSL::WiFiClientSecure client;
  1159. #else
  1160. WiFiClientSecure client;
  1161. #endif
  1162. bool connection_ok = false;
  1163. bool valid = false; // valid format to be handled
  1164. bool flip = true; // bitmap is stored bottom-to-top
  1165. bool has_multicolors = display.epd2.panel == GxEPD2::ACeP565;
  1166. uint32_t startTime = millis();
  1167. if ((x >= display.width()) || (y >= display.height())) return;
  1168. display.fillScreen(GxEPD_WHITE);
  1169. Serial.print("connecting to "); Serial.println(host);
  1170. #if defined (ESP8266)
  1171. client.setBufferSizes(4096, 4096); // doesn't help
  1172. if (fingerprint) client.setFingerprint(fingerprint);
  1173. #elif defined (ESP32)
  1174. if (certificate) client.setCACert(certificate);
  1175. #endif
  1176. if (!client.connect(host, httpsPort))
  1177. {
  1178. Serial.println("connection failed");
  1179. return;
  1180. }
  1181. Serial.print("requesting URL: ");
  1182. Serial.println(String("https://") + host + path + filename);
  1183. client.print(String("GET ") + path + filename + " HTTP/1.1\r\n" +
  1184. "Host: " + host + "\r\n" +
  1185. "User-Agent: GxEPD2_WiFi_Example\r\n" +
  1186. "Connection: close\r\n\r\n");
  1187. Serial.println("request sent");
  1188. while (client.connected())
  1189. {
  1190. String line = client.readStringUntil('\n');
  1191. if (!connection_ok)
  1192. {
  1193. connection_ok = line.startsWith("HTTP/1.1 200 OK");
  1194. if (connection_ok) Serial.println(line);
  1195. //if (!connection_ok) Serial.println(line);
  1196. }
  1197. if (!connection_ok) Serial.println(line);
  1198. //Serial.println(line);
  1199. if (line == "\r")
  1200. {
  1201. Serial.println("headers received");
  1202. break;
  1203. }
  1204. }
  1205. if (!connection_ok) return;
  1206. // Parse BMP header
  1207. if (read16(client) == 0x4D42) // BMP signature
  1208. {
  1209. uint32_t fileSize = read32(client);
  1210. uint32_t creatorBytes = read32(client);
  1211. uint32_t imageOffset = read32(client); // Start of image data
  1212. uint32_t headerSize = read32(client);
  1213. uint32_t width = read32(client);
  1214. uint32_t height = read32(client);
  1215. uint16_t planes = read16(client);
  1216. uint16_t depth = read16(client); // bits per pixel
  1217. uint32_t format = read32(client);
  1218. uint32_t bytes_read = 7 * 4 + 3 * 2; // read so far
  1219. if ((planes == 1) && ((format == 0) || (format == 3))) // uncompressed is handled, 565 also
  1220. {
  1221. Serial.print("File size: "); Serial.println(fileSize);
  1222. Serial.print("Image Offset: "); Serial.println(imageOffset);
  1223. Serial.print("Header size: "); Serial.println(headerSize);
  1224. Serial.print("Bit Depth: "); Serial.println(depth);
  1225. Serial.print("Image size: ");
  1226. Serial.print(width);
  1227. Serial.print('x');
  1228. Serial.println(height);
  1229. // BMP rows are padded (if needed) to 4-byte boundary
  1230. uint32_t rowSize = (width * depth / 8 + 3) & ~3;
  1231. if (depth < 8) rowSize = ((width * depth + 8 - depth) / 8 + 3) & ~3;
  1232. if (height < 0)
  1233. {
  1234. height = -height;
  1235. flip = false;
  1236. }
  1237. uint16_t w = width;
  1238. uint16_t h = height;
  1239. if ((x + w - 1) >= display.width()) w = display.width() - x;
  1240. if ((y + h - 1) >= display.height()) h = display.height() - y;
  1241. //if (w <= max_row_width) // handle with direct drawing
  1242. {
  1243. valid = true;
  1244. uint8_t bitmask = 0xFF;
  1245. uint8_t bitshift = 8 - depth;
  1246. uint16_t red, green, blue;
  1247. bool whitish, colored;
  1248. if (depth == 1) with_color = false;
  1249. if (depth <= 8)
  1250. {
  1251. if (depth < 8) bitmask >>= depth;
  1252. //bytes_read += skip(client, 54 - bytes_read); //palette is always @ 54
  1253. bytes_read += skip(client, imageOffset - (4 << depth) - bytes_read); // 54 for regular, diff for colorsimportant
  1254. for (uint16_t pn = 0; pn < (1 << depth); pn++)
  1255. {
  1256. blue = client.read();
  1257. green = client.read();
  1258. red = client.read();
  1259. client.read();
  1260. bytes_read += 4;
  1261. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  1262. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  1263. if (0 == pn % 8) mono_palette_buffer[pn / 8] = 0;
  1264. mono_palette_buffer[pn / 8] |= whitish << pn % 8;
  1265. if (0 == pn % 8) color_palette_buffer[pn / 8] = 0;
  1266. color_palette_buffer[pn / 8] |= colored << pn % 8;
  1267. //Serial.print("0x00"); Serial.print(red, HEX); Serial.print(green, HEX); Serial.print(blue, HEX);
  1268. //Serial.print(" : "); Serial.print(whitish); Serial.print(", "); Serial.println(colored);
  1269. rgb_palette_buffer[pn] = ((red & 0xF8) << 8) | ((green & 0xFC) << 3) | ((blue & 0xF8) >> 3);
  1270. }
  1271. }
  1272. uint32_t rowPosition = flip ? imageOffset + (height - h) * rowSize : imageOffset;
  1273. //Serial.print("skip "); Serial.println(rowPosition - bytes_read);
  1274. bytes_read += skip(client, rowPosition - bytes_read);
  1275. for (uint16_t row = 0; row < h; row++, rowPosition += rowSize) // for each line
  1276. {
  1277. if (!connection_ok || !(client.connected() || client.available())) break;
  1278. delay(1); // yield() to avoid WDT
  1279. uint32_t in_remain = rowSize;
  1280. uint32_t in_idx = 0;
  1281. uint32_t in_bytes = 0;
  1282. uint8_t in_byte = 0; // for depth <= 8
  1283. uint8_t in_bits = 0; // for depth <= 8
  1284. uint16_t color = GxEPD_WHITE;
  1285. for (uint16_t col = 0; col < w; col++) // for each pixel
  1286. {
  1287. yield();
  1288. if (!connection_ok || !(client.connected() || client.available())) break;
  1289. // Time to read more pixel data?
  1290. if (in_idx >= in_bytes) // ok, exact match for 24bit also (size IS multiple of 3)
  1291. {
  1292. uint32_t get = in_remain > sizeof(input_buffer) ? sizeof(input_buffer) : in_remain;
  1293. uint32_t got = read8n(client, input_buffer, get);
  1294. while ((got < get) && connection_ok)
  1295. {
  1296. //Serial.print("got "); Serial.print(got); Serial.print(" < "); Serial.print(get); Serial.print(" @ "); Serial.println(bytes_read);
  1297. uint32_t gotmore = read8n(client, input_buffer + got, get - got);
  1298. got += gotmore;
  1299. connection_ok = gotmore > 0;
  1300. }
  1301. in_bytes = got;
  1302. in_remain -= got;
  1303. bytes_read += got;
  1304. }
  1305. if (!connection_ok)
  1306. {
  1307. Serial.print("Error: got no more after "); Serial.print(bytes_read); Serial.println(" bytes read!");
  1308. break;
  1309. }
  1310. switch (depth)
  1311. {
  1312. case 24:
  1313. blue = input_buffer[in_idx++];
  1314. green = input_buffer[in_idx++];
  1315. red = input_buffer[in_idx++];
  1316. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  1317. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  1318. color = ((red & 0xF8) << 8) | ((green & 0xFC) << 3) | ((blue & 0xF8) >> 3);
  1319. break;
  1320. case 16:
  1321. {
  1322. uint8_t lsb = input_buffer[in_idx++];
  1323. uint8_t msb = input_buffer[in_idx++];
  1324. if (format == 0) // 555
  1325. {
  1326. blue = (lsb & 0x1F) << 3;
  1327. green = ((msb & 0x03) << 6) | ((lsb & 0xE0) >> 2);
  1328. red = (msb & 0x7C) << 1;
  1329. color = ((red & 0xF8) << 8) | ((green & 0xFC) << 3) | ((blue & 0xF8) >> 3);
  1330. }
  1331. else // 565
  1332. {
  1333. blue = (lsb & 0x1F) << 3;
  1334. green = ((msb & 0x07) << 5) | ((lsb & 0xE0) >> 3);
  1335. red = (msb & 0xF8);
  1336. color = (msb << 8) | lsb;
  1337. }
  1338. whitish = with_color ? ((red > 0x80) && (green > 0x80) && (blue > 0x80)) : ((red + green + blue) > 3 * 0x80); // whitish
  1339. colored = (red > 0xF0) || ((green > 0xF0) && (blue > 0xF0)); // reddish or yellowish?
  1340. }
  1341. break;
  1342. case 1:
  1343. case 4:
  1344. case 8:
  1345. {
  1346. if (0 == in_bits)
  1347. {
  1348. in_byte = input_buffer[in_idx++];
  1349. in_bits = 8;
  1350. }
  1351. uint16_t pn = (in_byte >> bitshift) & bitmask;
  1352. whitish = mono_palette_buffer[pn / 8] & (0x1 << pn % 8);
  1353. colored = color_palette_buffer[pn / 8] & (0x1 << pn % 8);
  1354. in_byte <<= depth;
  1355. in_bits -= depth;
  1356. color = rgb_palette_buffer[pn];
  1357. }
  1358. break;
  1359. }
  1360. if (with_color && has_multicolors)
  1361. {
  1362. // keep color
  1363. }
  1364. else if (whitish)
  1365. {
  1366. color = GxEPD_WHITE;
  1367. }
  1368. else if (colored && with_color)
  1369. {
  1370. color = GxEPD_COLORED;
  1371. }
  1372. else
  1373. {
  1374. color = GxEPD_BLACK;
  1375. }
  1376. uint16_t yrow = y + (flip ? h - row - 1 : row);
  1377. display.drawPixel(x + col, yrow, color);
  1378. } // end pixel
  1379. } // end line
  1380. }
  1381. Serial.print("bytes read "); Serial.println(bytes_read);
  1382. }
  1383. }
  1384. Serial.print("loaded in "); Serial.print(millis() - startTime); Serial.println(" ms");
  1385. client.stop();
  1386. if (!valid)
  1387. {
  1388. Serial.println("bitmap format not handled.");
  1389. }
  1390. }
  1391. 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, const char* certificate)
  1392. {
  1393. Serial.println(); Serial.print("downloading file \""); Serial.print(filename); Serial.println("\"");
  1394. display.setFullWindow();
  1395. display.firstPage();
  1396. do
  1397. {
  1398. drawBitmapFrom_HTTPS_ToBuffer(host, path, filename, fingerprint, x, y, with_color, certificate);
  1399. }
  1400. while (display.nextPage());
  1401. }
  1402. uint16_t read16(WiFiClient& client)
  1403. {
  1404. // BMP data is stored little-endian, same as Arduino.
  1405. uint16_t result;
  1406. ((uint8_t *)&result)[0] = client.read(); // LSB
  1407. ((uint8_t *)&result)[1] = client.read(); // MSB
  1408. return result;
  1409. }
  1410. uint32_t read32(WiFiClient& client)
  1411. {
  1412. // BMP data is stored little-endian, same as Arduino.
  1413. uint32_t result;
  1414. ((uint8_t *)&result)[0] = client.read(); // LSB
  1415. ((uint8_t *)&result)[1] = client.read();
  1416. ((uint8_t *)&result)[2] = client.read();
  1417. ((uint8_t *)&result)[3] = client.read(); // MSB
  1418. return result;
  1419. }
  1420. #if USE_BearSSL
  1421. uint32_t skip(BearSSL::WiFiClientSecure& client, int32_t bytes)
  1422. {
  1423. int32_t remain = bytes;
  1424. uint32_t start = millis();
  1425. while ((client.connected() || client.available()) && (remain > 0))
  1426. {
  1427. if (client.available())
  1428. {
  1429. int16_t v = client.read();
  1430. remain--;
  1431. }
  1432. else delay(1);
  1433. if (millis() - start > 2000) break; // don't hang forever
  1434. }
  1435. return bytes - remain;
  1436. }
  1437. uint32_t read8n(BearSSL::WiFiClientSecure& client, uint8_t* buffer, int32_t bytes)
  1438. {
  1439. int32_t remain = bytes;
  1440. uint32_t start = millis();
  1441. while ((client.connected() || client.available()) && (remain > 0))
  1442. {
  1443. if (client.available())
  1444. {
  1445. int16_t v = client.read();
  1446. *buffer++ = uint8_t(v);
  1447. remain--;
  1448. }
  1449. else delay(1);
  1450. if (millis() - start > 2000) break; // don't hang forever
  1451. }
  1452. return bytes - remain;
  1453. }
  1454. #endif
  1455. uint32_t skip(WiFiClient& client, int32_t bytes)
  1456. {
  1457. int32_t remain = bytes;
  1458. uint32_t start = millis();
  1459. while ((client.connected() || client.available()) && (remain > 0))
  1460. {
  1461. if (client.available())
  1462. {
  1463. int16_t v = client.read();
  1464. remain--;
  1465. }
  1466. else delay(1);
  1467. if (millis() - start > 2000) break; // don't hang forever
  1468. }
  1469. return bytes - remain;
  1470. }
  1471. uint32_t read8n(WiFiClient& client, uint8_t* buffer, int32_t bytes)
  1472. {
  1473. int32_t remain = bytes;
  1474. uint32_t start = millis();
  1475. while ((client.connected() || client.available()) && (remain > 0))
  1476. {
  1477. if (client.available())
  1478. {
  1479. int16_t v = client.read();
  1480. *buffer++ = uint8_t(v);
  1481. remain--;
  1482. }
  1483. else delay(1);
  1484. if (millis() - start > 2000) break; // don't hang forever
  1485. }
  1486. return bytes - remain;
  1487. }