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

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