From 117b22041d954f6ce422b4d54e6b1d72e5458657 Mon Sep 17 00:00:00 2001 From: manuel Date: Sat, 7 Jan 2023 21:52:50 +0000 Subject: [PATCH] Upload files to 'controller' --- controller/Controller2.lua | 150 +++++++++++++++++++++++++++++++++++++ 1 file changed, 150 insertions(+) create mode 100644 controller/Controller2.lua diff --git a/controller/Controller2.lua b/controller/Controller2.lua new file mode 100644 index 0000000..518df22 --- /dev/null +++ b/controller/Controller2.lua @@ -0,0 +1,150 @@ +Controller = {} + +require("Monitor") + + + +function Controller:new(t) + t = t or {} + setmetatable(t, self) + self.__index = self + return t +end + +function Controller:init() + self.monitor = Monitor:new() + self.monitor:init() + self.monitor:setStatus(1, "not started") + self.monitor:setButton("start") + self.redstone = {} + self.turtleStatus = "unknown" + +end + +function Controller:sendMessage(id, message, protocol) + rednet.send(id, message, protocol) + local id2, message2 = rednet.receive("received", 1) + return message == message2 +end + +function Controller:doAction(id, message, protocol) + self:sendMessage(id, message, protocol) + local id2, message2 + repeat + id2, message2 = rednet.receive() + until id2 == id + return message2 == "confirm", message2 +end + +function Controller:setSlaveID(id) + self.slaveID = id +end + +function Controller:initSlave() + if self:sendMessage(self.slaveID, "init", "command") then + local id, message, protocol = rednet.receive(nil, 60) + if (protocol == "confirm") then + return true + end + return false + end +end + +function Controller:updateTreeStatus() + if self:sendMessage(self.slaveID, "checkTree", "command") then + local id, message, protocol = rednet.receive(nil, 60) + if (protocol == "confirm") then + self.treeStatus = message + return message + end + end +end + +function Controller:waitForRedstone(direction) + self.redstone[direction] = false + while(redstone.getInput(direction) == true) do + sleep(0.05) + end + + while(redstone.getInput(direction) == false) do + sleep(0.05) + end + self.redstone[direction] = true + return true +end + +function Controller:autoWood() + self.stop = false + while(self.stop == false) do + self:updateFuelLevel() + self:updateDisplay(4) + local update = self:updateTreeStatus() + if(update == nil) then + self.turtleStatus = "error" + return false + elseif(update == "sapling") then + self.turtleStatus = "waiting" + self:updateFuelLevel() + self:updateDisplay(4) + local t = os.clock() + self:doAction(self.slaveID, "sort") + sleep(60) + elseif(update == "tree")then + self.turtleStatus = "cutting tree" + self:updateFuelLevel() + self:updateDisplay(4) + self:doAction(self.slaveID, "cutTree") + self.turtleStatus = "planting tree" + self:updateFuelLevel + self:updateDisplay(4) + self:doAction(self.slaveID, "plantTree") + end + end +end + +function Controller:updateFuelLevel() + self.turtleStatus = "checking tree" + local s, fuelLevel = self:doAction("getFuelLevel") + if(s == true) then + self.turtleFuelLevel = fuelLevel + end + return s +end + +function Controller:updateDisplay(line) + if(self.monitor.sb.lines[line] ~= self.turtleStatus) then + self.monitor:setStatus(line, self.turtleStatus) + end + self:displayFuelLevel(line + 1) +end + +function Controller:displayFuelLevel(line) + if(self.monitor.sb.lines[line] ~= self.turtleFuelLevel) then + self.monitor:setStatus(line, self.turtleFuelLevel) + end +end + +function Main() + local ct = Controller:new() + ct:init() + ct:setSlaveID(10) + + ct:waitForRedstone("top") + + ct.monitor:setStatus(1, "Turtle:") + ct.monitor:setStatus(2, "Id: 10") + ct.monitor:setStatus(3, "not connected") + ct.monitor:setButton("init turtle") + + ct:waitForRedstone("top") + + ct:initSlave() + ct.monitor:setStatus(3, "connected") + ct.monitor:setButton("start turtle") + + ct:waitForRedstone("top") + + ct:autoWood() + + +end \ No newline at end of file