From 5362c3079f332a1a506a60dc5de1df45698a1d8f Mon Sep 17 00:00:00 2001 From: Tassilo Schweyer Date: Sun, 28 Dec 2025 23:51:17 +0100 Subject: [PATCH] tweak --- shittyrobot.py | 110 +++++++++++++++++++++++++++---------------------- 1 file changed, 61 insertions(+), 49 deletions(-) diff --git a/shittyrobot.py b/shittyrobot.py index 6693e90..fbe817c 100644 --- a/shittyrobot.py +++ b/shittyrobot.py @@ -29,6 +29,15 @@ def do_connect(tries=0): print(sta_if.ifconfig()) 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(): 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_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) +LED_DAT = Pin(6, Pin.OUT, drive=Pin.DRIVE_3) +LED_NP = neopixel.NeoPixel(LED_DAT, 6, timing=1, bpp=4) -do_connect() +#do_connect() +do_ap() s = setup_listener() c = [] 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 - 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) elif next_action is None: return last_start @@ -83,61 +96,60 @@ def controlloop(last_start, next_action): MOTOR_B1.off() # we just started new action - return time.time() + return time.ticks_us() -def mainloop(s): - poller = select.poll() - poller.register(s, select.POLLIN) +def mainloop(l): + print('Accepting connection') + (y, origin_sockaddr) = s.accept() + y.settimeout(0.1) + y.write("\255\254\34Welcome!\nPress wasd to move!\n>") last_action_start = 0 ctr = 0 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: + ctr += 1 # 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 + if time.time() > last_pixel_update + 0.5: + #LED_NP[ctr % 6] = (ctr*3 % 255, ctr*2 % 255, (ctr+1)*3 % 255) + last_pixel_update = time.time() + LED_NP.write() - 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() + a = None + try: + a = y.read(1) + print('RCV: ', a) + y.write(a) + except OSError: + # timeout + a = b'x' + if a == b'': + # EOF + y.close() + mainloop(l) + elif 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 in [b'\n', b'\r']: + pass + else: + next_action = None