luat_lcd.c 11 KB

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