luat_lcd.c 8.2 KB

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