adds timer example glow layout
This commit is contained in:
parent
2f18366c16
commit
2568d72c5e
|
@ -0,0 +1,74 @@
|
|||
-- A Multibow template layout, useful for starting your own keymap layouts.
|
||||
|
||||
--[[
|
||||
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.
|
||||
]]--
|
||||
|
||||
local glow = {} -- module
|
||||
|
||||
local mb = require("snippets/multibow")
|
||||
|
||||
--[[
|
||||
The Keybow layout is as follows when in landscape orientation, with the USB
|
||||
cable going off "northwards":
|
||||
|
||||
┋┋
|
||||
┌╌╌╌╌┐ ┌╌╌╌╌┐ ┌╌╌╌╌┐ ┌╌╌╌╌┐
|
||||
┊ 11 ┊ ┊ 8 ┊ ┊ 5 ┊ ┊ 2 ┊
|
||||
└╌╌╌╌┘ └╌╌╌╌┘ └╌╌╌╌┘ └╌╌╌╌┘
|
||||
┌╌╌╌╌┐ ┌╌╌╌╌┐ ┌╌╌╌╌┐ ┌╌╌╌╌┐
|
||||
┊ 10 ┊ ┊ 7 ┊ ┊ 4 ┊ ┊ 1 ┊
|
||||
└╌╌╌╌┘ └╌╌╌╌┘ └╌╌╌╌┘ └╌╌╌╌┘
|
||||
┌╌╌╌╌┐ ┌╌╌╌╌┐ ┌╌╌╌╌┐ ┌╌╌╌╌┐
|
||||
┊ 9 ┊ ┊ 6 ┊ ┊ 3 ┊ ┊ 0 ┊
|
||||
└╌╌╌╌┘ └╌╌╌╌┘ └╌╌╌╌┘ └╌╌╌╌┘
|
||||
|
||||
]]--
|
||||
|
||||
local timer = nil
|
||||
local t = 0
|
||||
local every = 10
|
||||
|
||||
function glow.activate()
|
||||
t = 0
|
||||
timer = mb.every(every, glow.led)
|
||||
end
|
||||
|
||||
function glow.deactivate()
|
||||
timer:cancel()
|
||||
timer = nil
|
||||
end
|
||||
|
||||
function glow.led()
|
||||
t = t + every
|
||||
brightness = 0.5 * (1 - math.cos(t/1000*math.pi))
|
||||
mb.led(0, {r=brightness, g=brightness, b=brightness})
|
||||
end
|
||||
|
||||
-- The keymap layout...
|
||||
glow.keymap = {
|
||||
name="glow",
|
||||
activate=glow.activate,
|
||||
deactivate=glow.deactivate
|
||||
}
|
||||
mb.register_keymap(glow.keymap)
|
||||
|
||||
return glow -- module
|
|
@ -63,8 +63,12 @@ function mb.register_keymap(keymap)
|
|||
-- (albeit the LEDs will only update later). Also maintain the (ordered)
|
||||
-- sequence of registered primary keymaps.
|
||||
if not (keymap.permanent or keymap.secondary) then
|
||||
local ckm = mb.current_keymap
|
||||
mb.current_keymap = mb.current_keymap or keymap
|
||||
table.insert(mb.primary_keymaps, keymap)
|
||||
if ckm and ckm.activate then
|
||||
ckm.activate()
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -132,11 +136,17 @@ end
|
|||
-- Activates a specific keymap by name. Please note that it isn't necessary
|
||||
-- to "activate" permanent keymaps at all (and thus this deed cannot be done).
|
||||
function mb.activate_keymap(name)
|
||||
if mb.current_keymap and mb.current_keymap.deactivate then
|
||||
mb.current_keymap.deactivate()
|
||||
end
|
||||
name = type(name) == "table" and name.name or name
|
||||
local keymap = mb.keymaps[name]
|
||||
if keymap and not keymap.permanent then
|
||||
mb.current_keymap = keymap
|
||||
mb.activate_leds()
|
||||
if keymap.activate then
|
||||
keymap.activate()
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -81,10 +81,16 @@ function mb.every(everyms, timerf, ...)
|
|||
timerf = timerf,
|
||||
targs = {...}
|
||||
}
|
||||
-- add an alarm after the first period which then triggers our shim
|
||||
-- function; only the shim function will then trigger the user function,
|
||||
-- and also readd the (shim) alarm so that the cycle will repeat ad
|
||||
-- nauseam.
|
||||
local tim = mb.after(
|
||||
everyms,
|
||||
function(shim)
|
||||
shim.timerf(table.unpack(shim.targs))
|
||||
everyms,
|
||||
function(shim) -- luacheck: ignore 431/shim
|
||||
if shim.timerf then
|
||||
shim.timerf(table.unpack(shim.targs))
|
||||
end
|
||||
shim.tim.at = mb.now + everyms
|
||||
mb.timers:add(shim.tim.at, shim.tim)
|
||||
end,
|
||||
|
|
|
@ -0,0 +1,74 @@
|
|||
--[[
|
||||
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 tt = require("spec/snippets/ticktock")
|
||||
|
||||
describe("template multibow keymap", function()
|
||||
|
||||
local mb = require("snippets/multibow")
|
||||
local glow
|
||||
|
||||
before_each(function()
|
||||
require("layouts/empty")
|
||||
glow = require("layouts/glow")
|
||||
end)
|
||||
|
||||
inslit("installs a glow, empty primary keymaps", function()
|
||||
assert.is_not_nil(glow) -- we're going over the top here...
|
||||
assert.is_not_nil(glow.keymap) -- ...even more so.
|
||||
|
||||
-- empty must be registered first, glow second.
|
||||
local kms = mb.registered_keymaps()
|
||||
assert.is.equal(2, #kms)
|
||||
local keymap = nil
|
||||
for _, km in ipairs(kms) do
|
||||
if km.name == "glow" then
|
||||
keymap = km
|
||||
end
|
||||
end
|
||||
assert.is_not_nil(keymap)
|
||||
assert.is_falsy(keymap.permanent)
|
||||
assert.is_falsy(keymap.secondary)
|
||||
assert.is.equal(keymap.name, "glow")
|
||||
end)
|
||||
|
||||
|
||||
inslit("calls activation handler", function()
|
||||
local act = spy.on(glow.keymap, "activate")
|
||||
local deact = spy.on(glow.keymap, "deactivate")
|
||||
local led = spy.on(glow, "led")
|
||||
|
||||
mb.activate_keymap("glow")
|
||||
assert.spy(act).was.called(1)
|
||||
assert.spy(deact).was_not.called()
|
||||
|
||||
tt.ticktock(100)
|
||||
assert.spy(led).was.called(10)
|
||||
|
||||
act:clear()
|
||||
mb.activate_keymap("empty")
|
||||
assert.spy(act).was_not.called()
|
||||
assert.spy(deact).was.called(1)
|
||||
end)
|
||||
|
||||
end)
|
|
@ -0,0 +1,33 @@
|
|||
--[[
|
||||
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.
|
||||
]]--
|
||||
|
||||
local tt = {} -- module
|
||||
|
||||
function tt.ticktock(ms)
|
||||
local delta = 10
|
||||
local now = mb.now
|
||||
for passed = delta,ms,delta do
|
||||
_G["tick"](now + passed)
|
||||
end
|
||||
end
|
||||
|
||||
return tt -- module
|
|
@ -22,14 +22,7 @@ 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
|
||||
local tt = require("spec/snippets/ticktock")
|
||||
|
||||
describe("multibow timers", function()
|
||||
|
||||
|
@ -39,7 +32,7 @@ describe("multibow timers", function()
|
|||
|
||||
it("ticks", function()
|
||||
assert.is.equal(mb.now, 0)
|
||||
ticktock(100)
|
||||
tt.ticktock(100)
|
||||
assert.is.equal(mb.now, 100)
|
||||
end)
|
||||
|
||||
|
@ -47,17 +40,17 @@ describe("multibow timers", function()
|
|||
local trigger = spy.new(function() end)
|
||||
local timer = mb.after(100, trigger, 1, 2, 3)
|
||||
|
||||
ticktock(50)
|
||||
tt.ticktock(50)
|
||||
assert.spy(trigger).was_not.called()
|
||||
assert.is.truthy(timer:isarmed())
|
||||
ticktock(200)
|
||||
tt.ticktock(200)
|
||||
assert.spy(trigger).was.called(1)
|
||||
assert.spy(trigger).was.called_with(1, 2, 3)
|
||||
assert.is.falsy(timer:isarmed())
|
||||
|
||||
trigger:clear()
|
||||
mb.after(-1, trigger)
|
||||
ticktock(100)
|
||||
tt.ticktock(100)
|
||||
assert.spy(trigger).was.called(1)
|
||||
end)
|
||||
|
||||
|
@ -67,12 +60,12 @@ describe("multibow timers", function()
|
|||
mb.after(100, t2)
|
||||
mb.after(50, t1)
|
||||
|
||||
ticktock(60)
|
||||
tt.ticktock(60)
|
||||
assert.spy(t1).was.called(1)
|
||||
assert.spy(t2).was_not.called()
|
||||
|
||||
t1:clear()
|
||||
ticktock(50)
|
||||
tt.ticktock(50)
|
||||
assert.spy(t1).was_not.called()
|
||||
assert.spy(t2).was.called(1)
|
||||
end)
|
||||
|
@ -81,15 +74,15 @@ describe("multibow timers", function()
|
|||
local trigger = spy.new(function() end)
|
||||
local timer = mb.after(100, trigger, 1, 2, 3)
|
||||
|
||||
ticktock(50)
|
||||
tt.ticktock(50)
|
||||
assert.is.truthy(timer:isarmed())
|
||||
timer:cancel()
|
||||
assert.is.falsy(timer:isarmed())
|
||||
ticktock(60)
|
||||
tt.ticktock(60)
|
||||
assert.spy(trigger).was_not.called()
|
||||
|
||||
timer:cancel()
|
||||
ticktock(10)
|
||||
tt.ticktock(10)
|
||||
assert.spy(trigger).was_not.called()
|
||||
end)
|
||||
|
||||
|
@ -97,13 +90,13 @@ describe("multibow timers", function()
|
|||
local trigger = spy.new(function() end)
|
||||
local timer = mb.every(20, trigger, 1, 2, 3)
|
||||
|
||||
ticktock(50)
|
||||
tt.ticktock(50)
|
||||
assert.spy(trigger).was.called(2)
|
||||
assert.spy(trigger).was.called_with(1, 2, 3)
|
||||
|
||||
trigger:clear()
|
||||
timer:cancel()
|
||||
ticktock(50)
|
||||
tt.ticktock(50)
|
||||
assert.spy(trigger).was_not.called()
|
||||
end)
|
||||
|
||||
|
|
Loading…
Reference in New Issue