Skip to content

Commit

Permalink
handle double pressed for launchpad
Browse files Browse the repository at this point in the history
  • Loading branch information
westlicht committed Oct 23, 2020
1 parent 20ecc8c commit 4c6f291
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "LaunchpadController.h"

#include "core/Debug.h"
#include "os/os.h"

#define CALL_MODE_FUNCTION(_mode_, _function_, ...) \
switch (_mode_) { \
Expand Down Expand Up @@ -864,7 +865,6 @@ void LaunchpadController::setSceneLed(int col, Color color) {
//----------------------------------------

void LaunchpadController::dispatchButtonEvent(const Button& button, ButtonAction action) {

if (globalButton(button, action)) {
return;
}
Expand All @@ -873,7 +873,24 @@ void LaunchpadController::dispatchButtonEvent(const Button& button, ButtonAction
}

void LaunchpadController::buttonDown(int row, int col) {
dispatchButtonEvent(Button(row, col), ButtonAction::Down);
Button button(row, col);

dispatchButtonEvent(button, ButtonAction::Down);

uint32_t currentTicks = os::ticks();
uint32_t deltaTicks = currentTicks - _buttonTracker.lastTicks;

if (button != _buttonTracker.lastButton || deltaTicks > os::time::ms(300)) {
_buttonTracker.count = 1;
} else {
++_buttonTracker.count;
}

_buttonTracker.lastButton = button;
_buttonTracker.lastTicks = currentTicks;

if (_buttonTracker.count == 1) dispatchButtonEvent(button, ButtonAction::Press);
if (_buttonTracker.count == 2) dispatchButtonEvent(button, ButtonAction::DoublePress);
}

void LaunchpadController::buttonUp(int row, int col) {
Expand Down
23 changes: 20 additions & 3 deletions src/apps/sequencer/ui/controllers/launchpad/LaunchpadController.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,20 @@ class LaunchpadController : public Controller {
inline Color colorYellow(int brightness = 3) const { return Color(brightness, brightness); }

struct Button {
int row;
int col;
int row = -1;
int col = -1;

Button() = default;
Button(int row, int col) : row(row), col(col) {}

bool operator==(const Button &other) const {
return row == other.row && col == other.col;
}

bool operator!=(const Button &other) const {
return !(*this == other);
}

template<typename T>
bool is() const {
return row == T::row && col == T::col;
Expand All @@ -51,7 +60,9 @@ class LaunchpadController : public Controller {

enum class ButtonAction {
Down,
Up
Up,
Press,
DoublePress
};

struct Navigation {
Expand Down Expand Up @@ -158,6 +169,12 @@ class LaunchpadController : public Controller {
return buttonState(T::row, T::col);
}

struct {
Button lastButton;
uint32_t lastTicks = 0;
uint8_t count = 1;
} _buttonTracker;

Project &_project;
Container<LaunchpadDevice, LaunchpadMk2Device, LaunchpadProDevice> _deviceContainer;
LaunchpadDevice *_device;
Expand Down

0 comments on commit 4c6f291

Please sign in to comment.