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.

mapper.py 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 util
  9. import time
  10. import sys
  11. useNTP = False
  12. try:
  13. import ntptime
  14. useNTP = True
  15. except:
  16. pass
  17. # Does nothing. Take this as parent for new mappers.
  18. class MapperNull:
  19. def __init__(self, g):
  20. self.gui = g
  21. self.width = self.gui.width
  22. self.height = self.gui.height
  23. self.multiplier = self.gui.multiplier
  24. self.panelW = self.gui.panelW
  25. self.panelH = self.gui.panelH
  26. if hasattr(self.gui, "matrix"):
  27. self.matrix = self.gui.matrix
  28. def loop_start(self):
  29. return self.gui.loop_start()
  30. def loop_end(self):
  31. self.gui.loop_end()
  32. def loop(self, func = None):
  33. self.gui.loop(func)
  34. def set_pixel(self, x, y, color):
  35. self.gui.set_pixel(x, y, color)
  36. # For some reason the red and green LEDs on older Pimoroni panels
  37. # are far brighter than on newer panels.
  38. # Adjust this by multiplying rg channels with 0.75, depending
  39. # on hard-corded coordinate ranges.
  40. class MapperColorAdjust(MapperNull):
  41. def set_pixel(self, x, y, color):
  42. # third panel from the left, with 64 <= x < 96,
  43. # is "old" type with brighter LEDs.
  44. # rest of panels to the left and right are less bright.
  45. # so adjust brightness of inner panel rg channels down.
  46. if (x >= (self.gui.panelW * 2)) and (x < (self.gui.panelW * 3)):
  47. color = (int(color[0] * 0.75), int(color[1] * 0.75), color[2])
  48. self.gui.set_pixel(x, y, color)
  49. # This converts a long 128x32 strip to a 64x64 panel.
  50. # The Pi is always connected to d, the last subpanel.
  51. #
  52. # (0, 0) (128, 0)
  53. # a b c d
  54. # (0, 32) (128, 32)
  55. #
  56. # on the hardware is converted to this for the effects:
  57. #
  58. # (0, 0) (64, 0)
  59. # a b
  60. # c d
  61. # (0, 64) (64, 64)
  62. class MapperStripToRect(MapperNull):
  63. def __init__(self, g):
  64. super().__init__(g)
  65. self.width = int(self.gui.width / 2)
  66. self.height = self.gui.height * 2
  67. def set_pixel(self, x, y, color):
  68. if y >= self.gui.height:
  69. x += self.width
  70. y -= self.panelH
  71. self.gui.set_pixel(x, y, color)
  72. # Fetches current time via NTP.
  73. # System time will be in UTC afterwards, not with local time-zone
  74. # offset like when using rshell (eg. when using with a Pico).
  75. #
  76. # Brightness of display will be adjusted according to current time.
  77. # To avoid being too bright at night.
  78. #
  79. # When used with the Interstate75 Pico implementation,
  80. # this needs to be the "first" element of the mapper chain.
  81. # Otherwise special handling for PicoText will not work.
  82. class MapperReduceBrightness(MapperNull):
  83. def __init__(self, g):
  84. super().__init__(g)
  85. self.last = None
  86. self.connected = False
  87. self.refresh = 60 * 60 * 6
  88. self.factor = 1.0
  89. def fetch(self):
  90. self.factor = 1.0
  91. if not useNTP:
  92. return
  93. if not self.connected:
  94. self.connected = util.connectToWiFi()
  95. if self.connected:
  96. now = time.time()
  97. if (self.last == None) or ((now - self.last) >= self.refresh) or (now < self.last):
  98. self.last = now
  99. try:
  100. print("Before sync: " + str(time.localtime()))
  101. ntptime.settime()
  102. print("After sync: " + str(time.localtime()))
  103. except Exception as e:
  104. print()
  105. sys.print_exception(e)
  106. print()
  107. return
  108. # (year, month, day, hour, minute, second, ?, ?)
  109. now = time.localtime()
  110. # 8pm utc == 22pm dst germany
  111. night = (now[0], now[1], now[2], 20, 0, 0, 0, 0)
  112. # 5am utc == 7am dst germany
  113. morning = (now[0], now[1], now[2], 5, 0, 0, 0, 0)
  114. if (now > morning) and (now < night):
  115. self.factor = 1.0
  116. else:
  117. self.factor = 0.25
  118. def adjust(self, color):
  119. return (int(color[0] * self.factor), int(color[1] * self.factor), int(color[2] * self.factor))
  120. def loop_end(self):
  121. super().loop_end()
  122. self.fetch()
  123. def set_pixel(self, x, y, color):
  124. color = self.adjust(color)
  125. self.gui.set_pixel(x, y, color)