drv_lcd.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2018-08-14 flybreak the first version
  9. * 2018-09-18 balanceTWK add sleep mode function
  10. * 2018-09-27 ZYLX optimized display speed
  11. */
  12. #include <rtdevice.h>
  13. #include "drv_spi.h"
  14. #include "drv_lcd.h"
  15. #include "drv_lcd_font.h"
  16. #include "drv_gpio.h"
  17. #define DBG_SECTION_NAME "LCD"
  18. #define DBG_COLOR
  19. #define DBG_LEVEL DBG_LOG
  20. #include <rtdbg.h>
  21. #define LCD_PWR_PIN GET_PIN(B, 7)
  22. #define LCD_DC_PIN GET_PIN(B, 4)
  23. #define LCD_RES_PIN GET_PIN(B, 6)
  24. #define LCD_CLEAR_SEND_NUMBER 5760
  25. rt_uint16_t BACK_COLOR = WHITE, FORE_COLOR = BLACK;
  26. static struct rt_spi_device *spi_dev_lcd;
  27. static int rt_hw_lcd_config(void)
  28. {
  29. spi_dev_lcd = (struct rt_spi_device *)rt_device_find("spi30");
  30. /* config spi */
  31. {
  32. struct rt_spi_configuration cfg;
  33. cfg.data_width = 8;
  34. cfg.mode = RT_SPI_MASTER | RT_SPI_MODE_0 | RT_SPI_MSB;
  35. cfg.max_hz = 42 * 1000 * 1000; /* 42M,SPI max 42MHz,lcd 4-wire spi */
  36. rt_spi_configure(spi_dev_lcd, &cfg);
  37. }
  38. return RT_EOK;
  39. }
  40. static rt_err_t lcd_write_cmd(const rt_uint8_t cmd)
  41. {
  42. rt_size_t len;
  43. rt_pin_write(LCD_DC_PIN, PIN_LOW);
  44. len = rt_spi_send(spi_dev_lcd, &cmd, 1);
  45. if (len != 1)
  46. {
  47. LOG_I("lcd_write_cmd error. %d", len);
  48. return -RT_ERROR;
  49. }
  50. else
  51. {
  52. return RT_EOK;
  53. }
  54. }
  55. static rt_err_t lcd_write_data(const rt_uint8_t data)
  56. {
  57. rt_size_t len;
  58. rt_pin_write(LCD_DC_PIN, PIN_HIGH);
  59. len = rt_spi_send(spi_dev_lcd, &data, 1);
  60. if (len != 1)
  61. {
  62. LOG_I("lcd_write_data error. %d", len);
  63. return -RT_ERROR;
  64. }
  65. else
  66. {
  67. return RT_EOK;
  68. }
  69. }
  70. static rt_err_t lcd_write_half_word(const rt_uint16_t da)
  71. {
  72. rt_size_t len;
  73. char data[2] = {0};
  74. data[0] = da >> 8;
  75. data[1] = da;
  76. rt_pin_write(LCD_DC_PIN, PIN_HIGH);
  77. len = rt_spi_send(spi_dev_lcd, data, 2);
  78. if (len != 2)
  79. {
  80. LOG_I("lcd_write_half_word error. %d", len);
  81. return -RT_ERROR;
  82. }
  83. else
  84. {
  85. return RT_EOK;
  86. }
  87. }
  88. static void lcd_gpio_init(void)
  89. {
  90. rt_hw_lcd_config();
  91. rt_pin_mode(LCD_DC_PIN, PIN_MODE_OUTPUT);
  92. rt_pin_mode(LCD_RES_PIN, PIN_MODE_OUTPUT);
  93. rt_pin_mode(LCD_PWR_PIN, PIN_MODE_OUTPUT);
  94. rt_pin_write(LCD_PWR_PIN, PIN_LOW);
  95. rt_pin_write(LCD_RES_PIN, PIN_LOW);
  96. //wait at least 100ms for reset
  97. rt_thread_delay(RT_TICK_PER_SECOND / 10);
  98. rt_pin_write(LCD_RES_PIN, PIN_HIGH);
  99. }
  100. static int rt_hw_lcd_init(void)
  101. {
  102. __HAL_RCC_GPIOD_CLK_ENABLE();
  103. rt_hw_spi_device_attach("spi3", "spi30", GPIOD, GPIO_PIN_7);
  104. lcd_gpio_init();
  105. /* Memory Data Access Control */
  106. lcd_write_cmd(0x36);
  107. lcd_write_data(0x00);
  108. /* RGB 5-6-5-bit */
  109. lcd_write_cmd(0x3A);
  110. lcd_write_data(0x65);
  111. /* Porch Setting */
  112. lcd_write_cmd(0xB2);
  113. lcd_write_data(0x0C);
  114. lcd_write_data(0x0C);
  115. lcd_write_data(0x00);
  116. lcd_write_data(0x33);
  117. lcd_write_data(0x33);
  118. /* Gate Control */
  119. lcd_write_cmd(0xB7);
  120. lcd_write_data(0x35);
  121. /* VCOM Setting */
  122. lcd_write_cmd(0xBB);
  123. lcd_write_data(0x19);
  124. /* LCM Control */
  125. lcd_write_cmd(0xC0);
  126. lcd_write_data(0x2C);
  127. /* VDV and VRH Command Enable */
  128. lcd_write_cmd(0xC2);
  129. lcd_write_data(0x01);
  130. /* VRH Set */
  131. lcd_write_cmd(0xC3);
  132. lcd_write_data(0x12);
  133. /* VDV Set */
  134. lcd_write_cmd(0xC4);
  135. lcd_write_data(0x20);
  136. /* Frame Rate Control in Normal Mode */
  137. lcd_write_cmd(0xC6);
  138. lcd_write_data(0x0F);
  139. /* Power Control 1 */
  140. lcd_write_cmd(0xD0);
  141. lcd_write_data(0xA4);
  142. lcd_write_data(0xA1);
  143. /* Positive Voltage Gamma Control */
  144. lcd_write_cmd(0xE0);
  145. lcd_write_data(0xD0);
  146. lcd_write_data(0x04);
  147. lcd_write_data(0x0D);
  148. lcd_write_data(0x11);
  149. lcd_write_data(0x13);
  150. lcd_write_data(0x2B);
  151. lcd_write_data(0x3F);
  152. lcd_write_data(0x54);
  153. lcd_write_data(0x4C);
  154. lcd_write_data(0x18);
  155. lcd_write_data(0x0D);
  156. lcd_write_data(0x0B);
  157. lcd_write_data(0x1F);
  158. lcd_write_data(0x23);
  159. /* Negative Voltage Gamma Control */
  160. lcd_write_cmd(0xE1);
  161. lcd_write_data(0xD0);
  162. lcd_write_data(0x04);
  163. lcd_write_data(0x0C);
  164. lcd_write_data(0x11);
  165. lcd_write_data(0x13);
  166. lcd_write_data(0x2C);
  167. lcd_write_data(0x3F);
  168. lcd_write_data(0x44);
  169. lcd_write_data(0x51);
  170. lcd_write_data(0x2F);
  171. lcd_write_data(0x1F);
  172. lcd_write_data(0x1F);
  173. lcd_write_data(0x20);
  174. lcd_write_data(0x23);
  175. /* Display Inversion On */
  176. lcd_write_cmd(0x21);
  177. /* Sleep Out */
  178. lcd_write_cmd(0x11);
  179. /* wait for power stability */
  180. rt_thread_mdelay(100);
  181. lcd_clear(WHITE);
  182. /* display on */
  183. rt_pin_write(LCD_PWR_PIN, PIN_HIGH);
  184. lcd_write_cmd(0x29);
  185. return RT_EOK;
  186. }
  187. INIT_DEVICE_EXPORT(rt_hw_lcd_init);
  188. /**
  189. * Set background color and foreground color
  190. *
  191. * @param back background color
  192. * @param fore fore color
  193. *
  194. * @return void
  195. */
  196. void lcd_set_color(rt_uint16_t back, rt_uint16_t fore)
  197. {
  198. BACK_COLOR = back;
  199. FORE_COLOR = fore;
  200. }
  201. void lcd_display_on(void)
  202. {
  203. rt_pin_write(LCD_PWR_PIN, PIN_HIGH);
  204. }
  205. void lcd_display_off(void)
  206. {
  207. rt_pin_write(LCD_PWR_PIN, PIN_LOW);
  208. }
  209. /* lcd enter the minimum power consumption mode and backlight off. */
  210. void lcd_enter_sleep(void)
  211. {
  212. rt_pin_write(LCD_PWR_PIN, PIN_LOW);
  213. rt_thread_mdelay(5);
  214. lcd_write_cmd(0x10);
  215. }
  216. /* lcd turn off sleep mode and backlight on. */
  217. void lcd_exit_sleep(void)
  218. {
  219. rt_pin_write(LCD_PWR_PIN, PIN_HIGH);
  220. rt_thread_mdelay(5);
  221. lcd_write_cmd(0x11);
  222. rt_thread_mdelay(120);
  223. }
  224. /**
  225. * Set drawing area
  226. *
  227. * @param x1 start of x position
  228. * @param y1 start of y position
  229. * @param x2 end of x position
  230. * @param y2 end of y position
  231. *
  232. * @return void
  233. */
  234. void lcd_address_set(rt_uint16_t x1, rt_uint16_t y1, rt_uint16_t x2, rt_uint16_t y2)
  235. {
  236. lcd_write_cmd(0x2a);
  237. lcd_write_data(x1 >> 8);
  238. lcd_write_data(x1);
  239. lcd_write_data(x2 >> 8);
  240. lcd_write_data(x2);
  241. lcd_write_cmd(0x2b);
  242. lcd_write_data(y1 >> 8);
  243. lcd_write_data(y1);
  244. lcd_write_data(y2 >> 8);
  245. lcd_write_data(y2);
  246. lcd_write_cmd(0x2C);
  247. }
  248. /**
  249. * clear the lcd.
  250. *
  251. * @param color Fill color
  252. *
  253. * @return void
  254. */
  255. void lcd_clear(rt_uint16_t color)
  256. {
  257. rt_uint16_t i, j;
  258. rt_uint8_t data[2] = {0};
  259. rt_uint8_t *buf = RT_NULL;
  260. data[0] = color >> 8;
  261. data[1] = color;
  262. lcd_address_set(0, 0, LCD_W - 1, LCD_H - 1);
  263. /* 5760 = 240*240/20 */
  264. buf = rt_malloc(LCD_CLEAR_SEND_NUMBER);
  265. if (buf)
  266. {
  267. /* 2880 = 5760/2 color is 16 bit */
  268. for (j = 0; j < LCD_CLEAR_SEND_NUMBER / 2; j++)
  269. {
  270. buf[j * 2] = data[0];
  271. buf[j * 2 + 1] = data[1];
  272. }
  273. rt_pin_write(LCD_DC_PIN, PIN_HIGH);
  274. for (i = 0; i < 20; i++)
  275. {
  276. rt_spi_send(spi_dev_lcd, buf, LCD_CLEAR_SEND_NUMBER);
  277. }
  278. rt_free(buf);
  279. }
  280. else
  281. {
  282. rt_pin_write(LCD_DC_PIN, PIN_HIGH);
  283. for (i = 0; i < LCD_W; i++)
  284. {
  285. for (j = 0; j < LCD_H; j++)
  286. {
  287. rt_spi_send(spi_dev_lcd, data, 2);
  288. }
  289. }
  290. }
  291. }
  292. /**
  293. * display a point on the lcd.
  294. *
  295. * @param x x position
  296. * @param y y position
  297. *
  298. * @return void
  299. */
  300. void lcd_draw_point(rt_uint16_t x, rt_uint16_t y)
  301. {
  302. lcd_address_set(x, y, x, y);
  303. lcd_write_half_word(FORE_COLOR);
  304. }
  305. /**
  306. * display a point on the lcd using the given colour.
  307. *
  308. * @param x x position
  309. * @param y y position
  310. * @param color color of point
  311. *
  312. * @return void
  313. */
  314. void lcd_draw_point_color(rt_uint16_t x, rt_uint16_t y, rt_uint16_t color)
  315. {
  316. lcd_address_set(x, y, x, y);
  317. lcd_write_half_word(color);
  318. }
  319. /**
  320. * full color on the lcd.
  321. *
  322. * @param x_start start of x position
  323. * @param y_start start of y position
  324. * @param x_end end of x position
  325. * @param y_end end of y position
  326. * @param color Fill color
  327. *
  328. * @return void
  329. */
  330. void lcd_fill(rt_uint16_t x_start, rt_uint16_t y_start, rt_uint16_t x_end, rt_uint16_t y_end, rt_uint16_t color)
  331. {
  332. rt_uint16_t i = 0, j = 0;
  333. rt_uint32_t size = 0, size_remain = 0;
  334. rt_uint8_t *fill_buf = RT_NULL;
  335. size = (x_end - x_start) * (y_end - y_start) * 2;
  336. if (size > LCD_CLEAR_SEND_NUMBER)
  337. {
  338. /* the number of remaining to be filled */
  339. size_remain = size - LCD_CLEAR_SEND_NUMBER;
  340. size = LCD_CLEAR_SEND_NUMBER;
  341. }
  342. lcd_address_set(x_start, y_start, x_end, y_end);
  343. fill_buf = (rt_uint8_t *)rt_malloc(size);
  344. if (fill_buf)
  345. {
  346. /* fast fill */
  347. while (1)
  348. {
  349. for (i = 0; i < size / 2; i++)
  350. {
  351. fill_buf[2 * i] = color >> 8;
  352. fill_buf[2 * i + 1] = color;
  353. }
  354. rt_pin_write(LCD_DC_PIN, PIN_HIGH);
  355. rt_spi_send(spi_dev_lcd, fill_buf, size);
  356. /* Fill completed */
  357. if (size_remain == 0)
  358. break;
  359. /* calculate the number of fill next time */
  360. if (size_remain > LCD_CLEAR_SEND_NUMBER)
  361. {
  362. size_remain = size_remain - LCD_CLEAR_SEND_NUMBER;
  363. }
  364. else
  365. {
  366. size = size_remain;
  367. size_remain = 0;
  368. }
  369. }
  370. rt_free(fill_buf);
  371. }
  372. else
  373. {
  374. for (i = y_start; i <= y_end; i++)
  375. {
  376. for (j = x_start; j <= x_end; j++)lcd_write_half_word(color);
  377. }
  378. }
  379. }
  380. /**
  381. * display a line on the lcd.
  382. *
  383. * @param x1 x1 position
  384. * @param y1 y1 position
  385. * @param x2 x2 position
  386. * @param y2 y2 position
  387. *
  388. * @return void
  389. */
  390. void lcd_draw_line(rt_uint16_t x1, rt_uint16_t y1, rt_uint16_t x2, rt_uint16_t y2)
  391. {
  392. rt_uint16_t t;
  393. rt_uint32_t i = 0;
  394. int xerr = 0, yerr = 0, delta_x, delta_y, distance;
  395. int incx, incy, row, col;
  396. if (y1 == y2)
  397. {
  398. /* fast draw transverse line */
  399. rt_uint32_t x_offset = 0;
  400. if (x1 < x2)
  401. {
  402. x_offset = x2 - x1;
  403. lcd_address_set(x1, y1, x2, y2);
  404. }
  405. else if (x1 > x2)
  406. {
  407. x_offset = x1 - x2;
  408. lcd_address_set(x2, y2, x1, y1);
  409. }
  410. else
  411. {
  412. lcd_draw_point(x1, y1);
  413. return;
  414. }
  415. rt_uint8_t line_buf[480] = {0};
  416. for (i = 0; i < x_offset; i++)
  417. {
  418. line_buf[2 * i] = FORE_COLOR >> 8;
  419. line_buf[2 * i + 1] = FORE_COLOR;
  420. }
  421. rt_pin_write(LCD_DC_PIN, PIN_HIGH);
  422. rt_spi_send(spi_dev_lcd, line_buf, x_offset * 2);
  423. return ;
  424. }
  425. delta_x = x2 - x1;
  426. delta_y = y2 - y1;
  427. row = x1;
  428. col = y1;
  429. if (delta_x > 0)incx = 1;
  430. else if (delta_x == 0)incx = 0;
  431. else
  432. {
  433. incx = -1;
  434. delta_x = -delta_x;
  435. }
  436. if (delta_y > 0)incy = 1;
  437. else if (delta_y == 0)incy = 0;
  438. else
  439. {
  440. incy = -1;
  441. delta_y = -delta_y;
  442. }
  443. if (delta_x > delta_y)distance = delta_x;
  444. else distance = delta_y;
  445. for (t = 0; t <= distance + 1; t++)
  446. {
  447. lcd_draw_point(row, col);
  448. xerr += delta_x ;
  449. yerr += delta_y ;
  450. if (xerr > distance)
  451. {
  452. xerr -= distance;
  453. row += incx;
  454. }
  455. if (yerr > distance)
  456. {
  457. yerr -= distance;
  458. col += incy;
  459. }
  460. }
  461. }
  462. /**
  463. * display a rectangle on the lcd.
  464. *
  465. * @param x1 x1 position
  466. * @param y1 y1 position
  467. * @param x2 x2 position
  468. * @param y2 y2 position
  469. *
  470. * @return void
  471. */
  472. void lcd_draw_rectangle(rt_uint16_t x1, rt_uint16_t y1, rt_uint16_t x2, rt_uint16_t y2)
  473. {
  474. lcd_draw_line(x1, y1, x2, y1);
  475. lcd_draw_line(x1, y1, x1, y2);
  476. lcd_draw_line(x1, y2, x2, y2);
  477. lcd_draw_line(x2, y1, x2, y2);
  478. }
  479. /**
  480. * display a circle on the lcd.
  481. *
  482. * @param x x position of Center
  483. * @param y y position of Center
  484. * @param r radius
  485. *
  486. * @return void
  487. */
  488. void lcd_draw_circle(rt_uint16_t x0, rt_uint16_t y0, rt_uint8_t r)
  489. {
  490. int a, b;
  491. int di;
  492. a = 0;
  493. b = r;
  494. di = 3 - (r << 1);
  495. while (a <= b)
  496. {
  497. lcd_draw_point(x0 - b, y0 - a);
  498. lcd_draw_point(x0 + b, y0 - a);
  499. lcd_draw_point(x0 - a, y0 + b);
  500. lcd_draw_point(x0 - b, y0 - a);
  501. lcd_draw_point(x0 - a, y0 - b);
  502. lcd_draw_point(x0 + b, y0 + a);
  503. lcd_draw_point(x0 + a, y0 - b);
  504. lcd_draw_point(x0 + a, y0 + b);
  505. lcd_draw_point(x0 - b, y0 + a);
  506. a++;
  507. //Bresenham
  508. if (di < 0)di += 4 * a + 6;
  509. else
  510. {
  511. di += 10 + 4 * (a - b);
  512. b--;
  513. }
  514. lcd_draw_point(x0 + a, y0 + b);
  515. }
  516. }
  517. static void lcd_show_char(rt_uint16_t x, rt_uint16_t y, rt_uint8_t data, rt_uint32_t size)
  518. {
  519. rt_uint8_t temp;
  520. rt_uint8_t num = 0;;
  521. rt_uint8_t pos, t;
  522. rt_uint16_t colortemp = FORE_COLOR;
  523. rt_uint8_t *font_buf = RT_NULL;
  524. if (x > LCD_W - size / 2 || y > LCD_H - size)return;
  525. data = data - ' ';
  526. #ifdef ASC2_1608
  527. if (size == 16)
  528. {
  529. lcd_address_set(x, y, x + size / 2 - 1, y + size - 1);//(x,y,x+8-1,y+16-1)
  530. font_buf = (rt_uint8_t *)rt_malloc(size * size);
  531. if (!font_buf)
  532. {
  533. /* fast show char */
  534. for (pos = 0; pos < size * (size / 2) / 8; pos++)
  535. {
  536. temp = asc2_1608[(rt_uint16_t)data * size * (size / 2) / 8 + pos];
  537. for (t = 0; t < 8; t++)
  538. {
  539. if (temp & 0x80)colortemp = FORE_COLOR;
  540. else colortemp = BACK_COLOR;
  541. lcd_write_half_word(colortemp);
  542. temp <<= 1;
  543. }
  544. }
  545. }
  546. else
  547. {
  548. for (pos = 0; pos < size * (size / 2) / 8; pos++)
  549. {
  550. temp = asc2_1608[(rt_uint16_t)data * size * (size / 2) / 8 + pos];
  551. for (t = 0; t < 8; t++)
  552. {
  553. if (temp & 0x80)colortemp = FORE_COLOR;
  554. else colortemp = BACK_COLOR;
  555. font_buf[2 * (8 * pos + t)] = colortemp >> 8;
  556. font_buf[2 * (8 * pos + t) + 1] = colortemp;
  557. temp <<= 1;
  558. }
  559. }
  560. rt_pin_write(LCD_DC_PIN, PIN_HIGH);
  561. rt_spi_send(spi_dev_lcd, font_buf, size * size);
  562. rt_free(font_buf);
  563. }
  564. }
  565. else
  566. #endif
  567. #ifdef ASC2_2412
  568. if (size == 24)
  569. {
  570. lcd_address_set(x, y, x + size / 2 - 1, y + size - 1);
  571. font_buf = (rt_uint8_t *)rt_malloc(size * size);
  572. if (!font_buf)
  573. {
  574. /* fast show char */
  575. for (pos = 0; pos < (size * 16) / 8; pos++)
  576. {
  577. temp = asc2_2412[(rt_uint16_t)data * (size * 16) / 8 + pos];
  578. if (pos % 2 == 0)
  579. {
  580. num = 8;
  581. }
  582. else
  583. {
  584. num = 4;
  585. }
  586. for (t = 0; t < num; t++)
  587. {
  588. if (temp & 0x80)colortemp = FORE_COLOR;
  589. else colortemp = BACK_COLOR;
  590. lcd_write_half_word(colortemp);
  591. temp <<= 1;
  592. }
  593. }
  594. }
  595. else
  596. {
  597. for (pos = 0; pos < (size * 16) / 8; pos++)
  598. {
  599. temp = asc2_2412[(rt_uint16_t)data * (size * 16) / 8 + pos];
  600. if (pos % 2 == 0)
  601. {
  602. num = 8;
  603. }
  604. else
  605. {
  606. num = 4;
  607. }
  608. for (t = 0; t < num; t++)
  609. {
  610. if (temp & 0x80)colortemp = FORE_COLOR;
  611. else colortemp = BACK_COLOR;
  612. if (num == 8)
  613. {
  614. font_buf[2 * (12 * (pos / 2) + t)] = colortemp >> 8;
  615. font_buf[2 * (12 * (pos / 2) + t) + 1] = colortemp;
  616. }
  617. else
  618. {
  619. font_buf[2 * (8 + 12 * (pos / 2) + t)] = colortemp >> 8;
  620. font_buf[2 * (8 + 12 * (pos / 2) + t) + 1] = colortemp;
  621. }
  622. temp <<= 1;
  623. }
  624. }
  625. rt_pin_write(LCD_DC_PIN, PIN_HIGH);
  626. rt_spi_send(spi_dev_lcd, font_buf, size * size);
  627. rt_free(font_buf);
  628. }
  629. }
  630. else
  631. #endif
  632. #ifdef ASC2_3216
  633. if (size == 32)
  634. {
  635. lcd_address_set(x, y, x + size / 2 - 1, y + size - 1);
  636. font_buf = (rt_uint8_t *)rt_malloc(size * size);
  637. if (!font_buf)
  638. {
  639. /* fast show char */
  640. for (pos = 0; pos < size * (size / 2) / 8; pos++)
  641. {
  642. temp = asc2_3216[(rt_uint16_t)data * size * (size / 2) / 8 + pos];
  643. for (t = 0; t < 8; t++)
  644. {
  645. if (temp & 0x80)colortemp = FORE_COLOR;
  646. else colortemp = BACK_COLOR;
  647. lcd_write_half_word(colortemp);
  648. temp <<= 1;
  649. }
  650. }
  651. }
  652. else
  653. {
  654. for (pos = 0; pos < size * (size / 2) / 8; pos++)
  655. {
  656. temp = asc2_3216[(rt_uint16_t)data * size * (size / 2) / 8 + pos];
  657. for (t = 0; t < 8; t++)
  658. {
  659. if (temp & 0x80)colortemp = FORE_COLOR;
  660. else colortemp = BACK_COLOR;
  661. font_buf[2 * (8 * pos + t)] = colortemp >> 8;
  662. font_buf[2 * (8 * pos + t) + 1] = colortemp;
  663. temp <<= 1;
  664. }
  665. }
  666. rt_pin_write(LCD_DC_PIN, PIN_HIGH);
  667. rt_spi_send(spi_dev_lcd, font_buf, size * size);
  668. rt_free(font_buf);
  669. }
  670. }
  671. else
  672. #endif
  673. {
  674. LOG_E("There is no any define ASC2_1208 && ASC2_2412 && ASC2_2416 && ASC2_3216 !");
  675. }
  676. }
  677. /**
  678. * display the number on the lcd.
  679. *
  680. * @param x x position
  681. * @param y y position
  682. * @param num number
  683. * @param len length of number
  684. * @param size size of font
  685. *
  686. * @return void
  687. */
  688. void lcd_show_num(rt_uint16_t x, rt_uint16_t y, rt_uint32_t num, rt_uint8_t len, rt_uint32_t size)
  689. {
  690. lcd_show_string(x, y, size, "%d", num);
  691. }
  692. /**
  693. * display the string on the lcd.
  694. *
  695. * @param x x position
  696. * @param y y position
  697. * @param size size of font
  698. * @param p the string to be display
  699. *
  700. * @return 0: display success
  701. * -1: size of font is not support
  702. */
  703. rt_err_t lcd_show_string(rt_uint16_t x, rt_uint16_t y, rt_uint32_t size, const char *fmt, ...)
  704. {
  705. #define LCD_STRING_BUF_LEN 128
  706. va_list args;
  707. rt_uint8_t buf[LCD_STRING_BUF_LEN] = {0};
  708. rt_uint8_t *p = RT_NULL;
  709. if (size != 16 && size != 24 && size != 32)
  710. {
  711. LOG_E("font size(%d) is not support!", size);
  712. return -RT_ERROR;
  713. }
  714. va_start(args, fmt);
  715. rt_vsnprintf((char *)buf, 100, (const char *)fmt, args);
  716. va_end(args);
  717. p = buf;
  718. while (*p != '\0')
  719. {
  720. if (x > LCD_W - size / 2)
  721. {
  722. x = 0;
  723. y += size;
  724. }
  725. if (y > LCD_H - size)
  726. {
  727. y = x = 0;
  728. lcd_clear(RED);
  729. }
  730. lcd_show_char(x, y, *p, size);
  731. x += size / 2;
  732. p++;
  733. }
  734. return RT_EOK;
  735. }
  736. /**
  737. * display the image on the lcd.
  738. *
  739. * @param x x position
  740. * @param y y position
  741. * @param length length of image
  742. * @param wide wide of image
  743. * @param p image
  744. *
  745. * @return 0: display success
  746. * -1: the image is too large
  747. */
  748. rt_err_t lcd_show_image(rt_uint16_t x, rt_uint16_t y, rt_uint16_t length, rt_uint16_t wide, const rt_uint8_t *p)
  749. {
  750. RT_ASSERT(p);
  751. if (x + length > LCD_W || y + wide > LCD_H)
  752. {
  753. return -RT_ERROR;
  754. }
  755. lcd_address_set(x, y, x + length - 1, y + wide - 1);
  756. rt_pin_write(LCD_DC_PIN, PIN_HIGH);
  757. rt_spi_send(spi_dev_lcd, p, length * wide * 2);
  758. return RT_EOK;
  759. }
  760. #ifdef PKG_USING_QRCODE
  761. QRCode qrcode;
  762. static rt_uint8_t get_enlargement_factor(rt_uint16_t x, rt_uint16_t y, rt_uint8_t size)
  763. {
  764. rt_uint8_t enlargement_factor = 1 ;
  765. if (x + size * 8 <= LCD_W && y + size * 8 <= LCD_H)
  766. {
  767. enlargement_factor = 8;
  768. }
  769. else if (x + size * 4 <= LCD_W &&y + size * 4 <= LCD_H)
  770. {
  771. enlargement_factor = 4;
  772. }
  773. else if (x + size * 2 <= LCD_W && y + size * 2 <= LCD_H)
  774. {
  775. enlargement_factor = 2;
  776. }
  777. return enlargement_factor;
  778. }
  779. static void show_qrcode_by_point(rt_uint16_t x, rt_uint16_t y, rt_uint8_t size, rt_uint8_t enlargement_factor)
  780. {
  781. rt_uint32_t width = 0, high = 0;
  782. for (high = 0; high < size; high++)
  783. {
  784. for (width = 0; width < size; width++)
  785. {
  786. if (qrcode_getModule(&qrcode, width, high))
  787. {
  788. /* magnify pixel */
  789. for (rt_uint32_t offset_y = 0; offset_y < enlargement_factor; offset_y++)
  790. {
  791. for (rt_uint32_t offset_x = 0; offset_x < enlargement_factor; offset_x++)
  792. {
  793. lcd_draw_point(x + enlargement_factor * width + offset_x, y + enlargement_factor * high + offset_y);
  794. }
  795. }
  796. }
  797. }
  798. }
  799. }
  800. static void show_qrcode_by_line(rt_uint16_t x, rt_uint16_t y, rt_uint8_t size, rt_uint8_t enlargement_factor,rt_uint8_t *qrcode_buf)
  801. {
  802. rt_uint32_t width = 0, high = 0;
  803. for (high = 0; high < qrcode.size; high++)
  804. {
  805. for (width = 0; width < qrcode.size; width++)
  806. {
  807. if (qrcode_getModule(&qrcode, width, high))
  808. {
  809. /* magnify pixel */
  810. for (rt_uint32_t offset_y = 0; offset_y < enlargement_factor; offset_y++)
  811. {
  812. for (rt_uint32_t offset_x = 0; offset_x < enlargement_factor; offset_x++)
  813. {
  814. /* save the information of modules */
  815. qrcode_buf[2 * (enlargement_factor * width + offset_x + offset_y * qrcode.size * enlargement_factor)] = FORE_COLOR >> 8;
  816. qrcode_buf[2 * (enlargement_factor * width + offset_x + offset_y * qrcode.size * enlargement_factor) + 1] = FORE_COLOR;
  817. }
  818. }
  819. }
  820. else
  821. {
  822. /* magnify pixel */
  823. for (rt_uint32_t offset_y = 0; offset_y < enlargement_factor; offset_y++)
  824. {
  825. for (rt_uint32_t offset_x = 0; offset_x < enlargement_factor; offset_x++)
  826. {
  827. /* save the information of blank */
  828. qrcode_buf[2 * (enlargement_factor * width + offset_x + offset_y * qrcode.size * enlargement_factor)] = BACK_COLOR >> 8;
  829. qrcode_buf[2 * (enlargement_factor * width + offset_x + offset_y * qrcode.size * enlargement_factor) + 1] = BACK_COLOR;
  830. }
  831. }
  832. }
  833. }
  834. /* display a line of qrcode */
  835. lcd_show_image(x, y + high * enlargement_factor, qrcode.size * enlargement_factor, enlargement_factor, qrcode_buf);
  836. }
  837. }
  838. /**
  839. * display the qrcode on the lcd.
  840. * size = (4 * version +17) * enlargement
  841. *
  842. * @param x x position
  843. * @param y y position
  844. * @param version version of qrcode
  845. * @param ecc level of error correction
  846. * @param data string
  847. * @param enlargement enlargement_factor
  848. *
  849. * @return 0: display success
  850. * -1: generate qrcode failed
  851. * -5: memory low
  852. */
  853. rt_err_t lcd_show_qrcode(rt_uint16_t x, rt_uint16_t y, rt_uint8_t version, rt_uint8_t ecc, const char *data, rt_uint8_t enlargement)
  854. {
  855. RT_ASSERT(data);
  856. rt_int8_t result = 0;
  857. rt_uint8_t enlargement_factor = 1;
  858. rt_uint8_t *qrcode_buf = RT_NULL;
  859. if (x + version * 4 + 17 > LCD_W || y + version * 4 + 17 > LCD_H)
  860. {
  861. LOG_E("The qrcode is too big!");
  862. return -RT_ERROR;
  863. }
  864. rt_uint8_t *qrcodeBytes = (rt_uint8_t *)rt_calloc(1, qrcode_getBufferSize(version));
  865. if (qrcodeBytes == RT_NULL)
  866. {
  867. LOG_E("no memory for qrcode!");
  868. return -RT_ENOMEM;
  869. }
  870. /* generate qrcode */
  871. result = qrcode_initText(&qrcode, qrcodeBytes, version, ecc, data);
  872. if (result >= 0)
  873. {
  874. /* set enlargement factor */
  875. if(enlargement == 0)
  876. {
  877. enlargement_factor = get_enlargement_factor(x, y, qrcode.size);
  878. }
  879. else
  880. {
  881. enlargement_factor = enlargement;
  882. }
  883. /* malloc memory for quick display of qrcode */
  884. qrcode_buf = rt_malloc(qrcode.size * 2 * enlargement_factor * enlargement_factor);
  885. if (qrcode_buf == RT_NULL)
  886. {
  887. /* clear lcd */
  888. lcd_fill(x, y, x + qrcode.size, y + qrcode.size, BACK_COLOR);
  889. /* draw point to display qrcode */
  890. show_qrcode_by_point(x, y, qrcode.size, enlargement_factor);
  891. }
  892. else
  893. {
  894. /* quick display of qrcode */
  895. show_qrcode_by_line(x, y, qrcode.size, enlargement_factor,qrcode_buf);
  896. }
  897. result = RT_EOK;
  898. }
  899. else
  900. {
  901. LOG_E("QRCODE(%s) generate falied(%d)\n", data, result);
  902. result = -RT_ENOMEM;
  903. goto __exit;
  904. }
  905. __exit:
  906. if (qrcodeBytes)
  907. {
  908. rt_free(qrcodeBytes);
  909. }
  910. if (qrcode_buf)
  911. {
  912. rt_free(qrcode_buf);
  913. }
  914. return result;
  915. }
  916. #endif