Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # WS 2-0 320x240 display
- # Tony Goodhew 10 Aug 2024
- from machine import Pin,SPI,PWM
- import framebuf
- import time
- import array
- import gc
- BL = 13
- DC = 8
- RST = 12
- MOSI = 11
- SCK = 10
- CS = 9
- class LCD_1inch3(framebuf.FrameBuffer): # For 320x240 display
- def __init__(self):
- self.width = 320
- self.height = 240
- self.cs = Pin(CS,Pin.OUT)
- self.rst = Pin(RST,Pin.OUT)
- self.cs(1)
- self.spi = SPI(1)
- self.spi = SPI(1,1000_000)
- self.spi = SPI(1,100000_000,polarity=0, phase=0,sck=Pin(SCK),mosi=Pin(MOSI),miso=None)
- self.dc = Pin(DC,Pin.OUT)
- self.dc(1)
- self.buffer = bytearray(self.height * self.width * 2)
- super().__init__(self.buffer, self.width, self.height, framebuf.RGB565)
- self.init_display()
- self.RED = 0x07E0
- self.GREEN = 0x001f
- self.BLUE = 0xf800
- self.WHITE = 0xffff
- self.BLACK = 0x0000
- def write_cmd(self, cmd):
- self.cs(1)
- self.dc(0)
- self.cs(0)
- self.spi.write(bytearray([cmd]))
- self.cs(1)
- def write_data(self, buf):
- self.cs(1)
- self.dc(1)
- self.cs(0)
- self.spi.write(bytearray([buf]))
- self.cs(1)
- def init_display(self):
- """Initialize display"""
- self.rst(1)
- self.rst(0)
- self.rst(1)
- self.write_cmd(0x36)
- self.write_data(0x70) # 0x70
- self.write_cmd(0x3A)
- self.write_data(0x05)
- self.write_cmd(0xB2)
- self.write_data(0x0C)
- self.write_data(0x0C)
- self.write_data(0x00)
- self.write_data(0x33)
- self.write_data(0x33)
- self.write_cmd(0xB7)
- self.write_data(0x35)
- self.write_cmd(0xBB)
- self.write_data(0x19)
- self.write_cmd(0xC0)
- self.write_data(0x2C)
- self.write_cmd(0xC2)
- self.write_data(0x01)
- self.write_cmd(0xC3)
- self.write_data(0x12)
- self.write_cmd(0xC4)
- self.write_data(0x20)
- self.write_cmd(0xC6)
- self.write_data(0x0F)
- self.write_cmd(0xD0)
- self.write_data(0xA4)
- self.write_data(0xA1)
- self.write_cmd(0xE0)
- self.write_data(0xD0)
- self.write_data(0x04)
- self.write_data(0x0D)
- self.write_data(0x11)
- self.write_data(0x13)
- self.write_data(0x2B)
- self.write_data(0x3F)
- self.write_data(0x54)
- self.write_data(0x4C)
- self.write_data(0x18)
- self.write_data(0x0D)
- self.write_data(0x0B)
- self.write_data(0x1F)
- self.write_data(0x23)
- self.write_cmd(0xE1)
- self.write_data(0xD0)
- self.write_data(0x04)
- self.write_data(0x0C)
- self.write_data(0x11)
- self.write_data(0x13)
- self.write_data(0x2C)
- self.write_data(0x3F)
- self.write_data(0x44)
- self.write_data(0x51)
- self.write_data(0x2F)
- self.write_data(0x1F)
- self.write_data(0x1F)
- self.write_data(0x20)
- self.write_data(0x23)
- self.write_cmd(0x21)
- self.write_cmd(0x11)
- self.write_cmd(0x29)
- def show(self):
- self.write_cmd(0x2A)
- self.write_data(0x00)
- self.write_data(0x00)
- self.write_data(0x01)
- self.write_data(0x3f)
- self.write_cmd(0x2B)
- self.write_data(0x00)
- self.write_data(0x00)
- self.write_data(0x00)
- self.write_data(0xEF)
- self.write_cmd(0x2C)
- self.cs(1)
- self.dc(1)
- self.cs(0)
- self.spi.write(self.buffer)
- self.cs(1)
- # == New procedure from Waveshare ==
- def write_text(self,text,x,y,size,color):
- ''' Method to write Text on OLED/LCD Displays
- with a variable font size
- Args:
- text: the string of chars to be displayed
- x: x co-ordinate of starting position
- y: y co-ordinate of starting position
- size: font size of text
- color: color of text to be displayed
- '''
- background = self.pixel(x,y)
- info = []
- # Creating reference characters to read their values
- self.text(text,x,y,color)
- for i in range(x,x+(8*len(text))):
- for j in range(y,y+8):
- # Fetching and saving details of pixels, such as
- # x co-ordinate, y co-ordinate, and color of the pixel
- px_color = self.pixel(i,j)
- info.append((i,j,px_color)) if px_color == color else None
- # Clearing the reference characters from the screen
- self.text(text,x,y,background)
- # Writing the custom-sized font characters on screen
- for px_info in info:
- self.fill_rect(size*px_info[0] - (size-1)*x , size*px_info[1] - (size-1)*y, size, size, px_info[2])
- # Additional routines from Tony ===
- def centred(text,y,size,color):
- x = int((LCD.width - len(text) * 8 * size)/2)
- LCD.write_text(text,x,y,size,color)
- # Colour Mixing Routine
- def colour(R,G,B): # Compact method!
- mix1 = ((R&0xF8)*256) + ((G&0xFC)*8) + ((B&0xF8)>>3)
- return (mix1 & 0xFF) *256 + int((mix1 & 0xFF00) /256) # low nibble first
- # ==== Main ====
- pwm = PWM(Pin(BL))
- pwm.freq(1000)
- pwm.duty_u16(32768)#max 65535
- LCD = LCD_1inch3()
- LCD.fill(LCD.WHITE)
- LCD.show()
- # Set up buttons
- key0 = Pin(15,Pin.IN,Pin.PULL_UP)
- key1 = Pin(17,Pin.IN,Pin.PULL_UP)
- key2 = Pin(2 ,Pin.IN,Pin.PULL_UP)
- key3= Pin(3 ,Pin.IN,Pin.PULL_UP)
- centre_x =int(LCD.width/2)
- centre_y = int(LCD.height/2)
- R = int((LCD.width + LCD.height)/4)
- r = int((LCD.width + LCD.height)/8)
- LCD.fill(0)
- LCD.show()
- LCD.ellipse(centre_x, centre_y,R,r,colour(255,255,0),1)
- LCD.show()
- LCD.ellipse(centre_x, centre_y,r,r,colour(0,0,255))
- LCD.show()
- for i in range(r, 0, -1):
- print(i) # <==================== WHY IS THIS NEEDED? ==================
- LCD.ellipse(centre_x, centre_y,i+1, i+1,colour(i*5,i*3,i*4), 1)
- LCD.show()
- time.sleep(0.1)
- time.sleep(2)
- LCD.fill(0)
- LCD.show()
- gc.collect
- print()
- print(gc.mem_free())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement