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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. #!/usr/bin/env python3
  2. # For the Pimoroni Interstate75 Raspberry Pi Pico RGB LED Matrix interface:
  3. # https://github.com/pimoroni/pimoroni-pico
  4. #
  5. # ----------------------------------------------------------------------------
  6. # "THE BEER-WARE LICENSE" (Revision 42):
  7. # <xythobuz@xythobuz.de> wrote this file. As long as you retain this notice
  8. # you can do whatever you want with this stuff. If we meet some day, and you
  9. # think this stuff is worth it, you can buy me a beer in return. Thomas Buck
  10. # ----------------------------------------------------------------------------
  11. import interstate75
  12. import hub75
  13. from mapper import MapperReduceBrightness
  14. import time
  15. from machine import Pin, ADC
  16. import math
  17. # https://github.com/pimoroni/pimoroni-pico/blob/main/micropython/examples/interstate75/75W/clock.py
  18. @micropython.native # noqa: F821
  19. def from_hsv(h, s, v):
  20. i = math.floor(h * 6.0)
  21. f = h * 6.0 - i
  22. v *= 255.0
  23. p = v * (1.0 - s)
  24. q = v * (1.0 - f * s)
  25. t = v * (1.0 - (1.0 - f) * s)
  26. i = int(i) % 6
  27. if i == 0:
  28. return int(v), int(t), int(p)
  29. if i == 1:
  30. return int(q), int(v), int(p)
  31. if i == 2:
  32. return int(p), int(v), int(t)
  33. if i == 3:
  34. return int(p), int(q), int(v)
  35. if i == 4:
  36. return int(t), int(p), int(v)
  37. if i == 5:
  38. return int(v), int(p), int(q)
  39. def batt_to_color(batt):
  40. h = batt[0] / 100.0 * 0.3333
  41. r, g, b = from_hsv(h, 1.0, 1.0)
  42. return r, g, b
  43. class PicoMatrix:
  44. def __init__(self, w = 32, h = 32):
  45. self.width = w # x-axis
  46. self.height = h # y-axis
  47. self.panelW = w # x-axis
  48. self.panelH = h # y-axis
  49. # compatibility to TestGUI
  50. self.multiplier = 1.0
  51. if (w != 32) or (h != 32):
  52. raise RuntimeError("TODO not yet supported")
  53. mode = interstate75.DISPLAY_INTERSTATE75_32X32
  54. self.matrix = interstate75.Interstate75(display = mode, panel_type = hub75.PANEL_FM6126A)
  55. self.black = self.matrix.display.create_pen(0, 0, 0)
  56. self.white = self.matrix.display.create_pen(255, 255, 255)
  57. self.ledTime = time.time()
  58. self.led = Pin("LED", Pin.OUT)
  59. self.ledRefresh = 0.5
  60. self.adc = ADC(26)
  61. self.battState = None
  62. self.battTime = time.time()
  63. self.battRefresh = 10
  64. self.loop_start() # initialize with blank image for ScrollText constructor
  65. def exit(self):
  66. self.matrix.stop()
  67. def battery(self):
  68. n = const(10)
  69. bits = const(10)
  70. raw = 0
  71. for i in range(0, n):
  72. raw += self.adc.read_u16() >> (16 - bits)
  73. time.sleep(0.1 / n)
  74. raw /= n
  75. v_adc_ref = const(3.3) # V
  76. v_adc = (raw / ((1 << bits) - 1)) * v_adc_ref
  77. r1 = const(27.0) # kOhm
  78. r2 = const(5.6) # kOhm
  79. v_bat_uncal = (v_adc * (r1 + r2)) / r2
  80. # Calibration
  81. # TODO supports only 2 points
  82. cal_pts = [
  83. # adc, bat
  84. #(13.65, 15.46),
  85. #(13.60, 15.34),
  86. (13.625, 15.4), # avg of above
  87. (14.30, 16.13),
  88. ]
  89. # https://www.ti.com/europe/downloads/f2810_12_calibration_10.pdf
  90. gain = (cal_pts[0][1] - cal_pts[1][1]) / (cal_pts[0][0] - cal_pts[1][0])
  91. offset = cal_pts[1][1] - cal_pts[1][0] * gain
  92. v_bat = v_bat_uncal * gain + offset
  93. # Use this to gather calibration data
  94. #v_bat = v_bat_uncal
  95. #print(v_bat)
  96. # TODO auto-detect cell count
  97. n_lipo = const(4) # 4S
  98. v_cell = v_bat / n_lipo
  99. v_cell_min = const(3.25)
  100. v_cell_max = const(4.2)
  101. p_cell = (v_cell - v_cell_min) / (v_cell_max - v_cell_min) * 100.0
  102. p_cell = max(min(p_cell, 100.0), 0.0)
  103. ret = (p_cell, v_cell, v_bat)
  104. r, g, b = batt_to_color(ret)
  105. self.matrix.set_led(r, g, b)
  106. return ret
  107. def batteryCache(self, refresh = False):
  108. now = time.time()
  109. if (self.battState == None) or ((now - self.battTime) >= self.battRefresh) or (now < self.battTime) or refresh:
  110. self.battTime = now
  111. self.battState = self.battery()
  112. return self.battState
  113. def heartbeat(self):
  114. now = time.time()
  115. if ((now - self.ledTime) >= self.ledRefresh) or (now < self.ledTime):
  116. self.ledTime = now
  117. self.led.toggle()
  118. def loop_start(self):
  119. self.matrix.display.set_pen(self.black)
  120. self.matrix.display.clear()
  121. self.matrix.display.set_pen(self.white)
  122. return False # no input, never quit on our own
  123. def loop_end(self):
  124. self.matrix.update()
  125. # LED heartbeat blink
  126. self.heartbeat()
  127. # update battery if necessary
  128. self.batteryCache(False)
  129. def set_pixel(self, x, y, color):
  130. if (x < 0) or (y < 0) or (x >= self.width) or (y >= self.height):
  131. return
  132. pen = self.matrix.display.create_pen(color[0], color[1], color[2])
  133. self.matrix.display.set_pen(pen)
  134. self.matrix.display.pixel(int(x), int(y))
  135. class PicoText:
  136. def __init__(self, g, fg = (255, 255, 255), bg = (0, 0, 0), c = (0, 255, 0)):
  137. self.gui = g
  138. self.fg = fg
  139. self.bg = bg
  140. self.color = c
  141. def text(self, s, f, offset = 0, earlyAbort = True, yOff = 0, compat = True):
  142. if not earlyAbort:
  143. self.gui.matrix.display.set_font(f)
  144. return self.gui.matrix.display.measure_text(s, scale=1)
  145. color = self.fg
  146. if isinstance(self.gui, MapperReduceBrightness):
  147. color = self.gui.adjust(color)
  148. pen = self.gui.matrix.display.create_pen(color[0], color[1], color[2])
  149. self.gui.matrix.display.set_pen(pen)
  150. self.gui.matrix.display.set_font(f)
  151. if not compat:
  152. # absolute positioning
  153. x = offset
  154. y = yOff
  155. else:
  156. # centered, like BDF DrawText implementation
  157. fontOff = 0
  158. if f == "bitmap6":
  159. fontOff = 3
  160. elif f == "bitmap8":
  161. fontOff = 4
  162. elif f == "bitmap14_outline":
  163. fontOff = 7
  164. x = -offset
  165. y = int(self.gui.height / 2 - fontOff + yOff)
  166. self.gui.matrix.display.text(s, x, y, scale=1)
  167. class PicoBatt:
  168. def __init__(self, g, ti = 5.0, tt = 5.0):
  169. self.gui = g
  170. self.timeImage = ti
  171. self.timeText = tt
  172. self.text = PicoText(self.gui)
  173. self.restart()
  174. def restart(self):
  175. self.start = time.time()
  176. def finished(self):
  177. now = time.time()
  178. return ((now - self.start) >= (self.timeText + self.timeImage)) or (now < self.start)
  179. def drawText(self, refresh = False):
  180. batt = self.gui.batteryCache(refresh)
  181. c = batt_to_color(batt)
  182. self.text.fg = (255, 255, 255)
  183. self.text.text( "Batt:", "bitmap8", 0, True, 8 * 0, False)
  184. self.text.fg = c
  185. self.text.text("{:.2f}%".format(batt[0]), "bitmap8", 0, True, 8 * 1, False)
  186. self.text.text("{:.2f}V".format(batt[1]), "bitmap8", 0, True, 8 * 2, False)
  187. self.text.text("{:.2f}V".format(batt[2]), "bitmap8", 0, True, 8 * 3, False)
  188. def drawImage(self, refresh = False):
  189. x_off = const(1)
  190. y_off = const(16)
  191. nub_w = const(3)
  192. nub_h = const(6)
  193. w = self.gui.width - x_off * 2
  194. h = self.gui.height - y_off
  195. batt = self.gui.batteryCache(refresh)
  196. c = batt_to_color(batt)
  197. fill_w = int(batt[0] / 100.0 * (w - nub_w))
  198. s = "{:.0f}%".format(batt[0])
  199. s_w = self.text.text(s, "bitmap14_outline", 0, False)
  200. self.text.fg = (255, 255, 255)
  201. self.text.text(s, "bitmap14_outline", int((self.gui.width - s_w) / 2), True, 1, False)
  202. for x in range(0, w - nub_w):
  203. for y in range(0, h):
  204. if (x == 0) or (x == (w - nub_w - 1)) or (y == 0) or (y == (h - 1)) or (x < fill_w):
  205. self.gui.set_pixel(x + x_off, y + y_off, c)
  206. for x in range(0, nub_w):
  207. for y in range(0, nub_h):
  208. self.gui.set_pixel(x + x_off + w - nub_w, y + y_off + int((h - nub_h) / 2), c)
  209. def draw(self, refresh = False):
  210. now = time.time()
  211. if (now - self.start) < self.timeImage:
  212. self.drawImage(refresh)
  213. else:
  214. self.drawText(refresh)
  215. if __name__ == "__main__":
  216. import time
  217. t = PicoMatrix(32, 32)
  218. s = PicoText(t)
  219. b = PicoBatt(t, 10.0, 10.0)
  220. start = time.time()
  221. i = 0
  222. def helper():
  223. global s, start, i, b
  224. now = time.time()
  225. if ((now - start) > 5.0) or (now < start):
  226. start = now
  227. i = (i + 1) % 6
  228. if i == 0:
  229. b.restart()
  230. if i < 4:
  231. b.draw(True)
  232. time.sleep(1.0)
  233. elif i == 4:
  234. s.text("Abgj6", "bitmap6", 0, True, 0, False)
  235. s.text("Abdgj8", "bitmap8", 0, True, 6 + 2, False)
  236. s.text("Ag14", "bitmap14_outline", 0, True, 6 + 2 + 8 + 1, False)
  237. else:
  238. s.text("Drinks:", "bitmap8", 0)
  239. util.loop(t, helper)