Platform: Playdate simulator running on Windows 10; SDK release 2.0.3
I am just starting out with Playdate development and I'm trying to add crank support to the DrumMachine example. I want it so that when the crank is turned the playhead scrubs forwards or backwards through the sequence. I have added this code to the example (along with import "CoreLibs/crank"
):
function playdate.cranked()
local crank_tick = playdate.getCrankTicks(sequence:getLength())
if crank_tick ~= 0 then
local new_step = sequence:getCurrentStep() + crank_tick
-- wrap when hitting the ends
if new_step > sequence:getLength() then
new_step = 1
elseif new_step < 1 then
new_step = sequence:getLength()
end
sequence:goToStep(new_step, true)
end
end
The code is meant to move the sequence's step based on the getCrankTicks
function. However, it exhibits strange behaviour: When turning the crank forwards, the playhead stays in the same spot (unless it is turned fast enough to jump 2 at once). When turning it backwards, the playhead does move backwards, but in steps of 2, rather than 1. At this point, it sounds like there is a -1 offset from the specified location, and so adding 1 should fix it (line 5 of the function):
local new_step = sequence:getCurrentStep() + crank_tick + 1
However, that just causes the problem in the other direction; now it jumps forwards 2 steps and won't move backwards. If I multiply crank_tick
by 2, then it always jumps the expected 2 steps going both directions. Going to further multiplication levels leads to the following observation:
- When the sequence is stepped by an odd number in either direction, there seems to be a -1 offset.
- When it is stepped by an even number in either direction, it works as expected (stepping by that amount).
(Side note: Is the source code for the API publicly available anywhere? I think looking at the implementation could save a lot of guesswork on issues like these.)