drv_lcd.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  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 = {0};
  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. if(percent > 100)
  210. {
  211. percent = 100;
  212. }
  213. pwm_dev = (struct rt_device_pwm*)rt_device_find("pwm4");
  214. if(pwm_dev != RT_NULL)
  215. {
  216. rt_pwm_set(pwm_dev, 2, 1000000, percent*10000); /* PB7, PWM4 CH2 with 1000Hz */
  217. rt_pwm_enable(pwm_dev, 2);
  218. }
  219. }
  220. void lcd_display_on(void)
  221. {
  222. lcd_display_brightness(100);
  223. }
  224. void lcd_display_off(void)
  225. {
  226. lcd_display_brightness(0);
  227. }
  228. /* lcd enter the minimum power consumption mode and backlight off. */
  229. void lcd_enter_sleep(void)
  230. {
  231. lcd_display_off();
  232. rt_thread_mdelay(5);
  233. lcd_write_cmd(0x10);
  234. }
  235. /* lcd turn off sleep mode and backlight on. */
  236. void lcd_exit_sleep(void)
  237. {
  238. lcd_display_on();
  239. rt_thread_mdelay(5);
  240. lcd_write_cmd(0x11);
  241. rt_thread_mdelay(120);
  242. }
  243. /**
  244. * Set drawing area
  245. *
  246. * @param x1 start of x position
  247. * @param y1 start of y position
  248. * @param x2 end of x position
  249. * @param y2 end of y position
  250. *
  251. * @return void
  252. */
  253. void lcd_address_set(rt_uint16_t x1, rt_uint16_t y1, rt_uint16_t x2, rt_uint16_t y2)
  254. {
  255. lcd_write_cmd(0x2a);
  256. lcd_write_data(x1 >> 8);
  257. lcd_write_data(x1);
  258. lcd_write_data(x2 >> 8);
  259. lcd_write_data(x2);
  260. lcd_write_cmd(0x2b);
  261. lcd_write_data(y1 >> 8);
  262. lcd_write_data(y1);
  263. lcd_write_data(y2 >> 8);
  264. lcd_write_data(y2);
  265. lcd_write_cmd(0x2C);
  266. }
  267. #ifndef BSP_USING_LVGL
  268. /**
  269. * clear the lcd.
  270. *
  271. * @param color Fill color
  272. *
  273. * @return void
  274. */
  275. void lcd_clear(rt_uint16_t color)
  276. {
  277. rt_uint16_t i, j;
  278. rt_uint8_t data[2] = {0};
  279. rt_uint8_t *buf = RT_NULL;
  280. data[0] = color >> 8;
  281. data[1] = color;
  282. lcd_address_set(0, 0, LCD_W - 1, LCD_H - 1);
  283. buf = rt_malloc(LCD_CLEAR_SEND_NUMBER);
  284. if (buf)
  285. {
  286. /* 2880 = 5760/2 color is 16 bit */
  287. for (j = 0; j < LCD_CLEAR_SEND_NUMBER / 2; j++)
  288. {
  289. buf[j * 2] = data[0];
  290. buf[j * 2 + 1] = data[1];
  291. }
  292. rt_pin_write(LCD_DC_PIN, PIN_HIGH);
  293. for (i = 0; i < 20; i++)
  294. {
  295. rt_spi_send(spi_dev_lcd, buf, LCD_CLEAR_SEND_NUMBER);
  296. }
  297. rt_free(buf);
  298. }
  299. else
  300. {
  301. rt_pin_write(LCD_DC_PIN, PIN_HIGH);
  302. for (i = 0; i < LCD_W; i++)
  303. {
  304. for (j = 0; j < LCD_H; j++)
  305. {
  306. rt_spi_send(spi_dev_lcd, data, 2);
  307. }
  308. }
  309. }
  310. }
  311. /**
  312. * display a point on the lcd.
  313. *
  314. * @param x x position
  315. * @param y y position
  316. *
  317. * @return void
  318. */
  319. void lcd_draw_point(rt_uint16_t x, rt_uint16_t y)
  320. {
  321. lcd_address_set(x, y, x, y);
  322. lcd_write_half_word(FORE_COLOR);
  323. }
  324. /**
  325. * display a point on the lcd using the given colour.
  326. *
  327. * @param x x position
  328. * @param y y position
  329. * @param color color of point
  330. *
  331. * @return void
  332. */
  333. void lcd_draw_point_color(rt_uint16_t x, rt_uint16_t y, rt_uint16_t color)
  334. {
  335. lcd_address_set(x, y, x, y);
  336. lcd_write_half_word(color);
  337. }
  338. /**
  339. * full color on the lcd.
  340. *
  341. * @param x_start start of x position
  342. * @param y_start start of y position
  343. * @param x_end end of x position
  344. * @param y_end end of y position
  345. * @param color Fill color
  346. *
  347. * @return void
  348. */
  349. 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)
  350. {
  351. rt_uint16_t i = 0, j = 0;
  352. rt_uint32_t size = 0, size_remain = 0;
  353. rt_uint8_t *fill_buf = RT_NULL;
  354. size = (x_end - x_start + 1) * (y_end - y_start + 1) * 2;
  355. if (size > LCD_CLEAR_SEND_NUMBER)
  356. {
  357. /* the number of remaining to be filled */
  358. size_remain = size - LCD_CLEAR_SEND_NUMBER;
  359. size = LCD_CLEAR_SEND_NUMBER;
  360. }
  361. lcd_address_set(x_start, y_start, x_end, y_end);
  362. fill_buf = (rt_uint8_t *)rt_malloc(size);
  363. if (fill_buf)
  364. {
  365. /* fast fill */
  366. while (1)
  367. {
  368. for (i = 0; i < size / 2; i++)
  369. {
  370. fill_buf[2 * i] = color >> 8;
  371. fill_buf[2 * i + 1] = color;
  372. }
  373. rt_pin_write(LCD_DC_PIN, PIN_HIGH);
  374. rt_spi_send(spi_dev_lcd, fill_buf, size);
  375. /* Fill completed */
  376. if (size_remain == 0)
  377. break;
  378. /* calculate the number of fill next time */
  379. if (size_remain > LCD_CLEAR_SEND_NUMBER)
  380. {
  381. size_remain = size_remain - LCD_CLEAR_SEND_NUMBER;
  382. }
  383. else
  384. {
  385. size = size_remain;
  386. size_remain = 0;
  387. }
  388. }
  389. rt_free(fill_buf);
  390. }
  391. else
  392. {
  393. for (i = y_start; i <= y_end; i++)
  394. {
  395. for (j = x_start; j <= x_end; j++)lcd_write_half_word(color);
  396. }
  397. }
  398. }
  399. #endif /* BSP_USING_LVGL */
  400. /**
  401. * full color array on the lcd.
  402. *
  403. * @param x_start start of x position
  404. * @param y_start start of y position
  405. * @param x_end end of x position
  406. * @param y_end end of y position
  407. * @param color Fill color array's pointer
  408. *
  409. * @return void
  410. */
  411. 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)
  412. {
  413. rt_uint32_t size = 0;
  414. size = (x_end - x_start + 1) * (y_end - y_start + 1) * 2/*16bit*/;
  415. lcd_address_set(x_start, y_start, x_end, y_end);
  416. rt_pin_write(LCD_DC_PIN, PIN_HIGH);
  417. rt_spi_send(spi_dev_lcd, pcolor, size);
  418. }
  419. #ifndef BSP_USING_LVGL
  420. /**
  421. * display a line on the lcd.
  422. *
  423. * @param x1 x1 position
  424. * @param y1 y1 position
  425. * @param x2 x2 position
  426. * @param y2 y2 position
  427. *
  428. * @return void
  429. */
  430. void lcd_draw_line(rt_uint16_t x1, rt_uint16_t y1, rt_uint16_t x2, rt_uint16_t y2)
  431. {
  432. rt_uint16_t t;
  433. rt_uint32_t i = 0;
  434. int xerr = 0, yerr = 0, delta_x, delta_y, distance;
  435. int incx, incy, row, col;
  436. if (y1 == y2)
  437. {
  438. /* fast draw transverse line */
  439. rt_uint32_t x_offset = 0;
  440. if (x1 < x2)
  441. {
  442. x_offset = x2 - x1;
  443. lcd_address_set(x1, y1, x2, y2);
  444. }
  445. else if (x1 > x2)
  446. {
  447. x_offset = x1 - x2;
  448. lcd_address_set(x2, y2, x1, y1);
  449. }
  450. else
  451. {
  452. lcd_draw_point(x1, y1);
  453. return;
  454. }
  455. rt_uint8_t line_buf[480] = {0};
  456. for (i = 0; i < x_offset; i++)
  457. {
  458. line_buf[2 * i] = FORE_COLOR >> 8;
  459. line_buf[2 * i + 1] = FORE_COLOR;
  460. }
  461. rt_pin_write(LCD_DC_PIN, PIN_HIGH);
  462. rt_spi_send(spi_dev_lcd, line_buf, x_offset * 2);
  463. return ;
  464. }
  465. delta_x = x2 - x1;
  466. delta_y = y2 - y1;
  467. row = x1;
  468. col = y1;
  469. if (delta_x > 0)incx = 1;
  470. else if (delta_x == 0)incx = 0;
  471. else
  472. {
  473. incx = -1;
  474. delta_x = -delta_x;
  475. }
  476. if (delta_y > 0)incy = 1;
  477. else if (delta_y == 0)incy = 0;
  478. else
  479. {
  480. incy = -1;
  481. delta_y = -delta_y;
  482. }
  483. if (delta_x > delta_y)distance = delta_x;
  484. else distance = delta_y;
  485. for (t = 0; t <= distance + 1; t++)
  486. {
  487. lcd_draw_point(row, col);
  488. xerr += delta_x ;
  489. yerr += delta_y ;
  490. if (xerr > distance)
  491. {
  492. xerr -= distance;
  493. row += incx;
  494. }
  495. if (yerr > distance)
  496. {
  497. yerr -= distance;
  498. col += incy;
  499. }
  500. }
  501. }
  502. /**
  503. * display a rectangle on the lcd.
  504. *
  505. * @param x1 x1 position
  506. * @param y1 y1 position
  507. * @param x2 x2 position
  508. * @param y2 y2 position
  509. *
  510. * @return void
  511. */
  512. void lcd_draw_rectangle(rt_uint16_t x1, rt_uint16_t y1, rt_uint16_t x2, rt_uint16_t y2)
  513. {
  514. lcd_draw_line(x1, y1, x2, y1);
  515. lcd_draw_line(x1, y1, x1, y2);
  516. lcd_draw_line(x1, y2, x2, y2);
  517. lcd_draw_line(x2, y1, x2, y2);
  518. }
  519. /**
  520. * display a circle on the lcd.
  521. *
  522. * @param x x position of Center
  523. * @param y y position of Center
  524. * @param r radius
  525. *
  526. * @return void
  527. */
  528. void lcd_draw_circle(rt_uint16_t x0, rt_uint16_t y0, rt_uint8_t r)
  529. {
  530. int a, b;
  531. int di;
  532. a = 0;
  533. b = r;
  534. di = 3 - (r << 1);
  535. while (a <= b)
  536. {
  537. lcd_draw_point(x0 - b, y0 - a);
  538. lcd_draw_point(x0 + b, y0 - a);
  539. lcd_draw_point(x0 - a, y0 + b);
  540. lcd_draw_point(x0 - b, y0 - a);
  541. lcd_draw_point(x0 - a, y0 - b);
  542. lcd_draw_point(x0 + b, y0 + a);
  543. lcd_draw_point(x0 + a, y0 - b);
  544. lcd_draw_point(x0 + a, y0 + b);
  545. lcd_draw_point(x0 - b, y0 + a);
  546. a++;
  547. //Bresenham
  548. if (di < 0)di += 4 * a + 6;
  549. else
  550. {
  551. di += 10 + 4 * (a - b);
  552. b--;
  553. }
  554. lcd_draw_point(x0 + a, y0 + b);
  555. }
  556. }
  557. static void lcd_show_char(rt_uint16_t x, rt_uint16_t y, rt_uint8_t data, rt_uint32_t size)
  558. {
  559. rt_uint8_t temp;
  560. rt_uint8_t num = 0;;
  561. rt_uint8_t pos, t;
  562. rt_uint16_t colortemp = FORE_COLOR;
  563. rt_uint8_t *font_buf = RT_NULL;
  564. if (x > LCD_W - size / 2 || y > LCD_H - size)return;
  565. data = data - ' ';
  566. #ifdef ASC2_1608
  567. if (size == 16)
  568. {
  569. lcd_address_set(x, y, x + size / 2 - 1, y + size - 1);//(x,y,x+8-1,y+16-1)
  570. font_buf = (rt_uint8_t *)rt_malloc(size * size);
  571. if (!font_buf)
  572. {
  573. /* fast show char */
  574. for (pos = 0; pos < size * (size / 2) / 8; pos++)
  575. {
  576. temp = asc2_1608[(rt_uint16_t)data * size * (size / 2) / 8 + pos];
  577. for (t = 0; t < 8; t++)
  578. {
  579. if (temp & 0x80)colortemp = FORE_COLOR;
  580. else colortemp = BACK_COLOR;
  581. lcd_write_half_word(colortemp);
  582. temp <<= 1;
  583. }
  584. }
  585. }
  586. else
  587. {
  588. for (pos = 0; pos < size * (size / 2) / 8; pos++)
  589. {
  590. temp = asc2_1608[(rt_uint16_t)data * size * (size / 2) / 8 + pos];
  591. for (t = 0; t < 8; t++)
  592. {
  593. if (temp & 0x80)colortemp = FORE_COLOR;
  594. else colortemp = BACK_COLOR;
  595. font_buf[2 * (8 * pos + t)] = colortemp >> 8;
  596. font_buf[2 * (8 * pos + t) + 1] = colortemp;
  597. temp <<= 1;
  598. }
  599. }
  600. rt_pin_write(LCD_DC_PIN, PIN_HIGH);
  601. rt_spi_send(spi_dev_lcd, font_buf, size * size);
  602. rt_free(font_buf);
  603. }
  604. }
  605. else
  606. #endif
  607. #ifdef ASC2_2412
  608. if (size == 24)
  609. {
  610. lcd_address_set(x, y, x + size / 2 - 1, y + size - 1);
  611. font_buf = (rt_uint8_t *)rt_malloc(size * size);
  612. if (!font_buf)
  613. {
  614. /* fast show char */
  615. for (pos = 0; pos < (size * 16) / 8; pos++)
  616. {
  617. temp = asc2_2412[(rt_uint16_t)data * (size * 16) / 8 + pos];
  618. if (pos % 2 == 0)
  619. {
  620. num = 8;
  621. }
  622. else
  623. {
  624. num = 4;
  625. }
  626. for (t = 0; t < num; 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 * 16) / 8; pos++)
  638. {
  639. temp = asc2_2412[(rt_uint16_t)data * (size * 16) / 8 + pos];
  640. if (pos % 2 == 0)
  641. {
  642. num = 8;
  643. }
  644. else
  645. {
  646. num = 4;
  647. }
  648. for (t = 0; t < num; t++)
  649. {
  650. if (temp & 0x80)colortemp = FORE_COLOR;
  651. else colortemp = BACK_COLOR;
  652. if (num == 8)
  653. {
  654. font_buf[2 * (12 * (pos / 2) + t)] = colortemp >> 8;
  655. font_buf[2 * (12 * (pos / 2) + t) + 1] = colortemp;
  656. }
  657. else
  658. {
  659. font_buf[2 * (8 + 12 * (pos / 2) + t)] = colortemp >> 8;
  660. font_buf[2 * (8 + 12 * (pos / 2) + t) + 1] = colortemp;
  661. }
  662. temp <<= 1;
  663. }
  664. }
  665. rt_pin_write(LCD_DC_PIN, PIN_HIGH);
  666. rt_spi_send(spi_dev_lcd, font_buf, size * size);
  667. rt_free(font_buf);
  668. }
  669. }
  670. else
  671. #endif
  672. #ifdef ASC2_3216
  673. if (size == 32)
  674. {
  675. lcd_address_set(x, y, x + size / 2 - 1, y + size - 1);
  676. font_buf = (rt_uint8_t *)rt_malloc(size * size);
  677. if (!font_buf)
  678. {
  679. /* fast show char */
  680. for (pos = 0; pos < size * (size / 2) / 8; pos++)
  681. {
  682. temp = asc2_3216[(rt_uint16_t)data * size * (size / 2) / 8 + pos];
  683. for (t = 0; t < 8; t++)
  684. {
  685. if (temp & 0x80)colortemp = FORE_COLOR;
  686. else colortemp = BACK_COLOR;
  687. lcd_write_half_word(colortemp);
  688. temp <<= 1;
  689. }
  690. }
  691. }
  692. else
  693. {
  694. for (pos = 0; pos < size * (size / 2) / 8; pos++)
  695. {
  696. temp = asc2_3216[(rt_uint16_t)data * size * (size / 2) / 8 + pos];
  697. for (t = 0; t < 8; t++)
  698. {
  699. if (temp & 0x80)colortemp = FORE_COLOR;
  700. else colortemp = BACK_COLOR;
  701. font_buf[2 * (8 * pos + t)] = colortemp >> 8;
  702. font_buf[2 * (8 * pos + t) + 1] = colortemp;
  703. temp <<= 1;
  704. }
  705. }
  706. rt_pin_write(LCD_DC_PIN, PIN_HIGH);
  707. rt_spi_send(spi_dev_lcd, font_buf, size * size);
  708. rt_free(font_buf);
  709. }
  710. }
  711. else
  712. #endif
  713. {
  714. LOG_E("There is no any define ASC2_1208 && ASC2_2412 && ASC2_2416 && ASC2_3216 !");
  715. }
  716. }
  717. /**
  718. * display the number on the lcd.
  719. *
  720. * @param x x position
  721. * @param y y position
  722. * @param num number
  723. * @param len length of number
  724. * @param size size of font
  725. *
  726. * @return void
  727. */
  728. void lcd_show_num(rt_uint16_t x, rt_uint16_t y, rt_uint32_t num, rt_uint8_t len, rt_uint32_t size)
  729. {
  730. lcd_show_string(x, y, size, "%d", num);
  731. }
  732. /**
  733. * display the string on the lcd.
  734. *
  735. * @param x x position
  736. * @param y y position
  737. * @param size size of font
  738. * @param p the string to be display
  739. *
  740. * @return 0: display success
  741. * -1: size of font is not support
  742. */
  743. rt_err_t lcd_show_string(rt_uint16_t x, rt_uint16_t y, rt_uint32_t size, const char *fmt, ...)
  744. {
  745. #define LCD_STRING_BUF_LEN 128
  746. va_list args;
  747. rt_uint8_t buf[LCD_STRING_BUF_LEN] = {0};
  748. rt_uint8_t *p = RT_NULL;
  749. if (size != 16 && size != 24 && size != 32)
  750. {
  751. LOG_E("font size(%d) is not support!", size);
  752. return -RT_ERROR;
  753. }
  754. va_start(args, fmt);
  755. rt_vsnprintf((char *)buf, 100, (const char *)fmt, args);
  756. va_end(args);
  757. p = buf;
  758. while (*p != '\0')
  759. {
  760. if (x > LCD_W - size / 2)
  761. {
  762. x = 0;
  763. y += size;
  764. }
  765. if (y > LCD_H - size)
  766. {
  767. y = x = 0;
  768. lcd_clear(RED);
  769. }
  770. lcd_show_char(x, y, *p, size);
  771. x += size / 2;
  772. p++;
  773. }
  774. return RT_EOK;
  775. }
  776. /**
  777. * display the image on the lcd.
  778. *
  779. * @param x x position
  780. * @param y y position
  781. * @param length length of image
  782. * @param wide wide of image
  783. * @param p image
  784. *
  785. * @return 0: display success
  786. * -1: the image is too large
  787. */
  788. 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)
  789. {
  790. RT_ASSERT(p);
  791. if (x + length > LCD_W || y + wide > LCD_H)
  792. {
  793. return -RT_ERROR;
  794. }
  795. lcd_address_set(x, y, x + length - 1, y + wide - 1);
  796. rt_pin_write(LCD_DC_PIN, PIN_HIGH);
  797. rt_spi_send(spi_dev_lcd, p, length * wide * 2);
  798. return RT_EOK;
  799. }
  800. #endif /* BSP_USING_LVGL */