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.

util.py 6.9KB

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