This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
gamax92's profile picture

Bundled Cable Networking

Started by gamax92, 18 March 2013 - 02:29 PM
gamax92 #1
Posted 18 March 2013 - 03:29 PM
Just the basics of networking. Use for whatever reason.
Perhaps you wanted to use wireless redstone modules to get transdimensional communication.
Perhaps you like being slow and perfer 130 bits per second instead of instanious wireless modems.

Works like the rednet api.
open(side) open side
close(side) close last opened side.
send(msg) send a message
receive() receive a message, returns senderID, message.

For transdimensional communication, hook up the black wire as the inverse of what you want.
I.E on the sender side, put a wireless reciever. On the receiver side, put a wireless transmitter.

pastebin get yFhpEECP rbcnet

local sSide

local toBits = function(num)
	local t = {}
	while num > 0 do
		rest = math.fmod(num, 2)
		t[#t + 1] = rest
		num = (num - rest) / 2
	end
	return tabrev(t)
end

tabrev = function(tInput)
	local iSize = #tInput
	local tOutput = {}
	for k,v in ipairs(tInput) do
		tOutput[iSize - k + 1] = v
    end
    return tOutput
end

local string_lpad = function(str, len, char)
    if char == nil then char = ' ' end
    return str .. string.rep(char, len - #str)
end

local string_rpad = function(str, len, char)
    if char == nil then char = ' ' end
    return string.rep(char, len - #str) .. str
end

local function sendbits(sBits, bSync)
    redstone.setBundledOutput(sSide,tonumber(string_rpad(sBits .. tostring(bSync) .. "1",16,"0"),2))
    while true do
        bundled = toFBits(redstone.getBundledInput(sSide),16)
        if string.sub(bundled,1,1) == tostring(bSync) then break end
        sleep(0.05)
    end
end

local function recvbits(size,bSync)
    while true do
        bundled = toFBits(redstone.getBundledInput(sSide),16)
        if string.sub(bundled,16,16) == "0" then return "" end
        if string.sub(bundled,15,15) ~= tostring(bSync) then break end
        sleep(0.05)
    end
    redstone.setBundledOutput(sSide,tonumber(string_lpad(tostring(1 - bSync),16,"0"),2))
    return string.sub(bundled,14 - size + 1,14)
end

function toFBits(num,len)
    local num2tab = toBits(num)
    local tab2str = table.concat(num2tab)
    local str2fmt = string_rpad(tab2str,len,"0")
	return str2fmt
end

function open(side)
	sSide = side
end

function close(side)
	sSide = nil
end

function receive()
	if sSide == nil then
		error("No open sides")
	end
    while redstone.getBundledInput(sSide) == 0 do sleep(0.05) end
    redstone.setBundledOutput(sSide,0)
	local iID = recvbits(8,0)
	iID = iID .. recvbits(8,1)
    iID = tonumber(iID,2)
	local sData = ""
    local bSync = 0
    while true do
		bundled = toFBits(redstone.getBundledInput(sSide),16)
		if string.sub(bundled,16,16) == "0" then break end
		sData = sData .. recvbits(13,bSync)
        bSync = 1 - bSync
    end
	sData = string.sub(sData,1,math.floor(string.len(sData) / 8) * 8)
    local sMsg = ""
    local ENDMrk = 0
	for i = 1, string.len(sData), 8 do
        sMsg = sMsg .. string.char(tonumber(string.sub(sData,i,i+7),2))
		if tonumber(string.sub(sData,i,i+7),2) == 255 then ENDMrk = ((i - 1) / 8) end
    end
	sMsg = string.sub(sMsg,1,ENDMrk)
    redstone.setBundledOutput(sSide,0)
    return iID, sMsg
end

function send(message)
	if sSide == nil then
		error("No open sides")
	end
    redstone.setBundledOutput(sSide,0)
	local sID = toFBits(os.computerID(),16)
	sendbits(string.sub(sID,1,8),1)
	sendbits(string.sub(sID,9,16),0)
    local sBits = ""
    for i = 1, string.len(message) do
        sBits = sBits .. toFBits(string.byte(string.sub(message,i,i)),8)
    end
	sBits = sBits .. "11111111"
    local bSync = 1
    for i = 1, string.len(sBits), 13 do
        sendbits(string_lpad(string.sub(sBits,i , i + 12),13,"0"),bSync)
        bSync = 1 - bSync
    end
    redstone.setBundledOutput(sSide,0)
end
Kingdaro #2
Posted 18 March 2013 - 03:35 PM
Why the duplicate topic?
1lann #3
Posted 18 March 2013 - 05:36 PM
Why the duplicate topic?
One is for redstone, this one is for bundled cables