Advertisement
TonyGo

Untitled

Jan 3rd, 2023
1,066
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 29.15 KB | Source Code | 0 0
  1. from machine import Pin,I2C,SPI,PWM,ADC
  2. import framebuf
  3. import time
  4. import math
  5. import gc
  6. import random
  7.  
  8. I2C_SDA = 6
  9. I2C_SDL = 7
  10.  
  11. DC = 8
  12. CS = 9
  13. SCK = 10
  14. MOSI = 11
  15. RST = 12
  16.  
  17. BL = 25
  18.  
  19. Vbat_Pin = 29
  20. width = 240
  21. height = 240
  22.  
  23.  
  24. class LCD_1inch28(framebuf.FrameBuffer):
  25.     def __init__(self):
  26.         self.width = 240
  27.         self.height = 240
  28.        
  29.         self.cs = Pin(CS,Pin.OUT)
  30.         self.rst = Pin(RST,Pin.OUT)
  31.        
  32.         self.cs(1)
  33.         self.spi = SPI(1,100_000_000,polarity=0, phase=0,sck=Pin(SCK),mosi=Pin(MOSI),miso=None)
  34.         self.dc = Pin(DC,Pin.OUT)
  35.         self.dc(1)
  36.         self.buffer = bytearray(self.height * self.width * 2)
  37.         super().__init__(self.buffer, self.width, self.height, framebuf.RGB565)
  38.         self.init_display()
  39.        
  40.         self.red   =   0x07E0
  41.         self.green =   0x001f
  42.         self.blue  =   0xf800
  43.         self.white =   0xffff
  44.        
  45.         self.fill(self.white)
  46.         self.show()
  47.  
  48.         self.pwm = PWM(Pin(BL))
  49.         self.pwm.freq(5000)
  50.        
  51.     def write_cmd(self, cmd):
  52.         self.cs(1)
  53.         self.dc(0)
  54.         self.cs(0)
  55.         self.spi.write(bytearray([cmd]))
  56.         self.cs(1)
  57.  
  58.     def write_data(self, buf):
  59.         self.cs(1)
  60.         self.dc(1)
  61.         self.cs(0)
  62.         self.spi.write(bytearray([buf]))
  63.         self.cs(1)
  64.     def set_bl_pwm(self,duty):
  65.         self.pwm.duty_u16(duty)#max 65535
  66.     def init_display(self):
  67.         """Initialize display"""  
  68.         self.rst(1)
  69.         time.sleep(0.01)
  70.         self.rst(0)
  71.         time.sleep(0.01)
  72.         self.rst(1)
  73.         time.sleep(0.05)
  74.        
  75.         self.write_cmd(0xEF)
  76.         self.write_cmd(0xEB)
  77.         self.write_data(0x14)
  78.        
  79.         self.write_cmd(0xFE)
  80.         self.write_cmd(0xEF)
  81.  
  82.         self.write_cmd(0xEB)
  83.         self.write_data(0x14)
  84.  
  85.         self.write_cmd(0x84)
  86.         self.write_data(0x40)
  87.  
  88.         self.write_cmd(0x85)
  89.         self.write_data(0xFF)
  90.  
  91.         self.write_cmd(0x86)
  92.         self.write_data(0xFF)
  93.  
  94.         self.write_cmd(0x87)
  95.         self.write_data(0xFF)
  96.  
  97.         self.write_cmd(0x88)
  98.         self.write_data(0x0A)
  99.  
  100.         self.write_cmd(0x89)
  101.         self.write_data(0x21)
  102.  
  103.         self.write_cmd(0x8A)
  104.         self.write_data(0x00)
  105.  
  106.         self.write_cmd(0x8B)
  107.         self.write_data(0x80)
  108.  
  109.         self.write_cmd(0x8C)
  110.         self.write_data(0x01)
  111.  
  112.         self.write_cmd(0x8D)
  113.         self.write_data(0x01)
  114.  
  115.         self.write_cmd(0x8E)
  116.         self.write_data(0xFF)
  117.  
  118.         self.write_cmd(0x8F)
  119.         self.write_data(0xFF)
  120.  
  121.  
  122.         self.write_cmd(0xB6)
  123.         self.write_data(0x00)
  124.         self.write_data(0x20)
  125.  
  126.         self.write_cmd(0x36)
  127.         self.write_data(0x98)
  128.  
  129.         self.write_cmd(0x3A)
  130.         self.write_data(0x05)
  131.  
  132.  
  133.         self.write_cmd(0x90)
  134.         self.write_data(0x08)
  135.         self.write_data(0x08)
  136.         self.write_data(0x08)
  137.         self.write_data(0x08)
  138.  
  139.         self.write_cmd(0xBD)
  140.         self.write_data(0x06)
  141.        
  142.         self.write_cmd(0xBC)
  143.         self.write_data(0x00)
  144.  
  145.         self.write_cmd(0xFF)
  146.         self.write_data(0x60)
  147.         self.write_data(0x01)
  148.         self.write_data(0x04)
  149.  
  150.         self.write_cmd(0xC3)
  151.         self.write_data(0x13)
  152.         self.write_cmd(0xC4)
  153.         self.write_data(0x13)
  154.  
  155.         self.write_cmd(0xC9)
  156.         self.write_data(0x22)
  157.  
  158.         self.write_cmd(0xBE)
  159.         self.write_data(0x11)
  160.  
  161.         self.write_cmd(0xE1)
  162.         self.write_data(0x10)
  163.         self.write_data(0x0E)
  164.  
  165.         self.write_cmd(0xDF)
  166.         self.write_data(0x21)
  167.         self.write_data(0x0c)
  168.         self.write_data(0x02)
  169.  
  170.         self.write_cmd(0xF0)  
  171.         self.write_data(0x45)
  172.         self.write_data(0x09)
  173.         self.write_data(0x08)
  174.         self.write_data(0x08)
  175.         self.write_data(0x26)
  176.         self.write_data(0x2A)
  177.  
  178.         self.write_cmd(0xF1)    
  179.         self.write_data(0x43)
  180.         self.write_data(0x70)
  181.         self.write_data(0x72)
  182.         self.write_data(0x36)
  183.         self.write_data(0x37)  
  184.         self.write_data(0x6F)
  185.  
  186.  
  187.         self.write_cmd(0xF2)  
  188.         self.write_data(0x45)
  189.         self.write_data(0x09)
  190.         self.write_data(0x08)
  191.         self.write_data(0x08)
  192.         self.write_data(0x26)
  193.         self.write_data(0x2A)
  194.  
  195.         self.write_cmd(0xF3)  
  196.         self.write_data(0x43)
  197.         self.write_data(0x70)
  198.         self.write_data(0x72)
  199.         self.write_data(0x36)
  200.         self.write_data(0x37)
  201.         self.write_data(0x6F)
  202.  
  203.         self.write_cmd(0xED)
  204.         self.write_data(0x1B)
  205.         self.write_data(0x0B)
  206.  
  207.         self.write_cmd(0xAE)
  208.         self.write_data(0x77)
  209.        
  210.         self.write_cmd(0xCD)
  211.         self.write_data(0x63)
  212.  
  213.  
  214.         self.write_cmd(0x70)
  215.         self.write_data(0x07)
  216.         self.write_data(0x07)
  217.         self.write_data(0x04)
  218.         self.write_data(0x0E)
  219.         self.write_data(0x0F)
  220.         self.write_data(0x09)
  221.         self.write_data(0x07)
  222.         self.write_data(0x08)
  223.         self.write_data(0x03)
  224.  
  225.         self.write_cmd(0xE8)
  226.         self.write_data(0x34)
  227.  
  228.         self.write_cmd(0x62)
  229.         self.write_data(0x18)
  230.         self.write_data(0x0D)
  231.         self.write_data(0x71)
  232.         self.write_data(0xED)
  233.         self.write_data(0x70)
  234.         self.write_data(0x70)
  235.         self.write_data(0x18)
  236.         self.write_data(0x0F)
  237.         self.write_data(0x71)
  238.         self.write_data(0xEF)
  239.         self.write_data(0x70)
  240.         self.write_data(0x70)
  241.  
  242.         self.write_cmd(0x63)
  243.         self.write_data(0x18)
  244.         self.write_data(0x11)
  245.         self.write_data(0x71)
  246.         self.write_data(0xF1)
  247.         self.write_data(0x70)
  248.         self.write_data(0x70)
  249.         self.write_data(0x18)
  250.         self.write_data(0x13)
  251.         self.write_data(0x71)
  252.         self.write_data(0xF3)
  253.         self.write_data(0x70)
  254.         self.write_data(0x70)
  255.  
  256.         self.write_cmd(0x64)
  257.         self.write_data(0x28)
  258.         self.write_data(0x29)
  259.         self.write_data(0xF1)
  260.         self.write_data(0x01)
  261.         self.write_data(0xF1)
  262.         self.write_data(0x00)
  263.         self.write_data(0x07)
  264.  
  265.         self.write_cmd(0x66)
  266.         self.write_data(0x3C)
  267.         self.write_data(0x00)
  268.         self.write_data(0xCD)
  269.         self.write_data(0x67)
  270.         self.write_data(0x45)
  271.         self.write_data(0x45)
  272.         self.write_data(0x10)
  273.         self.write_data(0x00)
  274.         self.write_data(0x00)
  275.         self.write_data(0x00)
  276.  
  277.         self.write_cmd(0x67)
  278.         self.write_data(0x00)
  279.         self.write_data(0x3C)
  280.         self.write_data(0x00)
  281.         self.write_data(0x00)
  282.         self.write_data(0x00)
  283.         self.write_data(0x01)
  284.         self.write_data(0x54)
  285.         self.write_data(0x10)
  286.         self.write_data(0x32)
  287.         self.write_data(0x98)
  288.  
  289.         self.write_cmd(0x74)
  290.         self.write_data(0x10)
  291.         self.write_data(0x85)
  292.         self.write_data(0x80)
  293.         self.write_data(0x00)
  294.         self.write_data(0x00)
  295.         self.write_data(0x4E)
  296.         self.write_data(0x00)
  297.        
  298.         self.write_cmd(0x98)
  299.         self.write_data(0x3e)
  300.         self.write_data(0x07)
  301.  
  302.         self.write_cmd(0x35)
  303.         self.write_cmd(0x21)
  304.  
  305.         self.write_cmd(0x11)
  306.         time.sleep(0.12)
  307.         self.write_cmd(0x29)
  308.         time.sleep(0.02)
  309.        
  310.         self.write_cmd(0x21)
  311.  
  312.         self.write_cmd(0x11)
  313.  
  314.         self.write_cmd(0x29)
  315.  
  316.     def show(self):
  317.         self.write_cmd(0x2A)
  318.         self.write_data(0x00)
  319.         self.write_data(0x00)
  320.         self.write_data(0x00)
  321.         self.write_data(0xef)
  322.        
  323.         self.write_cmd(0x2B)
  324.         self.write_data(0x00)
  325.         self.write_data(0x00)
  326.         self.write_data(0x00)
  327.         self.write_data(0xEF)
  328.        
  329.         self.write_cmd(0x2C)
  330.        
  331.         self.cs(1)
  332.         self.dc(1)
  333.         self.cs(0)
  334.         self.spi.write(self.buffer)
  335.         self.cs(1)
  336.  
  337.  
  338. class QMI8658(object):
  339.     def __init__(self,address=0X6B):
  340.         self._address = address
  341.         self._bus = I2C(id=1,scl=Pin(I2C_SDL),sda=Pin(I2C_SDA),freq=100_000)
  342.         bRet=self.WhoAmI()
  343.         if bRet :
  344.             self.Read_Revision()
  345.         else    :
  346.             return NULL
  347.         self.Config_apply()
  348.  
  349.     def _read_byte(self,cmd):
  350.         rec=self._bus.readfrom_mem(int(self._address),int(cmd),1)
  351.         return rec[0]
  352.     def _read_block(self, reg, length=1):
  353.         rec=self._bus.readfrom_mem(int(self._address),int(reg),length)
  354.         return rec
  355.     def _read_u16(self,cmd):
  356.         LSB = self._bus.readfrom_mem(int(self._address),int(cmd),1)
  357.         MSB = self._bus.readfrom_mem(int(self._address),int(cmd)+1,1)
  358.         return (MSB[0] << 8) + LSB[0]
  359.     def _write_byte(self,cmd,val):
  360.         self._bus.writeto_mem(int(self._address),int(cmd),bytes([int(val)]))
  361.        
  362.     def WhoAmI(self):
  363.         bRet=False
  364.         if (0x05) == self._read_byte(0x00):
  365.             bRet = True
  366.         return bRet
  367.     def Read_Revision(self):
  368.         return self._read_byte(0x01)
  369.     def Config_apply(self):
  370.         # REG CTRL1
  371.         self._write_byte(0x02,0x60)
  372.         # REG CTRL2 : QMI8658AccRange_8g  and QMI8658AccOdr_1000Hz
  373.         self._write_byte(0x03,0x23)
  374.         # REG CTRL3 : QMI8658GyrRange_512dps and QMI8658GyrOdr_1000Hz
  375.         self._write_byte(0x04,0x53)
  376.         # REG CTRL4 : No
  377.         self._write_byte(0x05,0x00)
  378.         # REG CTRL5 : Enable Gyroscope And Accelerometer Low-Pass Filter
  379.         self._write_byte(0x06,0x11)
  380.         # REG CTRL6 : Disables Motion on Demand.
  381.         self._write_byte(0x07,0x00)
  382.         # REG CTRL7 : Enable Gyroscope And Accelerometer
  383.         self._write_byte(0x08,0x03)
  384.  
  385.     def Read_Raw_XYZ(self):
  386.         xyz=[0,0,0,0,0,0]
  387.         raw_timestamp = self._read_block(0x30,3)
  388.         raw_acc_xyz=self._read_block(0x35,6)
  389.         raw_gyro_xyz=self._read_block(0x3b,6)
  390.         raw_xyz=self._read_block(0x35,12)
  391.         timestamp = (raw_timestamp[2]<<16)|(raw_timestamp[1]<<8)|(raw_timestamp[0])
  392.         for i in range(6):
  393.             # xyz[i]=(raw_acc_xyz[(i*2)+1]<<8)|(raw_acc_xyz[i*2])
  394.             # xyz[i+3]=(raw_gyro_xyz[((i+3)*2)+1]<<8)|(raw_gyro_xyz[(i+3)*2])
  395.             xyz[i] = (raw_xyz[(i*2)+1]<<8)|(raw_xyz[i*2])
  396.             if xyz[i] >= 32767:
  397.                 xyz[i] = xyz[i]-65535
  398.         return xyz
  399.     def Read_XYZ(self):
  400.         xyz=[0,0,0,0,0,0]
  401.         raw_xyz=self.Read_Raw_XYZ()  
  402.         #QMI8658AccRange_8g
  403.         acc_lsb_div=(1<<12)
  404.         #QMI8658GyrRange_512dps
  405.         gyro_lsb_div = 64
  406.         for i in range(3):
  407.             xyz[i]=raw_xyz[i]/acc_lsb_div#(acc_lsb_div/1000.0)
  408.             xyz[i+3]=raw_xyz[i+3]*1.0/gyro_lsb_div
  409.         return xyz
  410.  
  411. def colour(R,G,B): # Convert RGB888 to RGB565
  412.     return (((G&0b00011100)<<3) +((B&0b11111000)>>3)<<8) + (R&0b11111000)+((G&0b11100000)>>5)
  413.  
  414. LCD = LCD_1inch28()            #=============== Initialise the display ===================
  415. LCD.set_bl_pwm(65535)          # Brightness
  416. #qmi8658=QMI8658()             # Initialise gyro accl
  417. #Vbat= ADC(Pin(Vbat_Pin))      # Lipo valtage pin
  418.  
  419. # ========== Start of Triangles code =============
  420. # Modified from https://github.com/SpiderMaf/PiPicoDsply/blob/main/filled-triangles.py
  421. # To work on WaveShare Pi Pico displays
  422. # ========== Version 2 FIXED ! 23 May 2022 ==========
  423. class Point:
  424.     def __init__(self,x,y):
  425.         self.X=x
  426.         self.Y=y
  427.     def __str__(self):
  428.         return "Point(%s,%s)"%(self.X,self.Y)
  429.        
  430. class Triangle:
  431.     def __init__(self,p1,p2,p3):
  432.         self.P1=p1
  433.         self.P2=p2
  434.         self.P3=p3
  435.  
  436.     def __str__(self):
  437.         return "Triangle(%s,%s,%s)"%(self.P1,self.P2,self.P3)
  438.    
  439.     def draw(self):
  440.         print("I should draw now")
  441.         self.fillTri()
  442.     # Filled triangle routines ported from http://www.sunshine2k.de/coding/java/TriangleRasterization/TriangleRasterization.html      
  443.     def sortVerticesAscendingByY(self):    
  444.         if self.P1.Y > self.P2.Y:
  445.             vTmp = self.P1
  446.             self.P1 = self.P2
  447.             self.P2 = vTmp
  448.        
  449.         if self.P1.Y > self.P3.Y:
  450.             vTmp = self.P1
  451.             self.P1 = self.P3
  452.             self.P3 = vTmp
  453.  
  454.         if self.P2.Y > self.P3.Y:
  455.             vTmp = self.P2
  456.             self.P2 = self.P3
  457.             self.P3 = vTmp
  458.        
  459.     def fillTri(self):
  460.         self.sortVerticesAscendingByY()
  461.         if self.P2.Y == self.P3.Y:
  462.             fillBottomFlatTriangle(self.P1, self.P2, self.P3)
  463.         else:
  464.             if self.P1.Y == self.P2.Y:
  465.                 fillTopFlatTriangle(self.P1, self.P2, self.P3)
  466.             else:
  467.                 newx = int(self.P1.X + (float(self.P2.Y - self.P1.Y) / float(self.P3.Y - self.P1.Y)) * (self.P3.X - self.P1.X))
  468.                 newy = self.P2.Y                
  469.                 pTmp = Point( newx,newy )
  470. #                print(pTmp)
  471.                 fillBottomFlatTriangle(self.P1, self.P2, pTmp)
  472.                 fillTopFlatTriangle(self.P2, pTmp, self.P3)
  473.  
  474. def fillBottomFlatTriangle(p1,p2,p3):
  475.    
  476. #    print("BF",p1,p2,p3)
  477.     if p2.Y > p3.Y:
  478.         ty = p3.Y
  479.         p3.Y = p2.Y
  480.         p2.Y = ty
  481.         tx = p3.X
  482.         p3.X = p2.X
  483.         p2.X = tx
  484.         print(p1,p2,p3)
  485.    
  486.     slope1 = float(p2.X - p1.X) / float (p2.Y - p1.Y)
  487.     slope2 = float(p3.X - p1.X) / float (p3.Y - p1.Y)
  488.  
  489.     x1 = p1.X
  490.     x2 = p1.X + 0.5
  491. #    print("B",p1.Y,p2.Y)
  492.     for scanlineY in range(p1.Y,p2.Y):
  493. #        print(scanlineY)
  494. #        LCD.pixel_span(int(x1), scanlineY, int(x2)-int(x1))   # Switch pixel_span() to hline() / Pimoroni to WS
  495.         LCD.hline(int(x1),scanlineY, int(x2)-int(x1),c)        
  496.         LCD.hline(int(x2),scanlineY, -(int(x2)-int(x1)),c)
  497. #        LCD.show()          #                  Here and below        
  498. #        utime.sleep(0.1)    #     <===== Uncomment to see how graphic elements are drawn
  499.         x1 += slope1
  500.         x2 += slope2
  501. #    LCD.show()              #                  LCD.show() and utime.sleep(0.1)
  502. def fillTopFlatTriangle(p1,p2,p3):
  503. #    print("TF",p1,p2,p3)
  504.     slope1 = float(p3.X - p1.X) / float(p3.Y - p1.Y)
  505.     slope2 = float(p3.X - p2.X) / float(p3.Y - p2.Y)
  506.  
  507.     x1 = p3.X
  508.     x2 = p3.X + 0.5
  509. #    print("T",p3.Y,p1.Y-1)
  510.     for scanlineY in range (p3.Y,p1.Y-1,-1):
  511. #        print(scanlineY)
  512. #        LCD.pixel_span(int(x1), scanlineY, int(x2)-int(x1))  # Switch pixel_span() to hline() / Pimoroni to WS
  513.         LCD.hline(int(x1),scanlineY, int(x2)-int(x1)+1,c)        
  514.         LCD.hline(int(x2),scanlineY, -(int(x2)-int(x1)-1),c)
  515. #        LCD.show()
  516. #        utime.sleep(0.1)
  517.         x1 -= slope1
  518.         x2 -= slope2
  519. #    LCD.show()            
  520. # ============== End of Triangles Code ===============
  521.  
  522. # =========== New GFX Routines ============
  523. def clear(c):
  524.     LCD.fill(c)
  525.    
  526. def triangle(x1,y1,x2,y2,x3,y3,c): # Draw outline triangle
  527.     LCD.line(x1,y1,x2,y2,c)
  528.     LCD.line(x2,y2,x3,y3,c)
  529.     LCD.line(x3,y3,x1,y1,c)
  530.    
  531. def tri_filled(x1,y1,x2,y2,x3,y3,c): # Draw filled triangle
  532.  
  533.     t=Triangle(Point(x1,y1),Point(x2,y2),Point(x3,y3)) # Define corners
  534.     t.fillTri() # Call main code block  
  535.  
  536. def circle(x,y,r,c):
  537.     LCD.hline(x-r,y,r*2,c)
  538.     for i in range(1,r):
  539.         a = int(math.sqrt(r*r-i*i)) # Pythagoras!
  540.         LCD.hline(x-a,y+i,a*2,c) # Lower half
  541.         LCD.hline(x-a,y-i,a*2,c) # Upper half
  542.  
  543. def ring(x,y,r,c):
  544.     LCD.pixel(x-r,y,c)
  545.     LCD.pixel(x+r,y,c)
  546.     LCD.pixel(x,y-r,c)
  547.     LCD.pixel(x,y+r,c)
  548.     for i in range(1,r):
  549.         a = int(math.sqrt(r*r-i*i))
  550.         LCD.pixel(x-a,y-i,c)
  551.         LCD.pixel(x+a,y-i,c)
  552.         LCD.pixel(x-a,y+i,c)
  553.         LCD.pixel(x+a,y+i,c)
  554.         LCD.pixel(x-i,y-a,c)
  555.         LCD.pixel(x+i,y-a,c)
  556.         LCD.pixel(x-i,y+a,c)
  557.         LCD.pixel(x+i,y+a,c)
  558. # ===========Start of FONTS Section=========================
  559. # Standard ASCII 5x8 font
  560. # https://gist.github.com/tdicola/229b3eeddc12d58fb0bc724a9062aa05
  561. FONT_HEIGHT = 8
  562. FONT_WIDTH = 5
  563. FONT = bytes([
  564.     0x00, 0x00, 0x00, 0x00, 0x00, # <space>
  565.     0x3E, 0x5B, 0x4F, 0x5B, 0x3E,
  566.     0x3E, 0x6B, 0x4F, 0x6B, 0x3E,
  567.     0x1C, 0x3E, 0x7C, 0x3E, 0x1C,
  568.     0x18, 0x3C, 0x7E, 0x3C, 0x18,
  569.     0x1C, 0x57, 0x7D, 0x57, 0x1C,
  570.     0x1C, 0x5E, 0x7F, 0x5E, 0x1C,
  571.     0x00, 0x18, 0x3C, 0x18, 0x00,
  572.     0xFF, 0xE7, 0xC3, 0xE7, 0xFF,
  573.     0x00, 0x18, 0x24, 0x18, 0x00,
  574.     0xFF, 0xE7, 0xDB, 0xE7, 0xFF,
  575.     0x30, 0x48, 0x3A, 0x06, 0x0E,
  576.     0x26, 0x29, 0x79, 0x29, 0x26,
  577.     0x40, 0x7F, 0x05, 0x05, 0x07,
  578.     0x40, 0x7F, 0x05, 0x25, 0x3F,
  579.     0x5A, 0x3C, 0xE7, 0x3C, 0x5A,
  580.     0x7F, 0x3E, 0x1C, 0x1C, 0x08,
  581.     0x08, 0x1C, 0x1C, 0x3E, 0x7F,
  582.     0x14, 0x22, 0x7F, 0x22, 0x14,
  583.     0x5F, 0x5F, 0x00, 0x5F, 0x5F,
  584.     0x06, 0x09, 0x7F, 0x01, 0x7F,
  585.     0x00, 0x66, 0x89, 0x95, 0x6A,
  586.     0x60, 0x60, 0x60, 0x60, 0x60,
  587.     0x94, 0xA2, 0xFF, 0xA2, 0x94,
  588.     0x08, 0x04, 0x7E, 0x04, 0x08, # UP
  589.     0x10, 0x20, 0x7E, 0x20, 0x10, # Down
  590.     0x08, 0x08, 0x2A, 0x1C, 0x08, # Right
  591.     0x08, 0x1C, 0x2A, 0x08, 0x08, # Left
  592.     0x1E, 0x10, 0x10, 0x10, 0x10,
  593.     0x0C, 0x1E, 0x0C, 0x1E, 0x0C,
  594.     0x30, 0x38, 0x3E, 0x38, 0x30,
  595.     0x06, 0x0E, 0x3E, 0x0E, 0x06,
  596.     0x00, 0x00, 0x00, 0x00, 0x00,
  597.     0x00, 0x00, 0x5F, 0x00, 0x00,
  598.     0x00, 0x07, 0x00, 0x07, 0x00,
  599.     0x14, 0x7F, 0x14, 0x7F, 0x14,
  600.     0x24, 0x2A, 0x7F, 0x2A, 0x12,
  601.     0x23, 0x13, 0x08, 0x64, 0x62,
  602.     0x36, 0x49, 0x56, 0x20, 0x50,
  603.     0x00, 0x08, 0x07, 0x03, 0x00,
  604.     0x00, 0x1C, 0x22, 0x41, 0x00,
  605.     0x00, 0x41, 0x22, 0x1C, 0x00,
  606.     0x2A, 0x1C, 0x7F, 0x1C, 0x2A,
  607.     0x08, 0x08, 0x3E, 0x08, 0x08,
  608.     0x00, 0x80, 0x70, 0x30, 0x00,
  609.     0x08, 0x08, 0x08, 0x08, 0x08,
  610.     0x00, 0x00, 0x60, 0x60, 0x00,
  611.     0x20, 0x10, 0x08, 0x04, 0x02,
  612.     0x3E, 0x51, 0x49, 0x45, 0x3E,
  613.     0x00, 0x42, 0x7F, 0x40, 0x00,
  614.     0x72, 0x49, 0x49, 0x49, 0x46,
  615.     0x21, 0x41, 0x49, 0x4D, 0x33,
  616.     0x18, 0x14, 0x12, 0x7F, 0x10,
  617.     0x27, 0x45, 0x45, 0x45, 0x39,
  618.     0x3C, 0x4A, 0x49, 0x49, 0x31,
  619.     0x41, 0x21, 0x11, 0x09, 0x07,
  620.     0x36, 0x49, 0x49, 0x49, 0x36,
  621.     0x46, 0x49, 0x49, 0x29, 0x1E,
  622.     0x00, 0x00, 0x14, 0x00, 0x00,
  623.     0x00, 0x40, 0x34, 0x00, 0x00,
  624.     0x00, 0x08, 0x14, 0x22, 0x41,
  625.     0x14, 0x14, 0x14, 0x14, 0x14,
  626.     0x00, 0x41, 0x22, 0x14, 0x08,
  627.     0x02, 0x01, 0x59, 0x09, 0x06,
  628.     0x3E, 0x41, 0x5D, 0x59, 0x4E,
  629.     0x7C, 0x12, 0x11, 0x12, 0x7C, # A
  630.     0x7F, 0x49, 0x49, 0x49, 0x36,
  631.     0x3E, 0x41, 0x41, 0x41, 0x22,
  632.     0x7F, 0x41, 0x41, 0x41, 0x3E,
  633.     0x7F, 0x49, 0x49, 0x49, 0x41,
  634.     0x7F, 0x09, 0x09, 0x09, 0x01,
  635.     0x3E, 0x41, 0x41, 0x51, 0x73,
  636.     0x7F, 0x08, 0x08, 0x08, 0x7F,
  637.     0x00, 0x41, 0x7F, 0x41, 0x00,
  638.     0x20, 0x40, 0x41, 0x3F, 0x01,
  639.     0x7F, 0x08, 0x14, 0x22, 0x41,
  640.     0x7F, 0x40, 0x40, 0x40, 0x40,
  641.     0x7F, 0x02, 0x1C, 0x02, 0x7F,
  642.     0x7F, 0x04, 0x08, 0x10, 0x7F,
  643.     0x3E, 0x41, 0x41, 0x41, 0x3E,
  644.     0x7F, 0x09, 0x09, 0x09, 0x06,
  645.     0x3E, 0x41, 0x51, 0x21, 0x5E,
  646.     0x7F, 0x09, 0x19, 0x29, 0x46,
  647.     0x26, 0x49, 0x49, 0x49, 0x32,
  648.     0x03, 0x01, 0x7F, 0x01, 0x03,
  649.     0x3F, 0x40, 0x40, 0x40, 0x3F,
  650.     0x1F, 0x20, 0x40, 0x20, 0x1F,
  651.     0x3F, 0x40, 0x38, 0x40, 0x3F,
  652.     0x63, 0x14, 0x08, 0x14, 0x63,
  653.     0x03, 0x04, 0x78, 0x04, 0x03,
  654.     0x61, 0x59, 0x49, 0x4D, 0x43,
  655.     0x00, 0x7F, 0x41, 0x41, 0x41,
  656.     0x02, 0x04, 0x08, 0x10, 0x20,
  657.     0x00, 0x41, 0x41, 0x41, 0x7F,
  658.     0x04, 0x02, 0x01, 0x02, 0x04,
  659.     0x40, 0x40, 0x40, 0x40, 0x40,
  660.     0x00, 0x03, 0x07, 0x08, 0x00,
  661.     0x20, 0x54, 0x54, 0x78, 0x40,
  662.     0x7F, 0x28, 0x44, 0x44, 0x38,
  663.     0x38, 0x44, 0x44, 0x44, 0x28,
  664.     0x38, 0x44, 0x44, 0x28, 0x7F,
  665.     0x38, 0x54, 0x54, 0x54, 0x18,
  666.     0x00, 0x08, 0x7E, 0x09, 0x02,
  667.     0x18, 0xA4, 0xA4, 0x9C, 0x78,
  668.     0x7F, 0x08, 0x04, 0x04, 0x78,
  669.     0x00, 0x44, 0x7D, 0x40, 0x00,
  670.     0x20, 0x40, 0x40, 0x3D, 0x00,
  671.     0x7F, 0x10, 0x28, 0x44, 0x00,
  672.     0x00, 0x41, 0x7F, 0x40, 0x00,
  673.     0x7C, 0x04, 0x78, 0x04, 0x78,
  674.     0x7C, 0x08, 0x04, 0x04, 0x78,
  675.     0x38, 0x44, 0x44, 0x44, 0x38,
  676.     0xFC, 0x18, 0x24, 0x24, 0x18,
  677.     0x18, 0x24, 0x24, 0x18, 0xFC,
  678.     0x7C, 0x08, 0x04, 0x04, 0x08,
  679.     0x48, 0x54, 0x54, 0x54, 0x24,
  680.     0x04, 0x04, 0x3F, 0x44, 0x24,
  681.     0x3C, 0x40, 0x40, 0x20, 0x7C,
  682.     0x1C, 0x20, 0x40, 0x20, 0x1C,
  683.     0x3C, 0x40, 0x30, 0x40, 0x3C,
  684.     0x44, 0x28, 0x10, 0x28, 0x44,
  685.     0x4C, 0x90, 0x90, 0x90, 0x7C,
  686.     0x44, 0x64, 0x54, 0x4C, 0x44,
  687.     0x00, 0x08, 0x36, 0x41, 0x00,
  688.     0x00, 0x00, 0x77, 0x00, 0x00,
  689.     0x00, 0x41, 0x36, 0x08, 0x00,
  690.     0x02, 0x01, 0x02, 0x04, 0x02,
  691.     0x3C, 0x26, 0x23, 0x26, 0x3C,
  692.     0x1E, 0xA1, 0xA1, 0x61, 0x12, # Extension starts here
  693.     0x3A, 0x40, 0x40, 0x20, 0x7A,
  694.     0x38, 0x54, 0x54, 0x55, 0x59,
  695.     0x21, 0x55, 0x55, 0x79, 0x41,
  696.     0x22, 0x54, 0x54, 0x78, 0x42, # a-umlaut
  697.     0x21, 0x55, 0x54, 0x78, 0x40,
  698.     0x20, 0x54, 0x55, 0x79, 0x40,
  699.     0x0C, 0x1E, 0x52, 0x72, 0x12,
  700.     0x39, 0x55, 0x55, 0x55, 0x59,
  701.     0x39, 0x54, 0x54, 0x54, 0x59,
  702.     0x39, 0x55, 0x54, 0x54, 0x58,
  703.     0x00, 0x00, 0x45, 0x7C, 0x41,
  704.     0x00, 0x02, 0x45, 0x7D, 0x42,
  705.     0x00, 0x01, 0x45, 0x7C, 0x40,
  706.     0x7D, 0x12, 0x11, 0x12, 0x7D, # A-umlaut
  707.     0xF0, 0x28, 0x25, 0x28, 0xF0,
  708.     0x7C, 0x54, 0x55, 0x45, 0x00,
  709.     0x20, 0x54, 0x54, 0x7C, 0x54,
  710.     0x7C, 0x0A, 0x09, 0x7F, 0x49,
  711.     0x32, 0x49, 0x49, 0x49, 0x32,
  712.     0x3A, 0x44, 0x44, 0x44, 0x3A, # o-umlaut
  713.     0x32, 0x4A, 0x48, 0x48, 0x30,
  714.     0x3A, 0x41, 0x41, 0x21, 0x7A,
  715.     0x3A, 0x42, 0x40, 0x20, 0x78,
  716.     0x00, 0x9D, 0xA0, 0xA0, 0x7D,
  717.     0x3D, 0x42, 0x42, 0x42, 0x3D, # O-umlaut
  718.     0x3D, 0x40, 0x40, 0x40, 0x3D,
  719.     0x3C, 0x24, 0xFF, 0x24, 0x24,
  720.     0x48, 0x7E, 0x49, 0x43, 0x66,
  721.     0x2B, 0x2F, 0xFC, 0x2F, 0x2B,
  722.     0xFF, 0x09, 0x29, 0xF6, 0x20,
  723.     0xC0, 0x88, 0x7E, 0x09, 0x03,
  724.     0x20, 0x54, 0x54, 0x79, 0x41,
  725.     0x00, 0x00, 0x44, 0x7D, 0x41,
  726.     0x30, 0x48, 0x48, 0x4A, 0x32,
  727.     0x38, 0x40, 0x40, 0x22, 0x7A,
  728.     0x00, 0x7A, 0x0A, 0x0A, 0x72,
  729.     0x7D, 0x0D, 0x19, 0x31, 0x7D,
  730.     0x26, 0x29, 0x29, 0x2F, 0x28,
  731.     0x26, 0x29, 0x29, 0x29, 0x26,
  732.     0x30, 0x48, 0x4D, 0x40, 0x20,
  733.     0x38, 0x08, 0x08, 0x08, 0x08,
  734.     0x08, 0x08, 0x08, 0x08, 0x38,
  735.     0x2F, 0x10, 0xC8, 0xAC, 0xBA,
  736.     0x2F, 0x10, 0x28, 0x34, 0xFA,
  737.     0x00, 0x00, 0x7B, 0x00, 0x00,
  738.     0x08, 0x14, 0x2A, 0x14, 0x22,
  739.     0x22, 0x14, 0x2A, 0x14, 0x08,
  740.     0x55, 0x00, 0x55, 0x00, 0x55, # 176 (25% block) missing in old code
  741.     0xAA, 0x55, 0xAA, 0x55, 0xAA, # 50% block
  742.     0xFF, 0x55, 0xFF, 0x55, 0xFF, # 75% block
  743.     0x00, 0x00, 0x00, 0xFF, 0x00,
  744.     0x10, 0x10, 0x10, 0xFF, 0x00,
  745.     0x14, 0x14, 0x14, 0xFF, 0x00,
  746.     0x10, 0x10, 0xFF, 0x00, 0xFF,
  747.     0x10, 0x10, 0xF0, 0x10, 0xF0,
  748.     0x14, 0x14, 0x14, 0xFC, 0x00,
  749.     0x14, 0x14, 0xF7, 0x00, 0xFF,
  750.     0x00, 0x00, 0xFF, 0x00, 0xFF,
  751.     0x14, 0x14, 0xF4, 0x04, 0xFC,
  752.     0x14, 0x14, 0x17, 0x10, 0x1F,
  753.     0x10, 0x10, 0x1F, 0x10, 0x1F,
  754.     0x14, 0x14, 0x14, 0x1F, 0x00,
  755.     0x10, 0x10, 0x10, 0xF0, 0x00,
  756.     0x00, 0x00, 0x00, 0x1F, 0x10,
  757.     0x10, 0x10, 0x10, 0x1F, 0x10,
  758.     0x10, 0x10, 0x10, 0xF0, 0x10,
  759.     0x00, 0x00, 0x00, 0xFF, 0x10,
  760.     0x10, 0x10, 0x10, 0x10, 0x10,
  761.     0x10, 0x10, 0x10, 0xFF, 0x10,
  762.     0x00, 0x00, 0x00, 0xFF, 0x14,
  763.     0x00, 0x00, 0xFF, 0x00, 0xFF,
  764.     0x00, 0x00, 0x1F, 0x10, 0x17,
  765.     0x00, 0x00, 0xFC, 0x04, 0xF4,
  766.     0x14, 0x14, 0x17, 0x10, 0x17,
  767.     0x14, 0x14, 0xF4, 0x04, 0xF4,
  768.     0x00, 0x00, 0xFF, 0x00, 0xF7,
  769.     0x14, 0x14, 0x14, 0x14, 0x14,
  770.     0x14, 0x14, 0xF7, 0x00, 0xF7,
  771.     0x14, 0x14, 0x14, 0x17, 0x14,
  772.     0x10, 0x10, 0x1F, 0x10, 0x1F,
  773.     0x14, 0x14, 0x14, 0xF4, 0x14,
  774.     0x10, 0x10, 0xF0, 0x10, 0xF0,
  775.     0x00, 0x00, 0x1F, 0x10, 0x1F,
  776.     0x00, 0x00, 0x00, 0x1F, 0x14,
  777.     0x00, 0x00, 0x00, 0xFC, 0x14,
  778.     0x00, 0x00, 0xF0, 0x10, 0xF0,
  779.     0x10, 0x10, 0xFF, 0x10, 0xFF,
  780.     0x14, 0x14, 0x14, 0xFF, 0x14,
  781.     0x10, 0x10, 0x10, 0x1F, 0x00,
  782.     0x00, 0x00, 0x00, 0xF0, 0x10,
  783.     0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  784.     0xF0, 0xF0, 0xF0, 0xF0, 0xF0,
  785.     0xFF, 0xFF, 0xFF, 0x00, 0x00,
  786.     0x00, 0x00, 0x00, 0xFF, 0xFF,
  787.     0x0F, 0x0F, 0x0F, 0x0F, 0x0F,
  788.     0x38, 0x44, 0x44, 0x38, 0x44, # alpha - Greek characters start here  at 224
  789.     0xFC, 0x4A, 0x4A, 0x4A, 0x34, # sharp-s or beta
  790.     0x7E, 0x02, 0x02, 0x06, 0x06,
  791.     0x02, 0x7E, 0x02, 0x7E, 0x02, # pi
  792.     0x63, 0x55, 0x49, 0x41, 0x63,
  793.     0x38, 0x44, 0x44, 0x3C, 0x04,
  794.     0x40, 0x7E, 0x20, 0x1E, 0x20, # mu
  795.     0x06, 0x02, 0x7E, 0x02, 0x02,
  796.     0x99, 0xA5, 0xE7, 0xA5, 0x99,
  797.     0x1C, 0x2A, 0x49, 0x2A, 0x1C,
  798.     0x4C, 0x72, 0x01, 0x72, 0x4C, # omega
  799.     0x30, 0x4A, 0x4D, 0x4D, 0x30,
  800.     0x30, 0x48, 0x78, 0x48, 0x30,
  801.     0xBC, 0x62, 0x5A, 0x46, 0x3D,
  802.     0x3E, 0x49, 0x49, 0x49, 0x00,
  803.     0x7E, 0x01, 0x01, 0x01, 0x7E, # End of Greek chars
  804.     0x2A, 0x2A, 0x2A, 0x2A, 0x2A, # equivalent to 240
  805.     0x44, 0x44, 0x5F, 0x44, 0x44, # + or -
  806.     0x40, 0x51, 0x4A, 0x44, 0x40, # >=
  807.     0x40, 0x44, 0x4A, 0x51, 0x40, # <=
  808.     0x00, 0x00, 0xFF, 0x01, 0x03, # top of integral
  809.     0xE0, 0x80, 0xFF, 0x00, 0x00, # bottom of integral
  810.     0x08, 0x08, 0x6B, 0x6B, 0x08,
  811.     0x36, 0x12, 0x36, 0x24, 0x36, # approximately
  812.     0x06, 0x0F, 0x09, 0x0F, 0x06, # Degree
  813.     0x00, 0x00, 0x18, 0x18, 0x00,
  814.     0x00, 0x00, 0x10, 0x10, 0x00,
  815.     0x30, 0x40, 0xFF, 0x01, 0x01, # sq root
  816.     0x00, 0x1F, 0x01, 0x01, 0x1E, # n superscript
  817.     0x00, 0x19, 0x1D, 0x17, 0x12, # squared (^2)
  818.     0x00, 0x3C, 0x3C, 0x3C, 0x3C,
  819.     0x00, 0x00, 0x00, 0x00, 0x00  # 255 also a <space>
  820. ])
  821.  
  822. def character(asc,xt,yt,sz,r,g,b):  # Single character sz is size: 1 or 2
  823.     cc = colour(r,g,b)
  824.     code = asc * 5    # 5 bytes per character
  825.     for ii in range(5):
  826.         line = FONT[code + ii]
  827.         for yy in range(8):
  828.             if (line >> yy) & 0x1:
  829.                 LCD.pixel(ii*sz+xt,yy*sz+yt,cc)
  830.                 if sz > 1:
  831.                     LCD.pixel(ii*sz+xt+1,yy*sz+yt,cc)
  832.                     LCD.pixel(ii*sz+xt,yy*sz+yt+1,cc)
  833.                     LCD.pixel(ii*sz+xt+1,yy*sz+yt+1,cc)
  834.                 if sz == 3:
  835.                     LCD.pixel(ii*sz+xt,  yy*sz+yt+2,cc)
  836.                     LCD.pixel(ii*sz+xt+1,yy*sz+yt+2,cc)
  837.                     LCD.pixel(ii*sz+xt+2,yy*sz+yt+2,cc)
  838.                     LCD.pixel(ii*sz+xt+2,yy*sz+yt,cc)
  839.                     LCD.pixel(ii*sz+xt+2,yy*sz+yt+1,cc)
  840.                                    
  841. def prnt_st(asci,xx,yy,sz,r,g,b):  # Text string
  842.     if sz == 1: move = 6
  843.     if sz == 2: move = 11
  844.     if sz == 3: move = 17
  845.     for letter in(asci):
  846.         asci = ord(letter)
  847.         character(asci,xx,yy,sz,r,g,b)
  848.         xx = xx + move
  849.  
  850. def cntr_st(s,y,sz,r,g,b): # Centres text on line y
  851.     if sz == 1: w = 6
  852.     if sz == 2: w = 11
  853.     if sz == 3: w = 17
  854.     gap = int((width - len(s) * w)/2)
  855.     prnt_st(s,gap,y,sz,r,g,b)
  856.  
  857. # =========== End of font support routines ===========
  858.    
  859. # ==== Board now setup ========== MAIN BELOW====================
  860. clear(0)
  861.  
  862. # Draw the dial - once only
  863. LCD.fill(colour(255,0,0))                    # Fill screen red
  864. circle(120,120,100,colour(0,0,255))          # Blue circle
  865. LCD.fill_rect(0,121,240,120,colour(255,0,0)) # Overwrite lower half blue circle with red
  866. r = 103                                      # Tick outer radius
  867. for p in range(0,101,10):                    # White Ticks at 10 % intervals
  868.     theta = p * 1.8                          # Angle above horizontal in degrees
  869.     theta_rad = math.radians(theta)          # Angle in radians
  870.     yn = -int(r * math.sin(theta_rad))       # Calculate outer tick coordinates
  871.     xn = -int(r * math.cos(theta_rad))
  872.     LCD.line(120,120,120+xn,120+yn,colour(255,255,255)) # Draw the tick from centre
  873. circle(120,120,75,colour(255,0,0))           # Overwrite inner tick lines
  874. LCD.show()
  875.  
  876. # Counting up in fives
  877. r = 74 # Length of hand
  878. old_xn = 0
  879. old_yn = 0
  880. for p in range(0,101,5): # Percentages at 5 % interval
  881.     theta = p * 1.8
  882.     LCD.line(120,120,120+old_xn,120+old_yn,colour(255,0,0)) # Overwrite the old hand
  883.     LCD.fill_rect(0,121,240,120,colour(255,0,0))            # Clear text area
  884.     theta_rad = math.radians(theta)              
  885.     cntr_st(str(p) +" %",130,3,255,255,255)                 # Percentage value as text
  886.     yn = -int(r * math.sin(theta_rad))
  887.     xn = -int(r * math.cos(theta_rad))
  888.     LCD.line(120,120,120+xn,120+yn,colour(0,0,255))         # Draw the new hand
  889.     LCD.show()                                              # Update screen
  890.     time.sleep(0.1)                                         # Delay
  891.     old_xn = xn                                             # Store current hand end corordinates
  892.     old_yn = yn                                             #  for overwriting in next loop pass
  893.    
  894. # Random values
  895. for c in range(25):
  896.     p = random.randint(0,100)
  897.     theta = p * 1.8
  898.     LCD.line(120,120,120+old_xn,120+old_yn,colour(255,0,0)) # Overwrite the old hand
  899.     LCD.fill_rect(0,121,240,120,colour(255,0,0))            # Clear text area
  900.     theta_rad = math.radians(theta)  
  901.     cntr_st(str(p) +" %",130,3,255,255,255)                 # Centres text on line y
  902.     yn = -int(r * math.sin(theta_rad))
  903.     xn = -int(r * math.cos(theta_rad))
  904.     LCD.line(120,120,120+xn,120+yn,colour(0,255,0))         # Draw the new hand
  905.     LCD.show()                                              # Update screen
  906.     time.sleep(0.3)                                         # Delay
  907.     old_xn = xn                                             # Store current hand end corordinates
  908.     old_yn = yn                                             #  for overwriting in next loop pass    
  909.    
Tags: GGraphics
Advertisement
Comments
Add Comment
Please, Sign In to add comment
Advertisement