Advertisement
Guest User

Untitled

a guest
Feb 16th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. local component = require'component'
  2. local gpu = component.gpu
  3.  
  4.  
  5. -- # create table containing all screens
  6. local screens = component.list('screen')
  7.  
  8. -- # instead of invoking 'component.list' every time we make a gpu call
  9. -- # we just update the list whenever a screen is added or removed
  10. local function updatescreens(event, addr, ctype)
  11. if ctype ~= 'screen' then return nil end
  12. if event == 'component_added' then
  13. screens[addr] = 'screen'
  14. else
  15. screens[addr] = nil
  16. end
  17. end
  18.  
  19. -- # listening to component adding or removing events, invoking 'updatescreen' on event
  20. require'event'.listen('component_added', updatescreens)
  21. require'event'.listen('component_removed', updatescreens)
  22.  
  23. -- # creating proxy table for gpu calls
  24. local vgpu = {}
  25. -- # making vgpu its own metatable
  26. setmetatable(vgpu, vgpu)
  27.  
  28. -- # would not make sense to call 'bind' for every screen
  29. vgpu.bind = gpu.bind
  30.  
  31. -- # '__index' function
  32. function vgpu.__index(self, key)
  33. -- # since some gpu methods return some data, we differentiate between 'setter' and 'getter' methods
  34. -- # most setter function have 'set' in their name, 'fill' and 'copy' are the exception
  35. local setter = { copy = true; fill = true}
  36. if key:find('set') or setter[key] then
  37. -- # function that will only call the methods, does not return anything (for setters)
  38. return function(...)
  39. for addr in pairs(screens) do
  40. self.bind(addr)
  41. gpu[key](...)
  42. end
  43. end
  44. else
  45. -- # function that will return data (for getters)
  46. -- # returns table with screen address as key and table or single object as value
  47. -- # {'addr 1' = value1, 'addr 2' = value2, ...}
  48. return function(...)
  49. local res = {}
  50. for addr in pairs(screens) do
  51. self.bind(addr)
  52. local r = {gpu[key](...)}
  53. res[addr] = #r == 1 and r[1] or r
  54. end
  55. return res
  56. end
  57. end
  58. end
  59.  
  60. -- returns gpu proxy, so it can be used as a module
  61. return vgpu
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement