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 5.5KB

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