From 81bac41d5c79194154371915ab9401d5dc1689e7 Mon Sep 17 00:00:00 2001 From: Gergo Koteles Date: Tue, 26 Nov 2024 00:43:30 +0100 Subject: [PATCH] add dec and inc trigger modes to MIDI Buttons --- src/config.h | 4 ++-- src/midipin.cpp | 12 +++++++++++- src/midipin.h | 8 +++++++- src/uibuttons.cpp | 45 ++++++++++++++++++++++++++++++++++++++++++++- src/uibuttons.h | 12 ++++++++++-- 5 files changed, 74 insertions(+), 7 deletions(-) diff --git a/src/config.h b/src/config.h index f7da3efc..97681d48 100644 --- a/src/config.h +++ b/src/config.h @@ -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; @@ -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; diff --git a/src/midipin.cpp b/src/midipin.cpp index d03d73aa..b63b79b4 100644 --- a/src/midipin.cpp +++ b/src/midipin.cpp @@ -25,7 +25,8 @@ LOGMODULE ("midipin"); CMIDIPin::CMIDIPin (unsigned nPinNumber) : m_nPinNumber (nPinNumber), - m_nValue (HIGH) + m_nValue (HIGH), + m_nRawValue (MIDIPIN_CENTER) { } @@ -38,6 +39,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 @@ -50,6 +56,10 @@ void CMIDIPin::Write (unsigned nValue) // "off" m_nValue = HIGH; } + + // Save the raw value for INC and DEC + m_nRawValue = nValue; + return; } diff --git a/src/midipin.h b/src/midipin.h index 7fa3f1db..0764966d 100644 --- a/src/midipin.h +++ b/src/midipin.h @@ -32,6 +32,8 @@ #define MidiPinToCC(p) (((p)>=MIDI_PINS)?((p)-MIDI_PINS):0) #define isMidiPin(p) (((p)>=MIDI_PINS)?1:0) +#define MIDIPIN_CENTER 64 + class CMIDIPin { public: @@ -42,7 +44,10 @@ 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); @@ -50,6 +55,7 @@ class CMIDIPin private: unsigned m_nPinNumber; unsigned m_nValue; + unsigned m_nRawValue; }; #endif diff --git a/src/uibuttons.cpp b/src/uibuttons.cpp index 3df7f4ba..9dd975b1 100644 --- a/src/uibuttons.cpp +++ b/src/uibuttons.cpp @@ -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) { @@ -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; @@ -109,6 +121,7 @@ unsigned CUIButton::getPinNumber(void) CUIButton::BtnTrigger CUIButton::ReadTrigger (void) { unsigned value; + if (isMidiPin(m_pinNumber)) { if (!m_midipin) @@ -117,6 +130,18 @@ CUIButton::BtnTrigger CUIButton::ReadTrigger (void) return BtnTriggerNone; } value = m_midipin->Read(); + + if (m_decEvent || m_incEvent) + { + unsigned raw = m_midipin->ReadRaw(); + + if (raw == MIDIPIN_CENTER) + return BtnTriggerNone; + + // reset value to trigger only once + m_midipin->Write(MIDIPIN_CENTER); + return raw < MIDIPIN_CENTER ? BtnTriggerDec : BtnTriggerInc; + } } else { @@ -230,6 +255,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); @@ -250,6 +281,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); @@ -339,7 +376,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 @@ -450,6 +487,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); } diff --git a/src/uibuttons.h b/src/uibuttons.h index 76febddf..16f7b626 100644 --- a/src/uibuttons.h +++ b/src/uibuttons.h @@ -41,7 +41,9 @@ class CUIButton BtnTriggerNone = 0, BtnTriggerClick = 1, BtnTriggerDoubleClick = 2, - BtnTriggerLongPress = 3 + BtnTriggerLongPress = 3, + BtnTriggerDec = 4, + BtnTriggerInc = 5, }; enum BtnEvent @@ -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); @@ -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