Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --[[
- SimpleAdore - wireless made simple
- v1.0
- Copyright (C) 2017 HaddockDev
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- ]]
- --Vars
- local Channels = {}
- local Modem = nil
- local isDebug = false
- --Helper functions
- local function l (msg)
- if isDebug then
- print(msg)
- end
- end
- local function OneOf (needle,haystack)
- for _k,v in ipairs(haystack) do
- if v == needle then
- return true
- end
- end
- return false
- end
- local function FindPeripheral (type)
- local sides = {"top","bottom","left","right","back","front"}
- for _k,v in ipairs(sides) do
- if peripheral.getType(v) == type then
- return type
- end
- end
- return nil
- end
- local function ModemCheck(err)
- if not Modem then
- if err then
- error(err)
- else
- return false
- end
- end
- return true
- end
- local function Try(fn)
- pcall(function()fn()end)
- end
- --API
- function WrapModem(side)
- local side = side or FindPeripheral("wireless_modem")
- if side ~= nil then
- Modem = peripheral.wrap(side)
- l("Wrapped modem on side " .. side)
- end
- end
- function AddChannel(name, channel)
- Channels[name] = channel
- Modem.open(channel)
- l("Added channel " .. name .. "/" .. channel)
- end
- function GetChannels()
- return Channels
- end
- --Wireless
- function Transmit(channel, data)
- if type(data) == "table" then
- data = textutils.serialize(data)
- end
- Modem.transmit(channel, channel + 1, data)
- end
- function Recieve (channel)
- local ok = false
- while not ok do
- local e, _1, sChannel, _2, msg, dist = os.pullEvent("modem_message")
- if sChannel == channel then
- return msg, dist
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement