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.

manager.py 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. import time
  9. class Manager:
  10. def __init__(self, g):
  11. self.gui = g
  12. self.screens = []
  13. self.restart()
  14. def restart(self):
  15. self.index = 0
  16. self.done = False
  17. self.lastTime = time.time()
  18. if len(self.screens) > 0:
  19. self.screens[0][0].restart()
  20. def finished(self):
  21. return self.done
  22. def add(self, s, d = None):
  23. v = (s, d)
  24. self.screens.append(v)
  25. def draw(self):
  26. self.screens[self.index][0].draw()
  27. if self.screens[self.index][1] == None:
  28. # let screen decide when it is done
  29. if self.screens[self.index][0].finished():
  30. self.lastTime = time.time()
  31. self.index = (self.index + 1) % len(self.screens)
  32. self.done = (self.index == 0)
  33. self.screens[self.index][0].restart()
  34. else:
  35. # use given timeout
  36. now = time.time()
  37. if ((now - self.lastTime) > self.screens[self.index][1]) or (now < self.lastTime):
  38. self.lastTime = now
  39. self.index = (self.index + 1) % len(self.screens)
  40. self.done = (self.index == 0)
  41. self.screens[self.index][0].restart()
  42. if __name__ == "__main__":
  43. from splash import SplashScreen
  44. from scroll import ScrollText
  45. from solid import Solid
  46. from life import GameOfLife
  47. import util
  48. t = util.getTarget()
  49. splash = SplashScreen(t)
  50. t.loop_start()
  51. splash.draw()
  52. t.loop_end()
  53. m = Manager(t)
  54. m.add(ScrollText(t, "This appears once", "ib8x8u"))
  55. m.add(Solid(t, 1.0))
  56. m.add(ScrollText(t, "And this twice...", "ib8x8u", 2))
  57. m.add(Solid(t, 1.0))
  58. m.add(GameOfLife(t, 20, (0, 255, 0), (0, 0, 0), 20.0, True))
  59. m.add(Solid(t, 1.0))
  60. sub = Manager(t)
  61. sub.add(ScrollText(t, "Hello", "ib8x8u"))
  62. sub.add(Solid(t, 1.0, (0, 255, 0)))
  63. sub.add(ScrollText(t, "World", "ib8x8u"))
  64. sub.add(Solid(t, 1.0, (0, 0, 255)))
  65. m.add(sub)
  66. m.add(Solid(t, 1.0))
  67. m.restart()
  68. t.loop(m.draw)