// Include the libraries needed for the project
#include <ESP8266WiFi.h>
#include <espnow.h>

// Define the Pins for the LED and The Quizz push button
#define Led_Pin 0
#define Button_Pin 2

// Write the MAC address of your ESP01
uint8_t broadcastAddress[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };

// Create the variable which will hold the state of the system
int state = 0;

// Define variables to store button identifier to be sent
// Red button = 1
// Blue button = 2
// Green button = 3
// Orange button = 4
int buttonNumber = 4;

// Define variables to store incoming data
int incomingState;

// Variable to store if sending data was successful
String success;


//Structure to send data
//Must match the receiver structure
typedef struct struct_message {
  int value;
} struct_message;


// Create a struct_message for send data
struct_message ValueSent;
// Create a struct_message to hold incoming data
struct_message incomingValue;


// ESPNOW function called when you want to send data
void OnDataSent(uint8_t *mac_addr, uint8_t sendStatus) {
  Serial.print("Last Packet Send Status: ");
  if (sendStatus == 0) {
    Serial.println("Delivery success");
  } else {
    Serial.println("Delivery fail");
  }
}

// ESPNOW function called when you receive data
void OnDataRecv(uint8_t *mac, uint8_t *incomingData, uint8_t len) {
  memcpy(&incomingValue, incomingData, sizeof(incomingValue));
  // Debug print
  /*
  Serial.print("Bytes received: ");
  Serial.println(len);
  */
  // Take the incoming data and copy it to it variable holder
  incomingState = incomingValue.value;
  state = incomingState;
}


// Function used to make the led blink x number of time
void blink(int i) {
  while (i > 0) {
    digitalWrite(Led_Pin, HIGH);  // turn the LED on (HIGH is the voltage level)
    //Serial.println("on"); //debug
    delay(250);                  // wait for a second
    digitalWrite(Led_Pin, LOW);  // turn the LED off by making the voltage LOW
    //Serial.println("off");
    delay(250);
    //Serial.println(i);
    --i;
    //Serial.println(i);
    //Serial.println(state);
  }
}


void setup() {
  // Init Serial Monitor
  Serial.begin(115200);

  // Init the led pin and the button pin
  pinMode(Button_Pin, INPUT_PULLUP);
  pinMode(Led_Pin, OUTPUT);

  // Set device as a Wi-Fi Station
  WiFi.mode(WIFI_STA);
  WiFi.disconnect();

  // Init ESP-NOW
  if (esp_now_init() != 0) {
    Serial.println("Error initializing ESP-NOW");
    return;
  }

  // Set ESP-NOW Role
  esp_now_set_self_role(ESP_NOW_ROLE_COMBO);

  // Once ESPNow is successfully Init, we will register for Send CB to
  // get the status of Trasnmitted packet
  esp_now_register_send_cb(OnDataSent);

  // Register peer
  esp_now_add_peer(broadcastAddress, ESP_NOW_ROLE_COMBO, 1, NULL, 0);

  // Register for a callback function that will be called when data is received
  esp_now_register_recv_cb(OnDataRecv);

  //Call blink after first setup
  blink(4);
  state = 0;
}



void loop() {
  //Check for the state(current step) and perform the required action
  //State 0 :Wait for Quizz button to be pressed, when pressed send your board number to the master
  //State 1 :Blink 5x and then change to State 0
  //State 2 :Blink 1x then change to State 4
  //State 3 :Turn OFF led
  //State 4 :Turn ON led
  if (state == 0) {
    if (digitalRead(Button_Pin) == 0) {
      ValueSent.value = buttonNumber;
      // Send message via ESP-NOW
      esp_now_send(broadcastAddress, (uint8_t *)&ValueSent, sizeof(ValueSent));
      // debouncing delay
      delay(500);
    }
  }
  if (state == 1) {
    blink(5);
    state = 0;
  }
  if (state == 2) {
    blink(1);
    state = 4;
  }
  if (state == 3) {
    digitalWrite(Led_Pin, LOW);
  }
  if (state == 4) {
    digitalWrite(Led_Pin, HIGH);
  }
}
