ToLazyToThink

wirelessSwitch

Jul 28th, 2013
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.76 KB | None | 0 0
  1. rednet.open("right")
  2.  
  3. local args = {...}
  4.  
  5. if args[3] == nill then
  6.   print("usage: " .. shell.getRunningProgram() .. " <watcherID> <side> <defaultState> [invert]")
  7.   error()
  8. end
  9.  
  10. local wid   = tonumber(args[1])
  11. local side  = args[2]
  12. local state = args[3]
  13. local invert = args[4] == "invert"
  14.  
  15. function xor(a, b)
  16.   return not a ~= not b
  17. end
  18.  
  19. redstone.setOutput(side, xor(state == "on", invert))
  20. print("initial " .. state)
  21.  
  22. rednet.send(wid, "stateplease")
  23. while true do
  24.   local id, msg = rednet.receive()
  25.   if id == wid and msg == "on" then
  26.     redstone.setOutput(side, xor(true, invert))
  27.     print(os.clock() .. " on")
  28.   end
  29.   if id == wid and msg == "off" then
  30.     redstone.setOutput(side, xor(false, invert))
  31.     print(os.clock() .. " off")
  32.   end
  33. end
Advertisement
Add Comment
Please, Sign In to add comment