luat_lcd.c 16 KB

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