-
-
Save ambv/293fa69b38e63dc468b092f217b4d67c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
local g = grid.connect() | |
local screen_dirty = false | |
local grid_dirty = false | |
local pixels = {} | |
local pattern = 0 | |
local pattern1_metro = metro.init() | |
local pattern2_metro = metro.init() | |
local redraw_metro = metro.init() | |
function init() | |
engine.name = 'PolyPerc' | |
redraw_metro.event = redraw_event | |
redraw_metro:start(1 / 30) | |
pattern1_metro.count = 30 | |
pattern1_metro.time = 0.030 | |
pattern1_metro.event = function(stage) | |
if math.fmod(stage, 30) == 1 then | |
random_pattern(1, 8) | |
else | |
fadeout(1, 8) | |
end | |
end | |
pattern2_metro.count = 30 | |
pattern2_metro.time = 0.030 | |
pattern2_metro.event = function(stage) | |
if math.fmod(stage, 30) == 1 then | |
random_pattern(9, 16) | |
else | |
fadeout(9, 16) | |
end | |
end | |
for i = 1, g.cols * g.rows do | |
pixels[i] = 0 | |
end | |
end | |
function key(k, z) | |
if z == 0 then | |
if k == 2 then | |
clock.link.start() | |
end | |
if k == 3 then | |
clock.link.stop() | |
end | |
end | |
end | |
function clock.transport.start() | |
if pattern ~= 0 then | |
return | |
end | |
pattern = clock.run(function() | |
local tick = 0 | |
while true do | |
clock.sync(1) -- 1 beat | |
tick = tick + 1 | |
if math.fmod(tick, 4) == 1 then | |
pattern1_metro:start() | |
else | |
pattern2_metro:start() | |
end | |
end | |
end) | |
end | |
function clock.transport.stop() | |
if pattern == 0 then | |
return | |
end | |
clock.cancel(pattern) | |
pattern1_metro:stop() | |
pattern2_metro:stop() | |
pattern = 0 | |
end | |
function fadeout(start, stop) | |
local pidx = 0 | |
for x = start, stop do | |
for y = 1, g.rows do | |
pidx = x + ((y - 1) * g.cols) | |
if pixels[pidx] > 0 then | |
pixels[pidx] = pixels[pidx] - 1 | |
grid_dirty = true | |
end | |
end | |
end | |
end | |
function random_pattern(start, stop) | |
local pidx = 0 | |
for x = start, stop do | |
for y = 1, g.rows do | |
pidx = x + ((y - 1) * g.cols) | |
pixels[pidx] = math.random(0, 15) | |
end | |
end | |
grid_dirty = true | |
end | |
function redraw_event() | |
if screen_dirty then | |
redraw() | |
screen_dirty = false | |
end | |
if grid_dirty then | |
grid_redraw() | |
grid_dirty = false | |
end | |
end | |
function grid_redraw() | |
local pidx = 0 | |
g:intensity(15) | |
for x = 1, g.cols do | |
for y = 1, g.rows do | |
pidx = x + ((y - 1) * g.cols) | |
g:led(x, y, pixels[pidx]) | |
end | |
end | |
g:refresh() | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment