#include <WS2812FX.h>
#include <BluetoothSerial.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <SPI.h>

#include "1_Brightness_Select.h"
#include "2_Get_Color_Name_Function.h"
#include "3_Speed_Select.h"
#include "4_Effect_Mode_Function.h"
#include "5_Display_Configuration.h"
#include "6_Manual_Control.h"
#include "7_Crius_Logo.h"
#include "8_Bluetooth_Connection.h"
//#include "9_Battery_Monitor.h"
#include "10_Deep_Sleep_Configuration.h"


void setup() {
  Serial.begin(9600);
  ESP_BT.begin("Crius LED Panel"); //Name of your Bluetooth interface -> will show up on your phone

  pinMode(buttonPinModeUP, INPUT_PULLUP);
  pinMode(buttonPinModeDN, INPUT_PULLUP);
  pinMode(27, INPUT_PULLUP);

  ws2812fx.init();
  ws2812fx.setSpeed(0);
  ws2812fx.setColor(0x000000);
  ws2812fx.setMode(FX_MODE_STATIC);
  ws2812fx.setBrightness(0);
  ws2812fx.start();
  pinMode(powerPinG, OUTPUT);
  pinMode(powerPinR, OUTPUT);
  BOOT_Screen();

  // take some initial audio measurements to establish the "quiet" level
  for (int i = 0; i < 20; i++) {
    quietLevel += analogRead(39); // 0-1023
    delay(25);
  }

  quietLevel /= 20;
  Serial.print("\nquietLevel is "); Serial.println(quietLevel);

  // parameters: index, start, stop, mode, color, speed, reverse
  ws2812fx.setSegment(0,  0,  LED_COUNT - 1, FX_MODE_STATIC, RED, 8000, NO_OPTIONS);
}

void loop() {

  ESP_BT.register_callback(callback);

  Display_Configuration();

  if (BTisConnected == false) {
    OnOff_Check();
    bluetoothNotConnected();
    MANUAL();
  }

  if (ESP_BT.available()) {

    if (BTisConnected == true) {

      bluetoothConnected();

      // -------------------- Receive Bluetooth signal ---------------------- //
      VAL = ESP_BT.read();
      //    Serial.print(VAL);

      //Color `Selection Configuration//
      Color_Select();
      Basic_Colors_Select();

      //Speed Selection Configuration//
      Speed_Select();

      // Mode Selection Configuration//
      Mode_Select();

      //Brigthness Configuration//
      Brightness_Select();
    }
  }
  //  σBattery_Monitor();
}
