adds timers
This commit is contained in:
parent
96a255f40b
commit
123e3aaa57
|
@ -0,0 +1,50 @@
|
|||
--[[
|
||||
Copyright 2019 Harald Albrecht
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
]]--
|
||||
|
||||
-- luacheck: globals mb tick
|
||||
|
||||
-- Our timer queue ... is nothing more than a priority queue.
|
||||
mb.timers = mb.pq:new()
|
||||
mb.now = 0
|
||||
|
||||
-- Activates the given timer user function after a certain amount of time has
|
||||
-- passed.
|
||||
function mb.after(afterms, timerf)
|
||||
afterms = afterms < 0 and 0 or afterms
|
||||
mb.timers:add(mb.now + afterms, timerf)
|
||||
end
|
||||
|
||||
-- Tick gets called by the Keybow "firmware" every 1ms (or so). If any timers
|
||||
-- have gone off by now, then call their timer user functions. Keybow's tick
|
||||
-- ms counter has its epoch set when the Keybow Lua scripting started (so it's
|
||||
-- not a *nix timestamp or such).
|
||||
function tick(t)
|
||||
mb.now = t
|
||||
while true do
|
||||
local next, timerf = mb.timers:peek()
|
||||
if next == nil or t < next then
|
||||
break
|
||||
end
|
||||
mb.timers:remove()
|
||||
timerf(t)
|
||||
end
|
||||
end
|
|
@ -31,6 +31,8 @@ require "keybow"
|
|||
-- Pulls in the individual modules that make up Multibow.
|
||||
mb.path = (...):match("^(.-)[^%/]+$")
|
||||
|
||||
mb.pq = require(mb.path .. "mb/prioqueue")
|
||||
require(mb.path .. "mb/timer")
|
||||
require(mb.path .. "mb/morekeys")
|
||||
require(mb.path .. "mb/keymaps")
|
||||
require(mb.path .. "mb/keys")
|
||||
|
|
|
@ -38,9 +38,14 @@ describe("prioqueue", function()
|
|||
|
||||
p, v = q:remove()
|
||||
assert.is.same({100, "bar1"}, {p, v})
|
||||
|
||||
q:add(100, "bar3")
|
||||
|
||||
p, v = q:remove()
|
||||
assert.is.same({100, "bar2"}, {p, v})
|
||||
p, v = q:remove()
|
||||
assert.is.same({100, "bar3"}, {p, v})
|
||||
p, v = q:remove()
|
||||
assert.is.same({200, "foo"}, {p, v})
|
||||
p, v = q:remove()
|
||||
assert.is.same({300, "zoo"}, {p, v})
|
||||
|
|
|
@ -0,0 +1,77 @@
|
|||
--[[
|
||||
Copyright 2019 Harald Albrecht
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
]]--
|
||||
|
||||
require "mocked-keybow"
|
||||
local mb = require("snippets/multibow")
|
||||
|
||||
local ticktock = function(ms)
|
||||
local delta = 10
|
||||
local now = mb.now
|
||||
for passed = delta,ms,delta do
|
||||
_G["tick"](now + passed)
|
||||
end
|
||||
end
|
||||
|
||||
describe("multibow timers", function()
|
||||
|
||||
before_each(function()
|
||||
mb.now = 0
|
||||
end)
|
||||
|
||||
it("ticks", function()
|
||||
assert.is.equal(mb.now, 0)
|
||||
ticktock(100)
|
||||
assert.is.equal(mb.now, 100)
|
||||
end)
|
||||
|
||||
it("triggers alarm at the right time", function()
|
||||
local timer = spy.new(function() end)
|
||||
mb.after(100, timer)
|
||||
|
||||
ticktock(50)
|
||||
assert.spy(timer).was_not.called()
|
||||
ticktock(200)
|
||||
assert.spy(timer).was.called(1)
|
||||
|
||||
timer:clear()
|
||||
mb.after(-1, timer)
|
||||
ticktock(100)
|
||||
assert.spy(timer).was.called(1)
|
||||
end)
|
||||
|
||||
it("triggers multiple alarms in correct order", function()
|
||||
local t1 = spy.new(function() end)
|
||||
local t2 = spy.new(function() end)
|
||||
mb.after(100, t2)
|
||||
mb.after(50, t1)
|
||||
|
||||
ticktock(60)
|
||||
assert.spy(t1).was.called(1)
|
||||
assert.spy(t2).was_not.called()
|
||||
|
||||
t1:clear()
|
||||
ticktock(50)
|
||||
assert.spy(t1).was_not.called()
|
||||
assert.spy(t2).was.called(1)
|
||||
end)
|
||||
|
||||
end)
|
Loading…
Reference in New Issue