Advertisement
Guest User

wireless.lua

a guest
Apr 4th, 2020
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.18 KB | None | 0 0
  1. -- Must have already opened a valid rednet
  2. -- connection BEFORE loading this API
  3. local config = {
  4.     protocol = "wireless",
  5.     host = "accessPoint",
  6.     timout = 30    
  7. }
  8. local id = rednet.lookup(config.protocol, config.hostName)
  9.  
  10. function send(protocol, host, msg)
  11.     local packet = {
  12.         type = "HOSTED",
  13.         host = host,
  14.         payload = msg
  15.     }
  16.     rednet.send(id, packet, protocol)
  17. end
  18.  
  19. function receive(protocol, timeout)
  20.     if timeout == nil then timeout = config.timeout end    
  21.     local senderId, packet, prot = rednet.receive(protocol, timout)
  22.     if (senderId ~= id) or
  23.        (packet == nil) or
  24.        (prot ~= protocol) then
  25.         return nil
  26.     else
  27.         return packet.senderId,
  28.                packet.payload,
  29.                prot
  30.     end        
  31. end
  32.  
  33.  
  34. function reply(senderId, msg, protocol)
  35.     local packet = {
  36.         type = "WITH_ID",
  37.         senderId = senderId,
  38.         payload = msg
  39.     }
  40.     rednet.send(id, packet, protocol)    
  41. end
  42.  
  43. function broadcast(msg, protocol)
  44.     local packet = {
  45.         type = "BROADCAST",
  46.         msg = msg,
  47.         protocol = protocol
  48.     }
  49.     rednet.send(id, packet, protocol)
  50. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement