refactor, clean update comments
This commit is contained in:
parent
aabcec3b54
commit
f71eaf69ed
19
main.py
19
main.py
@ -1,9 +1,11 @@
|
|||||||
import _thread
|
import _thread
|
||||||
import ubinascii
|
import ubinascii
|
||||||
from machine import Pin, PWM, Timer
|
|
||||||
|
|
||||||
|
# WebSocket connection
|
||||||
conn = None
|
conn = None
|
||||||
|
# blockly highlihgt WebSocket connection
|
||||||
conn_highlight = None
|
conn_highlight = None
|
||||||
|
# extract a unique name for the robot from the device MAC address
|
||||||
name = "sumo-%s" % ubinascii.hexlify(wlan.config("mac")[-3:]).decode("ascii")
|
name = "sumo-%s" % ubinascii.hexlify(wlan.config("mac")[-3:]).decode("ascii")
|
||||||
|
|
||||||
# remote server
|
# remote server
|
||||||
@ -14,8 +16,11 @@ url_highlight = "ws://iot.koodur.com:80/p2p/" + name + "-highlight/browser/"
|
|||||||
#url = "ws://10.42.0.1:80/p2p/" + name + "/browser/"
|
#url = "ws://10.42.0.1:80/p2p/" + name + "/browser/"
|
||||||
#url_highlight = "ws://10.42.0.1:80/p2p/" + name + "-highlight/browser/"
|
#url_highlight = "ws://10.42.0.1:80/p2p/" + name + "-highlight/browser/"
|
||||||
|
|
||||||
|
# code to execute
|
||||||
ast = ""
|
ast = ""
|
||||||
|
# scope, info to be sent to the client
|
||||||
scope = dict()
|
scope = dict()
|
||||||
|
# SumoRobot object
|
||||||
sumorobot = None
|
sumorobot = None
|
||||||
|
|
||||||
def step():
|
def step():
|
||||||
@ -31,9 +36,11 @@ def step():
|
|||||||
)
|
)
|
||||||
# execute code
|
# execute code
|
||||||
exec(ast)
|
exec(ast)
|
||||||
|
# when robot was stopped
|
||||||
if sumorobot.terminate:
|
if sumorobot.terminate:
|
||||||
# disable forceful termination
|
# disable forceful termination of delays in code
|
||||||
sumorobot.terminate = False
|
sumorobot.terminate = False
|
||||||
|
# stop the robot
|
||||||
sumorobot.move(STOP)
|
sumorobot.move(STOP)
|
||||||
# leave time to process WebSocket commands
|
# leave time to process WebSocket commands
|
||||||
sleep_ms(50)
|
sleep_ms(50)
|
||||||
@ -41,7 +48,6 @@ def step():
|
|||||||
def ws_handler():
|
def ws_handler():
|
||||||
global ast
|
global ast
|
||||||
global conn
|
global conn
|
||||||
global no_delay
|
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
@ -72,13 +78,14 @@ def ws_handler():
|
|||||||
elif data == b"ping":
|
elif data == b"ping":
|
||||||
conn.send(repr(scope))
|
conn.send(repr(scope))
|
||||||
elif data.startswith("start:"):
|
elif data.startswith("start:"):
|
||||||
print("Got code")#, data[6:])
|
#print("Got code:", data[6:])
|
||||||
ast = compile(data[6:], "snippet", "exec")
|
ast = compile(data[6:], "snippet", "exec")
|
||||||
elif data == b"stop":
|
elif data == b"stop":
|
||||||
ast = ""
|
ast = ""
|
||||||
sumorobot.move(STOP)
|
sumorobot.move(STOP)
|
||||||
|
# for terminating delays in code
|
||||||
sumorobot.terminate = True
|
sumorobot.terminate = True
|
||||||
print("Got stop")
|
#print("Got stop")
|
||||||
elif b"Gone" in data:
|
elif b"Gone" in data:
|
||||||
print("Server said 410 Gone, attempting to reconnect...")
|
print("Server said 410 Gone, attempting to reconnect...")
|
||||||
conn = uwebsockets.connect(url)
|
conn = uwebsockets.connect(url)
|
||||||
@ -98,7 +105,7 @@ print("Sending ping")
|
|||||||
conn.send("{'ping': true}")
|
conn.send("{'ping': true}")
|
||||||
conn.send("{'ip': '" + wlan.ifconfig()[0] + "'}")
|
conn.send("{'ip': '" + wlan.ifconfig()[0] + "'}")
|
||||||
|
|
||||||
# connect to the block highlight websocket
|
# connect to the blockly highlight websocket
|
||||||
conn_highlight = uwebsockets.connect(url_highlight)
|
conn_highlight = uwebsockets.connect(url_highlight)
|
||||||
|
|
||||||
# initialize SumoRobot object
|
# initialize SumoRobot object
|
||||||
|
Loading…
Reference in New Issue
Block a user