Python RGB Matrix games and animations https://www.xythobuz.de/ledmatrix_v2.html
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

pico.py 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. class PicoMatrix:
  13. def __init__(self, w = 32, h = 32):
  14. self.width = w # x-axis
  15. self.height = h # y-axis
  16. self.panelW = w # x-axis
  17. self.panelH = h # y-axis
  18. # compatibility to TestGUI
  19. self.multiplier = 1.0
  20. if (w != 32) or (h != 32):
  21. raise RuntimeError("TODO not yet supported")
  22. self.matrix = interstate75.Interstate75(display = interstate75.DISPLAY_INTERSTATE75_32X32)
  23. self.black = self.matrix.display.create_pen(0, 0, 0)
  24. self.white = self.matrix.display.create_pen(255, 255, 255)
  25. self.loop_start() # initialize with blank image for ScrollText constructor
  26. def loop_start(self):
  27. self.matrix.display.set_pen(self.black)
  28. self.matrix.display.clear()
  29. self.matrix.display.set_pen(self.white)
  30. return False # no input, never quit on our own
  31. def loop_end(self):
  32. self.matrix.update()
  33. def loop(self, func = None):
  34. while True:
  35. if self.loop_start():
  36. break
  37. if func != None:
  38. func()
  39. self.loop_end()
  40. self.matrix.stop()
  41. def set_pixel(self, x, y, color):
  42. if (x < 0) or (y < 0) or (x >= self.width) or (y >= self.height):
  43. return
  44. pen = self.matrix.display.create_pen(color[0], color[1], color[2])
  45. self.matrix.display.set_pen(pen)
  46. self.matrix.display.pixel(int(x), int(y))
  47. class PicoText:
  48. def __init__(self, g, fg = (255, 255, 255), bg = (0, 0, 0), c = (0, 255, 0)):
  49. self.gui = g
  50. self.fg = fg
  51. self.bg = bg
  52. self.color = c
  53. def text(self, s, f, offset = 0, earlyAbort = True, yOff = 0, compat = True):
  54. if not earlyAbort:
  55. return self.gui.matrix.display.measure_text(s, scale=1)
  56. pen = self.gui.matrix.display.create_pen(self.fg[0], self.fg[1], self.fg[2])
  57. self.gui.matrix.display.set_pen(pen)
  58. self.gui.matrix.display.set_font(f)
  59. if not compat:
  60. # absolute positioning
  61. x = offset
  62. y = yOff
  63. else:
  64. # centered, like BDF DrawText implementation
  65. fontOff = 0
  66. if f == "bitmap6":
  67. fontOff = 3
  68. elif f == "bitmap8":
  69. fontOff = 4
  70. elif f == "bitmap14_outline":
  71. fontOff = 7
  72. x = -offset
  73. y = int(self.gui.height / 2 - fontOff + yOff)
  74. self.gui.matrix.display.text(s, x, y, scale=1)
  75. if __name__ == "__main__":
  76. import time
  77. t = PicoMatrix(32, 32)
  78. s = PicoText(t)
  79. start = time.time()
  80. i = 0
  81. def helper():
  82. global s, start, i
  83. if (time.time() - start) > 2.0:
  84. start = time.time()
  85. i = (i + 1) % 2
  86. if i == 0:
  87. s.text("Abgj6", "bitmap6", 0, True, 0, False)
  88. s.text("Abdgj8", "bitmap8", 0, True, 6 + 2, False)
  89. s.text("Ag14", "bitmap14_outline", 0, True, 6 + 2 + 8 + 1, False)
  90. else:
  91. s.text("Drinks:", "bitmap8", 0)
  92. t.loop(helper)