EPD.c 42 KB

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