Posted 09 October 2012 - 07:43 PM
I had a hard time finding a simple, capable remote control program for a turtle, so I came up with this.
Once a rednet message matching the password is received, the turtle will execute any turtle API command it receives from the ID that send the password. A corresponding controller program is useful but not necessary. What may be needed is to add additional functions (stored in a table named "turtle" for easy access) to provide additional functionality, since the program currently lacks a way to pass arguments to the functions it calls (which is why turtle.select() and such are not supported).
cldt = {}
function trply(mssgstr) -- i.e. "unbreakable block in path"
print(mssgstr)
if cldt.rcID then
rednet.send(cldt.rcID,mssgstr) end
end
rednet.open("right")
print("Enter confirmation password")
cldt[1] = io.read()
print("Send password from control terminal")
repeat cldt[2], cldt[3] = rednet.receive() -- you can set a timeout value here, but I didn't
if cldt[3] == cldt[1] then cldt.rcID = cldt[2] trply("password accepted") break end
until not cldt[2]
while cldt.rcID do cldt[2], cldt[3] = rednet.receive()
if cldt[2] == cldt.rcID then
if turtle[cldt[3]] then turtle[cldt[3]]()
elseif cldt[3] == "trmn8RC" then cldt.rcID = nil end
end
end
print("Remote Control Terminated")
Once a rednet message matching the password is received, the turtle will execute any turtle API command it receives from the ID that send the password. A corresponding controller program is useful but not necessary. What may be needed is to add additional functions (stored in a table named "turtle" for easy access) to provide additional functionality, since the program currently lacks a way to pass arguments to the functions it calls (which is why turtle.select() and such are not supported).
rcmdlst = {
[79] = "place",
[71] = "placeUp",
[82] = "placeDown",
[83] = "drop",
[210] = "place",
[211] = "dig",
[199] = "dropUp",
[207] = "dropDown",
[201] = "up",
[209] = "down",
[200] = "forward",
[208] = "back",
[203] = "turnLeft",
[205] = "turnRight",
[26] = "digUp",
[27] = "digDown",
[28] = "suck",
[51] = "suckUp",
[52] = "suckDown",
[45] = "trmn8RC",
}
cldt = {}
shell.run('clear')
print("Enter confirmation password for remote turtle control")
if not rednet.broadcast(io.read()) then print("Modem Access Error") return false
else cldt[1],cldt[2] = rednet.receive() print(cldt[1],cldt[2]) end
if cldt[2] == "password accepted" then
print("Use numberpad to control turtleID"..cldt[1]..[[
170 to place[Up/Down]
'.' Home End, to drop[Up/Down]
Enter, <, >, to suck[Up/Down]
Del, [, ] to dig [Up/Down]
Direction Keys and PgUp/Dn to move
Press x to end Remote Control]])
cldt[3] = false
repeat
cldt[4],cldt[5],cldt[6] = os.pullEvent()
if cldt[4] == "key" and rcmdlst[cldt[5]] then rednet.broadcast(rcmdlst[cldt[5]])
if cldt[5] == 45 then cldt[3] = true end
elseif cldt[4] == "rednet_message" then print(cldt[5]..": "..cldt[6]) end
until cldt[3]
else print("Remote Control confirmation failed") end