luat_lcd.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. #include "luat_base.h"
  2. #include "luat_lcd.h"
  3. #include "luat_gpio.h"
  4. #include "luat_spi.h"
  5. #include "luat_mem.h"
  6. #include "luat_rtos.h"
  7. #define LUAT_LOG_TAG "lcd"
  8. #include "luat_log.h"
  9. luat_color_t BACK_COLOR = LCD_WHITE, FORE_COLOR = LCD_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) {
  17. uint16_t cmd = 0,cmd_len = 0;
  18. uint8_t cmd_send = 0;
  19. uint8_t cmds[32]={0};
  20. for (size_t i = 0; i < conf->opts->init_cmds_len; i++){
  21. cmd = conf->opts->init_cmds[i];
  22. switch(((cmd >> 8) & 0xFF)) {
  23. case 0x0000:
  24. case 0x0002:
  25. if (i!=0){
  26. if (cmd_len){
  27. lcd_write_cmd_data(conf,cmd_send, cmds, cmd_len);
  28. }else{
  29. lcd_write_cmd_data(conf,cmd_send, NULL, 0);
  30. }
  31. }
  32. cmd_send = (uint8_t)(cmd & 0xFF);
  33. cmd_len = 0;
  34. break;
  35. case 0x0001:
  36. luat_rtos_task_sleep(cmd & 0xFF);
  37. break;
  38. case 0x0003:
  39. cmds[cmd_len]= (uint8_t)(cmd & 0xFF);
  40. cmd_len++;
  41. break;
  42. default:
  43. break;
  44. }
  45. if (i==conf->opts->init_cmds_len-1){
  46. if (cmd_len){
  47. lcd_write_cmd_data(conf,cmd_send, cmds, cmd_len);
  48. }else{
  49. lcd_write_cmd_data(conf,cmd_send, NULL, 0);
  50. }
  51. }
  52. }
  53. }
  54. int lcd_write_data(luat_lcd_conf_t* conf, const uint8_t data){
  55. size_t len;
  56. if (conf->port == LUAT_LCD_SPI_DEVICE){
  57. len = luat_spi_device_send((luat_spi_device_t*)(conf->lcd_spi_device), (const char*)&data, 1);
  58. }else{
  59. len = luat_spi_send(conf->port, (const char*)&data, 1);
  60. }
  61. if (len != 1){
  62. LLOGI("lcd_write_data error. %d", len);
  63. return -1;
  64. }else{
  65. return 0;
  66. }
  67. }
  68. int lcd_write_cmd_data(luat_lcd_conf_t* conf,const uint8_t cmd, const uint8_t *data, uint8_t data_len){
  69. if (conf->opts->write_cmd_data){
  70. return conf->opts->write_cmd_data(conf,cmd,data,data_len);
  71. }
  72. size_t len;
  73. if (conf->interface_mode==LUAT_LCD_IM_4_WIRE_8_BIT_INTERFACE_I || conf->interface_mode==LUAT_LCD_IM_4_WIRE_8_BIT_INTERFACE_II){
  74. luat_gpio_set(conf->pin_dc, Luat_GPIO_LOW);
  75. }
  76. #ifdef LUAT_LCD_CMD_DELAY_US
  77. if (conf->dc_delay_us){
  78. luat_timer_us_delay(conf->dc_delay_us);
  79. }
  80. #endif
  81. if (conf->port == LUAT_LCD_SPI_DEVICE){
  82. len = luat_spi_device_send((luat_spi_device_t*)(conf->lcd_spi_device), (const char*)&cmd, 1);
  83. }else{
  84. len = luat_spi_send(conf->port, (const char*)&cmd, 1);
  85. }
  86. if (conf->interface_mode==LUAT_LCD_IM_4_WIRE_8_BIT_INTERFACE_I || conf->interface_mode==LUAT_LCD_IM_4_WIRE_8_BIT_INTERFACE_II){
  87. luat_gpio_set(conf->pin_dc, Luat_GPIO_HIGH);
  88. }
  89. if (len != 1){
  90. LLOGI("lcd_write_cmd error. %d", len);
  91. return -1;
  92. }else{
  93. #ifdef LUAT_LCD_CMD_DELAY_US
  94. if (conf->dc_delay_us){
  95. luat_timer_us_delay(conf->dc_delay_us);
  96. }
  97. #endif
  98. }
  99. if (data_len){
  100. if (conf->port == LUAT_LCD_SPI_DEVICE){
  101. len = luat_spi_device_send((luat_spi_device_t*)(conf->lcd_spi_device), (const char*)data, data_len);
  102. }else{
  103. len = luat_spi_send(conf->port, (const char*)data, data_len);
  104. }
  105. if (len != data_len){
  106. LLOGI("lcd_write_data error. %d", len);
  107. return -1;
  108. }
  109. }
  110. return 0;
  111. }
  112. luat_lcd_conf_t* luat_lcd_get_default(void) {
  113. for (size_t i = 0; i < LUAT_LCD_CONF_COUNT; i++){
  114. if (confs[i] != NULL) {
  115. return confs[i];
  116. }
  117. }
  118. return NULL;
  119. }
  120. const char* luat_lcd_name(luat_lcd_conf_t* conf) {
  121. return conf->opts->name;
  122. }
  123. int luat_lcd_init(luat_lcd_conf_t* conf) {
  124. uint8_t direction_date = 0;
  125. conf->is_init_done = 0;
  126. if (conf->w == 0)
  127. conf->w = LCD_W;
  128. if (conf->h == 0)
  129. conf->h = LCD_H;
  130. if (conf->pin_pwr != LUAT_GPIO_NONE)
  131. luat_gpio_mode(conf->pin_pwr, Luat_GPIO_OUTPUT, Luat_GPIO_DEFAULT, Luat_GPIO_LOW); // POWER
  132. if (conf->interface_mode==LUAT_LCD_IM_4_WIRE_8_BIT_INTERFACE_I || conf->interface_mode==LUAT_LCD_IM_4_WIRE_8_BIT_INTERFACE_II){
  133. if (conf->pin_dc != LUAT_GPIO_NONE) luat_gpio_mode(conf->pin_dc, Luat_GPIO_OUTPUT, Luat_GPIO_DEFAULT, Luat_GPIO_HIGH); // DC
  134. }
  135. luat_gpio_mode(conf->pin_rst, Luat_GPIO_OUTPUT, Luat_GPIO_DEFAULT, Luat_GPIO_LOW); // RST
  136. if (conf->pin_pwr != LUAT_GPIO_NONE)
  137. luat_gpio_set(conf->pin_pwr, Luat_GPIO_LOW);
  138. luat_gpio_set(conf->pin_rst, Luat_GPIO_LOW);
  139. luat_rtos_task_sleep(100);
  140. luat_gpio_set(conf->pin_rst, Luat_GPIO_HIGH);
  141. luat_rtos_task_sleep(120);
  142. luat_lcd_wakeup(conf);
  143. luat_rtos_task_sleep(120);
  144. // 发送初始化命令
  145. if (conf->opts->init){
  146. conf->opts->init(conf);
  147. }else{
  148. luat_lcd_execute_cmds(conf);
  149. if(strcmp(conf->opts->name,"custom") == 0){
  150. luat_heap_free(conf->opts->init_cmds);
  151. }
  152. luat_lcd_set_direction(conf,conf->direction);
  153. }
  154. luat_lcd_wakeup(conf);
  155. /* wait for power stability */
  156. luat_rtos_task_sleep(100);
  157. luat_lcd_clear(conf,LCD_BLACK);
  158. /* display on */
  159. luat_lcd_display_on(conf);
  160. conf->is_init_done = 1;
  161. for (size_t i = 0; i < LUAT_LCD_CONF_COUNT; i++){
  162. if (confs[i] == NULL) {
  163. confs[i] = conf;
  164. return 0;
  165. }
  166. }
  167. return -1;
  168. }
  169. int luat_lcd_close(luat_lcd_conf_t* conf) {
  170. if (conf->pin_pwr != 255)
  171. luat_gpio_set(conf->pin_pwr, Luat_GPIO_LOW);
  172. return 0;
  173. }
  174. int luat_lcd_display_off(luat_lcd_conf_t* conf) {
  175. if (conf->pin_pwr != 255)
  176. luat_gpio_set(conf->pin_pwr, Luat_GPIO_LOW);
  177. lcd_write_cmd_data(conf,0x28, NULL, 0);
  178. return 0;
  179. }
  180. int luat_lcd_display_on(luat_lcd_conf_t* conf) {
  181. if (conf->pin_pwr != 255)
  182. luat_gpio_set(conf->pin_pwr, Luat_GPIO_HIGH);
  183. lcd_write_cmd_data(conf,0x29, NULL, 0);
  184. return 0;
  185. }
  186. int luat_lcd_sleep(luat_lcd_conf_t* conf) {
  187. if (conf->pin_pwr != 255)
  188. luat_gpio_set(conf->pin_pwr, Luat_GPIO_LOW);
  189. luat_rtos_task_sleep(5);
  190. lcd_write_cmd_data(conf,conf->opts->sleep_cmd?conf->opts->sleep_cmd:LUAT_LCD_DEFAULT_SLEEP, NULL, 0);
  191. return 0;
  192. }
  193. int luat_lcd_wakeup(luat_lcd_conf_t* conf) {
  194. if (conf->pin_pwr != 255)
  195. luat_gpio_set(conf->pin_pwr, Luat_GPIO_HIGH);
  196. luat_rtos_task_sleep(5);
  197. lcd_write_cmd_data(conf,conf->opts->wakeup_cmd?conf->opts->wakeup_cmd:LUAT_LCD_DEFAULT_WAKEUP, NULL, 0);
  198. return 0;
  199. }
  200. int luat_lcd_inv_off(luat_lcd_conf_t* conf) {
  201. lcd_write_cmd_data(conf,0x20, NULL, 0);
  202. return 0;
  203. }
  204. int luat_lcd_inv_on(luat_lcd_conf_t* conf) {
  205. lcd_write_cmd_data(conf,0x21, NULL, 0);
  206. return 0;
  207. }
  208. int luat_lcd_set_address(luat_lcd_conf_t* conf,int16_t x1, int16_t y1, int16_t x2, int16_t y2) {
  209. uint8_t data_x[] = {(x1+conf->xoffset)>>8,x1+conf->xoffset,(x2+conf->xoffset)>>8,x2+conf->xoffset};
  210. lcd_write_cmd_data(conf,0x2a, data_x, 4);
  211. uint8_t data_y[] = {(y1+conf->yoffset)>>8,y1+conf->yoffset,(y2+conf->yoffset)>>8,y2+conf->yoffset};
  212. lcd_write_cmd_data(conf,0x2b, data_y, 4);
  213. lcd_write_cmd_data(conf,0x2c, NULL, 0);
  214. return 0;
  215. }
  216. int luat_lcd_set_color(luat_color_t back, luat_color_t fore){
  217. BACK_COLOR = back;
  218. FORE_COLOR = fore;
  219. return 0;
  220. }
  221. int luat_lcd_set_direction(luat_lcd_conf_t* conf, uint8_t direction){
  222. uint8_t direction_date = 0;
  223. if(direction==0) direction_date = conf->opts->direction0;
  224. else if(direction==1) direction_date = conf->opts->direction90;
  225. else if(direction==2) direction_date = conf->opts->direction180;
  226. else direction_date = conf->opts->direction270;
  227. lcd_write_cmd_data(conf,0x36, &direction_date, 1);
  228. return 0;
  229. }
  230. #ifndef LUAT_USE_LCD_CUSTOM_DRAW
  231. int luat_lcd_flush(luat_lcd_conf_t* conf) {
  232. if (conf->buff == NULL) {
  233. return 0;
  234. }
  235. //LLOGD("luat_lcd_flush range %d %d", conf->flush_y_min, conf->flush_y_max);
  236. if (conf->flush_y_max < conf->flush_y_min) {
  237. // 没有需要刷新的内容,直接跳过
  238. //LLOGD("luat_lcd_flush no need");
  239. return 0;
  240. }
  241. uint32_t size = conf->w * (conf->flush_y_max - conf->flush_y_min + 1) * 2;
  242. luat_lcd_set_address(conf, 0, conf->flush_y_min, conf->w - 1, conf->flush_y_max);
  243. const char* tmp = (const char*)(conf->buff + conf->flush_y_min * conf->w);
  244. if (conf->port == LUAT_LCD_SPI_DEVICE){
  245. luat_spi_device_send((luat_spi_device_t*)(conf->lcd_spi_device), tmp, size);
  246. }else{
  247. luat_spi_send(conf->port, tmp, size);
  248. }
  249. // 重置为不需要刷新的状态
  250. conf->flush_y_max = 0;
  251. conf->flush_y_min = conf->h;
  252. return 0;
  253. }
  254. int luat_lcd_draw(luat_lcd_conf_t* conf, int16_t x1, int16_t y1, int16_t x2, int16_t y2, luat_color_t* color) {
  255. if (x1 >= conf->w || y1 >= conf->h || x2 < 0 || y2 < 0 || x2 < x1 || y2 < y1) {
  256. // LLOGE("out of lcd buff range %d %d %d %d", x1, y1, x2, y2);
  257. // LLOGE("out of lcd buff range %d %d %d %d %d", x1 >= conf->w, y1 >= conf->h, y2 < 0, x2 < x1, y2 < y1);
  258. return 0;
  259. }
  260. if (y2 >= conf->h) {
  261. y2 = conf->h - 1;
  262. }
  263. if (conf->opts->lcd_draw)
  264. return conf->opts->lcd_draw(conf, x1, y1, x2, y2, color);
  265. // 直接刷屏模式
  266. if (conf->buff == NULL) {
  267. // 常规数据, 整体传输
  268. if (x1 >= 0 && y1 >= 0 && x2 <= conf->w && y2 <= conf->h) {
  269. uint32_t size = (x2 - x1 + 1) * (y2 - y1 + 1);
  270. // LLOGD("draw %dx%d %dx%d %d", x1, y1, x2, y2, size);
  271. luat_lcd_set_address(conf, x1, y1, x2, y2);
  272. if (conf->port == LUAT_LCD_SPI_DEVICE){
  273. luat_spi_device_send((luat_spi_device_t*)(conf->lcd_spi_device), (const char*)color, size* sizeof(luat_color_t));
  274. }else{
  275. luat_spi_send(conf->port, (const char*)color, size * sizeof(luat_color_t));
  276. }
  277. }
  278. // 超出边界的数据, 按行传输
  279. else {
  280. int line_size = (x2 - x1 + 1);
  281. // LLOGD("want draw %dx%d %dx%d %d", x1, y1, x2, y2, line_size);
  282. luat_color_t* ptr = (luat_color_t*)color;
  283. for (int i = y1; i <= y2; i++)
  284. {
  285. if (i < 0) {
  286. ptr += line_size;
  287. continue;
  288. }
  289. luat_color_t* line = ptr;
  290. int lsize = line_size;
  291. int tmp_x1 = x1;
  292. int tmp_x2 = x2;
  293. if (x1 < 0) {
  294. line += ( - x1);
  295. lsize += (x1);
  296. tmp_x1 = 0;
  297. }
  298. if (x2 > conf->w) {
  299. lsize -= (x2 - conf->w);
  300. tmp_x2 = conf->w;
  301. }
  302. // LLOGD("action draw %dx%d %dx%d %d", tmp_x1, i, tmp_x2, i, lsize);
  303. luat_lcd_set_address(conf, tmp_x1, i, tmp_x2, i);
  304. if (conf->port == LUAT_LCD_SPI_DEVICE){
  305. luat_spi_device_send((luat_spi_device_t*)(conf->lcd_spi_device), (const char*)line, lsize * sizeof(luat_color_t));
  306. }else{
  307. luat_spi_send(conf->port, (const char*)line, lsize * sizeof(luat_color_t));
  308. }
  309. ptr += line_size;
  310. }
  311. // TODO
  312. // LLOGD("超出边界,特殊处理");
  313. }
  314. return 0;
  315. }
  316. // buff模式
  317. int16_t x_end = x2 >= conf->w? (conf->w - 1):x2;
  318. luat_color_t* dst = (conf->buff);
  319. size_t lsize = (x2 - x1 + 1);
  320. for (int16_t x = x1; x <= x2; x++)
  321. {
  322. if (x < 0 || x >= conf->w)
  323. continue;
  324. for (int16_t y = y1; y <= y2; y++)
  325. {
  326. if (y < 0 || y >= conf->h)
  327. continue;
  328. memcpy((char*)(dst + (conf->w * y + x)), (char*)(color + (lsize * (y-y1) + (x-x1))), sizeof(luat_color_t));
  329. }
  330. }
  331. // 存储需要刷新的区域
  332. if (y1 < conf->flush_y_min) {
  333. if (y1 >= 0)
  334. conf->flush_y_min = y1;
  335. else
  336. conf->flush_y_min = 0;
  337. }
  338. if (y2 > conf->flush_y_max) {
  339. conf->flush_y_max = y2;
  340. }
  341. return 0;
  342. }
  343. #endif
  344. int luat_lcd_draw_point(luat_lcd_conf_t* conf, int16_t x, int16_t y, luat_color_t color) {
  345. // 注意, 这里需要把颜色swap了
  346. luat_color_t tmp = color_swap(color);
  347. return luat_lcd_draw(conf, x, y, x, y, &tmp);
  348. }
  349. int luat_lcd_clear(luat_lcd_conf_t* conf, luat_color_t color){
  350. luat_lcd_draw_fill(conf, 0, 0, conf->w - 1, conf->h, color);
  351. return 0;
  352. }
  353. int luat_lcd_draw_fill(luat_lcd_conf_t* conf,int16_t x1,int16_t y1,int16_t x2,int16_t y2, luat_color_t color) {
  354. int16_t i;
  355. for(i=y1;i<y2;i++)
  356. {
  357. luat_lcd_draw_line(conf, x1, i, x2, i, color);
  358. }
  359. return 0;
  360. }
  361. int luat_lcd_draw_vline(luat_lcd_conf_t* conf, int16_t x, int16_t y,int16_t h, luat_color_t color) {
  362. if (h<=0) return 0;
  363. return luat_lcd_draw_line(conf, x, y, x, y + h - 1, color);
  364. }
  365. int luat_lcd_draw_hline(luat_lcd_conf_t* conf, int16_t x, int16_t y,int16_t w, luat_color_t color) {
  366. if (w<=0) return 0;
  367. return luat_lcd_draw_line(conf, x, y, x + w - 1, y, color);
  368. }
  369. int luat_lcd_draw_line(luat_lcd_conf_t* conf,int16_t x1, int16_t y1, int16_t x2, int16_t y2,luat_color_t color) {
  370. int16_t t;
  371. uint32_t i = 0;
  372. int xerr = 0, yerr = 0, delta_x, delta_y, distance;
  373. int incx, incy, row, col;
  374. if (x1 == x2 || y1 == y2) // 直线
  375. {
  376. size_t dots = (x2 - x1 + 1) * (y2 - y1 + 1);//点数量
  377. luat_color_t* line_buf = (luat_color_t*) luat_heap_malloc(dots * sizeof(luat_color_t));
  378. // 颜色swap
  379. luat_color_t tmp = color_swap(color);
  380. if (line_buf) {
  381. for (i = 0; i < dots; i++)
  382. {
  383. line_buf[i] = tmp;
  384. }
  385. luat_lcd_draw(conf, x1, y1, x2, y2, line_buf);
  386. luat_heap_free(line_buf);
  387. return 0;
  388. }
  389. }
  390. delta_x = x2 - x1;
  391. delta_y = y2 - y1;
  392. row = x1;
  393. col = y1;
  394. if (delta_x > 0)incx = 1;
  395. else if (delta_x == 0)incx = 0;
  396. else
  397. {
  398. incx = -1;
  399. delta_x = -delta_x;
  400. }
  401. if (delta_y > 0)incy = 1;
  402. else if (delta_y == 0)incy = 0;
  403. else
  404. {
  405. incy = -1;
  406. delta_y = -delta_y;
  407. }
  408. if (delta_x > delta_y)distance = delta_x;
  409. else distance = delta_y;
  410. for (t = 0; t <= distance + 1; t++)
  411. {
  412. luat_lcd_draw_point(conf,row, col,color);
  413. xerr += delta_x ;
  414. yerr += delta_y ;
  415. if (xerr > distance)
  416. {
  417. xerr -= distance;
  418. row += incx;
  419. }
  420. if (yerr > distance)
  421. {
  422. yerr -= distance;
  423. col += incy;
  424. }
  425. }
  426. return 0;
  427. }
  428. int luat_lcd_draw_rectangle(luat_lcd_conf_t* conf,int16_t x1, int16_t y1, int16_t x2, int16_t y2, luat_color_t color){
  429. luat_lcd_draw_line(conf,x1, y1, x2, y1, color);
  430. luat_lcd_draw_line(conf,x1, y1, x1, y2, color);
  431. luat_lcd_draw_line(conf,x1, y2, x2, y2, color);
  432. luat_lcd_draw_line(conf,x2, y1, x2, y2, color);
  433. return 0;
  434. }
  435. int luat_lcd_draw_circle(luat_lcd_conf_t* conf,int16_t x0, int16_t y0, uint8_t r, luat_color_t color){
  436. int a, b;
  437. int di;
  438. a = 0;
  439. b = r;
  440. di = 3 - (r << 1);
  441. while (a <= b)
  442. {
  443. luat_lcd_draw_point(conf,x0 - b, y0 - a,color);
  444. luat_lcd_draw_point(conf,x0 + b, y0 - a,color);
  445. luat_lcd_draw_point(conf,x0 - a, y0 + b,color);
  446. luat_lcd_draw_point(conf,x0 - b, y0 - a,color);
  447. luat_lcd_draw_point(conf,x0 - a, y0 - b,color);
  448. luat_lcd_draw_point(conf,x0 + b, y0 + a,color);
  449. luat_lcd_draw_point(conf,x0 + a, y0 - b,color);
  450. luat_lcd_draw_point(conf,x0 + a, y0 + b,color);
  451. luat_lcd_draw_point(conf,x0 - b, y0 + a,color);
  452. a++;
  453. //Bresenham
  454. if (di < 0)di += 4 * a + 6;
  455. else
  456. {
  457. di += 10 + 4 * (a - b);
  458. b--;
  459. }
  460. luat_lcd_draw_point(conf,x0 + a, y0 + b,color);
  461. }
  462. return 0;
  463. }