浏览代码

add: st7735v驱动

chenxuuu 4 年之前
父节点
当前提交
29e71e55f8
共有 2 个文件被更改,包括 181 次插入4 次删除
  1. 174 0
      components/lcd/luat_lcd_st7735v.c
  2. 7 4
      components/lcd/luat_lib_lcd.c

+ 174 - 0
components/lcd/luat_lcd_st7735v.c

@@ -0,0 +1,174 @@
+#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 "st7735v"
+#include "luat_log.h"
+
+#define LCD_W 128
+#define LCD_H 160
+#define LCD_DIRECTION 0
+
+static int st7735v_sleep(luat_lcd_conf_t* conf) {
+    if (conf->pin_pwr != 255)
+        luat_gpio_set(conf->pin_pwr, Luat_GPIO_LOW);
+    luat_timer_mdelay(5);
+    lcd_write_cmd(conf,0x10);
+    return 0;
+}
+
+static int st7735v_wakeup(luat_lcd_conf_t* conf) {
+    if (conf->pin_pwr != 255)
+        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 st7735v_close(luat_lcd_conf_t* conf) {
+    if (conf->pin_pwr != 255)
+        luat_gpio_set(conf->pin_pwr, Luat_GPIO_LOW);
+    return 0;
+}
+static int st7735v_init(luat_lcd_conf_t* conf) {
+    if (conf->w == 0)
+        conf->w = LCD_W;
+    if (conf->h == 0)
+        conf->h = LCD_H;
+    if (conf->direction == 0)
+        conf->direction = LCD_DIRECTION;
+
+    if (conf->pin_pwr != 255)
+        luat_gpio_mode(conf->pin_pwr, Luat_GPIO_OUTPUT, Luat_GPIO_DEFAULT, Luat_GPIO_LOW); // POWER
+    luat_gpio_mode(conf->pin_dc, Luat_GPIO_OUTPUT, Luat_GPIO_DEFAULT, Luat_GPIO_HIGH); // DC
+    luat_gpio_mode(conf->pin_rst, Luat_GPIO_OUTPUT, Luat_GPIO_DEFAULT, Luat_GPIO_LOW); // RST
+
+    if (conf->pin_pwr != 255)
+    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(120);//ms
+
+    lcd_write_cmd(conf,0x11);
+
+    luat_timer_mdelay(120);//ms
+
+    lcd_write_cmd(conf,0x21);
+
+    lcd_write_cmd(conf,0xB1);
+    lcd_write_data(conf,0x05);
+    lcd_write_data(conf,0x3A);
+    lcd_write_data(conf,0x3A);
+
+    lcd_write_cmd(conf,0xB2);
+    lcd_write_data(conf,0x05);
+    lcd_write_data(conf,0x3A);
+    lcd_write_data(conf,0x3A);
+
+    lcd_write_cmd(conf,0xB3);
+    lcd_write_data(conf,0x05);
+    lcd_write_data(conf,0x3A);
+    lcd_write_data(conf,0x3A);
+    lcd_write_data(conf,0x05);
+    lcd_write_data(conf,0x3A);
+    lcd_write_data(conf,0x3A);
+
+    lcd_write_cmd(conf,0xB4);//Dotinversion
+    lcd_write_data(conf,0x03);
+
+    lcd_write_cmd(conf,0xC0);
+    lcd_write_data(conf,0x62);
+    lcd_write_data(conf,0x02);
+    lcd_write_data(conf,0x04);
+
+    lcd_write_cmd(conf,0xC1);
+    lcd_write_data(conf,0xC0);
+
+    lcd_write_cmd(conf,0xC2);
+    lcd_write_data(conf,0x0D);
+    lcd_write_data(conf,0x00);
+
+    lcd_write_cmd(conf,0xC3);
+    lcd_write_data(conf,0x8D);
+    lcd_write_data(conf,0x6A);
+
+    lcd_write_cmd(conf,0xC4);
+    lcd_write_data(conf,0x8D);
+    lcd_write_data(conf,0xEE);
+
+    lcd_write_cmd(conf,0xC5);//VCOM
+    lcd_write_data(conf,0x0E);
+
+    lcd_write_cmd(conf,0xE0);
+    lcd_write_data(conf,0x10);
+    lcd_write_data(conf,0x0E);
+    lcd_write_data(conf,0x02);
+    lcd_write_data(conf,0x03);
+    lcd_write_data(conf,0x0E);
+    lcd_write_data(conf,0x07);
+    lcd_write_data(conf,0x02);
+    lcd_write_data(conf,0x07);
+    lcd_write_data(conf,0x0A);
+    lcd_write_data(conf,0x12);
+    lcd_write_data(conf,0x27);
+    lcd_write_data(conf,0x37);
+    lcd_write_data(conf,0x00);
+    lcd_write_data(conf,0x0D);
+    lcd_write_data(conf,0x0E);
+    lcd_write_data(conf,0x10);
+
+
+    lcd_write_cmd(conf,0xE1);
+    lcd_write_data(conf,0x10);
+    lcd_write_data(conf,0x0E);
+    lcd_write_data(conf,0x03);
+    lcd_write_data(conf,0x03);
+    lcd_write_data(conf,0x0F);
+    lcd_write_data(conf,0x06);
+    lcd_write_data(conf,0x02);
+    lcd_write_data(conf,0x08);
+    lcd_write_data(conf,0x0A);
+    lcd_write_data(conf,0x13);
+    lcd_write_data(conf,0x26);
+    lcd_write_data(conf,0x36);
+    lcd_write_data(conf,0x00);
+    lcd_write_data(conf,0x0D);
+    lcd_write_data(conf,0x0E);
+    lcd_write_data(conf,0x10);
+
+    lcd_write_cmd(conf,0x3A);
+    lcd_write_data(conf,0x05);
+
+    // lcd_write_cmd(conf,0x36);
+    // lcd_write_data(conf,0xC8);
+
+    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,0x29);
+
+    /* wait for power stability */
+    luat_timer_mdelay(100);
+    luat_lcd_clear(conf,WHITE);
+    /* display on */
+    luat_lcd_display_on(conf);
+    return 0;
+};
+
+const luat_lcd_opts_t lcd_opts_st7735v = {
+    .name = "st7735v",
+    .init = st7735v_init,
+    .close = st7735v_close,
+    .sleep = st7735v_sleep,
+    .wakeup = st7735v_wakeup,
+};
+

+ 7 - 4
components/lcd/luat_lib_lcd.c

@@ -34,6 +34,7 @@ enum
 extern uint32_t BACK_COLOR , FORE_COLOR ;
 
 extern const luat_lcd_opts_t lcd_opts_st7735;
+extern const luat_lcd_opts_t lcd_opts_st7735v;
 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;
@@ -49,7 +50,7 @@ static uint32_t lcd_str_fg_color,lcd_str_bg_color;
 /*
 lcd显示屏初始化
 @api lcd.init(tp, args)
-@string lcd类型, 当前支持st7789/st7735/st7735s/gc9a01/gc9106l/gc9306/ili9341/custom
+@string lcd类型, 当前支持st7789/st7735/st7735v/st7735s/gc9a01/gc9106l/gc9306/ili9341/custom
 @table 附加参数,与具体设备有关,pin_pwr为可选项,可不设置port:spi端口,例如0,1,2...如果为device方式则为"device";pin_dc:lcd数据/命令选择引脚;pin_rst:lcd复位引脚;pin_pwr:lcd背光引脚 可选项,可不设置;direction:lcd屏幕方向 0:0° 1:180° 2:270° 3:90°;w:lcd 水平分辨率;h:lcd 竖直分辨率;xoffset:x偏移(不同屏幕ic 不同屏幕方向会有差异);yoffset:y偏移(不同屏幕ic 不同屏幕方向会有差异)
 @userdata spi设备,当port = "device"时有效
 @usage
@@ -68,7 +69,7 @@ static int l_lcd_init(lua_State* L) {
         conf->port = LUAT_LCD_SPI_DEVICE;
     }
     const char* tp = luaL_checklstring(L, 1, &len);
-    if (!strcmp("st7735", tp) || !strcmp("st7789", tp) || !strcmp("st7735s", tp)
+    if (!strcmp("st7735", tp) || !strcmp("st7735v", tp) || !strcmp("st7789", tp) || !strcmp("st7735s", tp)
             || !strcmp("gc9a01", tp)  || !strcmp("gc9106l", tp)
             || !strcmp("gc9306", tp)  || !strcmp("ili9341", tp)  || !strcmp("ili9488", tp)
             || !strcmp("custom", tp)) {
@@ -141,6 +142,8 @@ static int l_lcd_init(lua_State* L) {
         }
         if (!strcmp("st7735", tp))
             conf->opts = (luat_lcd_opts_t*)&lcd_opts_st7735;
+        else if (!strcmp("st7735v", tp))
+            conf->opts = (luat_lcd_opts_t*)&lcd_opts_st7735v;
         else if (!strcmp("st7735s", tp))
             conf->opts = (luat_lcd_opts_t*)&lcd_opts_st7735s;
         else if (!strcmp("st7789", tp))
@@ -955,7 +958,7 @@ static int l_lcd_draw_gtfont_utf8(lua_State *L) {
         uint16_t str = unicodetogb2312(e);
         get_font(buf, 1, str, size, size, size);
         gtfont_draw_w(buf , x ,y , size , size,luat_lcd_draw_point,default_conf,0);
-        x+=size;    
+        x+=size;
       }
     }
     return 0;
@@ -993,7 +996,7 @@ static int l_lcd_draw_gtfont_utf8_gray(lua_State* L) {
 			get_font(buf, 1, str, size*font_g, size*font_g, size*font_g);
 			Gray_Process(buf,size,size,font_g);
       gtfont_draw_gray_hz(buf, x, y, size , size, font_g, 1,luat_lcd_draw_point,default_conf,0);
-        	x+=size;    
+        	x+=size;
         }
     }
     return 0;