drv_lcd.c 22 KB

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