#include <Adafruit_NeoPixel.h>

#include <SPI.h> // SPI
#include <MFRC522.h> // RFID




#define SS_PIN 10
#define RST_PIN 9
    
// Déclaration 
MFRC522 rfid(SS_PIN, RST_PIN); 

// Tableau contentent l'ID
byte nuidPICC[4];
byte Code_Acces[4]={0xB9, 0x9B, 0x13, 0xBA}; 
byte CodeVerif=0;

#define BUTTON_PIN   2
#define PIXEL_PIN    6  // Digital IO pin connected to the NeoPixels.
#define PIXEL_COUNT 16  // Number of NeoPixels

// Declare our NeoPixel strip object:
Adafruit_NeoPixel strip(PIXEL_COUNT, PIXEL_PIN, NEO_GRB + NEO_KHZ800);

void setup() {
  pinMode(BUTTON_PIN, INPUT_PULLUP);

  strip.begin(); // Initialize NeoPixel strip object (REQUIRED)
  strip.show();  // Initialize all pixels to 'off'


    // Init RS232
  Serial.begin(9600);

  // Init SPI bus
  SPI.begin(); 

  // Init MFRC522 
  rfid.PCD_Init(); 
}

void loop() {

  strip.setBrightness(20);
  //Allume_En_Decalage(strip.Color(0,0,255),100);


  // Initialisé la boucle si aucun badge n'est présent 
  if ( !rfid.PICC_IsNewCardPresent()){
     Allume_Tout(strip.Color( 255,0,0));
     delay(1000);
     return;
  }


  // Vérifier la présence d'un nouveau badge 
  if ( !rfid.PICC_ReadCardSerial()){
     Allume_Tout(strip.Color( 0,255,0));
     delay(1000);
     return;
  }

   

  Allume_En_Decalage(strip.Color(0,0,255),100);

  // Enregistrer l'ID du badge (4 octets) 
  for (byte i = 0; i < 4; i++) 
  {
    nuidPICC[i] = rfid.uid.uidByte[i];
  }


  CodeVerif = GetAccesState(Code_Acces,nuidPICC);
  if (CodeVerif==1)
  {
     Allume_Tout(strip.Color( 0,255,0));
     delay(10000);
  }

  
  // Affichage de l'ID 
  Serial.println("Un badge est détecté");
  Serial.println(" L'UID du tag est:");
  for (byte i = 0; i < 4; i++) 
  {
    Serial.print(nuidPICC[i], HEX);
    Serial.print(" ");    
  }

  Serial.println();

  // Re-Init RFID
  rfid.PICC_HaltA(); // Halt PICC
  rfid.PCD_StopCrypto1(); // Stop encryption on PCD


  // Récupère la valeur du bouton 
  //boolean newState = digitalRead(BUTTON_PIN);

  
  //colorWipe(strip.Color(255,   0,   0), 200);    // Red
  //rainbow(50);
/*
  Allume_multi_couleur();
  delay(1000);
  Allume_Tout(strip.Color( 70,255,0));
  delay(1000);
  Allume_En_Decalage(strip.Color(0,0,0),50);
  delay(1000);
*/


  
}

void Allume_En_Decalage(uint32_t color, int wait) {
  for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip...
    strip.setPixelColor(i, color);         //  Set pixel's color (in RAM)
    strip.show();                          //  Update strip to match
    delay(wait);                           //  Pause for a moment
  }
}


void Allume_Tout(uint32_t color){
  for(int t=0; t<strip.numPixels(); t++) {
    strip.setPixelColor(t, color);
  }
  strip.show();
  }

void Allume_multi_couleur() {

    for(int i=0; i<strip.numPixels(); i++) { // For each pixel in strip...
      int pixelHue = (i * 65536L / strip.numPixels());
      strip.setPixelColor(i, strip.gamma32(strip.ColorHSV(pixelHue)));
    }
    strip.show(); // Update strip with new contents
}

byte GetAccesState(byte *CodeAcces,byte *NewCode) 
{
  byte StateAcces=0; 
  if ((CodeAcces[0]==NewCode[0])&&(CodeAcces[1]==NewCode[1])&&
  (CodeAcces[2]==NewCode[2])&& (CodeAcces[3]==NewCode[3]))
    return StateAcces=1; 
  else
    return StateAcces=0; 
}
