forked from hkc/cc-stuff
1
0
Fork 0
cc-stuff/turtos/ctrl.lua

67 lines
2.2 KiB
Lua
Raw Normal View History

2024-01-12 21:15:52 +03:00
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
)