Advertisement
Guest User

Untitled

a guest
May 17th, 2015
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.42 KB | None | 0 0
  1. local libcomponent = {
  2. type = function (address)
  3. if address == FS1 then return "filesystem"
  4. elseif address == SCREEN then return "screen"
  5. else
  6. print("unknown addr",address)
  7. end
  8. end
  9. }
  10. -- from machine.lua in open computers
  11. local function spcall(...)
  12. local result = table.pack(pcall(...))
  13. if not result[1] then
  14. error(tostring(result[2]), 0)
  15. else
  16. return table.unpack(result, 2, result.n)
  17. end
  18. end
  19. function checkArg(n, have, ...)
  20. have = type(have)
  21. local function check(want, ...)
  22. if not want then
  23. return false
  24. else
  25. return have == want or check(...)
  26. end
  27. end
  28. if not check(...) then
  29. local msg = string.format("bad argument #%d (%s expected, got %s)",n, table.concat({...}, " or "), have)
  30. error(msg, 3)
  31. end
  32. end
  33. function libcomponent.proxy(address)
  34. print(debug.traceback())
  35. print(type(libcomponent))
  36. print(libcomponent.type)
  37. local type, reason = spcall(libcomponent.type, address)
  38. if not type then
  39. return nil, reason
  40. end
  41. end
  42. -- end of copy/paste
  43. local FS1 = "4e33d6c6-7229-40da-9688-e1276d9f978b"
  44. local SCREEN = "73822386-3f58-4d0b-8fad-fdf5a34d0a8a"
  45. local KEYBOARD = "de3153c6-0a80-4c1a-9e5b-867935d759df"
  46. local EEPROM = "56af8068-b597-4336-bfdb-b9df970309d8"
  47. function libcomponent.list(filter)
  48. print("listing:",filter)
  49. local ret = {}
  50. local first = nil
  51. local firstname = nil
  52. function test(name)
  53. -- todo, substring search
  54. if filter == name then
  55. return true
  56. elseif filter == nil then
  57. return true
  58. end
  59. return false
  60. end
  61. function addDev(addr,name)
  62. ret[addr] = name
  63. if first == nil then
  64. first = addr
  65. firstname = name
  66. end
  67. end
  68. if test("eeprom") then addDev(EEPROM,"eeprom") end
  69. if test("screen") then addDev(SCREEN,"screen") end
  70. if test("gpu") then addDev("1d721b0c-b681-4b80-ba46-7eacbc6d1cfb","gpu") end
  71. if test("filesystem") then addDev(FS1,"filesystem") end
  72. for k,v in pairs(ret) do print(k,v) end
  73. print(ret)
  74. local meta = {}
  75. meta.__call = function ()
  76. -- TODO, iterator
  77. local hack = first
  78. first = nil
  79. return hack
  80. end
  81. setmetatable(ret,meta)
  82. return ret
  83. end
  84. local eepromUserData = nil;
  85. function libcomponent.invoke(address,method,...)
  86. if address == nil then
  87. print(debug.traceback())
  88. end
  89. print("calling ",method," on ",address)
  90. --if ... then print(...) end
  91. if address == FS1 then
  92. if method == "open" then
  93. local filename = ...
  94. return io.open("FS1/"..filename,"r")
  95. elseif method == "read" then
  96. local fh,size = ...
  97. if size == math.huge then size = 4096 end
  98. return fh:read(size)
  99. elseif method == "close" then
  100. local fh = ...
  101. return fh:close()
  102. end
  103. elseif address == SCREEN then
  104. if method == "getKeyboards" then
  105. return KEYBOARD
  106. end
  107. elseif address == EEPROM then
  108. if method == "setData" then
  109. eepromUserData = ...
  110. elseif method == "getData" then
  111. return eepromUserData
  112. end
  113. end
  114. end
  115. computer = {}
  116. function computer.beep(...)
  117. print("BEEP!",...)
  118. end
  119. function table.pack(...)
  120. return { n=select("#",...), ... }
  121. end
  122. function table.unpack(...)
  123. return unpack(...)
  124. end
  125. function load(...)
  126. return loadstring(...)
  127. end
  128.  
  129. local fh = assert(io.open("bios.lua","r"))
  130. local eeprom = fh:read("*all")
  131. local prom,err = loadstring(eeprom,"=eeprom")
  132. if prom then
  133. component = libcomponent
  134. local result,ret = pcall(prom)
  135. if result then
  136. io.stdout:write("sucess")
  137. else
  138. print("fail:",ret)
  139. end
  140. else
  141. io.stdout:write("failed to load prom:"..err.."\n")
  142. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement