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.

pico.py 9.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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 battery(self):
  66. n = const(10)
  67. bits = const(10)
  68. raw = 0
  69. for i in range(0, n):
  70. raw += self.adc.read_u16() >> (16 - bits)
  71. time.sleep(0.1 / n)
  72. raw /= n
  73. v_adc_ref = const(3.3) # V
  74. v_adc = (raw / ((1 << bits) - 1)) * v_adc_ref
  75. r1 = const(27.0) # kOhm
  76. r2 = const(5.6) # kOhm
  77. v_bat_uncal = (v_adc * (r1 + r2)) / r2
  78. # Calibration
  79. # TODO supports only 2 points
  80. cal_pts = [
  81. # adc, bat
  82. (13.65, 15.46),
  83. (13.60, 15.34),
  84. # TODO better values
  85. ]
  86. # https://www.ti.com/europe/downloads/f2810_12_calibration_10.pdf
  87. gain = (cal_pts[0][1] - cal_pts[1][1]) / (cal_pts[0][0] - cal_pts[1][0])
  88. offset = cal_pts[1][1] - cal_pts[1][0] * gain
  89. v_bat = v_bat_uncal * gain + offset
  90. # TODO auto-detect cell count
  91. n_lipo = const(4) # 4S
  92. v_cell = v_bat / n_lipo
  93. v_cell_min = const(3.25)
  94. v_cell_max = const(4.2)
  95. p_cell = (v_cell - v_cell_min) / (v_cell_max - v_cell_min) * 100.0
  96. p_cell = max(min(p_cell, 100.0), 0.0)
  97. ret = (p_cell, v_cell, v_bat)
  98. r, g, b = batt_to_color(ret)
  99. self.matrix.set_led(r, g, b)
  100. return ret
  101. def batteryCache(self, refresh = False):
  102. now = time.time()
  103. if (self.battState == None) or ((now - self.battTime) >= self.battRefresh) or (now < self.battTime) or refresh:
  104. self.battTime = now
  105. self.battState = self.battery()
  106. return self.battState
  107. def heartbeat(self):
  108. now = time.time()
  109. if ((now - self.ledTime) >= self.ledRefresh) or (now < self.ledTime):
  110. self.ledTime = now
  111. self.led.toggle()
  112. def loop_start(self):
  113. self.matrix.display.set_pen(self.black)
  114. self.matrix.display.clear()
  115. self.matrix.display.set_pen(self.white)
  116. return False # no input, never quit on our own
  117. def loop_end(self):
  118. self.matrix.update()
  119. # LED heartbeat blink
  120. self.heartbeat()
  121. # update battery if necessary
  122. self.batteryCache(False)
  123. def loop(self, func = None):
  124. while True:
  125. if self.loop_start():
  126. break
  127. if func != None:
  128. func()
  129. self.loop_end()
  130. self.matrix.stop()
  131. def set_pixel(self, x, y, color):
  132. if (x < 0) or (y < 0) or (x >= self.width) or (y >= self.height):
  133. return
  134. pen = self.matrix.display.create_pen(color[0], color[1], color[2])
  135. self.matrix.display.set_pen(pen)
  136. self.matrix.display.pixel(int(x), int(y))
  137. class PicoText:
  138. def __init__(self, g, fg = (255, 255, 255), bg = (0, 0, 0), c = (0, 255, 0)):
  139. self.gui = g
  140. self.fg = fg
  141. self.bg = bg
  142. self.color = c
  143. def text(self, s, f, offset = 0, earlyAbort = True, yOff = 0, compat = True):
  144. if not earlyAbort:
  145. self.gui.matrix.display.set_font(f)
  146. return self.gui.matrix.display.measure_text(s, scale=1)
  147. color = self.fg
  148. if isinstance(self.gui, MapperReduceBrightness):
  149. color = self.gui.adjust(color)
  150. pen = self.gui.matrix.display.create_pen(color[0], color[1], color[2])
  151. self.gui.matrix.display.set_pen(pen)
  152. self.gui.matrix.display.set_font(f)
  153. if not compat:
  154. # absolute positioning
  155. x = offset
  156. y = yOff
  157. else:
  158. # centered, like BDF DrawText implementation
  159. fontOff = 0
  160. if f == "bitmap6":
  161. fontOff = 3
  162. elif f == "bitmap8":
  163. fontOff = 4
  164. elif f == "bitmap14_outline":
  165. fontOff = 7
  166. x = -offset
  167. y = int(self.gui.height / 2 - fontOff + yOff)
  168. self.gui.matrix.display.text(s, x, y, scale=1)
  169. class PicoBatt:
  170. def __init__(self, g, ti = 5.0, tt = 5.0):
  171. self.gui = g
  172. self.timeImage = ti
  173. self.timeText = tt
  174. self.text = PicoText(self.gui)
  175. self.restart()
  176. def restart(self):
  177. self.start = time.time()
  178. def finished(self):
  179. now = time.time()
  180. return ((now - self.start) >= (self.timeText + self.timeImage)) or (now < self.start)
  181. def drawText(self, refresh = False):
  182. batt = self.gui.batteryCache(refresh)
  183. c = batt_to_color(batt)
  184. self.text.fg = (255, 255, 255)
  185. self.text.text( "Batt:", "bitmap8", 0, True, 8 * 0, False)
  186. self.text.fg = c
  187. self.text.text("{:.2f}%".format(batt[0]), "bitmap8", 0, True, 8 * 1, False)
  188. self.text.text("{:.2f}V".format(batt[1]), "bitmap8", 0, True, 8 * 2, False)
  189. self.text.text("{:.2f}V".format(batt[2]), "bitmap8", 0, True, 8 * 3, False)
  190. def drawImage(self, refresh = False):
  191. x_off = const(1)
  192. y_off = const(16)
  193. nub_w = const(3)
  194. nub_h = const(6)
  195. w = self.gui.width - x_off * 2
  196. h = self.gui.height - y_off
  197. batt = self.gui.batteryCache(refresh)
  198. c = batt_to_color(batt)
  199. fill_w = int(batt[0] / 100.0 * (w - nub_w))
  200. s = "{:.0f}%".format(batt[0])
  201. s_w = self.text.text(s, "bitmap14_outline", 0, False)
  202. self.text.fg = (255, 255, 255)
  203. self.text.text(s, "bitmap14_outline", int((self.gui.width - s_w) / 2), True, 1, False)
  204. for x in range(0, w - nub_w):
  205. for y in range(0, h):
  206. if (x == 0) or (x == (w - nub_w - 1)) or (y == 0) or (y == (h - 1)) or (x < fill_w):
  207. self.gui.set_pixel(x + x_off, y + y_off, c)
  208. for x in range(0, nub_w):
  209. for y in range(0, nub_h):
  210. self.gui.set_pixel(x + x_off + w - nub_w, y + y_off + int((h - nub_h) / 2), c)
  211. def draw(self, refresh = False):
  212. now = time.time()
  213. if (now - self.start) < self.timeImage:
  214. self.drawImage(refresh)
  215. else:
  216. self.drawText(refresh)
  217. if __name__ == "__main__":
  218. import time
  219. t = PicoMatrix(32, 32)
  220. s = PicoText(t)
  221. b = PicoBatt(t, 10.0, 10.0)
  222. start = time.time()
  223. i = 0
  224. def helper():
  225. global s, start, i, b
  226. now = time.time()
  227. if ((now - start) > 5.0) or (now < start):
  228. start = now
  229. i = (i + 1) % 6
  230. if i == 0:
  231. b.restart()
  232. if i < 4:
  233. b.draw(True)
  234. time.sleep(1.0)
  235. elif i == 4:
  236. s.text("Abgj6", "bitmap6", 0, True, 0, False)
  237. s.text("Abdgj8", "bitmap8", 0, True, 6 + 2, False)
  238. s.text("Ag14", "bitmap14_outline", 0, True, 6 + 2 + 8 + 1, False)
  239. else:
  240. s.text("Drinks:", "bitmap8", 0)
  241. t.loop(helper)