GxEPD2_SerialFlash_Loader.ino 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. // GxEPD2_SerialFlash_Loader : Display Library example for SPI e-paper panels from Dalian Good Display and boards from Waveshare.
  2. //
  3. // Display Library based on Demo Example from Good Display: http://www.good-display.com/download_list/downloadcategoryid=34&isMode=false.html
  4. //
  5. // Author: Jean-Marc Zingg
  6. //
  7. // Version: see library.properties
  8. //
  9. // Library: https://github.com/ZinggJM/GxEPD2
  10. //
  11. // this example uses the SerialFlash library from: https://github.com/PaulStoffregen/SerialFlash
  12. // with a modification for use with ESP32 or the STM32 package available here: https://github.com/ZinggJM/SerialFlash
  13. // download it as .zip file and install with Library Mananger method "Add .ZIP Library..."
  14. //
  15. // this example can run on ESP6266 or ESP32 using WiFi connection
  16. // Supporting Arduino Forum Topics:
  17. // Waveshare e-paper displays with SPI: http://forum.arduino.cc/index.php?topic=487007.0
  18. // Good Display ePaper for Arduino : https://forum.arduino.cc/index.php?topic=436411.0
  19. #include <SerialFlash.h>
  20. // digital pin for flash chip CS pin:
  21. //const int FlashChipSelect = SS; // for standard slave select pin
  22. const int FlashChipSelect = 5; // use D1 on my Wemos D1 mini wired for e-papers
  23. #if defined (ESP8266)
  24. #include <ESP8266WiFi.h>
  25. #define USE_BearSSL true
  26. #endif
  27. #include <WiFiClient.h>
  28. #include <WiFiClientSecure.h>
  29. const char* ssid = "........";
  30. const char* password = "........";
  31. const int httpPort = 80;
  32. const int httpsPort = 443;
  33. 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";
  34. 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";
  35. #if USE_BearSSL
  36. 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};
  37. #else
  38. const char* fp_rawcontent = "cc aa 48 48 66 46 0e 91 53 2c 9c 7c 23 2a b1 74 4d 29 9d 33";
  39. #endif
  40. const char* host_rawcontent = "raw.githubusercontent.com";
  41. const char* path_rawcontent = "/ZinggJM/GxEPD2/master/extras/bitmaps/";
  42. const char* path_prenticedavid = "/prenticedavid/MCUFRIEND_kbv/master/extras/bitmaps/";
  43. void setup()
  44. {
  45. Serial.begin(115200);
  46. Serial.println();
  47. Serial.println("GxEPD2_SerialFlash_Loader");
  48. #ifdef RE_INIT_NEEDED
  49. WiFi.persistent(true);
  50. WiFi.mode(WIFI_STA); // switch off AP
  51. WiFi.setAutoConnect(true);
  52. WiFi.setAutoReconnect(true);
  53. WiFi.disconnect();
  54. #endif
  55. if (!WiFi.getAutoConnect() || ( WiFi.getMode() != WIFI_STA) || ((WiFi.SSID() != ssid) && String(ssid) != "........"))
  56. {
  57. Serial.println();
  58. Serial.print("WiFi.getAutoConnect()=");
  59. Serial.println(WiFi.getAutoConnect());
  60. Serial.print("WiFi.SSID()=");
  61. Serial.println(WiFi.SSID());
  62. WiFi.mode(WIFI_STA); // switch off AP
  63. Serial.print("Connecting to ");
  64. Serial.println(ssid);
  65. WiFi.begin(ssid, password);
  66. }
  67. int ConnectTimeout = 30; // 15 seconds
  68. while (WiFi.status() != WL_CONNECTED)
  69. {
  70. delay(500);
  71. Serial.print(".");
  72. Serial.print(WiFi.status());
  73. if (--ConnectTimeout <= 0)
  74. {
  75. Serial.println();
  76. Serial.println("WiFi connect timeout");
  77. return;
  78. }
  79. }
  80. Serial.println();
  81. Serial.println("WiFi connected");
  82. // Print the IP address
  83. Serial.println(WiFi.localIP());
  84. if (!SerialFlash.begin(FlashChipSelect))
  85. {
  86. Serial.println("Unable to access SPI Flash chip");
  87. return;
  88. }
  89. Serial.println("SerialFlash started");
  90. eraseSerialFlash();
  91. listFiles();
  92. downloadBitmaps_200x200();
  93. downloadBitmaps_other();
  94. downloadBitmaps_test();
  95. listFiles();
  96. }
  97. void loop()
  98. {
  99. }
  100. void listFiles()
  101. {
  102. Serial.println("All Files on SPI Flash chip:");
  103. SerialFlash.opendir();
  104. while (1)
  105. {
  106. char filename[64];
  107. uint32_t filesize;
  108. if (SerialFlash.readdir(filename, sizeof(filename), filesize))
  109. {
  110. Serial.print(" ");
  111. Serial.print(filename);
  112. spaces(20 - strlen(filename));
  113. Serial.print(" ");
  114. Serial.print(filesize);
  115. Serial.print(" bytes");
  116. Serial.println();
  117. }
  118. else
  119. {
  120. Serial.println("no more files...");
  121. break; // no more files
  122. }
  123. }
  124. }
  125. void spaces(int num)
  126. {
  127. for (int i=0; i < num; i++)
  128. {
  129. Serial.print(" ");
  130. }
  131. }
  132. void downloadBitmaps_200x200()
  133. {
  134. downloadFile_HTTPS(host_rawcontent, path_rawcontent, "logo200x200.bmp", fp_rawcontent, "logo200x200.bmp");
  135. downloadFile_HTTPS(host_rawcontent, path_rawcontent, "first200x200.bmp", fp_rawcontent, "first200x200.bmp");
  136. downloadFile_HTTPS(host_rawcontent, path_rawcontent, "second200x200.bmp", fp_rawcontent, "second200x200.bmp");
  137. downloadFile_HTTPS(host_rawcontent, path_rawcontent, "third200x200.bmp", fp_rawcontent, "third200x200.bmp");
  138. downloadFile_HTTPS(host_rawcontent, path_rawcontent, "fourth200x200.bmp", fp_rawcontent, "fourth200x200.bmp");
  139. downloadFile_HTTPS(host_rawcontent, path_rawcontent, "fifth200x200.bmp", fp_rawcontent, "fifth200x200.bmp");
  140. downloadFile_HTTPS(host_rawcontent, path_rawcontent, "sixth200x200.bmp", fp_rawcontent, "sixth200x200.bmp");
  141. downloadFile_HTTPS(host_rawcontent, path_rawcontent, "seventh200x200.bmp", fp_rawcontent, "seventh200x200.bmp");
  142. downloadFile_HTTPS(host_rawcontent, path_rawcontent, "eighth200x200.bmp", fp_rawcontent, "eighth200x200.bmp");
  143. }
  144. void downloadBitmaps_other()
  145. {
  146. downloadFile_HTTP("www.squix.org", "/blog/wunderground/", "chanceflurries.bmp", "chanceflurries.bmp");
  147. downloadFile_HTTPS(host_rawcontent, path_prenticedavid, "betty_1.bmp", fp_rawcontent, "betty_1.bmp");
  148. downloadFile_HTTPS(host_rawcontent, path_prenticedavid, "betty_4.bmp", fp_rawcontent, "betty_4.bmp");
  149. downloadFile_HTTPS(host_rawcontent, path_prenticedavid, "marilyn_240x240x8.bmp", fp_rawcontent, "marilyn_240x240x8.bmp");
  150. downloadFile_HTTPS(host_rawcontent, path_prenticedavid, "miniwoof.bmp", fp_rawcontent, "miniwoof.bmp");
  151. //downloadFile_HTTPS(host_rawcontent, path_prenticedavid, "test.bmp", fp_rawcontent, "test.bmp");
  152. downloadFile_HTTPS(host_rawcontent, path_prenticedavid, "tiger.bmp", fp_rawcontent, "tiger.bmp");
  153. downloadFile_HTTPS(host_rawcontent, path_prenticedavid, "tiger_178x160x4.bmp", fp_rawcontent, "tiger_178x160x4.bmp");
  154. downloadFile_HTTPS(host_rawcontent, path_prenticedavid, "tiger_240x317x4.bmp", fp_rawcontent, "tiger_240x317x4.bmp");
  155. downloadFile_HTTPS(host_rawcontent, path_prenticedavid, "tiger_320x200x24.bmp", fp_rawcontent, "tiger_320x200x24.bmp");
  156. //downloadFile_HTTPS(host_rawcontent, path_prenticedavid, "tiger16T.bmp", fp_rawcontent, "tiger16T.bmp");
  157. downloadFile_HTTPS(host_rawcontent, path_prenticedavid, "woof.bmp", fp_rawcontent, "woof.bmp");
  158. }
  159. void downloadBitmaps_test()
  160. {
  161. downloadFile_HTTPS(host_rawcontent, path_rawcontent, "output5.bmp", fp_rawcontent, "output5.bmp");
  162. downloadFile_HTTPS(host_rawcontent, path_rawcontent, "output6.bmp", fp_rawcontent, "output6.bmp");
  163. downloadFile_HTTPS(host_rawcontent, path_rawcontent, "tractor_1.bmp", fp_rawcontent, "tractor_1.bmp");
  164. downloadFile_HTTPS(host_rawcontent, path_rawcontent, "tractor_4.bmp", fp_rawcontent, "tractor_4.bmp");
  165. downloadFile_HTTPS(host_rawcontent, path_rawcontent, "tractor_8.bmp", fp_rawcontent, "tractor_8.bmp");
  166. downloadFile_HTTPS(host_rawcontent, path_rawcontent, "tractor_11.bmp", fp_rawcontent, "tractor_11.bmp");
  167. downloadFile_HTTPS(host_rawcontent, path_rawcontent, "tractor_44.bmp", fp_rawcontent, "tractor_44.bmp");
  168. downloadFile_HTTPS(host_rawcontent, path_rawcontent, "tractor_88.bmp", fp_rawcontent, "tractor_88.bmp");
  169. }
  170. void downloadFile_HTTP(const char* host, const char* path, const char* filename, const char* target)
  171. {
  172. WiFiClient client;
  173. Serial.print("connecting to ");
  174. Serial.println(host);
  175. if (!client.connect(host, httpPort))
  176. {
  177. Serial.println("connection failed");
  178. return;
  179. }
  180. Serial.print("requesting URL: ");
  181. Serial.println(String("http://") + host + path + filename);
  182. client.print(String("GET ") + path + filename + " HTTP/1.1\r\n" +
  183. "Host: " + host + "\r\n" +
  184. "User-Agent: GxEPD2_SerialFlash_Loader\r\n" +
  185. "Connection: close\r\n\r\n");
  186. Serial.println("request sent");
  187. bool ok = false;
  188. int32_t content_length = 0;
  189. while (client.connected() || client.available())
  190. {
  191. String line = client.readStringUntil('\n');
  192. if (!ok)
  193. {
  194. ok = line.startsWith("HTTP/1.1 200 OK");
  195. if (ok) Serial.println(line);
  196. //if (!ok) Serial.println(line);
  197. }
  198. if (!ok) Serial.println(line);
  199. //Serial.println(line);
  200. if (line.startsWith("Content-Length: "))
  201. {
  202. content_length = line.substring(16, line.length()).toInt();
  203. Serial.print("content_length is "); Serial.println(content_length);
  204. }
  205. if (line == "\r")
  206. {
  207. Serial.println("headers received");
  208. break;
  209. }
  210. }
  211. if (!ok) return;
  212. uint8_t buffer[512];
  213. #if defined(ESP8266)
  214. client.peekBytes(buffer, 2);
  215. Serial.write(buffer[0]); Serial.write(buffer[1]); Serial.println();
  216. #endif
  217. size_t total = 0;
  218. //if (!SerialFlash.createErasable(target, content_length)) // would use much more space
  219. if (!SerialFlash.create(target, content_length))
  220. {
  221. Serial.print(target); Serial.println(" create failed");
  222. return;
  223. }
  224. SerialFlashFile file = SerialFlash.open(target);
  225. if (!file)
  226. {
  227. Serial.print(target); Serial.println(" open failed");
  228. return;
  229. }
  230. while (client.connected() || client.available())
  231. {
  232. size_t available = client.available();
  233. size_t fetch = available <= sizeof(buffer) ? available : sizeof(buffer);
  234. if (fetch > 0)
  235. {
  236. size_t got = client.read(buffer, fetch);
  237. file.write(buffer, got);
  238. total += got;
  239. }
  240. delay(1); // yield();
  241. }
  242. file.close();
  243. Serial.print("done, "); Serial.print(total); Serial.println(" bytes transferred");
  244. }
  245. void downloadFile_HTTPS(const char* host, const char* path, const char* filename, const char* fingerprint, const char* target)
  246. {
  247. // Use WiFiClientSecure class to create TLS connection
  248. #if USE_BearSSL
  249. BearSSL::WiFiClientSecure client;
  250. #else
  251. WiFiClientSecure client;
  252. #endif
  253. Serial.println(); Serial.print("downloading file \""); Serial.print(filename); Serial.println("\"");
  254. Serial.print("connecting to "); Serial.println(host);
  255. #if USE_BearSSL
  256. if (fingerprint) client.setFingerprint((uint8_t*)fingerprint);
  257. #endif
  258. if (!client.connect(host, httpsPort))
  259. {
  260. Serial.println("connection failed");
  261. return;
  262. }
  263. #if defined (ESP8266) && !USE_BearSSL
  264. if (fingerprint)
  265. {
  266. if (client.verify(fingerprint, host))
  267. {
  268. Serial.println("certificate matches");
  269. }
  270. else
  271. {
  272. Serial.println("certificate doesn't match");
  273. return;
  274. }
  275. }
  276. #endif
  277. Serial.print("requesting URL: ");
  278. Serial.println(String("https://") + host + path + filename);
  279. client.print(String("GET ") + path + filename + " HTTP/1.1\r\n" +
  280. "Host: " + host + "\r\n" +
  281. "User-Agent: GxEPD2_SerialFlash_Loader\r\n" +
  282. "Connection: close\r\n\r\n");
  283. Serial.println("request sent");
  284. bool ok = false;
  285. int32_t content_length = 0;
  286. while (client.connected() || client.available())
  287. {
  288. String line = client.readStringUntil('\n');
  289. if (!ok)
  290. {
  291. ok = line.startsWith("HTTP/1.1 200 OK");
  292. if (ok) Serial.println(line);
  293. //if (!ok) Serial.println(line);
  294. }
  295. if (!ok) Serial.println(line);
  296. //Serial.print("line: "); Serial.println(line);
  297. if (line.startsWith("Content-Length: "))
  298. {
  299. content_length = line.substring(16, line.length()).toInt();
  300. Serial.print("content_length is "); Serial.println(content_length);
  301. }
  302. if (line == "\r")
  303. {
  304. Serial.println("headers received");
  305. break;
  306. }
  307. }
  308. if (!ok) return;
  309. //return;
  310. uint8_t buffer[512];
  311. #if defined(ESP8266)
  312. client.peekBytes(buffer, 2);
  313. Serial.write(buffer[0]); Serial.write(buffer[1]); Serial.println();
  314. #endif
  315. size_t total = 0;
  316. //if (!SerialFlash.createErasable(target, content_length)) // would use much more space
  317. if (!SerialFlash.create(target, content_length))
  318. {
  319. Serial.print(target); Serial.println(" create failed");
  320. return;
  321. }
  322. SerialFlashFile file = SerialFlash.open(target);
  323. if (!file)
  324. {
  325. Serial.print(target); Serial.println(" open failed");
  326. return;
  327. }
  328. while (client.connected() || client.available())
  329. {
  330. // this doesn't work as expected, but it helps for long downloads
  331. int32_t start = millis();
  332. for (int16_t t = 0, dly = 50; t < 20; t++, dly += 50)
  333. {
  334. if (!(client.connected() || client.available())) break;
  335. if (client.available()) break; // read would not recover after having returned 0
  336. delay(dly);
  337. }
  338. if (!(client.connected() || client.available())) break;
  339. int32_t elapsed = millis() - start;
  340. if (elapsed > 250)
  341. {
  342. Serial.print("waited for available "); Serial.print(millis() - start); Serial.print(" ms @ "); Serial.println(total);
  343. }
  344. size_t available = client.available();
  345. if (0 == available)
  346. {
  347. Serial.print("download error: timeout on available() after "); Serial.print(total); Serial.println(" bytes");
  348. break; // don't hang forever
  349. }
  350. size_t fetch = available <= sizeof(buffer) ? available : sizeof(buffer);
  351. if (fetch > 0)
  352. {
  353. size_t got = client.read(buffer, fetch);
  354. file.write(buffer, got);
  355. total += got;
  356. }
  357. delay(1); // yield();
  358. if (total > 30000) delay(250); // helps for long downloads
  359. }
  360. file.close();
  361. Serial.print("done, "); Serial.print(total); Serial.println(" bytes transferred");
  362. }
  363. void eraseSerialFlash()
  364. {
  365. unsigned char id[5];
  366. SerialFlash.readID(id);
  367. unsigned long size = SerialFlash.capacity(id);
  368. unsigned long startMillis = millis();
  369. if (size > 0)
  370. {
  371. Serial.print("Flash Memory has ");
  372. Serial.print(size);
  373. Serial.println(" bytes.");
  374. Serial.println("Erasing ALL Flash Memory:");
  375. // Estimate the (lengthy) wait time.
  376. Serial.print(" estimated wait: ");
  377. int seconds = (float)size / eraseBytesPerSecond(id) + 0.5;
  378. Serial.print(seconds);
  379. Serial.println(" seconds.");
  380. Serial.println(" Yes, full chip erase is SLOW!");
  381. SerialFlash.eraseAll();
  382. unsigned long dotMillis = millis();
  383. unsigned char dotcount = 0;
  384. while (SerialFlash.ready() == false)
  385. {
  386. if (millis() - dotMillis > 1000)
  387. {
  388. dotMillis = dotMillis + 1000;
  389. Serial.print(".");
  390. dotcount = dotcount + 1;
  391. if (dotcount >= 60)
  392. {
  393. Serial.println();
  394. dotcount = 0;
  395. }
  396. }
  397. yield();
  398. }
  399. if (dotcount > 0) Serial.println();
  400. Serial.println("Erase completed");
  401. unsigned long elapsed = millis() - startMillis;
  402. Serial.print(" actual wait: ");
  403. Serial.print(elapsed / 1000ul);
  404. Serial.println(" seconds.");
  405. }
  406. }
  407. float eraseBytesPerSecond(const unsigned char *id)
  408. {
  409. if (id[0] == 0x20) return 152000.0; // Micron
  410. if (id[0] == 0x01) return 500000.0; // Spansion
  411. if (id[0] == 0xEF) return 419430.0; // Winbond
  412. if (id[0] == 0xC2) return 279620.0; // Macronix
  413. return 320000.0; // guess?
  414. }