Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Craftnet API
- -- Version 1.0
- -- By JPiolho
- -- Feel free to copy and edit as long you credit the original author
- -- More Info @ http://bit.ly/zoxn6V
- _connections = {}
- MODE = {
- Clocked = 0,
- Pulsed = 1
- }
- local function Conn(id)
- return _connections[id]
- end
- local function BitsToNum(btbl)
- local num = 0
- local exp = 1
- local i,v
- for i,v in pairs(btbl) do
- if v == 1 then
- num = num + exp
- end
- exp = exp * 2
- end
- return num
- end
- function Open(datasides,clockside,mode)
- local connInfo = {
- Side = datasides,
- ClockSide = clockside,
- Clock = true,
- Mode = mode,
- Receiving = false,
- Sending = false,
- Speed = 0.1,
- Buffer = {}
- }
- if mode == nil then connInfo.Mode = MODE.Clocked end
- table.insert(_connections,connInfo);
- return #_connections
- end
- function Receive(conn)
- local info = Conn(conn)
- while true do
- local e,v = os.pullEvent()
- if e == "redstone" then
- if info.Receiving == false and redstone.getInput(info.ClockSide) == true then
- local anyTrue = false
- local i
- for i = 1, #info.Side do
- if redstone.getInput(info.Side[i]) == true then
- anyTrue = true
- end
- end
- if anyTrue then
- info.Receiving = true
- break;
- end
- end
- end
- end
- end
- function IsReceiving(conn)
- return Conn(conn).Receiving
- end
- function IsSending(conn)
- return Conn(conn).Sending
- end
- function IsBusy(conn)
- return Conn(conn).Receiving or Conn(conn).Sending
- end
- function GetSpeed(conn)
- return Conn(conn).Speed
- end
- function WaitForClock(conn)
- local info = Conn(conn)
- while true do
- local e,v = os.pullEvent()
- if e == "redstone" then
- local expectedClock
- if info.Clock == true then expectedClock = false else expectedClock = true end
- if redstone.getInput(info.ClockSide) == expectedClock then
- info.Clock = expectedClock
- break
- end
- end
- end
- end
- local function ReceiveNumber(conn,size)
- local info = Conn(conn)
- local x = 1
- local bits = {}
- while x < size do
- WaitForClock(conn)
- local i
- for i = 1, #info.Side do
- local val
- if redstone.getInput(info.Side[i]) == true then val = 1 else val = 0 end
- table.insert(bits,val)
- x = x + 1
- if x >= 8 then break end -- Dont read more than 8 bits
- end
- end
- return BitsToNum(bits)
- end
- function ReceiveWeirdo(conn,size)
- return ReceiveNumber(conn,size)
- end
- function ReceiveByte(conn)
- return ReceiveNumber(conn,8)
- end
- function ReceiveInt16(conn)
- return ReceiveNumber(conn,16)
- end
- function ReceiveInt32(conn)
- return ReceiveNumber(conn,32)
- end
- function SetSpeed(conn,speed)
- local info = Conn(conn)
- info.Speed = speed
- end
- function ReceiveString(conn)
- local size = ReceiveByte(conn)
- local txt = ""
- local i
- for i = 1,size do
- txt = txt .. string.char(ReceiveByte(conn))
- end
- return txt
- end
- function BeginPacket(conn)
- local info = Conn(conn)
- if info.Receiving then
- return
- end
- local i
- for i = 1, #info.Side do
- redstone.setOutput(info.Side[i],true)
- end
- redstone.setOutput(info.ClockSide,true)
- info.Clock = true
- info.Sending = true
- sleep(info.Speed)
- end
- local function SendNumber(conn,byte,size)
- local info = Conn(conn)
- if info.Receiving == true then
- return
- end
- local bits = bit.tobits(byte)
- if #bits < size then
- local x
- for x = #bits+1,size do
- table.insert(bits,0)
- end
- end
- local k = 1
- while k < #bits do
- local i
- for i = 1, #info.Side do
- if k > size then
- redstone.setOutput(info.Side[i],false)
- end
- local val = bits[k]
- if val == 1 then
- redstone.setOutput(info.Side[i],true)
- else
- redstone.setOutput(info.Side[i],false)
- end
- k = k + 1
- end
- if info.Clock == true then
- info.Clock = false
- redstone.setOutput(info.ClockSide,false)
- else
- info.Clock = true
- redstone.setOutput(info.ClockSide,true)
- end
- sleep(info.Speed)
- end
- end
- function SendByte(conn,byte)
- SendNumber(conn,byte,8)
- end
- function SendInt16(conn,int16)
- SendNumber(conn,int16,16)
- end
- function SendInt32(conn,int32)
- SendNumber(conn,int32,32)
- end
- function SendWeirdo(conn,weirdo,size)
- SendNumber(conn,weirdo,size)
- end
- function SendString(conn,text)
- local info = Conn(conn)
- if info.Receiving == true then
- return
- end
- SendByte(conn,string.len(text))
- local i
- for i = 1, string.len(text) do
- SendByte(conn,string.byte(text,i))
- end
- end
- function EndPacket(conn,sleep)
- local info = Conn(conn)
- info.Sending = false
- info.Receiving = false
- local i
- for i = 1, #info.Side do
- redstone.setOutput(info.Side[i],false)
- end
- redstone.setOutput(info.ClockSide,false)
- if sleep ~= nil and sleep == true then
- sleep(info.Speed)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment