/* ============================================
  code is placed under the MIT license
  Copyright (c) 2024 J-M-L
  For the Arduino Forum : https://forum.arduino.cc/u/j-m-l

  Permission is hereby granted, free of charge, to any person obtaining a copy
  of this software and associated documentation files (the "Software"), to deal
  in the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  THE SOFTWARE.
  ===============================================
*/

#include <Toggle.h>

const byte brochesBoutons[] = {2, 3, 4, 5};
const byte nbBoutons = sizeof brochesBoutons / sizeof * brochesBoutons;
Toggle boutons[nbBoutons];

enum {REPOS, ACTIVATION} etatEntrees = REPOS;

void action(byte configuration) {
  Serial.print("ACTION\t");
  for (int i = nbBoutons - 1; i >= 0; --i) Serial.print(bitRead(configuration, i));
  Serial.println();
}

void gesionEntrees() {
  static unsigned long t0;
  static byte memoireEntrees = 0;

  switch (etatEntrees) {
    case REPOS:
      for (byte i = 0; i < nbBoutons; i++) {
        boutons[i].poll();
        if (boutons[i].onPress()) {
          Serial.print("Appui bouton "); Serial.println(i);
          t0 = millis();
          memoireEntrees = (1 << i);
          etatEntrees = ACTIVATION;
          break;
        }
      }
      break;

    case ACTIVATION:
      if (millis() - t0 >= 2000) {
        action(memoireEntrees);
        memoireEntrees = 0;
        etatEntrees = REPOS;
      } else {
        for (byte i = 0; i < nbBoutons; i++) {
          if (bitRead(memoireEntrees, i)) continue; // on ne teste pas deux fois
          boutons[i].poll();
          if (boutons[i].onPress()) {
            Serial.print("Appui bouton "); Serial.println(i);
            t0 = millis();
            memoireEntrees |= (1 << i);
            if (memoireEntrees == ((1 << nbBoutons) - 1)) { // si tous les boutons sont choisis
              action(memoireEntrees);
              memoireEntrees = 0;
              etatEntrees = REPOS;
            }
            break;
          }
        }
      }
      break;
  }
}

void setup() {
  for (byte i = 0; i < nbBoutons; i++) boutons[i].begin(brochesBoutons[i]);
  Serial.begin(115200);
}

void loop() {
  gesionEntrees();
}
B0
B1
B2
B3