Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- ###############################
- -- h2peripheral API
- -- version 0.1
- -- http://hevohevo.hatenablog.com/
- -- Write in your program.
- -- os.loadAPI("h2peripheral")
- os.loadAPI("rom/apis/peripheral") -- to make sure
- if rs and peripheral and os then
- local types = { -- White-list
- drive="Disk Drive",
- monitor="Monitor and Advanced Monitor",
- advanced_monitor="Advanced Monitor",
- modem="Wired Modem and Wireless Modem",
- wireless_modem="Wireless Modem",
- workbench="Crafty Turtle's workbench",
- printer="Printer",
- command="Command Block"
- }
- function wrapByName(str)
- if not types[str] then return false end -- check with White-list
- local f_tbl = nil
- for _,side in pairs(rs.getSides()) do -- search valid sides
- local type_name = peripheral.getType(side)
- -- "drive", "monitor", "modem", "workbench", "printer", "command"
- if type_name == str then
- f_tbl = peripheral.wrap(side)
- f_tbl.side = side
- break
- elseif str == "advanced_monitor" and type_name == "monitor" and peripheral.call(side, "isColor") then
- f_tbl = peripheral.wrap(side)
- f_tbl.side = side
- break
- elseif str == "wireless_modem" and type_name == "modem" and peripheral.call(side, "isWireless") then
- f_tbl = peripheral.wrap(side)
- f_tbl.side = side
- break
- end
- end
- return f_tbl
- end
- function wrap(str) -- selector
- local f_tbl = nil
- if types[str] then -- types
- f_tbl = wrapByName(str)
- else -- sides and the others
- f_tbl = peripheral.wrap(str)
- if f_tbl then f_tbl.side = str end
- end
- return f_tbl
- end
- function waitForDetect(str)
- local f_tbl = nil
- repeat
- f_tbl = wrap(str)
- if not f_tbl then os.pullEvent("peripheral") end
- until f_tbl
- return f_tbl
- end
- end
- --[[
- local tmp1 = waitForDetect("wireless_modem")
- local tmp2 = waitForDetect("advanced_monitor")
- for k,v in pairs(tmp1) do
- print(k,' = ',v)
- end
- --]]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement