EPD.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589
  1. #include <stdio.h>
  2. #include "EPD.h"
  3. #include <string.h>
  4. #include "esp_log.h"
  5. #include "freertos/FreeRTOS.h"
  6. #include "freertos/task.h"
  7. #include "freertos/queue.h"
  8. #include "GUI_Paint.h"
  9. #include "freertos/timers.h"
  10. // #include "../components/../main/user_sleep.h"
  11. #include "esp_timer.h"
  12. uint8_t current_display = 0;
  13. #include "../../main/user_sleep.h"
  14. #include "esp_sleep.h"
  15. static const char *LOG_TAG = "EPD";
  16. #if HARDWARE_SPI
  17. spi_device_handle_t epd_spi;
  18. // uint8_t cmd[1]={0};
  19. // epd_write_cmd(screen,0x00,false);
  20. // cmd[0] = 0x17;
  21. // epd_write_data(screen,cmd,1);
  22. // Place data into DRAM. Constant data gets placed into DROM by default, which is not accessible by DMA.
  23. DRAM_ATTR static const epd_init_cmd_t init_cmds[] = {
  24. {0x00, {0x1f, 0x0E}, 2},
  25. {0x50, {0x18, 0x07}, 2},
  26. {0xe0, {0x02}, 1},
  27. {0xe5, {0x5c}, 1},
  28. {0x11, {0}, 0x80},
  29. #if 0
  30. {0x00,{0x1B},1},
  31. #else
  32. //{0x00,{0x8F},1},
  33. #endif
  34. {0, {0}, 0xff},
  35. };
  36. // This function is called (in irq context!) just before a transmission starts. It will
  37. // set the D/C line to the value indicated in the user field.
  38. static void spi_pre_transfer_callback(spi_transaction_t *t)
  39. {
  40. int dc = (int)t->user;
  41. gpio_set_level(PIN_L_DC, dc);
  42. gpio_set_level(PIN_R_DC, dc);
  43. }
  44. #if 0
  45. void epd_spi_write(struct EPD_INFO_SET* epd_pin_set,unsigned char value)
  46. {
  47. int i = 0,data = value;
  48. epd_set_level(epd_pin_set->sclk_pin, LOW_LEVEL);
  49. for(i=0;i<8;i++)
  50. {
  51. if(data&0x0080)
  52. {
  53. epd_set_level(epd_pin_set->sda_pin,HIGH_LEVEL);
  54. }
  55. else
  56. {
  57. epd_set_level(epd_pin_set->sda_pin,LOW_LEVEL);
  58. }
  59. // ets_delay_us(1);
  60. epd_set_level(epd_pin_set->sclk_pin, HIGH_LEVEL);
  61. // ets_delay_us(1);
  62. epd_set_level(epd_pin_set->sclk_pin, LOW_LEVEL);
  63. data = ((data)<<1 & 0xff);
  64. }
  65. }
  66. #endif
  67. /*********************************************************************************
  68. * function : SPI_Write
  69. * Description : spi写入数据
  70. * Input :
  71. * Output :
  72. * Author : 祁鑫 Data : 2023 8.11
  73. **********************************************************************************/
  74. IRAM_ATTR void SPI_Write(unsigned char value)
  75. {
  76. int i = 0, data = value;
  77. int j = 100;
  78. gpio_set_level(PIN_SPI_CLK, 0);
  79. for (i = 0; i < 8; i++)
  80. {
  81. if (data & 0x0080)
  82. {
  83. gpio_set_level(PIN_SPI_MOSI, 1);
  84. // EPD_SDA_1;
  85. }
  86. else
  87. {
  88. gpio_set_level(PIN_SPI_MOSI, 0);
  89. // EPD_SDA_0;
  90. }
  91. // vTaskDelay(1/ portTICK_PERIOD_MS);
  92. gpio_set_level(PIN_SPI_CLK, 1);
  93. // vTaskDelay(1/ portTICK_PERIOD_MS);
  94. gpio_set_level(PIN_SPI_CLK, 0);
  95. data = ((data << 1) & 0xff);
  96. }
  97. }
  98. // Initialize the display
  99. void epd_init(void)
  100. {
  101. #if !SOFTWARE_SPI_ENABLE // 硬件spi
  102. esp_err_t ret;
  103. spi_bus_config_t buscfg = {
  104. .miso_io_num = -1,
  105. .mosi_io_num = PIN_SPI_MOSI,
  106. .sclk_io_num = PIN_SPI_CLK,
  107. .quadwp_io_num = -1,
  108. .quadhd_io_num = -1,
  109. .max_transfer_sz = 4096 // dma
  110. };
  111. spi_device_interface_config_t devcfg = {
  112. .clock_speed_hz = 20 * 1000 * 1000, // Clock out at 10 MHz
  113. .mode = 0, // SPI mode 0
  114. .spics_io_num = -1, // CS pin
  115. .queue_size = 7, // We want to be able to queue 7 transactions at a time
  116. .pre_cb = spi_pre_transfer_callback, // Specify pre-transfer callback to handle D/C line
  117. };
  118. // Initialize the SPI bus
  119. ret = spi_bus_initialize(EPD_HOST, &buscfg, SPI_DMA_CH_AUTO);
  120. ESP_ERROR_CHECK(ret);
  121. // Attach the LCD to the SPI bus
  122. ret = spi_bus_add_device(EPD_HOST, &devcfg, &epd_spi);
  123. ESP_ERROR_CHECK(ret);
  124. #else // 软件模拟spi
  125. gpio_reset_pin(PIN_SPI_MOSI);
  126. gpio_reset_pin(PIN_SPI_CLK);
  127. gpio_reset_pin(PIN_L_CS);
  128. gpio_reset_pin(PIN_L_DC);
  129. gpio_reset_pin(PIN_L_RST);
  130. gpio_reset_pin(PIN_L_BUSY);
  131. gpio_reset_pin(PIN_R_CS);
  132. gpio_reset_pin(PIN_R_DC);
  133. gpio_reset_pin(PIN_R_RST);
  134. gpio_reset_pin(PIN_R_BUSY);
  135. #endif
  136. // gpio_reset_pin(PIN_L_CS);
  137. // Initialize non-SPI GPIOs
  138. gpio_config_t io_conf = {};
  139. io_conf.pin_bit_mask = PIN_EPD_OUTPUT;
  140. io_conf.mode = GPIO_MODE_OUTPUT;
  141. io_conf.pull_up_en = 0;
  142. gpio_config(&io_conf);
  143. io_conf.pin_bit_mask = PIN_EPD_INPUT;
  144. io_conf.mode = GPIO_MODE_INPUT;
  145. io_conf.pull_up_en = 0;
  146. gpio_config(&io_conf);
  147. // Reset the display
  148. int reson = is_wake_up_reson(); // 返回唤醒的原因
  149. if (
  150. 1 //(reson != ESP_SLEEP_WAKEUP_ULP) && (reson != ESP_SLEEP_WAKEUP_TIMER) && (reson != ESP_SLEEP_WAKEUP_EXT0)
  151. )
  152. {
  153. gpio_set_level(PIN_L_RST, 0);
  154. vTaskDelay(10 / portTICK_PERIOD_MS);
  155. gpio_set_level(PIN_L_RST, 1);
  156. vTaskDelay(10 / portTICK_PERIOD_MS);
  157. gpio_set_level(PIN_R_RST, 0);
  158. vTaskDelay(10 / portTICK_PERIOD_MS);
  159. gpio_set_level(PIN_R_RST, 1);
  160. vTaskDelay(10 / portTICK_PERIOD_MS);
  161. #if 1
  162. int cmd = 0;
  163. // Send all the commands
  164. while (init_cmds[cmd].databytes != 0xff)
  165. {
  166. #if 1
  167. epd_write_cmd(SCREEN_LEFT, init_cmds[cmd].cmd, false);
  168. epd_write_data(SCREEN_LEFT, init_cmds[cmd].data, init_cmds[cmd].databytes & 0x1F);
  169. epd_write_cmd(SCREEN_RIGHT, init_cmds[cmd].cmd, false);
  170. epd_write_data(SCREEN_RIGHT, init_cmds[cmd].data, init_cmds[cmd].databytes & 0x1F);
  171. #endif
  172. if (init_cmds[cmd].databytes & 0x80)
  173. {
  174. vTaskDelay(100 / portTICK_PERIOD_MS);
  175. }
  176. cmd++;
  177. }
  178. #endif
  179. }
  180. #if QUICK_DISPLAY
  181. uint8_t *old_buffer = heap_caps_malloc(480 * 81, MALLOC_CAP_8BIT);
  182. memset(old_buffer, 0X00, 480 * 81);
  183. epd_cache_quick_full_screen_refresh(SCREEN_LEFT, NULL, old_buffer);
  184. epd_write_cmd(SCREEN_LEFT, 0x04, false); // POWER ON
  185. epd_check_status(SCREEN_LEFT); // waiting for the electronic paper IC to release the idle signal
  186. // Refresh
  187. epd_refresh(SCREEN_LEFT);
  188. epd_write_cmd(SCREEN_LEFT, 0x02, false); // Power OFF
  189. epd_check_status(SCREEN_LEFT);
  190. epd_sleep(SCREEN_LEFT);
  191. free(old_buffer);
  192. #else
  193. #endif
  194. }
  195. void epd_init_cmd(screen_t screen)
  196. {
  197. if (screen == SCREEN_LEFT)
  198. {
  199. gpio_set_level(PIN_L_RST, 0);
  200. vTaskDelay(10 / portTICK_PERIOD_MS);
  201. gpio_set_level(PIN_L_RST, 1);
  202. vTaskDelay(10 / portTICK_PERIOD_MS);
  203. int cmd = 0;
  204. // Send all the commands
  205. while (init_cmds[cmd].databytes != 0xff)
  206. {
  207. #if 1
  208. epd_write_cmd(SCREEN_LEFT, init_cmds[cmd].cmd, false);
  209. epd_write_data(SCREEN_LEFT, init_cmds[cmd].data, init_cmds[cmd].databytes & 0x1F);
  210. #endif
  211. if (init_cmds[cmd].databytes & 0x80)
  212. {
  213. vTaskDelay(10 / portTICK_PERIOD_MS);
  214. }
  215. cmd++;
  216. }
  217. }
  218. if (screen == SCREEN_RIGHT)
  219. {
  220. gpio_set_level(PIN_R_RST, 0);
  221. vTaskDelay(10 / portTICK_PERIOD_MS);
  222. gpio_set_level(PIN_R_RST, 1);
  223. vTaskDelay(10 / portTICK_PERIOD_MS);
  224. int cmd = 0;
  225. // Send all the commands
  226. while (init_cmds[cmd].databytes != 0xff)
  227. {
  228. #if 1
  229. epd_write_cmd(SCREEN_RIGHT, init_cmds[cmd].cmd, false);
  230. epd_write_data(SCREEN_RIGHT, init_cmds[cmd].data, init_cmds[cmd].databytes & 0x1F);
  231. #endif
  232. if (init_cmds[cmd].databytes & 0x80)
  233. {
  234. vTaskDelay(100 / portTICK_PERIOD_MS);
  235. }
  236. cmd++;
  237. }
  238. }
  239. }
  240. static void spi_get_result(spi_device_handle_t spi)
  241. {
  242. spi_transaction_t *rtrans = NULL;
  243. esp_err_t ret;
  244. spi_device_get_trans_result(spi, &rtrans, portMAX_DELAY);
  245. // assert(ret==ESP_OK);
  246. }
  247. void epd_cmd(spi_device_handle_t spi, const uint8_t cmd, bool keep_cs_active)
  248. {
  249. #if !SOFTWARE_SPI_ENABLE
  250. esp_err_t ret;
  251. spi_transaction_t t;
  252. memset(&t, 0, sizeof(t)); // Zero out the transaction
  253. t.length = 8; // Command is 8 bits
  254. t.tx_buffer = &cmd; // The data is the cmd itself
  255. t.user = (void *)0; // D/C needs to be set to 0
  256. if (keep_cs_active)
  257. {
  258. t.flags = SPI_TRANS_CS_KEEP_ACTIVE; // Keep CS active after data transfer
  259. }
  260. ret = spi_device_queue_trans(spi, &t, portMAX_DELAY);
  261. // assert(ret==ESP_OK); //Should have had no issues.
  262. spi_get_result(spi);
  263. #else
  264. SPI_Write(cmd);
  265. #endif
  266. }
  267. void epd_data(spi_device_handle_t spi, const uint8_t *data, int len)
  268. {
  269. #if !SOFTWARE_SPI_ENABLE
  270. esp_err_t ret;
  271. spi_transaction_t t;
  272. int i;
  273. if (len == 0)
  274. return; // no need to send anything
  275. if (len > SPI_MAX_LEN)
  276. {
  277. for (i = 0; i < len; i = i + SPI_MAX_LEN)
  278. {
  279. memset(&t, 0, sizeof(t)); // Zero out the transaction
  280. t.length = SPI_MAX_LEN * 8; // Len is in bytes, transaction length is in bits.
  281. t.tx_buffer = data + i; // Data
  282. t.user = (void *)1; // D/C needs to be set to 1
  283. ret = spi_device_queue_trans(spi, &t, portMAX_DELAY); // Transmit!
  284. // assert(ret==ESP_OK); //Should have had no issues.
  285. if (ret != ESP_OK)
  286. {
  287. printf("spi_device_queue_trans err:ret =%d", ret);
  288. }
  289. spi_get_result(spi);
  290. }
  291. i -= SPI_MAX_LEN;
  292. memset(&t, 0, sizeof(t)); // Zero out the transaction
  293. t.length = (len - i) * 8; // Len is in bytes, transaction length is in bits.
  294. t.tx_buffer = data + i; // Data
  295. t.user = (void *)1; // D/C needs to be set to 1
  296. ret = spi_device_queue_trans(spi, &t, portMAX_DELAY); // Transmit!
  297. // assert(ret==ESP_OK); //Should have had no issues.
  298. if (ret != ESP_OK)
  299. {
  300. printf("spi_device_queue_trans err:ret =%d", ret);
  301. }
  302. spi_get_result(spi);
  303. return;
  304. }
  305. memset(&t, 0, sizeof(t)); // Zero out the transaction
  306. t.length = len * 8; // Len is in bytes, transaction length is in bits.
  307. t.tx_buffer = data; // Data
  308. t.user = (void *)1; // D/C needs to be set to 1
  309. ret = spi_device_queue_trans(spi, &t, portMAX_DELAY); // Transmit!
  310. // assert(ret==ESP_OK); //Should have had no issues.
  311. if (ret != ESP_OK)
  312. {
  313. printf("spi_device_queue_trans err:ret =%d", ret);
  314. }
  315. spi_get_result(spi);
  316. #else
  317. {
  318. for (int i = 0; i < len; i++)
  319. {
  320. SPI_Write(data[i]);
  321. }
  322. }
  323. #endif
  324. }
  325. void epd_write_cmd(screen_t screen, unsigned char command, bool keep_cs_active)
  326. {
  327. if (screen == SCREEN_LEFT)
  328. {
  329. gpio_set_level(PIN_R_CS, 1);
  330. gpio_set_level(PIN_L_CS, 0);
  331. gpio_set_level(PIN_L_DC, 0);
  332. epd_cmd(epd_spi, command, keep_cs_active);
  333. gpio_set_level(PIN_R_CS, 1);
  334. gpio_set_level(PIN_L_CS, 1);
  335. }
  336. else if (screen == SCREEN_RIGHT)
  337. {
  338. gpio_set_level(PIN_L_CS, 1);
  339. gpio_set_level(PIN_R_CS, 0);
  340. gpio_set_level(PIN_R_DC, 0);
  341. epd_cmd(epd_spi, command, keep_cs_active);
  342. gpio_set_level(PIN_L_CS, 1);
  343. gpio_set_level(PIN_R_CS, 1);
  344. }
  345. else
  346. {
  347. ESP_LOGE(LOG_TAG, "cmd err:screen");
  348. }
  349. }
  350. void epd_write_data(screen_t screen, const uint8_t *data, int len)
  351. {
  352. if (screen == SCREEN_LEFT)
  353. {
  354. gpio_set_level(PIN_R_CS, 1);
  355. gpio_set_level(PIN_L_CS, 0);
  356. gpio_set_level(PIN_L_DC, 1);
  357. epd_data(epd_spi, data, len);
  358. gpio_set_level(PIN_L_CS, 1);
  359. }
  360. else if (screen == SCREEN_RIGHT)
  361. {
  362. gpio_set_level(PIN_L_CS, 1);
  363. gpio_set_level(PIN_R_CS, 0);
  364. gpio_set_level(PIN_R_DC, 1);
  365. epd_data(epd_spi, data, len);
  366. gpio_set_level(PIN_R_CS, 1);
  367. }
  368. else
  369. {
  370. ESP_LOGE(LOG_TAG, "data err:screen");
  371. }
  372. }
  373. bool epd_check_status(screen_t screen)
  374. {
  375. #if 1//死等
  376. if(screen == SCREEN_LEFT)
  377. {
  378. while(!gpio_get_level(PIN_L_BUSY))
  379. {
  380. };
  381. }
  382. else if(screen == SCREEN_RIGHT)
  383. {
  384. while(!gpio_get_level(PIN_R_BUSY));
  385. }
  386. else
  387. {
  388. ESP_LOGE(LOG_TAG,"check err:screen");
  389. }
  390. #else
  391. int count = 0;
  392. unsigned char busy;
  393. int64_t t_after_us = 0;
  394. int64_t t_before_us = esp_timer_get_time();
  395. // while(1)
  396. {
  397. //=1 BUSY
  398. if (screen == SCREEN_LEFT)
  399. {
  400. while (1)
  401. {
  402. busy = gpio_get_level(PIN_L_BUSY);
  403. busy = (busy & 0x01);
  404. if (busy == 1)
  405. {
  406. // printf("left lcd idle\r\n");
  407. break;
  408. }
  409. #if 0
  410. vTaskDelay(10 / portTICK_PERIOD_MS);
  411. count ++;
  412. if(count >= 200){
  413. printf("l---------------time out ---\n");
  414. break;
  415. }
  416. #else
  417. t_after_us = esp_timer_get_time();
  418. if ((t_after_us - t_before_us) > 1000 * 1000)
  419. {
  420. printf("t_before_us =%lld ,t_after_us = %lld\n", t_before_us, t_after_us);
  421. return 0;
  422. break;
  423. }
  424. #endif
  425. }
  426. }
  427. else if (screen == SCREEN_RIGHT)
  428. {
  429. // while(!gpio_get_level(PIN_R_BUSY));
  430. while (1)
  431. {
  432. busy = gpio_get_level(PIN_R_BUSY);
  433. busy = (busy & 0x01);
  434. if (busy == 1)
  435. {
  436. // printf("right lcd idle\r\n");
  437. break;
  438. }
  439. #if 0
  440. vTaskDelay(10 / portTICK_PERIOD_MS);
  441. count ++;
  442. if(count >= 200){
  443. printf("r---------------time out ---\n");
  444. break;
  445. }
  446. #else
  447. t_after_us = esp_timer_get_time();
  448. if ((t_after_us - t_before_us) > 1000 * 1000)
  449. {
  450. printf(" r t_before_us =%lld ,t_after_us = %lld\n", t_before_us, t_after_us);
  451. return 0;
  452. break;
  453. }
  454. #endif
  455. }
  456. }
  457. else
  458. {
  459. ESP_LOGE(LOG_TAG, "check err:screen");
  460. }
  461. }
  462. #endif
  463. return 1;
  464. }
  465. void deepsleep_epd_check_status(screen_t screen)
  466. {
  467. if (screen == SCREEN_LEFT)
  468. {
  469. while (!gpio_get_level(PIN_L_BUSY))
  470. ;
  471. }
  472. else if (screen == SCREEN_RIGHT)
  473. {
  474. while (!gpio_get_level(PIN_R_BUSY))
  475. ;
  476. }
  477. else
  478. {
  479. ESP_LOGE(LOG_TAG, "check err:screen");
  480. }
  481. }
  482. void epd_check_power_off(screen_t screen)
  483. {
  484. if (screen == SCREEN_LEFT)
  485. {
  486. while (gpio_get_level(PIN_L_BUSY))
  487. {
  488. vTaskDelay(20 / portTICK_PERIOD_MS);
  489. printf("left power off\r\n");
  490. }
  491. }
  492. else if (screen == SCREEN_RIGHT)
  493. {
  494. while (gpio_get_level(PIN_R_BUSY))
  495. {
  496. vTaskDelay(20 / portTICK_PERIOD_MS);
  497. printf("right power off\r\n");
  498. }
  499. }
  500. else
  501. {
  502. ESP_LOGE(LOG_TAG, "check err:screen");
  503. }
  504. }
  505. void epd_check_power_on(screen_t screen)
  506. {
  507. if (screen == SCREEN_LEFT)
  508. {
  509. while (gpio_get_level(PIN_L_BUSY))
  510. {
  511. printf("left power on\r\n");
  512. }
  513. }
  514. else if (screen == SCREEN_RIGHT)
  515. {
  516. while (gpio_get_level(PIN_R_BUSY))
  517. {
  518. printf("right power on\r\n");
  519. }
  520. }
  521. else
  522. {
  523. ESP_LOGE(LOG_TAG, "check err:screen");
  524. }
  525. }
  526. void epd_refresh(screen_t screen)
  527. {
  528. epd_write_cmd(screen, 0x12, false); // DISPLAY REFRESH
  529. // ets_delay_us(200); //!!!The delay here is necessary, 200uS at least!!!
  530. // vTaskDelay(1 / portTICK_PERIOD_MS);
  531. epd_check_status(screen); // waiting for the electronic paper//时间较长
  532. }
  533. void epd_sleep(screen_t screen)
  534. {
  535. // printf("SLEEP SCREEN =%d\r\n",screen);
  536. #if 0
  537. epd_write_cmd(screen,0x04,false); // Power ON
  538. epd_check_power_on(screen);
  539. #endif
  540. // epd_check_status(screen);
  541. // epd_refresh(screen);
  542. uint8_t tmp = 0xa5;
  543. epd_write_cmd(screen, 0x07, false);
  544. epd_write_data(screen, &tmp, 1);
  545. // vTaskDelay(20 / portTICK_PERIOD_MS);
  546. #if 0
  547. epd_write_cmd(screen,0x02,false); // Power OFF
  548. vTaskDelay(20 / portTICK_PERIOD_MS);
  549. epd_check_power_off(screen);
  550. #endif
  551. #if 1
  552. // epd_write_cmd(screen,0x02,false); // Power OFF
  553. // epd_check_status(screen);
  554. #endif
  555. }
  556. IRAM_ATTR void epd_display(screen_t screen, const unsigned char *picData)
  557. {
  558. epd_write_cmd(screen, 0x13, false);
  559. epd_check_status(screen);
  560. epd_write_data(screen, picData, 38880);
  561. epd_check_status(screen);
  562. epd_write_cmd(screen, 0x04, false); // POWER ON
  563. epd_check_status(screen); // waiting for the electronic paper IC to release the idle signal
  564. // Refresh
  565. epd_refresh(screen);
  566. epd_write_cmd(screen, 0x02, false); // Power OFF
  567. epd_check_status(screen);
  568. epd_sleep(screen);
  569. }
  570. #if 1
  571. void epd_cache(screen_t screen, const unsigned char *picData)
  572. {
  573. epd_write_cmd(screen, 0x13, false);
  574. epd_write_data(screen, picData, 38880);
  575. }
  576. void epd_cache_quick_full_screen_refresh(screen_t screen, const unsigned char *old, const unsigned char *new)
  577. {
  578. if ((screen == SCREEN_LEFT) && (old != NULL))
  579. {
  580. printf("left quick\r\n");
  581. // Reset the display
  582. gpio_set_level(PIN_L_RST, 0);
  583. vTaskDelay(10 / portTICK_PERIOD_MS);
  584. gpio_set_level(PIN_L_RST, 1);
  585. vTaskDelay(10 / portTICK_PERIOD_MS);
  586. uint8_t cmd[1] = {0};
  587. epd_write_cmd(screen, 0x00, false);
  588. cmd[0] = 0x1F;
  589. epd_write_data(screen, cmd, 1);
  590. #if 0
  591. uint8_t cmd[1]={0};
  592. epd_write_cmd(screen,0x61,false);
  593. cmd[0] = 0xF0;
  594. epd_write_data(screen,cmd,1);
  595. cmd[0] = 0x01;
  596. epd_write_data(screen,cmd,1);
  597. cmd[0] = 0xA0;
  598. epd_write_data(screen,cmd,1);
  599. #endif
  600. #if 0
  601. int cmd=0;
  602. //Send all the commands
  603. while (init_cmds[cmd].databytes!=0xff) {
  604. epd_write_cmd(SCREEN_LEFT, init_cmds[cmd].cmd, false);
  605. epd_write_data(SCREEN_LEFT, init_cmds[cmd].data, init_cmds[cmd].databytes&0x1F);
  606. epd_write_cmd(SCREEN_RIGHT, init_cmds[cmd].cmd, false);
  607. epd_write_data(SCREEN_RIGHT, init_cmds[cmd].data, init_cmds[cmd].databytes&0x1F);
  608. if (init_cmds[cmd].databytes&0x80) {
  609. vTaskDelay(100 / portTICK_PERIOD_MS);
  610. }
  611. cmd++;
  612. }
  613. #endif
  614. }
  615. if ((screen == SCREEN_RIGHT) && (old != NULL))
  616. {
  617. printf("right quick\r\n");
  618. gpio_set_level(PIN_R_RST, 0);
  619. vTaskDelay(10 / portTICK_PERIOD_MS);
  620. gpio_set_level(PIN_R_RST, 1);
  621. vTaskDelay(10 / portTICK_PERIOD_MS);
  622. // uint8_t cmd[1]={0};
  623. // epd_write_cmd(screen,0x00,false);
  624. // cmd[0] = 0x1F;
  625. // epd_write_data(screen,cmd,1);
  626. #if 0
  627. uint8_t cmd[1]={0};
  628. epd_write_cmd(screen,0x61,false);
  629. cmd[0] = 0xF0;
  630. epd_write_data(screen,cmd,1);
  631. cmd[0] = 0x01;
  632. epd_write_data(screen,cmd,1);
  633. cmd[0] = 0xA0;
  634. epd_write_data(screen,cmd,1);
  635. #endif
  636. #if 0
  637. int cmd=0;
  638. //Send all the commands
  639. while (init_cmds[cmd].databytes!=0xff) {
  640. epd_write_cmd(SCREEN_LEFT, init_cmds[cmd].cmd, false);
  641. epd_write_data(SCREEN_LEFT, init_cmds[cmd].data, init_cmds[cmd].databytes&0x1F);
  642. epd_write_cmd(SCREEN_RIGHT, init_cmds[cmd].cmd, false);
  643. epd_write_data(SCREEN_RIGHT, init_cmds[cmd].data, init_cmds[cmd].databytes&0x1F);
  644. if (init_cmds[cmd].databytes&0x80) {
  645. vTaskDelay(100 / portTICK_PERIOD_MS);
  646. }
  647. cmd++;
  648. }
  649. #endif
  650. }
  651. if (old != NULL)
  652. {
  653. epd_write_cmd(screen, 0x10, false);
  654. epd_check_status(screen);
  655. epd_write_data(screen, old, 38880);
  656. }
  657. #if 0
  658. uint8_t cmd[1]={0};
  659. epd_write_cmd(screen,0x61,false);
  660. cmd[0] = 0xF0;
  661. epd_write_data(screen,cmd,1);
  662. cmd[0] = 0x01;
  663. epd_write_data(screen,cmd,1);
  664. cmd[0] = 0xA0;
  665. epd_write_data(screen,cmd,1);
  666. #endif
  667. epd_write_cmd(screen, 0x13, false);
  668. epd_write_data(screen, new, 38880);
  669. }
  670. #endif
  671. void epd_display_partal(uint16_t Xstart, uint16_t Ystart, uint16_t Xend, uint16_t Yend,
  672. screen_t screen, const unsigned char *picData)
  673. {
  674. unsigned int row, col;
  675. uint8_t Xstart_H, Xstart_L, Ystart_H, Ystart_L, Xend_H, Xend_L, Yend_H, Yend_L;
  676. Xstart_H = ((Xstart) >> 8) & 0xff;
  677. Xstart_L = (Xstart) & 0xff;
  678. Ystart_H = ((Ystart) >> 8) & 0xff;
  679. Ystart_L = (Ystart) & 0xff;
  680. Xend_H = ((Xend - 1) >> 8) & 0xff;
  681. Xend_L = (Xend - 1) & 0xff;
  682. Yend_H = ((Yend - 1) >> 8) & 0xff;
  683. Yend_L = (Yend - 1) & 0xff;
  684. epd_write_cmd(screen, 0x91, false); // partial in
  685. epd_check_status(screen);
  686. epd_write_cmd(screen, 0x90, false);
  687. epd_check_status(screen);
  688. epd_write_data(screen, &Xstart_H, 1);
  689. epd_write_data(screen, &Xstart_L, 1);
  690. epd_write_data(screen, &Xend_H, 1);
  691. epd_write_data(screen, &Xend_L, 1);
  692. epd_write_data(screen, &Ystart_H, 1);
  693. epd_write_data(screen, &Ystart_L, 1);
  694. epd_write_data(screen, &Yend_H, 1);
  695. epd_write_data(screen, &Yend_L, 1);
  696. uint8_t tmp = 0x01;
  697. epd_write_data(screen, &tmp, 1);
  698. epd_write_cmd(screen, 0x13, false);
  699. epd_check_status(screen);
  700. epd_write_data(screen, picData, ((Yend - Ystart) * (Xend - Xstart) / 8));
  701. epd_write_cmd(screen, 0x92, false);
  702. epd_write_cmd(screen, 0x04, false); // Power ON
  703. epd_check_status(screen);
  704. epd_refresh(screen);
  705. epd_write_cmd(screen, 0x02, false); // Power OFF
  706. epd_check_status(screen);
  707. }
  708. void epd_partial_cache(uint16_t Xstart, uint16_t Ystart, uint16_t Xend, uint16_t Yend,
  709. screen_t screen, const unsigned char *picData)
  710. {
  711. unsigned int row, col;
  712. uint8_t Xstart_H, Xstart_L, Ystart_H, Ystart_L, Xend_H, Xend_L, Yend_H, Yend_L;
  713. Xstart_H = ((Xstart) >> 8) & 0xff;
  714. Xstart_L = (Xstart) & 0xff;
  715. Ystart_H = ((Ystart) >> 8) & 0xff;
  716. Ystart_L = (Ystart) & 0xff;
  717. Xend_H = ((Xend - 1) >> 8) & 0xff;
  718. Xend_L = (Xend - 1) & 0xff;
  719. Yend_H = ((Yend - 1) >> 8) & 0xff;
  720. Yend_L = (Yend - 1) & 0xff;
  721. epd_write_cmd(screen, 0x91, false); // partial in
  722. epd_check_status(screen);
  723. epd_write_cmd(screen, 0x90, false);
  724. epd_check_status(screen);
  725. epd_write_data(screen, &Xstart_H, 1);
  726. epd_write_data(screen, &Xstart_L, 1);
  727. epd_write_data(screen, &Xend_H, 1);
  728. epd_write_data(screen, &Xend_L, 1);
  729. epd_write_data(screen, &Ystart_H, 1);
  730. epd_write_data(screen, &Ystart_L, 1);
  731. epd_write_data(screen, &Yend_H, 1);
  732. epd_write_data(screen, &Yend_L, 1);
  733. uint8_t tmp = 0x01;
  734. epd_write_data(screen, &tmp, 1);
  735. epd_write_cmd(screen, 0x13, false);
  736. epd_check_status(screen);
  737. epd_write_data(screen, picData, ((Yend - Ystart) * (Xend - Xstart) / 8));
  738. epd_write_cmd(screen, 0x92, false);
  739. }
  740. void epd_cache_quick_partial_screen_refresh(screen_t screen, uint16_t Xstart, uint16_t Ystart, uint16_t Xend, uint16_t Yend, const unsigned char *old, const unsigned char *new)
  741. {
  742. if ((screen == SCREEN_LEFT) && (old != NULL))
  743. {
  744. printf("left partial quick\r\n");
  745. // Reset the display
  746. gpio_set_level(PIN_L_RST, 0);
  747. vTaskDelay(10 / portTICK_PERIOD_MS);
  748. gpio_set_level(PIN_L_RST, 1);
  749. vTaskDelay(10 / portTICK_PERIOD_MS);
  750. uint8_t cmd[1] = {0};
  751. epd_write_cmd(screen, 0x00, false);
  752. cmd[0] = 0x1F;
  753. epd_write_data(screen, cmd, 1);
  754. }
  755. if ((screen == SCREEN_RIGHT) && (old != NULL))
  756. {
  757. printf("right partial quick\r\n");
  758. gpio_set_level(PIN_R_RST, 0);
  759. vTaskDelay(10 / portTICK_PERIOD_MS);
  760. gpio_set_level(PIN_R_RST, 1);
  761. vTaskDelay(10 / portTICK_PERIOD_MS);
  762. }
  763. #if 0
  764. if (old !=NULL)
  765. {
  766. epd_write_cmd(screen,0x10,false);
  767. epd_write_data(screen,old,38880);
  768. }
  769. epd_write_cmd(screen,0x13,false);
  770. epd_write_data(screen,new,38880);
  771. #endif
  772. unsigned int row, col;
  773. uint8_t Xstart_H, Xstart_L, Ystart_H, Ystart_L, Xend_H, Xend_L, Yend_H, Yend_L;
  774. Xstart_H = ((Xstart) >> 8) & 0xff;
  775. Xstart_L = (Xstart) & 0xff;
  776. Ystart_H = ((Ystart) >> 8) & 0xff;
  777. Ystart_L = (Ystart) & 0xff;
  778. Xend_H = ((Xend - 1) >> 8) & 0xff;
  779. Xend_L = (Xend - 1) & 0xff;
  780. Yend_H = ((Yend - 1) >> 8) & 0xff;
  781. Yend_L = (Yend - 1) & 0xff;
  782. epd_write_cmd(screen, 0x91, false); // partial in
  783. epd_check_status(screen);
  784. epd_write_cmd(screen, 0x90, false);
  785. epd_check_status(screen);
  786. epd_write_data(screen, &Xstart_H, 1);
  787. epd_write_data(screen, &Xstart_L, 1);
  788. epd_write_data(screen, &Xend_H, 1);
  789. epd_write_data(screen, &Xend_L, 1);
  790. epd_write_data(screen, &Ystart_H, 1);
  791. epd_write_data(screen, &Ystart_L, 1);
  792. epd_write_data(screen, &Yend_H, 1);
  793. epd_write_data(screen, &Yend_L, 1);
  794. uint8_t tmp = 0x01;
  795. epd_write_data(screen, &tmp, 1);
  796. // epd_write_cmd(screen,0x13,false);
  797. // epd_check_status(screen);
  798. printf("1\r\n");
  799. if (old != NULL)
  800. {
  801. printf("2\r\n");
  802. epd_write_cmd(screen, 0x10, false);
  803. epd_check_status(screen);
  804. epd_write_data(screen, old, ((Yend - Ystart) * (Xend - Xstart) / 8));
  805. epd_write_cmd(screen, 0x92, false);
  806. }
  807. printf("3\r\n");
  808. epd_write_cmd(screen, 0x13, false);
  809. epd_check_status(screen);
  810. epd_write_data(screen, new, ((Yend - Ystart) * (Xend - Xstart) / 8));
  811. epd_write_cmd(screen, 0x92, false);
  812. }
  813. static uint8_t get_data(uint8_t *color, uint8_t pix_size)
  814. {
  815. uint8_t data = 0;
  816. int i = 0, j = 0;
  817. for (i = 0; i < 8; i++)
  818. {
  819. data = data | (color[j] << (7 - i));
  820. j = j + pix_size;
  821. }
  822. return data;
  823. }
  824. void epd_partial_cache1(uint16_t Xstart, uint16_t Ystart, uint16_t Xend, uint16_t Yend,
  825. screen_t screen, const unsigned char *picData)
  826. {
  827. unsigned int row, col;
  828. uint8_t Xstart_H, Xstart_L, Ystart_H, Ystart_L, Xend_H, Xend_L, Yend_H, Yend_L;
  829. Xstart_H = ((Xstart) >> 8) & 0xff;
  830. Xstart_L = (Xstart) & 0xff;
  831. Ystart_H = ((Ystart) >> 8) & 0xff;
  832. Ystart_L = (Ystart) & 0xff;
  833. Xend_H = ((Xend - 1) >> 8) & 0xff;
  834. Xend_L = (Xend - 1) & 0xff;
  835. Yend_H = ((Yend - 1) >> 8) & 0xff;
  836. Yend_L = (Yend - 1) & 0xff;
  837. epd_write_cmd(screen, 0x91, false); // partial in
  838. epd_check_status(screen);
  839. epd_write_cmd(screen, 0x90, false);
  840. epd_check_status(screen);
  841. epd_write_data(screen, &Xstart_H, 1);
  842. epd_write_data(screen, &Xstart_L, 1);
  843. epd_write_data(screen, &Xend_H, 1);
  844. epd_write_data(screen, &Xend_L, 1);
  845. epd_write_data(screen, &Ystart_H, 1);
  846. epd_write_data(screen, &Ystart_L, 1);
  847. epd_write_data(screen, &Yend_H, 1);
  848. epd_write_data(screen, &Yend_L, 1);
  849. uint8_t tmp = 0x01;
  850. epd_write_data(screen, &tmp, 1);
  851. epd_write_cmd(screen, 0x13, false);
  852. epd_check_status(screen);
  853. #if 0
  854. epd_write_data(screen,picData,((Yend - Ystart)*(Xend - Xstart)));
  855. #else
  856. uint8_t data;
  857. // disp_drv.hor_res = 648;
  858. // disp_drv.ver_res = 480;
  859. for (int i = 480 - 1; i >= 0; i--)
  860. {
  861. for (int j = 0; j < 648;)
  862. {
  863. data = get_data(&picData[j + i * 648 * 1], 1);
  864. // printf("%02x",data);
  865. epd_write_data(screen, &data, 1);
  866. j = j + 8 * 1;
  867. }
  868. }
  869. #endif
  870. epd_write_cmd(screen, 0x92, false);
  871. }
  872. void epd_powerOn_refresh(screen_t screen)
  873. {
  874. epd_write_cmd(screen, 0x04, false); // Power ON
  875. epd_check_status(screen);
  876. epd_refresh(screen);
  877. epd_write_cmd(screen, 0x02, false); // Power OFF
  878. epd_check_status(screen);
  879. }
  880. // 定义最大重复计数
  881. #define MAX_REPEAT 255
  882. // 压缩函数
  883. unsigned int compressRLE(uint8_t *input, unsigned int size, uint8_t *output)
  884. {
  885. unsigned int i = 0;
  886. unsigned int outputSize = 0;
  887. while (i < size)
  888. {
  889. uint8_t current = input[i];
  890. unsigned int count = 1;
  891. i++;
  892. while (i < size && input[i] == current && count < MAX_REPEAT)
  893. {
  894. count++;
  895. i++;
  896. }
  897. // 输出像素值和计数值到output
  898. output[outputSize++] = current;
  899. output[outputSize++] = count;
  900. }
  901. return outputSize;
  902. }
  903. // 解压函数
  904. unsigned int decompressRLE(uint8_t *input, unsigned int size, uint8_t *output)
  905. {
  906. unsigned int i = 0;
  907. unsigned int outputSize = 0;
  908. while (i < size)
  909. {
  910. uint8_t current = input[i];
  911. i++;
  912. unsigned int count = input[i];
  913. i++;
  914. // 输出像素值count次到output
  915. for (unsigned int j = 0; j < count; j++)
  916. {
  917. output[outputSize++] = current;
  918. }
  919. }
  920. return outputSize;
  921. }
  922. void test()
  923. {
  924. // 定义图像数据(十六进制数组)
  925. uint8_t imageData[] = {0x12, 0x12, 0x12, 0x13, 0x14, 0x14, 0x14, 0x15, 0x15, 0x15};
  926. int imageSize = sizeof(imageData) / sizeof(imageData[0]);
  927. printf("原始图像数据:\n");
  928. for (int i = 0; i < imageSize; i++)
  929. {
  930. printf("0x%02X ", imageData[i]);
  931. }
  932. printf("\n");
  933. uint8_t compressedData[2 * imageSize]; // 假设压缩后的数据不会超过原数据的两倍大小
  934. uint8_t decompressedData[imageSize]; // 假设解压后的数据不会超过原数据大小
  935. printf("压缩后的数据:\n");
  936. int compressedSize = compressRLE(imageData, imageSize, compressedData);
  937. for (int i = 0; i < compressedSize; i++)
  938. {
  939. printf("0x%02X ", compressedData[i]);
  940. }
  941. printf("\n");
  942. printf("解压后的数据:\n");
  943. int decompressedSize = decompressRLE(compressedData, compressedSize, decompressedData);
  944. for (int i = 0; i < decompressedSize; i++)
  945. {
  946. printf("0x%02X ", decompressedData[i]);
  947. }
  948. printf("\n");
  949. printf("压缩后的数据大小:%d 字节\n", compressedSize);
  950. printf("解压后的数据大小:%d 字节\n", decompressedSize);
  951. }
  952. // uint8_t tmp[38880];
  953. IRAM_ATTR bool epd_cache_quick(screen_t screen, const unsigned char *old, const unsigned char *new)
  954. {
  955. bool ret = false;
  956. if ((screen == SCREEN_LEFT) && (old != NULL))
  957. {
  958. printf("SCREEN_LEFT quick\r\n");
  959. // Reset the display
  960. gpio_set_level(PIN_L_RST, 0);
  961. vTaskDelay(10 / portTICK_PERIOD_MS);
  962. gpio_set_level(PIN_L_RST, 1);
  963. vTaskDelay(10 / portTICK_PERIOD_MS);
  964. ret = epd_check_status(screen);
  965. if (!ret)
  966. {
  967. printf("err:2busy !!!!!\n");
  968. goto err;
  969. }
  970. uint8_t cmd[1] = {0};
  971. epd_write_cmd(screen, 0x00, false);
  972. cmd[0] = 0x1F;
  973. epd_write_data(screen, cmd, 1);
  974. }
  975. if ((screen == SCREEN_RIGHT) && (old != NULL))
  976. {
  977. printf("SCREEN_RIGHT quick\r\n");
  978. gpio_set_level(PIN_R_RST, 0);
  979. vTaskDelay(10 / portTICK_PERIOD_MS);
  980. gpio_set_level(PIN_R_RST, 1);
  981. vTaskDelay(10 / portTICK_PERIOD_MS);
  982. ret = epd_check_status(screen);
  983. if (!ret)
  984. {
  985. printf("err:3busy !!!!!\n");
  986. goto err;
  987. }
  988. uint8_t cmd2[1] = {0};
  989. epd_write_cmd(screen, 0x00, false);
  990. cmd2[0] = 0x1F;
  991. epd_write_data(screen, cmd2, 1);
  992. }
  993. #if 1
  994. if (old != NULL)
  995. {
  996. epd_write_cmd(screen, 0x10, false);
  997. ret = epd_check_status(screen);
  998. if (!ret)
  999. {
  1000. printf("err:busy !!!!!\n");
  1001. goto err;
  1002. }
  1003. epd_write_data(screen, old, 38880);
  1004. ret = epd_check_status(screen);
  1005. if (!ret)
  1006. {
  1007. printf("err:2busy !!!!!\n");
  1008. goto err;
  1009. }
  1010. }
  1011. #else
  1012. epd_write_cmd(screen, 0x10, false);
  1013. epd_check_status(screen);
  1014. memcpy(&tmp, new, 38880);
  1015. for (int i = 0; i < 38880; i++)
  1016. {
  1017. tmp[i] = ~tmp[i];
  1018. }
  1019. epd_write_data(screen, &tmp, 38880);
  1020. #endif
  1021. epd_write_cmd(screen, 0x13, false);
  1022. ret = epd_check_status(screen);
  1023. if (!ret)
  1024. {
  1025. printf("err:2busy !!!!!\n");
  1026. goto err;
  1027. }
  1028. epd_write_data(screen, new, 38880);
  1029. ret = epd_check_status(screen);
  1030. if (!ret)
  1031. {
  1032. printf("err:2busy !!!!!\n");
  1033. goto err;
  1034. }
  1035. #if 0
  1036. if(screen == SCREEN_LEFT)
  1037. {
  1038. left_refresh_complete = true;
  1039. }
  1040. if(screen == SCREEN_RIGHT)
  1041. {
  1042. right_refresh_complete = true;
  1043. }
  1044. #else
  1045. // vTaskDelay(0000 / portTICK_PERIOD_MS);
  1046. // refresh_timer_start(500);
  1047. // xTimerStart(slow_display_timer, 0); //开始定时器
  1048. #endif
  1049. return true;
  1050. err:
  1051. ESP_LOGE(LOG_TAG, "err:epd_check_status");
  1052. return false;
  1053. }
  1054. void epd_powerOn_refresh_sleep(screen_t screen)
  1055. {
  1056. epd_write_cmd(screen, 0x04, false); // Power ON
  1057. epd_check_status(screen);
  1058. epd_refresh(screen);
  1059. epd_sleep(screen);
  1060. }
  1061. void deepsleep_epd_powerOn_refresh_sleep(screen_t screen)
  1062. {
  1063. epd_write_cmd(screen, 0x04, false); // Power ON
  1064. // epd_check_status(screen);
  1065. deepsleep_epd_check_status(screen);
  1066. epd_write_cmd(screen, 0x12, false); // DISPLAY REFRESH
  1067. deepsleep_epd_check_status(screen);
  1068. // epd_refresh(screen);
  1069. // epd_sleep(screen);
  1070. }
  1071. #else /*HAREWARE_SPI*/
  1072. static void task_delay_ms(uint32_t ms_count)
  1073. {
  1074. vTaskDelay(ms_count / portTICK_PERIOD_MS);
  1075. }
  1076. void epd_pin_init(struct EPD_INFO_SET *epd_pin_set)
  1077. {
  1078. esp_log_level_set("gpio", ESP_LOG_NONE); // close gpio logging
  1079. ESP_LOGI(LOG_TAG, "epd_pin_init");
  1080. #define EPD_OUT_PIN_SEL ((1ULL << epd_pin_set->sclk_pin) | \
  1081. (1ULL << epd_pin_set->sda_pin) | \
  1082. (1ULL << epd_pin_set->res_pin) | \
  1083. (1ULL << epd_pin_set->dc_pin) | \
  1084. (1ULL << epd_pin_set->cs_pin))
  1085. #define EPD_IN_PIN_SEL (1ULL << epd_pin_set->busy_pin)
  1086. gpio_config_t epd_pin_cfg = {};
  1087. epd_pin_cfg.intr_type = GPIO_INTR_DISABLE;
  1088. epd_pin_cfg.mode = GPIO_MODE_OUTPUT;
  1089. epd_pin_cfg.pin_bit_mask = EPD_OUT_PIN_SEL;
  1090. epd_pin_cfg.pull_down_en = 0;
  1091. epd_pin_cfg.pull_up_en = 0;
  1092. gpio_config(&epd_pin_cfg);
  1093. epd_pin_cfg.intr_type = GPIO_INTR_DISABLE;
  1094. epd_pin_cfg.mode = GPIO_MODE_INPUT;
  1095. epd_pin_cfg.pin_bit_mask = EPD_IN_PIN_SEL;
  1096. epd_pin_cfg.pull_down_en = 0;
  1097. epd_pin_cfg.pull_up_en = 1;
  1098. gpio_config(&epd_pin_cfg);
  1099. esp_log_level_set("gpio", LOG_LOCAL_LEVEL);
  1100. }
  1101. void epd_spi_write(struct EPD_INFO_SET *epd_pin_set, unsigned char value)
  1102. {
  1103. int i = 0, data = value;
  1104. epd_set_level(epd_pin_set->sclk_pin, LOW_LEVEL);
  1105. for (i = 0; i < 8; i++)
  1106. {
  1107. if (data & 0x0080)
  1108. {
  1109. epd_set_level(epd_pin_set->sda_pin, HIGH_LEVEL);
  1110. }
  1111. else
  1112. {
  1113. epd_set_level(epd_pin_set->sda_pin, LOW_LEVEL);
  1114. }
  1115. // ets_delay_us(1);
  1116. epd_set_level(epd_pin_set->sclk_pin, HIGH_LEVEL);
  1117. // ets_delay_us(1);
  1118. epd_set_level(epd_pin_set->sclk_pin, LOW_LEVEL);
  1119. data = ((data) << 1 & 0xff);
  1120. }
  1121. }
  1122. void epd_write_cmd(struct EPD_INFO_SET *epd_pin_set, unsigned char command)
  1123. {
  1124. epd_set_level(epd_pin_set->cs_pin, LOW_LEVEL);
  1125. epd_set_level(epd_pin_set->dc_pin, LOW_LEVEL);
  1126. epd_spi_write(epd_pin_set, command);
  1127. epd_set_level(epd_pin_set->cs_pin, HIGH_LEVEL);
  1128. }
  1129. void epd_write_data(struct EPD_INFO_SET *epd_pin_set, unsigned char command)
  1130. {
  1131. epd_set_level(epd_pin_set->cs_pin, LOW_LEVEL);
  1132. epd_set_level(epd_pin_set->dc_pin, HIGH_LEVEL);
  1133. epd_spi_write(epd_pin_set, command);
  1134. epd_set_level(epd_pin_set->cs_pin, HIGH_LEVEL);
  1135. // ets_delay_us(2);
  1136. }
  1137. void epd_init(struct EPD_INFO_SET *epd_pin_set)
  1138. {
  1139. epd_set_level(epd_pin_set->res_pin, LOW_LEVEL);
  1140. task_delay_ms(10);
  1141. epd_set_level(epd_pin_set->res_pin, HIGH_LEVEL);
  1142. task_delay_ms(10);
  1143. }
  1144. void epd_check_status(struct EPD_INFO_SET *epd_pin_set)
  1145. {
  1146. while (!epd_get_level(epd_pin_set->busy_pin))
  1147. ;
  1148. }
  1149. void epd_refresh(struct EPD_INFO_SET *epd_pin_set)
  1150. {
  1151. epd_write_cmd(epd_pin_set, 0x12); // DISPLAY REFRESH
  1152. // ets_delay_us(200); //!!!The delay here is necessary, 200uS at least!!!
  1153. task_delay_ms(1);
  1154. epd_check_status(epd_pin_set); // waiting for the electronic paper
  1155. }
  1156. void epd_sleep(struct EPD_INFO_SET *epd_pin_set)
  1157. {
  1158. epd_write_cmd(epd_pin_set, 0x07);
  1159. epd_write_data(epd_pin_set, 0xa5);
  1160. }
  1161. void epd_screen_init(struct EPD_INFO_SET *epd_pin_set)
  1162. {
  1163. epd_pin_init(epd_pin_set);
  1164. epd_init(epd_pin_set);
  1165. epd_write_cmd(epd_pin_set, 0x00);
  1166. epd_write_data(epd_pin_set, 0x1f);
  1167. epd_write_data(epd_pin_set, 0x09);
  1168. epd_write_cmd(epd_pin_set, 0x50);
  1169. epd_write_data(epd_pin_set, 0x18);
  1170. epd_write_data(epd_pin_set, 0x07);
  1171. epd_write_cmd(epd_pin_set, 0xe0);
  1172. epd_write_data(epd_pin_set, 0x02);
  1173. epd_write_cmd(epd_pin_set, 0xe5);
  1174. epd_write_data(epd_pin_set, 0x5c);
  1175. }
  1176. void epd_clear_black(struct EPD_INFO_SET *epd_pin_set)
  1177. {
  1178. unsigned int i;
  1179. epd_write_cmd(epd_pin_set, 0x13);
  1180. for (i = 0; i < 38880; i++)
  1181. {
  1182. epd_write_data(epd_pin_set, 0xff);
  1183. }
  1184. epd_write_cmd(epd_pin_set, 0x04); // POWER ON
  1185. epd_check_status(epd_pin_set); // waiting for the electronic paper IC to release the idle signal
  1186. epd_refresh(epd_pin_set);
  1187. epd_write_cmd(epd_pin_set, 0x02); // Power OFF
  1188. epd_check_status(epd_pin_set);
  1189. }
  1190. void epd_clear_write(struct EPD_INFO_SET *epd_pin_set)
  1191. {
  1192. unsigned int i;
  1193. epd_write_cmd(epd_pin_set, 0x13);
  1194. for (i = 0; i < 38880; i++)
  1195. {
  1196. epd_write_data(epd_pin_set, 0x00);
  1197. }
  1198. epd_write_cmd(epd_pin_set, 0x04); // POWER ON
  1199. epd_check_status(epd_pin_set); // waiting for the electronic paper IC to release the idle signal
  1200. // Refresh
  1201. epd_refresh(epd_pin_set);
  1202. epd_write_cmd(epd_pin_set, 0x02); // Power OFF
  1203. epd_check_status(epd_pin_set);
  1204. }
  1205. // PAINT left_screen_paint;
  1206. // PAINT right_screen_paint;
  1207. void epd_display(struct EPD_INFO_SET *epd_pin_set, const unsigned char *picData)
  1208. {
  1209. unsigned int i;
  1210. epd_write_cmd(epd_pin_set, 0x13);
  1211. for (i = 0; i < 38880; i++)
  1212. {
  1213. epd_write_data(epd_pin_set, picData[i]);
  1214. }
  1215. epd_write_cmd(epd_pin_set, 0x04); // POWER ON
  1216. epd_check_status(epd_pin_set); // waiting for the electronic paper IC to release the idle signal
  1217. // Refresh
  1218. epd_refresh(epd_pin_set);
  1219. epd_write_cmd(epd_pin_set, 0x02); // Power OFF
  1220. epd_check_status(epd_pin_set);
  1221. }
  1222. void epd_cache(struct EPD_INFO_SET *epd_pin_set, const unsigned char *picData)
  1223. {
  1224. unsigned int i;
  1225. epd_write_cmd(epd_pin_set, 0x13);
  1226. for (i = 0; i < 38880; i++)
  1227. {
  1228. epd_write_data(epd_pin_set, picData[i]);
  1229. }
  1230. }
  1231. void epd_display_partal(uint16_t Xstart, uint16_t Ystart, uint16_t Xend, uint16_t Yend,
  1232. struct EPD_INFO_SET *epd_pin_set, const unsigned char *picData)
  1233. {
  1234. unsigned int row, col;
  1235. uint8_t Xstart_H, Xstart_L, Ystart_H, Ystart_L, Xend_H, Xend_L, Yend_H, Yend_L;
  1236. Xstart_H = ((Xstart) >> 8) & 0xff;
  1237. Xstart_L = (Xstart) & 0xff;
  1238. Ystart_H = ((Ystart) >> 8) & 0xff;
  1239. Ystart_L = (Ystart) & 0xff;
  1240. Xend_H = ((Xend - 1) >> 8) & 0xff;
  1241. Xend_L = (Xend - 1) & 0xff;
  1242. Yend_H = ((Yend - 1) >> 8) & 0xff;
  1243. Yend_L = (Yend - 1) & 0xff;
  1244. epd_write_cmd(epd_pin_set, 0x91); // partial in
  1245. epd_check_status(epd_pin_set);
  1246. epd_write_cmd(epd_pin_set, 0x90);
  1247. epd_check_status(epd_pin_set);
  1248. epd_write_data(epd_pin_set, Xstart_H);
  1249. epd_write_data(epd_pin_set, Xstart_L);
  1250. epd_write_data(epd_pin_set, Xend_H);
  1251. epd_write_data(epd_pin_set, Xend_L);
  1252. epd_write_data(epd_pin_set, Ystart_H);
  1253. epd_write_data(epd_pin_set, Ystart_L);
  1254. epd_write_data(epd_pin_set, Yend_H);
  1255. epd_write_data(epd_pin_set, Yend_L);
  1256. epd_write_data(epd_pin_set, 0x01);
  1257. epd_write_cmd(epd_pin_set, 0x13);
  1258. epd_check_status(epd_pin_set);
  1259. int i = 0;
  1260. for (col = 0; col < (Yend - Ystart); col++)
  1261. {
  1262. for (row = 0; row < ((Xend - Xstart) / 8); row++)
  1263. {
  1264. epd_write_data(epd_pin_set, picData[i++]);
  1265. }
  1266. }
  1267. epd_write_cmd(epd_pin_set, 0x92);
  1268. epd_write_cmd(epd_pin_set, 0x04); // Power ON
  1269. epd_check_status(epd_pin_set);
  1270. epd_refresh(epd_pin_set);
  1271. epd_write_cmd(epd_pin_set, 0x02); // Power OFF
  1272. epd_check_status(epd_pin_set);
  1273. }
  1274. void epd_partial_cache(uint16_t Xstart, uint16_t Ystart, uint16_t Xend, uint16_t Yend,
  1275. struct EPD_INFO_SET *epd_pin_set, const unsigned char *picData)
  1276. {
  1277. unsigned int row, col;
  1278. uint8_t Xstart_H, Xstart_L, Ystart_H, Ystart_L, Xend_H, Xend_L, Yend_H, Yend_L;
  1279. Xstart_H = ((Xstart) >> 8) & 0xff;
  1280. Xstart_L = (Xstart) & 0xff;
  1281. Ystart_H = ((Ystart) >> 8) & 0xff;
  1282. Ystart_L = (Ystart) & 0xff;
  1283. Xend_H = ((Xend - 1) >> 8) & 0xff;
  1284. Xend_L = (Xend - 1) & 0xff;
  1285. Yend_H = ((Yend - 1) >> 8) & 0xff;
  1286. Yend_L = (Yend - 1) & 0xff;
  1287. epd_write_cmd(epd_pin_set, 0x91); // partial in
  1288. epd_check_status(epd_pin_set);
  1289. epd_write_cmd(epd_pin_set, 0x90);
  1290. epd_check_status(epd_pin_set);
  1291. epd_write_data(epd_pin_set, Xstart_H);
  1292. epd_write_data(epd_pin_set, Xstart_L);
  1293. epd_write_data(epd_pin_set, Xend_H);
  1294. epd_write_data(epd_pin_set, Xend_L);
  1295. epd_write_data(epd_pin_set, Ystart_H);
  1296. epd_write_data(epd_pin_set, Ystart_L);
  1297. epd_write_data(epd_pin_set, Yend_H);
  1298. epd_write_data(epd_pin_set, Yend_L);
  1299. epd_write_data(epd_pin_set, 0x01);
  1300. epd_write_cmd(epd_pin_set, 0x13);
  1301. epd_check_status(epd_pin_set);
  1302. int i = 0;
  1303. for (col = 0; col < (Yend - Ystart); col++)
  1304. {
  1305. for (row = 0; row < ((Xend - Xstart) / 8); row++)
  1306. {
  1307. epd_write_data(epd_pin_set, picData[i++]);
  1308. }
  1309. }
  1310. epd_write_cmd(epd_pin_set, 0x92);
  1311. }
  1312. void epd_powerOn_refresh(struct EPD_INFO_SET *epd_pin_set)
  1313. {
  1314. epd_write_cmd(epd_pin_set, 0x04); // Power ON
  1315. epd_check_status(epd_pin_set);
  1316. epd_refresh(epd_pin_set);
  1317. epd_write_cmd(epd_pin_set, 0x02); // Power OFF
  1318. epd_check_status(epd_pin_set);
  1319. }
  1320. #endif /*HAREWARE_SPI*/