EPD.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614
  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 = true;
  13. bool is_epd_ok_cplt = true;
  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. int i;
  309. for (i = 0; i < len; i = i + SPI_MAX_LEN)
  310. {
  311. memset(&t, 0, sizeof(t)); // Zero out the transaction
  312. t.length = SPI_MAX_LEN * 8; // Len is in bytes, transaction length is in bits.
  313. t.tx_buffer = data + i; // Data
  314. t.user = (void *)screen; // D/C needs to be set to 1
  315. ret = user_spi_device_queue_trans(spi, &t, portMAX_DELAY); // Transmit!
  316. // assert(ret==ESP_OK); //Should have had no issues.
  317. spi_get_result(spi);
  318. // printf("-next-%d\n",len);
  319. }
  320. i -= SPI_MAX_LEN;
  321. memset(&t, 0, sizeof(t)); // Zero out the transaction
  322. t.length = (len - i) * 8; // Len is in bytes, transaction length is in bits.
  323. t.tx_buffer = data + i; // Data
  324. t.user = (void *)screen; // D/C needs to be set to 1
  325. ret = user_spi_device_queue_trans(spi, &t, portMAX_DELAY); // Transmit!
  326. assert(ret == ESP_OK); // Should have had no issues.
  327. spi_get_result(spi);
  328. // printf("-next2--%d\n", len);
  329. return;
  330. }
  331. memset(&t, 0, sizeof(t)); // Zero out the transaction
  332. t.length = len * 8; // Len is in bytes, transaction length is in bits.
  333. t.tx_buffer = data; // Data
  334. t.user = (void *)screen; // D/C needs to be set to 1
  335. ret = user_spi_device_queue_trans(spi, &t, portMAX_DELAY); // Transmit!
  336. // assert(ret==ESP_OK); //Should have had no issues.
  337. if (ret != ESP_OK)
  338. {
  339. printf("spi_device_queue_trans err:ret =%d", ret);
  340. }
  341. spi_get_result(spi);
  342. #else
  343. {
  344. for (int i = 0; i < len; i++)
  345. {
  346. SPI_Write(data[i]);
  347. }
  348. }
  349. #endif
  350. }
  351. void epd_write_cmd(screen_t screen, unsigned char command, bool keep_cs_active)
  352. {
  353. if (screen == SCREEN_LEFT)
  354. {
  355. gpio_set_level(PIN_R_CS, 1);
  356. gpio_set_level(PIN_L_CS, 0);
  357. gpio_set_level(PIN_L_DC, 0);
  358. epd_cmd(epd_spi, command, keep_cs_active);
  359. gpio_set_level(PIN_R_CS, 1);
  360. gpio_set_level(PIN_L_CS, 1);
  361. }
  362. else if (screen == SCREEN_RIGHT)
  363. {
  364. gpio_set_level(PIN_L_CS, 1);
  365. gpio_set_level(PIN_R_CS, 0);
  366. gpio_set_level(PIN_R_DC, 0);
  367. epd_cmd(epd_spi, command, keep_cs_active);
  368. gpio_set_level(PIN_L_CS, 1);
  369. gpio_set_level(PIN_R_CS, 1);
  370. }
  371. else
  372. {
  373. ESP_LOGE(LOG_TAG, "cmd err:screen");
  374. }
  375. }
  376. void epd_write_data(screen_t screen, const uint8_t *data, int len)
  377. {
  378. if (screen == SCREEN_LEFT)
  379. {
  380. gpio_set_level(PIN_R_CS, 1);
  381. gpio_set_level(PIN_L_CS, 0);
  382. gpio_set_level(PIN_L_DC, 1);
  383. epd_data(screen, epd_spi, data, len);
  384. gpio_set_level(PIN_L_CS, 1);
  385. }
  386. else if (screen == SCREEN_RIGHT)
  387. {
  388. gpio_set_level(PIN_L_CS, 1);
  389. gpio_set_level(PIN_R_CS, 0);
  390. gpio_set_level(PIN_R_DC, 1);
  391. epd_data(screen, epd_spi, data, len);
  392. gpio_set_level(PIN_R_CS, 1);
  393. }
  394. else
  395. {
  396. ESP_LOGE(LOG_TAG, "data err:screen");
  397. }
  398. }
  399. bool epd_check_status(screen_t screen)
  400. {
  401. #if 0 // 死等
  402. if(screen == SCREEN_LEFT)
  403. {
  404. while(!gpio_get_level(PIN_L_BUSY))
  405. {
  406. };
  407. }
  408. else if(screen == SCREEN_RIGHT)
  409. {
  410. while(!gpio_get_level(PIN_R_BUSY));
  411. }
  412. else
  413. {
  414. ESP_LOGE(LOG_TAG,"check err:screen");
  415. }
  416. #else
  417. int count = 0;
  418. unsigned char busy;
  419. int64_t t_after_us = 0;
  420. int64_t t_before_us = esp_timer_get_time();
  421. // while(1)
  422. {
  423. //=1 BUSY
  424. if (screen == SCREEN_LEFT)
  425. {
  426. while (1)
  427. {
  428. busy = gpio_get_level(PIN_L_BUSY);
  429. busy = (busy & 0x01);
  430. if (busy == 1)
  431. {
  432. // printf("left lcd idle\r\n");
  433. break;
  434. }
  435. #if 0
  436. vTaskDelay(10 / portTICK_PERIOD_MS);
  437. count ++;
  438. if(count >= 200){
  439. printf("l---------------time out ---\n");
  440. break;
  441. }
  442. #else
  443. t_after_us = esp_timer_get_time();
  444. if ((t_after_us - t_before_us) > 1000 * 1000)
  445. {
  446. // printf("t_before_us =%lld ,t_after_us = %lld\n", t_before_us, t_after_us);
  447. return 0;
  448. break;
  449. }
  450. #endif
  451. }
  452. }
  453. else if (screen == SCREEN_RIGHT)
  454. {
  455. // while(!gpio_get_level(PIN_R_BUSY));
  456. while (1)
  457. {
  458. busy = gpio_get_level(PIN_R_BUSY);
  459. busy = (busy & 0x01);
  460. if (busy == 1)
  461. {
  462. // printf("right lcd idle\r\n");
  463. break;
  464. }
  465. #if 0
  466. vTaskDelay(10 / portTICK_PERIOD_MS);
  467. count ++;
  468. if(count >= 200){
  469. printf("r---------------time out ---\n");
  470. break;
  471. }
  472. #else
  473. t_after_us = esp_timer_get_time();
  474. if ((t_after_us - t_before_us) > 1000 * 1000)
  475. {
  476. // printf(" r t_before_us =%lld ,t_after_us = %lld\n", t_before_us, t_after_us);
  477. return 0;
  478. break;
  479. }
  480. #endif
  481. }
  482. }
  483. else
  484. {
  485. ESP_LOGE(LOG_TAG, "check err:screen");
  486. }
  487. }
  488. #endif
  489. return 1;
  490. }
  491. void deepsleep_epd_check_status(screen_t screen)
  492. {
  493. if (screen == SCREEN_LEFT)
  494. {
  495. while (!gpio_get_level(PIN_L_BUSY))
  496. ;
  497. }
  498. else if (screen == SCREEN_RIGHT)
  499. {
  500. while (!gpio_get_level(PIN_R_BUSY))
  501. ;
  502. }
  503. else
  504. {
  505. ESP_LOGE(LOG_TAG, "check err:screen");
  506. }
  507. }
  508. void epd_check_power_off(screen_t screen)
  509. {
  510. if (screen == SCREEN_LEFT)
  511. {
  512. while (gpio_get_level(PIN_L_BUSY))
  513. {
  514. vTaskDelay(20 / portTICK_PERIOD_MS);
  515. printf("left power off\r\n");
  516. }
  517. }
  518. else if (screen == SCREEN_RIGHT)
  519. {
  520. while (gpio_get_level(PIN_R_BUSY))
  521. {
  522. vTaskDelay(20 / portTICK_PERIOD_MS);
  523. printf("right power off\r\n");
  524. }
  525. }
  526. else
  527. {
  528. ESP_LOGE(LOG_TAG, "check err:screen");
  529. }
  530. }
  531. void epd_check_power_on(screen_t screen)
  532. {
  533. if (screen == SCREEN_LEFT)
  534. {
  535. while (gpio_get_level(PIN_L_BUSY))
  536. {
  537. printf("left power on\r\n");
  538. }
  539. }
  540. else if (screen == SCREEN_RIGHT)
  541. {
  542. while (gpio_get_level(PIN_R_BUSY))
  543. {
  544. printf("right power on\r\n");
  545. }
  546. }
  547. else
  548. {
  549. ESP_LOGE(LOG_TAG, "check err:screen");
  550. }
  551. }
  552. void epd_refresh(screen_t screen)
  553. {
  554. epd_write_cmd(screen, 0x12, false); // DISPLAY REFRESH
  555. // ets_delay_us(200); //!!!The delay here is necessary, 200uS at least!!!
  556. // vTaskDelay(1 / portTICK_PERIOD_MS);
  557. epd_check_status(screen); // waiting for the electronic paper//时间较长
  558. }
  559. void epd_sleep(screen_t screen)
  560. {
  561. // printf("SLEEP SCREEN =%d\r\n",screen);
  562. #if 0
  563. epd_write_cmd(screen,0x04,false); // Power ON
  564. epd_check_power_on(screen);
  565. #endif
  566. // epd_check_status(screen);
  567. // epd_refresh(screen);
  568. uint8_t tmp = 0xa5;
  569. epd_write_cmd(screen, 0x07, false);
  570. epd_write_data(screen, &tmp, 1);
  571. // vTaskDelay(20 / portTICK_PERIOD_MS);
  572. #if 0
  573. epd_write_cmd(screen,0x02,false); // Power OFF
  574. vTaskDelay(20 / portTICK_PERIOD_MS);
  575. epd_check_power_off(screen);
  576. #endif
  577. #if 1
  578. // epd_write_cmd(screen,0x02,false); // Power OFF
  579. // epd_check_status(screen);
  580. #endif
  581. }
  582. IRAM_ATTR void epd_display(screen_t screen, const unsigned char *picData)
  583. {
  584. epd_write_cmd(screen, 0x13, false);
  585. epd_check_status(screen);
  586. epd_write_data(screen, picData, 38880);
  587. epd_check_status(screen);
  588. epd_write_cmd(screen, 0x04, false); // POWER ON
  589. epd_check_status(screen); // waiting for the electronic paper IC to release the idle signal
  590. // Refresh
  591. epd_refresh(screen);
  592. epd_write_cmd(screen, 0x02, false); // Power OFF
  593. epd_check_status(screen);
  594. epd_sleep(screen);
  595. }
  596. #if 1
  597. void epd_cache(screen_t screen, const unsigned char *picData)
  598. {
  599. epd_write_cmd(screen, 0x13, false);
  600. epd_write_data(screen, picData, 38880);
  601. }
  602. void epd_cache_quick_full_screen_refresh(screen_t screen, const unsigned char *old, const unsigned char *new)
  603. {
  604. if ((screen == SCREEN_LEFT) && (old != NULL))
  605. {
  606. printf("left quick\r\n");
  607. // Reset the display
  608. gpio_set_level(PIN_L_RST, 0);
  609. vTaskDelay(10 / portTICK_PERIOD_MS);
  610. gpio_set_level(PIN_L_RST, 1);
  611. vTaskDelay(10 / portTICK_PERIOD_MS);
  612. uint8_t cmd[1] = {0};
  613. epd_write_cmd(screen, 0x00, false);
  614. cmd[0] = 0x1F;
  615. epd_write_data(screen, cmd, 1);
  616. #if 0
  617. uint8_t cmd[1]={0};
  618. epd_write_cmd(screen,0x61,false);
  619. cmd[0] = 0xF0;
  620. epd_write_data(screen,cmd,1);
  621. cmd[0] = 0x01;
  622. epd_write_data(screen,cmd,1);
  623. cmd[0] = 0xA0;
  624. epd_write_data(screen,cmd,1);
  625. #endif
  626. #if 0
  627. int cmd=0;
  628. //Send all the commands
  629. while (init_cmds[cmd].databytes!=0xff) {
  630. epd_write_cmd(SCREEN_LEFT, init_cmds[cmd].cmd, false);
  631. epd_write_data(SCREEN_LEFT, init_cmds[cmd].data, init_cmds[cmd].databytes&0x1F);
  632. epd_write_cmd(SCREEN_RIGHT, init_cmds[cmd].cmd, false);
  633. epd_write_data(SCREEN_RIGHT, init_cmds[cmd].data, init_cmds[cmd].databytes&0x1F);
  634. if (init_cmds[cmd].databytes&0x80) {
  635. vTaskDelay(100 / portTICK_PERIOD_MS);
  636. }
  637. cmd++;
  638. }
  639. #endif
  640. }
  641. if ((screen == SCREEN_RIGHT) && (old != NULL))
  642. {
  643. printf("right quick\r\n");
  644. gpio_set_level(PIN_R_RST, 0);
  645. vTaskDelay(10 / portTICK_PERIOD_MS);
  646. gpio_set_level(PIN_R_RST, 1);
  647. vTaskDelay(10 / portTICK_PERIOD_MS);
  648. // uint8_t cmd[1]={0};
  649. // epd_write_cmd(screen,0x00,false);
  650. // cmd[0] = 0x1F;
  651. // epd_write_data(screen,cmd,1);
  652. #if 0
  653. uint8_t cmd[1]={0};
  654. epd_write_cmd(screen,0x61,false);
  655. cmd[0] = 0xF0;
  656. epd_write_data(screen,cmd,1);
  657. cmd[0] = 0x01;
  658. epd_write_data(screen,cmd,1);
  659. cmd[0] = 0xA0;
  660. epd_write_data(screen,cmd,1);
  661. #endif
  662. #if 0
  663. int cmd=0;
  664. //Send all the commands
  665. while (init_cmds[cmd].databytes!=0xff) {
  666. epd_write_cmd(SCREEN_LEFT, init_cmds[cmd].cmd, false);
  667. epd_write_data(SCREEN_LEFT, init_cmds[cmd].data, init_cmds[cmd].databytes&0x1F);
  668. epd_write_cmd(SCREEN_RIGHT, init_cmds[cmd].cmd, false);
  669. epd_write_data(SCREEN_RIGHT, init_cmds[cmd].data, init_cmds[cmd].databytes&0x1F);
  670. if (init_cmds[cmd].databytes&0x80) {
  671. vTaskDelay(100 / portTICK_PERIOD_MS);
  672. }
  673. cmd++;
  674. }
  675. #endif
  676. }
  677. if (old != NULL)
  678. {
  679. epd_write_cmd(screen, 0x10, false);
  680. epd_check_status(screen);
  681. epd_write_data(screen, old, 38880);
  682. }
  683. #if 0
  684. uint8_t cmd[1]={0};
  685. epd_write_cmd(screen,0x61,false);
  686. cmd[0] = 0xF0;
  687. epd_write_data(screen,cmd,1);
  688. cmd[0] = 0x01;
  689. epd_write_data(screen,cmd,1);
  690. cmd[0] = 0xA0;
  691. epd_write_data(screen,cmd,1);
  692. #endif
  693. epd_write_cmd(screen, 0x13, false);
  694. epd_write_data(screen, new, 38880);
  695. }
  696. #endif
  697. void epd_display_partal(uint16_t Xstart, uint16_t Ystart, uint16_t Xend, uint16_t Yend,
  698. screen_t screen, const unsigned char *picData)
  699. {
  700. unsigned int row, col;
  701. uint8_t Xstart_H, Xstart_L, Ystart_H, Ystart_L, Xend_H, Xend_L, Yend_H, Yend_L;
  702. Xstart_H = ((Xstart) >> 8) & 0xff;
  703. Xstart_L = (Xstart) & 0xff;
  704. Ystart_H = ((Ystart) >> 8) & 0xff;
  705. Ystart_L = (Ystart) & 0xff;
  706. Xend_H = ((Xend - 1) >> 8) & 0xff;
  707. Xend_L = (Xend - 1) & 0xff;
  708. Yend_H = ((Yend - 1) >> 8) & 0xff;
  709. Yend_L = (Yend - 1) & 0xff;
  710. epd_write_cmd(screen, 0x91, false); // partial in
  711. epd_check_status(screen);
  712. epd_write_cmd(screen, 0x90, false);
  713. epd_check_status(screen);
  714. epd_write_data(screen, &Xstart_H, 1);
  715. epd_write_data(screen, &Xstart_L, 1);
  716. epd_write_data(screen, &Xend_H, 1);
  717. epd_write_data(screen, &Xend_L, 1);
  718. epd_write_data(screen, &Ystart_H, 1);
  719. epd_write_data(screen, &Ystart_L, 1);
  720. epd_write_data(screen, &Yend_H, 1);
  721. epd_write_data(screen, &Yend_L, 1);
  722. uint8_t tmp = 0x01;
  723. epd_write_data(screen, &tmp, 1);
  724. epd_write_cmd(screen, 0x13, false);
  725. epd_check_status(screen);
  726. epd_write_data(screen, picData, ((Yend - Ystart) * (Xend - Xstart) / 8));
  727. epd_write_cmd(screen, 0x92, false);
  728. epd_write_cmd(screen, 0x04, false); // Power ON
  729. epd_check_status(screen);
  730. epd_refresh(screen);
  731. epd_write_cmd(screen, 0x02, false); // Power OFF
  732. epd_check_status(screen);
  733. }
  734. void epd_partial_cache(uint16_t Xstart, uint16_t Ystart, uint16_t Xend, uint16_t Yend,
  735. screen_t screen, const unsigned char *picData)
  736. {
  737. unsigned int row, col;
  738. uint8_t Xstart_H, Xstart_L, Ystart_H, Ystart_L, Xend_H, Xend_L, Yend_H, Yend_L;
  739. Xstart_H = ((Xstart) >> 8) & 0xff;
  740. Xstart_L = (Xstart) & 0xff;
  741. Ystart_H = ((Ystart) >> 8) & 0xff;
  742. Ystart_L = (Ystart) & 0xff;
  743. Xend_H = ((Xend - 1) >> 8) & 0xff;
  744. Xend_L = (Xend - 1) & 0xff;
  745. Yend_H = ((Yend - 1) >> 8) & 0xff;
  746. Yend_L = (Yend - 1) & 0xff;
  747. epd_write_cmd(screen, 0x91, false); // partial in
  748. epd_check_status(screen);
  749. epd_write_cmd(screen, 0x90, false);
  750. epd_check_status(screen);
  751. epd_write_data(screen, &Xstart_H, 1);
  752. epd_write_data(screen, &Xstart_L, 1);
  753. epd_write_data(screen, &Xend_H, 1);
  754. epd_write_data(screen, &Xend_L, 1);
  755. epd_write_data(screen, &Ystart_H, 1);
  756. epd_write_data(screen, &Ystart_L, 1);
  757. epd_write_data(screen, &Yend_H, 1);
  758. epd_write_data(screen, &Yend_L, 1);
  759. uint8_t tmp = 0x01;
  760. epd_write_data(screen, &tmp, 1);
  761. epd_write_cmd(screen, 0x13, false);
  762. epd_check_status(screen);
  763. epd_write_data(screen, picData, ((Yend - Ystart) * (Xend - Xstart) / 8));
  764. epd_write_cmd(screen, 0x92, false);
  765. }
  766. 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)
  767. {
  768. if ((screen == SCREEN_LEFT) && (old != NULL))
  769. {
  770. printf("left partial quick\r\n");
  771. // Reset the display
  772. gpio_set_level(PIN_L_RST, 0);
  773. vTaskDelay(10 / portTICK_PERIOD_MS);
  774. gpio_set_level(PIN_L_RST, 1);
  775. vTaskDelay(10 / portTICK_PERIOD_MS);
  776. uint8_t cmd[1] = {0};
  777. epd_write_cmd(screen, 0x00, false);
  778. cmd[0] = 0x1F;
  779. epd_write_data(screen, cmd, 1);
  780. }
  781. if ((screen == SCREEN_RIGHT) && (old != NULL))
  782. {
  783. printf("right partial quick\r\n");
  784. gpio_set_level(PIN_R_RST, 0);
  785. vTaskDelay(10 / portTICK_PERIOD_MS);
  786. gpio_set_level(PIN_R_RST, 1);
  787. vTaskDelay(10 / portTICK_PERIOD_MS);
  788. }
  789. #if 0
  790. if (old !=NULL)
  791. {
  792. epd_write_cmd(screen,0x10,false);
  793. epd_write_data(screen,old,38880);
  794. }
  795. epd_write_cmd(screen,0x13,false);
  796. epd_write_data(screen,new,38880);
  797. #endif
  798. unsigned int row, col;
  799. uint8_t Xstart_H, Xstart_L, Ystart_H, Ystart_L, Xend_H, Xend_L, Yend_H, Yend_L;
  800. Xstart_H = ((Xstart) >> 8) & 0xff;
  801. Xstart_L = (Xstart) & 0xff;
  802. Ystart_H = ((Ystart) >> 8) & 0xff;
  803. Ystart_L = (Ystart) & 0xff;
  804. Xend_H = ((Xend - 1) >> 8) & 0xff;
  805. Xend_L = (Xend - 1) & 0xff;
  806. Yend_H = ((Yend - 1) >> 8) & 0xff;
  807. Yend_L = (Yend - 1) & 0xff;
  808. epd_write_cmd(screen, 0x91, false); // partial in
  809. epd_check_status(screen);
  810. epd_write_cmd(screen, 0x90, false);
  811. epd_check_status(screen);
  812. epd_write_data(screen, &Xstart_H, 1);
  813. epd_write_data(screen, &Xstart_L, 1);
  814. epd_write_data(screen, &Xend_H, 1);
  815. epd_write_data(screen, &Xend_L, 1);
  816. epd_write_data(screen, &Ystart_H, 1);
  817. epd_write_data(screen, &Ystart_L, 1);
  818. epd_write_data(screen, &Yend_H, 1);
  819. epd_write_data(screen, &Yend_L, 1);
  820. uint8_t tmp = 0x01;
  821. epd_write_data(screen, &tmp, 1);
  822. // epd_write_cmd(screen,0x13,false);
  823. // epd_check_status(screen);
  824. printf("1\r\n");
  825. if (old != NULL)
  826. {
  827. printf("2\r\n");
  828. epd_write_cmd(screen, 0x10, false);
  829. epd_check_status(screen);
  830. epd_write_data(screen, old, ((Yend - Ystart) * (Xend - Xstart) / 8));
  831. epd_write_cmd(screen, 0x92, false);
  832. }
  833. printf("3\r\n");
  834. epd_write_cmd(screen, 0x13, false);
  835. epd_check_status(screen);
  836. epd_write_data(screen, new, ((Yend - Ystart) * (Xend - Xstart) / 8));
  837. epd_write_cmd(screen, 0x92, false);
  838. }
  839. static uint8_t get_data(uint8_t *color, uint8_t pix_size)
  840. {
  841. uint8_t data = 0;
  842. int i = 0, j = 0;
  843. for (i = 0; i < 8; i++)
  844. {
  845. data = data | (color[j] << (7 - i));
  846. j = j + pix_size;
  847. }
  848. return data;
  849. }
  850. void epd_partial_cache1(uint16_t Xstart, uint16_t Ystart, uint16_t Xend, uint16_t Yend,
  851. screen_t screen, const unsigned char *picData)
  852. {
  853. unsigned int row, col;
  854. uint8_t Xstart_H, Xstart_L, Ystart_H, Ystart_L, Xend_H, Xend_L, Yend_H, Yend_L;
  855. Xstart_H = ((Xstart) >> 8) & 0xff;
  856. Xstart_L = (Xstart) & 0xff;
  857. Ystart_H = ((Ystart) >> 8) & 0xff;
  858. Ystart_L = (Ystart) & 0xff;
  859. Xend_H = ((Xend - 1) >> 8) & 0xff;
  860. Xend_L = (Xend - 1) & 0xff;
  861. Yend_H = ((Yend - 1) >> 8) & 0xff;
  862. Yend_L = (Yend - 1) & 0xff;
  863. epd_write_cmd(screen, 0x91, false); // partial in
  864. epd_check_status(screen);
  865. epd_write_cmd(screen, 0x90, false);
  866. epd_check_status(screen);
  867. epd_write_data(screen, &Xstart_H, 1);
  868. epd_write_data(screen, &Xstart_L, 1);
  869. epd_write_data(screen, &Xend_H, 1);
  870. epd_write_data(screen, &Xend_L, 1);
  871. epd_write_data(screen, &Ystart_H, 1);
  872. epd_write_data(screen, &Ystart_L, 1);
  873. epd_write_data(screen, &Yend_H, 1);
  874. epd_write_data(screen, &Yend_L, 1);
  875. uint8_t tmp = 0x01;
  876. epd_write_data(screen, &tmp, 1);
  877. epd_write_cmd(screen, 0x13, false);
  878. epd_check_status(screen);
  879. #if 0
  880. epd_write_data(screen,picData,((Yend - Ystart)*(Xend - Xstart)));
  881. #else
  882. uint8_t data;
  883. // disp_drv.hor_res = 648;
  884. // disp_drv.ver_res = 480;
  885. for (int i = 480 - 1; i >= 0; i--)
  886. {
  887. for (int j = 0; j < 648;)
  888. {
  889. data = get_data(&picData[j + i * 648 * 1], 1);
  890. // printf("%02x",data);
  891. epd_write_data(screen, &data, 1);
  892. j = j + 8 * 1;
  893. }
  894. }
  895. #endif
  896. epd_write_cmd(screen, 0x92, false);
  897. }
  898. void epd_powerOn_refresh(screen_t screen)
  899. {
  900. epd_write_cmd(screen, 0x04, false); // Power ON
  901. epd_check_status(screen);
  902. epd_refresh(screen);
  903. epd_write_cmd(screen, 0x02, false); // Power OFF
  904. epd_check_status(screen);
  905. }
  906. // 定义最大重复计数
  907. #define MAX_REPEAT 255
  908. // 压缩函数
  909. unsigned int compressRLE(uint8_t *input, unsigned int size, uint8_t *output)
  910. {
  911. unsigned int i = 0;
  912. unsigned int outputSize = 0;
  913. while (i < size)
  914. {
  915. uint8_t current = input[i];
  916. unsigned int count = 1;
  917. i++;
  918. while (i < size && input[i] == current && count < MAX_REPEAT)
  919. {
  920. count++;
  921. i++;
  922. }
  923. // 输出像素值和计数值到output
  924. output[outputSize++] = current;
  925. output[outputSize++] = count;
  926. }
  927. return outputSize;
  928. }
  929. // 解压函数
  930. unsigned int decompressRLE(uint8_t *input, unsigned int size, uint8_t *output)
  931. {
  932. unsigned int i = 0;
  933. unsigned int outputSize = 0;
  934. while (i < size)
  935. {
  936. uint8_t current = input[i];
  937. i++;
  938. unsigned int count = input[i];
  939. i++;
  940. // 输出像素值count次到output
  941. for (unsigned int j = 0; j < count; j++)
  942. {
  943. output[outputSize++] = current;
  944. }
  945. }
  946. return outputSize;
  947. }
  948. void test()
  949. {
  950. // 定义图像数据(十六进制数组)
  951. uint8_t imageData[] = {0x12, 0x12, 0x12, 0x13, 0x14, 0x14, 0x14, 0x15, 0x15, 0x15};
  952. int imageSize = sizeof(imageData) / sizeof(imageData[0]);
  953. printf("原始图像数据:\n");
  954. for (int i = 0; i < imageSize; i++)
  955. {
  956. printf("0x%02X ", imageData[i]);
  957. }
  958. printf("\n");
  959. uint8_t compressedData[2 * imageSize]; // 假设压缩后的数据不会超过原数据的两倍大小
  960. uint8_t decompressedData[imageSize]; // 假设解压后的数据不会超过原数据大小
  961. printf("压缩后的数据:\n");
  962. int compressedSize = compressRLE(imageData, imageSize, compressedData);
  963. for (int i = 0; i < compressedSize; i++)
  964. {
  965. printf("0x%02X ", compressedData[i]);
  966. }
  967. printf("\n");
  968. printf("解压后的数据:\n");
  969. int decompressedSize = decompressRLE(compressedData, compressedSize, decompressedData);
  970. for (int i = 0; i < decompressedSize; i++)
  971. {
  972. printf("0x%02X ", decompressedData[i]);
  973. }
  974. printf("\n");
  975. printf("压缩后的数据大小:%d 字节\n", compressedSize);
  976. printf("解压后的数据大小:%d 字节\n", decompressedSize);
  977. }
  978. // uint8_t tmp[38880];
  979. IRAM_ATTR bool epd_cache_quick(screen_t screen, const unsigned char *old, const unsigned char *new)
  980. {
  981. bool ret = false;
  982. if ((screen == SCREEN_LEFT) && (old != NULL))
  983. {
  984. // printf("SCREEN_LEFT quick\r\n");
  985. // Reset the display
  986. gpio_set_level(PIN_L_RST, 0);
  987. vTaskDelay(10 / portTICK_PERIOD_MS);
  988. gpio_set_level(PIN_L_RST, 1);
  989. vTaskDelay(10 / portTICK_PERIOD_MS);
  990. ret = epd_check_status(screen);
  991. if (!ret)
  992. {
  993. printf("err:2busy !!!!!\n");
  994. goto err;
  995. }
  996. uint8_t cmd[1] = {0};
  997. epd_write_cmd(screen, 0x00, false);
  998. cmd[0] = 0x1F;
  999. epd_write_data(screen, cmd, 1);
  1000. }
  1001. if ((screen == SCREEN_RIGHT) && (old != NULL))
  1002. {
  1003. // printf("SCREEN_RIGHT quick\r\n");
  1004. gpio_set_level(PIN_R_RST, 0);
  1005. vTaskDelay(10 / portTICK_PERIOD_MS);
  1006. gpio_set_level(PIN_R_RST, 1);
  1007. vTaskDelay(10 / portTICK_PERIOD_MS);
  1008. ret = epd_check_status(screen);
  1009. if (!ret)
  1010. {
  1011. printf("err:3busy !!!!!\n");
  1012. goto err;
  1013. }
  1014. uint8_t cmd2[1] = {0};
  1015. epd_write_cmd(screen, 0x00, false);
  1016. cmd2[0] = 0x1F;
  1017. epd_write_data(screen, cmd2, 1);
  1018. }
  1019. #if 1
  1020. if (old != NULL)
  1021. {
  1022. epd_write_cmd(screen, 0x10, false);
  1023. ret = epd_check_status(screen);
  1024. if (!ret)
  1025. {
  1026. printf("err:busy !!!!!\n");
  1027. goto err;
  1028. }
  1029. epd_write_data(screen, old, 38880);
  1030. ret = epd_check_status(screen);
  1031. if (!ret)
  1032. {
  1033. printf("err:2busy !!!!!\n");
  1034. goto err;
  1035. }
  1036. }
  1037. #else
  1038. epd_write_cmd(screen, 0x10, false);
  1039. epd_check_status(screen);
  1040. memcpy(&tmp, new, 38880);
  1041. for (int i = 0; i < 38880; i++)
  1042. {
  1043. tmp[i] = ~tmp[i];
  1044. }
  1045. epd_write_data(screen, &tmp, 38880);
  1046. #endif
  1047. epd_write_cmd(screen, 0x13, false);
  1048. ret = epd_check_status(screen);
  1049. if (!ret)
  1050. {
  1051. printf("err:2busy !!!!!\n");
  1052. goto err;
  1053. }
  1054. epd_write_data(screen, new, 38880);
  1055. ret = epd_check_status(screen);
  1056. if (!ret)
  1057. {
  1058. printf("err:2busy !!!!!\n");
  1059. goto err;
  1060. }
  1061. #if 0
  1062. if(screen == SCREEN_LEFT)
  1063. {
  1064. left_refresh_complete = true;
  1065. }
  1066. if(screen == SCREEN_RIGHT)
  1067. {
  1068. right_refresh_complete = true;
  1069. }
  1070. #else
  1071. // vTaskDelay(0000 / portTICK_PERIOD_MS);
  1072. // refresh_timer_start(500);
  1073. // xTimerStart(slow_display_timer, 0); //开始定时器
  1074. #endif
  1075. return true;
  1076. err:
  1077. ESP_LOGE(LOG_TAG, "err:epd_check_status");
  1078. return false;
  1079. }
  1080. void epd_powerOn_refresh_sleep(screen_t screen)
  1081. {
  1082. epd_write_cmd(screen, 0x04, false); // Power ON
  1083. epd_check_status(screen);
  1084. epd_refresh(screen);
  1085. epd_sleep(screen);
  1086. }
  1087. void deepsleep_epd_powerOn_refresh_sleep(screen_t screen)
  1088. {
  1089. epd_write_cmd(screen, 0x04, false); // Power ON
  1090. // epd_check_status(screen);
  1091. deepsleep_epd_check_status(screen);
  1092. epd_write_cmd(screen, 0x12, false); // DISPLAY REFRESH
  1093. deepsleep_epd_check_status(screen);
  1094. // epd_refresh(screen);
  1095. // epd_sleep(screen);
  1096. }
  1097. #else /*HAREWARE_SPI*/
  1098. static void task_delay_ms(uint32_t ms_count)
  1099. {
  1100. vTaskDelay(ms_count / portTICK_PERIOD_MS);
  1101. }
  1102. void epd_pin_init(struct EPD_INFO_SET *epd_pin_set)
  1103. {
  1104. esp_log_level_set("gpio", ESP_LOG_NONE); // close gpio logging
  1105. ESP_LOGI(LOG_TAG, "epd_pin_init");
  1106. #define EPD_OUT_PIN_SEL ((1ULL << epd_pin_set->sclk_pin) | \
  1107. (1ULL << epd_pin_set->sda_pin) | \
  1108. (1ULL << epd_pin_set->res_pin) | \
  1109. (1ULL << epd_pin_set->dc_pin) | \
  1110. (1ULL << epd_pin_set->cs_pin))
  1111. #define EPD_IN_PIN_SEL (1ULL << epd_pin_set->busy_pin)
  1112. gpio_config_t epd_pin_cfg = {};
  1113. epd_pin_cfg.intr_type = GPIO_INTR_DISABLE;
  1114. epd_pin_cfg.mode = GPIO_MODE_OUTPUT;
  1115. epd_pin_cfg.pin_bit_mask = EPD_OUT_PIN_SEL;
  1116. epd_pin_cfg.pull_down_en = 0;
  1117. epd_pin_cfg.pull_up_en = 0;
  1118. gpio_config(&epd_pin_cfg);
  1119. epd_pin_cfg.intr_type = GPIO_INTR_DISABLE;
  1120. epd_pin_cfg.mode = GPIO_MODE_INPUT;
  1121. epd_pin_cfg.pin_bit_mask = EPD_IN_PIN_SEL;
  1122. epd_pin_cfg.pull_down_en = 0;
  1123. epd_pin_cfg.pull_up_en = 1;
  1124. gpio_config(&epd_pin_cfg);
  1125. esp_log_level_set("gpio", LOG_LOCAL_LEVEL);
  1126. }
  1127. void epd_spi_write(struct EPD_INFO_SET *epd_pin_set, unsigned char value)
  1128. {
  1129. int i = 0, data = value;
  1130. epd_set_level(epd_pin_set->sclk_pin, LOW_LEVEL);
  1131. for (i = 0; i < 8; i++)
  1132. {
  1133. if (data & 0x0080)
  1134. {
  1135. epd_set_level(epd_pin_set->sda_pin, HIGH_LEVEL);
  1136. }
  1137. else
  1138. {
  1139. epd_set_level(epd_pin_set->sda_pin, LOW_LEVEL);
  1140. }
  1141. // ets_delay_us(1);
  1142. epd_set_level(epd_pin_set->sclk_pin, HIGH_LEVEL);
  1143. // ets_delay_us(1);
  1144. epd_set_level(epd_pin_set->sclk_pin, LOW_LEVEL);
  1145. data = ((data) << 1 & 0xff);
  1146. }
  1147. }
  1148. void epd_write_cmd(struct EPD_INFO_SET *epd_pin_set, unsigned char command)
  1149. {
  1150. epd_set_level(epd_pin_set->cs_pin, LOW_LEVEL);
  1151. epd_set_level(epd_pin_set->dc_pin, LOW_LEVEL);
  1152. epd_spi_write(epd_pin_set, command);
  1153. epd_set_level(epd_pin_set->cs_pin, HIGH_LEVEL);
  1154. }
  1155. void epd_write_data(struct EPD_INFO_SET *epd_pin_set, unsigned char command)
  1156. {
  1157. epd_set_level(epd_pin_set->cs_pin, LOW_LEVEL);
  1158. epd_set_level(epd_pin_set->dc_pin, HIGH_LEVEL);
  1159. epd_spi_write(epd_pin_set, command);
  1160. epd_set_level(epd_pin_set->cs_pin, HIGH_LEVEL);
  1161. // ets_delay_us(2);
  1162. }
  1163. void epd_init(struct EPD_INFO_SET *epd_pin_set)
  1164. {
  1165. epd_set_level(epd_pin_set->res_pin, LOW_LEVEL);
  1166. task_delay_ms(10);
  1167. epd_set_level(epd_pin_set->res_pin, HIGH_LEVEL);
  1168. task_delay_ms(10);
  1169. }
  1170. void epd_check_status(struct EPD_INFO_SET *epd_pin_set)
  1171. {
  1172. while (!epd_get_level(epd_pin_set->busy_pin))
  1173. ;
  1174. }
  1175. void epd_refresh(struct EPD_INFO_SET *epd_pin_set)
  1176. {
  1177. epd_write_cmd(epd_pin_set, 0x12); // DISPLAY REFRESH
  1178. // ets_delay_us(200); //!!!The delay here is necessary, 200uS at least!!!
  1179. task_delay_ms(1);
  1180. epd_check_status(epd_pin_set); // waiting for the electronic paper
  1181. }
  1182. void epd_sleep(struct EPD_INFO_SET *epd_pin_set)
  1183. {
  1184. epd_write_cmd(epd_pin_set, 0x07);
  1185. epd_write_data(epd_pin_set, 0xa5);
  1186. }
  1187. void epd_screen_init(struct EPD_INFO_SET *epd_pin_set)
  1188. {
  1189. epd_pin_init(epd_pin_set);
  1190. epd_init(epd_pin_set);
  1191. epd_write_cmd(epd_pin_set, 0x00);
  1192. epd_write_data(epd_pin_set, 0x1f);
  1193. epd_write_data(epd_pin_set, 0x09);
  1194. epd_write_cmd(epd_pin_set, 0x50);
  1195. epd_write_data(epd_pin_set, 0x18);
  1196. epd_write_data(epd_pin_set, 0x07);
  1197. epd_write_cmd(epd_pin_set, 0xe0);
  1198. epd_write_data(epd_pin_set, 0x02);
  1199. epd_write_cmd(epd_pin_set, 0xe5);
  1200. epd_write_data(epd_pin_set, 0x5c);
  1201. }
  1202. void epd_clear_black(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, 0xff);
  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. epd_refresh(epd_pin_set);
  1213. epd_write_cmd(epd_pin_set, 0x02); // Power OFF
  1214. epd_check_status(epd_pin_set);
  1215. }
  1216. void epd_clear_write(struct EPD_INFO_SET *epd_pin_set)
  1217. {
  1218. unsigned int i;
  1219. epd_write_cmd(epd_pin_set, 0x13);
  1220. for (i = 0; i < 38880; i++)
  1221. {
  1222. epd_write_data(epd_pin_set, 0x00);
  1223. }
  1224. epd_write_cmd(epd_pin_set, 0x04); // POWER ON
  1225. epd_check_status(epd_pin_set); // waiting for the electronic paper IC to release the idle signal
  1226. // Refresh
  1227. epd_refresh(epd_pin_set);
  1228. epd_write_cmd(epd_pin_set, 0x02); // Power OFF
  1229. epd_check_status(epd_pin_set);
  1230. }
  1231. // PAINT left_screen_paint;
  1232. // PAINT right_screen_paint;
  1233. void epd_display(struct EPD_INFO_SET *epd_pin_set, const unsigned char *picData)
  1234. {
  1235. unsigned int i;
  1236. epd_write_cmd(epd_pin_set, 0x13);
  1237. for (i = 0; i < 38880; i++)
  1238. {
  1239. epd_write_data(epd_pin_set, picData[i]);
  1240. }
  1241. epd_write_cmd(epd_pin_set, 0x04); // POWER ON
  1242. epd_check_status(epd_pin_set); // waiting for the electronic paper IC to release the idle signal
  1243. // Refresh
  1244. epd_refresh(epd_pin_set);
  1245. epd_write_cmd(epd_pin_set, 0x02); // Power OFF
  1246. epd_check_status(epd_pin_set);
  1247. }
  1248. void epd_cache(struct EPD_INFO_SET *epd_pin_set, const unsigned char *picData)
  1249. {
  1250. unsigned int i;
  1251. epd_write_cmd(epd_pin_set, 0x13);
  1252. for (i = 0; i < 38880; i++)
  1253. {
  1254. epd_write_data(epd_pin_set, picData[i]);
  1255. }
  1256. }
  1257. void epd_display_partal(uint16_t Xstart, uint16_t Ystart, uint16_t Xend, uint16_t Yend,
  1258. struct EPD_INFO_SET *epd_pin_set, const unsigned char *picData)
  1259. {
  1260. unsigned int row, col;
  1261. uint8_t Xstart_H, Xstart_L, Ystart_H, Ystart_L, Xend_H, Xend_L, Yend_H, Yend_L;
  1262. Xstart_H = ((Xstart) >> 8) & 0xff;
  1263. Xstart_L = (Xstart) & 0xff;
  1264. Ystart_H = ((Ystart) >> 8) & 0xff;
  1265. Ystart_L = (Ystart) & 0xff;
  1266. Xend_H = ((Xend - 1) >> 8) & 0xff;
  1267. Xend_L = (Xend - 1) & 0xff;
  1268. Yend_H = ((Yend - 1) >> 8) & 0xff;
  1269. Yend_L = (Yend - 1) & 0xff;
  1270. epd_write_cmd(epd_pin_set, 0x91); // partial in
  1271. epd_check_status(epd_pin_set);
  1272. epd_write_cmd(epd_pin_set, 0x90);
  1273. epd_check_status(epd_pin_set);
  1274. epd_write_data(epd_pin_set, Xstart_H);
  1275. epd_write_data(epd_pin_set, Xstart_L);
  1276. epd_write_data(epd_pin_set, Xend_H);
  1277. epd_write_data(epd_pin_set, Xend_L);
  1278. epd_write_data(epd_pin_set, Ystart_H);
  1279. epd_write_data(epd_pin_set, Ystart_L);
  1280. epd_write_data(epd_pin_set, Yend_H);
  1281. epd_write_data(epd_pin_set, Yend_L);
  1282. epd_write_data(epd_pin_set, 0x01);
  1283. epd_write_cmd(epd_pin_set, 0x13);
  1284. epd_check_status(epd_pin_set);
  1285. int i = 0;
  1286. for (col = 0; col < (Yend - Ystart); col++)
  1287. {
  1288. for (row = 0; row < ((Xend - Xstart) / 8); row++)
  1289. {
  1290. epd_write_data(epd_pin_set, picData[i++]);
  1291. }
  1292. }
  1293. epd_write_cmd(epd_pin_set, 0x92);
  1294. epd_write_cmd(epd_pin_set, 0x04); // Power ON
  1295. epd_check_status(epd_pin_set);
  1296. epd_refresh(epd_pin_set);
  1297. epd_write_cmd(epd_pin_set, 0x02); // Power OFF
  1298. epd_check_status(epd_pin_set);
  1299. }
  1300. void epd_partial_cache(uint16_t Xstart, uint16_t Ystart, uint16_t Xend, uint16_t Yend,
  1301. struct EPD_INFO_SET *epd_pin_set, const unsigned char *picData)
  1302. {
  1303. unsigned int row, col;
  1304. uint8_t Xstart_H, Xstart_L, Ystart_H, Ystart_L, Xend_H, Xend_L, Yend_H, Yend_L;
  1305. Xstart_H = ((Xstart) >> 8) & 0xff;
  1306. Xstart_L = (Xstart) & 0xff;
  1307. Ystart_H = ((Ystart) >> 8) & 0xff;
  1308. Ystart_L = (Ystart) & 0xff;
  1309. Xend_H = ((Xend - 1) >> 8) & 0xff;
  1310. Xend_L = (Xend - 1) & 0xff;
  1311. Yend_H = ((Yend - 1) >> 8) & 0xff;
  1312. Yend_L = (Yend - 1) & 0xff;
  1313. epd_write_cmd(epd_pin_set, 0x91); // partial in
  1314. epd_check_status(epd_pin_set);
  1315. epd_write_cmd(epd_pin_set, 0x90);
  1316. epd_check_status(epd_pin_set);
  1317. epd_write_data(epd_pin_set, Xstart_H);
  1318. epd_write_data(epd_pin_set, Xstart_L);
  1319. epd_write_data(epd_pin_set, Xend_H);
  1320. epd_write_data(epd_pin_set, Xend_L);
  1321. epd_write_data(epd_pin_set, Ystart_H);
  1322. epd_write_data(epd_pin_set, Ystart_L);
  1323. epd_write_data(epd_pin_set, Yend_H);
  1324. epd_write_data(epd_pin_set, Yend_L);
  1325. epd_write_data(epd_pin_set, 0x01);
  1326. epd_write_cmd(epd_pin_set, 0x13);
  1327. epd_check_status(epd_pin_set);
  1328. int i = 0;
  1329. for (col = 0; col < (Yend - Ystart); col++)
  1330. {
  1331. for (row = 0; row < ((Xend - Xstart) / 8); row++)
  1332. {
  1333. epd_write_data(epd_pin_set, picData[i++]);
  1334. }
  1335. }
  1336. epd_write_cmd(epd_pin_set, 0x92);
  1337. }
  1338. void epd_powerOn_refresh(struct EPD_INFO_SET *epd_pin_set)
  1339. {
  1340. epd_write_cmd(epd_pin_set, 0x04); // Power ON
  1341. epd_check_status(epd_pin_set);
  1342. epd_refresh(epd_pin_set);
  1343. epd_write_cmd(epd_pin_set, 0x02); // Power OFF
  1344. epd_check_status(epd_pin_set);
  1345. }
  1346. #endif /*HAREWARE_SPI*/