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

[1.3+]Islanderon's Programs - Wireless Redstone!

Started by islanderon01, 29 March 2012 - 08:14 PM
islanderon01 #1
Posted 29 March 2012 - 10:14 PM
Islanderon's Programs






[left]Wireless Redstone (Requires WR API)[/left]

SpoilerIntroduction

So, 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 top and bottom redstone signals ~ Added in v1.0.1
- 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.
Cloudy #2
Posted 30 March 2012 - 01:00 AM
Neat idea. Do you plan on adding support for redpower cables?
FuzzyPurp #3
Posted 30 March 2012 - 01:07 AM
Nice idea, totally just removed wireless redstone mod, and as Cloudy said, add support for Bundles, i'm too lazy to update your code for my own personal use, when i know eventually you will do it. :o/>/>
Espen #4
Posted 30 March 2012 - 10:34 AM
The only downside I can see in comparison with the Wireless Redstone Mod is that the top is blocked off from outputting redstone signals, because of the modem.
But that's only 1 out of 6 sides and in probably 90% of applications you don't need output there or you can work around it.
Compared to the fact that this saves you a whole mod AND that with CC 1.3.2 the LuaJ Thread Problem has been solved, I personally find this very useful.
So thx islanderon01, big kudos to you for coming up with this nice idea. :o/>/>
Mads #5
Posted 30 March 2012 - 11:55 AM
Neat idea. Do you plan on adding support for redpower cables?
It's "Wireless Redstone"
FuzzyPurp #6
Posted 30 March 2012 - 12:02 PM
Neat idea. Do you plan on adding support for redpower cables?
It's "Wireless Redstone"

Hehe - but imagine wireless bundle cables!!
islanderon01 #7
Posted 30 March 2012 - 08:13 PM
Neat idea. Do you plan on adding support for redpower cables?

Maybe, I've never really used the redpower mod because I'm not a genius with redstone :o/>/> , but I'll see if I can do it.

The only downside I can see in comparison with the Wireless Redstone Mod is that the top is blocked off from outputting redstone signals, because of the modem.
But that's only 1 out of 6 sides and in probably 90% of applications you don't need output there or you can work around it.
Compared to the fact that this saves you a whole mod AND that with CC 1.3.2 the LuaJ Thread Problem has been solved, I personally find this very useful.
So thx islanderon01, big kudos to you for coming up with this nice idea. :o/>/>

The modem can be on any side, it auto detects which side it is on then opens it. Btw, my real username is islanderon, it was taken somehow when I registered so I just added on '01'.
islanderon01 #8
Posted 02 April 2012 - 12:43 AM
ATM I am working on adding bundled cable support and I'm working on a Wireless Redstone API.
Matchlighter #9
Posted 02 April 2012 - 12:44 AM
- Support for multiple senders on one frequency

That's actually quite simple. Just have a number (or a table of frequencies/sided, depending on how your code works) and every time an 'on' signal comes in, add to it. When an 'off' signal comes, substract from it. You could actually do some neat logic like this. AND, the number has to be 2. OR, the number can be 1 or 2. XOR, …you get my point.
islanderon01 #10
Posted 02 April 2012 - 12:47 AM
- Support for multiple senders on one frequency

That's actually quite simple. Just have a number (or a table of frequencies/sided, depending on how your code works) and every time an 'on' signal comes in, add to it. When an 'off' signal comes, substract from it. You could actually do some neat logic like this. AND, the number has to be 2. OR, the number can be 1 or 2. XOR, …you get my point.

This would work if my system didn't work as it does, when the sender gets a redstone change, it sends the states of all the sides (i.e. true or false). I tried using your method earlier, it caused the receiver to go nuts.
Matchlighter #11
Posted 02 April 2012 - 12:51 AM
- Support for multiple senders on one frequency

That's actually quite simple. Just have a number (or a table of frequencies/sided, depending on how your code works) and every time an 'on' signal comes in, add to it. When an 'off' signal comes, substract from it. You could actually do some neat logic like this. AND, the number has to be 2. OR, the number can be 1 or 2. XOR, …you get my point.

This would work if my system didn't work as it does, when the sender gets a redstone change, it sends the states of all the sides (i.e. true or false). I tried using your method earlier, it caused the receiver to go nuts.

Ahh. I see. But it would be kinda neat to see it work like that with the logic.
islanderon01 #12
Posted 11 June 2013 - 03:07 PM
Wow, I almost completly forgot about this with all the extra work in school. But now I have a whole empty summer with nothing to do, so I'll see if I can make some improvements to the code. Is it just me or has the fad of bundle cables died off? If it has then I won't bother trying to impliment bundle cables into the code.
Trainguyrom #13
Posted 13 June 2013 - 03:31 PM
Darn. I was going to make my own wireless redstone using CC. Oh well, at least I have some ideas to allow multiple senders, and I can use my own style.
encrypted #14
Posted 12 July 2013 - 10:19 AM
can't seem to get this to work. when i run the program i get "wireless:122: attemp to index ? (a nil value)"