Posted 10 September 2013 - 04:12 PM
How to have a program check for modems, then open the ones that exist. This could be useful for rednet programs, or a email program.
local present = false
for _, side in pairs(rs.getSides()) do
if peripheral.getType(side) == "modem" then
present = true
rednet.open(side) -- this or wrap to a modem object if you're planning to use the modem API instead of rednet
end
end
if not present then
print("A modem was not found, please attach one and re-run this program")
end
-- Returns a table with the sides and peripheral types
function getPeripheralList()
local sides = peripheral.getNames()
local pTable = {}
for k,side in pairs(sides) do
pTable[side] = peripheral.getType(side)
end
return pTable
end
-- finds the first peripheral of type 'peripheralType' and wraps it
function getPeripheral(peripheralType)
local pTable = getPeripheralList()
for side, pType in pairs(pTable) do
if(peripheralType == pType) then
return peripheral.wrap(side)
end
end
return nil
end
-- finds all peripherals of type 'peripheralType' and wraps them
function getAllPeripherals(peripheralType)
local pTable = getPeripheralList()
local wrappedTable = {}
for side, pType in pairs(pTable) do
if(peripheralType == pType) then
wrappedTable[side] = peripheral.wrap(side)
end
end
return wrappedTable
end
local monitor = getPeripheral("monitor")
monitor.write("Hello!")
local cells = getAllPeripherals("redstone_energy_cell")
for k,cell in pairs(cells) do
print(cell.getEnergyStored())[font=Monaco, Menlo, Consolas,]getEnergyStored[/font][font=Monaco, Menlo, Consolas,]()[/font]
end