Serial flashing while controller is running is possible

This commit is contained in:
Ollo 2021-02-10 23:05:54 +01:00
parent 3059744dd7
commit b5f36e5445
4 changed files with 53 additions and 23 deletions

View File

@ -6,10 +6,10 @@ local t=nil
function handleSingleCommand(client, topic, data) function handleSingleCommand(client, topic, data)
if (data == "ON") then if (data == "ON") then
briPer=100 briPercent=100
m:publish(mqttPrefix .. "/clock", "ON", 0, 0) m:publish(mqttPrefix .. "/clock", "ON", 0, 0)
elseif (data == "OFF") then elseif (data == "OFF") then
briPer=0 briPercent=0
m:publish(mqttPrefix .. "/clock", "OFF", 0, 0) m:publish(mqttPrefix .. "/clock", "OFF", 0, 0)
elseif ((data:sub(1,1) == "#" and data:len() == 7) or (string.match(data, "%d+,%d+,%d+"))) then elseif ((data:sub(1,1) == "#" and data:len() == 7) or (string.match(data, "%d+,%d+,%d+"))) then
local red=0 local red=0
@ -22,7 +22,7 @@ function handleSingleCommand(client, topic, data)
else else
red, green, blue = string.match(data, "(%d+),(%d+),(%d+)") red, green, blue = string.match(data, "(%d+),(%d+),(%d+)")
end end
colorBg=string.char(green * briPer / 100, red * briPer / 100, blue * briPer / 100) colorBg=string.char(green * briPercent / 100, red * briPercent / 100, blue * briPercent / 100)
print("Updated BG: " .. tostring(red) .. "," .. tostring(green) .. "," .. tostring(blue) ) print("Updated BG: " .. tostring(red) .. "," .. tostring(green) .. "," .. tostring(blue) )
m:publish(mqttPrefix .. "/background", tostring(red) .. "," .. tostring(green) .. "," .. tostring(blue), 0, 0) m:publish(mqttPrefix .. "/background", tostring(red) .. "," .. tostring(green) .. "," .. tostring(blue), 0, 0)
if (displayTime~= nil) then if (displayTime~= nil) then
@ -30,7 +30,7 @@ function handleSingleCommand(client, topic, data)
end end
else else
if (tonumber(data) >= 0 and tonumber(data) <= 100) then if (tonumber(data) >= 0 and tonumber(data) <= 100) then
briPer=tonumber(data) briPercent=tonumber(data)
m:publish(mqttPrefix .. "/clock", tostring(data), 0, 0) m:publish(mqttPrefix .. "/clock", tostring(data), 0, 0)
else else
print "Unknown MQTT command" print "Unknown MQTT command"
@ -55,7 +55,7 @@ function parseBgColor(data, row)
end end
if ((red ~= nil) and (green ~= nil) and (blue ~= nil) ) then if ((red ~= nil) and (green ~= nil) and (blue ~= nil) ) then
m:publish(mqttPrefix .. "/"..row, tostring(red) .. "," .. tostring(green) .. "," .. tostring(blue), 0, 0) m:publish(mqttPrefix .. "/"..row, tostring(red) .. "," .. tostring(green) .. "," .. tostring(blue), 0, 0)
return string.char(green * briPer / 100, red * briPer / 100, blue * briPer / 100) return string.char(green * briPercent / 100, red * briPercent / 100, blue * briPercent / 100)
else else
return nil return nil
end end
@ -116,7 +116,7 @@ function registerMqtt()
end end
end) end)
m:connect(mqttServer, 1883, 0, function(client) m:connect(mqttServer, 1883, false, function(client)
print("MQTT is connected") print("MQTT is connected")
mqttConnected = true mqttConnected = true
-- subscribe topic with qos = 0 -- subscribe topic with qos = 0
@ -158,15 +158,15 @@ function startMqttClient()
if (t ~= nil) then if (t ~= nil) then
temp=readTemp() temp=readTemp()
end end
if (oldBrightness ~= briPer) then if (oldBrightness ~= briPercent) then
m:publish(mqttPrefix .. "/brightness", tostring(briPer), 0, 0) m:publish(mqttPrefix .. "/brightness", tostring(briPercent), 0, 0)
elseif (temp ~= nil and temp ~= oldTemp) then elseif (temp ~= nil and temp ~= oldTemp) then
oldTemp = temp oldTemp = temp
m:publish(mqttPrefix .. "/temp", tostring(temp/100).."."..tostring(temp%100), 0, 0) m:publish(mqttPrefix .. "/temp", tostring(temp/100).."."..tostring(temp%100), 0, 0)
else else
m:publish(mqttPrefix .. "/heap", tostring(node.heap()), 0, 0) m:publish(mqttPrefix .. "/heap", tostring(node.heap()), 0, 0)
end end
oldBrightness = briPer oldBrightness = briPercent
end end
end) end)
mqtttimer:start() mqtttimer:start()

View File

@ -8,6 +8,8 @@ DIET=bin/luasrcdiet
DEVICE=$1 DEVICE=$1
BAUD=115200 BAUD=115200
LUASCRIPT_STOP=${TOOLDIR}/stopController.lua
# check environment # check environment
if [ ! -f $LUATOOL ]; then if [ ! -f $LUATOOL ]; then
echo "$LUATOOL not found" echo "$LUATOOL not found"
@ -59,6 +61,17 @@ if [ $# -eq 1 ]; then
echo "STOOOOP" echo "STOOOOP"
exit 1 exit 1
fi fi
else
echo "Reboot ESP and stop init timer"
if [ ! -f $LUASCRIPT_STOP ]; then
echo "Cannot find $LUASCRIPT_STOP"
exit 1
fi
python3 $LUATOOL -p $DEVICE -f $LUASCRIPT_STOP -b $BAUD --volatile --delay 2
if [ $? -ne 0 ]; then
echo "Could not reboot"
exit 1
fi
fi fi
echo "Start Flasing ..." echo "Start Flasing ..."
@ -78,7 +91,9 @@ for f in $FILES; do
fi fi
done done
echo "Reboot the ESP" if [ $# -eq 1 ]; then
echo "node.restart()" >> $DEVICE echo "Reboot the ESP"
echo "node.restart()" >> $DEVICE
fi
exit 0 exit 0

View File

@ -54,6 +54,9 @@ class AbstractTransport:
def writer(self, data): def writer(self, data):
self.writeln("file.writeline([==[" + data + "]==])\r") self.writeln("file.writeline([==[" + data + "]==])\r")
def execute(self, data):
self.writeln(data + "\r")
def performcheck(self, expected): def performcheck(self, expected):
line = '' line = ''
char = '' char = ''
@ -190,6 +193,7 @@ if __name__ == '__main__':
parser.add_argument('-e', '--echo', action='store_true', help='Echo output of MCU until script is terminated.') parser.add_argument('-e', '--echo', action='store_true', help='Echo output of MCU until script is terminated.')
parser.add_argument('--delay', default=0.3, help='Delay in seconds between each write.', type=float) parser.add_argument('--delay', default=0.3, help='Delay in seconds between each write.', type=float)
parser.add_argument('--delete', default=None, help='Delete a lua/lc file from device.') parser.add_argument('--delete', default=None, help='Delete a lua/lc file from device.')
parser.add_argument('--volatile', action='store_true', help='Volatile (nothing stored on ESP)')
parser.add_argument('--ip', default=None, help='Connect to a telnet server on the device (--ip IP[:port])') parser.add_argument('--ip', default=None, help='Connect to a telnet server on the device (--ip IP[:port])')
args = parser.parse_args() args = parser.parse_args()
@ -201,7 +205,7 @@ if __name__ == '__main__':
char = transport.read(1) char = transport.read(1)
if char == '' or char == chr(62): if char == '' or char == chr(62):
break break
sys.stdout.write(char) sys.stdout.write(str.encode(char))
sys.exit(0) sys.exit(0)
if args.id: if args.id:
@ -212,7 +216,7 @@ if __name__ == '__main__':
if char == '' or char == chr(62): if char == '' or char == chr(62):
break break
if char.isdigit(): if char.isdigit():
id += char id += char.decode("utf-8")
print("\n"+id) print("\n"+id)
sys.exit(0) sys.exit(0)
@ -222,10 +226,10 @@ if __name__ == '__main__':
fn = "" fn = ""
while True: while True:
char = transport.read(1) char = transport.read(1)
if char == '' or ord(char) == 62: if char == '' or len(char) == 0 or ord(char) == 62:
break break
if char not in ['\r', '\n']: if char not in ['\r', '\n']:
fn += char.decode("utf-8") fn += str(char)
else: else:
if fn: if fn:
file_list.append(fn.strip()) file_list.append(fn.strip())
@ -270,7 +274,7 @@ if __name__ == '__main__':
sys.stderr.write("Upload starting\r\n") sys.stderr.write("Upload starting\r\n")
# remove existing file on device # remove existing file on device
if args.append==False: if args.append==False and not args.volatile:
if args.verbose: if args.verbose:
sys.stderr.write("Stage 1. Deleting old file from flash memory") sys.stderr.write("Stage 1. Deleting old file from flash memory")
transport.writeln("file.open(\"" + args.dest + "\", \"w\")\r") transport.writeln("file.open(\"" + args.dest + "\", \"w\")\r")
@ -284,23 +288,31 @@ if __name__ == '__main__':
# read source file line by line and write to device # read source file line by line and write to device
if args.verbose: if args.verbose:
sys.stderr.write("\r\nStage 2. Creating file in flash memory and write first line") sys.stderr.write("\r\nStage 2. Creating file in flash memory and write first line")
if args.append: if not args.volatile:
transport.writeln("file.open(\"" + args.dest + "\", \"a+\")\r") if args.append:
transport.writeln("file.open(\"" + args.dest + "\", \"a+\")\r")
else:
transport.writeln("file.open(\"" + args.dest + "\", \"w+\")\r")
else: else:
transport.writeln("file.open(\"" + args.dest + "\", \"w+\")\r") if args.verbose:
sys.stderr.write("\r\nStage 2. Directly execute the script...")
line = f.readline() line = f.readline()
if args.verbose: if args.verbose:
sys.stderr.write("\r\nStage 3. Start writing data to flash memory...") sys.stderr.write("\r\nStage 3. Start writing data to flash memory...")
while line != '': while line != '':
transport.writer(line.strip()) if args.volatile:
transport.execute(line.strip())
else:
transport.writer(line.strip())
line = f.readline() line = f.readline()
# close both files # close both files
f.close() f.close()
if args.verbose: if args.verbose:
sys.stderr.write("\r\nStage 4. Flush data and closing file") sys.stderr.write("\r\nStage 4. Flush data and closing file")
transport.writeln("file.flush()\r") if not args.volatile:
transport.writeln("file.close()\r") transport.writeln("file.flush()\r")
transport.writeln("file.close()\r")
# compile? # compile?
if args.compile: if args.compile:
@ -319,7 +331,8 @@ if __name__ == '__main__':
if args.verbose: if args.verbose:
sys.stderr.write("\r\nEchoing MCU output, press Ctrl-C to exit") sys.stderr.write("\r\nEchoing MCU output, press Ctrl-C to exit")
while True: while True:
sys.stdout.write(transport.read(1)) data = transport.read(1)
sys.stdout.write( data.decode("ascii") )
# close serial port # close serial port
transport.close() transport.close()

2
tools/stopController.lua Normal file
View File

@ -0,0 +1,2 @@
node.restart()
if (initTimer ~= nil) then initTimer:unregister() end