Advertisement
TonyGo

Graphics routine for Pimoroni Pico Explorer

Jan 25th, 2021
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.36 KB | None | 0 0
  1. # Graphics routines for Pimoroni Pico Explorer
  2. # and Raspberry Pi Pico
  3. # Tony Goodhew (arduinolink(AT)gmail.com)
  4. # 25 January 2021
  5. import picoexplorer as display
  6. import time
  7. import math
  8. width = display.get_width()
  9. height = display.get_height()
  10. display_buffer = bytearray(width * height * 2)
  11. display.init(display_buffer)
  12.  
  13. # Set the backlight to 90%
  14. display.set_backlight(0.9)
  15.  
  16. # Define special 5x8 characters - 8 bytes each - 0...7
  17. # Bytes top to bottom, 5 least significant bits only
  18. smiley = [0x00,0x0A,0x00,0x04,0x11,0x0E,0x00,0x00]
  19. sad = [0x00,0x0A,0x00,0x04,0x00,0x0E,0x11,0x00]
  20. heart = [0,0,0,10,31,14,4,0]
  21. b_heart = [0,10,31,0,0,14,4,0]
  22. up_arrow =[0,4,14,21,4,4,0,0]
  23. down_arrow = [0,4,4,21,14,4,0,0]
  24. bits = [128,64,32,16,8,4,2,1] # Powers of 2
  25.  
  26. def mychar(xpos, ypos, pattern): # Print defined character
  27. for line in range(8): # 5x8 characters
  28. for ii in range(5): # Low value bits only
  29. i = ii + 3
  30. dot = pattern[line] & bits[i] # Extract bit
  31. if dot: # Only print WHITE dots
  32. display.pixel(xpos+i*2, ypos+line*2)
  33. display.pixel(xpos+i*2+1, ypos+line*2)
  34. display.pixel(xpos+i*2, ypos+line*2+1)
  35. display.pixel(xpos+i*2+1, ypos+line*2+1)
  36.  
  37. def horiz(l,t,r): # left, top, right
  38. n = r-l+1 # Horizontal line
  39. for i in range(n):
  40. display.pixel(l + i, t )
  41.  
  42. def vert(l,t,b): # left, top, bottom
  43. n = b-t+1 # Vertical line
  44. for i in range(n):
  45. display.pixel(l, t+i)
  46.  
  47. def box(l,t,r,b): # left, top, right, bottom
  48. horiz(l,t,r) # Empty rectangle
  49. horiz(l,b,r)
  50. vert(l,t,b)
  51. vert(r,t,b)
  52.  
  53. def line(x,y,xx,yy): # (x,y) to (xx,yy)
  54. if x > xx:
  55. t = x # Swap co-ordinates if necessary
  56. x = xx
  57. xx = t
  58. t = y
  59. y = yy
  60. yy = t
  61. if xx-x == 0: # Avoid div by zero if vertical
  62. vert(x,min(y,yy),max(y,yy))
  63. else: # Draw line one dot at a time L to R
  64. n=xx-x+1
  65. grad = float((yy-y)/(xx-x)) # Calculate gradient
  66. for i in range(n):
  67. y3 = y + int(grad * i)
  68. display.pixel(x+i,y3) # One dot at a time
  69.  
  70. def ring(cx,cy,rr): # Centre and radius
  71. display.circle(cx,cy,rr)
  72. display.set_pen(0,0,0) # background colour
  73. display.circle(cx,cy,rr-1)
  74.  
  75. def ring2(cx,cy,r): # Centre (x,y), radius
  76. for angle in range(0, 90, 2): # 0 to 90 degrees in 2s
  77. y3=int(r*math.sin(math.radians(angle)))
  78. x3=int(r*math.cos(math.radians(angle)))
  79. display.pixel(cx-x3,cy+y3) # 4 quadrants
  80. display.pixel(cx-x3,cy-y3)
  81. display.pixel(cx+x3,cy+y3)
  82. display.pixel(cx+x3,cy-y3)
  83.  
  84. def show(t): # Update display and short pause
  85. display.update()
  86. time.sleep(t)
  87.  
  88. # ===== Main ======
  89. display.set_pen(255,255,0)
  90. line(10,10,100,100)
  91. show(1)
  92. display.set_pen(255,0,255)
  93. line(10,100,100,10)
  94. show(1)
  95. display.set_pen(0,255,255)
  96. box(0,105,100,205)
  97. show(1)
  98. display.set_pen(255,0,0)
  99. ring(160,50,50)
  100. show(1)
  101. display.set_pen(0,0,255)
  102. ring2(160,160,50)
  103. show(1)
  104. display.text("Tony Goodhew", 15, 220, 240,3)
  105. display.update()
  106. mychar(20, 130, up_arrow) # Defined characters
  107. mychar(40, 130, smiley)
  108. mychar(60, 130, heart)
  109. mychar(20, 160, down_arrow)
  110. mychar(40, 160, sad)
  111. mychar(60, 160, b_heart)
  112. display.update()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement