Python RGB Matrix games and animations https://www.xythobuz.de/ledmatrix_v2.html
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

draw.py 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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 = self.fonts[0] # TODO selection of fonts
  28. g = f.glyph(c).draw()
  29. # invert color
  30. g = g.replace(1, 2).replace(0, 1).replace(2, 0)
  31. # render to pixel data
  32. img = Image.frombytes('RGBA',
  33. (g.width(), g.height()),
  34. g.tobytes('RGBA'))
  35. return (img, o)
  36. def drawGlyph(self, g, xOff, yOff):
  37. for x in range(0, g.width):
  38. for y in range(0, g.height):
  39. p = g.getpixel((x, y))
  40. self.gui.set_pixel(xOff + x, yOff + y, p)
  41. def text(self, s, offset = 0):
  42. w = 0
  43. for c in s:
  44. g, y = self.getGlyph(c)
  45. self.drawGlyph(g, -offset + w, y)
  46. w += g.width
  47. return w
  48. class ScrollText:
  49. def __init__(self, g, t, i = 1, s = 100):
  50. self.gui = g
  51. self.drawer = DrawText(self.gui)
  52. self.text = t
  53. self.iterations = i
  54. self.speed = 1.0 / s
  55. self.width = self.drawer.text(self.text)
  56. self.restart()
  57. def restart(self):
  58. self.offset = -self.gui.width
  59. self.last = time.time()
  60. self.count = 0
  61. def finished(self):
  62. return (self.count >= self.iterations)
  63. def draw(self):
  64. if (time.time() - self.last) > self.speed:
  65. self.offset = (self.offset + 1)
  66. if self.offset >= self.width:
  67. self.offset = -self.gui.width
  68. self.count += 1
  69. self.last = time.time()
  70. self.drawer.text(self.text, self.offset)
  71. if __name__ == "__main__":
  72. import platform
  73. t = None
  74. if platform.machine() == "armv7l":
  75. from pi import PiMatrix
  76. t = PiMatrix()
  77. else:
  78. from test import TestGUI
  79. t = TestGUI()
  80. d = ScrollText(t, "Hello, World!")
  81. t.debug_loop(d.draw)