luat_lcd.c 10 KB

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