Python RGB Matrix games and animations https://www.xythobuz.de/ledmatrix_v2.html
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

pico.py 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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, mem32
  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. # For all matrix pins r0, g0, b0, r1, g1, b1, a, b, c, d, e:
  32. # reduce drive-strength to minimum 2mA and set slew to slow.
  33. # Attempt to reduce ghosting on display. Not really helping :(
  34. io_base = 0x4001c000
  35. io_off = 0x04
  36. io_inc = 0x04
  37. for p in range(0, 11):
  38. reg = io_base + (p * io_inc) + io_off
  39. val = mem32[reg]
  40. val = val & 0xFFFFFFCE
  41. mem32[reg] = val
  42. self.loop_start() # initialize with blank image for ScrollText constructor
  43. def loop_start(self):
  44. self.matrix.display.set_pen(self.black)
  45. self.matrix.display.clear()
  46. self.matrix.display.set_pen(self.white)
  47. return False # no input, never quit on our own
  48. def loop_end(self):
  49. self.matrix.update()
  50. def loop(self, func = None):
  51. while True:
  52. if self.loop_start():
  53. break
  54. if func != None:
  55. func()
  56. self.loop_end()
  57. now = time.time()
  58. if ((now - self.ledTime) >= 0.5) or (now < self.ledTime):
  59. self.ledTime = now
  60. self.led.toggle()
  61. self.matrix.stop()
  62. def set_pixel(self, x, y, color):
  63. if (x < 0) or (y < 0) or (x >= self.width) or (y >= self.height):
  64. return
  65. pen = self.matrix.display.create_pen(color[0], color[1], color[2])
  66. self.matrix.display.set_pen(pen)
  67. self.matrix.display.pixel(int(x), int(y))
  68. class PicoText:
  69. def __init__(self, g, fg = (255, 255, 255), bg = (0, 0, 0), c = (0, 255, 0)):
  70. self.gui = g
  71. self.fg = fg
  72. self.bg = bg
  73. self.color = c
  74. def text(self, s, f, offset = 0, earlyAbort = True, yOff = 0, compat = True):
  75. if not earlyAbort:
  76. return self.gui.matrix.display.measure_text(s, scale=1)
  77. color = self.fg
  78. if isinstance(self.gui, MapperReduceBrightness):
  79. color = self.gui.adjust(color)
  80. pen = self.gui.matrix.display.create_pen(color[0], color[1], color[2])
  81. self.gui.matrix.display.set_pen(pen)
  82. self.gui.matrix.display.set_font(f)
  83. if not compat:
  84. # absolute positioning
  85. x = offset
  86. y = yOff
  87. else:
  88. # centered, like BDF DrawText implementation
  89. fontOff = 0
  90. if f == "bitmap6":
  91. fontOff = 3
  92. elif f == "bitmap8":
  93. fontOff = 4
  94. elif f == "bitmap14_outline":
  95. fontOff = 7
  96. x = -offset
  97. y = int(self.gui.height / 2 - fontOff + yOff)
  98. self.gui.matrix.display.text(s, x, y, scale=1)
  99. if __name__ == "__main__":
  100. import time
  101. t = PicoMatrix(32, 32)
  102. s = PicoText(t)
  103. start = time.time()
  104. i = 0
  105. def helper():
  106. global s, start, i
  107. now = time.time()
  108. if ((now - start) > 2.0) or (now < start):
  109. start = now
  110. i = (i + 1) % 2
  111. if i == 0:
  112. s.text("Abgj6", "bitmap6", 0, True, 0, False)
  113. s.text("Abdgj8", "bitmap8", 0, True, 6 + 2, False)
  114. s.text("Ag14", "bitmap14_outline", 0, True, 6 + 2 + 8 + 1, False)
  115. else:
  116. s.text("Drinks:", "bitmap8", 0)
  117. t.loop(helper)