Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- component = {
- type = function (address)
- if address == FS1 then return "filesystem"
- elseif address == SCREEN then return "screen"
- else
- print("unknown addr",address)
- end
- end
- }
- -- from machine.lua in open computers
- local function spcall(...)
- local result = table.pack(pcall(...))
- if not result[1] then
- error(tostring(result[2]), 0)
- else
- return table.unpack(result, 2, result.n)
- end
- end
- function checkArg(n, have, ...)
- have = type(have)
- local function check(want, ...)
- if not want then
- return false
- else
- return have == want or check(...)
- end
- end
- if not check(...) then
- local msg = string.format("bad argument #%d (%s expected, got %s)",n, table.concat({...}, " or "), have)
- error(msg, 3)
- end
- end
- function component.proxy(address)
- print(debug.traceback())
- print(type(component))
- print(component.type)
- local type, reason = spcall(component.type, address)
- if not type then
- return nil, reason
- end
- end
- -- end of copy/paste
- local FS1 = "4e33d6c6-7229-40da-9688-e1276d9f978b"
- local SCREEN = "73822386-3f58-4d0b-8fad-fdf5a34d0a8a"
- local KEYBOARD = "de3153c6-0a80-4c1a-9e5b-867935d759df"
- local EEPROM = "56af8068-b597-4336-bfdb-b9df970309d8"
- function component.list(filter)
- print("listing:",filter)
- local ret = {}
- local first = nil
- local firstname = nil
- function test(name)
- -- todo, substring search
- if filter == name then
- return true
- elseif filter == nil then
- return true
- end
- return false
- end
- function addDev(addr,name)
- ret[addr] = name
- if first == nil then
- first = addr
- firstname = name
- end
- end
- if test("eeprom") then addDev(EEPROM,"eeprom") end
- if test("screen") then addDev(SCREEN,"screen") end
- if test("gpu") then addDev("1d721b0c-b681-4b80-ba46-7eacbc6d1cfb","gpu") end
- if test("filesystem") then addDev(FS1,"filesystem") end
- for k,v in pairs(ret) do print(k,v) end
- print(ret)
- local meta = {}
- meta.__call = function ()
- -- TODO, iterator
- local hack = first
- first = nil
- return hack
- end
- setmetatable(ret,meta)
- return ret
- end
- local eepromUserData = nil;
- function component.invoke(address,method,...)
- if address == nil then
- print(debug.traceback())
- end
- print("calling ",method," on ",address)
- --if ... then print(...) end
- if address == FS1 then
- if method == "open" then
- local filename = ...
- return io.open("FS1/"..filename,"r")
- elseif method == "read" then
- local fh,size = ...
- if size == math.huge then size = 4096 end
- return fh:read(size)
- elseif method == "close" then
- local fh = ...
- return fh:close()
- end
- elseif address == SCREEN then
- if method == "getKeyboards" then
- return KEYBOARD
- end
- elseif address == EEPROM then
- if method == "setData" then
- eepromUserData = ...
- elseif method == "getData" then
- return eepromUserData
- end
- end
- end
- computer = {}
- function computer.beep(...)
- print("BEEP!",...)
- end
- function table.pack(...)
- return { n=select("#",...), ... }
- end
- function table.unpack(...)
- return unpack(...)
- end
- function load(...)
- return loadstring(...)
- end
- local fh = assert(io.open("bios.lua","r"))
- local eeprom = fh:read("*all")
- local prom,err = loadstring(eeprom,"=eeprom")
- if prom then
- local result,ret = pcall(prom)
- if result then
- io.stdout:write("sucess")
- else
- print("fail:",ret)
- end
- else
- io.stdout:write("failed to load prom:"..err.."\n")
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement