wbennet997

send/recv

Jul 27th, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.24 KB | None | 0 0
  1. -- Wireless Redstone by Islanderon
  2. -- Version 1.0.2
  3. -- WARNING: Requires the WR api to be named 'wr'.
  4. local function openRednet()
  5. local listOfSides = rs.getSides()
  6. local listofPossibles = {}
  7. local counter1 = 0
  8. while true do
  9.   counter1 = counter1 +1
  10.  
  11.   if peripheral.isPresent(tostring(listOfSides[counter1])) and peripheral.getType(listOfSides[counter1]) == "modem" then
  12.    table.insert(listofPossibles,tostring(listOfSides[counter1]))
  13.   end
  14.  
  15.   if counter1 == 6 and table.maxn(listofPossibles) == 0 then
  16.    print("no wifi present")
  17.    return nil
  18.   end
  19.  
  20.   if counter1 == 6 and table.maxn(listofPossibles) ~= 0 then
  21.    rednet.open(listofPossibles[1])
  22.    return listofPossibles[1]
  23.   end
  24. end
  25. end
  26. function fromboolean(input)
  27. if input == true then
  28. return "true"
  29. else
  30. return "false"
  31. end
  32. end
  33. function toboolean(input)
  34. if input == "true" then
  35. return true
  36. else
  37. return false
  38. end
  39. end
  40. function find(str, match, startIndex)  --Finds @match in @str optionally after @startIndex
  41. if not match then return nil end
  42. str = tostring(str)
  43. local _ = startIndex or 1
  44. local _s = nil
  45. local _e = nil
  46. local _len = match:len()
  47. while true do
  48. local _t = str:sub( _ , _len + _ - 1)
  49. if _t == match then
  50. _s = _
  51. _e = _ + _len - 1
  52. break
  53. end
  54. _ = _ + 1
  55. if _ > str:len() then break end
  56. end
  57. if _s == nil then return nil else return _s, _e end
  58. end
  59. function seperate(str, divider) -- Seperate function by tommas
  60. if not divider then return nil end
  61. str = tostring(str)
  62. local start = {}
  63. local endS = {}
  64. local n=1
  65. repeat
  66. if n==1 then
  67. start[n], endS[n] = find(str, divider)
  68. else
  69. start[n], endS[n] = find(str, divider, endS[n-1]+1)
  70.                                 end
  71. n=n+1
  72. until start[n-1]==nil
  73. local subs = {}
  74. for n=1, #start+1 do
  75. if n==1 then
  76. subs[n] = str:sub(1, start[n]-1)
  77. elseif n==#start+1 then
  78. subs[n] = str:sub(endS[n-1]+1)
  79. else
  80. subs[n] = str:sub(endS[n-1]+1, start[n]-1)
  81.                                 end
  82. end
  83. return subs
  84. end
  85. openRednet()
  86. valid = 0
  87. function runprogram()
  88. term.clear()
  89. term.setCursorPos(1,1)
  90. if valid == 1 then
  91. print "Invalid Choice"
  92. end
  93. print "Pick One:"
  94. print "[1] Sender"
  95. print "[2] Receiver"
  96. print ""
  97. event, choice = os.pullEvent()
  98. if event == "key" and choice == 2 then -- Sending
  99. write "Frequency: "
  100. freq = io.read()
  101. term.clear()
  102. term.setCursorPos(1,1)
  103. print ("Now sending redstone input to frequency "..freq..", hold CTRL-T to end.")
  104. while true do
  105.   os.pullEvent("redstone")
  106.   states = {redstone.getInput("left"), redstone.getInput("right"), redstone.getInput("back"), redstone.getInput("front"), redstone.getInput("top"), redstone.getInput("bottom")}
  107.   wr.sendState(freq, "left", states[1])
  108.   wr.sendState(freq, "right", states[2])
  109.   wr.sendState(freq, "back", states[3])
  110.   wr.sendState(freq, "front", states[4])
  111.   wr.sendState(freq, "top", states[5])
  112.   wr.sendState(freq, "bottom", states[6])
  113.    print ("SENT: Redstone change")
  114. end
  115. elseif event == "key" and choice == 3 then -- Receiving
  116. write "Frequency: "
  117. freq = io.read()
  118. term.clear()
  119. term.setCursorPos(1,1)
  120. print ("Now receiving data on frequency "..freq..", hold CTRL-T to end.")
  121. while true do
  122.   rec = wr.recState(freq, true)
  123.   rec[2] = fromboolean(rec[2])
  124.   print (rec[1].." : "..rec[2])
  125. end
  126. else
  127. valid = 1
  128. runprogram()
  129. end
  130. end
  131. runprogram()
Add Comment
Please, Sign In to add comment