#define SizeRegisterModules 20
#include "QB.h"

int IDarg = 0;
String ParsedCommand[SizeRegisterModules];

void ShowError(String errorMessage) {
  Serial.println("Error: \n " + errorMessage);
}

void ShowMessage(String type, String Message) {
  Serial.println("[" + type + "] " + Message);
}

char getCharCommand() {
  if (Serial.available() > 0) {
    char cmdraw = Serial.read();
    if (cmdraw > 0) return cmdraw;
  }
  return '?';
}
void song() {
Qb_PLAY ("MNT120L16O4E.D+.E.D+.E.<B.>D.C.<A.<E.A.>C.E.A.B.<E.G+.>E.G+.B.>C.O2E.A.");
Qb_PLAY (">E.>E.D+.E.D+.E.<B.>D.C.<A.<E.A.>C.E.A.B.<E.G+.>D.>C.<B.A.<E.A.P32O4E.");
Qb_PLAY ("D+.E.D+.E.<B.>D.C.<A.<E.A.>C.E.A.B.<E.G+.>E.G+.B.>C.O2E.A.>E.>E.D+.E.");
Qb_PLAY ("D+.E.<B.>D.C.<A.<E.A.>C.E.A.B.<E.G+.>D.>C.<B.A.<E.A.>B.>C.D.E.O2G.>C.G.");
Qb_PLAY (">F.E.D.O2G.B.>F.>E.D.C.O2E.A.>G.>D.C.<B.<E.>E.P64E.>E.<E.>E.P64E.>E.<");
Qb_PLAY ("D+.E.D+.E.D+.E.D+.E.D+.E.D+.E.<B.>D.C.<A.<E.A.>C.E.A.B.<E.G+.>E.G+.B.");
Qb_PLAY (">C.O2E.A.>E.>E.D+.E.D+.E.<B.>D.C.<A.<E.A.>C.E.A.B.<E.G+.>D.>C.<B.A.<E.");
Qb_PLAY ("A.P32>B.>C.D.E.O2G.>C.G.>F.E.D.O2G.B.>F.>E.D.C.O2E.A.>E.>D.C.<B.<E.>");
Qb_PLAY ("E.>E.<E.>E.P64E.>E.<D+.E.D+.E.D+.E.D+.E.D+.E.D+.E.<B.>D.C.<A.<E.A.>C.E.");
Qb_PLAY ("A.B.<E.G+.>E.G+.B.>C.O2E.A.>E.>E.D+.E.D+.E.<B.>D.C.<A.<E.A.>C.E.A.B.<E.");
}

void mainL(String *args) {
  if (args[0] == "play") {
    if (args[1] == "1") {
      song();
    }
  }
}

void setup() {
  Serial.begin(9600);
  ShowMessage("Note", "Type ';' to indicate the end of command, and then press 'Enter'");
  Serial.print("\n \n \n \n>");
}

void loop() {
  char CharCommand = getCharCommand();
  if (CharCommand != ';' && CharCommand != '?') {
    Serial.print(CharCommand);
    if (CharCommand == ' ') {
      IDarg++;
    } else {
      ParsedCommand[IDarg] += CharCommand;
    }
  } else if (CharCommand == ';') {
    Serial.println(" ");
    mainL(ParsedCommand);
    IDarg = 0;
    for (int i = 0; i <= 20; i++) {
      ParsedCommand[i] = "";
    }
    Serial.print(">");
  }
}
