/* ============================================================================================================
 *    ёRadio
 * ============================================================================================================
 *    Web-radio based on
 *    ESP32-audioI2S     https://github.com/schreibfaul1/ESP32-audioI2S  
 *    or/and
 *    ESP32-vs1053_ext   https://github.com/schreibfaul1/ESP32-vs1053_ext
 *    libraries
 * ============================================================================================================
 *    Project home       https://github.com/e2002/yoradio
 *    Wiki               https://github.com/e2002/yoradio/wiki
 *    Описание на 4PDA   https://4pda.to/forum/index.php?s=&showtopic=1010378&view=findpost&p=112992611
 *    Как это прошить?   https://4pda.to/forum/index.php?act=findpost&pid=112992611&anchor=Spoil-112992611-2
 * ============================================================================================================
 *    Here goes!
 * ============================================================================================================
 */
#include "Arduino.h"
#include "src/core/options.h"
#include "src/core/config.h"
#include "src/core/telnet.h"
#include "src/core/player.h"
#include "src/core/display.h"
#include "src/core/network.h"
#include "src/core/netserver.h"
#include "src/core/controls.h"
#include "src/core/mqtt.h"
#include "src/core/optionschecker.h"

extern __attribute__((weak)) void yoradio_on_setup();

void setup() {
  Serial.begin(115200);
//analogWrite
    pinMode(26, INPUT);
      pinMode(12, OUTPUT);//Q2 питание
  digitalWrite(12, LOW);

 pinMode(34, INPUT_PULLUP);//статус внешней зарядки

  //esp_sleep_enable_ext0_wakeup(GPIO_NUM_0,0); //1 = High, 0 = Low
  
  if(LED_BUILTIN!=255) pinMode(LED_BUILTIN, OUTPUT);
  if (yoradio_on_setup) yoradio_on_setup();
  config.init();
  display.init();
  player.init();
  
  network.begin();
  if (network.status != CONNECTED) {
    netserver.begin();
    display.putRequest(DSP_START);
    while(!display.ready()) delay(10);
    return;
  }
  netserver.begin();
  telnet.begin();
  initControls();
  
  display.putRequest(DSP_START);
  while(!display.ready()) delay(10);
  #ifdef MQTT_ROOT_TOPIC
    mqttInit();
  #endif
  if (config.store.play_mode==PM_SDCARD) player.initHeaders(config.station.url);
  player.lockOutput=false;
  if (config.store.smartstart == 1) player.sendCommand({PR_PLAY, config.store.lastStation});
  pinMode(0, INPUT_PULLUP);
}

void loop() {
    pinMode(0, INPUT_PULLUP);
  power_off(digitalRead(0));
  telnet.loop();
  if (network.status == CONNECTED) {
    player.loop();
    loopControls();
  }
  netserver.loop();
}

#include "src/core/audiohandlers.h"
