Python RGB Matrix games and animations https://www.xythobuz.de/ledmatrix_v2.html
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

util.py 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. password = None
  110. for name, a, b, c, d, e in visible:
  111. for t_ssid, t_password in Config.networks:
  112. if name.decode("utf-8") == t_ssid:
  113. ssid = t_ssid
  114. password = t_password
  115. break
  116. if (ssid == None) or (password == None):
  117. print("No known network found")
  118. wifiConnected = False
  119. return False
  120. # Start connection
  121. wlan.connect(ssid, password)
  122. # Wait for connect success or failure
  123. max_wait = 40
  124. error_count = max_wait
  125. while max_wait > 0:
  126. if wlan.status() >= 3:
  127. break
  128. elif wlan.status() < 0:
  129. wlan.connect(ssid, password)
  130. error_count -= 1
  131. if error_count <= 0:
  132. break
  133. else:
  134. max_wait -= 1
  135. print('waiting for connection...')
  136. time.sleep(0.5)
  137. # Handle connection error
  138. if wlan.status() != 3:
  139. print('wifi connection failed %d' % wlan.status())
  140. wifiConnected = False
  141. return False
  142. else:
  143. print('connected')
  144. status = wlan.ifconfig()
  145. print('ip = ' + status[0])
  146. wifiConnected = True
  147. return True
  148. def getRequests():
  149. global wifiConnected
  150. try:
  151. # try to get normal python lib
  152. import requests
  153. return requests.get
  154. except Exception as e:
  155. print()
  156. if hasattr(sys, "print_exception"):
  157. sys.print_exception(e)
  158. else:
  159. print(e)
  160. print()
  161. # if it fails try the Pi Pico MicroPython implementation
  162. import urequests as requests
  163. # in this case we also need to connect to WiFi first
  164. if not wifiConnected:
  165. if not connectToWiFi():
  166. return None
  167. return requests.get
  168. return None
  169. def getTextDrawer():
  170. try:
  171. # Try BDF parser library
  172. from bdf import DrawText
  173. return DrawText
  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. # fall back to the Pico Interstate75 implementation
  182. from pico import PicoText
  183. return PicoText
  184. return None
  185. def getInput():
  186. try:
  187. # try evdev library
  188. from gamepad import InputWrapper
  189. return InputWrapper()
  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 PicoInput
  199. return PicoInput()
  200. return None
  201. def loop(gui, func = None):
  202. while True:
  203. if gui.loop_start():
  204. break
  205. if func != None:
  206. func()
  207. gui.loop_end()
  208. gui.exit()