tttapa / Control-Surface

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

Button combination at the setup stage and selection saved #848

Closed gringow831 closed 1 year ago

gringow831 commented 1 year ago

At the moment to enable and disable the buttons I use a dipswitch but I am trying to modify this to use button combinations. I managed with one button at a time at the setup stage since I want to have this selected and saved. Is there a way to use a combination of 2 buttons to change that mode? (enable or disable the first2 or second2?

The second part is I have been trying to modify my previous projects to save that selection on the EEPROM. The address that has been enabled or disabled since there will be 5 different "modes" I want to keep the last action rather than every time the controller gets connected needing to select the mode I'd like to work on.

#include <Control_Surface.h>
#include <EEPROM.h>

USBMIDI_Interface midi;

 NoteButton first2[] = {
  {5, MCU::SELECT_1},
  {7, MCU::SELECT_2},
 };

 NoteButton second2[] = {
  {5, MCU::SELECT_3},
  {7, MCU::SELECT_4},
};

Button btn5 {5}; 
Button btn7 {7};

uint8_t savedSetting; //for the first3
uint8_t savedSetting_1; //for the second3

void setup() {

  RelativeCCSender::setMode(MACKIE_CONTROL_RELATIVE);
  Control_Surface.begin();
  btn5.begin();
  btn7.begin();

NoteButton::disable(second2); //disable the second3 NoteButton

//if pressed while boot enable the first3 button (IT BREAKS THE CODE IF THE FIRST2 ARE ALREADY SELECTED BUT NECESARY IF SAVED SECOND2 TO GO BACK)
if (btn5.update()) {
  if (btn5.getState()==Button::Released){
    }
  if (btn5.getState()==Button::Falling){
  NoteButton::enable(first2);
  NoteButton::disable(second2);
    }
}

//enable second3 and disable first3 NoteButtons
if (btn7.update()) {
    if (btn7.getState()==Button::Released){
    }
   if (btn7.getState()==Button::Falling){
  NoteButton::disable(first2);
  NoteButton::enable(second2);

    }
}

savedSetting = EEPROM.read(0);
  if (savedSetting < btn5.update())
    first2.getAddress (savedSetting);
  else
    savedSetting = btn5.getState();

  savedSetting = EEPROM.read(1);
  if (savedSetting_1 < btn7.update())
    second2.MIDIaddress address(savedSetting_1);
  else
    savedSetting_1 = btn7.getState();

}

void loop() {
  Control_Surface.loop();

  //RECALLING SETTINGS DISABLED OR ENABLED
  auto currSetting = btn5.update();
  if (savedSetting != currSetting) {
    EEPROM.write(0, currSetting);
    savedSetting = currSetting;
  }

  auto currSetting_1 = btn7.update();
  if (savedSetting_1 != currSetting_1) {
    EEPROM.write(1, currSetting_1);
    savedSetting_1 = currSetting_1;
  }

  }
gringow831 commented 1 year ago

Is IsEnabled() the way to go in order to save the disable enable to EEPROM? I can't seem to make it work

void setup() {
  savedSetting = EEPROM.read(0);
  if (savedSetting != first2[2].isEnabled())
  {    }
  else 
    savedSetting = first2[2].isEnabled();

  savedSetting_1 = EEPROM.read(1);
  if (savedSetting_1 != second2[2].isEnabled())
  {    }
  else 
    savedSetting_1 = second2[2].isEnabled();

NoteButton::disable(second2); //disable the second2 NoteButton

//if pressed while boot enable the first3 button (IT BREAKS THE CODE IF THE FIRST2 ARE ALREADY SELECTED BUT NECESARY IF SAVED SECOND2 TO GO BACK)
if (btn5.update()) {
  if (btn5.getState()==Button::Released){
    }
  if (btn5.getState()==Button::Falling){
  NoteButton::enable(first2);
  NoteButton::disable(second2);
    }}

//enable second3 and disable first3 NoteButtons
if (btn7.update()) {
    if (btn7.getState()==Button::Released){
    }
   if (btn7.getState()==Button::Falling){
  NoteButton::disable(first2);
  NoteButton::enable(second2);
     }
}
}

void loop() {
  Control_Surface.loop();

  auto currSetting = first2[2].isEnabled();
  if (savedSetting != currSetting) {
    EEPROM.write(0, currSetting);
    savedSetting = currSetting;
  }

  auto currSetting_1 = second2[2].isEnabled();
  if (savedSetting_1 != currSetting_1) {
    EEPROM.write(1, currSetting_1);
    savedSetting_1 = currSetting_1;
  }
  }
gringow831 commented 1 year ago

Is it possible to save the settings and reload them if the settings were modified at the setup? In my example I have managed to change which "mode" is being used at the setup stage but I would like that to reload when reconnecting rather than every time needing to follow the process manually

I can't seem to find the solution to this problem, I tried many ways and wouldn't want to break the teensy by adding EEPROM.write inside the Button state at the setup stage just before reading the memory again.