luat_lcd.c 15 KB

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