EPD.c 44 KB

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