123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274 |
- #!/usr/bin/env python3
-
- # ----------------------------------------------------------------------------
- # "THE BEER-WARE LICENSE" (Revision 42):
- # <xythobuz@xythobuz.de> wrote this file. As long as you retain this notice
- # you can do whatever you want with this stuff. If we meet some day, and you
- # think this stuff is worth it, you can buy me a beer in return. Thomas Buck
- # ----------------------------------------------------------------------------
-
- import sys
-
- cachedTarget = None
- targetPlatform = None
- wifiConnected = False
-
- def isPi():
- global targetPlatform
-
- if targetPlatform == None:
- getTarget()
-
- return targetPlatform == "pi"
-
- def isPico():
- global targetPlatform
-
- if targetPlatform == None:
- getTarget()
-
- return targetPlatform == "pico"
-
- def getTarget(i = None):
- global targetPlatform, cachedTarget
-
- if cachedTarget != None:
- return cachedTarget
-
- target = None
- try:
- # First we try the Raspberry Pi interface
- from pi import PiMatrix
- pi = PiMatrix()
-
- # TODO hard-coded adjustments
- from mapper import MapperReduceBrightness, MapperColorAdjust, MapperStripToRect
- bright = MapperReduceBrightness(pi, i)
- #col = MapperColorAdjust(bright)
- #target = col
- #target = MapperStripToRect(col)
- target = MapperStripToRect(bright)
-
- if targetPlatform == None:
- # only print once
- print("Raspberry Pi Adafruit RGB LED Matrix detected")
- targetPlatform = "pi"
- except Exception as e:
- target = None
-
- print()
- if hasattr(sys, "print_exception"):
- sys.print_exception(e)
- else:
- print(e)
- print()
-
- try:
- # Next we try the Pico Interstate75 interface
- from pico import PicoMatrix
- pico = PicoMatrix(i)
-
- # TODO hard-coded adjustments
- from mapper import MapperReduceBrightness
- target = MapperReduceBrightness(pico, i)
-
- if targetPlatform == None:
- # only print once
- print("Raspberry Pi Pico Interstate75 RGB LED Matrix detected")
- targetPlatform = "pico"
- except Exception as e:
- target = None
-
- print()
- if hasattr(sys, "print_exception"):
- sys.print_exception(e)
- else:
- print(e)
- print()
-
- # If this fails fall back to the SDL/pygame GUI
- from test import TestGUI
- target = TestGUI()
-
- if targetPlatform == None:
- # only print once
- print("Falling back to GUI debug interface")
- targetPlatform = "tk"
-
- cachedTarget = target
- return target
-
- # https://github.com/raspberrypi/pico-examples/blob/master/pico_w/wifi/python_test_tcp/micropython_test_tcp_client.py
- def connectToWiFi():
- global wifiConnected
-
- if wifiConnected:
- return True
-
- # only use WiFi on Pico
- try:
- from pico import PicoMatrix
- except Exception as e:
- print()
- if hasattr(sys, "print_exception"):
- sys.print_exception(e)
- else:
- print(e)
- print()
-
- wifiConnected = True
- return True
-
- import network
- import time
- from config import Config
-
- # Check if wifi details have been set
- if len(Config.networks) == 0:
- print('Please set wifi ssid and password in config.py')
- wifiConnected = False
- return False
-
- # Start WiFi hardware
- wlan = network.WLAN(network.STA_IF)
- wlan.active(True)
-
- # Look for known networks
- visible = wlan.scan()
- ssid = None
- user = None
- password = None
-
- print(visible)
- if len(visible) == 0:
- print("No networks visible at all")
- wifiConnected = False
- return False
-
- for name, a, b, c, d, e in visible:
- for net in Config.networks:
- if len(net) == 2:
- t_ssid, t_password = net
- elif len(net) == 3:
- t_ssid, t_user, t_password = net
-
- if name.decode("utf-8") == t_ssid:
- ssid = t_ssid
- if len(net) == 3:
- user = t_user
- password = t_password
- break
- if (ssid == None) or (password == None):
- print("No known network found")
- wifiConnected = False
- return False
-
- # Start connection
- if user != None:
- wlan.seteap(user, password)
- wlan.connect(ssid)
- else:
- wlan.connect(ssid, password)
-
- # Wait for connect success or failure
- max_wait = 40
- error_count = max_wait
- while max_wait > 0:
- if wlan.status() >= 3:
- break
- elif wlan.status() < 0:
- wlan.connect(ssid, password)
- error_count -= 1
- if error_count <= 0:
- break
- else:
- max_wait -= 1
- print('waiting for connection...')
- time.sleep(0.5)
-
- # Handle connection error
- if wlan.status() != 3:
- print('wifi connection failed %d' % wlan.status())
- wifiConnected = False
- return False
- else:
- print('connected')
- status = wlan.ifconfig()
- print('ip = ' + status[0])
-
- wifiConnected = True
- return True
-
- def getRequests():
- global wifiConnected
-
- try:
- # try to get normal python lib
- import requests
- return requests.get, requests.post
- except Exception as e:
- print()
- if hasattr(sys, "print_exception"):
- sys.print_exception(e)
- else:
- print(e)
- print()
-
- # if it fails try the Pi Pico MicroPython implementation
- import urequests as requests
-
- # in this case we also need to connect to WiFi first
- if not wifiConnected:
- if not connectToWiFi():
- return None, None
-
- return requests.get, requests.post
-
- return None, None
-
- def getTextDrawer():
- try:
- # Try BDF parser library
- from bdf import DrawText
- return DrawText
- except Exception as e:
- print()
- if hasattr(sys, "print_exception"):
- sys.print_exception(e)
- else:
- print(e)
- print()
-
- # fall back to the Pico Interstate75 implementation
- from pico import PicoText
- return PicoText
-
- return None
-
- def getInput():
- try:
- # try evdev library
- from gamepad import InputWrapper
- return InputWrapper()
- except Exception as e:
- print()
- if hasattr(sys, "print_exception"):
- sys.print_exception(e)
- else:
- print(e)
- print()
-
- # fall back to the Pico Interstate75 implementation
- from pico import PicoInput
- return PicoInput()
-
- return None
-
- def loop(gui, func = None):
- while True:
- if gui.loop_start():
- break
- if func != None:
- func()
- gui.loop_end()
- gui.exit()
|