From 1bb6b75716b02fe3abdb1842248d21c723268cee Mon Sep 17 00:00:00 2001 From: Maximilian Wagner Date: Thu, 25 Apr 2024 13:15:27 +0200 Subject: [PATCH] fixes some bugs, untested --- main.py | 7 +++--- src/server.py | 63 +++++++++++++++++++++++++++++++++------------------ src/slave.py | 17 +++++++++++++- 3 files changed, 60 insertions(+), 27 deletions(-) diff --git a/main.py b/main.py index 48da2ad..b440c18 100644 --- a/main.py +++ b/main.py @@ -1,4 +1,5 @@ from machine import Pin +import src.server as server import src.slave as slave import src.wireless as wireless import src.data as data @@ -7,10 +8,8 @@ import src.data as data wireless.espnow_setup() if data.master: - # server has autostart in another _thread - import src.server as server + server.start() Pin(2, Pin.OUT).value(1) else: - Pin(2, Pin.OUT).value(1) - slave.loop() + slave.loop(Pin(2, Pin.OUT).value(1)) diff --git a/src/server.py b/src/server.py index 64de894..aef7b6e 100644 --- a/src/server.py +++ b/src/server.py @@ -67,23 +67,33 @@ def docs(con, _): @app.route("GET", "/definition") def definition(con, request: HTTPRequest): # Get slave and tm definition from request and send off - tm = "DEFINE:" + unescape(str(request.parameters.get("tm"))) - slave_num = int(str(request.parameters.get("select_slave"))[6:]) -1 - slave = data.slave_list[slave_num] + para_tm = request.parameters.get("tm") + para_slave = request.parameters.get("select_slave") + + if para_tm != None and para_slave != None: + tm = "DEFINE:" + unescape(str(para_tm)) + slave_num = int(str(para_slave)[6:]) -1 + slave = data.slave_list[slave_num] + + print(tm) + espnow_send(slave, tm) - print(tm) - espnow_send(slave, tm) HTTPResponse(con, 302).redirect("/") @app.route("GET", "/input") def input(con, request: HTTPRequest): - # Get slave and input band from request and send off - band = "BAND:" + str(request.parameters.get("band")) - slave_num = int(str(request.parameters.get("select_slave"))[6:]) -1 - slave = data.slave_list[slave_num] + para_band = request.parameters.get("band") + para_ss = request.parameters.get("select_slave") + + if para_band != None and para_ss != None: + # Get slave and input band from request and send off + band = "BAND:" + str(para_band) + slave_num = int(str(para_ss)[6:]) -1 + slave = data.slave_list[slave_num] + + espnow_send(slave, band) - espnow_send(slave, band) HTTPResponse(con, 302).redirect("/") @@ -92,14 +102,18 @@ def run(con, request: HTTPRequest): # use builder to incorporate that into response global status - select_slave = str(request.parameters.get("select_slave")) - if "Slave" in select_slave: - status["slave_selected"]["value"] = unescape(select_slave) + select_slave = request.parameters.get("select_slave") + if select_slave != None: + select_slave = str(select_slave) + if "Slave" in select_slave: + status["slave_selected"]["value"] = unescape(select_slave) - # Go to simulator page where you can run all or step through - HTTPResponse(con, 200, "HTML").send_raw( - HTMLBuilder(["html/head.html", "html/run.html"], status).build() - ) + # Go to simulator page where you can run all or step through + HTTPResponse(con, 200, "HTML").send_raw( + HTMLBuilder(["html/head.html", "html/run.html"], status).build() + ) + + HTTPResponse(con, 302).redirect("/") @app.route("GET", "/run_all") @@ -129,9 +143,15 @@ def run_step(con, _): @app.route("GET", "/pass") def pass_band(con, request: HTTPRequest): + para_sfrom = request.parameters.get("select_slave") + para_sto = request.parameters.get("to_slave") + + if para_sfrom == None or para_sto == None: + HTTPResponse(con, 302).redirect("/") + try: - slave_from = int(str(request.parameters.get("select_slave"))[6:]) -1 - slave_to = int(str(request.parameters.get("to_slave"))[6:]) -1 + slave_from = int(str(para_sfrom)[6:]) -1 + slave_to = int(str(para_sto)[6:]) -1 slave_from = data.slave_list[slave_from] slave_to = data.slave_list[slave_to] @@ -152,7 +172,6 @@ def result(con, _): # and also endresult of computation chain result_list = [] for i in range(0, len(data.slave_list)): - print("test: ", i) # todo: parse result_dict of src.data to display res while not espnow_send(data.slave_list[i], "RESULT"): pass @@ -189,6 +208,6 @@ def reset(con, _): HTTPResponse(con, 302).redirect("/run") - -_thread.start_new_thread(app.start, ()) +def start() -> None: + _thread.start_new_thread(app.start, ()) diff --git a/src/slave.py b/src/slave.py index 954c18f..b453c33 100644 --- a/src/slave.py +++ b/src/slave.py @@ -1,3 +1,5 @@ +import _thread +from time import sleep from src.wireless import espnow_send, espnow_recv, espnow_recv_raw from lib.tm_sim import TuringMachine as TM import src.data as data @@ -66,8 +68,17 @@ def __reset() -> None: print(f"TM:CONFIG:RESET") +def __blink(pin) -> None: + pin.value(0) + sleep(.3) + pin.value(1) + sleep(.3) + pin.value(0) + sleep(.3) + pin.value(1) -def loop(): + +def loop(pin): display.setup() display.slave() display.draw() @@ -75,6 +86,10 @@ def loop(): while True: msg = espnow_recv() + # blinking in new _thread for faster execution + # all below cant be done that way because of race conditions + _thread.start_new_thread(__blink, (pin)) + if "DEFINE" in msg: __define(msg.split(':')[1])