你的位置:首页 > 技术 > Python

micropython 使用ili9163显示IP地址

2020-01-04 浏览:(31) Python 评论(0)

mian.py

exec(open('./wifi.py').read(),globals())
exec(open('./tft.py').read(),globals())

wifi.py

import network
wlan = network.WLAN(network.STA_IF) # create station interface
wlan.active(True)       # activate the interface
wlan.connect('028sd.com', '111111') # connect to an AP
while not wlan.isconnected():
  pass
wlan.config('mac')      # get the interface's MAC address
wlan.ifconfig()         # get the interface's IP/netmask/gw/DNS addresses

tft.py

from machine import Pin, SPI
from ili9163 import ILI9163_SPI,ILI9163
VCC = Pin(13, Pin.OUT)
VCC.on()
spi = SPI(baudrate=1000000,sck=Pin(27), mosi=Pin(26),miso=Pin(4))  #miso=Pin(4)是无交的因为必须配置所以乱写的
ili = ILI9163_SPI(128, 160, spi, Pin(25), Pin(33), Pin(12))
ili.fill(1)

ili.text('IP:%s' % wlan.ifconfig()[0], 0, 10, 0x0000)
ili.show()
ili.text('hi! fukun', 0, 140, 0x0000)
ili.show()

ili9163.py  模块文件

import time
from micropython import const
import framebuf

class ILI9163(framebuf.FrameBuffer):
    def __init__(self, width, height):
        self.width = width
        self.height = height
        self.buffer = bytearray(self.width * self.height // 8)

        self.palette = bytearray(self.height * 4)

        for y in range(0, self.height):
            pb = y * 4
            self.palette[pb + 0] = 0x00
            self.palette[pb + 1] = 0x00
            self.palette[pb + 2] = 0xff
            self.palette[pb + 3] = 0xff

        super().__init__(self.buffer, self.width, self.height, framebuf.MONO_HMSB)
        self.init_display()

    def set_line_range_palette(self, y0, y1, bcol, fcol):
        for y in range(y0, y1):
            pb = y * 4
            self.palette[pb + 0] = bcol & 0xff
            self.palette[pb + 1] = bcol >> 8
            self.palette[pb + 2] = fcol & 0xff
            self.palette[pb + 3] = fcol >> 8

    def init_display(self):
        self.write_cmd(0x11) # Exit Sleep

        time.sleep_ms(20)

        self.write_cmd(0x26, [0x04]) # Set Default Gamma
        self.write_cmd(0xB1, [0x08,0x08]) # Set Frame Rate
        self.write_cmd(0xC0, [0x0a,0x02]) # Set VRH1[4:0] & VC[2:0] for VCI1 & GVDD
        self.write_cmd(0xC1, [0x03]) # Set BT[2:0] for AVDD & VCL & VGH & VGL
        self.write_cmd(0xC5, [0x4f,0x5a]) # Set VMH[6:0] & VML[6:0] for VOMH & VCOML

        self.write_cmd(0x3a, [0x55]) # Set Color Format, 5=16 bit,3=12 bit
        self.write_cmd(0x36, [0xc0]) # RGB

        self.write_cmd(0x2A, [0,0,0,self.width]) # Set Column Address
        self.write_cmd(0x2B, [0,0,0,self.height]) # Set Page Address

        self.write_cmd(0xB4, [0]) #  display inversion
        self.write_cmd(0xC7, [0x40])
        self.write_cmd(0xf2, [0x01]) # Enable Gamma bit
        self.write_cmd(0xE0, [0x3f,0x25,0x1c,0x1e,0x20,0x12,0x2a,0x90,0x24,0x11,0x00,0x00,0x00,0x00,0x00])
        self.write_cmd(0xE1, [0x20,0x20,0x20,0x20,0x05,0x00,0x15,0xa7,0x3d,0x18,0x25,0x2a,0x2b,0x2b,0x3a])
        
        self.write_cmd(0xB7, [0,0])
        self.write_cmd(0x29) #  Display On
        self.write_cmd(0x2C) #  reset frame ptr

        self.fill(0)
        self.show()

    def poweroff(self):
        pass

    def poweron(self):
        self.write_cmd(0x29)

    def contrast(self, contrast):
        pass

    def invert(self, invert):
        pass

    def show(self):
        x0 = 0
        x1 = self.width - 1

        y0 = 0
        y1 = self.height + 0 - 1

        self.write_cmd(0x2A, [x0, 0, x1])
        self.write_cmd(0x2B, [y0, 0, y1])
        self.write_cmd(0x2C)
        self.write_data(self.buffer)


class ILI9163_SPI(ILI9163):
    def __init__(self, width, height, spi, dc, res, cs):
        self.rate = 10 * 1024 * 1024
        
        dc.init(dc.OUT, value=0)
        res.init(res.OUT, value=0)
        cs.init(cs.OUT, value=1)
        
        self.spi = spi
        self.dc = dc
        self.res = res
        self.cs = cs

        self.res(1)
        time.sleep_ms(1)
        self.res(0)
        time.sleep_ms(10)
        self.res(1)

        super().__init__(width, height)

    def write_cmd(self, cmd, data=None):
        self.spi.init(baudrate=self.rate, polarity=0, phase=0)
        
        self.cs(1)
        self.dc(0)
        self.cs(0)
        self.spi.write(bytearray([cmd]))

        if data != None:
            self.cs(1)
            self.dc(1)
            self.cs(0)
            self.spi.write(bytearray(data))

        self.cs(1)

    def write_data(self, buf):
        self.spi.init(baudrate=self.rate, polarity=0, phase=0)
        
        self.cs(1)
        self.dc(1)
        self.cs(0)

        line_buffer = bytearray(self.width * 2)
        
        # 32 lines of padding...
        #for _ in range(0, 0):
        #    self.spi.write(line_buffer)
        
        for y in range(0, self.height):
            pb = y * 4

            bcoll = self.palette[pb + 0]
            bcolh = self.palette[pb + 1]
            fcoll = self.palette[pb + 2]
            fcolh = self.palette[pb + 3]

            for x in range(0, self.width // 8):
                b = buf[y * (self.width // 8) + x]
                
                for i in range(0, 8):
                    bit = b & 1
                    b = b >> 1

                    px = x * 8 + i
                    bo = px * 2

                    coll = fcoll if bit == 1 else bcoll
                    colh = fcolh if bit == 1 else bcolh

                    line_buffer[bo + 0] = coll
                    line_buffer[bo + 1] = colh

            self.spi.write(line_buffer)

        self.cs(1)

# Convert RGB888 to BRG565
# ((b & 0xF8) << 8) | ((r & 0xFC) << 3) | (g >> 3)

# Sample code
# ===========

# HSPI = 1
# VSPI = 2

# from machine import Pin, SPI
# spi = SPI(VSPI, sck=Pin(18), mosi=Pin(23))

# ili = ILI9163_SPI(128, 128, spi, Pin(2), Pin(4), Pin(15))

# ili.text('Hello World', 0, 0, 1)

ili9163.rar