diff --git a/Controller/wheatController.lua b/Controller/wheatController.lua index 7856e41..724e45b 100644 --- a/Controller/wheatController.lua +++ b/Controller/wheatController.lua @@ -1,3 +1,13 @@ +wheatController = {} + +function wheatController:new(t) + t = t or {} + setmetatable(t, self) + self.__index = self + return t +end + + function toMatrix(str) local matrix = {} assert(type(str) == "string") @@ -10,10 +20,27 @@ function toMatrix(str) str = str:sub(2, len - 1) len = len - 2 - - if(str:sub(1,1) == "{") then - + if ob = 1 then + return toArray(str) + else + local level = 0 + local lastC = 0 + for i = 1, len do + local c = str:sub(i,i) + if c == "{" then + level = level + 1 + elseif c == "}" then + level = level - 1 + elseif c == "," then + if level == 0 then + matrix[#matrix+1] = toMatrix(str:sub(lastC + 1, i - 1)) + lastC = i + end + end + end + return matrix end + end function toArray(str) @@ -30,4 +57,23 @@ function toArray(str) arr[#arr+1] = str end return arr +end + +function wheatController:sendMessage(id, message, protocol) + rednet.send(id, message, protocol) + local id2, message2 = rednet.receive("received", 1) + return message == message2 +end + +function wheatController:doAction(id, message, protocol) + self:sendMessage(id, message, protocol) + local id2, message2, protocol2 + repeat + id2, message2, protocol2 = rednet.receive() + until id2 == id + return protocol2 == "confirm", message2 +end + +function wheatController:setSlaveID(id) + self.slaveID = id end \ No newline at end of file