EPD.c 44 KB

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