SHARE
TWEET

ComputerCraft Tutorial: h2peripheral_API 0_2

hevohevo Jan 29th, 2014 173 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -- ###############################
  2. -- h2peripheral API
  3. -- version 0.2b
  4. -- http://hevohevo.hatenablog.com/
  5. -- Write in your program.
  6. --    os.loadAPI("h2peripheral")
  7.  
  8. -- os.loadAPI("rom/apis/peripheral")
  9.  
  10. if peripheral and os then
  11.   local types = {                   -- White-list
  12.     drive="Disk Drive",
  13.     monitor="Monitor and  Advanced Monitor",
  14.     advanced_monitor="Advanced Monitor",
  15.     modem="Wired Modem and Wireless Modem",
  16.     wireless_modem="Wireless Modem",
  17.     workbench="Crafty Turtle's Workbench",
  18.     printer="Printer",
  19.     command="Command Block"
  20.   }
  21.  
  22.   function getAllTypeNames()
  23.     return types
  24.   end
  25.   function isAdvancedMonitor(side)
  26.     return peripheral.getType(side)=="monitor" and peripheral.call(side, "isColor")
  27.   end
  28.   function isWirelessModem(side)
  29.     return peripheral.getType(side)=="modem" and peripheral.call(side, "isWireless")
  30.   end
  31.   function getType(side)
  32.     local name = peripheral.getType(side)
  33.     local correct_name = name
  34.     if isAdvancedMonitor(side) then correct_name = "advanced_monitor" end
  35.     if isWirelessModem(side) then correct_name = "wireless_modem" end
  36.     return name, correct_name
  37.   end
  38.  
  39.   function wrapByName(str)
  40.     if not types[str] then return false end --  check with White-list
  41.  
  42.     local results = {}
  43.     for _,side in pairs(peripheral.getNames()) do   --  search valid sides
  44.       local type_name, correct_type_name = getType(side)
  45.  
  46.       if str == type_name or str == correct_type_name then
  47.         local wrapped = peripheral.wrap(side)
  48.         wrapped.getType = function() return type_name, correct_type_name end
  49.         wrapped.getSide = function() return side end
  50.         table.insert(results, wrapped)
  51.       else
  52.         -- do nothing
  53.       end
  54.     end
  55.     return unpack(results)
  56.   end
  57.  
  58.   function wrap(str) -- selector
  59.     local results = {}
  60.     if types[str] then       -- types
  61.       results = {wrapByName(str)}
  62.     else                     -- sides and the others
  63.       local result = peripheral.wrap(str)
  64.       if result then
  65.           result.getType = function() return getType(str) end
  66.           result.getSide = function() return str end
  67.         results = {result}
  68.       end
  69.     end
  70.     return unpack(results)
  71.   end
  72.  
  73.   find = wrap
  74.  
  75.   function waitForDetection(str)
  76.     local results = {}
  77.     repeat
  78.       results = {wrap(str)}
  79.       if #results == 0 then os.pullEvent("peripheral") end
  80.     until #results >0
  81.    
  82.     return unpack(results)
  83.   end
  84.  
  85. end
  86.  
  87. --[[
  88. print('Now waiting...')
  89. local tmp1 = waitForDetection("wireless_modem")
  90. print('ok.')
  91.  
  92. for k,v in pairs(tmp1) do
  93.   print(k,' = ',v)
  94. end
  95. --]]
RAW Paste Data
Pastebin PRO Summer Special!
Get 40% OFF on Pastebin PRO accounts!
Top