[left]Wireless Redstone (Requires WR API)[/left]
Spoiler
IntroductionSo, you're probably wondering, what does this do? Well, the title is pretty much a dead giveaway. Input redstone into one computer, it comes out of another computer. This program utilizes rednet so it is limited by rednet's wifi range. Before you use this program, you MUST have a rednet modem on your computer.
How To Use It
Get two computers with rednet modems on them and run the program on both, the program will automatically open the modem, so no need to do it yourself. On one computer, choose the 'Sender' option, and on the other, choose the 'Receiver' option. When it asks for the frequency, make sure you enter the same on both the sender and the receiver (note: the frequency can be absolutely anything, use any symbol, letter, or number.) . Now input a redstone signal into the sender, place a redstone wire on the same side at the receiver and watch as the receiver lights up the wire. (note: the receiver must be setup before inputting, or else it will not work.)
WARNING: Using multiple senders on one frequency can cause mix-ups at the receiver.
The Code
Spoiler
-- Wireless Redstone by Islanderon
-- Version 1.0.2
-- WARNING: Requires the WR api to be named 'wr'.
local function openRednet()
local listOfSides = rs.getSides()
local listofPossibles = {}
local counter1 = 0
while true do
counter1 = counter1 +1
if peripheral.isPresent(tostring(listOfSides[counter1])) and peripheral.getType(listOfSides[counter1]) == "modem" then
table.insert(listofPossibles,tostring(listOfSides[counter1]))
end
if counter1 == 6 and table.maxn(listofPossibles) == 0 then
print("no wifi present")
return nil
end
if counter1 == 6 and table.maxn(listofPossibles) ~= 0 then
rednet.open(listofPossibles[1])
return listofPossibles[1]
end
end
end
function fromboolean(input)
if input == true then
return "true"
else
return "false"
end
end
function toboolean(input)
if input == "true" then
return true
else
return false
end
end
function find(str, match, startIndex) --Finds @match in @str optionally after @startIndex
if not match then return nil end
str = tostring(str)
local _ = startIndex or 1
local _s = nil
local _e = nil
local _len = match:len()
while true do
local _t = str:sub( _ , _len + _ - 1)
if _t == match then
_s = _
_e = _ + _len - 1
break
end
_ = _ + 1
if _ > str:len() then break end
end
if _s == nil then return nil else return _s, _e end
end
function seperate(str, divider) -- Seperate function by tommas
if not divider then return nil end
str = tostring(str)
local start = {}
local endS = {}
local n=1
repeat
if n==1 then
start[n], endS[n] = find(str, divider)
else
start[n], endS[n] = find(str, divider, endS[n-1]+1)
end
n=n+1
until start[n-1]==nil
local subs = {}
for n=1, #start+1 do
if n==1 then
subs[n] = str:sub(1, start[n]-1)
elseif n==#start+1 then
subs[n] = str:sub(endS[n-1]+1)
else
subs[n] = str:sub(endS[n-1]+1, start[n]-1)
end
end
return subs
end
openRednet()
valid = 0
function runprogram()
term.clear()
term.setCursorPos(1,1)
if valid == 1 then
print "Invalid Choice"
end
print "Pick One:"
print "[1] Sender"
print "[2] Receiver"
print ""
event, choice = os.pullEvent()
if event == "key" and choice == 2 then -- Sending
write "Frequency: "
freq = io.read()
term.clear()
term.setCursorPos(1,1)
print ("Now sending redstone input to frequency "..freq..", hold CTRL-T to end.")
while true do
os.pullEvent("redstone")
states = {redstone.getInput("left"), redstone.getInput("right"), redstone.getInput("back"), redstone.getInput("front"), redstone.getInput("top"), redstone.getInput("bottom")}
wr.sendState(freq, "left", states[1])
wr.sendState(freq, "right", states[2])
wr.sendState(freq, "back", states[3])
wr.sendState(freq, "front", states[4])
wr.sendState(freq, "top", states[5])
wr.sendState(freq, "bottom", states[6])
print ("SENT: Redstone change")
end
elseif event == "key" and choice == 3 then -- Receiving
write "Frequency: "
freq = io.read()
term.clear()
term.setCursorPos(1,1)
print ("Now receiving data on frequency "..freq..", hold CTRL-T to end.")
while true do
rec = wr.recState(freq, true)
rec[2] = fromboolean(rec[2])
print (rec[1].." : "..rec[2])
end
else
valid = 1
runprogram()
end
end
runprogram()
Changelog
v1.0.0
- Initial release
v1.0.1
- Added support for top and bottom redstone signals
v1.0.2
- Now requires the WR API
Planned Features
-
- Support for multiple senders on one frequency
- Support for redpower bundled cables
- Your idea?
WR (Wireless Redstone) API
[left]
Spoiler
[/left][left]Functions[/left]
sendState(frequency, side, state): Sends a redstone state (i.e. true or false) to the specified frequency.
- frequency(string): The frequency of which to send to.
- side(string): The side (i.e. left, right, top, bottom) that is to be changed.
- state(string): The state(i. e. true or false) of which to change the side to.
recState(frequency, set, timeout): Receives the first state detected on the specified frequency.
- frequency(string): The frequency of which to listen on.
- set(boolean): if set to true, the received redstone state will be outputted automatically.
- timeout(string)(optional): The amount of time in seconds before the listener stops, if no timeout is specified, it will listen until a state is received.
- Return Values: This returns a table with the side(string) and state(now converted to a boolean).
The Code
Spoiler
-- WR API by Islanderon
-- Version 1.1
local function openRednet()
local listOfSides = rs.getSides()
local listofPossibles = {}
local counter1 = 0
while true do
counter1 = counter1 +1
if peripheral.isPresent(tostring(listOfSides[counter1])) and peripheral.getType(listOfSides[counter1]) == "modem" then
table.insert(listofPossibles,tostring(listOfSides[counter1]))
end
if counter1 == 6 and table.maxn(listofPossibles) == 0 then
print("no wifi present")
return nil
end
if counter1 == 6 and table.maxn(listofPossibles) ~= 0 then
rednet.open(listofPossibles[1])
return listofPossibles[1]
end
end
end
local function toboolean(input)
if input == "true" then
return true
else
return false
end
end
local function fromboolean(input)
if input == true then
return "true"
else
return "false"
end
end
local function find(str, match, startIndex) --Find function by tommas
if not match then return nil end
str = tostring(str)
local _ = startIndex or 1
local _s = nil
local _e = nil
local _len = match:len()
while true do
local _t = str:sub( _ , _len + _ - 1)
if _t == match then
_s = _
_e = _ + _len - 1
break
end
_ = _ + 1
if _ > str:len() then break end
end
if _s == nil then return nil else return _s, _e end
end
local function seperate(str, divider) -- Seperate function by tommas
if not divider then return nil end
str = tostring(str)
local start = {}
local endS = {}
local n=1
repeat
if n==1 then
start[n], endS[n] = find(str, divider)
else
start[n], endS[n] = find(str, divider, endS[n-1]+1)
end
n=n+1
until start[n-1]==nil
local subs = {}
for n=1, #start+1 do
if n==1 then
subs[n] = str:sub(1, start[n]-1)
elseif n==#start+1 then
subs[n] = str:sub(endS[n-1]+1)
else
subs[n] = str:sub(endS[n-1]+1, start[n]-1)
end
end
return subs
end
function sendState(freq, side, state)
openRednet()
state = fromboolean(state)
rednet.broadcast("RSW : "..freq.." : "..side.." : "..state)
end
function recState(freq, set, timeout)
openRednet()
if timeout == nil then
id, msg = rednet.receive()
else
id, msg = rednet.receive(tonumber(timeout))
end
parseddata = seperate(msg, " : ")
if parseddata[1] == "RSW" and parseddata[2] == freq then
finald = { }
finald[1] = parseddata[3]
finald[2] = toboolean(parseddata[4])
if set == true then
redstone.setOutput(finald[1], finald[2])
return finald
else
return finald
end
end
end
Changelog
v1.0
- Intial release.
v1.1
- 'set' arg added to recState
- Running any of the functions will automatically open any rednet modems on your computer.