Forráskód Böngészése

add: lcd添加偏移参数,添加st7735s驱动

chenxuuu 4 éve
szülő
commit
9f7c205f03

+ 4 - 1
components/lcd/luat_lcd.h

@@ -25,7 +25,7 @@
 
 #define LUAT_LCD_COLOR_DEPTH 32
 
-#if (LUAT_LCD_COLOR_DEPTH == 32) 
+#if (LUAT_LCD_COLOR_DEPTH == 32)
 #define luat_color_t uint32_t
 #elif (LUAT_LCD_COLOR_DEPTH == 16)
 #define luat_color_t uint16_t
@@ -47,6 +47,9 @@ typedef struct luat_lcd_conf {
     uint32_t w;
     uint32_t h;
 
+    uint8_t xoffset;//偏移
+    uint8_t yoffset;//偏移
+
     void* userdata;
     struct luat_lcd_opts* opts;
 } luat_lcd_conf_t;

+ 2 - 2
components/lcd/luat_lcd_custom.c

@@ -47,7 +47,7 @@ static int custom_init(luat_lcd_conf_t* conf) {
     luat_gpio_set(conf->pin_rst, Luat_GPIO_LOW);
     luat_timer_mdelay(100);
     luat_gpio_set(conf->pin_rst, Luat_GPIO_HIGH);
-    
+
     // 发送初始化命令
     luat_lcd_custom_t * cst = (luat_lcd_custom_t *)conf->userdata;
     luat_lcd_execute_cmds(conf, cst->initcmd, cst->init_cmd_count);
@@ -56,7 +56,7 @@ static int custom_init(luat_lcd_conf_t* conf) {
 
 static int custom_draw(luat_lcd_conf_t* conf, uint16_t x_start, uint16_t y_start, uint16_t x_end, uint16_t y_end, luat_color_t* color) {
     uint32_t size = size = (x_end - x_start+1) * (y_end - y_start+1) * 2;
-    luat_lcd_set_address(conf,x_start, y_start, x_end, y_end);
+    luat_lcd_set_address(conf,x_start+conf->xoffset, y_start+conf->yoffset, x_end+conf->xoffset, y_end+conf->yoffset);
     luat_gpio_set(conf->pin_dc, Luat_GPIO_HIGH);
     luat_spi_send(conf->port, (const char*)color, size);
     return 0;

+ 1 - 1
components/lcd/luat_lcd_gc9106l.c

@@ -118,7 +118,7 @@ static int gc9106l_init(luat_lcd_conf_t* conf) {
 
 static int gc9106l_draw(luat_lcd_conf_t* conf, uint16_t x_start, uint16_t y_start, uint16_t x_end, uint16_t y_end, luat_color_t* color) {
     uint32_t size = size = (x_end - x_start+1) * (y_end - y_start+1) * 2;
-    luat_lcd_set_address(conf,x_start, y_start, x_end, y_end);
+    luat_lcd_set_address(conf,x_start+conf->xoffset, y_start+conf->yoffset, x_end+conf->xoffset, y_end+conf->yoffset);
     luat_gpio_set(conf->pin_dc, Luat_GPIO_HIGH);
     luat_spi_send(conf->port, (const char*)color, size);
     return 0;

+ 1 - 1
components/lcd/luat_lcd_gc9306.c

@@ -186,7 +186,7 @@ static int gc9306_init(luat_lcd_conf_t* conf) {
 
 static int gc9306_draw(luat_lcd_conf_t* conf, uint16_t x_start, uint16_t y_start, uint16_t x_end, uint16_t y_end, luat_color_t* color) {
     uint32_t size = size = (x_end - x_start+1) * (y_end - y_start+1) * 2;
-    luat_lcd_set_address(conf,x_start, y_start, x_end, y_end);
+    luat_lcd_set_address(conf,x_start+conf->xoffset, y_start+conf->yoffset, x_end+conf->xoffset, y_end+conf->yoffset);
     luat_gpio_set(conf->pin_dc, Luat_GPIO_HIGH);
     luat_spi_send(conf->port, (const char*)color, size);
     return 0;

+ 1 - 1
components/lcd/luat_lcd_gc9a01.c

@@ -285,7 +285,7 @@ static int gc9a01_init(luat_lcd_conf_t* conf) {
 
 static int gc9a01_draw(luat_lcd_conf_t* conf, uint16_t x_start, uint16_t y_start, uint16_t x_end, uint16_t y_end, luat_color_t* color) {
     uint32_t size = size = (x_end - x_start+1) * (y_end - y_start+1) * 2;
-    luat_lcd_set_address(conf,x_start, y_start, x_end, y_end);
+    luat_lcd_set_address(conf,x_start+conf->xoffset, y_start+conf->yoffset, x_end+conf->xoffset, y_end+conf->yoffset);
     luat_gpio_set(conf->pin_dc, Luat_GPIO_HIGH);
     luat_spi_send(conf->port, (const char*)color, size);
     return 0;

+ 1 - 1
components/lcd/luat_lcd_ili9341.c

@@ -162,7 +162,7 @@ static int ili9341_init(luat_lcd_conf_t* conf) {
 
 static int ili9341_draw(luat_lcd_conf_t* conf, uint16_t x_start, uint16_t y_start, uint16_t x_end, uint16_t y_end, luat_color_t* color) {
     uint32_t size = size = (x_end - x_start+1) * (y_end - y_start+1) * 2;
-    luat_lcd_set_address(conf,x_start, y_start, x_end, y_end);
+    luat_lcd_set_address(conf,x_start+conf->xoffset, y_start+conf->yoffset, x_end+conf->xoffset, y_end+conf->yoffset);
     luat_gpio_set(conf->pin_dc, Luat_GPIO_HIGH);
     luat_spi_send(conf->port, (const char*)color, size);
     return 0;

+ 1 - 1
components/lcd/luat_lcd_st7735.c

@@ -148,7 +148,7 @@ static int st7735_init(luat_lcd_conf_t* conf) {
 
 static int st7735_draw(luat_lcd_conf_t* conf, uint16_t x_start, uint16_t y_start, uint16_t x_end, uint16_t y_end, luat_color_t* color) {
     uint32_t size = size = (x_end - x_start+1) * (y_end - y_start+1) * 2;
-    luat_lcd_set_address(conf,x_start, y_start, x_end, y_end);
+    luat_lcd_set_address(conf,x_start+conf->xoffset, y_start+conf->yoffset, x_end+conf->xoffset, y_end+conf->yoffset);
     luat_gpio_set(conf->pin_dc, Luat_GPIO_HIGH);
     luat_spi_send(conf->port, (const char*)color, size);
     return 0;

+ 171 - 0
components/lcd/luat_lcd_st7735s.c

@@ -0,0 +1,171 @@
+#include "luat_base.h"
+#include "luat_lcd.h"
+#include "luat_gpio.h"
+#include "luat_spi.h"
+#include "luat_malloc.h"
+#include "luat_timer.h"
+
+#define LUAT_LOG_TAG "st7735s"
+#include "luat_log.h"
+
+#define LCD_W 128
+#define LCD_H 160
+#define LCD_DIRECTION 0
+
+static int st7735s_sleep(luat_lcd_conf_t* conf) {
+    luat_gpio_set(conf->pin_pwr, Luat_GPIO_LOW);
+    luat_timer_mdelay(5);
+    lcd_write_cmd(conf,0x10);
+    return 0;
+}
+
+static int st7735s_wakeup(luat_lcd_conf_t* conf) {
+    luat_gpio_set(conf->pin_pwr, Luat_GPIO_HIGH);
+    luat_timer_mdelay(5);
+    lcd_write_cmd(conf,0x11);
+    //luat_timer_mdelay(120); // 外部休眠就好了吧
+    return 0;
+}
+
+static int st7735s_close(luat_lcd_conf_t* conf) {
+    luat_gpio_set(conf->pin_pwr, Luat_GPIO_LOW);
+    return 0;
+}
+static int st7735s_init(luat_lcd_conf_t* conf) {
+    if (conf->w == 0)
+        conf->w = LCD_W;
+    if (conf->h == 0)
+        conf->h = LCD_H;
+
+    luat_gpio_mode(conf->pin_dc, Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, 0); // DC
+    luat_gpio_mode(conf->pin_pwr, Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, 0); // POWER
+    luat_gpio_mode(conf->pin_rst, Luat_GPIO_OUTPUT, Luat_GPIO_PULLUP, 0); // RST
+
+    luat_gpio_mode(conf->pin_dc, Luat_GPIO_OUTPUT, Luat_GPIO_DEFAULT, Luat_GPIO_HIGH); // DC
+    luat_gpio_mode(conf->pin_pwr, Luat_GPIO_OUTPUT, Luat_GPIO_DEFAULT, Luat_GPIO_LOW); // POWER
+    luat_gpio_mode(conf->pin_rst, Luat_GPIO_OUTPUT, Luat_GPIO_DEFAULT, Luat_GPIO_LOW); // RST
+
+    luat_gpio_set(conf->pin_pwr, Luat_GPIO_LOW);
+    luat_gpio_set(conf->pin_rst, Luat_GPIO_LOW);
+    luat_timer_mdelay(100);
+    luat_gpio_set(conf->pin_rst, Luat_GPIO_HIGH);
+    luat_timer_mdelay(100);
+
+	lcd_write_cmd(conf,0x11);     //Sleep out
+	luat_timer_mdelay(120);                //Delay 120ms
+	lcd_write_cmd(conf,0xB1);     //Normal mode
+	lcd_write_data(conf,0x05);
+	lcd_write_data(conf,0x3C);
+	lcd_write_data(conf,0x3C);
+	lcd_write_cmd(conf,0xB2);     //Idle mode
+	lcd_write_data(conf,0x05);
+	lcd_write_data(conf,0x3C);
+	lcd_write_data(conf,0x3C);
+	lcd_write_cmd(conf,0xB3);     //Partial mode
+	lcd_write_data(conf,0x05);
+	lcd_write_data(conf,0x3C);
+	lcd_write_data(conf,0x3C);
+	lcd_write_data(conf,0x05);
+	lcd_write_data(conf,0x3C);
+	lcd_write_data(conf,0x3C);
+	lcd_write_cmd(conf,0xB4);     //Dot inversion
+	lcd_write_data(conf,0x03);
+	lcd_write_cmd(conf,0xC0);     //AVDD GVDD
+	lcd_write_data(conf,0xAB);
+	lcd_write_data(conf,0x0B);
+	lcd_write_data(conf,0x04);
+	lcd_write_cmd(conf,0xC1);     //VGH VGL
+	lcd_write_data(conf,0xC5);   //C0
+	lcd_write_cmd(conf,0xC2);     //Normal Mode
+	lcd_write_data(conf,0x0D);
+	lcd_write_data(conf,0x00);
+	lcd_write_cmd(conf,0xC3);     //Idle
+	lcd_write_data(conf,0x8D);
+	lcd_write_data(conf,0x6A);
+	lcd_write_cmd(conf,0xC4);     //Partial+Full
+	lcd_write_data(conf,0x8D);
+	lcd_write_data(conf,0xEE);
+	lcd_write_cmd(conf,0xC5);     //VCOM
+	lcd_write_data(conf,0x0F);
+	lcd_write_cmd(conf,0xE0);     //positive gamma
+	lcd_write_data(conf,0x07);
+	lcd_write_data(conf,0x0E);
+	lcd_write_data(conf,0x08);
+	lcd_write_data(conf,0x07);
+	lcd_write_data(conf,0x10);
+	lcd_write_data(conf,0x07);
+	lcd_write_data(conf,0x02);
+	lcd_write_data(conf,0x07);
+	lcd_write_data(conf,0x09);
+	lcd_write_data(conf,0x0F);
+	lcd_write_data(conf,0x25);
+	lcd_write_data(conf,0x36);
+	lcd_write_data(conf,0x00);
+	lcd_write_data(conf,0x08);
+	lcd_write_data(conf,0x04);
+	lcd_write_data(conf,0x10);
+	lcd_write_cmd(conf,0xE1);     //negative gamma
+	lcd_write_data(conf,0x0A);
+	lcd_write_data(conf,0x0D);
+	lcd_write_data(conf,0x08);
+	lcd_write_data(conf,0x07);
+	lcd_write_data(conf,0x0F);
+	lcd_write_data(conf,0x07);
+	lcd_write_data(conf,0x02);
+	lcd_write_data(conf,0x07);
+	lcd_write_data(conf,0x09);
+	lcd_write_data(conf,0x0F);
+	lcd_write_data(conf,0x25);
+	lcd_write_data(conf,0x35);
+	lcd_write_data(conf,0x00);
+	lcd_write_data(conf,0x09);
+	lcd_write_data(conf,0x04);
+	lcd_write_data(conf,0x10);
+
+	lcd_write_cmd(conf,0xFC);
+	lcd_write_data(conf,0x80);
+
+	lcd_write_cmd(conf,0x3A);
+	lcd_write_data(conf,0x05);
+	lcd_write_cmd(conf,0x36);
+    if(conf->direction==0)lcd_write_data(conf,0xC8);
+    else if(conf->direction==1)lcd_write_data(conf,0x78);
+    else if(conf->direction==2)lcd_write_data(conf,0x08);
+    else lcd_write_data(conf,0xA8);
+	lcd_write_cmd(conf,0x21);     //Display inversion
+	lcd_write_cmd(conf,0x29);     //Display on
+	lcd_write_cmd(conf,0x2A);     //Set Column Address
+	lcd_write_data(conf,0x00);
+	lcd_write_data(conf,conf->xoffset);  //26
+	lcd_write_data(conf,0x00);
+	lcd_write_data(conf,conf->w+conf->xoffset-1);   //105
+	lcd_write_cmd(conf,0x2B);     //Set Page Address
+	lcd_write_data(conf,0x00);
+	lcd_write_data(conf,conf->yoffset);    //1
+	lcd_write_data(conf,0x00);
+	lcd_write_data(conf,conf->h+conf->yoffset-1);    //160
+	lcd_write_cmd(conf,0x2C);
+
+    luat_lcd_clear(conf,WHITE);
+    /* display on */
+    luat_lcd_display_on(conf);
+    return 0;
+};
+
+static int st7735s_draw(luat_lcd_conf_t* conf, uint16_t x_start, uint16_t y_start, uint16_t x_end, uint16_t y_end, luat_color_t* color) {
+    uint32_t size = size = (x_end - x_start+1) * (y_end - y_start+1) * 2;
+    luat_lcd_set_address(conf,x_start+conf->xoffset, y_start+conf->yoffset, x_end+conf->xoffset, y_end+conf->yoffset);
+    luat_gpio_set(conf->pin_dc, Luat_GPIO_HIGH);
+    luat_spi_send(conf->port, (const char*)color, size);
+    return 0;
+}
+
+const luat_lcd_opts_t lcd_opts_st7735s = {
+    .name = "st7735s",
+    .init = st7735s_init,
+    .close = st7735s_close,
+    .draw = st7735s_draw,
+    .sleep = st7735s_sleep,
+    .wakeup = st7735s_wakeup,
+};
+

+ 1 - 1
components/lcd/luat_lcd_st7789.c

@@ -138,7 +138,7 @@ static int st7789_init(luat_lcd_conf_t* conf) {
 
 static int st7789_draw(luat_lcd_conf_t* conf, uint16_t x_start, uint16_t y_start, uint16_t x_end, uint16_t y_end, luat_color_t* color) {
     uint32_t size = size = (x_end - x_start+1) * (y_end - y_start+1) * 2;
-    luat_lcd_set_address(conf,x_start, y_start, x_end, y_end);
+    luat_lcd_set_address(conf,x_start+conf->xoffset, y_start+conf->yoffset, x_end+conf->xoffset, y_end+conf->yoffset);
     luat_gpio_set(conf->pin_dc, Luat_GPIO_HIGH);
     luat_spi_send(conf->port, (const char*)color, size);
     return 0;

+ 25 - 10
components/lcd/luat_lib_lcd.c

@@ -15,6 +15,7 @@
 extern uint32_t BACK_COLOR , FORE_COLOR ;
 
 extern const luat_lcd_opts_t lcd_opts_st7735;
+extern const luat_lcd_opts_t lcd_opts_st7735s;
 extern const luat_lcd_opts_t lcd_opts_st7789;
 extern const luat_lcd_opts_t lcd_opts_gc9a01;
 extern const luat_lcd_opts_t lcd_opts_gc9106l;
@@ -27,16 +28,16 @@ static luat_lcd_conf_t *default_conf = NULL;
 /*
 lcd显示屏初始化
 @api lcd.init(tp, args)
-@string lcd类型, 当前支持st7789/st7735/gc9a01/gc9106l/gc9306/ili9341/custom
+@string lcd类型, 当前支持st7789/st7735/st7735s/gc9a01/gc9106l/gc9306/ili9341/custom
 @table 附加参数,与具体设备有关
 @usage
 -- 初始化spi0的st7789 注意:lcd初始化之前需要先初始化spi
-lcd.init("st7789",{port = 0,pin_dc = 23, pin_pwr = 7,pin_rst = 22,direction = 0,w = 240,h = 320})
+lcd.init("st7789",{port = 0,pin_dc = 23, pin_pwr = 7,pin_rst = 22,direction = 0,w = 240,h = 320,xoffset = 0,yoffset = 0})
 */
 static int l_lcd_init(lua_State* L) {
     size_t len = 0;
     const char* tp = luaL_checklstring(L, 1, &len);
-    if (!strcmp("st7735", tp) || !strcmp("st7789", tp)
+    if (!strcmp("st7735", tp) || !strcmp("st7789", tp) || !strcmp("st7735s", tp)
             || !strcmp("gc9a01", tp)  || !strcmp("gc9106l", tp)
             || !strcmp("gc9306", tp)  || !strcmp("ili9341", tp)
             || !strcmp("custom", tp)) {
@@ -49,47 +50,61 @@ static int l_lcd_init(lua_State* L) {
             lua_pushstring(L, "port");
             if (LUA_TNUMBER == lua_gettable(L, 2)) {
                 conf->port = luaL_checkinteger(L, -1);
-            };
+            }
             lua_pop(L, 1);
 
             lua_pushstring(L, "pin_dc");
             if (LUA_TNUMBER == lua_gettable(L, 2)) {
                 conf->pin_dc = luaL_checkinteger(L, -1);
-            };
+            }
             lua_pop(L, 1);
 
             lua_pushstring(L, "pin_pwr");
             if (LUA_TNUMBER == lua_gettable(L, 2)) {
                 conf->pin_pwr = luaL_checkinteger(L, -1);
-            };
+            }
             lua_pop(L, 1);
 
             lua_pushstring(L, "pin_rst");
             if (LUA_TNUMBER == lua_gettable(L, 2)) {
                 conf->pin_rst = luaL_checkinteger(L, -1);
-            };
+            }
             lua_pop(L, 1);
 
             lua_pushstring(L, "direction");
             if (LUA_TNUMBER == lua_gettable(L, 2)) {
                 conf->direction = luaL_checkinteger(L, -1);
-            };
+            }
             lua_pop(L, 1);
 
             lua_pushstring(L, "w");
             if (LUA_TNUMBER == lua_gettable(L, 2)) {
                 conf->w = luaL_checkinteger(L, -1);
-            };
+            }
             lua_pop(L, 1);
 
             lua_pushstring(L, "h");
             if (LUA_TNUMBER == lua_gettable(L, 2)) {
                 conf->h = luaL_checkinteger(L, -1);
-            };
+            }
+            lua_pop(L, 1);
+
+            lua_pushstring(L, "xoffset");
+            if (LUA_TNUMBER == lua_gettable(L, 2)) {
+                conf->xoffset = luaL_checkinteger(L, -1);
+            }
+            lua_pop(L, 1);
+
+            lua_pushstring(L, "yoffset");
+            if (LUA_TNUMBER == lua_gettable(L, 2)) {
+                conf->yoffset = luaL_checkinteger(L, -1);
+            }
             lua_pop(L, 1);
         }
         if (!strcmp("st7735", tp))
             conf->opts = (luat_lcd_opts_t*)&lcd_opts_st7735;
+        else if (!strcmp("st7735s", tp))
+            conf->opts = (luat_lcd_opts_t*)&lcd_opts_st7735s;
         else if (!strcmp("st7789", tp))
             conf->opts = (luat_lcd_opts_t*)&lcd_opts_st7789;
         else if (!strcmp("gc9a01", tp))