Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- Small documentation:
- Made by: Jarle212 aka Jarle Trollebø aka aborysa
- This project is put on hold, feel free to expand.
- This api can be used to send information via bundle cable to other CCcomputers or external hardware.
- Protocol:
- 7 first bits are reserved for ID, can be usefull if someone decides to build a router
- 8 next bits are reserved as message(char value)
- last bit is confirm bit
- Exsample program for sending a file(not recomended for large files, can take several minutes):
- Sender:
- os.loadAPI("LanAPI")
- file = fs.open(file,"r")
- content = file:readAll()
- file:close()
- LanAPI.send(ip/id, content, bundled output side)
- Receiver:
- os.loadAPI("LanAPI")
- content = LanAPI.readMessage(bundled input side)
- file = fs.open(file,"w")
- file:write(content)
- file:close()
- To use this application launch the receiver first
- Peer to peer chat room: http://pastebin.com/r3PsAFjG
- Usage of common functions:
- sendChar(id, ascii character value, side)
- cInt(int, system) --returns a representation of a base 10 number in base system
- sendMessage(id/ip, string message, side) --ip/id is not important for peer to peer
- readMessage(side) --Reads the message and returns a string(will wait for whole message, should be used with parallell for performance)
- readChar(side) reads the character from a side, returns ip and character value
- --]]
- local startSignal = 1
- local endSignal = 0
- local CharVal = {"A","B","C","D","E","F"}
- function cInt(int,system)
- local newString = "";
- local number = int;
- local addNumber = 0
- while true do
- addNumber = number % system
- if addNumber > 9 then
- newString = newString .. CharVal[addNumber-9]
- else
- newString = newString .. addNumber
- end
- number = math.floor(number/system)
- if number == 0 then
- break;
- end
- end
- return newString:reverse()
- end
- function createMessage(id, char)
- local Ipadd = cInt(id,16)
- local mChar = cInt(tostring(char),16)
- if Ipadd:len() == 1 then
- Ipadd = "0" .. Ipadd
- end
- if mChar:len() == 1 then
- mChar = "0" .. mChar
- end
- local message = mChar .. Ipadd
- return message
- end
- function sendChar(id,char,side)
- mess = createMessage(id,char)
- rs.setBundledOutput(side,tonumber(mess,16)+32768)
- sleep(0.1) --Minimum sleep
- rs.setBundledOutput(side,0)
- sleep(0.1)
- end
- function readChar(side)
- local signal = 0
- while not (signal >= 32768) do
- os.pullEvent("redstone")
- signal = rs.getBundledInput(side)
- end
- signal = signal - 32768
- local mess = cInt(signal,16)
- if not(mess:len() == 4) then
- mess = "0" .. mess
- end
- local ip = mess:sub(3,4)
- local value = mess:sub(1,2)
- return ip, value
- end
- function newReadChar(side)
- local signal = 0
- while true do
- newSignal = rs.getBundledInput(side)
- os.pullEvent("redstone")
- signal = rs.getBundledInput(side)
- if not(newSignal == signal) then
- signal = newSignal
- break
- end
- end
- signal = signal - 32768
- local mess = cInt(signal,16)
- if not(mess:len() == 4) then
- mess = "0" .. mess
- end
- local ip = mess:sub(3,4)
- local value = mess:sub(1,2)
- return ip, value
- end
- function readMessage(side)
- local message = ""
- local messageStarted = false
- local messageEnded = false
- local doRead = true
- while not(messageEnded) do
- local ip, value,doRead = readChar(side,doRead)
- if tonumber(value,16) == startSignal then
- messageStarted = true
- elseif tonumber(value,16) == endSignal then
- messageEnded = true
- else
- message = message .. string.char(tonumber(value,16))
- end
- end
- return message;
- end
- function sendMessage(id,str,side)
- sendChar(id,1,side)
- for i=1, str:len() do
- sendChar(id,string.byte(str:sub(i,i)),side)
- end
- sendChar(id,0,side)
- end
Advertisement
Add Comment
Please, Sign In to add comment