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.

util.py 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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 sys
  9. cachedTarget = None
  10. targetPlatform = None
  11. wifiConnected = False
  12. def isPi():
  13. global targetPlatform
  14. if targetPlatform == None:
  15. getTarget()
  16. return targetPlatform == "pi"
  17. def isPico():
  18. global targetPlatform
  19. if targetPlatform == None:
  20. getTarget()
  21. return targetPlatform == "pico"
  22. def getTarget(i = None):
  23. global targetPlatform, cachedTarget
  24. if cachedTarget != None:
  25. return cachedTarget
  26. target = None
  27. try:
  28. # First we try the Raspberry Pi interface
  29. from pi import PiMatrix
  30. pi = PiMatrix()
  31. # TODO hard-coded adjustments
  32. from mapper import MapperReduceBrightness, MapperColorAdjust, MapperStripToRect
  33. bright = MapperReduceBrightness(pi, i)
  34. #col = MapperColorAdjust(bright)
  35. #target = col
  36. #target = MapperStripToRect(col)
  37. target = MapperStripToRect(bright)
  38. if targetPlatform == None:
  39. # only print once
  40. print("Raspberry Pi Adafruit RGB LED Matrix detected")
  41. targetPlatform = "pi"
  42. except Exception as e:
  43. target = None
  44. print()
  45. if hasattr(sys, "print_exception"):
  46. sys.print_exception(e)
  47. else:
  48. print(e)
  49. print()
  50. try:
  51. # Next we try the Pico Interstate75 interface
  52. from pico import PicoMatrix
  53. pico = PicoMatrix(i)
  54. # TODO hard-coded adjustments
  55. from mapper import MapperReduceBrightness
  56. target = MapperReduceBrightness(pico, i)
  57. if targetPlatform == None:
  58. # only print once
  59. print("Raspberry Pi Pico Interstate75 RGB LED Matrix detected")
  60. targetPlatform = "pico"
  61. except Exception as e:
  62. target = None
  63. print()
  64. if hasattr(sys, "print_exception"):
  65. sys.print_exception(e)
  66. else:
  67. print(e)
  68. print()
  69. # If this fails fall back to the SDL/pygame GUI
  70. from test import TestGUI
  71. ui = TestGUI()
  72. # TODO hard-coded adjustments
  73. from mapper import MapperReduceBrightness
  74. target = MapperReduceBrightness(ui, i)
  75. if targetPlatform == None:
  76. # only print once
  77. print("Falling back to GUI debug interface")
  78. targetPlatform = "tk"
  79. cachedTarget = target
  80. return target
  81. # https://github.com/raspberrypi/pico-examples/blob/master/pico_w/wifi/python_test_tcp/micropython_test_tcp_client.py
  82. def connectToWiFi():
  83. global wifiConnected
  84. if wifiConnected:
  85. return True
  86. # only use WiFi on Pico
  87. try:
  88. from pico import PicoMatrix
  89. except Exception as e:
  90. print()
  91. if hasattr(sys, "print_exception"):
  92. sys.print_exception(e)
  93. else:
  94. print(e)
  95. print()
  96. wifiConnected = True
  97. return True
  98. import network
  99. import time
  100. from config import Config
  101. # Check if wifi details have been set
  102. if len(Config.networks) == 0:
  103. print('Please set wifi ssid and password in config.py')
  104. wifiConnected = False
  105. return False
  106. # Start WiFi hardware
  107. wlan = network.WLAN(network.STA_IF)
  108. wlan.active(True)
  109. # Look for known networks
  110. visible = wlan.scan()
  111. ssid = None
  112. user = None
  113. password = None
  114. print(visible)
  115. if len(visible) == 0:
  116. print("No networks visible at all")
  117. wifiConnected = False
  118. return False
  119. for name, a, b, c, d, e in visible:
  120. for net in Config.networks:
  121. if len(net) == 2:
  122. t_ssid, t_password = net
  123. elif len(net) == 3:
  124. t_ssid, t_user, t_password = net
  125. if name.decode("utf-8") == t_ssid:
  126. ssid = t_ssid
  127. if len(net) == 3:
  128. user = t_user
  129. password = t_password
  130. break
  131. if (ssid == None) or (password == None):
  132. print("No known network found")
  133. wifiConnected = False
  134. return False
  135. # Start connection
  136. if user != None:
  137. wlan.seteap(user, password)
  138. wlan.connect(ssid)
  139. else:
  140. wlan.connect(ssid, password)
  141. # Wait for connect success or failure
  142. max_wait = 40
  143. error_count = max_wait
  144. while max_wait > 0:
  145. if wlan.status() >= 3:
  146. break
  147. elif wlan.status() < 0:
  148. wlan.connect(ssid, password)
  149. error_count -= 1
  150. if error_count <= 0:
  151. break
  152. else:
  153. max_wait -= 1
  154. print('waiting for connection...')
  155. time.sleep(0.5)
  156. # Handle connection error
  157. if wlan.status() != 3:
  158. print('wifi connection failed %d' % wlan.status())
  159. wifiConnected = False
  160. return False
  161. else:
  162. print('connected')
  163. status = wlan.ifconfig()
  164. print('ip = ' + status[0])
  165. wifiConnected = True
  166. return True
  167. def getRequests():
  168. global wifiConnected
  169. try:
  170. # try to get normal python lib
  171. import requests
  172. return requests.get, requests.post
  173. except Exception as e:
  174. print()
  175. if hasattr(sys, "print_exception"):
  176. sys.print_exception(e)
  177. else:
  178. print(e)
  179. print()
  180. # if it fails try the Pi Pico MicroPython implementation
  181. import urequests as requests
  182. # in this case we also need to connect to WiFi first
  183. if not wifiConnected:
  184. if not connectToWiFi():
  185. return None, None
  186. return requests.get, requests.post
  187. return None, None
  188. def getTextDrawer():
  189. try:
  190. # Try BDF parser library
  191. from bdf import DrawText
  192. return DrawText
  193. except Exception as e:
  194. print()
  195. if hasattr(sys, "print_exception"):
  196. sys.print_exception(e)
  197. else:
  198. print(e)
  199. print()
  200. # fall back to the Pico Interstate75 implementation
  201. from pico import PicoText
  202. return PicoText
  203. return None
  204. def getInput():
  205. try:
  206. # try evdev library
  207. from gamepad import InputWrapper
  208. return InputWrapper()
  209. except Exception as e:
  210. print()
  211. if hasattr(sys, "print_exception"):
  212. sys.print_exception(e)
  213. else:
  214. print(e)
  215. print()
  216. # fall back to the Pico Interstate75 implementation
  217. from pico import PicoInput
  218. return PicoInput()
  219. return None
  220. def loop(gui, func = None):
  221. while True:
  222. if gui.loop_start():
  223. break
  224. if func != None:
  225. func()
  226. gui.loop_end()
  227. gui.exit()