Advertisement
TonyGo

Pico 2 problem

Aug 11th, 2024
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.27 KB | Software | 0 0
  1. # WS 2-0 320x240 display
  2. # Tony Goodhew 10 Aug 2024
  3. from machine import Pin,SPI,PWM
  4. import framebuf
  5. import time
  6. import array
  7. import gc
  8. BL = 13
  9. DC = 8
  10. RST = 12
  11. MOSI = 11
  12. SCK = 10
  13. CS = 9
  14.  
  15. class LCD_1inch3(framebuf.FrameBuffer): # For 320x240 display
  16.     def __init__(self):
  17.         self.width = 320
  18.         self.height = 240
  19.        
  20.         self.cs = Pin(CS,Pin.OUT)
  21.         self.rst = Pin(RST,Pin.OUT)
  22.        
  23.         self.cs(1)
  24.         self.spi = SPI(1)
  25.         self.spi = SPI(1,1000_000)
  26.         self.spi = SPI(1,100000_000,polarity=0, phase=0,sck=Pin(SCK),mosi=Pin(MOSI),miso=None)
  27.         self.dc = Pin(DC,Pin.OUT)
  28.         self.dc(1)
  29.         self.buffer = bytearray(self.height * self.width * 2)
  30.         super().__init__(self.buffer, self.width, self.height, framebuf.RGB565)
  31.         self.init_display()
  32.        
  33.         self.RED   =   0x07E0
  34.         self.GREEN =   0x001f
  35.         self.BLUE  =   0xf800
  36.         self.WHITE =   0xffff
  37.         self.BLACK =   0x0000
  38.        
  39.     def write_cmd(self, cmd):
  40.         self.cs(1)
  41.         self.dc(0)
  42.         self.cs(0)
  43.         self.spi.write(bytearray([cmd]))
  44.         self.cs(1)
  45.  
  46.     def write_data(self, buf):
  47.         self.cs(1)
  48.         self.dc(1)
  49.         self.cs(0)
  50.         self.spi.write(bytearray([buf]))
  51.         self.cs(1)
  52.  
  53.     def init_display(self):
  54.         """Initialize display"""  
  55.         self.rst(1)
  56.         self.rst(0)
  57.         self.rst(1)
  58.        
  59.         self.write_cmd(0x36)
  60.         self.write_data(0x70) # 0x70
  61.  
  62.         self.write_cmd(0x3A)
  63.         self.write_data(0x05)
  64.  
  65.         self.write_cmd(0xB2)
  66.         self.write_data(0x0C)
  67.         self.write_data(0x0C)
  68.         self.write_data(0x00)
  69.         self.write_data(0x33)
  70.         self.write_data(0x33)
  71.  
  72.         self.write_cmd(0xB7)
  73.         self.write_data(0x35)
  74.  
  75.         self.write_cmd(0xBB)
  76.         self.write_data(0x19)
  77.  
  78.         self.write_cmd(0xC0)
  79.         self.write_data(0x2C)
  80.  
  81.         self.write_cmd(0xC2)
  82.         self.write_data(0x01)
  83.  
  84.         self.write_cmd(0xC3)
  85.         self.write_data(0x12)  
  86.  
  87.         self.write_cmd(0xC4)
  88.         self.write_data(0x20)
  89.  
  90.         self.write_cmd(0xC6)
  91.         self.write_data(0x0F)
  92.  
  93.         self.write_cmd(0xD0)
  94.         self.write_data(0xA4)
  95.         self.write_data(0xA1)
  96.  
  97.         self.write_cmd(0xE0)
  98.         self.write_data(0xD0)
  99.         self.write_data(0x04)
  100.         self.write_data(0x0D)
  101.         self.write_data(0x11)
  102.         self.write_data(0x13)
  103.         self.write_data(0x2B)
  104.         self.write_data(0x3F)
  105.         self.write_data(0x54)
  106.         self.write_data(0x4C)
  107.         self.write_data(0x18)
  108.         self.write_data(0x0D)
  109.         self.write_data(0x0B)
  110.         self.write_data(0x1F)
  111.         self.write_data(0x23)
  112.  
  113.         self.write_cmd(0xE1)
  114.         self.write_data(0xD0)
  115.         self.write_data(0x04)
  116.         self.write_data(0x0C)
  117.         self.write_data(0x11)
  118.         self.write_data(0x13)
  119.         self.write_data(0x2C)
  120.         self.write_data(0x3F)
  121.         self.write_data(0x44)
  122.         self.write_data(0x51)
  123.         self.write_data(0x2F)
  124.         self.write_data(0x1F)
  125.         self.write_data(0x1F)
  126.         self.write_data(0x20)
  127.         self.write_data(0x23)
  128.        
  129.         self.write_cmd(0x21)
  130.  
  131.         self.write_cmd(0x11)
  132.  
  133.         self.write_cmd(0x29)
  134.  
  135.     def show(self):
  136.         self.write_cmd(0x2A)
  137.         self.write_data(0x00)
  138.         self.write_data(0x00)
  139.         self.write_data(0x01)
  140.         self.write_data(0x3f)
  141.        
  142.         self.write_cmd(0x2B)
  143.         self.write_data(0x00)
  144.         self.write_data(0x00)
  145.         self.write_data(0x00)
  146.         self.write_data(0xEF)
  147.        
  148.         self.write_cmd(0x2C)
  149.        
  150.         self.cs(1)
  151.         self.dc(1)
  152.         self.cs(0)
  153.         self.spi.write(self.buffer)
  154.         self.cs(1)
  155.        
  156. # == New procedure from Waveshare ==        
  157.     def write_text(self,text,x,y,size,color):
  158.         ''' Method to write Text on OLED/LCD Displays
  159.        with a variable font size
  160.        Args:
  161.            text: the string of chars to be displayed
  162.            x: x co-ordinate of starting position
  163.            y: y co-ordinate of starting position
  164.            size: font size of text
  165.            color: color of text to be displayed
  166.        '''
  167.         background = self.pixel(x,y)
  168.         info = []
  169.         # Creating reference characters to read their values
  170.         self.text(text,x,y,color)
  171.         for i in range(x,x+(8*len(text))):
  172.             for j in range(y,y+8):
  173.                 # Fetching and saving details of pixels, such as
  174.                 # x co-ordinate, y co-ordinate, and color of the pixel
  175.                 px_color = self.pixel(i,j)
  176.                 info.append((i,j,px_color)) if px_color == color else None
  177.         # Clearing the reference characters from the screen
  178.         self.text(text,x,y,background)
  179.         # Writing the custom-sized font characters on screen
  180.         for px_info in info:
  181.             self.fill_rect(size*px_info[0] - (size-1)*x , size*px_info[1] - (size-1)*y, size, size, px_info[2])  
  182.            
  183. # Additional routines from Tony ===
  184. def centred(text,y,size,color):
  185.     x = int((LCD.width - len(text) * 8 * size)/2)
  186.     LCD.write_text(text,x,y,size,color)        
  187.    
  188. # Colour Mixing Routine
  189. def colour(R,G,B): # Compact method!
  190.     mix1 = ((R&0xF8)*256) + ((G&0xFC)*8) + ((B&0xF8)>>3)
  191.     return  (mix1 & 0xFF) *256  + int((mix1 & 0xFF00) /256) # low nibble first
  192.  
  193. # ==== Main ====  
  194. pwm = PWM(Pin(BL))
  195. pwm.freq(1000)
  196. pwm.duty_u16(32768)#max 65535
  197.  
  198. LCD = LCD_1inch3()
  199.  
  200. LCD.fill(LCD.WHITE)
  201. LCD.show()
  202. # Set up buttons
  203. key0 = Pin(15,Pin.IN,Pin.PULL_UP)
  204. key1 = Pin(17,Pin.IN,Pin.PULL_UP)
  205. key2 = Pin(2 ,Pin.IN,Pin.PULL_UP)
  206. key3= Pin(3 ,Pin.IN,Pin.PULL_UP)
  207.  
  208. centre_x =int(LCD.width/2)
  209. centre_y = int(LCD.height/2)
  210. R = int((LCD.width + LCD.height)/4)
  211. r = int((LCD.width + LCD.height)/8)
  212. LCD.fill(0)
  213. LCD.show()
  214.  
  215. LCD.ellipse(centre_x, centre_y,R,r,colour(255,255,0),1)
  216. LCD.show()
  217. LCD.ellipse(centre_x, centre_y,r,r,colour(0,0,255))
  218. LCD.show()
  219.  
  220. for i in range(r, 0, -1):
  221.     print(i) # <==================== WHY IS THIS NEEDED? ==================
  222.     LCD.ellipse(centre_x, centre_y,i+1, i+1,colour(i*5,i*3,i*4), 1)
  223.     LCD.show()
  224.     time.sleep(0.1)
  225.  
  226. time.sleep(2)
  227. LCD.fill(0)
  228. LCD.show()
  229.  
  230. gc.collect
  231. print()
  232. print(gc.mem_free())
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement