Python RGB Matrix games and animations https://www.xythobuz.de/ledmatrix_v2.html
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

util.py 6.4KB

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