Python RGB Matrix games and animations https://www.xythobuz.de/ledmatrix_v2.html
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. #!/usr/bin/env python3
  2. from bdfparser import Font
  3. from PIL import Image
  4. import os
  5. import time
  6. class DrawText:
  7. def __init__(self, g):
  8. self.gui = g
  9. scriptDir = os.path.dirname(os.path.realpath(__file__))
  10. fontDir = os.path.join(scriptDir, "fonts")
  11. self.fonts = []
  12. for f in os.listdir(os.fsencode(fontDir)):
  13. filename = os.fsdecode(f)
  14. if not filename.lower().endswith(".bdf"):
  15. continue
  16. font = Font(os.path.join(fontDir, filename))
  17. #print(f"{filename} global size is "
  18. # f"{font.headers['fbbx']} x {font.headers['fbby']} (pixel), "
  19. # f"it contains {len(font)} glyphs.")
  20. # TODO hard-coded per-font offsets
  21. offset = 0
  22. if filename == "iv18x16u.bdf":
  23. offset = 6
  24. data = (font, offset, {})
  25. self.fonts.append(data)
  26. def getGlyph(self, c):
  27. f, o, cache = self.fonts[0] # TODO selection of fonts
  28. # only render glyphs once, cache resulting image data
  29. if not c in cache:
  30. g = f.glyph(c).draw()
  31. # invert color
  32. g = g.replace(1, 2).replace(0, 1).replace(2, 0)
  33. # render to pixel data
  34. img = Image.frombytes('RGBA',
  35. (g.width(), g.height()),
  36. g.tobytes('RGBA'))
  37. cache[c] = img
  38. return (cache[c], o)
  39. def drawGlyph(self, g, xOff, yOff):
  40. for x in range(0, g.width):
  41. for y in range(0, g.height):
  42. p = g.getpixel((x, y))
  43. self.gui.set_pixel(xOff + x, yOff + y, p)
  44. def text(self, s, offset = 0):
  45. w = 0
  46. for c in s:
  47. g, y = self.getGlyph(c)
  48. self.drawGlyph(g, -offset + w, y)
  49. w += g.width
  50. return w
  51. class ScrollText:
  52. def __init__(self, g, t, i = 1, s = 100):
  53. self.gui = g
  54. self.drawer = DrawText(self.gui)
  55. self.text = t
  56. self.iterations = i
  57. self.speed = 1.0 / s
  58. self.width = self.drawer.text(self.text)
  59. self.restart()
  60. def restart(self):
  61. self.offset = -self.gui.width
  62. self.last = time.time()
  63. self.count = 0
  64. def finished(self):
  65. return (self.count >= self.iterations)
  66. def draw(self):
  67. if (time.time() - self.last) > self.speed:
  68. self.offset = (self.offset + 1)
  69. if self.offset >= self.width:
  70. self.offset = -self.gui.width
  71. self.count += 1
  72. self.last = time.time()
  73. self.drawer.text(self.text, self.offset)
  74. if __name__ == "__main__":
  75. import platform
  76. t = None
  77. if platform.machine() == "armv7l":
  78. from pi import PiMatrix
  79. t = PiMatrix()
  80. else:
  81. from test import TestGUI
  82. t = TestGUI()
  83. d = ScrollText(t, "Hello, World!")
  84. t.debug_loop(d.draw)