LK005

Remote Switch

Jan 19th, 2021 (edited)
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.67 KB | None | 0 0
  1. local tArgs = { ... }
  2. local SIDES = {"front", "left", "back", "right", "top", "bottom"}
  3. local listenChannel = tonumber(tArgs[2])
  4. local modem
  5. local peripherals
  6. local redstoneSide = tArgs[1]
  7. local defaultState = tArgs[3]
  8.  
  9. function inputCheck()
  10.     if isSide(redstoneSide) ~= true then
  11.         print("Invalid side")
  12.         return
  13.     end
  14.  
  15.     if type(listenChannel) ~= "number" then
  16.         print("Channel must be a number")
  17.         return
  18.     end
  19.  
  20.     if defaultState == "true" then
  21.         redstone.setOutput(redstoneSide, true)
  22.     end
  23. end
  24.  
  25. function isSide(side)
  26.     for i=1,6 do
  27.         if side == SIDES[i] then
  28.             return true
  29.         end
  30.     end
  31.     return false
  32. end
  33.  
  34. function modemSetup(side)
  35.     print("Modem is setting up...")
  36.     modem = peripheral.wrap(side)
  37.     modem.open(listenChannel)  -- Open channel 3 so that we can listen on it
  38.     print("Modem is on the "..side.." side of the computer...")
  39. end
  40.  
  41. function findPeripherals()
  42.     local tempPeripherals = {}
  43.     for i=1,6 do
  44.         if peripheral.isPresent(SIDES[i]) == true then
  45.             local type = peripheral.getType(SIDES[i])
  46.             tempPeripherals[type] = {side = SIDES[i]}
  47.  
  48.             if type == "modem" then
  49.                 modemSetup(tempPeripherals.modem.side)
  50.             end
  51.         end
  52.     end
  53.     return tempPeripherals
  54. end
  55.  
  56. inputCheck()
  57.  
  58. peripherals = findPeripherals()
  59.  
  60. while true do
  61.     print("Looping, hold Ctrl+t to exit the program...")
  62.  
  63.     local event, modemSide, senderChannel, replyChannel, message, senderDistance = os.pullEvent("modem_message")
  64.  
  65.     if (message ~= "on") and (message ~= "off") then
  66.         modem.transmit(1, listenChannel, false)
  67.     else
  68.         if message == "on" then
  69.             redstone.setOutput(redstoneSide, true)
  70.         else
  71.             redstone.setOutput(redstoneSide, false)
  72.         end
  73.     end
  74. end
  75.  
  76. print("Goodbye...")
  77.  
Add Comment
Please, Sign In to add comment