Python RGB Matrix games and animations https://www.xythobuz.de/ledmatrix_v2.html
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

util.py 5.1KB

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