drv_lcd.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996
  1. /*
  2. * Copyright (c) 2006-2018, 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. */
  12. #include <rtdevice.h>
  13. #include "drv_spi.h"
  14. #include "drv_lcd.h"
  15. #include "drv_lcd_font.h"
  16. #include "drv_gpio.h"
  17. #define DBG_SECTION_NAME "LCD"
  18. #define DBG_COLOR
  19. #define DBG_LEVEL DBG_LOG
  20. #include <rtdbg.h>
  21. #define LCD_PWR_PIN GET_PIN(B, 7)
  22. #define LCD_DC_PIN GET_PIN(B, 4)
  23. #define LCD_RES_PIN GET_PIN(B, 6)
  24. #define LCD_CLEAR_SEND_NUMBER 5760
  25. rt_uint16_t BACK_COLOR = WHITE, FORE_COLOR = BLACK;
  26. static struct rt_spi_device *spi_dev_lcd;
  27. static int rt_hw_lcd_config(void)
  28. {
  29. spi_dev_lcd = (struct rt_spi_device *)rt_device_find("spi30");
  30. /* config spi */
  31. {
  32. struct rt_spi_configuration cfg;
  33. cfg.data_width = 8;
  34. cfg.mode = RT_SPI_MASTER | RT_SPI_MODE_0 | RT_SPI_MSB;
  35. cfg.max_hz = 42 * 1000 * 1000; /* 42M,SPI max 42MHz,lcd 4-wire spi */
  36. rt_spi_configure(spi_dev_lcd, &cfg);
  37. }
  38. return RT_EOK;
  39. }
  40. static rt_err_t lcd_write_cmd(const rt_uint8_t cmd)
  41. {
  42. rt_size_t len;
  43. rt_pin_write(LCD_DC_PIN, PIN_LOW);
  44. len = rt_spi_send(spi_dev_lcd, &cmd, 1);
  45. if (len != 1)
  46. {
  47. LOG_I("lcd_write_cmd error. %d", len);
  48. return -RT_ERROR;
  49. }
  50. else
  51. {
  52. return RT_EOK;
  53. }
  54. }
  55. static rt_err_t lcd_write_data(const rt_uint8_t data)
  56. {
  57. rt_size_t len;
  58. rt_pin_write(LCD_DC_PIN, PIN_HIGH);
  59. len = rt_spi_send(spi_dev_lcd, &data, 1);
  60. if (len != 1)
  61. {
  62. LOG_I("lcd_write_data error. %d", len);
  63. return -RT_ERROR;
  64. }
  65. else
  66. {
  67. return RT_EOK;
  68. }
  69. }
  70. static rt_err_t lcd_write_half_word(const rt_uint16_t da)
  71. {
  72. rt_size_t len;
  73. char data[2] = {0};
  74. data[0] = da >> 8;
  75. data[1] = da;
  76. rt_pin_write(LCD_DC_PIN, PIN_HIGH);
  77. len = rt_spi_send(spi_dev_lcd, data, 2);
  78. if (len != 2)
  79. {
  80. LOG_I("lcd_write_half_word error. %d", len);
  81. return -RT_ERROR;
  82. }
  83. else
  84. {
  85. return RT_EOK;
  86. }
  87. }
  88. static void lcd_gpio_init(void)
  89. {
  90. rt_hw_lcd_config();
  91. rt_pin_mode(LCD_DC_PIN, PIN_MODE_OUTPUT);
  92. rt_pin_mode(LCD_RES_PIN, PIN_MODE_OUTPUT);
  93. rt_pin_mode(LCD_PWR_PIN, PIN_MODE_OUTPUT);
  94. rt_pin_write(LCD_PWR_PIN, PIN_LOW);
  95. rt_pin_write(LCD_RES_PIN, PIN_LOW);
  96. //wait at least 100ms for reset
  97. rt_thread_delay(RT_TICK_PER_SECOND / 10);
  98. rt_pin_write(LCD_RES_PIN, PIN_HIGH);
  99. }
  100. static int rt_hw_lcd_init(void)
  101. {
  102. __HAL_RCC_GPIOD_CLK_ENABLE();
  103. rt_hw_spi_device_attach("spi3", "spi30", GPIOD, GPIO_PIN_7);
  104. lcd_gpio_init();
  105. /* Memory Data Access Control */
  106. lcd_write_cmd(0x36);
  107. lcd_write_data(0x00);
  108. /* RGB 5-6-5-bit */
  109. lcd_write_cmd(0x3A);
  110. lcd_write_data(0x65);
  111. /* Porch Setting */
  112. lcd_write_cmd(0xB2);
  113. lcd_write_data(0x0C);
  114. lcd_write_data(0x0C);
  115. lcd_write_data(0x00);
  116. lcd_write_data(0x33);
  117. lcd_write_data(0x33);
  118. /* Gate Control */
  119. lcd_write_cmd(0xB7);
  120. lcd_write_data(0x35);
  121. /* VCOM Setting */
  122. lcd_write_cmd(0xBB);
  123. lcd_write_data(0x19);
  124. /* LCM Control */
  125. lcd_write_cmd(0xC0);
  126. lcd_write_data(0x2C);
  127. /* VDV and VRH Command Enable */
  128. lcd_write_cmd(0xC2);
  129. lcd_write_data(0x01);
  130. /* VRH Set */
  131. lcd_write_cmd(0xC3);
  132. lcd_write_data(0x12);
  133. /* VDV Set */
  134. lcd_write_cmd(0xC4);
  135. lcd_write_data(0x20);
  136. /* Frame Rate Control in Normal Mode */
  137. lcd_write_cmd(0xC6);
  138. lcd_write_data(0x0F);
  139. /* Power Control 1 */
  140. lcd_write_cmd(0xD0);
  141. lcd_write_data(0xA4);
  142. lcd_write_data(0xA1);
  143. /* Positive Voltage Gamma Control */
  144. lcd_write_cmd(0xE0);
  145. lcd_write_data(0xD0);
  146. lcd_write_data(0x04);
  147. lcd_write_data(0x0D);
  148. lcd_write_data(0x11);
  149. lcd_write_data(0x13);
  150. lcd_write_data(0x2B);
  151. lcd_write_data(0x3F);
  152. lcd_write_data(0x54);
  153. lcd_write_data(0x4C);
  154. lcd_write_data(0x18);
  155. lcd_write_data(0x0D);
  156. lcd_write_data(0x0B);
  157. lcd_write_data(0x1F);
  158. lcd_write_data(0x23);
  159. /* Negative Voltage Gamma Control */
  160. lcd_write_cmd(0xE1);
  161. lcd_write_data(0xD0);
  162. lcd_write_data(0x04);
  163. lcd_write_data(0x0C);
  164. lcd_write_data(0x11);
  165. lcd_write_data(0x13);
  166. lcd_write_data(0x2C);
  167. lcd_write_data(0x3F);
  168. lcd_write_data(0x44);
  169. lcd_write_data(0x51);
  170. lcd_write_data(0x2F);
  171. lcd_write_data(0x1F);
  172. lcd_write_data(0x1F);
  173. lcd_write_data(0x20);
  174. lcd_write_data(0x23);
  175. /* Display Inversion On */
  176. lcd_write_cmd(0x21);
  177. /* Sleep Out */
  178. lcd_write_cmd(0x11);
  179. /* wait for power stability */
  180. rt_thread_mdelay(100);
  181. lcd_clear(WHITE);
  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. /* 5760 = 240*240/20 */
  264. buf = rt_malloc(LCD_CLEAR_SEND_NUMBER);
  265. if (buf)
  266. {
  267. /* 2880 = 5760/2 color is 16 bit */
  268. for (j = 0; j < LCD_CLEAR_SEND_NUMBER / 2; j++)
  269. {
  270. buf[j * 2] = data[0];
  271. buf[j * 2 + 1] = data[1];
  272. }
  273. rt_pin_write(LCD_DC_PIN, PIN_HIGH);
  274. for (i = 0; i < 20; i++)
  275. {
  276. rt_spi_send(spi_dev_lcd, buf, LCD_CLEAR_SEND_NUMBER);
  277. }
  278. rt_free(buf);
  279. }
  280. else
  281. {
  282. rt_pin_write(LCD_DC_PIN, PIN_HIGH);
  283. for (i = 0; i < LCD_W; i++)
  284. {
  285. for (j = 0; j < LCD_H; j++)
  286. {
  287. rt_spi_send(spi_dev_lcd, data, 2);
  288. }
  289. }
  290. }
  291. }
  292. /**
  293. * display a point on the lcd.
  294. *
  295. * @param x x position
  296. * @param y y position
  297. *
  298. * @return void
  299. */
  300. void lcd_draw_point(rt_uint16_t x, rt_uint16_t y)
  301. {
  302. lcd_address_set(x, y, x, y);
  303. lcd_write_half_word(FORE_COLOR);
  304. }
  305. /**
  306. * display a point on the lcd using the given colour.
  307. *
  308. * @param x x position
  309. * @param y y position
  310. * @param color color of point
  311. *
  312. * @return void
  313. */
  314. void lcd_draw_point_color(rt_uint16_t x, rt_uint16_t y, rt_uint16_t color)
  315. {
  316. lcd_address_set(x, y, x, y);
  317. lcd_write_half_word(color);
  318. }
  319. /**
  320. * full color on the lcd.
  321. *
  322. * @param x_start start of x position
  323. * @param y_start start of y position
  324. * @param x_end end of x position
  325. * @param y_end end of y position
  326. * @param color Fill color
  327. *
  328. * @return void
  329. */
  330. 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)
  331. {
  332. rt_uint16_t i = 0, j = 0;
  333. rt_uint32_t size = 0, size_remain = 0;
  334. rt_uint8_t *fill_buf = RT_NULL;
  335. size = (x_end - x_start) * (y_end - y_start) * 2;
  336. if (size > LCD_CLEAR_SEND_NUMBER)
  337. {
  338. /* the number of remaining to be filled */
  339. size_remain = size - LCD_CLEAR_SEND_NUMBER;
  340. size = LCD_CLEAR_SEND_NUMBER;
  341. }
  342. lcd_address_set(x_start, y_start, x_end, y_end);
  343. fill_buf = (rt_uint8_t *)rt_malloc(size);
  344. if (fill_buf)
  345. {
  346. /* fast fill */
  347. while (1)
  348. {
  349. for (i = 0; i < size / 2; i++)
  350. {
  351. fill_buf[2 * i] = color >> 8;
  352. fill_buf[2 * i + 1] = color;
  353. }
  354. rt_pin_write(LCD_DC_PIN, PIN_HIGH);
  355. rt_spi_send(spi_dev_lcd, fill_buf, size);
  356. /* Fill completed */
  357. if (size_remain == 0)
  358. break;
  359. /* calculate the number of fill next time */
  360. if (size_remain > LCD_CLEAR_SEND_NUMBER)
  361. {
  362. size_remain = size_remain - LCD_CLEAR_SEND_NUMBER;
  363. }
  364. else
  365. {
  366. size = size_remain;
  367. size_remain = 0;
  368. }
  369. }
  370. rt_free(fill_buf);
  371. }
  372. else
  373. {
  374. for (i = y_start; i <= y_end; i++)
  375. {
  376. for (j = x_start; j <= x_end; j++)lcd_write_half_word(color);
  377. }
  378. }
  379. }
  380. /**
  381. * display a line on the lcd.
  382. *
  383. * @param x1 x1 position
  384. * @param y1 y1 position
  385. * @param x2 x2 position
  386. * @param y2 y2 position
  387. *
  388. * @return void
  389. */
  390. void lcd_draw_line(rt_uint16_t x1, rt_uint16_t y1, rt_uint16_t x2, rt_uint16_t y2)
  391. {
  392. rt_uint16_t t;
  393. rt_uint32_t i = 0;
  394. int xerr = 0, yerr = 0, delta_x, delta_y, distance;
  395. int incx, incy, row, col;
  396. if (y1 == y2)
  397. {
  398. /* fast draw transverse line */
  399. lcd_address_set(x1, y1, x2, y2);
  400. rt_uint8_t line_buf[480] = {0};
  401. for (i = 0; i < x2 - x1; i++)
  402. {
  403. line_buf[2 * i] = FORE_COLOR >> 8;
  404. line_buf[2 * i + 1] = FORE_COLOR;
  405. }
  406. rt_pin_write(LCD_DC_PIN, PIN_HIGH);
  407. rt_spi_send(spi_dev_lcd, line_buf, (x2 - x1) * 2);
  408. return ;
  409. }
  410. delta_x = x2 - x1;
  411. delta_y = y2 - y1;
  412. row = x1;
  413. col = y1;
  414. if (delta_x > 0)incx = 1;
  415. else if (delta_x == 0)incx = 0;
  416. else
  417. {
  418. incx = -1;
  419. delta_x = -delta_x;
  420. }
  421. if (delta_y > 0)incy = 1;
  422. else if (delta_y == 0)incy = 0;
  423. else
  424. {
  425. incy = -1;
  426. delta_y = -delta_y;
  427. }
  428. if (delta_x > delta_y)distance = delta_x;
  429. else distance = delta_y;
  430. for (t = 0; t <= distance + 1; t++)
  431. {
  432. lcd_draw_point(row, col);
  433. xerr += delta_x ;
  434. yerr += delta_y ;
  435. if (xerr > distance)
  436. {
  437. xerr -= distance;
  438. row += incx;
  439. }
  440. if (yerr > distance)
  441. {
  442. yerr -= distance;
  443. col += incy;
  444. }
  445. }
  446. }
  447. /**
  448. * display a rectangle on the lcd.
  449. *
  450. * @param x1 x1 position
  451. * @param y1 y1 position
  452. * @param x2 x2 position
  453. * @param y2 y2 position
  454. *
  455. * @return void
  456. */
  457. void lcd_draw_rectangle(rt_uint16_t x1, rt_uint16_t y1, rt_uint16_t x2, rt_uint16_t y2)
  458. {
  459. lcd_draw_line(x1, y1, x2, y1);
  460. lcd_draw_line(x1, y1, x1, y2);
  461. lcd_draw_line(x1, y2, x2, y2);
  462. lcd_draw_line(x2, y1, x2, y2);
  463. }
  464. /**
  465. * display a circle on the lcd.
  466. *
  467. * @param x x position of Center
  468. * @param y y position of Center
  469. * @param r radius
  470. *
  471. * @return void
  472. */
  473. void lcd_draw_circle(rt_uint16_t x0, rt_uint16_t y0, rt_uint8_t r)
  474. {
  475. int a, b;
  476. int di;
  477. a = 0;
  478. b = r;
  479. di = 3 - (r << 1);
  480. while (a <= b)
  481. {
  482. lcd_draw_point(x0 - b, y0 - a);
  483. lcd_draw_point(x0 + b, y0 - a);
  484. lcd_draw_point(x0 - a, y0 + b);
  485. lcd_draw_point(x0 - b, y0 - a);
  486. lcd_draw_point(x0 - a, y0 - b);
  487. lcd_draw_point(x0 + b, y0 + a);
  488. lcd_draw_point(x0 + a, y0 - b);
  489. lcd_draw_point(x0 + a, y0 + b);
  490. lcd_draw_point(x0 - b, y0 + a);
  491. a++;
  492. //Bresenham
  493. if (di < 0)di += 4 * a + 6;
  494. else
  495. {
  496. di += 10 + 4 * (a - b);
  497. b--;
  498. }
  499. lcd_draw_point(x0 + a, y0 + b);
  500. }
  501. }
  502. static void lcd_show_char(rt_uint16_t x, rt_uint16_t y, rt_uint8_t data, rt_uint32_t size)
  503. {
  504. rt_uint8_t temp;
  505. rt_uint8_t num = 0;;
  506. rt_uint8_t pos, t;
  507. rt_uint16_t colortemp = FORE_COLOR;
  508. rt_uint8_t *font_buf = RT_NULL;
  509. if (x > LCD_W - size / 2 || y > LCD_H - size)return;
  510. data = data - ' ';
  511. #ifdef ASC2_1608
  512. if (size == 16)
  513. {
  514. lcd_address_set(x, y, x + size / 2 - 1, y + size - 1);//(x,y,x+8-1,y+16-1)
  515. font_buf = (rt_uint8_t *)rt_malloc(size * size);
  516. if (!font_buf)
  517. {
  518. /* fast show char */
  519. for (pos = 0; pos < size * (size / 2) / 8; pos++)
  520. {
  521. temp = asc2_1608[(rt_uint16_t)data * size * (size / 2) / 8 + pos];
  522. for (t = 0; t < 8; t++)
  523. {
  524. if (temp & 0x80)colortemp = FORE_COLOR;
  525. else colortemp = BACK_COLOR;
  526. lcd_write_half_word(colortemp);
  527. temp <<= 1;
  528. }
  529. }
  530. }
  531. else
  532. {
  533. for (pos = 0; pos < size * (size / 2) / 8; pos++)
  534. {
  535. temp = asc2_1608[(rt_uint16_t)data * size * (size / 2) / 8 + pos];
  536. for (t = 0; t < 8; t++)
  537. {
  538. if (temp & 0x80)colortemp = FORE_COLOR;
  539. else colortemp = BACK_COLOR;
  540. font_buf[2 * (8 * pos + t)] = colortemp >> 8;
  541. font_buf[2 * (8 * pos + t) + 1] = colortemp;
  542. temp <<= 1;
  543. }
  544. }
  545. rt_pin_write(LCD_DC_PIN, PIN_HIGH);
  546. rt_spi_send(spi_dev_lcd, font_buf, size * size);
  547. rt_free(font_buf);
  548. }
  549. }
  550. else
  551. #endif
  552. #ifdef ASC2_2412
  553. if (size == 24)
  554. {
  555. lcd_address_set(x, y, x + size / 2 - 1, y + size - 1);
  556. font_buf = (rt_uint8_t *)rt_malloc(size * size);
  557. if (!font_buf)
  558. {
  559. /* fast show char */
  560. for (pos = 0; pos < (size * 16) / 8; pos++)
  561. {
  562. temp = asc2_2412[(rt_uint16_t)data * (size * 16) / 8 + pos];
  563. if (pos % 2 == 0)
  564. {
  565. num = 8;
  566. }
  567. else
  568. {
  569. num = 4;
  570. }
  571. for (t = 0; t < num; t++)
  572. {
  573. if (temp & 0x80)colortemp = FORE_COLOR;
  574. else colortemp = BACK_COLOR;
  575. lcd_write_half_word(colortemp);
  576. temp <<= 1;
  577. }
  578. }
  579. }
  580. else
  581. {
  582. for (pos = 0; pos < (size * 16) / 8; pos++)
  583. {
  584. temp = asc2_2412[(rt_uint16_t)data * (size * 16) / 8 + pos];
  585. if (pos % 2 == 0)
  586. {
  587. num = 8;
  588. }
  589. else
  590. {
  591. num = 4;
  592. }
  593. for (t = 0; t < num; t++)
  594. {
  595. if (temp & 0x80)colortemp = FORE_COLOR;
  596. else colortemp = BACK_COLOR;
  597. if (num == 8)
  598. {
  599. font_buf[2 * (12 * (pos / 2) + t)] = colortemp >> 8;
  600. font_buf[2 * (12 * (pos / 2) + t) + 1] = colortemp;
  601. }
  602. else
  603. {
  604. font_buf[2 * (8 + 12 * (pos / 2) + t)] = colortemp >> 8;
  605. font_buf[2 * (8 + 12 * (pos / 2) + t) + 1] = colortemp;
  606. }
  607. temp <<= 1;
  608. }
  609. }
  610. rt_pin_write(LCD_DC_PIN, PIN_HIGH);
  611. rt_spi_send(spi_dev_lcd, font_buf, size * size);
  612. rt_free(font_buf);
  613. }
  614. }
  615. else
  616. #endif
  617. #ifdef ASC2_3216
  618. if (size == 32)
  619. {
  620. lcd_address_set(x, y, x + size / 2 - 1, y + size - 1);
  621. font_buf = (rt_uint8_t *)rt_malloc(size * size);
  622. if (!font_buf)
  623. {
  624. /* fast show char */
  625. for (pos = 0; pos < size * (size / 2) / 8; pos++)
  626. {
  627. temp = asc2_3216[(rt_uint16_t)data * size * (size / 2) / 8 + pos];
  628. for (t = 0; t < 8; t++)
  629. {
  630. if (temp & 0x80)colortemp = FORE_COLOR;
  631. else colortemp = BACK_COLOR;
  632. lcd_write_half_word(colortemp);
  633. temp <<= 1;
  634. }
  635. }
  636. }
  637. else
  638. {
  639. for (pos = 0; pos < size * (size / 2) / 8; pos++)
  640. {
  641. temp = asc2_3216[(rt_uint16_t)data * size * (size / 2) / 8 + pos];
  642. for (t = 0; t < 8; t++)
  643. {
  644. if (temp & 0x80)colortemp = FORE_COLOR;
  645. else colortemp = BACK_COLOR;
  646. font_buf[2 * (8 * pos + t)] = colortemp >> 8;
  647. font_buf[2 * (8 * pos + t) + 1] = colortemp;
  648. temp <<= 1;
  649. }
  650. }
  651. rt_pin_write(LCD_DC_PIN, PIN_HIGH);
  652. rt_spi_send(spi_dev_lcd, font_buf, size * size);
  653. rt_free(font_buf);
  654. }
  655. }
  656. else
  657. #endif
  658. {
  659. LOG_E("There is no any define ASC2_1208 && ASC2_2412 && ASC2_2416 && ASC2_3216 !");
  660. }
  661. }
  662. /**
  663. * display the number on the lcd.
  664. *
  665. * @param x x position
  666. * @param y y position
  667. * @param num number
  668. * @param len length of number
  669. * @param size size of font
  670. *
  671. * @return void
  672. */
  673. void lcd_show_num(rt_uint16_t x, rt_uint16_t y, rt_uint32_t num, rt_uint8_t len, rt_uint32_t size)
  674. {
  675. lcd_show_string(x, y, size, "%d", num);
  676. }
  677. /**
  678. * display the string on the lcd.
  679. *
  680. * @param x x position
  681. * @param y y position
  682. * @param size size of font
  683. * @param p the string to be display
  684. *
  685. * @return 0: display success
  686. * -1: size of font is not support
  687. */
  688. rt_err_t lcd_show_string(rt_uint16_t x, rt_uint16_t y, rt_uint32_t size, const char *fmt, ...)
  689. {
  690. #define LCD_STRING_BUF_LEN 128
  691. va_list args;
  692. rt_uint8_t buf[LCD_STRING_BUF_LEN] = {0};
  693. rt_uint8_t *p = RT_NULL;
  694. if (size != 16 && size != 24 && size != 32)
  695. {
  696. LOG_E("font size(%d) is not support!", size);
  697. return -RT_ERROR;
  698. }
  699. va_start(args, fmt);
  700. rt_vsnprintf((char *)buf, 100, (const char *)fmt, args);
  701. va_end(args);
  702. p = buf;
  703. while (*p != '\0')
  704. {
  705. if (x > LCD_W - size / 2)
  706. {
  707. x = 0;
  708. y += size;
  709. }
  710. if (y > LCD_H - size)
  711. {
  712. y = x = 0;
  713. lcd_clear(RED);
  714. }
  715. lcd_show_char(x, y, *p, size);
  716. x += size / 2;
  717. p++;
  718. }
  719. return RT_EOK;
  720. }
  721. /**
  722. * display the image on the lcd.
  723. *
  724. * @param x x position
  725. * @param y y position
  726. * @param length length of image
  727. * @param wide wide of image
  728. * @param p image
  729. *
  730. * @return 0: display success
  731. * -1: the image is too large
  732. */
  733. 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)
  734. {
  735. RT_ASSERT(p);
  736. if (x + length > LCD_W || y + wide > LCD_H)
  737. {
  738. return -RT_ERROR;
  739. }
  740. lcd_address_set(x, y, x + length - 1, y + wide - 1);
  741. rt_pin_write(LCD_DC_PIN, PIN_HIGH);
  742. rt_spi_send(spi_dev_lcd, p, length * wide * 2);
  743. return RT_EOK;
  744. }
  745. #ifdef PKG_USING_QRCODE
  746. QRCode qrcode;
  747. static rt_uint8_t get_enlargement_factor(rt_uint16_t x, rt_uint16_t y, rt_uint8_t size)
  748. {
  749. rt_uint8_t enlargement_factor = 1 ;
  750. if (x + size * 8 <= LCD_W && y + size * 8 <= LCD_H)
  751. {
  752. enlargement_factor = 8;
  753. }
  754. else if (x + size * 4 <= LCD_W &&y + size * 4 <= LCD_H)
  755. {
  756. enlargement_factor = 4;
  757. }
  758. else if (x + size * 2 <= LCD_W && y + size * 2 <= LCD_H)
  759. {
  760. enlargement_factor = 2;
  761. }
  762. return enlargement_factor;
  763. }
  764. static void show_qrcode_by_point(rt_uint16_t x, rt_uint16_t y, rt_uint8_t size, rt_uint8_t enlargement_factor)
  765. {
  766. rt_uint32_t width = 0, high = 0;
  767. for (high = 0; high < size; high++)
  768. {
  769. for (width = 0; width < size; width++)
  770. {
  771. if (qrcode_getModule(&qrcode, width, high))
  772. {
  773. /* magnify pixel */
  774. for (rt_uint32_t offset_y = 0; offset_y < enlargement_factor; offset_y++)
  775. {
  776. for (rt_uint32_t offset_x = 0; offset_x < enlargement_factor; offset_x++)
  777. {
  778. lcd_draw_point(x + enlargement_factor * width + offset_x, y + enlargement_factor * high + offset_y);
  779. }
  780. }
  781. }
  782. }
  783. }
  784. }
  785. static void show_qrcode_by_line(rt_uint16_t x, rt_uint16_t y, rt_uint8_t size, rt_uint8_t enlargement_factor,rt_uint8_t *qrcode_buf)
  786. {
  787. rt_uint32_t width = 0, high = 0;
  788. for (high = 0; high < qrcode.size; high++)
  789. {
  790. for (width = 0; width < qrcode.size; width++)
  791. {
  792. if (qrcode_getModule(&qrcode, width, high))
  793. {
  794. /* magnify pixel */
  795. for (rt_uint32_t offset_y = 0; offset_y < enlargement_factor; offset_y++)
  796. {
  797. for (rt_uint32_t offset_x = 0; offset_x < enlargement_factor; offset_x++)
  798. {
  799. /* save the information of modules */
  800. qrcode_buf[2 * (enlargement_factor * width + offset_x + offset_y * qrcode.size * enlargement_factor)] = FORE_COLOR >> 8;
  801. qrcode_buf[2 * (enlargement_factor * width + offset_x + offset_y * qrcode.size * enlargement_factor) + 1] = FORE_COLOR;
  802. }
  803. }
  804. }
  805. else
  806. {
  807. /* magnify pixel */
  808. for (rt_uint32_t offset_y = 0; offset_y < enlargement_factor; offset_y++)
  809. {
  810. for (rt_uint32_t offset_x = 0; offset_x < enlargement_factor; offset_x++)
  811. {
  812. /* save the information of blank */
  813. qrcode_buf[2 * (enlargement_factor * width + offset_x + offset_y * qrcode.size * enlargement_factor)] = BACK_COLOR >> 8;
  814. qrcode_buf[2 * (enlargement_factor * width + offset_x + offset_y * qrcode.size * enlargement_factor) + 1] = BACK_COLOR;
  815. }
  816. }
  817. }
  818. }
  819. /* display a line of qrcode */
  820. lcd_show_image(x, y + high * enlargement_factor, qrcode.size * enlargement_factor, enlargement_factor, qrcode_buf);
  821. }
  822. }
  823. /**
  824. * display the qrcode on the lcd.
  825. * size = (4 * version +17) * enlargement
  826. *
  827. * @param x x position
  828. * @param y y position
  829. * @param version version of qrcode
  830. * @param ecc level of error correction
  831. * @param data string
  832. * @param enlargement enlargement_factor
  833. *
  834. * @return 0: display success
  835. * -1: generate qrcode failed
  836. * -5: memory low
  837. */
  838. rt_err_t lcd_show_qrcode(rt_uint16_t x, rt_uint16_t y, rt_uint8_t version, rt_uint8_t ecc, const char *data, rt_uint8_t enlargement)
  839. {
  840. RT_ASSERT(data);
  841. rt_int8_t result = 0;
  842. rt_uint8_t enlargement_factor = 1;
  843. rt_uint8_t *qrcode_buf = RT_NULL;
  844. if (x + version * 4 + 17 > LCD_W || y + version * 4 + 17 > LCD_H)
  845. {
  846. LOG_E("The qrcode is too big!");
  847. return -RT_ERROR;
  848. }
  849. rt_uint8_t *qrcodeBytes = (rt_uint8_t *)rt_calloc(1, qrcode_getBufferSize(version));
  850. if (qrcodeBytes == RT_NULL)
  851. {
  852. LOG_E("no memory for qrcode!");
  853. return -RT_ENOMEM;
  854. }
  855. /* generate qrcode */
  856. result = qrcode_initText(&qrcode, qrcodeBytes, version, ecc, data);
  857. if (result >= 0)
  858. {
  859. /* set enlargement factor */
  860. if(enlargement == 0)
  861. {
  862. enlargement_factor = get_enlargement_factor(x, y, qrcode.size);
  863. }
  864. else
  865. {
  866. enlargement_factor = enlargement;
  867. }
  868. /* malloc memory for quick display of qrcode */
  869. qrcode_buf = rt_malloc(qrcode.size * 2 * enlargement_factor * enlargement_factor);
  870. if (qrcode_buf == RT_NULL)
  871. {
  872. /* clear lcd */
  873. lcd_fill(x, y, x + qrcode.size, y + qrcode.size, BACK_COLOR);
  874. /* draw point to display qrcode */
  875. show_qrcode_by_point(x, y, qrcode.size, enlargement_factor);
  876. }
  877. else
  878. {
  879. /* quick display of qrcode */
  880. show_qrcode_by_line(x, y, qrcode.size, enlargement_factor,qrcode_buf);
  881. }
  882. result = RT_EOK;
  883. }
  884. else
  885. {
  886. LOG_E("QRCODE(%s) generate falied(%d)\n", data, result);
  887. result = -RT_ENOMEM;
  888. goto __exit;
  889. }
  890. __exit:
  891. if (qrcodeBytes)
  892. {
  893. rt_free(qrcodeBytes);
  894. }
  895. if (qrcode_buf)
  896. {
  897. rt_free(qrcode_buf);
  898. }
  899. return result;
  900. }
  901. #endif