Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- ############################################
- -- radio_ctrl (turtle side)
- -- version 0.2
- -- http://hevohevo.hatenablog.com/
- -- 1. Refer to controller side program (http://pastebin.com/h93ywUvS).
- -- 2. Input to the following and reboot TurtleOS
- -- > pastebin get z6QVrviC startup
- -- ################## config ##################
- ModemSide = "right"
- Logfile = "Logfile"
- ControllerID = nil -- set ID, if you need to restrict controller ID.
- -- ################## functions ###############
- function writeLogfile(filename, str, err)
- local fh = fs.open(filename,"a")
- if not err then
- fh.writeLine(str)
- else
- fh.writeLine("--[[ "..err)
- fh.writeLine(str)
- fh.writeLine("--]]")
- end
- fh.close()
- end
- function eval(str)
- local status, err = pcall(loadstring(str))
- if status then -- success
- if string.find(str, "os.run") then
- writeLogfile(Logfile, str, "os.run: Logfile")
- elseif string.find(str, "clearLogfile") then
- -- do nothing
- else
- writeLogfile(Logfile, str)
- end
- else -- error
- writeLogfile(Logfile, str, err)
- print('Error: ',err)
- end
- end
- function clearLogfile()
- if fs.exists(Logfile) then fs.delete(Logfile) end
- local fh = fs.open(Logfile,"w")
- fh.writeLine("-- received messages log")
- fh.close()
- end
- -- ###############################
- -- Define usable functions in received messages
- _G["clearLogfile"] = clearLogfile
- -- ###############################
- -- Main
- rednet.open(ModemSide)
- clearLogfile()
- repeat
- local sender_id, message, distance = rednet.receive()
- print(message)
- if not ControllerID or (sender_id == ControllerID) then eval(message) end
- until message == "exit()"
Advertisement
Add Comment
Please, Sign In to add comment