Guest User

calicommAPI

a guest
May 19th, 2013
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.15 KB | None | 0 0
  1. ------------------------------------------------------------------
  2. ------------------------------------------------------------------
  3.  
  4. local codename = 'calicommAPI'
  5. local codeversion = '0.1.0.1'
  6. local whoami = tonumber(os.getComputerID ())
  7. print (">>>"..codename.." Version "..codeversion.."@ID:"..whoami)
  8.  
  9. local hardwaresides = redstone.getSides ()
  10. local verbose = false
  11. modemtable = {}
  12.  
  13. ------------------------------------------------------------------
  14.  
  15. function enableDebug (debugswitch)
  16.     if (type(debugswitch) == 'boolean') then
  17.         verbose = debugswitch
  18.     end
  19. end
  20.  
  21. ------------------------------------------------------------------
  22.  
  23. function initModems ()
  24.     if (verbose) then print ("*calicommAPI.initModems: called...") end
  25.     for i=1,#hardwaresides do
  26.         local modemside = hardwaresides[i]
  27.         local device = peripheral.getType (modemside)
  28.        
  29.         if (device == 'modem') then
  30.             local modemhandle = peripheral.wrap (modemside)
  31.             local iswlanmodem = modemhandle.isWireless ()
  32.        
  33.             if (iswlanmodem) then
  34.                 if (verbose) then print ("*calicommAPI.initModems: found WIRELESS modem @"..modemside) end
  35.             else
  36.                 if (verbose) then print ("*calicommAPI.initModems: found WIRED modem @"..modemside) end
  37.             end
  38.                        
  39.             table.insert (modemtable, {modemhandle, modemside, iswlanmodem})
  40.         end
  41.     end
  42.    
  43.     if (verbose) then print ("*calicommAPI.initModems: found "..#modemtable.." modems") end
  44.     return #modemtable
  45. end
  46.  
  47. ------------------------------------------------------------------
  48.  
  49. function openModemChannel (modemhandle, modemchannel)
  50.     if (verbose) then print ("*calicommAPI.openModemChannel: trying to open channel: "..modemchannel) end
  51.     if ((modemchannel < 1) or (modemchannel >= 65535)) then
  52.         if (verbose) then print ("*calicommAPI.openModemChannel: invalid channel!") end
  53.         return false
  54.     else
  55.         if (modemhandle.isOpen (modemchannel)) then
  56.             if (verbose) then print ("*calicommAPI.openModemChannel: channel already open!") end
  57.             return false
  58.         end
  59.         modemhandle.open (modemchannel)
  60.         if (modemhandle.isOpen (modemchannel)) then
  61.             if (verbose) then print ("*calicommAPI.openModemChannel: opening channel: success!") end
  62.             return true
  63.         else
  64.             if (verbose) then print ("*calicommAPI.openModemChannel: opening channel: failure!") end
  65.             return false
  66.         end    
  67.     end
  68. end
  69.  
  70. ------------------------------------------------------------------
  71.  
  72. function closeModemChannel (modemhandle, modemchannel)
  73.     if (verbose) then print ("*calicommAPI.closeModemChannel: trying to close channel: "..modemchannel) end
  74.     if ((modemchannel < 1) or (modemchannel >= 65535)) then
  75.         if (verbose) then print ("*calicommAPI.closeModemChannel: invalid channel!") end
  76.         return false
  77.     else
  78.         if (not modemhandle.isOpen (modemchannel)) then
  79.             if (verbose) then print ("*calicommAPI.closeModemChannel: channel not open!") end
  80.             return false
  81.         end
  82.         modemhandle.close (modemchannel)
  83.         if (modemhandle.isOpen (modemchannel)) then
  84.             if (verbose) then print ("*calicommAPI.closeModemChannel: closing channel: failure!") end
  85.             return false
  86.         else
  87.             if (verbose) then print ("*calicommAPI.closeModemChannel: closing channel: success!") end
  88.             return true
  89.         end    
  90.     end
  91. end
Advertisement
Add Comment
Please, Sign In to add comment