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

char First_Start=1;

    int   volume        = 1;
    int   songNumber    = 1;
    int   songsCount    = 1;
    bool  isPlaying     = true;
    bool  isNextPressed = false;
    unsigned long nextPressedTime;
    
    void printDetail(uint8_t type, int value);
    void checkButtons();



CRGB Wheel(byte WheelPos) 
{
  // return a color value based on an input value between 0 and 255
  if(WheelPos < 85)
    return CRGB(WheelPos * 3, 255 - WheelPos * 3, 0);
  else if(WheelPos < 170) {
    WheelPos -= 85;
    return CRGB(255 - WheelPos * 3, 0, WheelPos * 3);
  } else {
    WheelPos -= 170;
    return CRGB(0, WheelPos * 3, 255 - WheelPos * 3);
  }
}

void LumiZic()
{
                          
                  SerialBT.print("\n\\__Lecteur Musique___//");
                  SerialBT.print("\n\\__s -> Start/Pause__//");
                  SerialBT.print("\n\\__S -> STOP_________//");
                  SerialBT.print("\n\\__+ -> Volume-Up____//");
                  SerialBT.print("\n\\__-_-> Volume-Down__//");
                  SerialBT.print("\n\\__n -> Next_song____//");
                  SerialBT.print("\n\\__p -> Previous_s.__//");
                  SerialBT.print("\n\\__z -> Back Menu____//");
              
                  changement=1;
                         
                      while (changement == 1)                     
                      {           
                     {     

  int n, height;                          // read the audio signal and filter it
  
  n = analogRead(AUDIO_IN_PIN);            // remove the MX9614 bias of 1.25VDC
  n = abs(1023 - n);                      // hard limit noise/hum
  n = (n <= NOISE) ? 0 : abs(n - NOISE);  // apply the exponential filter to smooth the raw signal
  ADCFilter.Filter(n);
  lvl = ADCFilter.Current();
                                                              //  // plot the raw versus filtered signals
                                                              //  Serial.print(n);
                                                              //  Serial.print(" ");
                                                              //  Serial.println(lvl);
                                                              // calculate the number of pixels as a percentage of the range
                                                              // TO-DO: can be done dynamically by using a running average of min/max audio levels
  height = TOP * (lvl - minLvl) / (long)(maxLvl - minLvl);
  if(height < 0L) height = 0;
  else if(height > TOP) height = TOP;                         // turn the LEDs corresponding to the level on/off
  for(uint8_t i = 0; i < NUM_LEDS; i++) {                     // turn off LEDs above the current level
    if(i >= height) leds[i] = CRGB(0,0,0);                    // otherwise, turn them on!
    else leds[i] = Wheel( map( i, 0, NUM_LEDS-1, 30, 150 ) );
  }
  FastLED.show();
}
                         
        
                        
                          if (myDFPlayer.available()) 
                          {
                              uint8_t type = myDFPlayer.readType();
                                if(type == DFPlayerPlayFinished)
                                {
                                        myDFPlayer.playFolder(1, 1);  
                                }
                          }
                  
                        ValeurReceptionBT =(char)SerialBT.read();
                        
                        if (  Serial.available()  )    { SerialBT.write(Serial.read()); }   // bug avec music
                        
                        if (SerialBT.available()) 
                        {
                              
                              SerialBT.print("Reception :");                                // write on BT app
                              SerialBT.println(ValeurReceptionBT);                          // write on BT app      
                        }

                        Serial.write(SerialBT.read());                      // pour debugage
                        //ValeurReceptionBT = changement;                   // pour gestion BT

                        if(ValeurReceptionBT == '+')
                         {
                              SerialBT.print("\n\\________++++________//");
                              myDFPlayer.volumeUp();
                              myDFPlayer.volumeUp();
                              myDFPlayer.volumeUp();
                              myDFPlayer.volumeUp();
                         }
                          
                         if(ValeurReceptionBT == '-')
                         {
                              SerialBT.print("\n\\________----________//");
                              myDFPlayer.volumeDown();
                              myDFPlayer.volumeDown();
                              myDFPlayer.volumeDown();
                              myDFPlayer.volumeDown();
                         }
                          if(ValeurReceptionBT == 'S')
                         {
                              SerialBT.print("\n\\________STOP________//");
                              myDFPlayer.stop();
                              First_Start =1;    
                         }

                          if(ValeurReceptionBT == 'z')
                         {
                              SerialBT.print("\n\\________EXIT________//");
                              changement=0;
                              SerialBT.print("\n\n\\________MENU________//");
                         }

                         if(ValeurReceptionBT == 'n')
                         {
                             
                              myDFPlayer.next();
                              SerialBT.print("\n\n\\________NEXT________//");
                         }
                         if(ValeurReceptionBT == 'p')
                         {
                             
                              myDFPlayer.previous();
                              SerialBT.print("\n\n\\______PREVIOUS______//");
                         }
                          
                          if(ValeurReceptionBT == 's')
                         {
                              isPlaying = !isPlaying;
                              if(isPlaying == true)
                              {
                                  myDFPlayer.start();
                              } 
                              else 
                              {
                                  myDFPlayer.pause();
                                  SerialBT.print("\n\\____Start/Pause_____//"); 
                                  delay(500);
                                  if(First_Start == 1)
                                  {
                                      myDFPlayer.start();
                                      First_Start =0;
                                  }   
                              }
                         }
                         
                        if(ValeurReceptionBT == 'n')
                        {
                            SerialBT.print("\n\\____next song_____//");       //BUUUUG
                            myDFPlayer.playFolder(5, 1);
                            delay(500);
                         }     
                      }
}
