コード例 #1
0
class ST7735S():
    def __init__(self, width, hight):
        self.lcdlog = log.basicConfig()
        self.lcdlog = log.getLogger("LCD")
        self.lcdlog.setLevel(log.DEBUG)
        self.lcd = LCD()
        self.lcd_w = width
        self.lcd_h = hight

        self.st7735s_init_data = (
            0, 0, 0x11,
            2, 0, 120,

            0, 3, 0xb1,
            1, 1, 0x01,
            1, 1, 0x08,
            1, 1, 0x05,

            0, 3, 0xb2,
            1, 1, 0x05,
            1, 1, 0x3c,
            1, 1, 0x3c,

            0, 6, 0xb3,
            1, 1, 0x05,
            1, 1, 0x3c,
            1, 1, 0x3c,
            1, 1, 0x05,
            1, 1, 0x3c,
            1, 1, 0x3c,

            0, 1, 0xb4,
            1, 1, 0x03,

            0, 3, 0xc0,
            1, 1, 0x28,
            1, 1, 0x08,
            1, 1, 0x04,

            0, 1, 0xc1,
            1, 1, 0xc0,

            0, 2, 0xc2,
            1, 1, 0x0d,
            1, 1, 0x00,

            0, 2, 0xc3,
            1, 1, 0x8d,
            1, 1, 0x2a,

            0, 2, 0xc4,
            1, 1, 0x8d,
            1, 1, 0xee,

            0, 1, 0xc5,
            1, 1, 0x12,

            0, 1, 0x36,
            1, 1, 0xA0,

            0, 16, 0xe0,
            1, 1, 0x04,
            1, 1, 0x22,
            1, 1, 0x07,
            1, 1, 0x0a,
            1, 1, 0x2e,
            1, 1, 0x30,
            1, 1, 0x25,
            1, 1, 0x2a,
            1, 1, 0x28,
            1, 1, 0x26,
            1, 1, 0x2e,
            1, 1, 0x3a,
            1, 1, 0x00,
            1, 1, 0x01,
            1, 1, 0x03,
            1, 1, 0x13,

            0, 16, 0xe1,
            1, 1, 0x04,
            1, 1, 0x16,
            1, 1, 0x06,
            1, 1, 0x0d,
            1, 1, 0x2d,
            1, 1, 0x26,
            1, 1, 0x23,
            1, 1, 0x27,
            1, 1, 0x27,
            1, 1, 0x25,
            1, 1, 0x2d,
            1, 1, 0x3b,
            1, 1, 0x00,
            1, 1, 0x01,
            1, 1, 0x04,
            1, 1, 0x13,

            0, 1, 0x3a,
            1, 1, 0x05,

            0, 1, 0x35,
            1, 1, 0x00,

            0, 0, 0x29,
            1, 0, 0x2c,
        )

        self.st7735s_invalid_data = (
            0, 4, 0x2A,
            1, 1, XSTART_H,
            1, 1, XSTART_L,
            1, 1, XEND_H,
            1, 1, XEND_L,
            0, 4, 0x2B,
            1, 1, YSTART_H,
            1, 1, YSTART_L,
            1, 1, YEND_H,
            1, 1, YEND_L,
            0, 0, 0x2C,
        )
        lcd_clk = 26000
        lcd_init_data = bytearray(self.st7735s_init_data)
        lcd_invalid = bytearray(self.st7735s_invalid_data)


        ret = self.lcd.lcd_init(lcd_init_data, self.lcd_w, self.lcd_h, lcd_clk, 1, 4, 0, lcd_invalid, None, None, None)

        self.lcdlog.info('lcd.lcd_init ret = {}'.format(ret))
        self.lcdlog.info('lcd.lcd_clk = {}'.format(lcd_clk))
        '''清屏,设置白色'''
        self.lcd.lcd_clear(0xFFFF)

    '''
    单个字符显示,包括汉字和ASCII
    x - x轴坐标
    y - y轴坐标
    xsize - 字体宽度
    ysize - 字体高度
    ch_buf - 存放汉字字模的元组或者列表
    fc - 字体颜色,RGB565
    bc - 背景颜色,RGB565
    '''
    def lcd_show_char(self, x, y, xsize, ysize, ch_buf, fc, bc):
        rgb_buf = []
        t1 = xsize // 8
        t2 = xsize % 8
        if t2 != 0:
            xsize = (t1 + 1) * 8
        for i in range(0, len(ch_buf)):
            for j in range(0, 8):
                if (ch_buf[i] << j) & 0x80 == 0x00:
                    rgb_buf.append(bc & 0xff)
                    rgb_buf.append(bc >> 8)
                else:
                    rgb_buf.append(fc & 0xff)
                    rgb_buf.append(fc >> 8)
        self.lcd.lcd_write(bytearray(rgb_buf), x, y, x + xsize - 1, y + ysize - 1)

    '''
    ASCII字符显示,目前支持8x16、16x24的字体大小,
    如果需要其他字体大小需要自己增加对应大小的字库数据,并
    在下面函数中增加这个对应字库的字典。
    x - x轴显示起点
    y - y轴显示起点
    xsize - 字体宽度
    ysize - 字体高度
    ch - 待显示的ASCII字符
    fc - 字体颜色,RGB565
    bc - 背景颜色,RGB565
    '''
    def lcd_show_ascii(self, x, y, xsize, ysize, ch, fc, bc):
        ascii_dict = {}
        if xsize == 8 and ysize == 16:
            ascii_dict = fonts.ascii_8x16_dict
        elif xsize == 16 and ysize == 24:
            ascii_dict = fonts.ascii_16x24_dict

        for key in ascii_dict:
            if ch == key:
                self.lcd_show_char(x, y, xsize, ysize, ascii_dict[key], fc, bc)

    '''
    显示字符串,目前支持8x16的字体大小,
    如果需要其他字体大小需要自己增加对应大小的字库数据,并
    在lcd_show_ascii函数中增加这个对应字库的字典。
    x - x轴坐标
    y - y轴坐标
    xsize - 字体宽度
    ysize - 字体高度
    str - 待显示的 ASCII 字符串
    fc - 字体颜色,RGB565
    bc - 背景颜色,RGB565
    '''
    def lcd_show_ascii_str(self, x, y, xsize, ysize, str, fc, bc):
        xs = x
        ys = y
        if (len(str) * xsize + x) > self.lcd_w:
            raise Exception('Display out of range')
        for ch in str:
            self.lcd_show_ascii(xs, ys, xsize, ysize, ch, fc, bc)
            xs += xsize

    '''
    汉字显示,目前支持16x16、16x24、24x24的字体大小,
    如果需要其他字体大小需要自己增加对应大小的字库数据,并
    在下面函数中增加这个对应字库的字典。
    x - x轴显示起点
    y - y轴显示起点
    xsize - 字体宽度
    ysize - 字体高度
    ch - 待显示的ASCII字符
    fc - 字体颜色,RGB565
    bc - 背景颜色,RGB565
    '''
    def lcd_show_chinese(self, x, y, xsize, ysize, ch, fc, bc):
        hanzi_dict = {}
        if xsize == 16 and ysize == 16:
            hanzi_dict = fonts.hanzi_16x16_dict
        elif xsize == 16 and ysize == 24:
            hanzi_dict = fonts.hanzi_16x24_dict
        elif xsize == 24 and ysize == 24:
            hanzi_dict = fonts.hanzi_24x24_dict

        for key in hanzi_dict:
            if ch == key:
                self.lcd_show_char(x, y, xsize, ysize, hanzi_dict[key], fc, bc)

    '''
    汉字字符串显示,目前支持16x16的字体大小,
    如果需要其他字体大小需要自己增加对应大小的字库数据,并
    在lcd_show_chinese函数中增加这个对应字库的字典。
    x - x轴坐标
    y - y轴坐标
    xsize - 字体宽度
    ysize - 字体高度
    str - 待显示的多个汉字
    fc - 字体颜色,RGB565
    bc - 背景颜色,RGB565
    '''
    def lcd_show_chinese_str(self, x, y, xsize, ysize, str, fc, bc):
        xs = x
        ys = y
        # print('chstrlen={}, w={}'.format(len(str), self.lcd_w))
        if (len(str) / 3 * xsize + x) > self.lcd_w:
            raise Exception('Display out of range')
        for i in range(0, len(str), 3):
            index = i + 3
            ch = str[i:index]
            self.lcd_show_chinese(xs, ys, xsize, ysize, ch, fc, bc)
            xs += xsize

    '''
    图片显示
    如果图片宽高小于80x80,可直接该函数一次性写入并显示
    image_data - 存放待显示图片的RGB数据
    x - x轴显示起点
    y - y轴显示起点
    width - 图片宽度
    heigth - 图片高度
    '''
    def lcd_show_image(self, image_data, x, y, width, heigth):
        self.lcd.lcd_write(bytearray(image_data), x, y, x + width - 1, y + heigth - 1)

    '''
    图片显示
    如果图片宽高大于80x80,用该函数来分段写入显示,分段写入原理如下:
    以要显示图片的宽度为固定值,将待显示的图片分成若干宽高为 width * h 大小的图片,最后一块高度不足h的按实际高度计算,
    h为分割后每个图片的高度,可由用户通过参数 h 指定,h的值应该满足关系: width * h * 2 < 4096
    path - 存放图片数据的txt文件路径,包含文件名,如 '/usr/image.txt'
    x - x轴显示起点
    y - y轴显示起点
    width - 图片宽度
    heigth - 图片高度
    h - 分割后每个图片的高度
    '''
    def lcd_show_image_file(self, path, x, y, width, heigth, h):
        image_data = []
        read_n = 0  # 已经读取的字节数
        byte_n = 0  # 字节数
        xs = x
        ys = y
        h_step = h  # 按高度h_step个像素点作为步长
        h1 = heigth // h_step  # 当前图片按h_step大小分割,可以得到几个 width * h_step 大小的图片
        h2 = heigth % h_step  # 最后剩下的一块 大小不足 width * h_step 的图片的实际高度
        # print('h1 = {}, h2 = {}'.format(h1, h2))
        with open(path, "r", encoding='utf-8') as fd:
            # for line in fd.readlines():
            end = ''
            while not end:
                line = fd.readline()
                if line == '':
                    end = 1
                else:
                    curline = line.strip('\r\n').strip(',').split(',')
                    for i in curline:
                        byte_n += 1
                        read_n += 1
                        image_data.append(int(i))
                        if h1 > 0 and byte_n == width * h_step * 2:
                            self.lcd_show_image(image_data, xs, ys, width, h_step)
                            image_data = []
                            ys = ys + h_step
                            h1 -= 1
                            byte_n = 0
                            # print('image_data len = {}'.format(len(image_data)))
                        elif h1 == 0 and read_n == width * heigth * 2:
                            if h2 != 0:
                                self.lcd_show_image(image_data, xs, ys, width, h2)

    '''
    将24位色转换位16位色
    如红色的24位色为0xFF0000,则r=0xFF,g=0x00,b=0x00,
    将r、g、b的值传入下面函数即可得到16位相同颜色数据
    '''
    def get_rgb565_color(self, r, g, b):
        return ((r << 8) & 0xF800) | ((g << 3) & 0x07E0) | ((b >> 3) & 0x001F)
コード例 #2
0
    手动运行本例程时,可以去掉该延时,如果将例程文件名改为main.py,希望开机自动运行时,需要加上该延时,
    否则无法从CDC口看到下面的 poweron_print_once() 中打印的信息
    '''
    #utime.sleep(5)
    #checknet.poweron_print_once()
    '''
    如果用户程序包含网络相关代码,必须执行 wait_network_connected() 等待网络就绪(拨号成功);
    如果是网络无关代码,可以屏蔽 wait_network_connected()
    '''
    # checknet.wait_network_connected()

    # 用户代码
    image_data = bytearray(image.image_buf)
    '''######################【User code star】###################################################'''
    ret = lcd.lcd_init(LCD_INIT_DATA, 176, 220, 13000, 1, 4, 0,
                       ili9225_display_area, ili9225_display_on,
                       ili9225_display_off, None)
    print('lcd.lcd_init ret = {}'.format(ret))
    # '''清屏,设置白色'''

    #lcd.lcd_clear(0xf800)
    '''
    要显示的图片像素为 99*100,下面设置显示图片的起始坐标位置为(70,70),结束坐标为(168,169)
    lcd.lcd_write(data, x_start, y_start, x_end, y_end)
    要注意:显示图片时,坐标的设置和图片像素之间必须满足如下关系,这里以 99*100 像素为例说明:
    x_end - x_start + 1 = 99
    y_end - y_start + 1 = 100
    '''
    lcd.lcd_write(image_data, 0, 0, 127, 127)
    '''######################【User code end 】###################################################'''
コード例 #3
0
    XEND_H,
    1,
    1,
    XEND_L,
    0,
    4,
    0x2B,
    1,
    1,
    YSTART_H,
    1,
    1,
    YSTART_L,
    1,
    1,
    YEND_H,
    1,
    1,
    YEND_L,
    0,
    0,
    0x2C,
)

lcd_init_data = bytearray(st7735s_init_data)
lcd_invalid = bytearray(st7735s_invalid)
lcd.lcd_init(lcd_init_data, 160, 128, 13000, 1, 4, 0, lcd_invalid, None, None,
             None)

lcd.lcd_clear(0xFF00)