drv_lcd.c 22 KB

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