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.

pico.py 8.8KB

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