#include "SerialMulti.hpp"
CSerialMulti SerialM;

#include "SDAudioPlayer.hpp"
CSDAudioPlayer SDAudioPlayer;

void setup() {
  SerialM.begin(115200);
  SDAudioPlayer.begin();
}

void loop() {
  String s;
  char c, fn[20];
  static int ind = 0;
 
  if (SerialM.available()) {
    switch (c = SerialM.read()) {
      case '#': SerialM.restart(); break;
      case '!': SerialM.usbboot(); break;

      case '\33': SerialM.printf("\x1b[2J"); break;

      case '<': SerialM.printf("vol=%d\n", SDAudioPlayer.set_volume(SDAudioPlayer.get_volume() + 1)); break;
      case '>': SerialM.printf("vol=%d\n", SDAudioPlayer.set_volume(SDAudioPlayer.get_volume() - 1)); break;

      case ' ': SDAudioPlayer.stop(); break;

      case 'l':
        SDAudioPlayer.stop();
        SDAudioPlayer.sd.ls(SerialM.port(), "/", LS_R);
        break;

      case '1' ... '9':
        s = String(c) + ".raw";
        s.toCharArray(fn, 20);
        SerialM.printf("file=%s %s\n", fn, SDAudioPlayer.play(fn) ? " play" : " skip");
        break;

      case '-':
        if (--ind < 1) ind = 20;
        s = String(ind, DEC) + ".raw";
        s.toCharArray(fn, 20);
        SerialM.printf("file=%s %s\n", fn, SDAudioPlayer.play(fn) ? " play" : " skip");
        break;
      case '+':
        if (++ind > 20) ind = 1;
        s = String(ind, DEC) + ".raw";
        s.toCharArray(fn, 20);
        SerialM.printf("file=%s %s\n", fn, SDAudioPlayer.play(fn) ? " play" : " skip");
        break;
    }
  }
  SerialM.flush();
  delay(10);
}