Python RGB Matrix games and animations https://www.xythobuz.de/ledmatrix_v2.html
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

pico.py 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #!/usr/bin/env python3
  2. # For the Pimoroni Interstate75 Raspberry Pi Pico RGB LED Matrix interface:
  3. # https://github.com/pimoroni/pimoroni-pico
  4. #
  5. # ----------------------------------------------------------------------------
  6. # "THE BEER-WARE LICENSE" (Revision 42):
  7. # <xythobuz@xythobuz.de> wrote this file. As long as you retain this notice
  8. # you can do whatever you want with this stuff. If we meet some day, and you
  9. # think this stuff is worth it, you can buy me a beer in return. Thomas Buck
  10. # ----------------------------------------------------------------------------
  11. import interstate75
  12. from mapper import MapperReduceBrightness
  13. import time
  14. from machine import Pin
  15. class PicoMatrix:
  16. def __init__(self, w = 32, h = 32):
  17. self.width = w # x-axis
  18. self.height = h # y-axis
  19. self.panelW = w # x-axis
  20. self.panelH = h # y-axis
  21. # compatibility to TestGUI
  22. self.multiplier = 1.0
  23. if (w != 32) or (h != 32):
  24. raise RuntimeError("TODO not yet supported")
  25. mode = interstate75.DISPLAY_INTERSTATE75_32X32
  26. self.matrix = interstate75.Interstate75(display = mode)
  27. self.black = self.matrix.display.create_pen(0, 0, 0)
  28. self.white = self.matrix.display.create_pen(255, 255, 255)
  29. self.ledTime = time.time()
  30. self.led = Pin("LED", Pin.OUT)
  31. self.loop_start() # initialize with blank image for ScrollText constructor
  32. def loop_start(self):
  33. self.matrix.display.set_pen(self.black)
  34. self.matrix.display.clear()
  35. self.matrix.display.set_pen(self.white)
  36. return False # no input, never quit on our own
  37. def loop_end(self):
  38. self.matrix.update()
  39. def loop(self, func = None):
  40. while True:
  41. if self.loop_start():
  42. break
  43. if func != None:
  44. func()
  45. self.loop_end()
  46. now = time.time()
  47. if ((now - self.ledTime) >= 0.5) or (now < self.ledTime):
  48. self.ledTime = now
  49. self.led.toggle()
  50. self.matrix.stop()
  51. def set_pixel(self, x, y, color):
  52. if (x < 0) or (y < 0) or (x >= self.width) or (y >= self.height):
  53. return
  54. pen = self.matrix.display.create_pen(color[0], color[1], color[2])
  55. self.matrix.display.set_pen(pen)
  56. self.matrix.display.pixel(int(x), int(y))
  57. class PicoText:
  58. def __init__(self, g, fg = (255, 255, 255), bg = (0, 0, 0), c = (0, 255, 0)):
  59. self.gui = g
  60. self.fg = fg
  61. self.bg = bg
  62. self.color = c
  63. def text(self, s, f, offset = 0, earlyAbort = True, yOff = 0, compat = True):
  64. if not earlyAbort:
  65. return self.gui.matrix.display.measure_text(s, scale=1)
  66. color = self.fg
  67. if isinstance(self.gui, MapperReduceBrightness):
  68. color = self.gui.adjust(color)
  69. pen = self.gui.matrix.display.create_pen(color[0], color[1], color[2])
  70. self.gui.matrix.display.set_pen(pen)
  71. self.gui.matrix.display.set_font(f)
  72. if not compat:
  73. # absolute positioning
  74. x = offset
  75. y = yOff
  76. else:
  77. # centered, like BDF DrawText implementation
  78. fontOff = 0
  79. if f == "bitmap6":
  80. fontOff = 3
  81. elif f == "bitmap8":
  82. fontOff = 4
  83. elif f == "bitmap14_outline":
  84. fontOff = 7
  85. x = -offset
  86. y = int(self.gui.height / 2 - fontOff + yOff)
  87. self.gui.matrix.display.text(s, x, y, scale=1)
  88. if __name__ == "__main__":
  89. import time
  90. t = PicoMatrix(32, 32)
  91. s = PicoText(t)
  92. start = time.time()
  93. i = 0
  94. def helper():
  95. global s, start, i
  96. now = time.time()
  97. if ((now - start) > 2.0) or (now < start):
  98. start = now
  99. i = (i + 1) % 2
  100. if i == 0:
  101. s.text("Abgj6", "bitmap6", 0, True, 0, False)
  102. s.text("Abdgj8", "bitmap8", 0, True, 6 + 2, False)
  103. s.text("Ag14", "bitmap14_outline", 0, True, 6 + 2 + 8 + 1, False)
  104. else:
  105. s.text("Drinks:", "bitmap8", 0)
  106. t.loop(helper)