RC turtle when

This commit is contained in:
Casey 2024-01-12 21:15:52 +03:00
parent 5a581c519f
commit 1ea1bb7c9e
Signed by: hkc
GPG Key ID: F0F6CFE11CDB0960
3 changed files with 128 additions and 4 deletions

66
turtos/ctrl.lua Normal file
View File

@ -0,0 +1,66 @@
local wlan = peripheral.find("modem", function(addr, modem)
return modem.isWireless()
end)
if not wlan then error("no wireless interface") end
wlan.open(9999)
local respondedTurtles = {}
local keyStates = {}
parallel.waitForAny(
function()
while true do
local ev, keycode, repeating = os.pullEvent()
if ev == "key" then keyStates[keycode] = true
elseif ev == "key_up" then keyStates[keycode] = false
end
if keycode == keys.up then
wlan.transmit(9999, 9999, { _ = "move", dir = "fwd", dig = keyStates[keys.leftShift] })
elseif keycode == keys.down then
wlan.transmit(9999, 9999, { _ = "move", dir = "bck" })
elseif keycode == keys.left then
wlan.transmit(9999, 9999, { _ = "move", dir = "rotl" })
elseif keycode == keys.right then
wlan.transmit(9999, 9999, { _ = "move", dir = "rotr" })
elseif keycode == keys.pageUp then
wlan.transmit(9999, 9999, { _ = "move", dir = "up", dig = keyStates[keys.leftShift] })
elseif keycode == keys.pageDown then
wlan.transmit(9999, 9999, { _ = "move", dir = "down", dig = keyStates[keys.leftShift] })
end
end
end,
function()
while true do
local _, side, chan, rchan, data, dist = os.pullEvent("modem_message")
if chan == 9999 and rchan == chan then
if data._ == "WakeUp" then
respondedTurtles[data.from] = { true, "Hello!" }
elseif data._ == "Ack" then
respondedTurtles[data.from] = { true, "Got it" }
elseif data._ == "Error" then
respondedTurtles[data.from] = { false, data.error }
elseif data._ == "Result" then
respondedTurtles[data.from] = { true, data.out}
end
end
end
end,
function()
while true do
os.pullEvent()
term.setCursorPos(1, 1)
for id, res in pairs(respondedTurtles) do
term.write(string.format("%5d => ", id))
term.setTextColor(res[1] and colors.green or colors.red)
term.write(res[1] and "[ OK ] " or "[FAIL] ")
term.write(tostring(res[2]))
term.setTextColor(colors.white)
end
end
end
)

View File

@ -1,2 +1,60 @@
turtle.up()
turtle.down()
local wlan = peripheral.find("modem", function(addr, modem)
return modem.isWireless()
end)
if not wlan then error("no wireless interface") end
wlan.open(9999)
local ID = os.getComputerID()
wlan.transmit(9999, 9999, {
["_"] = "WakeUp",
["from"] = ID
})
parallel.waitForAll(function()
while true do
local _, side, chan, rchan, data, dist = os.pullEvent("modem_message")
if chan == 9999 and chan == rchan then
if data._ == "move" then
wlan.transmit(rchan, chan, { ["_"] = "Ack", ["from"] = ID })
if data.tgt == nil or data.tgt == ID then
local out = { false, "Invalid direction: " .. data.dir }
if data.dig then
if data.dir == "fwd" and turtle.detect() then turtle.dig()
elseif data.dir == "down" and turtle.detectDown() then turtle.digDown()
elseif data.dir == "up" and turtle.detectUp() then turtle.digUp()
end
end
if data.dir == "fwd" then out = { turtle.forward() }
elseif data.dir == "bck" then out = { turtle.back() }
elseif data.dir == "up" then out = { turtle.up() }
elseif data.dir == "down" then out = { turtle.down() }
elseif data.dir == "rotl" then out = { turtle.turnLeft() }
elseif data.dir == "rotr" then out = { turtle.turnRight() }
end
if not out[1] then
wlan.transmit(rchan, chan, {
["_"] = "Error",
["from"] = ID,
["error"] = out[2]
})
else
wlan.transmit(rchan, chan, {
["_"] = "Result",
["from"] = ID,
["out"] = out
})
end
end
elseif data._ == "shutdown" then
wlan.transmit(rchan, chan, { ["_"] = "Ack", ["from"] = ID })
os.shutdown()
elseif data._ == "reboot" then
wlan.transmit(rchan, chan, { ["_"] = "Ack", ["from"] = ID })
os.reboot()
end
end
end
end)

View File

@ -1,7 +1,7 @@
{
"ver": "0.0.1",
"files": [
{ "src": "startup.lua", "dst": "/startup.lua" },
{ "src": "main.lua", "dst": "/main.lua" }
{ "src": "/startup.lua", "dst": "/startup.lua" },
{ "src": "/main.lua", "dst": "/main.lua" }
]
}