It extends the "redstone" event that os.pullEvent() returns with the sides that have changed.
The API keeps track of the redstone input states on all 6 sides of the computer.
If any of the redstone input states change, the event "redstone" will return a string denoting the type of redstone-input, as well as a table of all the sides that have changed.
If vanilla redstone was changed, type will be "vanilla".
If bundled cable was changed, type will be "bundled_cable".
http://en.wikipedia.org/wiki/WTFPL
Exposed Function
pullEvent( event, side )
- event is an optional event filter. ( Just like the native event filter, so nothing new. )
- side is a table of strings and acts as a side filter, i.e. you can choose to listen to only specific sides and ignore all others.
- Returns: event, type, changes
- [indent=1]event == "redstone"[/indent]
- [indent=1]type == "vanilla" (for vanilla redstone) or "bundled_cable" (for redpower bundled cable)[/indent]
- [indent=1]changes = table of strings denoting the changed sides, e.g. { "back", "bottom }[/indent]
String
Table
How to use?
- Save the API Code and make note of the filename, e.g. 're'
- Load the API within the program where you want to make use of it via os.loadAPI, e.g. os.loadAPI( "re" )
- Then call the API's pullEvent by accessing it via the API name, e.g. re.pullEvent()
API - Code
Pastebin: http://pastebin.com/egdXMS6X
Direct-View:
Spoiler
local tRedstoneState = {} -- Holds the state of all redstone sides on the computer.
local tCableState = {} -- Holds the state of all bundled cables on the computer.
-- Get initial redstone state for all 6 sides.
for _, side in pairs( rs.getSides() ) do
tRedstoneState[ side ] = rs.getInput( side )
end
-- Get initial bundled cable state for all 6 sides.
for _, v in pairs( rs.getSides() ) do
tCableState[ v ] = rs.getBundledInput( v )
end
-- If a redstone side changes its state, then this function returns the event 'redstone' plus the string 'vanilla' and a table.
-- The returned table contains only sides that have changed and it has this format: { side = new_value }
-- If a bundled cable changes its state, then this function returns the event 'redstone' plus the string 'bundled_cable' and a table.
-- The returned table contains only sides that have changed and it has this format: { side = { new_value, old_value } }
function pullEvent( _sEvent, _tSide )
local bRedstoneChanged = false
local bCableChanged = false
local tChangedCable = {}
local tChangedRedstone = {}
local event, p1, p2, p3, p4, p5 = os.pullEvent( _sEventFilter )
local function checkRedstoneSides( _tSide )
-- Iterate over all _tSide sides of the computer.
-- If _tSide is not given, iterate over all 6 sides.
for _, side in pairs( _tSide or rs.getSides() ) do
local bCurrentInput = rs.getInput( side ) -- Get redstone input for current side.
if bCurrentInput ~= tRedstoneState[ side ] then -- Has the state changed?
tChangedRedstone[ side ] = bCurrentInput -- Add the changed side + its new value to the return table.
tRedstoneState[ side ] = bCurrentInput -- Update the local state table.
bRedstoneChanged = true
end
end
return bRedstoneChanged, tChangedRedstone
end
local function checkBundledCableSides( _tSide )
-- Iterate over all _tSide sides of the computer.
-- If _tSide is not given, iterate over all 6 sides.
for _, side in pairs( _tSide or rs.getSides() ) do
local nCableInput = rs.getBundledInput( side ) -- Get cable input for current side.
if nCableInput ~= tCableState[ side ] then -- Has the state changed?
tChangedCable[ side ] = { nCableInput, tCableState[ side ] } -- Add the changed side + its new and old values to the return table.
tCableState[ side ] = nCableInput -- Update the local state table.
bCableChanged = true
end
end
return bCableChanged, tChangedCable
end
if event == "redstone" then
local bChanged, tSides = checkRedstoneSides( _tSide )
if bChanged then return event, "vanilla", tSides, p2, p3, p4, p5 end
local bChanged, tSides = checkBundledCableSides( _tSide )
if bChanged then return event, "bundled_cable", tSides, p2, p3, p4, p5 end
end
return event, p1, p2, p3, p4, p5
end
Example Program to try out the API
Save the API as "re".
Then run the program below, change some redstone inputs and see the results on screen.
Pressing "e" will exit the program.
Pastebin: http://pastebin.com/SpSbfa0h
Direct-View:
Spoiler
term.clear()
term.setCursorPos(1, 1)
-- Load API
os.loadAPI("re")
if not re then
error( "Redstone Event Extension API not loaded!" )
end
--[[ === MAIN LOOP === ]]
while true do
local event, param1, param2 = re.pullEvent()
if event == "redstone" then
write( event..", "..param1..", " )
if param1 == "vanilla" then
for sSide, bChanged in pairs( param2 ) do
write( "{ "..sSide.." = "..tostring( bChanged ).." }" )
end
end
if param1 == "bundled_cable" then
local newValue = 1
local oldValue = 2
for sSide, tChanged in pairs( param2 ) do
write( "{ "..sSide.." = { "..tostring( tChanged[newValue] )..", "..tostring( tChanged[oldValue] ).." } }" )
end
end
write( "n" )
end
if event == "char" and string.lower( param1 ) == "e" then break end -- Pressing 'e' will exit the program.
end
-- Cleanup
os.unloadAPI( re )