From 0cd319935c1f6abc3e733fe0f82258358b62b838 Mon Sep 17 00:00:00 2001 From: "navid.sassan" Date: Sat, 9 Jan 2021 03:24:02 +0100 Subject: [PATCH] added scripts --- burn | 18 +++++++++++++ client | 47 +++++++++++++++++++++++++++++++++ control | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ startup | 5 ++++ 4 files changed, 152 insertions(+) create mode 100644 burn create mode 100644 client create mode 100644 control create mode 100644 startup diff --git a/burn b/burn new file mode 100644 index 0000000..3b9b7d9 --- /dev/null +++ b/burn @@ -0,0 +1,18 @@ +function select_next_item() + local sel_pos = 0 + turtle.select(sel_pos + 1) + + while(turtle.getItemCount() <= 0) do + sel_pos = (sel_pos + 1) % 16 + turtle.select(sel_pos + 1) + end +end + +while(true) do + select_next_item() + for i = 1,4,1 do + turtle.place() + turtle.turnRight() + end + sleep(20) +end diff --git a/client b/client new file mode 100644 index 0000000..dfffe75 --- /dev/null +++ b/client @@ -0,0 +1,47 @@ +function dump(o) + if o == nil then + return "" + end + + if type(o) == 'table' then + local s = '{ ' + for k,v in pairs(o) do + if type(k) ~= 'number' then k = '"'..k..'"' end + s = s .. '['..k..'] = ' .. dump(v) .. ',' + end + return s .. '} ' + end + + return tostring(o) +end + +rednet.open("left") +rednet.host("rmm", "t0-turtle01") +local master = rednet.lookup("rmm", "master") +while true do + local event, id, data = os.pullEvent() + if event == "rednet_message" then + print(id .. " (" .. event .. ")> " .. dump(data)) + if type(data) == 'table' then + if data[1] == 'eval' then + local cmd_fn, err = loadstring(data[2]) + if not cmd_fn then + print("Compile error:" .. err) + rednet.send(master, "Compile error:" .. err) + else + local success, output = pcall(cmd_fn) + if success then + if output then + print("Command output: " .. dump(output)) + rednet.send(master, "Command output: " .. dump(output)) + end + else + print("Runtime error:" .. output) + rednet.send(master, "Runtime error:" .. output) + end + end + end + end + end +end + diff --git a/control b/control new file mode 100644 index 0000000..f738eaf --- /dev/null +++ b/control @@ -0,0 +1,82 @@ +function dump(o) + if o == nil then + return "" + end + + if type(o) == 'table' then + local s = '{ ' + for k,v in pairs(o) do + if type(k) ~= 'number' then k = '"'..k..'"' end + s = s .. '['..k..'] = ' .. dump(v) .. ',' + end + return s .. '} ' + end + + return tostring(o) +end + +function split(s) + chunks = {} + + for substring in s:gmatch("%S+") do + table.insert(chunks, substring) + end + return chunks +end + +function recv() + while true do + local id, data = rednet.receive() + print("Message from " .. id .. ": " .. dump(data)) + write("> ") + end +end + +function move_control(turtle_id) + while true do + local event, param = os.pullEvent("key") + if event == "key" then + -- print("Got key: " .. param) + if param == 14 then + break + elseif param == 17 then + rednet.send(turtle_id, {"eval", "turtle.forward()"}) + elseif param == 30 then + rednet.send(turtle_id, {"eval", "turtle.turnLeft()"}) + elseif param == 31 then + rednet.send(turtle_id, {"eval", "turtle.back()"}) + elseif param == 32 then + rednet.send(turtle_id, {"eval", "turtle.turnRight()"}) + elseif param == 57 then + rednet.send(turtle_id, {"eval", "turtle.up()"}) + elseif param == 29 then + rednet.send(turtle_id, {"eval", "turtle.down()"}) + end + end + end +end + +function control() + while true do + write("> ") + local input = split(read()) + -- print(dump(input)) + if input[1] == 'lookup' then + print(rednet.lookup("rmm", input[2])) + elseif input[1] == 'eval' then + rednet.send(tonumber(input[2]), {"eval", input[3]}) + elseif input[1] == 'move' then + move_control(tonumber(input[2])) + else + print("Unknown command") + end + end +end + +rednet.open("back") +rednet.host("rmm", "master") + +while true do + parallel.waitForAny(recv, control) +end + diff --git a/startup b/startup new file mode 100644 index 0000000..90486be --- /dev/null +++ b/startup @@ -0,0 +1,5 @@ +local script = "script" + +shell.run("rm", script) +shell.run("get", script) +shell.run(script)