Skip to content

Instantly share code, notes, and snippets.

@artfwo
Last active February 27, 2020 22:43
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save artfwo/115a7129b461f3f07fae5016ae15d048 to your computer and use it in GitHub Desktop.
Save artfwo/115a7129b461f3f07fae5016ae15d048 to your computer and use it in GitHub Desktop.
-- clock test
--
-- ...
-- .
engine.name = 'PolyPerc'
local dividers = { 1, 2 }
local led_states = { false, false }
local tasks = { nil, nil }
function blink_generator(x)
return function()
led_states[x] = true
redraw()
clock.sleep(0.1)
led_states[x] = false
redraw()
end
end
function tick_generator(x, hz)
return function()
while true do
clock.sync(1/dividers[x])
clock.run(blink_generator(x))
engine.hz(hz)
end
end
end
function init()
engine.release(0.125)
clock.run(function()
while true do
redraw()
clock.sleep(1/60)
end
end)
end
function enc(n, z)
if n == 2 then
dividers[1] = math.max(1, dividers[1] + z)
-- redraw()
elseif n == 3 then
dividers[2] = math.max(1, dividers[2] + z)
-- redraw()
end
end
function key(n, z)
if z == 1 then
if n == 2 then
if tasks[1] then
clock.stop(tasks[1])
tasks[1] = nil
else
tasks[1] = clock.run(tick_generator(1, 200))
end
-- redraw()
elseif n == 3 then
if tasks[2] then
clock.stop(tasks[2])
tasks[2] = nil
else
tasks[2] = clock.run(tick_generator(2, 400))
end
-- redraw()
end
end
end
function redraw()
screen.clear()
if tasks[1] then screen.level(15) else screen.level(3) end
screen.rect(24, 16, 16, 16)
if led_states[1] then
screen.fill()
end
screen.stroke()
screen.move(30, 48)
screen.text_center("1/"..dividers[1])
if tasks[2] then screen.level(15) else screen.level(3) end
screen.rect(88, 16, 16, 16)
if led_states[2] then
screen.fill()
end
screen.stroke()
screen.move(96, 48)
screen.text_center("1/"..dividers[2])
screen.update()
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment