Python RGB Matrix games and animations https://www.xythobuz.de/ledmatrix_v2.html
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

util.py 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. targetPlatform = None
  9. def isPi():
  10. global targetPlatform
  11. if targetPlatform == None:
  12. getTarget()
  13. return targetPlatform == "pi"
  14. def isPico():
  15. global targetPlatform
  16. if targetPlatform == None:
  17. getTarget()
  18. return targetPlatform == "pico"
  19. def getTarget():
  20. global targetPlatform
  21. target = None
  22. try:
  23. # First we try the Raspberry Pi interface
  24. from pi import PiMatrix
  25. pi = PiMatrix()
  26. # TODO hard-coded adjustments
  27. from mapper import MapperColorAdjust, MapperStripToRect
  28. col = MapperColorAdjust(pi)
  29. target = MapperStripToRect(col)
  30. if targetPlatform == None:
  31. # only print once
  32. print("Raspberry Pi Adafruit RGB LED Matrix detected")
  33. targetPlatform = "pi"
  34. except:
  35. try:
  36. # Next we try the Pico Interstate75 interface
  37. from pico import PicoMatrix
  38. target = PicoMatrix()
  39. if targetPlatform == None:
  40. # only print once
  41. print("Raspberry Pi Pico Interstate75 RGB LED Matrix detected")
  42. targetPlatform = "pico"
  43. except:
  44. # If this fails fall back to the SDL/pygame GUI
  45. from test import TestGUI
  46. target = TestGUI()
  47. if targetPlatform == None:
  48. # only print once
  49. print("Falling back to GUI debug interface")
  50. targetPlatform = "tk"
  51. return target
  52. # https://github.com/raspberrypi/pico-examples/blob/master/pico_w/wifi/python_test_tcp/micropython_test_tcp_client.py
  53. def connectToWiFi():
  54. import network
  55. import time
  56. from config import Config
  57. # Check if wifi details have been set
  58. if len(Config.networks) == 0:
  59. print('Please set wifi ssid and password in config.py')
  60. return False
  61. # Start WiFi hardware
  62. wlan = network.WLAN(network.STA_IF)
  63. wlan.active(True)
  64. # Look for known networks
  65. visible = wlan.scan()
  66. ssid = None
  67. password = None
  68. for name, a, b, c, d, e in visible:
  69. for t_ssid, t_password in Config.networks:
  70. if name.decode("utf-8") == t_ssid:
  71. ssid = t_ssid
  72. password = t_password
  73. break
  74. if (ssid == None) or (password == None):
  75. print("No known network found")
  76. return False
  77. # Start connection
  78. wlan.connect(ssid, password)
  79. # Wait for connect success or failure
  80. max_wait = 20
  81. error_count = 20
  82. while max_wait > 0:
  83. if wlan.status() >= 3:
  84. break
  85. elif wlan.status() < 0:
  86. wlan.connect(ssid, password)
  87. error_count -= 1
  88. if error_count <= 0:
  89. break
  90. else:
  91. max_wait -= 1
  92. print('waiting for connection...')
  93. time.sleep(0.5)
  94. # Handle connection error
  95. if wlan.status() != 3:
  96. print('wifi connection failed %d' % wlan.status())
  97. return False
  98. else:
  99. print('connected')
  100. status = wlan.ifconfig()
  101. print('ip = ' + status[0])
  102. return True
  103. def getRequests():
  104. try:
  105. # try to get normal python lib
  106. import requests
  107. return requests.get
  108. except:
  109. # if it fails try the Pi Pico MicroPython implementation
  110. import urequests as requests
  111. # in this case we also need to connect to WiFi first
  112. if not connectToWiFi():
  113. return None
  114. return requests.get
  115. return None