This commit is contained in:
Tassilo Schweyer 2025-12-28 23:51:17 +01:00
parent 584b1a73a7
commit 5362c3079f

View file

@ -29,6 +29,15 @@ def do_connect(tries=0):
print(sta_if.ifconfig()) print(sta_if.ifconfig())
return return
def do_ap():
ap_if = network.WLAN(network.WLAN.IF_AP)
ap_if.active(True)
ap_if.config(ssid='My AP123', channel=11, key='asdf1234')
ap_if.ifconfig(('192.168.0.4', '255.255.255.0', '192.168.0.1', '8.8.8.8'))
print(ap_if.config('ssid'))
print(ap_if.config('channel'))
print('AP ready.')
def setup_listener(): def setup_listener():
addr = socket.getaddrinfo('0.0.0.0', 1337)[0][-1] addr = socket.getaddrinfo('0.0.0.0', 1337)[0][-1]
@ -43,19 +52,23 @@ MOTOR_A0 = Pin(0, Pin.OUT, drive=Pin.DRIVE_0)
MOTOR_A1 = Pin(1, 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_B0 = Pin(2, Pin.OUT, drive=Pin.DRIVE_0)
MOTOR_B1 = Pin(3, Pin.OUT, drive=Pin.DRIVE_0) MOTOR_B1 = Pin(3, Pin.OUT, drive=Pin.DRIVE_0)
LED_DAT = Pin(5, Pin.OUT) LED_DAT = Pin(6, Pin.OUT, drive=Pin.DRIVE_3)
LED_NP = neopixel.NeoPixel(LED_DAT, 8) LED_NP = neopixel.NeoPixel(LED_DAT, 6, timing=1, bpp=4)
do_connect() #do_connect()
do_ap()
s = setup_listener() s = setup_listener()
c = [] c = []
def controlloop(last_start, next_action): def controlloop(last_start, next_action):
if time.time() < last_start + 0.5: #print('CTRL: ', last_start, next_action)
if time.ticks_us() < last_start + 100000:
#print(' DEADTIME -> ignore')
return last_start return last_start
if next_action is None and time.time() > last_start + 1.000: if next_action is None and time.ticks_us() > last_start + 600000:
#print(' ACTION TIMED OUT -> stopping')
next_action = (0,0) next_action = (0,0)
elif next_action is None: elif next_action is None:
return last_start return last_start
@ -83,61 +96,60 @@ def controlloop(last_start, next_action):
MOTOR_B1.off() MOTOR_B1.off()
# we just started new action # we just started new action
return time.time() return time.ticks_us()
def mainloop(s): def mainloop(l):
poller = select.poll() print('Accepting connection')
poller.register(s, select.POLLIN) (y, origin_sockaddr) = s.accept()
y.settimeout(0.1)
y.write("\255\254\34Welcome!\nPress wasd to move!\n>")
last_action_start = 0 last_action_start = 0
ctr = 0 ctr = 0
next_action = None next_action = None
last_pixel_update = 0
for i in range(6):
LED_NP[i] = (255,255,255, 255)
LED_NP.write()
time.sleep(1)
while True: while True:
ctr += 1
# run one iteration of control loop # run one iteration of control loop
last_action_start = controlloop(last_action_start, next_action) last_action_start = controlloop(last_action_start, next_action)
LED_NP[ctr % 8] = (ctr*3 % 255, ctr*2 % 255, (ctr+1)*3 % 255) if time.time() > last_pixel_update + 0.5:
#LED_NP[ctr % 6] = (ctr*3 % 255, ctr*2 % 255, (ctr+1)*3 % 255)
# wait for up to 100ms last_pixel_update = time.time()
res = poller.poll(100) LED_NP.write()
if not res:
next_action = None
continue
for x in res: a = None
print(x) try:
if x[0] == s: a = y.read(1)
print('Accepting connection') print('RCV: ', a)
(y, origin_sockaddr) = s.accept() y.write(a)
#y.write("\377\375\042\377\373\001Welcome!\nPress wasd to move!\n>") except OSError:
y.write("\255\254\34Welcome!\nPress wasd to move!\n>") # timeout
#c.append(y) a = b'x'
print(' Registering handler') if a == b'':
poller.register(y, select.POLLIN) # EOF
else: y.close()
# some user socket mainloop(l)
user_s = x[0] elif a == b'w':
if x[1] == select.POLLIN: next_action = (1, 1)
a = user_s.read(1) elif a == b'a':
print('Read character: ', a) next_action = (0, 1)
if a == b'w': elif a == b'd':
next_action = (1, 1) next_action = (1, 0)
elif a == b'a': elif a == b's':
next_action = (0, 1) next_action = (-1, -1)
elif a == b'd': elif a in [b'\n', b'\r']:
next_action = (1, 0) pass
elif a == b's': else:
next_action = (-1, -1) next_action = None
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()