Python RGB Matrix games and animations https://www.xythobuz.de/ledmatrix_v2.html
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. target = ui
  76. if targetPlatform == None:
  77. # only print once
  78. print("Falling back to GUI debug interface")
  79. targetPlatform = "tk"
  80. cachedTarget = target
  81. return target
  82. # https://github.com/raspberrypi/pico-examples/blob/master/pico_w/wifi/python_test_tcp/micropython_test_tcp_client.py
  83. def connectToWiFi():
  84. global wifiConnected
  85. if wifiConnected:
  86. return True
  87. # only use WiFi on Pico
  88. try:
  89. from pico import PicoMatrix
  90. except Exception as e:
  91. print()
  92. if hasattr(sys, "print_exception"):
  93. sys.print_exception(e)
  94. else:
  95. print(e)
  96. print()
  97. wifiConnected = True
  98. return True
  99. import network
  100. import time
  101. from config import Config
  102. # Check if wifi details have been set
  103. if len(Config.networks) == 0:
  104. print('Please set wifi ssid and password in config.py')
  105. wifiConnected = False
  106. return False
  107. # Start WiFi hardware
  108. wlan = network.WLAN(network.STA_IF)
  109. wlan.active(True)
  110. # Look for known networks
  111. visible = wlan.scan()
  112. ssid = None
  113. user = None
  114. password = None
  115. print(visible)
  116. if len(visible) == 0:
  117. print("No networks visible at all")
  118. wifiConnected = False
  119. return False
  120. for name, a, b, c, d, e in visible:
  121. for net in Config.networks:
  122. if len(net) == 2:
  123. t_ssid, t_password = net
  124. elif len(net) == 3:
  125. t_ssid, t_user, t_password = net
  126. if name.decode("utf-8") == t_ssid:
  127. ssid = t_ssid
  128. if len(net) == 3:
  129. user = t_user
  130. password = t_password
  131. break
  132. if (ssid == None) or (password == None):
  133. print("No known network found")
  134. wifiConnected = False
  135. return False
  136. # Start connection
  137. if user != None:
  138. wlan.seteap(user, password)
  139. wlan.connect(ssid)
  140. else:
  141. wlan.connect(ssid, password)
  142. # Wait for connect success or failure
  143. max_wait = 40
  144. error_count = max_wait
  145. while max_wait > 0:
  146. if wlan.status() >= 3:
  147. break
  148. elif wlan.status() < 0:
  149. wlan.connect(ssid, password)
  150. error_count -= 1
  151. if error_count <= 0:
  152. break
  153. else:
  154. max_wait -= 1
  155. print('waiting for connection...')
  156. time.sleep(0.5)
  157. # Handle connection error
  158. if wlan.status() != 3:
  159. print('wifi connection failed %d' % wlan.status())
  160. wifiConnected = False
  161. return False
  162. else:
  163. print('connected')
  164. status = wlan.ifconfig()
  165. print('ip = ' + status[0])
  166. wifiConnected = True
  167. return True
  168. def getRequests():
  169. global wifiConnected
  170. try:
  171. # try to get normal python lib
  172. import requests
  173. return requests.get, requests.post
  174. except Exception as e:
  175. print()
  176. if hasattr(sys, "print_exception"):
  177. sys.print_exception(e)
  178. else:
  179. print(e)
  180. print()
  181. # if it fails try the Pi Pico MicroPython implementation
  182. import urequests as requests
  183. # in this case we also need to connect to WiFi first
  184. if not wifiConnected:
  185. if not connectToWiFi():
  186. return None, None
  187. return requests.get, requests.post
  188. return None, None
  189. def getTextDrawer():
  190. try:
  191. # Try BDF parser library
  192. from bdf import DrawText
  193. return DrawText
  194. except Exception as e:
  195. print()
  196. if hasattr(sys, "print_exception"):
  197. sys.print_exception(e)
  198. else:
  199. print(e)
  200. print()
  201. # fall back to the Pico Interstate75 implementation
  202. from pico import PicoText
  203. return PicoText
  204. return None
  205. def getInput():
  206. try:
  207. # try evdev library
  208. from gamepad import InputWrapper
  209. return InputWrapper()
  210. except Exception as e:
  211. print()
  212. if hasattr(sys, "print_exception"):
  213. sys.print_exception(e)
  214. else:
  215. print(e)
  216. print()
  217. # fall back to the Pico Interstate75 implementation
  218. from pico import PicoInput
  219. return PicoInput()
  220. return None
  221. def loop(gui, func = None):
  222. while True:
  223. if gui.loop_start():
  224. break
  225. if func != None:
  226. func()
  227. gui.loop_end()
  228. gui.exit()