hey y’all 
ahead of tomorrow’s norns release (which features the much-anticipated global clock), I figured it might help to document how I revisited super old code (less concepts) and successfully updated a beatclock-driven script to take advantage of the global clock! 
I figure we can use this as a space for community support getting scripts updated, so please feel free post your questions or share your speedbumps + successes!
overview: by using the new clock module, you can drive your script with a co-routine that syncs to the global clock and can be driven internally or thru MIDI, Ableton Link, or crow – without having to manage each of those sources/destinations separately in your script!
rounding up the old
first, I search my script for the word beatclock to find my old beatclock calls.
line 109 yields our first appearance
local beatclock = include "lib/beatclock-crow"
clk = beatclock.new()
clk_midi = midi.connect()
clk_midi.event = function(data) clk:process_midi(data) end
clk.on_select_external = function() clk:reset() end --from nattog
since I had aliased beatclock as clk throughout my script, I took a peek to see what clk stuff was being called elsewhere in my script (a search turns up 17 instances of the word ‘clk’).
line 369 yields a bunch of stuff in my `init()`
clk.on_step = function() iterate() end
clk.on_select_internal = function() clk:start() crow.input[2].mode("none") end
clk.on_select_external = function() print("external MIDI") crow.input[2].mode("none") end
clk.on_select_crow = function()
crow.input[2].mode("change",2,0.1,"both")
crow.input[2].change = change
end
clk:add_clock_params()
params:add{type = "number", id = "midi_device", name = "midi device", min = 1, max = 4, default = 1, action = function(value)
clk_midi.event = nil
clk_midi = midi.connect(value)
clk_midi.event = function(data) clk:process_midi(data) end
end}
and line 450 is where I started the old `beatclock` running
clk:start()
first blood
since I don’t want to use beatclock, I don’t need the instantiation stuff at line 109. the new global clock system is always running, from when norns wakes to when it sleeps.
what I do need to do is to tell my script how I want it to sync to the global clock. I do this by establishing a coroutine. coroutines are dope – can be synchronous or asynchronous, with the flexibility to pause and resume on demand.
for my script, I want to establish a coroutine that syncs to the global clock at a 16th note rate and performs the action that makes my script go.
when I was using beatclock, that go action was:
clk.on_step = function() iterate() end
so, I’ll instantiate a coroutine to simply do the same thing:
function pulse()
while true do
clock.sync(1/4)
iterate()
end
end
this just says:
- while my coroutine is running / true (we’ll get it running a little later)…
-
sync to the global clock at a subdivided rate (1 == 1 beat, so 1/4 == 1/4 of a beat == 16th notes at 4/4) and…
- execute the
iterate() function on every tick!
since the new global clock handles internal clocking, MIDI, crow, and Link, I don’t need to differentiate between the sources in my script – the system handles all the swapping!
now, I can delete all of that garbage from line 369! wowowow, we got rid of so much (honestly, sorta unreadable) code already and replaced it with a terse 6 line coroutine!
i’ll also kill the line at 450, which started the old beatclock, and replace it with a command that starts my pulse coroutine running:
clock.run(pulse)
I fire up the new coroutine-ified less concepts and confirm it’s all working!
tying up loose ends
nb. this bit isn’t typical of what most scripts will have to do. less concepts was written a long while ago and features some wonky choices™
removing my script’s beatclock dependency will also change my script’s params (beatclock supplies three: “clock”, “bpm”, and “clock_out”), so I do a quick search to see if I’ve got any hard references to these throughout my script.
turns out, I reference beatclock params in two places:
line 978 has getters
io.write(params:get("bpm") .. "\n")
io.write(params:get("clock_out") .. "\n")
line 1022 has setters
params:set("bpm", load_bpm)
params:set("clock_out", load_clock)
just to keep the experience equal for users of both the pre- and post-beatclock versions, I’ll just swap these out with the new params references:
978 becomes:
io.write(params:get("clock_tempo") .. "\n")
io.write(params:get("clock_midi_out") .. "\n")
and 1022 becomes:
params:set("clock_tempo", load_bpm)
params:set("clock_midi_out", load_clock)
…and that’s it! I slashed a ton of unreadable muck from my ancient script (see the full new version) and inherited all the shiny new global clock goodies. hope this helps y’all contextualize / approach yours a bit more easily 