tttapa / Control-Surface

Arduino library for creating MIDI controllers and other MIDI devices.
GNU General Public License v3.0
1.2k stars 136 forks source link

NoteButton and IncrementSelector on the same button #238

Open zeysh opened 4 years ago

zeysh commented 4 years ago

Hi Pieter,

I hope you well.

I have a strange behavior with my code bellow.

I want to have a button, which on the first press PLAY my DAW and the second press STOP.

I reused a code(https://github.com/tttapa/Control-Surface/issues/220) you gave to me to toggle functions. It seems to work well, but sometimes, the bank doesn't switch.

I just declared the same PIN to change (IncrementSelector) the bank and the NoteButton (Bankable::ManyAddresses::NoteButton). I guess that's why I got this glitch...

include // Include the Control Surface library

USBMIDI_Interface midi;

// Create a Bank object to select one of two modes (PLAY, STOP) Bank<2> bank;

// Create a Selector object that reads a push button connected to pin 4 // to select which mode is active. IncrementSelector<2> pushbutton = {bank, 4};

Bankable::ManyAddresses::NoteButton<2> button = { bank, // bank selects active address 4, // push button pin {{ // addresses MCU::PLAY, MCU::STOP, }}, };

// --------------------------------- Setup ---------------------------------- //

void setup() { Serial.begin(115200); Control_Surface.begin(); // Initialize Control Surface }

// ---------------------------------- Loop ---------------------------------- //

void loop() { Control_Surface.loop(); // Refresh all elements static setting_t prevSetting = -1; setting_t setting = bank.getSelection(); if (setting != prevSetting) { const char *mode; switch (setting) { case 0: mode = "PLAY"; break; case 1: mode = "STOP"; break; default: mode = ""; } Serial.println(mode); prevSetting = setting; } }

Midi trace
55859327         NoteOff chn  1 5d 7f
55863034          NoteOn chn  1 5d 7f
55865784         NoteOff chn  1 5d 7f
55869129          NoteOn chn  1 5d 7f
55871736         NoteOff chn  1 5d 7f
55874905          NoteOn chn  1 5e 7f
55877703         NoteOff chn  1 5e 7f
55881170          NoteOn chn  1 5d 7f
55883668         NoteOff chn  1 5d 7f
55886838          NoteOn chn  1 5d 7f
55889193         NoteOff chn  1 5d 7f
55892530          NoteOn chn  1 5d 7f
55895726         NoteOff chn  1 5d 7f
55899065          NoteOn chn  1 5d 7f
55909605         NoteOff chn  1 5d 7f
55911895          NoteOn chn  1 5d 7f
55913277         NoteOff chn  1 5d 7f
55921960          NoteOn chn  1 5e 7f
55928154         NoteOff chn  1 5e 7f
55953352          NoteOn chn  1 5d 7f
55958449         NoteOff chn  1 5d 7f
55978653          NoteOn chn  1 5e 7f
55983070         NoteOff chn  1 5e 7f
56016622          NoteOn chn  1 5d 7f
56020641         NoteOff chn  1 5d 7f
56044117          NoteOn chn  1 5e 7f
56049212         NoteOff chn  1 5e 7f
56079252          NoteOn chn  1 5d 7f
56084880         NoteOff chn  1 5d 7f
56107262          NoteOn chn  1 5d 7f
56112509         NoteOff chn  1 5d 7f
56276025          NoteOn chn  1 5d 7f
56281875         NoteOff chn  1 5d 7f
56460381          NoteOn chn  1 5e 7f
56464416         NoteOff chn  1 5e 7f


Another thing I want to add is a LongPress function on this button (RECORD). I see this code https://github.com/tttapa/Control-Surface/issues/87 but I don't really understand how I can add this to my code.
Do you have a complete example ?

Many thanks
tttapa commented 4 years ago

The problem is that you don't know when the button state changes. Basically, the code that loops is:

read input for selector
read input for note button
other stuff
repeat

If the state of the button changes between updating the selector and updating the note button, the note will be sent without changing the address.

The only solution is to write your own code that reads the input once, for both the selector and the button at the same time.

You'll have to do this anyway if you want to do more advanced button presses such as long presses etc.

You don't need to use a selector to change the bank, you can call bank.select(setting) directly, see https://tttapa.github.io/Control-Surface-doc/Doxygen/db/dbd/classBank.html#a801ab5dedea8200f1cdb26533cb6f78b