#include "magicSwitchBoard.h" #include "Arduino.h" #include "buttons.h" #define CHANNELS 3 typedef enum { idle, learn, active, last } states; states state = last; uint8_t sequence[CHANNELS] = {0, 0, 0}; const uint8_t buttonIndex[CHANNELS] = {4, 5, 3}; const uint32_t leds[CHANNELS] = {LED1, LED2, LED3}; uint8_t learnIndex = 0; void showLeds(void) { //loop through the button list for (int i = 0; i < CHANNELS; i++) { //check if the position is already programmed if (sequence[i]) { //get the button pointer buttons *currentbutton = getButton(buttonIndex[i]); //verify that the button pointer is not NULL if (currentbutton == NULL) { return; } //if the button is pressed, show LED or not if (currentbutton->state() == !RELEASED) { //write sequence led on digitalWrite(leds[sequence[i] + 1], 1); } else { //write sequence led off digitalWrite(leds[sequence[i] + 1], 0); } } } } void handleMagicSwitchBoard(void) { switch (state) { case idle: { if (anybutton()) { state = learn; } else { for (auto &&i : leds) { digitalWrite(i, 0); } } } break; case learn: { for (int i = 0; i < CHANNELS; i++) { buttons *currentbutton = getButton(buttonIndex[i]); if (currentbutton == NULL) { return; } if (currentbutton->state() == !RELEASED) { bool duplicate = false; for (auto &&n : sequence) { if (currentbutton->index() == n) { duplicate = true; } } if (!duplicate) { sequence[learnIndex] = currentbutton->index(); learnIndex++; } } } if (learnIndex == CHANNELS) { state = active; } } break; case active: { } break; default: { state = idle; } break; } showLeds(); }