147 lines
3.9 KiB
Python
147 lines
3.9 KiB
Python
import network
|
|
import time
|
|
import socket
|
|
import select
|
|
import neopixel
|
|
from machine import Pin
|
|
|
|
def do_connect(tries=0):
|
|
if tries > 3:
|
|
machine.reset()
|
|
print('Trying to connect to Wifi')
|
|
sta_if = network.WLAN(network.WLAN.IF_STA)
|
|
sta_if.active(True)
|
|
sta_if.disconnect()
|
|
attempts = 0
|
|
while True:
|
|
attempts += 1
|
|
print(' Scan: ', sta_if.scan())
|
|
sta_if.connect("39C3-open", "")
|
|
time.sleep(10)
|
|
if not sta_if.isconnected():
|
|
print(' Failed to connect. Sleeping 5s')
|
|
sta_if.disconnect()
|
|
time.sleep(5)
|
|
if attempts > 3:
|
|
sta_if.active(False)
|
|
do_connect(tries+1)
|
|
else:
|
|
print(sta_if.ifconfig())
|
|
return
|
|
|
|
|
|
def setup_listener():
|
|
addr = socket.getaddrinfo('0.0.0.0', 1337)[0][-1]
|
|
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
s.bind(addr)
|
|
s.listen(3)
|
|
#s.setblocking(False)
|
|
return s
|
|
|
|
|
|
MOTOR_A0 = Pin(0, Pin.OUT, drive=Pin.DRIVE_0)
|
|
MOTOR_A1 = Pin(1, Pin.OUT, drive=Pin.DRIVE_0)
|
|
MOTOR_B0 = Pin(2, Pin.OUT, drive=Pin.DRIVE_0)
|
|
MOTOR_B1 = Pin(3, Pin.OUT, drive=Pin.DRIVE_0)
|
|
LED_DAT = Pin(5, Pin.OUT)
|
|
LED_NP = neopixel.NeoPixel(LED_DAT, 8)
|
|
|
|
|
|
do_connect()
|
|
s = setup_listener()
|
|
c = []
|
|
|
|
def controlloop(last_start, next_action):
|
|
if time.time() < last_start + 0.5:
|
|
return last_start
|
|
|
|
if next_action is None and time.time() > last_start + 1.000:
|
|
next_action = (0,0)
|
|
elif next_action is None:
|
|
return last_start
|
|
|
|
print('Starting action: ', next_action)
|
|
|
|
if next_action[0] == 1:
|
|
MOTOR_A0.on()
|
|
MOTOR_A1.off()
|
|
elif next_action[0] == -1:
|
|
MOTOR_A0.off()
|
|
MOTOR_A1.on()
|
|
else:
|
|
MOTOR_A0.off()
|
|
MOTOR_A1.off()
|
|
|
|
if next_action[1] == 1:
|
|
MOTOR_B0.on()
|
|
MOTOR_B1.off()
|
|
elif next_action[1] == -1:
|
|
MOTOR_B0.off()
|
|
MOTOR_B1.on()
|
|
else:
|
|
MOTOR_B0.off()
|
|
MOTOR_B1.off()
|
|
|
|
# we just started new action
|
|
return time.time()
|
|
|
|
|
|
def mainloop(s):
|
|
poller = select.poll()
|
|
poller.register(s, select.POLLIN)
|
|
|
|
last_action_start = 0
|
|
ctr = 0
|
|
|
|
next_action = None
|
|
while True:
|
|
# run one iteration of control loop
|
|
last_action_start = controlloop(last_action_start, next_action)
|
|
LED_NP[ctr % 8] = (ctr*3 % 255, ctr*2 % 255, (ctr+1)*3 % 255)
|
|
|
|
# wait for up to 100ms
|
|
res = poller.poll(100)
|
|
if not res:
|
|
next_action = None
|
|
continue
|
|
|
|
for x in res:
|
|
print(x)
|
|
if x[0] == s:
|
|
print('Accepting connection')
|
|
(y, origin_sockaddr) = s.accept()
|
|
#y.write("\377\375\042\377\373\001Welcome!\nPress wasd to move!\n>")
|
|
y.write("\255\254\34Welcome!\nPress wasd to move!\n>")
|
|
#c.append(y)
|
|
print(' Registering handler')
|
|
poller.register(y, select.POLLIN)
|
|
else:
|
|
# some user socket
|
|
user_s = x[0]
|
|
if x[1] == select.POLLIN:
|
|
a = user_s.read(1)
|
|
print('Read character: ', a)
|
|
if a == b'w':
|
|
next_action = (1, 1)
|
|
elif a == b'a':
|
|
next_action = (0, 1)
|
|
elif a == b'd':
|
|
next_action = (1, 0)
|
|
elif a == b's':
|
|
next_action = (-1, -1)
|
|
elif a == b'':
|
|
print('EOF in ', x[0], ' Removing it.')
|
|
poller.unregister(x[0])
|
|
x[0].close()
|
|
elif x[1] in [select.POLLHUP, select.POLLERR]:
|
|
print('Error in ', x[0], ' Removing it.')
|
|
poller.unregister(x[0])
|
|
#del c[x[0]]
|
|
x[0].close()
|
|
|
|
|
|
|
|
|
|
mainloop(s)
|
|
|
|
|