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.

draw.py 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #!/usr/bin/env python3
  2. # ----------------------------------------------------------------------------
  3. # "THE BEER-WARE LICENSE" (Revision 42):
  4. # <xythobuz@xythobuz.de> wrote this file. As long as you retain this notice
  5. # you can do whatever you want with this stuff. If we meet some day, and you
  6. # think this stuff is worth it, you can buy me a beer in return. Thomas Buck
  7. # ----------------------------------------------------------------------------
  8. from bdfparser import Font
  9. from PIL import Image
  10. import os
  11. import time
  12. class DrawText:
  13. def __init__(self, g):
  14. self.gui = g
  15. scriptDir = os.path.dirname(os.path.realpath(__file__))
  16. fontDir = os.path.join(scriptDir, "fonts")
  17. self.fonts = []
  18. for f in os.listdir(os.fsencode(fontDir)):
  19. filename = os.fsdecode(f)
  20. if not filename.lower().endswith(".bdf"):
  21. continue
  22. font = Font(os.path.join(fontDir, filename))
  23. #print(f"{filename} global size is "
  24. # f"{font.headers['fbbx']} x {font.headers['fbby']} (pixel), "
  25. # f"it contains {len(font)} glyphs.")
  26. # TODO hard-coded per-font offsets
  27. offset = 0
  28. if filename == "iv18x16u.bdf":
  29. offset = 6
  30. data = (font, offset, {})
  31. self.fonts.append(data)
  32. def getGlyph(self, c):
  33. f, o, cache = self.fonts[0] # TODO selection of fonts
  34. # only render glyphs once, cache resulting image data
  35. if not c in cache:
  36. g = f.glyph(c).draw()
  37. # invert color
  38. g = g.replace(1, 2).replace(0, 1).replace(2, 0)
  39. # render to pixel data
  40. img = Image.frombytes('RGBA',
  41. (g.width(), g.height()),
  42. g.tobytes('RGBA'))
  43. cache[c] = img
  44. return (cache[c], o)
  45. def drawGlyph(self, g, xOff, yOff):
  46. if xOff >= self.gui.width:
  47. return
  48. for x in range(0, g.width):
  49. for y in range(0, g.height):
  50. xTarget = xOff + x
  51. if (xTarget < 0) or (xTarget >= self.gui.width):
  52. continue
  53. p = g.getpixel((x, y))
  54. self.gui.set_pixel(xTarget, yOff + y, p)
  55. def text(self, s, offset = 0, earlyAbort = True):
  56. w = 0
  57. for c in s:
  58. xOff = -offset + w
  59. if earlyAbort:
  60. if xOff >= self.gui.width:
  61. break
  62. g, y = self.getGlyph(c)
  63. w += g.width
  64. if xOff >= -10: # some wiggle room so chars dont disappear
  65. self.drawGlyph(g, xOff, y)
  66. return w
  67. class ScrollText:
  68. def __init__(self, g, t, i = 1, s = 100):
  69. self.gui = g
  70. self.drawer = DrawText(self.gui)
  71. self.text = t
  72. self.iterations = i
  73. self.speed = 1.0 / s
  74. self.width = self.drawer.text(self.text, 0, False)
  75. self.restart()
  76. def restart(self):
  77. self.offset = -self.gui.width
  78. self.last = time.time()
  79. self.count = 0
  80. def finished(self):
  81. return (self.count >= self.iterations)
  82. def draw(self):
  83. if (time.time() - self.last) > self.speed:
  84. self.last = time.time()
  85. self.offset = (self.offset + 1)
  86. if self.offset >= self.width:
  87. self.offset = -self.gui.width
  88. self.count += 1
  89. self.drawer.text(self.text, self.offset, True)
  90. if __name__ == "__main__":
  91. import platform
  92. t = None
  93. if platform.machine() == "armv7l":
  94. from pi import PiMatrix
  95. t = PiMatrix()
  96. else:
  97. from test import TestGUI
  98. t = TestGUI()
  99. d = ScrollText(t, "Hello, World!")
  100. t.debug_loop(d.draw)