#include <NmraDcc.h>
#include "Arduino.h"
#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"


SoftwareSerial mySoftwareSerial(10, 11); // RX, TX


DFRobotDFPlayerMini myDFPlayer;


NmraDcc  Dcc ;
DCC_MSG  Packet ;


// choix de l'adresse à changer si nécessaire
int adresse = 91;

int octetc;
int B[10];






long t0=millis();
int f0;
int f1;
int f2;
int f3;
int f4;
int f5;
int f6;
int f7;
int f8;
int f10=0;
int f20=0;
int f30=0;
int f40=0;
int f50=0;
int f60=0;
int f70=0;
int f80=0;
int cson=0;
int cson1=0;
int cson2=0;
int cson3=0;
int cson4=0;
int cson5=0;
int cson6=0;
int cson7=0;
int cson8=0;


// Réception des paquets DCC
void notifyDccMsg( DCC_MSG * Msg)
{
 
  for(uint8_t i = 0; i < Msg->Size; i++)
  {
   
   
    if ( Msg->Data[0]==adresse) {
    octetc = Msg->Data[1];
   
            B[7]=int(octetc/128);
            if(B[7]==1) octetc=octetc-128;
            B[6]=int(octetc/64);
            if(B[6]==1) octetc=octetc-64;
            B[5]=int(octetc/32);
            if(B[5]==1) octetc=octetc-32;
            B[4]=int(octetc/16);
            if(B[4]==1) octetc=octetc-16;
            B[3]=int(octetc/8);
            if(B[3]==1) octetc=octetc-8;
            B[2]=int(octetc/4);
            if(B[2]==1) octetc=octetc-4;
            B[1]=int(octetc/2);
            if(B[1]==1) octetc=octetc-2;
            B[0]=int(octetc);
            
           
            
             // fonctions du groupe 1
            if(B[7]==1 && B[6]==0 && B[5]==0 ) {
            f0=B[4];
            f1=B[0];
            f2=B[1];
            f3=B[2];
            f4=B[3];
            
            }
             // fonctions du groupe 2
            if(B[7]==1 && B[6]==0 && B[5]==1 && B[4]==1 ) {
            f5=B[0];
            f6=B[1];
            f7=B[2];
            f8=B[3];
             
            }
            
           
            
             // envoi des commandes au module DFPlayer

           if (f1==1 && cson1==0) {
           myDFPlayer.play(1);
           cson1=1;
          }
           if (f1==0 && cson1==1) cson1=0;

           if (f2==1 && cson2==0) {
           myDFPlayer.play(2);
           cson2=1;
          }
           if (f2==0 && cson2==1) cson2=0;

           if (f3==1 && cson3==0) {
           myDFPlayer.play(3);
           cson3=1;
          }
           if (f3==0 && cson3==1) cson3=0;

           if (f4==1 && cson4==0) {
           myDFPlayer.play(4);
           cson4=1;
          }
           if (f4==0 && cson4==1) cson4=0;

           if (f5==1 && cson5==0) {
           myDFPlayer.play(5);
           cson5=1;
          }
           if (f5==0 && cson5==1) cson5=0;

           if (f6==1 && cson6==0) {
           myDFPlayer.play(6);
           cson6=1;
          }
           if (f6==0 && cson6==1) cson6=0;

           if (f7==1 && cson7==0) {
           myDFPlayer.play(7);
           cson7=1;
          }
           if (f7==0 && cson7==1) cson7=0;

           if (f8==1 && cson8==0) {
           myDFPlayer.play(8);
           cson8=1;
          }
           if (f8==0 && cson8==1) cson8=0;
           

           
           
           
            
           if (f0==1) digitalWrite(13,HIGH);
           if (f0==0) digitalWrite(13,LOW);
            
  

            
             
            
            
  }
 
}

}



void setup()
{
  Serial.begin(9600);
  mySoftwareSerial.begin(9600);
 
 pinMode(13,OUTPUT);
 digitalWrite(13,LOW);

 Serial.println();
  Serial.println(F("DFRobot DFPlayer Mini Demo"));
  Serial.println(F("Initializing DFPlayer ... (May take 3~5 seconds)"));
  
  if (!myDFPlayer.begin(mySoftwareSerial,false)) {  //Use softwareSerial to communicate with mp3.
    Serial.println(F("Unable to begin:"));
    Serial.println(F("1.Please recheck the connection!"));
    Serial.println(F("2.Please insert the SD card!"));
    while(true){
      delay(0); // Code to compatible with ESP8266 watch dog.
    }
  }
  Serial.println(F("DFPlayer Mini online."));
  
  myDFPlayer.volume(25);  //Set volume value. From 0 to 30


 

  Serial.println("NMRA DCC Example 1");
  // Call the main DCC Init function to enable the DCC Receiver
  Dcc.init( MAN_ID_DIY, 10, FLAGS_OUTPUT_ADDRESS_MODE | FLAGS_DCC_ACCESSORY_DECODER , 0 );
  Serial.println("Init Done");
}

void loop()
{
 
  Dcc.process();
}
