Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local component = require'component'
- local gpu = component.gpu
- -- # create table containing all screens
- local screens = component.list('screen')
- -- # instead of invoking 'component.list' every time we make a gpu call
- -- # we just update the list whenever a screen is added or removed
- local function updatescreens(event, addr, ctype)
- if ctype ~= 'screen' then return nil end
- if event == 'component_added' then
- screens[addr] = 'screen'
- else
- screens[addr] = nil
- end
- end
- -- # listening to component adding or removing events, invoking 'updatescreen' on event
- require'event'.listen('component_added', updatescreens)
- require'event'.listen('component_removed', updatescreens)
- -- # creating proxy table for gpu calls
- local vgpu = {}
- -- # making vgpu its own metatable
- setmetatable(vgpu, vgpu)
- -- # would not make sense to call 'bind' for every screen
- vgpu.bind = gpu.bind
- -- # '__index' function
- function vgpu.__index(self, key)
- -- # since some gpu methods return some data, we differentiate between 'setter' and 'getter' methods
- -- # most setter function have 'set' in their name, 'fill' and 'copy' are the exception
- local setter = { copy = true; fill = true}
- if key:find('set') or setter[key] then
- -- # function that will only call the methods, does not return anything (for setters)
- return function(...)
- for addr in pairs(screens) do
- self.bind(addr)
- gpu[key](...)
- end
- end
- else
- -- # function that will return data (for getters)
- -- # returns table with screen address as key and table or single object as value
- -- # {'addr 1' = value1, 'addr 2' = value2, ...}
- return function(...)
- local res = {}
- for addr in pairs(screens) do
- self.bind(addr)
- local r = {gpu[key](...)}
- res[addr] = #r == 1 and r[1] or r
- end
- return res
- end
- end
- end
- -- returns gpu proxy, so it can be used as a module
- return vgpu
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement