drv_lcd.c 22 KB

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