luat_lcd.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  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_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. LLOGD("interface_mode:%d",conf->interface_mode);
  131. if (conf->pin_pwr != 255)
  132. luat_gpio_mode(conf->pin_pwr, Luat_GPIO_OUTPUT, Luat_GPIO_DEFAULT, Luat_GPIO_LOW); // POWER
  133. 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){
  134. luat_gpio_mode(conf->pin_dc, Luat_GPIO_OUTPUT, Luat_GPIO_DEFAULT, Luat_GPIO_HIGH); // DC
  135. }
  136. luat_gpio_mode(conf->pin_rst, Luat_GPIO_OUTPUT, Luat_GPIO_DEFAULT, Luat_GPIO_LOW); // RST
  137. if (conf->pin_pwr != 255)
  138. luat_gpio_set(conf->pin_pwr, Luat_GPIO_LOW);
  139. luat_gpio_set(conf->pin_rst, Luat_GPIO_LOW);
  140. luat_rtos_task_sleep(100);
  141. luat_gpio_set(conf->pin_rst, Luat_GPIO_HIGH);
  142. luat_rtos_task_sleep(120);
  143. luat_lcd_wakeup(conf);
  144. luat_rtos_task_sleep(120);
  145. // 发送初始化命令
  146. if (conf->opts->init){
  147. conf->opts->init(conf);
  148. }else{
  149. luat_lcd_execute_cmds(conf);
  150. if(conf->direction==0) direction_date = conf->opts->direction0;
  151. else if(conf->direction==1) direction_date = conf->opts->direction90;
  152. else if(conf->direction==2) direction_date = conf->opts->direction180;
  153. else direction_date = conf->opts->direction270;
  154. lcd_write_cmd_data(conf,0x36, &direction_date, 1);
  155. }
  156. luat_lcd_wakeup(conf);
  157. /* wait for power stability */
  158. luat_rtos_task_sleep(100);
  159. luat_lcd_clear(conf,LCD_BLACK);
  160. /* display on */
  161. luat_lcd_display_on(conf);
  162. conf->is_init_done = 1;
  163. for (size_t i = 0; i < LUAT_LCD_CONF_COUNT; i++){
  164. if (confs[i] == NULL) {
  165. confs[i] = conf;
  166. return 0;
  167. }
  168. }
  169. return -1;
  170. }
  171. int luat_lcd_close(luat_lcd_conf_t* conf) {
  172. if (conf->pin_pwr != 255)
  173. luat_gpio_set(conf->pin_pwr, Luat_GPIO_LOW);
  174. return 0;
  175. }
  176. int luat_lcd_display_off(luat_lcd_conf_t* conf) {
  177. if (conf->pin_pwr != 255)
  178. luat_gpio_set(conf->pin_pwr, Luat_GPIO_LOW);
  179. lcd_write_cmd_data(conf,0x28, NULL, 0);
  180. return 0;
  181. }
  182. int luat_lcd_display_on(luat_lcd_conf_t* conf) {
  183. if (conf->pin_pwr != 255)
  184. luat_gpio_set(conf->pin_pwr, Luat_GPIO_HIGH);
  185. lcd_write_cmd_data(conf,0x29, NULL, 0);
  186. return 0;
  187. }
  188. int luat_lcd_sleep(luat_lcd_conf_t* conf) {
  189. if (conf->pin_pwr != 255)
  190. luat_gpio_set(conf->pin_pwr, Luat_GPIO_LOW);
  191. luat_rtos_task_sleep(5);
  192. lcd_write_cmd_data(conf,0x10, NULL, 0);
  193. return 0;
  194. }
  195. int luat_lcd_wakeup(luat_lcd_conf_t* conf) {
  196. if (conf->pin_pwr != 255)
  197. luat_gpio_set(conf->pin_pwr, Luat_GPIO_HIGH);
  198. luat_rtos_task_sleep(5);
  199. lcd_write_cmd_data(conf,0x11, NULL, 0);
  200. return 0;
  201. }
  202. int luat_lcd_inv_off(luat_lcd_conf_t* conf) {
  203. lcd_write_cmd_data(conf,0x20, NULL, 0);
  204. return 0;
  205. }
  206. int luat_lcd_inv_on(luat_lcd_conf_t* conf) {
  207. lcd_write_cmd_data(conf,0x21, NULL, 0);
  208. return 0;
  209. }
  210. int luat_lcd_set_address(luat_lcd_conf_t* conf,int16_t x1, int16_t y1, int16_t x2, int16_t y2) {
  211. uint8_t data_x[] = {(x1+conf->xoffset)>>8,x1+conf->xoffset,(x2+conf->xoffset)>>8,x2+conf->xoffset};
  212. lcd_write_cmd_data(conf,0x2a, data_x, 4);
  213. uint8_t data_y[] = {(y1+conf->yoffset)>>8,y1+conf->yoffset,(y2+conf->yoffset)>>8,y2+conf->yoffset};
  214. lcd_write_cmd_data(conf,0x2b, data_y, 4);
  215. lcd_write_cmd_data(conf,0x2c, NULL, 0);
  216. return 0;
  217. }
  218. int luat_lcd_set_color(luat_color_t back, luat_color_t fore){
  219. BACK_COLOR = back;
  220. FORE_COLOR = fore;
  221. return 0;
  222. }
  223. #ifndef LUAT_USE_LCD_CUSTOM_DRAW
  224. int luat_lcd_flush(luat_lcd_conf_t* conf) {
  225. if (conf->buff == NULL) {
  226. return 0;
  227. }
  228. //LLOGD("luat_lcd_flush range %d %d", conf->flush_y_min, conf->flush_y_max);
  229. if (conf->flush_y_max < conf->flush_y_min) {
  230. // 没有需要刷新的内容,直接跳过
  231. //LLOGD("luat_lcd_flush no need");
  232. return 0;
  233. }
  234. uint32_t size = conf->w * (conf->flush_y_max - conf->flush_y_min + 1) * 2;
  235. luat_lcd_set_address(conf, 0, conf->flush_y_min, conf->w - 1, conf->flush_y_max);
  236. const char* tmp = (const char*)(conf->buff + conf->flush_y_min * conf->w);
  237. if (conf->port == LUAT_LCD_SPI_DEVICE){
  238. luat_spi_device_send((luat_spi_device_t*)(conf->lcd_spi_device), tmp, size);
  239. }else{
  240. luat_spi_send(conf->port, tmp, size);
  241. }
  242. // 重置为不需要刷新的状态
  243. conf->flush_y_max = 0;
  244. conf->flush_y_min = conf->h;
  245. return 0;
  246. }
  247. 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) {
  248. if (x1 >= conf->w || y1 >= conf->h || x2 < 0 || y2 < 0 || x2 < x1 || y2 < y1) {
  249. // LLOGE("out of lcd buff range %d %d %d %d", x1, y1, x2, y2);
  250. // LLOGE("out of lcd buff range %d %d %d %d %d", x1 >= conf->w, y1 >= conf->h, y2 < 0, x2 < x1, y2 < y1);
  251. return 0;
  252. }
  253. if (y2 >= conf->h) {
  254. y2 = conf->h - 1;
  255. }
  256. if (conf->opts->lcd_draw)
  257. return conf->opts->lcd_draw(conf, x1, y1, x2, y2, color);
  258. // 直接刷屏模式
  259. if (conf->buff == NULL) {
  260. // 常规数据, 整体传输
  261. if (x1 >= 0 && y1 >= 0 && x2 <= conf->w && y2 <= conf->h) {
  262. uint32_t size = (x2 - x1 + 1) * (y2 - y1 + 1);
  263. // LLOGD("draw %dx%d %dx%d %d", x1, y1, x2, y2, size);
  264. luat_lcd_set_address(conf, x1, y1, x2, y2);
  265. if (conf->port == LUAT_LCD_SPI_DEVICE){
  266. luat_spi_device_send((luat_spi_device_t*)(conf->lcd_spi_device), (const char*)color, size* sizeof(luat_color_t));
  267. }else{
  268. luat_spi_send(conf->port, (const char*)color, size * sizeof(luat_color_t));
  269. }
  270. }
  271. // 超出边界的数据, 按行传输
  272. else {
  273. int line_size = (x2 - x1 + 1);
  274. // LLOGD("want draw %dx%d %dx%d %d", x1, y1, x2, y2, line_size);
  275. luat_color_t* ptr = (luat_color_t*)color;
  276. for (int i = y1; i <= y2; i++)
  277. {
  278. if (i < 0) {
  279. ptr += line_size;
  280. continue;
  281. }
  282. luat_color_t* line = ptr;
  283. int lsize = line_size;
  284. int tmp_x1 = x1;
  285. int tmp_x2 = x2;
  286. if (x1 < 0) {
  287. line += ( - x1);
  288. lsize += (x1);
  289. tmp_x1 = 0;
  290. }
  291. if (x2 > conf->w) {
  292. lsize -= (x2 - conf->w);
  293. tmp_x2 = conf->w;
  294. }
  295. // LLOGD("action draw %dx%d %dx%d %d", tmp_x1, i, tmp_x2, i, lsize);
  296. luat_lcd_set_address(conf, tmp_x1, i, tmp_x2, i);
  297. if (conf->port == LUAT_LCD_SPI_DEVICE){
  298. luat_spi_device_send((luat_spi_device_t*)(conf->lcd_spi_device), (const char*)line, lsize * sizeof(luat_color_t));
  299. }else{
  300. luat_spi_send(conf->port, (const char*)line, lsize * sizeof(luat_color_t));
  301. }
  302. ptr += line_size;
  303. }
  304. // TODO
  305. // LLOGD("超出边界,特殊处理");
  306. }
  307. return 0;
  308. }
  309. // buff模式
  310. int16_t x_end = x2 >= conf->w? (conf->w - 1):x2;
  311. luat_color_t* dst = (conf->buff);
  312. size_t lsize = (x2 - x1 + 1);
  313. for (int16_t x = x1; x <= x2; x++)
  314. {
  315. if (x < 0 || x >= conf->w)
  316. continue;
  317. for (int16_t y = y1; y <= y2; y++)
  318. {
  319. if (y < 0 || y >= conf->h)
  320. continue;
  321. memcpy((char*)(dst + (conf->w * y + x)), (char*)(color + (lsize * (y-y1) + (x-x1))), sizeof(luat_color_t));
  322. }
  323. }
  324. // 存储需要刷新的区域
  325. if (y1 < conf->flush_y_min) {
  326. if (y1 >= 0)
  327. conf->flush_y_min = y1;
  328. else
  329. conf->flush_y_min = 0;
  330. }
  331. if (y2 > conf->flush_y_max) {
  332. conf->flush_y_max = y2;
  333. }
  334. return 0;
  335. }
  336. #endif
  337. int luat_lcd_draw_point(luat_lcd_conf_t* conf, int16_t x, int16_t y, luat_color_t color) {
  338. // 注意, 这里需要把颜色swap了
  339. luat_color_t tmp = color_swap(color);
  340. return luat_lcd_draw(conf, x, y, x, y, &tmp);
  341. }
  342. int luat_lcd_clear(luat_lcd_conf_t* conf, luat_color_t color){
  343. luat_lcd_draw_fill(conf, 0, 0, conf->w - 1, conf->h, color);
  344. return 0;
  345. }
  346. 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) {
  347. int16_t i;
  348. for(i=y1;i<y2;i++)
  349. {
  350. luat_lcd_draw_line(conf, x1, i, x2, i, color);
  351. }
  352. return 0;
  353. }
  354. int luat_lcd_draw_vline(luat_lcd_conf_t* conf, int16_t x, int16_t y,int16_t h, luat_color_t color) {
  355. if (h<=0) return 0;
  356. return luat_lcd_draw_line(conf, x, y, x, y + h - 1, color);
  357. }
  358. int luat_lcd_draw_hline(luat_lcd_conf_t* conf, int16_t x, int16_t y,int16_t w, luat_color_t color) {
  359. if (w<=0) return 0;
  360. return luat_lcd_draw_line(conf, x, y, x + w - 1, y, color);
  361. }
  362. 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) {
  363. int16_t t;
  364. uint32_t i = 0;
  365. int xerr = 0, yerr = 0, delta_x, delta_y, distance;
  366. int incx, incy, row, col;
  367. if (x1 == x2 || y1 == y2) // 直线
  368. {
  369. size_t dots = (x2 - x1 + 1) * (y2 - y1 + 1);//点数量
  370. luat_color_t* line_buf = (luat_color_t*) luat_heap_malloc(dots * sizeof(luat_color_t));
  371. // 颜色swap
  372. luat_color_t tmp = color_swap(color);
  373. if (line_buf) {
  374. for (i = 0; i < dots; i++)
  375. {
  376. line_buf[i] = tmp;
  377. }
  378. luat_lcd_draw(conf, x1, y1, x2, y2, line_buf);
  379. luat_heap_free(line_buf);
  380. return 0;
  381. }
  382. }
  383. delta_x = x2 - x1;
  384. delta_y = y2 - y1;
  385. row = x1;
  386. col = y1;
  387. if (delta_x > 0)incx = 1;
  388. else if (delta_x == 0)incx = 0;
  389. else
  390. {
  391. incx = -1;
  392. delta_x = -delta_x;
  393. }
  394. if (delta_y > 0)incy = 1;
  395. else if (delta_y == 0)incy = 0;
  396. else
  397. {
  398. incy = -1;
  399. delta_y = -delta_y;
  400. }
  401. if (delta_x > delta_y)distance = delta_x;
  402. else distance = delta_y;
  403. for (t = 0; t <= distance + 1; t++)
  404. {
  405. luat_lcd_draw_point(conf,row, col,color);
  406. xerr += delta_x ;
  407. yerr += delta_y ;
  408. if (xerr > distance)
  409. {
  410. xerr -= distance;
  411. row += incx;
  412. }
  413. if (yerr > distance)
  414. {
  415. yerr -= distance;
  416. col += incy;
  417. }
  418. }
  419. return 0;
  420. }
  421. 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){
  422. luat_lcd_draw_line(conf,x1, y1, x2, y1, color);
  423. luat_lcd_draw_line(conf,x1, y1, x1, y2, color);
  424. luat_lcd_draw_line(conf,x1, y2, x2, y2, color);
  425. luat_lcd_draw_line(conf,x2, y1, x2, y2, color);
  426. return 0;
  427. }
  428. int luat_lcd_draw_circle(luat_lcd_conf_t* conf,int16_t x0, int16_t y0, uint8_t r, luat_color_t color){
  429. int a, b;
  430. int di;
  431. a = 0;
  432. b = r;
  433. di = 3 - (r << 1);
  434. while (a <= b)
  435. {
  436. luat_lcd_draw_point(conf,x0 - b, y0 - a,color);
  437. luat_lcd_draw_point(conf,x0 + b, y0 - a,color);
  438. luat_lcd_draw_point(conf,x0 - a, y0 + b,color);
  439. luat_lcd_draw_point(conf,x0 - b, y0 - a,color);
  440. luat_lcd_draw_point(conf,x0 - a, y0 - b,color);
  441. luat_lcd_draw_point(conf,x0 + b, y0 + a,color);
  442. luat_lcd_draw_point(conf,x0 + a, y0 - b,color);
  443. luat_lcd_draw_point(conf,x0 + a, y0 + b,color);
  444. luat_lcd_draw_point(conf,x0 - b, y0 + a,color);
  445. a++;
  446. //Bresenham
  447. if (di < 0)di += 4 * a + 6;
  448. else
  449. {
  450. di += 10 + 4 * (a - b);
  451. b--;
  452. }
  453. luat_lcd_draw_point(conf,x0 + a, y0 + b,color);
  454. }
  455. return 0;
  456. }