luat_lcd.c 9.7 KB

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