fix: 对不支持weak的bsp, luat_http_client_onevent报重复定义了
[LuatOS.git] / components / lcd / luat_lcd.c
blob0c8ab984823f003d6494988524d279f2c9218c7d
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"
8 #define LUAT_LOG_TAG "lcd"
9 #include "luat_log.h"
11 luat_color_t BACK_COLOR = LCD_WHITE, FORE_COLOR = LCD_BLACK;
13 #define LUAT_LCD_CONF_COUNT (1)
14 static luat_lcd_conf_t* confs[LUAT_LCD_CONF_COUNT] = {0};
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);
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;
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);
55 int lcd_write_data(luat_lcd_conf_t* conf, const uint8_t data){
56 size_t len;
57 if (conf->port == LUAT_LCD_SPI_DEVICE){
58 len = luat_spi_device_send((luat_spi_device_t*)(conf->lcd_spi_device), (const char*)&data, 1);
59 }else{
60 len = luat_spi_send(conf->port, (const char*)&data, 1);
62 if (len != 1){
63 LLOGI("lcd_write_data error. %d", len);
64 return -1;
65 }else{
66 return 0;
70 int lcd_write_cmd_data(luat_lcd_conf_t* conf,const uint8_t cmd, const uint8_t *data, uint8_t data_len){
71 if (conf->opts->write_cmd_data){
72 return conf->opts->write_cmd_data(conf,cmd,data,data_len);
74 size_t len;
75 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){
76 luat_gpio_set(conf->pin_dc, Luat_GPIO_LOW);
78 #ifdef LUAT_LCD_CMD_DELAY_US
79 if (conf->dc_delay_us){
80 luat_timer_us_delay(conf->dc_delay_us);
82 #endif
83 if (conf->port == LUAT_LCD_SPI_DEVICE){
84 len = luat_spi_device_send((luat_spi_device_t*)(conf->lcd_spi_device), (const char*)&cmd, 1);
85 }else{
86 len = luat_spi_send(conf->port, (const char*)&cmd, 1);
88 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){
89 luat_gpio_set(conf->pin_dc, Luat_GPIO_HIGH);
91 if (len != 1){
92 LLOGI("lcd_write_cmd error. %d", len);
93 return -1;
94 }else{
95 #ifdef LUAT_LCD_CMD_DELAY_US
96 if (conf->dc_delay_us){
97 luat_timer_us_delay(conf->dc_delay_us);
99 #endif
101 if (data_len){
102 if (conf->port == LUAT_LCD_SPI_DEVICE){
103 len = luat_spi_device_send((luat_spi_device_t*)(conf->lcd_spi_device), (const char*)data, data_len);
104 }else{
105 len = luat_spi_send(conf->port, (const char*)data, data_len);
107 if (len != data_len){
108 LLOGI("lcd_write_data error. %d", len);
109 return -1;
112 return 0;
115 luat_lcd_conf_t* luat_lcd_get_default(void) {
116 for (size_t i = 0; i < LUAT_LCD_CONF_COUNT; i++){
117 if (confs[i] != NULL) {
118 return confs[i];
121 return NULL;
124 const char* luat_lcd_name(luat_lcd_conf_t* conf) {
125 return conf->opts->name;
128 int luat_lcd_init(luat_lcd_conf_t* conf) {
129 uint8_t direction_date = 0;
130 conf->is_init_done = 0;
131 if (conf->w == 0)
132 conf->w = LCD_W;
133 if (conf->h == 0)
134 conf->h = LCD_H;
135 if (conf->pin_pwr != LUAT_GPIO_NONE)
136 luat_gpio_mode(conf->pin_pwr, Luat_GPIO_OUTPUT, Luat_GPIO_DEFAULT, Luat_GPIO_LOW); // POWER
137 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){
138 if (conf->pin_dc != LUAT_GPIO_NONE) luat_gpio_mode(conf->pin_dc, Luat_GPIO_OUTPUT, Luat_GPIO_DEFAULT, Luat_GPIO_HIGH); // DC
140 luat_gpio_mode(conf->pin_rst, Luat_GPIO_OUTPUT, Luat_GPIO_DEFAULT, Luat_GPIO_LOW); // RST
142 if (conf->pin_pwr != LUAT_GPIO_NONE)
143 luat_gpio_set(conf->pin_pwr, Luat_GPIO_LOW);
144 luat_gpio_set(conf->pin_rst, Luat_GPIO_LOW);
145 luat_rtos_task_sleep(100);
146 luat_gpio_set(conf->pin_rst, Luat_GPIO_HIGH);
147 luat_rtos_task_sleep(120);
148 luat_lcd_wakeup(conf);
149 luat_rtos_task_sleep(120);
151 // 发送初始化命令
152 if (conf->opts->init){
153 conf->opts->init(conf);
154 }else{
155 luat_lcd_execute_cmds(conf);
156 if(strcmp(conf->opts->name,"custom") == 0){
157 luat_heap_free(conf->opts->init_cmds);
159 luat_lcd_set_direction(conf,conf->direction);
162 luat_lcd_wakeup(conf);
163 /* wait for power stability */
164 luat_rtos_task_sleep(100);
165 luat_lcd_clear(conf,LCD_BLACK);
166 /* display on */
167 luat_lcd_display_on(conf);
169 conf->is_init_done = 1;
170 for (size_t i = 0; i < LUAT_LCD_CONF_COUNT; i++){
171 if (confs[i] == NULL) {
172 confs[i] = conf;
173 return 0;
176 return -1;
179 int luat_lcd_close(luat_lcd_conf_t* conf) {
180 if (conf->pin_pwr != 255)
181 luat_gpio_set(conf->pin_pwr, Luat_GPIO_LOW);
182 return 0;
185 int luat_lcd_display_off(luat_lcd_conf_t* conf) {
186 if (conf->pin_pwr != 255)
187 luat_gpio_set(conf->pin_pwr, Luat_GPIO_LOW);
188 lcd_write_cmd_data(conf,0x28, NULL, 0);
189 return 0;
192 int luat_lcd_display_on(luat_lcd_conf_t* conf) {
193 if (conf->pin_pwr != 255)
194 luat_gpio_set(conf->pin_pwr, Luat_GPIO_HIGH);
195 lcd_write_cmd_data(conf,0x29, NULL, 0);
196 return 0;
199 int luat_lcd_sleep(luat_lcd_conf_t* conf) {
200 if (conf->pin_pwr != 255)
201 luat_gpio_set(conf->pin_pwr, Luat_GPIO_LOW);
202 luat_rtos_task_sleep(5);
203 lcd_write_cmd_data(conf,conf->opts->sleep_cmd?conf->opts->sleep_cmd:LUAT_LCD_DEFAULT_SLEEP, NULL, 0);
204 return 0;
207 int luat_lcd_wakeup(luat_lcd_conf_t* conf) {
208 if (conf->pin_pwr != 255)
209 luat_gpio_set(conf->pin_pwr, Luat_GPIO_HIGH);
210 luat_rtos_task_sleep(5);
211 lcd_write_cmd_data(conf,conf->opts->wakeup_cmd?conf->opts->wakeup_cmd:LUAT_LCD_DEFAULT_WAKEUP, NULL, 0);
212 return 0;
215 int luat_lcd_inv_off(luat_lcd_conf_t* conf) {
216 lcd_write_cmd_data(conf,0x20, NULL, 0);
217 return 0;
220 int luat_lcd_inv_on(luat_lcd_conf_t* conf) {
221 lcd_write_cmd_data(conf,0x21, NULL, 0);
222 return 0;
225 int luat_lcd_set_address(luat_lcd_conf_t* conf,int16_t x1, int16_t y1, int16_t x2, int16_t y2) {
226 uint8_t data_x[] = {(x1+conf->xoffset)>>8,x1+conf->xoffset,(x2+conf->xoffset)>>8,x2+conf->xoffset};
227 lcd_write_cmd_data(conf,0x2a, data_x, 4);
228 uint8_t data_y[] = {(y1+conf->yoffset)>>8,y1+conf->yoffset,(y2+conf->yoffset)>>8,y2+conf->yoffset};
229 lcd_write_cmd_data(conf,0x2b, data_y, 4);
230 lcd_write_cmd_data(conf,0x2c, NULL, 0);
231 return 0;
234 int luat_lcd_set_color(luat_color_t back, luat_color_t fore){
235 BACK_COLOR = back;
236 FORE_COLOR = fore;
237 return 0;
240 int luat_lcd_set_direction(luat_lcd_conf_t* conf, uint8_t direction){
241 uint8_t direction_date = 0;
242 if(direction==0) direction_date = conf->opts->direction0;
243 else if(direction==1) direction_date = conf->opts->direction90;
244 else if(direction==2) direction_date = conf->opts->direction180;
245 else direction_date = conf->opts->direction270;
246 lcd_write_cmd_data(conf,0x36, &direction_date, 1);
247 return 0;
250 #ifndef LUAT_USE_LCD_CUSTOM_DRAW
251 int luat_lcd_flush(luat_lcd_conf_t* conf) {
252 if (conf->buff == NULL) {
253 return 0;
255 //LLOGD("luat_lcd_flush range %d %d", conf->flush_y_min, conf->flush_y_max);
256 if (conf->flush_y_max < conf->flush_y_min) {
257 // 没有需要刷新的内容,直接跳过
258 //LLOGD("luat_lcd_flush no need");
259 return 0;
261 uint32_t size = conf->w * (conf->flush_y_max - conf->flush_y_min + 1) * 2;
262 luat_lcd_set_address(conf, 0, conf->flush_y_min, conf->w - 1, conf->flush_y_max);
263 const char* tmp = (const char*)(conf->buff + conf->flush_y_min * conf->w);
264 if (conf->port == LUAT_LCD_SPI_DEVICE){
265 luat_spi_device_send((luat_spi_device_t*)(conf->lcd_spi_device), tmp, size);
266 }else{
267 luat_spi_send(conf->port, tmp, size);
270 // 重置为不需要刷新的状态
271 conf->flush_y_max = 0;
272 conf->flush_y_min = conf->h;
274 return 0;
277 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) {
278 if (x1 >= conf->w || y1 >= conf->h || x2 < 0 || y2 < 0 || x2 < x1 || y2 < y1) {
279 // LLOGE("out of lcd buff range %d %d %d %d", x1, y1, x2, y2);
280 // LLOGE("out of lcd buff range %d %d %d %d %d", x1 >= conf->w, y1 >= conf->h, y2 < 0, x2 < x1, y2 < y1);
281 return 0;
283 if (y2 >= conf->h) {
284 y2 = conf->h - 1;
286 if (conf->opts->lcd_draw)
287 return conf->opts->lcd_draw(conf, x1, y1, x2, y2, color);
288 // 直接刷屏模式
289 if (conf->buff == NULL) {
290 // 常规数据, 整体传输
291 if (x1 >= 0 && y1 >= 0 && x2 <= conf->w && y2 <= conf->h) {
292 uint32_t size = (x2 - x1 + 1) * (y2 - y1 + 1);
293 // LLOGD("draw %dx%d %dx%d %d", x1, y1, x2, y2, size);
294 luat_lcd_set_address(conf, x1, y1, x2, y2);
295 if (conf->port == LUAT_LCD_SPI_DEVICE){
296 luat_spi_device_send((luat_spi_device_t*)(conf->lcd_spi_device), (const char*)color, size* sizeof(luat_color_t));
297 }else{
298 luat_spi_send(conf->port, (const char*)color, size * sizeof(luat_color_t));
301 // 超出边界的数据, 按行传输
302 else {
303 int line_size = (x2 - x1 + 1);
304 // LLOGD("want draw %dx%d %dx%d %d", x1, y1, x2, y2, line_size);
305 luat_color_t* ptr = (luat_color_t*)color;
306 for (int i = y1; i <= y2; i++)
308 if (i < 0) {
309 ptr += line_size;
310 continue;
312 luat_color_t* line = ptr;
313 int lsize = line_size;
314 int tmp_x1 = x1;
315 int tmp_x2 = x2;
316 if (x1 < 0) {
317 line += ( - x1);
318 lsize += (x1);
319 tmp_x1 = 0;
321 if (x2 > conf->w) {
322 lsize -= (x2 - conf->w);
323 tmp_x2 = conf->w;
325 // LLOGD("action draw %dx%d %dx%d %d", tmp_x1, i, tmp_x2, i, lsize);
326 luat_lcd_set_address(conf, tmp_x1, i, tmp_x2, i);
327 if (conf->port == LUAT_LCD_SPI_DEVICE){
328 luat_spi_device_send((luat_spi_device_t*)(conf->lcd_spi_device), (const char*)line, lsize * sizeof(luat_color_t));
329 }else{
330 luat_spi_send(conf->port, (const char*)line, lsize * sizeof(luat_color_t));
332 ptr += line_size;
335 // TODO
336 // LLOGD("超出边界,特殊处理");
338 return 0;
340 // buff模式
341 int16_t x_end = x2 >= conf->w? (conf->w - 1):x2;
342 luat_color_t* dst = (conf->buff);
343 size_t lsize = (x2 - x1 + 1);
344 for (int16_t x = x1; x <= x2; x++)
346 if (x < 0 || x >= conf->w)
347 continue;
348 for (int16_t y = y1; y <= y2; y++)
350 if (y < 0 || y >= conf->h)
351 continue;
352 memcpy((char*)(dst + (conf->w * y + x)), (char*)(color + (lsize * (y-y1) + (x-x1))), sizeof(luat_color_t));
355 // 存储需要刷新的区域
356 if (y1 < conf->flush_y_min) {
357 if (y1 >= 0)
358 conf->flush_y_min = y1;
359 else
360 conf->flush_y_min = 0;
362 if (y2 > conf->flush_y_max) {
363 conf->flush_y_max = y2;
365 return 0;
367 #endif
369 int luat_lcd_draw_point(luat_lcd_conf_t* conf, int16_t x, int16_t y, luat_color_t color) {
370 luat_color_t tmp = color;
371 if (conf->port != LUAT_LCD_HW_ID_0)
372 tmp = color_swap(color);// 注意, 这里需要把颜色swap了
373 return luat_lcd_draw(conf, x, y, x, y, &tmp);
376 int luat_lcd_clear(luat_lcd_conf_t* conf, luat_color_t color){
377 luat_lcd_draw_fill(conf, 0, 0, conf->w - 1, conf->h, color);
378 return 0;
381 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) {
382 int16_t i;
383 for(i=y1;i<y2;i++)
385 luat_lcd_draw_line(conf, x1, i, x2, i, color);
387 return 0;
390 int luat_lcd_draw_vline(luat_lcd_conf_t* conf, int16_t x, int16_t y,int16_t h, luat_color_t color) {
391 if (h<=0) return 0;
392 return luat_lcd_draw_line(conf, x, y, x, y + h - 1, color);
395 int luat_lcd_draw_hline(luat_lcd_conf_t* conf, int16_t x, int16_t y,int16_t w, luat_color_t color) {
396 if (w<=0) return 0;
397 return luat_lcd_draw_line(conf, x, y, x + w - 1, y, color);
400 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) {
401 luat_color_t tmp = color;
402 int16_t t;
403 uint32_t i = 0;
404 int xerr = 0, yerr = 0, delta_x, delta_y, distance;
405 int incx, incy, row, col;
406 if (x1 == x2 || y1 == y2) // 直线
408 size_t dots = (x2 - x1 + 1) * (y2 - y1 + 1);//点数量
409 luat_color_t* line_buf = (luat_color_t*) luat_heap_malloc(dots * sizeof(luat_color_t));
410 if (conf->port != LUAT_LCD_HW_ID_0)
411 tmp = color_swap(color);// 颜色swap
412 if (line_buf) {
413 for (i = 0; i < dots; i++)
415 line_buf[i] = tmp;
417 luat_lcd_draw(conf, x1, y1, x2, y2, line_buf);
418 luat_heap_free(line_buf);
419 return 0;
423 delta_x = x2 - x1;
424 delta_y = y2 - y1;
425 row = x1;
426 col = y1;
427 if (delta_x > 0)incx = 1;
428 else if (delta_x == 0)incx = 0;
429 else
431 incx = -1;
432 delta_x = -delta_x;
434 if (delta_y > 0)incy = 1;
435 else if (delta_y == 0)incy = 0;
436 else
438 incy = -1;
439 delta_y = -delta_y;
441 if (delta_x > delta_y)distance = delta_x;
442 else distance = delta_y;
443 for (t = 0; t <= distance + 1; t++)
445 luat_lcd_draw_point(conf,row, col,color);
446 xerr += delta_x ;
447 yerr += delta_y ;
448 if (xerr > distance)
450 xerr -= distance;
451 row += incx;
453 if (yerr > distance)
455 yerr -= distance;
456 col += incy;
459 return 0;
462 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){
463 luat_lcd_draw_line(conf,x1, y1, x2, y1, color);
464 luat_lcd_draw_line(conf,x1, y1, x1, y2, color);
465 luat_lcd_draw_line(conf,x1, y2, x2, y2, color);
466 luat_lcd_draw_line(conf,x2, y1, x2, y2, color);
467 return 0;
470 int luat_lcd_draw_circle(luat_lcd_conf_t* conf,int16_t x0, int16_t y0, uint8_t r, luat_color_t color){
471 int a, b;
472 int di;
473 a = 0;
474 b = r;
475 di = 3 - (r << 1);
476 while (a <= b)
478 luat_lcd_draw_point(conf,x0 - b, y0 - a,color);
479 luat_lcd_draw_point(conf,x0 + b, y0 - a,color);
480 luat_lcd_draw_point(conf,x0 - a, y0 + b,color);
481 luat_lcd_draw_point(conf,x0 - b, y0 - a,color);
482 luat_lcd_draw_point(conf,x0 - a, y0 - b,color);
483 luat_lcd_draw_point(conf,x0 + b, y0 + a,color);
484 luat_lcd_draw_point(conf,x0 + a, y0 - b,color);
485 luat_lcd_draw_point(conf,x0 + a, y0 + b,color);
486 luat_lcd_draw_point(conf,x0 - b, y0 + a,color);
487 a++;
488 //Bresenham
489 if (di < 0)di += 4 * a + 6;
490 else
492 di += 10 + 4 * (a - b);
493 b--;
495 luat_lcd_draw_point(conf,x0 + a, y0 + b,color);
497 return 0;