diff --git a/sys/apis/event.lua b/sys/apis/event.lua index 8aa2d9c..efff40c 100644 --- a/sys/apis/event.lua +++ b/sys/apis/event.lua @@ -61,19 +61,20 @@ local function nextUID() return Event.uid - 1 end -function Event.on(type, fn) - local event = Event.types[type] - if not event then - event = { } - Event.types[type] = event +function Event.on(event, fn) + + local handlers = Event.types[event] + if not handlers then + handlers = { } + Event.types[event] = handlers end local handler = { uid = nextUID(), - event = type, + event = event, fn = fn, } - event[handler.uid] = handler + handlers[handler.uid] = handler setmetatable(handler, { __index = Routine }) return handler @@ -88,14 +89,15 @@ end local function addTimer(interval, recurring, fn) local timerId = os.startTimer(interval) + local handler - return Event.on('timer', function(t, id) + handler = Event.on('timer', function(t, id) if timerId == id then fn(t, id) if recurring then timerId = os.startTimer(interval) else - Event.off(t) + Event.off(handler) end end end) @@ -160,26 +162,22 @@ function Event.exitPullEvents() os.sleep(0) end -local function processHandlers(e, ...) +local function processHandlers(event) - local event = Event.types[e] - if event then - - local keys = Util.keys(event) - for _,key in pairs(keys) do - - local h = event[key] - if h and not h.co then + local handlers = Event.types[event] + if handlers then + for _,h in pairs(handlers) do + if not h.co then -- callbacks are single threaded (only 1 co per handler) h.co = coroutine.create(h.fn) Event.routines[h.uid] = h - h:resume(h, ...) end end end end local function processRoutines(...) + local keys = Util.keys(Event.routines) for _,key in ipairs(keys) do local r = Event.routines[key] @@ -194,7 +192,7 @@ function Event.pullEvent(eventType) while true do local e = { os.pullEventRaw() } - processHandlers(table.unpack(e)) + processHandlers(e[1]) processRoutines(table.unpack(e)) if Event.terminate or e[1] == 'terminate' then diff --git a/sys/apps/Help.lua b/sys/apps/Help.lua index 1ac0132..0ad7702 100644 --- a/sys/apps/Help.lua +++ b/sys/apps/Help.lua @@ -41,7 +41,9 @@ local function showHelp(name) UI.term:reset() shell.run('help ' .. name) print('Press enter to return') - read() + repeat + local _, k = os.pullEvent('key_up') + until k == keys.enter end function page:eventHandler(event) diff --git a/sys/apps/recorder.lua b/sys/apps/recorder.lua index 2485256..9adfd91 100644 --- a/sys/apps/recorder.lua +++ b/sys/apps/recorder.lua @@ -71,28 +71,23 @@ if #filename == 0 then end print('Initializing...') -fs.mount('.recGif', 'ramfs', 'directory') -fs.mount('.recGif/GIF', 'urlfs', 'http://pastebin.com/raw/5uk9uRjC') -fs.mount('.recGif/package', 'urlfs', 'http://pastebin.com/raw/cUYTGbpb') -- don't pollute global env -local function loadAPI(filename, env) +-- convert these to require style apis +local function loadAPI(url, env) local apiEnv = Util.shallowCopy(env) apiEnv.shell = nil apiEnv.multishell = nil setmetatable(apiEnv, { __index = _G }) - local fn = loadfile(filename, apiEnv) + local fn = Util.loadUrl(url, apiEnv) fn() return apiEnv end -package = loadAPI('.recGif/package', getfenv(1)) -GIF = loadAPI('.recGif/GIF', getfenv(1)) +bbpack = loadAPI('http://pastebin.com/raw/PdrJjb5S', getfenv(1)) +GIF = loadAPI('http://pastebin.com/raw/5uk9uRjC', getfenv(1)) -local oldDir = shell.dir() -shell.setDir('.recGif') -shell.run("package get Y0eLUPtr") -shell.setDir(oldDir) +Util.runUrl(getfenv(1), 'http://pastebin.com/raw/cUYTGbpb', 'get', 'Y0eLUPtr') local function snooze() local myEvent = tostring({}) @@ -239,7 +234,7 @@ snooze() -- Load font data: do - local ascii, counter = GIF.toPaintutils(GIF.flattenGIF(GIF.loadGIF(".recGif/ascii.gif"))), 0 + local ascii, counter = GIF.toPaintutils(GIF.flattenGIF(GIF.loadGIF("ascii.gif"))), 0 local newFont, ybump, xbump = #ascii ~= #ascii[1], 0, 0 charW, charH, chars = newFont and #ascii[1] / 16 or #ascii[1] * 3 / 64, #ascii / 16, {} @@ -534,6 +529,6 @@ buffer = nil GIF.saveGIF(image, filename) -fs.unmount('.recGif') +fs.delete('ascii.gif') print("Encode complete") diff --git a/sys/extensions/tgps.lua b/sys/extensions/tgps.lua index 9538aa9..e77f85d 100644 --- a/sys/extensions/tgps.lua +++ b/sys/extensions/tgps.lua @@ -6,7 +6,7 @@ require = requireInjector(getfenv(1)) local GPS = require('gps') function turtle.enableGPS(timeout) - if turtle.point.gps == 'GPS' then + if turtle.point.gps then return turtle.point end