Help with Control Surface lib and banks

Hi! First time posting here :wink:

I have been looking for help by reading Control Surface library documentation for hours (maybe days) and I still can not find answer to may question.

Here is my goal :

  • I have a set of buttons. Each one sends a defined note on a defined channel.
  • I have to sets of buttons (up down) to scroll through 2 separate banks (channel and a transposer).
  • I want the notes takes account of both banks.

Quick code example :

Bank<16> channel(1);
Transposer<-1, +1> transposer(12);

IncrementDecrementSelector<channel.getNumberOfBanks()> channelSelector {
  channel,       // Bank to manage
  {25, 27},     // push button pins (increment, decrement)
  Wrap::Clamp, // Wrap around
};

IncrementDecrementSelector<transposer.getNumberOfBanks()> TransposerSelector {
  transposer,
  {7, 4},
  Wrap::Clamp, // Wrap around
};

// Instantiate an array of NoteButton objects
Bankable::NoteButton buttons[] {
  {channel, transposer, 33, {MIDI_Notes::C[4]}},// I know I cant do this, but how??
};

Hope you'll understand why i struggle... Thanks :slight_smile:

yeah, the constructor in the library expects only a single bankable object that manages which note or value is active depending on the selected bank.

As you cannot directly combine two independent banks this way, you need to create a custom wrapper (or a derived class) that calculates the effective note based on both the channel and the transposer banks, and then passes that result to the NoteButton.

May be something like this would work as C++ uses duck typing for templates (NoteButton is a template that works with any type that provides the methods it calls, typically get() and getNumberOfBanks() I think - so we would need to implement that )

template <typename Bank1, typename Bank2>
class CombinedBank {
  Bank1& bank1;
  Bank2& bank2;

public:
  CombinedBank(Bank1& b1, Bank2& b2) : bank1(b1), bank2(b2) {}

  uint8_t get() {
    return bank1.get() + bank2.get(); // <<<====  any formula combining both banks that suits your needs
  }

  uint8_t getNumberOfBanks() {
    return bank1.getNumberOfBanks() * bank2.getNumberOfBanks();
  }
};

Then you could possibly use it with NoteButton as we implemented the minimal “bankable” interface that NoteButton expects

CombinedBank channelTransposer(channel, transposer);

Bankable::NoteButton buttons[] {
  {channelTransposer, 33, {MIDI_Notes::C[4]}},
};

Here channelTransposer.get() returns the effective value considering both banks, and NoteButton sees it as a single bankable object.

You of course would need to adjust the combination formula in get() depending on how you want channel and transposer to affect the note. NoteButton will call get() each time it needs the current note, so the combined value will always reflect the current states of both banks

Totally untested... typed here

Many thanks for your help !

I'll try something similar.
In the meantime, someone gave me this example : Control Surface: Note-ManyAddresses-Transposer.ino
And there is this class in Control surface, but I dont understand if its a good way or how it coulb be used in my case : Control Surface: SingleAddressMultipleBanks< N > Class Template Reference

Alright !

Solution found :

BEGIN_CS_NAMESPACE

namespace Bankable {

/**
 * @brief   An abstract class for momentary push buttons that send MIDI events.
 *
 * The button is debounced.
 *
 * @see     Button
 */
template <class BankAddress, class Sender>
class DoubleBanksMIDIButton : public MIDIOutputElement {
  public:
    /**
     * @brief   Construct a new bankable MIDIButton.
     *
     * @param   channel_bankAddress
     *          The bankable MIDI address to send to.
     * @param   note_bankAddress
     *          The bankable MIDI address to send to.
     * @param   pin
     *          The digital input pin with the button connected.
     *          The internal pull-up resistor will be enabled.
     * @param   sender
     *          The MIDI sender to use.
     */
    DoubleBanksMIDIButton(BankAddress channel_bankAddress, BankAddress note_bankAddress, pin_t pin, const Sender &sender)
        : channel_address(channel_bankAddress), note_address(note_bankAddress), button(pin), sender(sender) {}

    void begin() override { button.begin(); }
    void update() override {
        Button::State state = button.update();
        if (state == Button::Falling) {
            channel_address.lock();
            note_address.lock();
            sender.sendOn(channel_address.getActiveAddress() + RelativeMIDIAddress(note_address.getActiveAddress().getAddress() - channel_address.getActiveAddress().getAddress()));
        } else if (state == Button::Rising) {
            sender.sendOff(channel_address.getActiveAddress() + RelativeMIDIAddress(note_address.getActiveAddress().getAddress() - channel_address.getActiveAddress().getAddress()));
            channel_address.unlock();
            note_address.unlock();
        }
    }

    /// @see @ref Button::invert()
    void invert() { button.invert(); }

    Button::State getButtonState() const { return button.getState(); }

  protected:
    BankAddress channel_address;
    BankAddress note_address;
    Button button;

  public:
    Sender sender;
};

/**
 * @brief   A class of MIDIOutputElement%s that read the input of a **momentary
 *          push button or switch**, and send out MIDI **Note** events.
 * 
 * A Note On event is sent when the button is pressed, and a Note Off
 * event is sent when the button is released.  
 * The button is debounced in software.  
 * This version can be banked.  
 *
 * @ingroup BankableMIDIOutputElements
 */
class DoubleBanksNoteButton : public DoubleBanksMIDIButton<SingleAddress, DigitalNoteSender> {
  public:
    /**
     * @brief   Create a new Bankable NoteButton object with the given pin, note
     *          number and channel.
     * 
     * @param   channel_config
     *          The bank configuration to use: the bank to add this element to,
     *          and whether to change the address, channel or cable number.
     * @param   note_config
     *          The bank configuration to use: the bank to add this element to,
     *          and whether to change the address, channel or cable number.
     * @param   pin
     *          The digital input pin to read from.  
     *          The internal pull-up resistor will be enabled.
     * @param   address
     *          The MIDI address containing the note number [0, 127], 
     *          channel [Channel_1, Channel_16], and optional cable number 
     *          [Cable_1, Cable_16].
     * @param   velocity
     *          The velocity of the MIDI Note events.
     */
    DoubleBanksNoteButton(OutputBankConfig<> channel_config, OutputBankConfig<> note_config, pin_t pin, MIDIAddress address,
               uint8_t velocity = 0x7F)
        : DoubleBanksMIDIButton<SingleAddress, DigitalNoteSender> {
              {channel_config, address},
              {note_config, address},
              pin,
              {velocity},
          } {}

    /// Set the velocity of the MIDI Note events.
    void setVelocity(uint8_t velocity) { this->sender.setVelocity(velocity); }
    /// Get the velocity of the MIDI Note events.
    uint8_t getVelocity() const { return this->sender.getVelocity(); }
};

} // namespace Bankable

END_CS_NAMESPACE


// Instantiate a MIDI over USB interface
USBMIDI_Interface midi;

uint8_t initChannel = 4;

Transposer<-2, +2> transposer(12);
 
// Instantiate a Selector to change the transposition
IncrementDecrementSelector<transposer.getNumberOfBanks()> transposerSelector {
  transposer,
  {34, 35},
  Wrap::Clamp, // Wrap around
};

Bank<16> channel(1);

// Instantiate a Bank selector to control which one of the four Banks is active.
IncrementDecrementSelector<channel.getNumberOfBanks()> channelSelector {
  channel,       // Bank to manage
  {25, 27},     // push button pins (increment, decrement)
  Wrap::Clamp, // Wrap around
};

Bankable::DoubleBanksNoteButton buttons[] {
  {{channel, BankType::ChangeChannel}, {transposer, BankType::ChangeAddress}, 33, {MIDI_Notes::C[4]}},
};

Just had to inherit few classes :wink:

Great ! Have fun