Advertisement
TonyGo

Waveshare 320x240 2" example

Apr 2nd, 2023
591
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 16.38 KB | Software | 0 0
  1. # WS 2.0" 320x240 Roll 2 dice
  2. # Tony Goodhew 3 May 2022, for thepihut.com
  3. from machine import Pin,SPI,PWM
  4. import framebuf
  5. import utime
  6.  
  7. import math
  8. import random
  9.  
  10. BL = 13
  11. DC = 8
  12. RST = 12
  13. MOSI = 11
  14. SCK = 10
  15. CS = 9
  16.  
  17. class lcd_1inch3(framebuf.FrameBuffer): # For 320x240 display
  18.     def __init__(self):
  19.         self.width = 320
  20.         self.height = 240
  21.        
  22.         self.cs = Pin(CS,Pin.OUT)
  23.         self.rst = Pin(RST,Pin.OUT)
  24.        
  25.         self.cs(1)
  26.         self.spi = SPI(1)
  27.         self.spi = SPI(1,1000_000)
  28.         self.spi = SPI(1,100000_000,polarity=0, phase=0,sck=Pin(SCK),mosi=Pin(MOSI),miso=None)
  29.         self.dc = Pin(DC,Pin.OUT)
  30.         self.dc(1)
  31.         self.buffer = bytearray(self.height * self.width * 2)
  32.         super().__init__(self.buffer, self.width, self.height, framebuf.RGB565)
  33.         self.init_display()
  34.        
  35.         self.RED   =   0x07E0
  36.         self.GREEN =   0x001f
  37.         self.BLUE  =   0xf800
  38.         self.WHITE =   0xffff
  39.         self.BALCK =   0x0000
  40.        
  41.     def write_cmd(self, cmd):
  42.         self.cs(1)
  43.         self.dc(0)
  44.         self.cs(0)
  45.         self.spi.write(bytearray([cmd]))
  46.         self.cs(1)
  47.  
  48.     def write_data(self, buf):
  49.         self.cs(1)
  50.         self.dc(1)
  51.         self.cs(0)
  52.         self.spi.write(bytearray([buf]))
  53.         self.cs(1)
  54.  
  55.     def init_display(self):
  56.         """Initialize display"""  
  57.         self.rst(1)
  58.         self.rst(0)
  59.         self.rst(1)
  60.        
  61.         self.write_cmd(0x36)
  62.         self.write_data(0x70)
  63.  
  64.         self.write_cmd(0x3A)
  65.         self.write_data(0x05)
  66.  
  67.         self.write_cmd(0xB2)
  68.         self.write_data(0x0C)
  69.         self.write_data(0x0C)
  70.         self.write_data(0x00)
  71.         self.write_data(0x33)
  72.         self.write_data(0x33)
  73.  
  74.         self.write_cmd(0xB7)
  75.         self.write_data(0x35)
  76.  
  77.         self.write_cmd(0xBB)
  78.         self.write_data(0x19)
  79.  
  80.         self.write_cmd(0xC0)
  81.         self.write_data(0x2C)
  82.  
  83.         self.write_cmd(0xC2)
  84.         self.write_data(0x01)
  85.  
  86.         self.write_cmd(0xC3)
  87.         self.write_data(0x12)  
  88.  
  89.         self.write_cmd(0xC4)
  90.         self.write_data(0x20)
  91.  
  92.         self.write_cmd(0xC6)
  93.         self.write_data(0x0F)
  94.  
  95.         self.write_cmd(0xD0)
  96.         self.write_data(0xA4)
  97.         self.write_data(0xA1)
  98.  
  99.         self.write_cmd(0xE0)
  100.         self.write_data(0xD0)
  101.         self.write_data(0x04)
  102.         self.write_data(0x0D)
  103.         self.write_data(0x11)
  104.         self.write_data(0x13)
  105.         self.write_data(0x2B)
  106.         self.write_data(0x3F)
  107.         self.write_data(0x54)
  108.         self.write_data(0x4C)
  109.         self.write_data(0x18)
  110.         self.write_data(0x0D)
  111.         self.write_data(0x0B)
  112.         self.write_data(0x1F)
  113.         self.write_data(0x23)
  114.  
  115.         self.write_cmd(0xE1)
  116.         self.write_data(0xD0)
  117.         self.write_data(0x04)
  118.         self.write_data(0x0C)
  119.         self.write_data(0x11)
  120.         self.write_data(0x13)
  121.         self.write_data(0x2C)
  122.         self.write_data(0x3F)
  123.         self.write_data(0x44)
  124.         self.write_data(0x51)
  125.         self.write_data(0x2F)
  126.         self.write_data(0x1F)
  127.         self.write_data(0x1F)
  128.         self.write_data(0x20)
  129.         self.write_data(0x23)
  130.        
  131.         self.write_cmd(0x21)
  132.  
  133.         self.write_cmd(0x11)
  134.  
  135.         self.write_cmd(0x29)
  136.  
  137.     def show(self):
  138.         self.write_cmd(0x2A)
  139.         self.write_data(0x00)
  140.         self.write_data(0x00)
  141.         self.write_data(0x01)
  142.         self.write_data(0x3f)
  143.        
  144.         self.write_cmd(0x2B)
  145.         self.write_data(0x00)
  146.         self.write_data(0x00)
  147.         self.write_data(0x00)
  148.         self.write_data(0xEF)
  149.        
  150.         self.write_cmd(0x2C)
  151.        
  152.         self.cs(1)
  153.         self.dc(1)
  154.         self.cs(0)
  155.         self.spi.write(self.buffer)
  156.         self.cs(1)
  157.        
  158.  
  159. # Colour Mixing Routine
  160. def colour(R,G,B): # Convert RGB888 to RGB565
  161.     return (((G&0b00011100)<<3) +((B&0b11111000)>>3)<<8) + (R&0b11111000)+((G&0b11100000)>>5)
  162.  
  163. def clear(c):
  164.     lcd.fill(c)
  165. # ===========Start of FONTS Section=========================
  166. # Standard ASCII 5x8 font
  167. # https://gist.github.com/tdicola/229b3eeddc12d58fb0bc724a9062aa05
  168. FONT_HEIGHT = 8
  169. FONT_WIDTH = 5
  170. FONT = bytes([
  171.     0x00, 0x00, 0x00, 0x00, 0x00, # <space>
  172.     0x3E, 0x5B, 0x4F, 0x5B, 0x3E,
  173.     0x3E, 0x6B, 0x4F, 0x6B, 0x3E,
  174.     0x1C, 0x3E, 0x7C, 0x3E, 0x1C,
  175.     0x18, 0x3C, 0x7E, 0x3C, 0x18,
  176.     0x1C, 0x57, 0x7D, 0x57, 0x1C,
  177.     0x1C, 0x5E, 0x7F, 0x5E, 0x1C,
  178.     0x00, 0x18, 0x3C, 0x18, 0x00,
  179.     0xFF, 0xE7, 0xC3, 0xE7, 0xFF,
  180.     0x00, 0x18, 0x24, 0x18, 0x00,
  181.     0xFF, 0xE7, 0xDB, 0xE7, 0xFF,
  182.     0x30, 0x48, 0x3A, 0x06, 0x0E,
  183.     0x26, 0x29, 0x79, 0x29, 0x26,
  184.     0x40, 0x7F, 0x05, 0x05, 0x07,
  185.     0x40, 0x7F, 0x05, 0x25, 0x3F,
  186.     0x5A, 0x3C, 0xE7, 0x3C, 0x5A,
  187.     0x7F, 0x3E, 0x1C, 0x1C, 0x08,
  188.     0x08, 0x1C, 0x1C, 0x3E, 0x7F,
  189.     0x14, 0x22, 0x7F, 0x22, 0x14,
  190.     0x5F, 0x5F, 0x00, 0x5F, 0x5F,
  191.     0x06, 0x09, 0x7F, 0x01, 0x7F,
  192.     0x00, 0x66, 0x89, 0x95, 0x6A,
  193.     0x60, 0x60, 0x60, 0x60, 0x60,
  194.     0x94, 0xA2, 0xFF, 0xA2, 0x94,
  195.     0x08, 0x04, 0x7E, 0x04, 0x08, # UP
  196.     0x10, 0x20, 0x7E, 0x20, 0x10, # Down
  197.     0x08, 0x08, 0x2A, 0x1C, 0x08, # Right
  198.     0x08, 0x1C, 0x2A, 0x08, 0x08, # Left
  199.     0x1E, 0x10, 0x10, 0x10, 0x10,
  200.     0x0C, 0x1E, 0x0C, 0x1E, 0x0C,
  201.     0x30, 0x38, 0x3E, 0x38, 0x30,
  202.     0x06, 0x0E, 0x3E, 0x0E, 0x06,
  203.     0x00, 0x00, 0x00, 0x00, 0x00,
  204.     0x00, 0x00, 0x5F, 0x00, 0x00,
  205.     0x00, 0x07, 0x00, 0x07, 0x00,
  206.     0x14, 0x7F, 0x14, 0x7F, 0x14,
  207.     0x24, 0x2A, 0x7F, 0x2A, 0x12,
  208.     0x23, 0x13, 0x08, 0x64, 0x62,
  209.     0x36, 0x49, 0x56, 0x20, 0x50,
  210.     0x00, 0x08, 0x07, 0x03, 0x00,
  211.     0x00, 0x1C, 0x22, 0x41, 0x00,
  212.     0x00, 0x41, 0x22, 0x1C, 0x00,
  213.     0x2A, 0x1C, 0x7F, 0x1C, 0x2A,
  214.     0x08, 0x08, 0x3E, 0x08, 0x08,
  215.     0x00, 0x80, 0x70, 0x30, 0x00,
  216.     0x08, 0x08, 0x08, 0x08, 0x08,
  217.     0x00, 0x00, 0x60, 0x60, 0x00,
  218.     0x20, 0x10, 0x08, 0x04, 0x02,
  219.     0x3E, 0x51, 0x49, 0x45, 0x3E,
  220.     0x00, 0x42, 0x7F, 0x40, 0x00,
  221.     0x72, 0x49, 0x49, 0x49, 0x46,
  222.     0x21, 0x41, 0x49, 0x4D, 0x33,
  223.     0x18, 0x14, 0x12, 0x7F, 0x10,
  224.     0x27, 0x45, 0x45, 0x45, 0x39,
  225.     0x3C, 0x4A, 0x49, 0x49, 0x31,
  226.     0x41, 0x21, 0x11, 0x09, 0x07,
  227.     0x36, 0x49, 0x49, 0x49, 0x36,
  228.     0x46, 0x49, 0x49, 0x29, 0x1E,
  229.     0x00, 0x00, 0x14, 0x00, 0x00,
  230.     0x00, 0x40, 0x34, 0x00, 0x00,
  231.     0x00, 0x08, 0x14, 0x22, 0x41,
  232.     0x14, 0x14, 0x14, 0x14, 0x14,
  233.     0x00, 0x41, 0x22, 0x14, 0x08,
  234.     0x02, 0x01, 0x59, 0x09, 0x06,
  235.     0x3E, 0x41, 0x5D, 0x59, 0x4E,
  236.     0x7C, 0x12, 0x11, 0x12, 0x7C, # A
  237.     0x7F, 0x49, 0x49, 0x49, 0x36,
  238.     0x3E, 0x41, 0x41, 0x41, 0x22,
  239.     0x7F, 0x41, 0x41, 0x41, 0x3E,
  240.     0x7F, 0x49, 0x49, 0x49, 0x41,
  241.     0x7F, 0x09, 0x09, 0x09, 0x01,
  242.     0x3E, 0x41, 0x41, 0x51, 0x73,
  243.     0x7F, 0x08, 0x08, 0x08, 0x7F,
  244.     0x00, 0x41, 0x7F, 0x41, 0x00,
  245.     0x20, 0x40, 0x41, 0x3F, 0x01,
  246.     0x7F, 0x08, 0x14, 0x22, 0x41,
  247.     0x7F, 0x40, 0x40, 0x40, 0x40,
  248.     0x7F, 0x02, 0x1C, 0x02, 0x7F,
  249.     0x7F, 0x04, 0x08, 0x10, 0x7F,
  250.     0x3E, 0x41, 0x41, 0x41, 0x3E,
  251.     0x7F, 0x09, 0x09, 0x09, 0x06,
  252.     0x3E, 0x41, 0x51, 0x21, 0x5E,
  253.     0x7F, 0x09, 0x19, 0x29, 0x46,
  254.     0x26, 0x49, 0x49, 0x49, 0x32,
  255.     0x03, 0x01, 0x7F, 0x01, 0x03,
  256.     0x3F, 0x40, 0x40, 0x40, 0x3F,
  257.     0x1F, 0x20, 0x40, 0x20, 0x1F,
  258.     0x3F, 0x40, 0x38, 0x40, 0x3F,
  259.     0x63, 0x14, 0x08, 0x14, 0x63,
  260.     0x03, 0x04, 0x78, 0x04, 0x03,
  261.     0x61, 0x59, 0x49, 0x4D, 0x43,
  262.     0x00, 0x7F, 0x41, 0x41, 0x41,
  263.     0x02, 0x04, 0x08, 0x10, 0x20,
  264.     0x00, 0x41, 0x41, 0x41, 0x7F,
  265.     0x04, 0x02, 0x01, 0x02, 0x04,
  266.     0x40, 0x40, 0x40, 0x40, 0x40,
  267.     0x00, 0x03, 0x07, 0x08, 0x00,
  268.     0x20, 0x54, 0x54, 0x78, 0x40,
  269.     0x7F, 0x28, 0x44, 0x44, 0x38,
  270.     0x38, 0x44, 0x44, 0x44, 0x28,
  271.     0x38, 0x44, 0x44, 0x28, 0x7F,
  272.     0x38, 0x54, 0x54, 0x54, 0x18,
  273.     0x00, 0x08, 0x7E, 0x09, 0x02,
  274.     0x18, 0xA4, 0xA4, 0x9C, 0x78,
  275.     0x7F, 0x08, 0x04, 0x04, 0x78,
  276.     0x00, 0x44, 0x7D, 0x40, 0x00,
  277.     0x20, 0x40, 0x40, 0x3D, 0x00,
  278.     0x7F, 0x10, 0x28, 0x44, 0x00,
  279.     0x00, 0x41, 0x7F, 0x40, 0x00,
  280.     0x7C, 0x04, 0x78, 0x04, 0x78,
  281.     0x7C, 0x08, 0x04, 0x04, 0x78,
  282.     0x38, 0x44, 0x44, 0x44, 0x38,
  283.     0xFC, 0x18, 0x24, 0x24, 0x18,
  284.     0x18, 0x24, 0x24, 0x18, 0xFC,
  285.     0x7C, 0x08, 0x04, 0x04, 0x08,
  286.     0x48, 0x54, 0x54, 0x54, 0x24,
  287.     0x04, 0x04, 0x3F, 0x44, 0x24,
  288.     0x3C, 0x40, 0x40, 0x20, 0x7C,
  289.     0x1C, 0x20, 0x40, 0x20, 0x1C,
  290.     0x3C, 0x40, 0x30, 0x40, 0x3C,
  291.     0x44, 0x28, 0x10, 0x28, 0x44,
  292.     0x4C, 0x90, 0x90, 0x90, 0x7C,
  293.     0x44, 0x64, 0x54, 0x4C, 0x44,
  294.     0x00, 0x08, 0x36, 0x41, 0x00,
  295.     0x00, 0x00, 0x77, 0x00, 0x00,
  296.     0x00, 0x41, 0x36, 0x08, 0x00,
  297.     0x02, 0x01, 0x02, 0x04, 0x02,
  298.     0x3C, 0x26, 0x23, 0x26, 0x3C,
  299.     0x1E, 0xA1, 0xA1, 0x61, 0x12, # Extension starts here
  300.     0x3A, 0x40, 0x40, 0x20, 0x7A,
  301.     0x38, 0x54, 0x54, 0x55, 0x59,
  302.     0x21, 0x55, 0x55, 0x79, 0x41,
  303.     0x22, 0x54, 0x54, 0x78, 0x42, # a-umlaut
  304.     0x21, 0x55, 0x54, 0x78, 0x40,
  305.     0x20, 0x54, 0x55, 0x79, 0x40,
  306.     0x0C, 0x1E, 0x52, 0x72, 0x12,
  307.     0x39, 0x55, 0x55, 0x55, 0x59,
  308.     0x39, 0x54, 0x54, 0x54, 0x59,
  309.     0x39, 0x55, 0x54, 0x54, 0x58,
  310.     0x00, 0x00, 0x45, 0x7C, 0x41,
  311.     0x00, 0x02, 0x45, 0x7D, 0x42,
  312.     0x00, 0x01, 0x45, 0x7C, 0x40,
  313.     0x7D, 0x12, 0x11, 0x12, 0x7D, # A-umlaut
  314.     0xF0, 0x28, 0x25, 0x28, 0xF0,
  315.     0x7C, 0x54, 0x55, 0x45, 0x00,
  316.     0x20, 0x54, 0x54, 0x7C, 0x54,
  317.     0x7C, 0x0A, 0x09, 0x7F, 0x49,
  318.     0x32, 0x49, 0x49, 0x49, 0x32,
  319.     0x3A, 0x44, 0x44, 0x44, 0x3A, # o-umlaut
  320.     0x32, 0x4A, 0x48, 0x48, 0x30,
  321.     0x3A, 0x41, 0x41, 0x21, 0x7A,
  322.     0x3A, 0x42, 0x40, 0x20, 0x78,
  323.     0x00, 0x9D, 0xA0, 0xA0, 0x7D,
  324.     0x3D, 0x42, 0x42, 0x42, 0x3D, # O-umlaut
  325.     0x3D, 0x40, 0x40, 0x40, 0x3D,
  326.     0x3C, 0x24, 0xFF, 0x24, 0x24,
  327.     0x48, 0x7E, 0x49, 0x43, 0x66,
  328.     0x2B, 0x2F, 0xFC, 0x2F, 0x2B,
  329.     0xFF, 0x09, 0x29, 0xF6, 0x20,
  330.     0xC0, 0x88, 0x7E, 0x09, 0x03,
  331.     0x20, 0x54, 0x54, 0x79, 0x41,
  332.     0x00, 0x00, 0x44, 0x7D, 0x41,
  333.     0x30, 0x48, 0x48, 0x4A, 0x32,
  334.     0x38, 0x40, 0x40, 0x22, 0x7A,
  335.     0x00, 0x7A, 0x0A, 0x0A, 0x72,
  336.     0x7D, 0x0D, 0x19, 0x31, 0x7D,
  337.     0x26, 0x29, 0x29, 0x2F, 0x28,
  338.     0x26, 0x29, 0x29, 0x29, 0x26,
  339.     0x30, 0x48, 0x4D, 0x40, 0x20,
  340.     0x38, 0x08, 0x08, 0x08, 0x08,
  341.     0x08, 0x08, 0x08, 0x08, 0x38,
  342.     0x2F, 0x10, 0xC8, 0xAC, 0xBA,
  343.     0x2F, 0x10, 0x28, 0x34, 0xFA,
  344.     0x00, 0x00, 0x7B, 0x00, 0x00,
  345.     0x08, 0x14, 0x2A, 0x14, 0x22,
  346.     0x22, 0x14, 0x2A, 0x14, 0x08,
  347.     0x55, 0x00, 0x55, 0x00, 0x55, # 176 (25% block) missing in old code
  348.     0xAA, 0x55, 0xAA, 0x55, 0xAA, # 50% block
  349.     0xFF, 0x55, 0xFF, 0x55, 0xFF, # 75% block
  350.     0x00, 0x00, 0x00, 0xFF, 0x00,
  351.     0x10, 0x10, 0x10, 0xFF, 0x00,
  352.     0x14, 0x14, 0x14, 0xFF, 0x00,
  353.     0x10, 0x10, 0xFF, 0x00, 0xFF,
  354.     0x10, 0x10, 0xF0, 0x10, 0xF0,
  355.     0x14, 0x14, 0x14, 0xFC, 0x00,
  356.     0x14, 0x14, 0xF7, 0x00, 0xFF,
  357.     0x00, 0x00, 0xFF, 0x00, 0xFF,
  358.     0x14, 0x14, 0xF4, 0x04, 0xFC,
  359.     0x14, 0x14, 0x17, 0x10, 0x1F,
  360.     0x10, 0x10, 0x1F, 0x10, 0x1F,
  361.     0x14, 0x14, 0x14, 0x1F, 0x00,
  362.     0x10, 0x10, 0x10, 0xF0, 0x00,
  363.     0x00, 0x00, 0x00, 0x1F, 0x10,
  364.     0x10, 0x10, 0x10, 0x1F, 0x10,
  365.     0x10, 0x10, 0x10, 0xF0, 0x10,
  366.     0x00, 0x00, 0x00, 0xFF, 0x10,
  367.     0x10, 0x10, 0x10, 0x10, 0x10,
  368.     0x10, 0x10, 0x10, 0xFF, 0x10,
  369.     0x00, 0x00, 0x00, 0xFF, 0x14,
  370.     0x00, 0x00, 0xFF, 0x00, 0xFF,
  371.     0x00, 0x00, 0x1F, 0x10, 0x17,
  372.     0x00, 0x00, 0xFC, 0x04, 0xF4,
  373.     0x14, 0x14, 0x17, 0x10, 0x17,
  374.     0x14, 0x14, 0xF4, 0x04, 0xF4,
  375.     0x00, 0x00, 0xFF, 0x00, 0xF7,
  376.     0x14, 0x14, 0x14, 0x14, 0x14,
  377.     0x14, 0x14, 0xF7, 0x00, 0xF7,
  378.     0x14, 0x14, 0x14, 0x17, 0x14,
  379.     0x10, 0x10, 0x1F, 0x10, 0x1F,
  380.     0x14, 0x14, 0x14, 0xF4, 0x14,
  381.     0x10, 0x10, 0xF0, 0x10, 0xF0,
  382.     0x00, 0x00, 0x1F, 0x10, 0x1F,
  383.     0x00, 0x00, 0x00, 0x1F, 0x14,
  384.     0x00, 0x00, 0x00, 0xFC, 0x14,
  385.     0x00, 0x00, 0xF0, 0x10, 0xF0,
  386.     0x10, 0x10, 0xFF, 0x10, 0xFF,
  387.     0x14, 0x14, 0x14, 0xFF, 0x14,
  388.     0x10, 0x10, 0x10, 0x1F, 0x00,
  389.     0x00, 0x00, 0x00, 0xF0, 0x10,
  390.     0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
  391.     0xF0, 0xF0, 0xF0, 0xF0, 0xF0,
  392.     0xFF, 0xFF, 0xFF, 0x00, 0x00,
  393.     0x00, 0x00, 0x00, 0xFF, 0xFF,
  394.     0x0F, 0x0F, 0x0F, 0x0F, 0x0F,
  395.     0x38, 0x44, 0x44, 0x38, 0x44, # alpha - Greek characters start here  at 224
  396.     0xFC, 0x4A, 0x4A, 0x4A, 0x34, # sharp-s or beta
  397.     0x7E, 0x02, 0x02, 0x06, 0x06,
  398.     0x02, 0x7E, 0x02, 0x7E, 0x02, # pi
  399.     0x63, 0x55, 0x49, 0x41, 0x63,
  400.     0x38, 0x44, 0x44, 0x3C, 0x04,
  401.     0x40, 0x7E, 0x20, 0x1E, 0x20, # mu
  402.     0x06, 0x02, 0x7E, 0x02, 0x02,
  403.     0x99, 0xA5, 0xE7, 0xA5, 0x99,
  404.     0x1C, 0x2A, 0x49, 0x2A, 0x1C,
  405.     0x4C, 0x72, 0x01, 0x72, 0x4C, # omega
  406.     0x30, 0x4A, 0x4D, 0x4D, 0x30,
  407.     0x30, 0x48, 0x78, 0x48, 0x30,
  408.     0xBC, 0x62, 0x5A, 0x46, 0x3D,
  409.     0x3E, 0x49, 0x49, 0x49, 0x00,
  410.     0x7E, 0x01, 0x01, 0x01, 0x7E, # End of Greek chars
  411.     0x2A, 0x2A, 0x2A, 0x2A, 0x2A, # equivalent to 240
  412.     0x44, 0x44, 0x5F, 0x44, 0x44, # + or -
  413.     0x40, 0x51, 0x4A, 0x44, 0x40, # >=
  414.     0x40, 0x44, 0x4A, 0x51, 0x40, # <=
  415.     0x00, 0x00, 0xFF, 0x01, 0x03, # top of integral
  416.     0xE0, 0x80, 0xFF, 0x00, 0x00, # bottom of integral
  417.     0x08, 0x08, 0x6B, 0x6B, 0x08,
  418.     0x36, 0x12, 0x36, 0x24, 0x36, # approximately
  419.     0x06, 0x0F, 0x09, 0x0F, 0x06,
  420.     0x00, 0x00, 0x18, 0x18, 0x00,
  421.     0x00, 0x00, 0x10, 0x10, 0x00,
  422.     0x30, 0x40, 0xFF, 0x01, 0x01, # sq root
  423.     0x00, 0x1F, 0x01, 0x01, 0x1E, # n superscript
  424.     0x00, 0x19, 0x1D, 0x17, 0x12, # squared (^2)
  425.     0x00, 0x3C, 0x3C, 0x3C, 0x3C,
  426.     0x00, 0x00, 0x00, 0x00, 0x00  # 255 also a <space>
  427. ])
  428.  
  429. def character(asc,xt,yt,sz,r,g,b):  # Single character sz is size: 1 or 2
  430.     cc = colour(r,g,b)
  431.     code = asc * 5    # 5 bytes per character
  432.     for ii in range(5):
  433.         line = FONT[code + ii]
  434.         for yy in range(8):
  435.             if (line >> yy) & 0x1:
  436.                 lcd.pixel(ii*sz+xt,yy*sz+yt,cc)
  437.                 if sz > 1:
  438.                     lcd.pixel(ii*sz+xt+1,yy*sz+yt,cc)
  439.                     lcd.pixel(ii*sz+xt,yy*sz+yt+1,cc)
  440.                     lcd.pixel(ii*sz+xt+1,yy*sz+yt+1,cc)
  441.                 if sz == 3:
  442.                     lcd.pixel(ii*sz+xt,  yy*sz+yt+2,cc)
  443.                     lcd.pixel(ii*sz+xt+1,yy*sz+yt+2,cc)
  444.                     lcd.pixel(ii*sz+xt+2,yy*sz+yt+2,cc)
  445.                     lcd.pixel(ii*sz+xt+2,yy*sz+yt,cc)
  446.                     lcd.pixel(ii*sz+xt+2,yy*sz+yt+1,cc)
  447.                                    
  448. def prnt_st(asci,xx,yy,sz,r,g,b):  # Text string
  449.     if sz == 1: move = 6
  450.     if sz == 2: move = 11
  451.     if sz == 3: move = 17
  452.     for letter in(asci):
  453.         asci = ord(letter)
  454.         character(asci,xx,yy,sz,r,g,b)
  455.         xx = xx + move
  456.  
  457. def cntr_st(s,y,sz,r,g,b): # Centres text on line y
  458.     if sz == 1: w = 6
  459.     if sz == 2: w = 11
  460.     if sz == 3: w = 17
  461.     gap = int((width - len(s) * w)/2)
  462.     prnt_st(s,gap,y,sz,r,g,b)
  463.  
  464. # =========== End of font support routines ===========
  465.        
  466. # ==== Main ====
  467. width = 320
  468. height = 240
  469. pwm = PWM(Pin(BL))
  470. pwm.freq(1000)
  471. pwm.duty_u16(32768)#max 65535
  472.  
  473. lcd = lcd_1inch3()
  474. lcd.fill(0)
  475. lcd.show()
  476. cntr_st(" Roll 2 dice",50,3,255,255,0)
  477. cntr_st("1000 times",100,3,0,255,0)
  478. cntr_st("ThePiHut.com",180,2,255,140,25)
  479. cntr_st('Waveshare Pico-LCD-2" 320x240 pixels',220,1,255,0,0)
  480. lcd.show()
  481. utime.sleep(3)
  482.  
  483. # Dice
  484. clear(0)
  485. lcd.show()
  486. b = colour(0,0,255)
  487. r = colour(255,0,0)
  488. y = colour(255,255,0)
  489. colwidth = 20
  490. totals = [0,0,0,0,0,0,0,0,0,0,0,0,0]
  491. for i in range(2,13):
  492.     lcd.text(str(i),15+i*colwidth,225,y)
  493. lcd.hline(50,220,230,b)
  494. lcd.vline(50,40,180,b)
  495. for i in range(1,191,10):
  496.     lcd.hline(40,39+i,10,b)
  497. for i in range(1,191,50):
  498.     lcd.hline(35,69+i,245,b)
  499. for i in range(0,151,50):
  500.     lcd.text(str(i),8,217-i,b)  
  501.     lcd.text("1000 throws of 2 dice",75,190,r)
  502. for i in range(1000):
  503.     lcd.fill_rect(20,19,220,15,0)
  504.     left = random.randint(1,6)
  505.     prnt_st(str(left),20,20,2,255,0,0)
  506.     right = random.randint(1,6)    
  507.     prnt_st(str(right),50,20,2,255,0,0)
  508.     t = left + right
  509.     prnt_st(str(t),80,20,2,255,255,0)
  510.     lcd.fill_rect(200,19,220,25,0)
  511.     prnt_st(str(i+1),200,20,3,255,0,0)
  512.     totals[t] = totals[t] + 1
  513.     lcd.hline(11+t*colwidth,220-totals[t],colwidth-1,y)
  514.     if i == 800:
  515.         lcd.text("1000 throws of 2 dice",75,190,b)
  516.     lcd.show()
  517.    
  518. clear(0)
  519. prnt_st("Results",50,10,3,0,255,0)
  520. for i in range(2,13):
  521.     s = str("")
  522.     if i < 10:
  523.         s = str(" ")
  524.     s = s + str(i)+"  "
  525.     ss = str(totals[i])
  526.     prnt_st(s,50,15+ i*16,2,255,255,0)
  527.     prnt_st(ss,90,15+ i*16,2,255,255,255)
  528. lcd.show()
  529. utime.sleep(6)
  530. clear(0)
  531. lcd.show()
  532.  
  533.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement