From 1ea1bb7c9ea5a3452ac00ce61ea60719bcac6d4d Mon Sep 17 00:00:00 2001 From: hkc Date: Fri, 12 Jan 2024 21:15:52 +0300 Subject: [PATCH] RC turtle when --- turtos/ctrl.lua | 66 ++++++++++++++++++++++++++++++++++++++++++++++ turtos/main.lua | 62 +++++++++++++++++++++++++++++++++++++++++-- turtos/update.json | 4 +-- 3 files changed, 128 insertions(+), 4 deletions(-) create mode 100644 turtos/ctrl.lua diff --git a/turtos/ctrl.lua b/turtos/ctrl.lua new file mode 100644 index 0000000..1ca65e2 --- /dev/null +++ b/turtos/ctrl.lua @@ -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 +) diff --git a/turtos/main.lua b/turtos/main.lua index 8ea1b07..a92cd05 100644 --- a/turtos/main.lua +++ b/turtos/main.lua @@ -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) diff --git a/turtos/update.json b/turtos/update.json index cab6a97..4e8cde5 100644 --- a/turtos/update.json +++ b/turtos/update.json @@ -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" } ] }