EPD.c 48 KB

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