drv_lcd.c 20 KB

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