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.

draw.py 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. elif filename == "ib8x8u.bdf":
  31. offset = 10
  32. data = (font, offset, {})
  33. self.fonts[filename[:-4]] = data
  34. def getGlyph(self, c, font):
  35. f, o, cache = self.fonts[font]
  36. # only render glyphs once, cache resulting image data
  37. if not c in cache:
  38. g = f.glyph(c).draw()
  39. # invert color
  40. g = g.replace(1, 2).replace(0, 1).replace(2, 0)
  41. # render to pixel data
  42. img = Image.frombytes('RGBA',
  43. (g.width(), g.height()),
  44. g.tobytes('RGBA'))
  45. cache[c] = img
  46. return (cache[c], o)
  47. def drawGlyph(self, g, xOff, yOff):
  48. if xOff >= self.gui.width:
  49. return
  50. for x in range(0, g.width):
  51. for y in range(0, g.height):
  52. xTarget = xOff + x
  53. if (xTarget < 0) or (xTarget >= self.gui.width):
  54. continue
  55. p = g.getpixel((x, y))
  56. self.gui.set_pixel(xTarget, yOff + y, p)
  57. def text(self, s, f, offset = 0, earlyAbort = True, yOff = 0):
  58. w = 0
  59. for c in s:
  60. xOff = -offset + w
  61. if earlyAbort:
  62. if xOff >= self.gui.width:
  63. break
  64. g, y = self.getGlyph(c, f)
  65. w += g.width
  66. if xOff >= -10: # some wiggle room so chars dont disappear
  67. self.drawGlyph(g, xOff, y + yOff)
  68. return w
  69. class ScrollText:
  70. def __init__(self, g, t, f, i = 1, s = 75):
  71. self.gui = g
  72. self.drawer = DrawText(self.gui)
  73. self.text = t
  74. self.font = f
  75. self.iterations = i
  76. self.speed = 1.0 / s
  77. self.width = self.drawer.text(self.text, self.font, 0, False)
  78. self.restart()
  79. def restart(self):
  80. self.offset = -self.gui.width
  81. self.last = time.time()
  82. self.count = 0
  83. def finished(self):
  84. return (self.count >= self.iterations)
  85. def draw(self):
  86. if (time.time() - self.last) > self.speed:
  87. off = (time.time() - self.last) / self.speed
  88. self.offset += int(off)
  89. self.last = time.time()
  90. if self.offset >= self.width:
  91. self.offset = -self.gui.width
  92. self.count += 1
  93. self.drawer.text(self.text, self.font, self.offset, True)
  94. if __name__ == "__main__":
  95. import util
  96. t = util.getTarget()
  97. #d = ScrollText(t, "This is a long scrolling text. Is it too fast or maybe too slow?", "iv18x16u")
  98. d = ScrollText(t, "This is a long scrolling text. Is it too fast or maybe too slow?", "ib8x8u")
  99. t.debug_loop(d.draw)