# Driver for 2.4" LCD for P2 Eval Board type headers import array import p2 from machine import Pin import time import uctypes import gc #globals ilicog = -1 # don't want to start a new cog if already running iliarr = array.array('L',[0, 0,0,0,0, 0,0,0,0, 0,0,0,0]) # 12, but only need 11 at this time fontname = "" class ILI934X(): #Colors White= 0xFFFF Black= 0x0000 MidnightBlue= 0x18CE MintCream= 0xF7FF Yellow= 0xFFE0 LightYellow= 0xFFFC Aqua= 0x07FF Blue= 0x001F Gray= 0x8410 Green= 0x0400 Lime= 0x07E0 Maroon= 0x8000 Navy= 0x0010 Olive= 0x8400 Purple= 0x8010 Red= 0xF800 Silver= 0xC618 Teal= 0x0410 #Commands TakePins_ = 1 WrByte_CMD_ = 2 WrByte_DAT_ = 3 WrWord_DAT_ = 4 WrWordN_DAT_ = 5 WrBytes_DAT_ = 6 RdBytes_DAT_ = 7 DrawLineCmd_ = 8 SetPixelCmd_ = 9 DrawCircleCmd_ = 10 DrawCharCmd_ = 11 TogglePin_ = 12 def loadfont(self,fname): gc.collect() global fontname f=open(fname,"rb") self.font=f.read() f.close() # print(self.font[0:4]) if (self.font[0:4]!=b"RFD1"): print("invalid font header...") return fontname = fname self.FntStartChar=self.font[4] # print("Start Char= ",self.FntStartChar) self.FntEndChar=self.font[5] # print("End Char= ",self.FntEndChar) self.FntHeight=self.font[6] # print("Height= ",self.FntHeight) self.FntMaxWidth=self.font[7] # print("Max Width= ",self.FntMaxWidth) self.FntBytes=self.font[8] # print("Bytes= ",self.FntBytes) self.FntVarData=self.font[9] self.FntBpp=self.font[10] self.FntWidthInfo=self.font[11] self.FntAdd = uctypes.addressof(self.font) self.rows = int(self.height/self.FntHeight) self.cols = int(self.width/self.FntMaxWidth) self.row = self.col = 0 def char(self,c,x,y): a = c if isinstance(c,str): a = ord(c) if fontname=="": return if (aself.FntEndChar): return i = a - self.FntStartChar w = int(self.FntMaxWidth/8) p = int(self.FntAdd+12+i*w*self.FntHeight) self.setwindow(x,y,x+self.FntMaxWidth-1, y+self.FntHeight-1) # print(x,y,x+self.FntMaxWidth-1, y+self.FntHeight-1) self.WaitDone() self.data[2]=self.FntHeight self.data[3]=w self.data[4]=p self.data[5]=self.forecolor self.data[6]=self.backcolor # print(self.data[2],self.data[3],self.data[4],self.data[5],self.data[6]) self.data[0]=self.DrawCharCmd_ def setwindow(self,x1,y1,x2,y2): if (x20: self.col -=1 elif c == 0x09: # tab self.print(" ") while self.col & 3: self.print(" ") elif c == 0x0C or c == 0x0B: self.flag = 1 return #before setting flag to 0 elif c == 0x0D or c == 0x0A: self.newline() else: self.print(c) else: if c == 0x0C: self.col = c % self.cols elif c == 0x0B: self.row = c % self.rows self.flag = 0 def printchar(self, c): y = self.row*self.FntHeight self.char(c, self.col*self.FntMaxWidth, y) self.col += 1 if (self.col == self.cols): self.newline() def print(self, s): # print a character (or string) at current row, col if isinstance(s,str): for c in s: self.printchar(c) elif isinstance(s,int): self.printchar(s) def newline(self): self.col = 0 self.row += 1 if (self.row == self.rows): self.row -= 1 def fore(self, c): self.forecolor=c def back(self, c): self.backcolor=c def __init__(self, basepin, height=240, width=320): # self, spi, cs, dc, rst, height=240, width=320, usd=False, init_spi=False): # Process arguments self.rst = basepin + 8 #self._rst = Pin(self.rst, value=1) p2.pinh(self.rst) self.cs = basepin + 12 self.dc = basepin + 11 self.basepin = basepin self.rd = basepin + 9 self.wr = basepin + 10 self.height = height self.width = width #self.pins = array.array('L',[self.basepin, self.rst, self.rd, self.wr, self.dc, self.cs]) #pins = [self.basepin, self.rst, self.rd, self.wr, self.dc, self.cs] #pina = bytearray(pins) #pina = uctypes.addressof(self.pins)+5 global iliarr self.data=iliarr # array.array('L',[0, 0,0,0,0, 0,0,0,0, 0,0,0,0]) # 12, but only need 11 at this time #testa = uctypes.bytearray_at(self.pins, 4) #print("Testing Pins = ",testa) # collect garbage gc.collect() #Start driver global ilicog #print(ilicog) if (ilicog < 0): f=open("ili934x.binary","rb") code=f.read() f.close() self.cog=p2.Cpu() ilicog = self.cog.start(code,self.data) #print(ilicog) #Hard Reset #self._rst.value(0) p2.pinl(self.rst) time.sleep_ms(1) #self._rst.value(1) p2.pinh(self.rst) time.sleep_ms(200) #TakePins self.data[1]=self.basepin self.data[4]=self.rst self.data[5]=self.rd self.data[6]=self.wr self.data[7]=self.dc self.data[8]=self.cs WriteDelay = int(p2.clkfreq()/15000000/2) if (WriteDelay > 8): WriteDelay -= 4 self.data[2]=WriteDelay ReadDelay = int(p2.clkfreq()/2000000/2) if (ReadDelay > 8): ReadDelay-=8 self.data[3]=ReadDelay #print("Taking Pins:",self.data) self.data[0]=self.TakePins_ self.WaitDone() #ConfigureLCD # Start initial Sequence self.WrByte_CMD(b'\x28') #'display OFF self.WrByte_CMD(b'\x11') self.WrByte_DAT(b'\x00') #'exit SLEEP mode self.WrByte_CMD(b'\xC0') self.WrByte_DAT(b'\x26') self.WrByte_DAT(b'\x04') #'power control 1 self.WrByte_CMD(b'\xC1') self.WrByte_DAT(b'\x04') #'power control 2 self.WrByte_CMD(b'\xC5') self.WrByte_DAT(b'\x34') self.WrByte_DAT(b'\x40') #'VCOM control 1 self.WrByte_CMD(b'\x36') self.WrByte_DAT(b'\xA8') #'memory access control = b'\xA0=RGB, b'\xA8=BGR self.WrByte_CMD(b'\xB1') self.WrByte_DAT(b'\x00') self.WrByte_DAT(b'\x18') #'frame rate control self.WrByte_CMD(b'\xB6') self.WrByte_DAT(b'\x0A') self.WrByte_DAT(b'\xA2') #'display function control self.WrByte_CMD(b'\xC7') self.WrByte_DAT(b'\xC0') #'VCOM control 2 self.WrByte_CMD(b'\x3A') self.WrByte_DAT(b'\x05') #'pixel format: b'\x06 = 18 bits per pixel, b'\x05 = 16 bits per pixel self.WrByte_CMD(b'\xE0') self.WrByte_DAT(b'\x1F') #'positive gamma correction self.WrByte_DAT(b'\x1B') self.WrByte_DAT(b'\x18') self.WrByte_DAT(b'\x0B') self.WrByte_DAT(b'\x0F') self.WrByte_DAT(b'\x09') self.WrByte_DAT(b'\x46') self.WrByte_DAT(b'\xB5') self.WrByte_DAT(b'\x37') self.WrByte_DAT(b'\x0A') self.WrByte_DAT(b'\x0C') self.WrByte_DAT(b'\x07') self.WrByte_DAT(b'\x07') self.WrByte_DAT(b'\x05') self.WrByte_DAT(b'\x00') self.WrByte_CMD(b'\xE1') self.WrByte_DAT(b'\x00') #'negative gamma correction self.WrByte_DAT(b'\x24') self.WrByte_DAT(b'\x27') self.WrByte_DAT(b'\x04') self.WrByte_DAT(b'\x10') self.WrByte_DAT(b'\x06') self.WrByte_DAT(b'\x39') self.WrByte_DAT(b'\x74') self.WrByte_DAT(b'\x48') self.WrByte_DAT(b'\x05') self.WrByte_DAT(b'\x13') self.WrByte_DAT(b'\x38') self.WrByte_DAT(b'\x38') self.WrByte_DAT(b'\x3A') self.WrByte_DAT(b'\x1F') self.WrByte_CMD(b'\x2A') self.WrByte_DAT(b'\x00') #'column address set (set horizontal window to full screen) self.WrByte_DAT(b'\x00') #'start b'\x00 self.WrByte_DAT(b'\x01') self.WrByte_DAT(b'\x3F') #'end b'\x013F =320-1 self.WrByte_CMD(b'\x2B') self.WrByte_DAT(b'\x00') #'page address set (set vertical window to full screen) self.WrByte_DAT(b'\x00') #start b'\x00 self.WrByte_DAT(b'\x00') self.WrByte_DAT(b'\xEF') #end b'\xEF =240-1 self.WrByte_CMD(b'\x29') #display on #self.WrByte_CMD(b'\x2C') #write pixels self.forecolor=self.White self.backcolor=self.Navy self.cls(self.backcolor) self.flag = 0 def WrByte_CMD(self, d): self.WaitDone() self.data[1]=int.from_bytes(d,"big") self.data[0]=self.WrByte_CMD_ def WrByte_DAT(self, d): self.WaitDone() self.data[1]=int.from_bytes(d,"big") self.data[0]=self.WrByte_DAT_ def WrBytes_DAT(self, d, n): self.WaitDone() self.data[1]=int.from_bytes(d,"big") self.data[2]=n self.data[0]=self.WrByte_DAT_ def WrWord_DAT(self, d): self.WaitDone() self.data[1]=d self.data[0]=self.WrWord_DAT_ def WrWordN_DAT(self, d, n): self.WaitDone() self.data[1]=d self.data[2]=n self.data[0]=self.WrWordN_DAT_ def TogglePin(self, pin): self.data[1]=pin self.WaitDone() self.data[0]=self.TogglePin_ def WaitDone(self): i=self.data[0] while (i!=0): i=self.data[0]