Upload files to 'controller'
This commit is contained in:
parent
80f6d8a96b
commit
117b22041d
150
controller/Controller2.lua
Normal file
150
controller/Controller2.lua
Normal file
@ -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
|
Loading…
x
Reference in New Issue
Block a user