Python RGB Matrix games and animations https://www.xythobuz.de/ledmatrix_v2.html
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

pico.py 9.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. # text drawing API
  142. def setText(self, s, f):
  143. self.text = s
  144. self.font = f
  145. # text drawing API
  146. def getDimensions(self):
  147. self.gui.matrix.display.set_font(self.font)
  148. w = self.gui.matrix.display.measure_text(self.text, scale=1)
  149. return (w, 42) # TODO wrong height
  150. # text drawing API
  151. def draw(self, xOff = 0, yOff = 0, compat = True):
  152. color = self.fg
  153. if isinstance(self.gui, MapperReduceBrightness):
  154. color = self.gui.adjust(color)
  155. pen = self.gui.matrix.display.create_pen(color[0], color[1], color[2])
  156. self.gui.matrix.display.set_pen(pen)
  157. self.gui.matrix.display.set_font(self.font)
  158. if not compat:
  159. # absolute positioning
  160. x = xOff
  161. y = yOff
  162. else:
  163. # centered, like BDF DrawText implementation
  164. fontOff = 0
  165. if self.font == "bitmap6":
  166. fontOff = 3
  167. elif self.font == "bitmap8":
  168. fontOff = 4
  169. elif self.font == "bitmap14_outline":
  170. fontOff = 7
  171. x = -xOff
  172. y = int(self.gui.height / 2 - fontOff + yOff)
  173. self.gui.matrix.display.text(self.text, x, y, scale=1)
  174. class PicoBatt:
  175. def __init__(self, g, ti = 5.0, tt = 5.0):
  176. self.gui = g
  177. self.timeImage = ti
  178. self.timeText = tt
  179. self.text = PicoText(self.gui)
  180. self.restart()
  181. def restart(self):
  182. self.start = time.time()
  183. def finished(self):
  184. now = time.time()
  185. return ((now - self.start) >= (self.timeText + self.timeImage)) or (now < self.start)
  186. def drawText(self, refresh = False):
  187. batt = self.gui.batteryCache(refresh)
  188. c = batt_to_color(batt)
  189. self.text.fg = (255, 255, 255)
  190. self.text.setText( "Batt:", "bitmap8")
  191. self.text.draw(0, 8 * 0, False)
  192. self.text.fg = c
  193. self.text.setText("{:.2f}%".format(batt[0]), "bitmap8")
  194. self.text.draw(0, 8 * 1, False)
  195. self.text.setText("{:.2f}V".format(batt[1]), "bitmap8")
  196. self.text.draw(0, 8 * 2, False)
  197. self.text.setText("{:.2f}V".format(batt[2]), "bitmap8")
  198. self.text.draw(0, 8 * 3, False)
  199. def drawImage(self, refresh = False):
  200. x_off = const(1)
  201. y_off = const(16)
  202. nub_w = const(3)
  203. nub_h = const(6)
  204. w = self.gui.width - x_off * 2
  205. h = self.gui.height - y_off
  206. batt = self.gui.batteryCache(refresh)
  207. c = batt_to_color(batt)
  208. fill_w = int(batt[0] / 100.0 * (w - nub_w))
  209. s = "{:.0f}%".format(batt[0])
  210. self.text.setText(s, "bitmap14_outline")
  211. s_w = self.text.getDimensions()[0]
  212. self.text.fg = (255, 255, 255)
  213. self.text.draw(int((self.gui.width - s_w) / 2), 1, False)
  214. for x in range(0, w - nub_w):
  215. for y in range(0, h):
  216. if (x == 0) or (x == (w - nub_w - 1)) or (y == 0) or (y == (h - 1)) or (x < fill_w):
  217. self.gui.set_pixel(x + x_off, y + y_off, c)
  218. for x in range(0, nub_w):
  219. for y in range(0, nub_h):
  220. self.gui.set_pixel(x + x_off + w - nub_w, y + y_off + int((h - nub_h) / 2), c)
  221. def draw(self, refresh = False):
  222. now = time.time()
  223. if (now - self.start) < self.timeImage:
  224. self.drawImage(refresh)
  225. else:
  226. self.drawText(refresh)
  227. if __name__ == "__main__":
  228. import time
  229. import util
  230. t = PicoMatrix(32, 32)
  231. s = PicoText(t)
  232. b = PicoBatt(t, 10.0, 10.0)
  233. start = time.time()
  234. i = 0
  235. def helper():
  236. global s, start, i, b
  237. now = time.time()
  238. if ((now - start) > 5.0) or (now < start):
  239. start = now
  240. i = (i + 1) % 6
  241. if i == 0:
  242. b.restart()
  243. if i < 4:
  244. b.draw(True)
  245. time.sleep(1.0)
  246. elif i == 4:
  247. s.setText("Abgj6", "bitmap6")
  248. s.draw(0, 0, False)
  249. s.setText("Abdgj8", "bitmap8")
  250. s.draw(0, 6 + 2, False)
  251. s.setText("Ag14", "bitmap14_outline")
  252. s.draw(0, 6 + 2 + 8 + 1, False)
  253. else:
  254. s.setText("Drinks:", "bitmap8")
  255. s.draw()
  256. util.loop(t, helper)