luat_lcd.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. #include "luat_base.h"
  2. #include "luat_lcd.h"
  3. #include "luat_gpio.h"
  4. #include "luat_spi.h"
  5. #include "luat_malloc.h"
  6. #include "luat_timer.h"
  7. #define LUAT_LOG_TAG "lcd"
  8. #include "luat_log.h"
  9. uint32_t BACK_COLOR = WHITE, FORE_COLOR = BLACK;
  10. #define LUAT_LCD_CONF_COUNT (2)
  11. static luat_lcd_conf_t* confs[LUAT_LCD_CONF_COUNT] = {0};
  12. void luat_lcd_execute_cmds(luat_lcd_conf_t* conf, uint32_t* cmds, uint32_t count) {
  13. uint32_t cmd = 0;
  14. for (size_t i = 0; i < count; i++)
  15. {
  16. cmd = cmds[i];
  17. switch(((cmd >> 16) & 0xFFFF)) {
  18. case 0x0000 :
  19. lcd_write_cmd(conf, (const uint8_t)(cmd & 0xFF));
  20. break;
  21. case 0x0001 :
  22. luat_timer_mdelay(cmd & 0xFF);
  23. break;
  24. case 0x0002 :
  25. lcd_write_cmd(conf, (const uint8_t)(cmd & 0xFF));
  26. break;
  27. case 0x0003 :
  28. lcd_write_data(conf, (const uint8_t)(cmd & 0xFF));
  29. break;
  30. default:
  31. break;
  32. }
  33. }
  34. }
  35. int lcd_write_cmd(luat_lcd_conf_t* conf, const uint8_t cmd){
  36. size_t len;
  37. luat_gpio_set(conf->pin_dc, Luat_GPIO_LOW);
  38. #ifdef LUAT_LCD_CMD_DELAY_US
  39. if (conf->dc_delay_us){
  40. luat_timer_us_delay(conf->dc_delay_us);
  41. }
  42. #endif
  43. if (conf->port == LUAT_LCD_SPI_DEVICE){
  44. len = luat_spi_device_send((luat_spi_device_t*)(conf->lcd_spi_device), (const char*)&cmd, 1);
  45. }else{
  46. len = luat_spi_send(conf->port, (const char*)&cmd, 1);
  47. }
  48. luat_gpio_set(conf->pin_dc, Luat_GPIO_HIGH);
  49. if (len != 1){
  50. LLOGI("lcd_write_cmd error. %d", len);
  51. return -1;
  52. }else{
  53. #ifdef LUAT_LCD_CMD_DELAY_US
  54. luat_timer_us_delay(LUAT_LCD_CMD_DELAY_US);
  55. #endif
  56. return 0;
  57. }
  58. }
  59. int lcd_write_data(luat_lcd_conf_t* conf, const uint8_t data){
  60. size_t len;
  61. if (conf->port == LUAT_LCD_SPI_DEVICE){
  62. len = luat_spi_device_send((luat_spi_device_t*)(conf->lcd_spi_device), (const char*)&data, 1);
  63. }else{
  64. len = luat_spi_send(conf->port, (const char*)&data, 1);
  65. }
  66. if (len != 1){
  67. LLOGI("lcd_write_data error. %d", len);
  68. return -1;
  69. }else{
  70. return 0;
  71. }
  72. }
  73. int lcd_write_half_word(luat_lcd_conf_t* conf, const uint32_t da){
  74. size_t len = 0;
  75. char data[2] = {0};
  76. data[0] = da >> 8;
  77. data[1] = da;
  78. if (conf->port == LUAT_LCD_SPI_DEVICE){
  79. len = luat_spi_device_send((luat_spi_device_t*)(conf->lcd_spi_device), (const char*)data, 2);
  80. }else{
  81. len = luat_spi_send(conf->port, (const char*)data, 2);
  82. }
  83. if (len != 2){
  84. LLOGI("lcd_write_half_word error. %d", len);
  85. return -1;
  86. }else{
  87. return 0;
  88. }
  89. }
  90. luat_lcd_conf_t* luat_lcd_get_default(void) {
  91. for (size_t i = 0; i < LUAT_LCD_CONF_COUNT; i++){
  92. if (confs[i] != NULL) {
  93. return confs[i];
  94. }
  95. }
  96. return NULL;
  97. }
  98. const char* luat_lcd_name(luat_lcd_conf_t* conf) {
  99. return conf->opts->name;
  100. }
  101. int luat_lcd_init(luat_lcd_conf_t* conf) {
  102. int ret = conf->opts->init(conf);
  103. if (ret == 0) {
  104. for (size_t i = 0; i < LUAT_LCD_CONF_COUNT; i++)
  105. {
  106. if (confs[i] == NULL) {
  107. confs[i] = conf;
  108. break;
  109. }
  110. }
  111. }
  112. return ret;
  113. }
  114. int luat_lcd_close(luat_lcd_conf_t* conf) {
  115. if (conf->pin_pwr != 255)
  116. luat_gpio_set(conf->pin_pwr, Luat_GPIO_LOW);
  117. return 0;
  118. }
  119. int luat_lcd_display_off(luat_lcd_conf_t* conf) {
  120. if (conf->pin_pwr != 255)
  121. luat_gpio_set(conf->pin_pwr, Luat_GPIO_LOW);
  122. lcd_write_cmd(conf,0x28);
  123. return 0;
  124. }
  125. int luat_lcd_display_on(luat_lcd_conf_t* conf) {
  126. if (conf->pin_pwr != 255)
  127. luat_gpio_set(conf->pin_pwr, Luat_GPIO_HIGH);
  128. lcd_write_cmd(conf,0x29);
  129. return 0;
  130. }
  131. int luat_lcd_sleep(luat_lcd_conf_t* conf) {
  132. if (conf->pin_pwr != 255)
  133. luat_gpio_set(conf->pin_pwr, Luat_GPIO_LOW);
  134. luat_timer_mdelay(5);
  135. lcd_write_cmd(conf,0x10);
  136. return 0;
  137. }
  138. int luat_lcd_wakeup(luat_lcd_conf_t* conf) {
  139. if (conf->pin_pwr != 255)
  140. luat_gpio_set(conf->pin_pwr, Luat_GPIO_HIGH);
  141. luat_timer_mdelay(5);
  142. lcd_write_cmd(conf,0x11);
  143. return 0;
  144. }
  145. int luat_lcd_inv_off(luat_lcd_conf_t* conf) {
  146. lcd_write_cmd(conf,0x20);
  147. return 0;
  148. }
  149. int luat_lcd_inv_on(luat_lcd_conf_t* conf) {
  150. lcd_write_cmd(conf,0x21);
  151. return 0;
  152. }
  153. int luat_lcd_set_address(luat_lcd_conf_t* conf,uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2) {
  154. lcd_write_cmd(conf,0x2a);
  155. lcd_write_data(conf,(x1+conf->xoffset)>>8);
  156. lcd_write_data(conf,x1+conf->xoffset);
  157. lcd_write_data(conf,(x2+conf->xoffset)>>8);
  158. lcd_write_data(conf,x2+conf->xoffset);
  159. lcd_write_cmd(conf,0x2b);
  160. lcd_write_data(conf,(y1+conf->yoffset)>>8);
  161. lcd_write_data(conf,y1+conf->yoffset);
  162. lcd_write_data(conf,(y2+conf->yoffset)>>8);
  163. lcd_write_data(conf,y2+conf->yoffset);
  164. lcd_write_cmd(conf,0x2C);
  165. return 0;
  166. }
  167. int luat_lcd_set_color(uint32_t back, uint32_t fore){
  168. BACK_COLOR = back;
  169. FORE_COLOR = fore;
  170. return 0;
  171. }
  172. int luat_lcd_draw(luat_lcd_conf_t* conf, uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2, luat_color_t* color) {
  173. uint32_t size = (x2 - x1 + 1) * (y2 - y1 + 1) * 2;
  174. luat_lcd_set_address(conf,x1, y1, x2, y2);
  175. luat_gpio_set(conf->pin_dc, Luat_GPIO_HIGH);
  176. if (conf->port == LUAT_LCD_SPI_DEVICE){
  177. luat_spi_device_send((luat_spi_device_t*)(conf->lcd_spi_device), (const char*)color, size);
  178. }else{
  179. luat_spi_send(conf->port, (const char*)color, size);
  180. }
  181. return 0;
  182. }
  183. int luat_lcd_clear(luat_lcd_conf_t* conf,uint32_t color){
  184. uint16_t i, j;
  185. uint8_t data[2] = {0};
  186. uint8_t *buf = NULL;
  187. data[0] = color >> 8;
  188. data[1] = color;
  189. luat_lcd_set_address(conf,0, 0, conf->w - 1, conf->h - 1);
  190. buf = luat_heap_malloc(conf->w*conf->h/10);
  191. if (buf)
  192. {
  193. for (j = 0; j < conf->w*conf->h/10 / 2; j++)
  194. {
  195. buf[j * 2] = data[0];
  196. buf[j * 2 + 1] = data[1];
  197. }
  198. luat_gpio_set(conf->pin_dc, Luat_GPIO_HIGH);
  199. for (i = 0; i < 20; i++)
  200. {
  201. if (conf->port == LUAT_LCD_SPI_DEVICE){
  202. luat_spi_device_send((luat_spi_device_t*)(conf->lcd_spi_device), (const char*)buf, conf->w*conf->h/10);
  203. }else{
  204. luat_spi_send(conf->port, (const char*)buf, conf->w*conf->h/10);
  205. }
  206. }
  207. luat_heap_free(buf);
  208. }
  209. else
  210. {
  211. luat_gpio_set(conf->pin_dc, Luat_GPIO_HIGH);
  212. for (i = 0; i < conf->w; i++)
  213. {
  214. for (j = 0; j < conf->h; j++)
  215. {
  216. if (conf->port == LUAT_LCD_SPI_DEVICE){
  217. luat_spi_device_send((luat_spi_device_t*)(conf->lcd_spi_device), (const char*)data, 2);
  218. }else{
  219. luat_spi_send(conf->port, (const char*)data, 2);
  220. }
  221. }
  222. }
  223. }
  224. return 0;
  225. }
  226. int luat_lcd_draw_fill(luat_lcd_conf_t* conf,uint16_t x1,uint16_t y1,uint16_t x2,uint16_t y2,uint32_t color){
  227. uint16_t i,j;
  228. luat_lcd_set_address(conf,x1,y1,x2-1,y2-1);//设置显示范围
  229. for(i=y1;i<y2;i++)
  230. {
  231. for(j=x1;j<x2;j++)
  232. {
  233. lcd_write_half_word(conf,color);
  234. }
  235. }
  236. return 0;
  237. }
  238. int luat_lcd_draw_point(luat_lcd_conf_t* conf, uint16_t x, uint16_t y, uint32_t color) {
  239. luat_lcd_set_address(conf,x, y, x, y);
  240. lcd_write_half_word(conf,color);
  241. return 0;
  242. }
  243. int luat_lcd_draw_vline(luat_lcd_conf_t* conf, uint16_t x, uint16_t y,uint16_t h, uint32_t color) {
  244. luat_lcd_set_address(conf,x, y, x, y + h - 1);
  245. while ( h-- ) {lcd_write_half_word(conf,color);}
  246. return 0;
  247. }
  248. int luat_lcd_draw_hline(luat_lcd_conf_t* conf, uint16_t x, uint16_t y,uint16_t w, uint32_t color) {
  249. luat_lcd_set_address(conf,x, y, x + w - 1, y);
  250. while ( w-- ) {lcd_write_half_word(conf,color);}
  251. return 0;
  252. }
  253. int luat_lcd_draw_line(luat_lcd_conf_t* conf,uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2,uint32_t color){
  254. uint16_t t;
  255. uint32_t i = 0;
  256. int xerr = 0, yerr = 0, delta_x, delta_y, distance;
  257. int incx, incy, row, col;
  258. if (x1 == x2 || y1 == y2)
  259. {
  260. /* fast draw transverse line */
  261. luat_lcd_set_address(conf,x1, y1, x2, y2);
  262. size_t dots = (x2 - x1 + 1) * (y2 - y1 + 1);//点数量
  263. uint8_t* line_buf = (uint8_t*)luat_heap_malloc(dots * 2);
  264. for (i = 0; i < dots; i++)
  265. {
  266. line_buf[2 * i] = color >> 8;
  267. line_buf[2 * i + 1] = color;
  268. }
  269. luat_gpio_set(conf->pin_dc, Luat_GPIO_HIGH);
  270. if (conf->port == LUAT_LCD_SPI_DEVICE){
  271. luat_spi_device_send((luat_spi_device_t*)(conf->lcd_spi_device), (const char*)line_buf, dots * 2);
  272. }else{
  273. luat_spi_send(conf->port, (const char*)line_buf, dots * 2);
  274. }
  275. luat_heap_free(line_buf);
  276. return 0;
  277. }
  278. delta_x = x2 - x1;
  279. delta_y = y2 - y1;
  280. row = x1;
  281. col = y1;
  282. if (delta_x > 0)incx = 1;
  283. else if (delta_x == 0)incx = 0;
  284. else
  285. {
  286. incx = -1;
  287. delta_x = -delta_x;
  288. }
  289. if (delta_y > 0)incy = 1;
  290. else if (delta_y == 0)incy = 0;
  291. else
  292. {
  293. incy = -1;
  294. delta_y = -delta_y;
  295. }
  296. if (delta_x > delta_y)distance = delta_x;
  297. else distance = delta_y;
  298. for (t = 0; t <= distance + 1; t++)
  299. {
  300. luat_lcd_draw_point(conf,row, col,color);
  301. xerr += delta_x ;
  302. yerr += delta_y ;
  303. if (xerr > distance)
  304. {
  305. xerr -= distance;
  306. row += incx;
  307. }
  308. if (yerr > distance)
  309. {
  310. yerr -= distance;
  311. col += incy;
  312. }
  313. }
  314. return 0;
  315. }
  316. int luat_lcd_draw_rectangle(luat_lcd_conf_t* conf,uint16_t x1, uint16_t y1, uint16_t x2, uint16_t y2,uint32_t color){
  317. luat_lcd_draw_line(conf,x1, y1, x2, y1,color);
  318. luat_lcd_draw_line(conf,x1, y1, x1, y2,color);
  319. luat_lcd_draw_line(conf,x1, y2, x2, y2,color);
  320. luat_lcd_draw_line(conf,x2, y1, x2, y2,color);
  321. return 0;
  322. }
  323. int luat_lcd_draw_circle(luat_lcd_conf_t* conf,uint16_t x0, uint16_t y0, uint8_t r,uint32_t color){
  324. int a, b;
  325. int di;
  326. a = 0;
  327. b = r;
  328. di = 3 - (r << 1);
  329. while (a <= b)
  330. {
  331. luat_lcd_draw_point(conf,x0 - b, y0 - a,color);
  332. luat_lcd_draw_point(conf,x0 + b, y0 - a,color);
  333. luat_lcd_draw_point(conf,x0 - a, y0 + b,color);
  334. luat_lcd_draw_point(conf,x0 - b, y0 - a,color);
  335. luat_lcd_draw_point(conf,x0 - a, y0 - b,color);
  336. luat_lcd_draw_point(conf,x0 + b, y0 + a,color);
  337. luat_lcd_draw_point(conf,x0 + a, y0 - b,color);
  338. luat_lcd_draw_point(conf,x0 + a, y0 + b,color);
  339. luat_lcd_draw_point(conf,x0 - b, y0 + a,color);
  340. a++;
  341. //Bresenham
  342. if (di < 0)di += 4 * a + 6;
  343. else
  344. {
  345. di += 10 + 4 * (a - b);
  346. b--;
  347. }
  348. luat_lcd_draw_point(conf,x0 + a, y0 + b,color);
  349. }
  350. return 0;
  351. }