Skip to content

Commit

Permalink
add dec and inc trigger modes to MIDI Buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
soyersoyer committed Nov 28, 2024
1 parent a90eb95 commit 2609c55
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 7 deletions.
4 changes: 2 additions & 2 deletions src/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ class CConfig // Configuration for MiniDexed
unsigned GetMIDIButtonSelect (void) const;
unsigned GetMIDIButtonHome (void) const;

// Action type for Midi buttons: "click", "doubleclick", "longpress", ""
// Action type for Midi buttons: "click", "doubleclick", "longpress", "dec", "inc", ""
const char *GetMIDIButtonActionPrev (void) const;
const char *GetMIDIButtonActionNext (void) const;
const char *GetMIDIButtonActionBack (void) const;
Expand All @@ -232,7 +232,7 @@ class CConfig // Configuration for MiniDexed
unsigned GetMIDIButtonTGUp (void) const;
unsigned GetMIDIButtonTGDown (void) const;

// Action type for buttons: "click", "doubleclick", "longpress", ""
// Action type for buttons: "click", "doubleclick", "longpress", "dec", "inc", ""
const char *GetMIDIButtonActionPgmUp (void) const;
const char *GetMIDIButtonActionPgmDown (void) const;
const char *GetMIDIButtonActionBankUp (void) const;
Expand Down
9 changes: 9 additions & 0 deletions src/midipin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ unsigned CMIDIPin::Read (void)
return m_nValue;
}

unsigned CMIDIPin::ReadRaw (void)
{
return m_nRawValue;
}

void CMIDIPin::Write (unsigned nValue)
{
// Takes values in the MIDI controller range 0 to 127
Expand All @@ -50,6 +55,10 @@ void CMIDIPin::Write (unsigned nValue)
// "off"
m_nValue = HIGH;
}

// Save the raw value for INC and DEC
m_nRawValue = nValue;

return;
}

6 changes: 5 additions & 1 deletion src/midipin.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,18 @@ class CMIDIPin
// Should be treated as a PULLED UP IO pin
// i.e. treated as "active low" (LOW) when pressed.
unsigned Read (void);


// returns the raw CC value
unsigned ReadRaw (void);

// MIDI CC values >=64 will set the MIDI pin to LOW ("on")
// MIDI CC values <= 63 will set the MIDI pin to HIGH ("off")
void Write (unsigned nValue);

private:
unsigned m_nPinNumber;
unsigned m_nValue;
unsigned m_nRawValue;
};

#endif
4 changes: 3 additions & 1 deletion src/minidexed.ini
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,9 @@ LongPressTimeout=400

# MIDI Button Navigation
# Specify MIDI CC to act as a button (0 = ununsed, so don't use CC 0)
# NB: Off < 64 < ON
# NB: Off < 64 < ON for longpress / click / doubleclick actions
# 13-15, 61-63, 125-127 for dec action
# 1-3, 17-19, 65-67 for inc action
# CC channel: 0=OFF; 1-16 MIDI Ch; >16 Omni
# If MIDIButtonNotes>0 then treat MIDIButton numbers as MIDI
# Note numbers, triggered with NoteOn/NoteOff, not CC numbers.
Expand Down
49 changes: 48 additions & 1 deletion src/uibuttons.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ CUIButton::CUIButton (void)
m_clickEvent(BtnEventNone),
m_doubleClickEvent(BtnEventNone),
m_longPressEvent(BtnEventNone),
m_decEvent(BtnEventNone),
m_incEvent(BtnEventNone),
m_doubleClickTimeout(0),
m_longPressTimeout(0)
{
Expand Down Expand Up @@ -101,6 +103,16 @@ void CUIButton::setLongPressEvent(BtnEvent longPressEvent)
m_longPressEvent = longPressEvent;
}

void CUIButton::setDecEvent(BtnEvent decEvent)
{
m_decEvent = decEvent;
}

void CUIButton::setIncEvent(BtnEvent incEvent)
{
m_incEvent = incEvent;
}

unsigned CUIButton::getPinNumber(void)
{
return m_pinNumber;
Expand All @@ -109,6 +121,7 @@ unsigned CUIButton::getPinNumber(void)
CUIButton::BtnTrigger CUIButton::ReadTrigger (void)
{
unsigned value;
unsigned raw = 0;
if (isMidiPin(m_pinNumber))
{
if (!m_midipin)
Expand All @@ -117,6 +130,7 @@ CUIButton::BtnTrigger CUIButton::ReadTrigger (void)
return BtnTriggerNone;
}
value = m_midipin->Read();
raw = m_midipin->ReadRaw();
}
else
{
Expand All @@ -128,6 +142,21 @@ CUIButton::BtnTrigger CUIButton::ReadTrigger (void)
value = m_pin->Read();
}

if (m_decEvent && (13 <= raw && raw <= 15 || 61 <= raw && raw <= 63 || 125 <= raw && raw <= 127))
{
reset();
// reset value to trigger only once
m_midipin->Write(0);
return BtnTriggerDec;
}
else if (m_incEvent && (1 <= raw && raw <= 3 || 17 <= raw && raw <= 19 || 65 <= raw && raw <= 67))
{
reset();
// reset value to trigger only once
m_midipin->Write(0);
return BtnTriggerInc;
}

if (m_timer < m_longPressTimeout) {
m_timer++;

Expand Down Expand Up @@ -230,6 +259,12 @@ CUIButton::BtnEvent CUIButton::Read (void) {
else if (trigger == BtnTriggerLongPress) {
return m_longPressEvent;
}
else if (trigger == BtnTriggerDec) {
return m_decEvent;
}
else if (trigger == BtnTriggerInc) {
return m_incEvent;
}

assert (trigger == BtnTriggerNone);

Expand All @@ -250,6 +285,12 @@ CUIButton::BtnTrigger CUIButton::triggerTypeFromString(const char* triggerString
else if (strcmp(triggerString, "longpress") == 0) {
return BtnTriggerLongPress;
}
else if (strcmp(triggerString, "dec") == 0) {
return BtnTriggerDec;
}
else if (strcmp(triggerString, "inc") == 0) {
return BtnTriggerInc;
}

LOGERR("Invalid action: %s", triggerString);

Expand Down Expand Up @@ -339,7 +380,7 @@ boolean CUIButtons::Initialize (void)

// Each normal button can be assigned up to 3 actions: click, doubleclick and
// longpress. We may not initialise all of the buttons.
// MIDI Buttons can be assigned to click, doubleclick, longpress
// MIDI buttons can be assigned to click, doubleclick, longpress, dec ,inc
unsigned pins[MAX_BUTTONS] = {
m_prevPin, m_nextPin, m_backPin, m_selectPin, m_homePin, m_pgmUpPin, m_pgmDownPin, m_BankUpPin, m_BankDownPin, m_TGUpPin, m_TGDownPin,
m_prevMidi, m_nextMidi, m_backMidi, m_selectMidi, m_homeMidi, m_pgmUpMidi, m_pgmDownMidi, m_BankUpMidi, m_BankDownMidi, m_TGUpMidi, m_TGDownMidi
Expand Down Expand Up @@ -450,6 +491,12 @@ void CUIButtons::bindButton(unsigned pinNumber, CUIButton::BtnTrigger trigger, C
else if (trigger == CUIButton::BtnTriggerLongPress) {
m_buttons[i].setLongPressEvent(event);
}
else if (trigger == CUIButton::BtnTriggerDec) {
m_buttons[i].setDecEvent(event);
}
else if (trigger == CUIButton::BtnTriggerInc) {
m_buttons[i].setIncEvent(event);
}
else {
assert (trigger == CUIButton::BtnTriggerNone);
}
Expand Down
12 changes: 10 additions & 2 deletions src/uibuttons.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ class CUIButton
BtnTriggerNone = 0,
BtnTriggerClick = 1,
BtnTriggerDoubleClick = 2,
BtnTriggerLongPress = 3
BtnTriggerLongPress = 3,
BtnTriggerDec = 4,
BtnTriggerInc = 5,
};

enum BtnEvent
Expand Down Expand Up @@ -70,6 +72,8 @@ class CUIButton
void setClickEvent(BtnEvent clickEvent);
void setDoubleClickEvent(BtnEvent doubleClickEvent);
void setLongPressEvent(BtnEvent longPressEvent);
void setDecEvent(BtnEvent decEvent);
void setIncEvent(BtnEvent incEvent);

unsigned getPinNumber(void);

Expand Down Expand Up @@ -100,7 +104,11 @@ class CUIButton
BtnEvent m_doubleClickEvent;
// Event to fire on long press
BtnEvent m_longPressEvent;

// Event to fire on dec
BtnEvent m_decEvent;
// Event to fire on inc
BtnEvent m_incEvent;

// Timeout for double click in tenths of a millisecond
unsigned m_doubleClickTimeout;
// Timeout for long press in tenths of a millisecond
Expand Down

0 comments on commit 2609c55

Please sign in to comment.