/*
   MODDER: @RED9030
*/
/*
   ESP32 version
   Ténica de multiplexación con ESP (CHARLIEPLEXING)
*/
/*
        A       B     C
   LED	PIN26	 PIN25  PIN14
   1	  LOW	   HIGH	  INPUT
   2	  HIGH	 LOW	  INPUT
   3	  INPUT  LOW	  HIGH
   4	  INPUT  HIGH	  LOW
   5	  LOW	   INPUT  HIGH
   6	  HIGH	 INPUT  LOW

   Pins ESP32 34,35,36,39 no output
*/
/*
 *****************************************************
      LIBRERIAS
 *****************************************************
*/


/*
 *****************************************************
      VARIABLES
 *****************************************************
*/
const int resolution = 12;  //Cambiar resolución depende del dispositivo en esp32 son 12bits y esp8266 son 10bits
const int leds_numbers = 6; //Números de leds a colocar para multiplexar de la ecuacion #leds=#pines(#pines-1).
int oldValue = LOW; // default/idle value for pin 8 is low.

// Definimos los pines a utilizar
//ESP32 PINS plexing 12bits resolution
const int pin_botton = 33; // Pin ADC con entrada del potenciometro ESP32
#define PIN_A 12      // Pin A 
#define PIN_B 14      // Pin B
#define PIN_C 13      // Pin C

//ESP8266 PINS plexing 10bits resolution
//const int pin_pot = A0; //Pin ADC con entrada del potenciometro ESP8266 00-A0 (lectura de analógico)
//#define PIN_A 4     // Pin A D2 
//#define PIN_B 5     // Pin B D1
//#define PIN_C 0     // Pin C D0

//ESTRUCTURA DE LOS DATOS
typedef struct datos {
  //uint16_t button1;
  uint16_t button1,button2,button3,button4;
} datos;

// Create a struct_message called myData
datos myData;

/*
 *****************************************************
      INICIO
 *****************************************************
*/
void setup() {
  // Inicialización del puerto serial
  Serial.begin(115200);
  pinMode(pin_botton, INPUT);
}

/*
 *****************************************************
      REPETICIÓN
 *****************************************************
*/
void loop() {

  myData.button1= digitalRead(pin_botton);
  //Ctrl_botones(myData.button1);
  if(myData.button1 != oldValue)
  {
    Serial.println(myData.button1);
    if(myData.button1 == HIGH)
    {
      Serial.println("The button is pressed.");
    }
    else
    {
      Serial.println("The button is released.");
    }
    // Remember the value for the next time.
    oldValue = myData.button1;
  }
  delay(100);
}

/*
 *****************************************************
      FUNCIONES
 *****************************************************
*/

//BOTONES DE CONTROL
//void Ctrl_botones(int newValue){}


// Función que pondrá en los estados correctos para encender un LED (HIGH, LOW e INPUT)
void ponerEstados(int pinHigh, int pinLow, int pinInput)
{
  pinMode(pinHigh, OUTPUT);
  digitalWrite(pinHigh, HIGH);
  pinMode(pinLow, OUTPUT);
  digitalWrite(pinLow, LOW);
  pinMode(pinInput, INPUT);
}

// Esta función se va a encargar de aplicar la lógica dependiendo del LED que queramos encender
void encenderLed(int led_num)
{
  switch (led_num)
  {
    case 1:
      ponerEstados(PIN_B, PIN_A, PIN_C);
      break;
    case 2:
      ponerEstados(PIN_A, PIN_B, PIN_C);
      break;
    case 3:
      ponerEstados(PIN_C, PIN_B, PIN_A);
      break;
    case 4:
      ponerEstados(PIN_B, PIN_C, PIN_A);
      break;
    case 5:
      ponerEstados(PIN_C, PIN_A, PIN_B);
      break;
    case 6:
      ponerEstados(PIN_A, PIN_C, PIN_B);
      break;
  }

}