luat_lcd.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690
  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. static luat_lcd_conf_t* lcd_confs[LUAT_LCD_CONF_COUNT] = {0};
  11. void luat_lcd_execute_cmds(luat_lcd_conf_t* conf) {
  12. uint16_t cmd = 0,cmd_len = 0;
  13. uint8_t cmd_send = 0;
  14. uint8_t cmds[32]={0};
  15. for (size_t i = 0; i < conf->opts->init_cmds_len; i++){
  16. cmd = conf->opts->init_cmds[i];
  17. switch(((cmd >> 8) & 0xFF)) {
  18. case 0x0000:
  19. case 0x0002:
  20. if (i!=0){
  21. if (cmd_len){
  22. lcd_write_cmd_data(conf,cmd_send, cmds, cmd_len);
  23. }else{
  24. lcd_write_cmd_data(conf,cmd_send, NULL, 0);
  25. }
  26. }
  27. cmd_send = (uint8_t)(cmd & 0xFF);
  28. cmd_len = 0;
  29. break;
  30. case 0x0001:
  31. luat_rtos_task_sleep(cmd & 0xFF);
  32. break;
  33. case 0x0003:
  34. cmds[cmd_len]= (uint8_t)(cmd & 0xFF);
  35. cmd_len++;
  36. break;
  37. default:
  38. break;
  39. }
  40. if (i==conf->opts->init_cmds_len-1){
  41. if (cmd_len){
  42. lcd_write_cmd_data(conf,cmd_send, cmds, cmd_len);
  43. }else{
  44. lcd_write_cmd_data(conf,cmd_send, NULL, 0);
  45. }
  46. }
  47. }
  48. }
  49. int lcd_write_data(luat_lcd_conf_t* conf, const uint8_t data){
  50. size_t len;
  51. if (conf->port == LUAT_LCD_SPI_DEVICE){
  52. len = luat_spi_device_send((luat_spi_device_t*)(conf->lcd_spi_device), (const char*)&data, 1);
  53. }else{
  54. len = luat_spi_send(conf->port, (const char*)&data, 1);
  55. }
  56. if (len != 1){
  57. LLOGI("lcd_write_data error. %d", len);
  58. return -1;
  59. }else{
  60. return 0;
  61. }
  62. }
  63. int lcd_write_cmd_data(luat_lcd_conf_t* conf,const uint8_t cmd, const uint8_t *data, uint8_t data_len){
  64. if (conf->opts->write_cmd_data){
  65. return conf->opts->write_cmd_data(conf,cmd,data,data_len);
  66. }
  67. size_t len;
  68. 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){
  69. luat_gpio_set(conf->pin_dc, Luat_GPIO_LOW);
  70. }
  71. #ifdef LUAT_LCD_CMD_DELAY_US
  72. if (conf->dc_delay_us){
  73. luat_timer_us_delay(conf->dc_delay_us);
  74. }
  75. #endif
  76. if (conf->port == LUAT_LCD_SPI_DEVICE){
  77. len = luat_spi_device_send((luat_spi_device_t*)(conf->lcd_spi_device), (const char*)&cmd, 1);
  78. }else{
  79. len = luat_spi_send(conf->port, (const char*)&cmd, 1);
  80. }
  81. 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){
  82. luat_gpio_set(conf->pin_dc, Luat_GPIO_HIGH);
  83. }
  84. if (len != 1){
  85. LLOGI("lcd_write_cmd error. %d", len);
  86. return -1;
  87. }else{
  88. #ifdef LUAT_LCD_CMD_DELAY_US
  89. if (conf->dc_delay_us){
  90. luat_timer_us_delay(conf->dc_delay_us);
  91. }
  92. #endif
  93. }
  94. if (data_len){
  95. if (conf->port == LUAT_LCD_SPI_DEVICE){
  96. len = luat_spi_device_send((luat_spi_device_t*)(conf->lcd_spi_device), (const char*)data, data_len);
  97. }else{
  98. len = luat_spi_send(conf->port, (const char*)data, data_len);
  99. }
  100. if (len != data_len){
  101. LLOGI("lcd_write_data error. %d", len);
  102. return -1;
  103. }
  104. }
  105. return 0;
  106. }
  107. luat_lcd_conf_t* luat_lcd_get_default(void) {
  108. for (size_t i = 0; i < LUAT_LCD_CONF_COUNT; i++){
  109. if (lcd_confs[i] != NULL) {
  110. return lcd_confs[i];
  111. }
  112. }
  113. return NULL;
  114. }
  115. int luat_lcd_conf_add(luat_lcd_conf_t* conf) {
  116. for (size_t i = 0; i < LUAT_LCD_CONF_COUNT; i++){
  117. if (lcd_confs[i] == NULL) {
  118. lcd_confs[i] = conf;
  119. return i;
  120. }
  121. }
  122. return -1;
  123. }
  124. const char* luat_lcd_name(luat_lcd_conf_t* conf) {
  125. return conf->opts->name;
  126. }
  127. int luat_lcd_init_default(luat_lcd_conf_t* conf) {
  128. conf->is_init_done = 0;
  129. if (conf->w == 0)
  130. conf->w = LCD_W;
  131. if (conf->h == 0)
  132. conf->h = LCD_H;
  133. if (conf->pin_pwr != LUAT_GPIO_NONE)
  134. luat_gpio_mode(conf->pin_pwr, Luat_GPIO_OUTPUT, Luat_GPIO_DEFAULT, Luat_GPIO_LOW); // POWER
  135. 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){
  136. if (conf->pin_dc != LUAT_GPIO_NONE) {
  137. luat_gpio_mode(conf->pin_dc, Luat_GPIO_OUTPUT, Luat_GPIO_DEFAULT, Luat_GPIO_HIGH); // DC
  138. }
  139. }
  140. luat_gpio_mode(conf->pin_rst, Luat_GPIO_OUTPUT, Luat_GPIO_DEFAULT, Luat_GPIO_LOW); // RST
  141. if (conf->pin_pwr != LUAT_GPIO_NONE) {
  142. luat_gpio_set(conf->pin_pwr, Luat_GPIO_LOW);
  143. }
  144. if (conf->opts->user_ctrl_init) {
  145. int res = conf->opts->user_ctrl_init(conf);
  146. if (res > 0) {
  147. goto INIT_NOT_DONE;
  148. }
  149. goto INIT_DONE;
  150. }
  151. luat_lcd_set_reset_pin_level(conf, Luat_GPIO_LOW);
  152. // luat_gpio_set(conf->pin_rst, Luat_GPIO_LOW);
  153. luat_rtos_task_sleep(100);
  154. luat_lcd_set_reset_pin_level(conf, Luat_GPIO_HIGH);
  155. // luat_gpio_set(conf->pin_rst, Luat_GPIO_HIGH);
  156. luat_rtos_task_sleep(120);
  157. luat_lcd_wakeup(conf);
  158. luat_rtos_task_sleep(120);
  159. // 发送初始化命令
  160. if (conf->opts->init){
  161. conf->opts->init(conf);
  162. // 在SDL2仿真模式下,不再继续发送硬件命令,直接完成初始化
  163. if (conf->opts && conf->opts->name && strcmp(conf->opts->name, "sdl2") == 0) {
  164. goto INIT_DONE;
  165. }
  166. }else{
  167. luat_lcd_execute_cmds(conf);
  168. if(strcmp(conf->opts->name,"custom") == 0){
  169. luat_heap_free(conf->opts->init_cmds);
  170. }
  171. luat_lcd_set_direction(conf,conf->direction);
  172. }
  173. luat_lcd_wakeup(conf);
  174. /* wait for power stability */
  175. luat_rtos_task_sleep(100);
  176. luat_lcd_clear(conf,LCD_BLACK);
  177. /* display on */
  178. luat_lcd_display_on(conf);
  179. INIT_DONE:
  180. conf->is_init_done = 1;
  181. INIT_NOT_DONE:
  182. for (size_t i = 0; i < LUAT_LCD_CONF_COUNT; i++){
  183. if (lcd_confs[i] == NULL) {
  184. lcd_confs[i] = conf;
  185. return 0;
  186. }
  187. }
  188. return -1;
  189. }
  190. int luat_lcd_setup_buff_default(luat_lcd_conf_t* conf){
  191. if (conf->buff) {
  192. LLOGE("lcd buff已经分配过了");
  193. }
  194. else
  195. {
  196. conf->buff = luat_heap_opt_malloc(LUAT_HEAP_PSRAM, sizeof(luat_color_t) * conf->w * conf->h);
  197. if (conf->buff == NULL) {
  198. LLOGW("psram 分配 lcd buff失败, 尝试在sram分配");
  199. conf->buff = luat_heap_opt_malloc(LUAT_HEAP_SRAM, sizeof(luat_color_t) * conf->w * conf->h);
  200. }
  201. if (conf->buff == NULL) {
  202. LLOGE("分配 lcd buff失败");
  203. return -1;
  204. }
  205. }
  206. if (conf->buff_ex) {
  207. LLOGE("lcd buff_ex已经分配过了");
  208. }
  209. else
  210. {
  211. conf->buff_ex = luat_heap_opt_malloc(LUAT_HEAP_PSRAM, sizeof(luat_color_t) * conf->w * conf->h);
  212. if (conf->buff_ex == NULL) {
  213. LLOGW("psram 分配 lcd buff_ex失败, 尝试在sram分配");
  214. conf->buff_ex = luat_heap_opt_malloc(LUAT_HEAP_SRAM, sizeof(luat_color_t) * conf->w * conf->h);
  215. }
  216. if (conf->buff_ex == NULL) {
  217. LLOGE("分配 lcd buff_ex失败");
  218. return -1;
  219. }
  220. }
  221. return 0;
  222. }
  223. LUAT_WEAK int luat_lcd_setup_buff(luat_lcd_conf_t* conf) {
  224. return luat_lcd_setup_buff_default(conf);
  225. }
  226. LUAT_WEAK int luat_lcd_init(luat_lcd_conf_t* conf) {
  227. return luat_lcd_init_default(conf);
  228. }
  229. int luat_lcd_close(luat_lcd_conf_t* conf) {
  230. if (conf->pin_pwr != LUAT_GPIO_NONE)
  231. luat_gpio_set(conf->pin_pwr, Luat_GPIO_LOW);
  232. return 0;
  233. }
  234. int luat_lcd_display_off(luat_lcd_conf_t* conf) {
  235. if (conf && conf->opts && conf->opts->name && strcmp(conf->opts->name, "sdl2") == 0) {
  236. return 0;
  237. }
  238. if (conf->pin_pwr != LUAT_GPIO_NONE) {
  239. luat_gpio_set(conf->pin_pwr, Luat_GPIO_LOW);
  240. }
  241. if (conf->port != LUAT_LCD_PORT_RGB) {
  242. lcd_write_cmd_data(conf,0x28, NULL, 0);
  243. }
  244. return 0;
  245. }
  246. int luat_lcd_display_on(luat_lcd_conf_t* conf) {
  247. if (conf && conf->opts && conf->opts->name && strcmp(conf->opts->name, "sdl2") == 0) {
  248. return 0;
  249. }
  250. if (conf->pin_pwr != LUAT_GPIO_NONE) {
  251. luat_gpio_set(conf->pin_pwr, Luat_GPIO_HIGH);
  252. }
  253. if (conf->port != LUAT_LCD_PORT_RGB) {
  254. lcd_write_cmd_data(conf,0x29, NULL, 0);
  255. }
  256. return 0;
  257. }
  258. int luat_lcd_sleep(luat_lcd_conf_t* conf) {
  259. if (conf->pin_pwr != LUAT_GPIO_NONE)
  260. luat_gpio_set(conf->pin_pwr, Luat_GPIO_LOW);
  261. luat_rtos_task_sleep(5);
  262. if (conf->opts->sleep_ctrl) {
  263. conf->opts->sleep_ctrl(conf, 1);
  264. } else {
  265. lcd_write_cmd_data(conf,conf->opts->sleep_cmd?conf->opts->sleep_cmd:LUAT_LCD_DEFAULT_SLEEP, NULL, 0);
  266. }
  267. return 0;
  268. }
  269. int luat_lcd_wakeup(luat_lcd_conf_t* conf) {
  270. if (conf && conf->opts && conf->opts->name && strcmp(conf->opts->name, "sdl2") == 0) {
  271. return 0;
  272. }
  273. if (conf->pin_pwr != LUAT_GPIO_NONE)
  274. luat_gpio_set(conf->pin_pwr, Luat_GPIO_HIGH);
  275. luat_rtos_task_sleep(5);
  276. if (conf->opts->sleep_ctrl) {
  277. conf->opts->sleep_ctrl(conf, 0);
  278. } else {
  279. lcd_write_cmd_data(conf,conf->opts->wakeup_cmd?conf->opts->wakeup_cmd:LUAT_LCD_DEFAULT_WAKEUP, NULL, 0);
  280. }
  281. return 0;
  282. }
  283. int luat_lcd_inv_off(luat_lcd_conf_t* conf) {
  284. lcd_write_cmd_data(conf,0x20, NULL, 0);
  285. return 0;
  286. }
  287. int luat_lcd_inv_on(luat_lcd_conf_t* conf) {
  288. lcd_write_cmd_data(conf,0x21, NULL, 0);
  289. return 0;
  290. }
  291. int luat_lcd_set_address(luat_lcd_conf_t* conf,int16_t x1, int16_t y1, int16_t x2, int16_t y2) {
  292. uint8_t data_x[] = {(x1+conf->xoffset)>>8,x1+conf->xoffset,(x2+conf->xoffset)>>8,x2+conf->xoffset};
  293. lcd_write_cmd_data(conf,0x2a, data_x, 4);
  294. uint8_t data_y[] = {(y1+conf->yoffset)>>8,y1+conf->yoffset,(y2+conf->yoffset)>>8,y2+conf->yoffset};
  295. lcd_write_cmd_data(conf,0x2b, data_y, 4);
  296. lcd_write_cmd_data(conf,0x2c, NULL, 0);
  297. return 0;
  298. }
  299. int luat_lcd_set_color(luat_color_t back, luat_color_t fore){
  300. BACK_COLOR = back;
  301. FORE_COLOR = fore;
  302. return 0;
  303. }
  304. int luat_lcd_set_direction(luat_lcd_conf_t* conf, uint8_t direction){
  305. uint8_t direction_date = 0;
  306. if(direction==0) direction_date = conf->opts->direction0;
  307. else if(direction==1) direction_date = conf->opts->direction90;
  308. else if(direction==2) direction_date = conf->opts->direction180;
  309. else direction_date = conf->opts->direction270;
  310. lcd_write_cmd_data(conf,0x36, &direction_date, 1);
  311. return 0;
  312. }
  313. #ifndef LUAT_USE_LCD_CUSTOM_DRAW
  314. int luat_lcd_flush_default(luat_lcd_conf_t* conf) {
  315. if (conf->buff == NULL) {
  316. return 0;
  317. }
  318. //LLOGD("luat_lcd_flush range %d %d", conf->flush_y_min, conf->flush_y_max);
  319. if (conf->flush_y_max < conf->flush_y_min) {
  320. // 没有需要刷新的内容,直接跳过
  321. //LLOGD("luat_lcd_flush no need");
  322. return 0;
  323. }
  324. if ((conf->port != LUAT_LCD_SPI_DEVICE) && conf->opts->lcd_draw) {
  325. //LLOGD("luat_lcd_flush user flush");
  326. if (conf->opts->no_ram_mode)
  327. {
  328. conf->opts->lcd_draw(conf, 0, 0, 0, 0, conf->buff);
  329. }
  330. else
  331. {
  332. conf->opts->lcd_draw(conf, 0, conf->flush_y_min, conf->w - 1, conf->flush_y_max, &conf->buff[conf->flush_y_min * conf->w]);
  333. }
  334. } else {
  335. uint32_t size = conf->w * (conf->flush_y_max - conf->flush_y_min + 1) * 2;
  336. luat_lcd_set_address(conf, 0, conf->flush_y_min, conf->w - 1, conf->flush_y_max);
  337. const char* tmp = (const char*)(conf->buff + conf->flush_y_min * conf->w);
  338. if (conf->port == LUAT_LCD_SPI_DEVICE){
  339. luat_spi_device_send((luat_spi_device_t*)(conf->lcd_spi_device), tmp, size);
  340. }else{
  341. luat_spi_send(conf->port, tmp, size);
  342. }
  343. }
  344. // 重置为不需要刷新的状态
  345. conf->flush_y_max = 0;
  346. conf->flush_y_min = conf->h;
  347. return 0;
  348. }
  349. LUAT_WEAK int luat_lcd_flush(luat_lcd_conf_t* conf) {
  350. return luat_lcd_flush_default(conf);
  351. }
  352. int luat_lcd_draw_default(luat_lcd_conf_t* conf, int16_t x1, int16_t y1, int16_t x2, int16_t y2, luat_color_t* color) {
  353. if (x1 >= conf->w || y1 >= conf->h || x2 < 0 || y2 < 0 || x2 < x1 || y2 < y1) {
  354. // LLOGE("out of lcd buff range %d %d %d %d", x1, y1, x2, y2);
  355. // LLOGE("out of lcd buff range %d %d %d %d %d", x1 >= conf->w, y1 >= conf->h, y2 < 0, x2 < x1, y2 < y1);
  356. return 0;
  357. }
  358. if (y2 >= conf->h) {
  359. y2 = conf->h - 1;
  360. }
  361. // 直接刷屏模式
  362. if (conf->buff == NULL) {
  363. // 常规数据, 整体传输
  364. if (x1 >= 0 && y1 >= 0 && x2 <= conf->w && y2 <= conf->h) {
  365. if ((conf->port != LUAT_LCD_SPI_DEVICE) && conf->opts->lcd_draw) {
  366. conf->opts->lcd_draw(conf, x1, y1, x2, y2, color);
  367. } else {
  368. uint32_t size = (x2 - x1 + 1) * (y2 - y1 + 1);
  369. // LLOGD("draw %dx%d %dx%d %d", x1, y1, x2, y2, size);
  370. luat_lcd_set_address(conf, x1, y1, x2, y2);
  371. if (conf->port == LUAT_LCD_SPI_DEVICE){
  372. luat_spi_device_send((luat_spi_device_t*)(conf->lcd_spi_device), (const char*)color, size* sizeof(luat_color_t));
  373. }else{
  374. luat_spi_send(conf->port, (const char*)color, size * sizeof(luat_color_t));
  375. }
  376. }
  377. }
  378. // 超出边界的数据, 按行传输
  379. else {
  380. int line_size = (x2 - x1 + 1);
  381. // LLOGD("want draw %dx%d %dx%d %d", x1, y1, x2, y2, line_size);
  382. luat_color_t* ptr = (luat_color_t*)color;
  383. for (int i = y1; i <= y2; i++)
  384. {
  385. if (i < 0) {
  386. ptr += line_size;
  387. continue;
  388. }
  389. luat_color_t* line = ptr;
  390. int lsize = line_size;
  391. int tmp_x1 = x1;
  392. int tmp_x2 = x2;
  393. if (x1 < 0) {
  394. line += ( - x1);
  395. lsize += (x1);
  396. tmp_x1 = 0;
  397. }
  398. if (x2 > conf->w) {
  399. lsize -= (x2 - conf->w);
  400. tmp_x2 = conf->w;
  401. }
  402. if ((conf->port != LUAT_LCD_SPI_DEVICE) && conf->opts->lcd_draw) {
  403. conf->opts->lcd_draw(conf, tmp_x1, i, tmp_x2, i, line);
  404. } else {
  405. // LLOGD("action draw %dx%d %dx%d %d", tmp_x1, i, tmp_x2, i, lsize);
  406. luat_lcd_set_address(conf, tmp_x1, i, tmp_x2, i);
  407. if (conf->port == LUAT_LCD_SPI_DEVICE){
  408. luat_spi_device_send((luat_spi_device_t*)(conf->lcd_spi_device), (const char*)line, lsize * sizeof(luat_color_t));
  409. }else{
  410. luat_spi_send(conf->port, (const char*)line, lsize * sizeof(luat_color_t));
  411. }
  412. }
  413. ptr += line_size;
  414. }
  415. // TODO
  416. // LLOGD("超出边界,特殊处理");
  417. }
  418. return 0;
  419. }
  420. // buff模式
  421. #if 0
  422. int16_t x_end = x2 >= conf->w? (conf->w - 1):x2;
  423. luat_color_t* dst = (conf->buff);
  424. size_t lsize = (x2 - x1 + 1);
  425. for (int16_t x = x1; x <= x2; x++)
  426. {
  427. if (x < 0 || x >= conf->w)
  428. continue;
  429. for (int16_t y = y1; y <= y2; y++)
  430. {
  431. if (y < 0 || y >= conf->h)
  432. continue;
  433. memcpy((char*)(dst + (conf->w * y + x)), (char*)(color + (lsize * (y-y1) + (x-x1))), sizeof(luat_color_t));
  434. }
  435. }
  436. // 存储需要刷新的区域
  437. if (y1 < conf->flush_y_min) {
  438. if (y1 >= 0)
  439. conf->flush_y_min = y1;
  440. else
  441. conf->flush_y_min = 0;
  442. }
  443. if (y2 > conf->flush_y_max) {
  444. conf->flush_y_max = y2;
  445. }
  446. #endif
  447. //LLOGI("X1 %d Y1 %d X2 %d Y2 %d", x1, y1, x2, y2);
  448. int16_t x_end = (x2 >= conf->w)?(conf->w - 1):x2;
  449. int16_t y_end = (y2 >= conf->h)?(conf->h - 1):y2;
  450. luat_color_t* dst = (conf->buff);
  451. size_t fromlsize = (x2 - x1 + 1);
  452. size_t tolsize = (x_end - x1 + 1);
  453. for (int16_t y = y1; y <= y_end; y++)
  454. {
  455. if (y < 0) continue;
  456. //LLOGI("H%d L %d <- %d X2 %d Y2 %d", y, conf->w * y + x1, fromlsize * (y - y1));
  457. memcpy(&dst[(conf->w * y + x1)], &color[fromlsize * (y - y1)], tolsize * sizeof(luat_color_t));
  458. }
  459. // 存储需要刷新的区域
  460. if (y1 < conf->flush_y_min) {
  461. if (y1 >= 0)
  462. conf->flush_y_min = y1;
  463. else
  464. conf->flush_y_min = 0;
  465. }
  466. if (y_end > conf->flush_y_max) {
  467. conf->flush_y_max = y_end;
  468. }
  469. return 0;
  470. }
  471. LUAT_WEAK 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) {
  472. return luat_lcd_draw_default(conf, x1, y1, x2, y2, color);
  473. }
  474. #endif
  475. int luat_lcd_draw_point(luat_lcd_conf_t* conf, int16_t x, int16_t y, luat_color_t color) {
  476. luat_color_t tmp = color;
  477. if (conf->endianness_swap)
  478. tmp = color_swap(color);// 注意, 这里需要把颜色swap了
  479. return luat_lcd_draw(conf, x, y, x, y, &tmp);
  480. }
  481. int luat_lcd_clear(luat_lcd_conf_t* conf, luat_color_t color){
  482. luat_lcd_draw_fill(conf, 0, 0, conf->w - 1, conf->h, color);
  483. return 0;
  484. }
  485. int luat_lcd_set_reset_pin_level(luat_lcd_conf_t* conf, uint8_t level){
  486. if (conf->opts->reset_ctrl)
  487. {
  488. return conf->opts->reset_ctrl(conf, level);
  489. }
  490. else
  491. {
  492. if (conf->pin_rst != LUAT_GPIO_NONE) {
  493. luat_gpio_set(conf->pin_rst, level);
  494. }
  495. }
  496. return 0;
  497. }
  498. 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) {
  499. int16_t i;
  500. if ((conf->port != LUAT_LCD_SPI_DEVICE) && !conf->buff && conf->opts->lcd_fill)
  501. {
  502. return conf->opts->lcd_fill(conf, x1, y1, x2, y2 - 1, color);
  503. }
  504. for(i=y1;i<y2;i++)
  505. {
  506. luat_lcd_draw_line(conf, x1, i, x2, i, color);
  507. }
  508. return 0;
  509. }
  510. int luat_lcd_draw_vline(luat_lcd_conf_t* conf, int16_t x, int16_t y,int16_t h, luat_color_t color) {
  511. if (h<=0) return 0;
  512. return luat_lcd_draw_line(conf, x, y, x, y + h - 1, color);
  513. }
  514. int luat_lcd_draw_hline(luat_lcd_conf_t* conf, int16_t x, int16_t y,int16_t w, luat_color_t color) {
  515. if (w<=0) return 0;
  516. return luat_lcd_draw_line(conf, x, y, x + w - 1, y, color);
  517. }
  518. 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) {
  519. luat_color_t tmp = color;
  520. int16_t t;
  521. uint32_t i = 0;
  522. int xerr = 0, yerr = 0, delta_x, delta_y, distance;
  523. int incx, incy, row, col;
  524. if (x1 == x2 || y1 == y2) // 直线
  525. {
  526. if (x1 > x2)
  527. {
  528. int16_t tmp = x1;
  529. x1 = x2;
  530. x2 = tmp;
  531. }
  532. if (y1 > y2)
  533. {
  534. int16_t tmp = y1;
  535. y1 = y2;
  536. y2 = tmp;
  537. }
  538. size_t dots = (x2 - x1 + 1) * (y2 - y1 + 1);//点数量
  539. luat_color_t* line_buf = (luat_color_t*) luat_heap_malloc(dots * sizeof(luat_color_t));
  540. if (conf->endianness_swap)
  541. tmp = color_swap(color);// 颜色swap
  542. if (line_buf) {
  543. for (i = 0; i < dots; i++)
  544. {
  545. line_buf[i] = tmp;
  546. }
  547. luat_lcd_draw(conf, x1, y1, x2, y2, line_buf);
  548. luat_heap_free(line_buf);
  549. return 0;
  550. }
  551. }
  552. delta_x = x2 - x1;
  553. delta_y = y2 - y1;
  554. row = x1;
  555. col = y1;
  556. if (delta_x > 0)incx = 1;
  557. else if (delta_x == 0)incx = 0;
  558. else
  559. {
  560. incx = -1;
  561. delta_x = -delta_x;
  562. }
  563. if (delta_y > 0)incy = 1;
  564. else if (delta_y == 0)incy = 0;
  565. else
  566. {
  567. incy = -1;
  568. delta_y = -delta_y;
  569. }
  570. if (delta_x > delta_y)distance = delta_x;
  571. else distance = delta_y;
  572. for (t = 0; t <= distance + 1; t++)
  573. {
  574. luat_lcd_draw_point(conf,row, col,color);
  575. xerr += delta_x ;
  576. yerr += delta_y ;
  577. if (xerr > distance)
  578. {
  579. xerr -= distance;
  580. row += incx;
  581. }
  582. if (yerr > distance)
  583. {
  584. yerr -= distance;
  585. col += incy;
  586. }
  587. }
  588. return 0;
  589. }
  590. 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){
  591. luat_lcd_draw_line(conf,x1, y1, x2, y1, color);
  592. luat_lcd_draw_line(conf,x1, y1, x1, y2, color);
  593. luat_lcd_draw_line(conf,x1, y2, x2, y2, color);
  594. luat_lcd_draw_line(conf,x2, y1, x2, y2, color);
  595. return 0;
  596. }
  597. int luat_lcd_draw_circle(luat_lcd_conf_t* conf,int16_t x0, int16_t y0, uint8_t r, luat_color_t color){
  598. int a, b;
  599. int di;
  600. a = 0;
  601. b = r;
  602. di = 3 - (r << 1);
  603. while (a <= b)
  604. {
  605. luat_lcd_draw_point(conf,x0 - b, y0 - a,color);
  606. luat_lcd_draw_point(conf,x0 + b, y0 - a,color);
  607. luat_lcd_draw_point(conf,x0 - a, y0 + b,color);
  608. luat_lcd_draw_point(conf,x0 - b, y0 - a,color);
  609. luat_lcd_draw_point(conf,x0 - a, y0 - b,color);
  610. luat_lcd_draw_point(conf,x0 + b, y0 + a,color);
  611. luat_lcd_draw_point(conf,x0 + a, y0 - b,color);
  612. luat_lcd_draw_point(conf,x0 + a, y0 + b,color);
  613. luat_lcd_draw_point(conf,x0 - b, y0 + a,color);
  614. a++;
  615. //Bresenham
  616. if (di < 0)di += 4 * a + 6;
  617. else
  618. {
  619. di += 10 + 4 * (a - b);
  620. b--;
  621. }
  622. luat_lcd_draw_point(conf,x0 + a, y0 + b,color);
  623. }
  624. return 0;
  625. }
  626. #ifndef LUAT_COMPILER_NOWEAK
  627. LUAT_WEAK int luat_lcd_qspi_config(luat_lcd_conf_t* conf, luat_lcd_qspi_conf_t *qspi_config) {
  628. return -1;
  629. };
  630. LUAT_WEAK int luat_lcd_qspi_auto_flush_on_off(luat_lcd_conf_t* conf, uint8_t on_off) {
  631. return -1;
  632. }
  633. LUAT_WEAK uint8_t luat_lcd_qspi_is_no_ram(luat_lcd_conf_t* conf) {
  634. return 0;
  635. }
  636. LUAT_WEAK int luat_lcd_run_api_in_service(luat_lcd_api api, void *param, uint32_t param_len) {
  637. return -1;
  638. };
  639. #endif