drv_lcd.c 21 KB

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