//**********************************************************************************************************
//*    audioI2S-- I2S audiodecoder for M5Stack Core2                                                       *
//**********************************************************************************************************
//
// first release on May.12/2021
//
//
// THE SOFTWARE IS PROVIDED "AS IS" FOR PRIVATE USE ONLY, IT IS NOT FOR COMMERCIAL USE IN WHOLE OR PART OR CONCEPT.
// FOR PERSONAL USE IT IS SUPPLIED WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHOR
// OR COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE
//

#include <M5Core2.h>
#include "Audio.h"

// Digital I/O used
#define SD_CS          4
#define SD_MOSI       23
#define SD_MISO       38
#define SD_SCK        18
#define I2S_DOUT       2
#define I2S_BCLK      12
#define I2S_LRC        0

Audio audio;
String ssid =     "xxxxxx";
String password = "xxxxxx";


void my_audio_info(Audio::msg_t m) {
    Serial.printf("%s: %s\n", m.s, m.msg);
}

void setup() {
    Audio::audio_info_callback = my_audio_info;
    M5.begin(true, true, true, true);
    M5.Axp.SetSpkEnable(true);
    M5.Lcd.fillScreen(BLACK);
    M5.Lcd.setTextColor(WHITE);
    M5.Lcd.setTextSize(2);

    pinMode(SD_CS, OUTPUT);
    digitalWrite(SD_CS, HIGH);
    SPI.begin(SD_SCK, SD_MISO, SD_MOSI);
    SPI.setFrequency(1000000);
    SD.begin(SD_CS);

    audio.setPinout(I2S_BCLK, I2S_LRC, I2S_DOUT);
    audio.setVolume(15); // 0...21

    WiFi.mode(WIFI_STA);
    WiFi.begin(ssid.c_str(), password.c_str());
    while (!WiFi.isConnected()) {
      delay(10);
    }
    ESP_LOGI(TAG, "Connected");
    ESP_LOGI(TAG, "Starting MP3...\n");

//  audio.connecttoFS(SD, "/320k_test.mp3");
//  audio.connecttoFS(SD, "test.wav");
  audio.connecttohost("http://air.ofr.fm:8008/jazz/mp3/128");
//  audio.connecttospeech("Миска вареників з картоплею та шкварками, змащених салом!", "uk-UA");
}

void loop() {
    vTaskDelay(1);
    audio.loop();
    if(Serial.available()){ // put streamURL in serial monitor
        audio.stopSong();
        String r=Serial.readString();
        r.trim();
        if(r.length()>5) audio.connecttohost(r.c_str());
        log_i("free heap=%i", ESP.getFreeHeap());
    }
}
