Kód: Egész kijelölése
/* MP3Shield_Serial_Interpreter.ino
//..............................................................................
// Idea: Mohannad Rawashdeh
// @modified: 2016. 02.
// By kapu48 HW.Serial interpreter
//
// SW. ver.: Arduino 1.6.7
//
// HW:
// www.elechouse.com MP3 1.2V Shield test . To Arduino Uno.
// http://www.elechouse.com/elechouse/index.php?main_page=product_info&cPath=168_170&products_id=2193
*/
#include <SoftwareSerial.h>
#include "MP3.h"
// testing 1 file Play times ms.
#define PLAY_TIME 50000
/** define mp3 class */
MP3 mp3;
volatile boolean stringComplete = false; // whether the string is complete
String inputString; // a string to hold incoming data
String inputStringOK; // a string to hold incoming data
uint8_t mp3Source, mp3Comand_, mp3Comand_I, string_I, string_II, var ; // Parancs feldolgozó indexelése
uint16_t mp3Comand_N;
unsigned char i;
uint8_t len;
unsigned long waitTime[2], wait0, wait1; // Multi tasking időzítések
u16 Number_SD, Number_USB, Number_FLASH, File_Number; // Számlálók
u8 Check_State, checkSource, set_volum; // Státus infók
u8 source = SOURCE_SD; // Default Source: 0 = SD, 1 = USB, 2 = FLASH
u16 fi = 1; // Default file
//------------------------------------- setup() -----------------------------------------
void setup() {
// put your setup code here, to run once:
Serial.begin(57600);
// reserve 30 bytes for the inputString:
inputString.reserve(30);
// reserve 30 bytes for the inputStringOK:
inputStringOK.reserve(30);
delay(100);
inputString = "";
inputStringOK = "";
// send an intro:
//Serial.println();
mp3.begin(MP3_SOFTWARE_SERIAL); // select software serial
delay(10);
//MyCheckSource(MAX_VOLUME, PLAY_TIME); // Adat tárolók megléte és tartalmának vizsgálata
/** set volum to the MAX = 31 to MIN = 0 */
mp3.volume(MAX_VOLUME - 6);
/** set mode, Use SINGLE, REPEAT, CYCLE or RANDOM as parameter
0x00 Single Play
0x01 Repeat single music
0x02 Repeat all
0x03 Play random*/
// set MP3 Shield SINGLE mode * /
mp3.set_mode(SINGLE_PLAY);
mp3.stop();
// play music in sd, '0001' for first music * /
//mp3.play_sd(0x0001);
// play music in USB-disk * /
//mp3.play_usb_disk(0x0001);
// play music in SPI FLASH * /
//mp3.play_spi_flash(0x0001);
wait0 = PLAY_TIME; // Játék hossza
wait1 = wait0 / 4;
waitTime[0] = wait0 + millis();
waitTime[1] = wait1 + millis();
Serial.println(F(" Input String?")); // Várja a HW.Serial parancsot!
}
//------------------------------------- loop() -----------------------------------------
void loop() {
// print the string when a newline arrives:
if (stringComplete) { // Ha kaptunk 1 parancsot ?
inputStringOK = inputString;
// Megszakítás rutin felkészítése
inputString = "";
stringComplete = false;
// A parancsokat vesszövel kel elválasztani. Az utolsó parancsot pontosvesszővel kel lezárni!
// Kis vagy nagy betük használhatók.
// Pl.: us,sp,10; (Jelentése: SOURCE_USB,SINGLE_PLAY,10.file;)
// Pl.: sp,10; (Jelentése: SOURCE_SD,SINGLE_PLAY,10.file;)
// vo,20; (Jelentése: VOLUME_PLAY,20; Lejátszás hangerő = 20)
// st; (Jelentése: STOP_PLAY )
// Pl2.: SD,RP,22; (Jelentése: SOURCE_SD,REPEAT_SINGLE_PLAY,22.file;)
// VO,31; (Jelentése: VOLUME_PLAY,31; Lejátszás hangerő = 31 MAXvolume)
// PA; (Jelentése: PAUSE_PLAY)
// PA; (ismétlés Jelentése: CONTINUES_PLAY)
/* Input HW.Serial to MP3_Interpreter Parancs készlett:
"SD" file SOURCE_SD
"US" file SOURCE_USB
"FL" file SOURCE_FLASH
"SP" SINGLE_PLAY, file num (1 - 9999) (Max 4 karakter!)
"RP" REPEAT_SINGLE_PLAY, file num (1 - 9999) (Max 4 karakter!)
"ST" STOP_PLAY
"PA" PAUSE_PLAY
"VO" VOLUME_PLAY, volume (0 - 31) (Max 2 karakter!)
*/
MP3_Interpreter(); // Parancs értelmező, és végrehajtó
Serial.println(F("\n Input String?")); // Várja a következő parancsot!
}
/* // Lejátszás tesztek ídőzítése
if(waitTime[0] < millis()){
waitTime[0] = wait0 + millis();
completePlays(source);
}
// Státus beolvasó teszt
if(waitTime[1] < millis()){
waitTime[1] = wait1 + millis();
/* Check Work State
RETURN VALUE: 0x01 Playing,
0x02 Stop,
0x03 Pause * /
Check_State = mp3.Check_Work_State();
Serial.print(F("132. Check_State = "));
Serial.println(Check_State);
// lejátszás ídőzítés vége
if(Check_State == STOP) waitTime[0] = 0; // 0-ás Várakozás törlése.
}
*/
}
//------------------------------------------- endDigit(void) --------------------------------------------
// Megkeresi a numerikus karakterek végét, és az egészet uint16_t -ra alakítja
boolean endDigit(void){
uint8_t string_I_ = ++string_I, num = 0;
string_II++;
boolean While = 1;
boolean comandError = 0;
do{
if((inputStringOK.substring(string_I, string_II) >= "0") && (inputStringOK.substring(string_I, string_II) <= "9")){
string_I++;
string_II++;
num++;
}else{
While = 0;
}
}while(While);
if(num){ // Volt értékes karakter?
string_I = string_I_;
string_II--;
mp3Comand_N = inputStringOK.substring(string_I, string_II).toInt();
string_I = string_II;
string_II++;
}else{ // ? Nem Numerikus char
comandError = 1;
return comandError;
}
if(inputStringOK.substring(string_I, string_II) == ";") { // ? Parancs vége
var = 0; // Ki léptetés a feldolgozó hurokból
}else if(inputStringOK.substring(string_I, string_II) == ",") { // ? Elválasztó jel
string_I++; // Következő 2 betüs parancs
string_II += 3;
}else{ // Error!
comandError = 1;
}
return comandError;
}
//--------------------------------------------- MP3_Interpreter(void) --------------------------------------
/* MP3_Interpreter:
"SD" file SOURCE_SD
"US" file SOURCE_USB
"FL" file SOURCE_FLASH
"SP" SINGLE_PLAY, file num (Max 4 digit!, 0 - 9999)
"RP" REPEAT_SINGLE_PLAY, file num (Max 4 digit!, 0 - 9999)
"ST" STOP_PLAY
"PA" PAUSE_PLAY
"VO" VOLUME_PLAY, volume (0 - 31) (Max 2 digit!, 0 - 31)
*/
boolean MP3_Interpreter(void){
bool comandError = 0;
string_I = 0;
string_II = 2;
var = 1;
mp3Comand_ = SINGLE_PLAY; // Default parancs
mp3Source = SOURCE_SD; // default Source
mp3Comand_N = 1; // Default Num
// Parancs sor értelmezése
while(var){
if(inputStringOK.substring(string_I, string_II) == "SD") { // SOURCE_SD
mp3Source = SOURCE_SD;
mp3Comand_I++;
string_I += 2; // Következő karakter
string_II++;
if(inputStringOK.substring(string_I, string_II) == ";") { // ? Parancs vége
var = 0; // Ki léptetés a feldolgozó hurokból
}else if(inputStringOK.substring(string_I, string_II) == ",") { // ? Elválasztó jel
string_I++;
string_II += 2;
}else{
var = 0; // Ki léptetés a feldolgozó hurokból
comandError = 1; // Parancs hiba
}
}else if(inputStringOK.substring(string_I, string_II) == "US") { // SOURCE_USB
mp3Source = SOURCE_USB;
mp3Comand_I++;
string_I += 2;
string_II++;
if(inputStringOK.substring(string_I, string_II) == ";") { // ? Parancs vége
var = 0;
}else if(inputStringOK.substring(string_I, string_II) == ",") { // ? Elválasztó jel
string_I++;
string_II += 2;
}else{
var = 0;
comandError = 1;
}
}else if(inputStringOK.substring(string_I, string_II) == "FL") { // SOURCE_FLASH
mp3Source = SOURCE_FLASH;
mp3Comand_I++;
string_I += 2;
string_II++;
if(inputStringOK.substring(string_I, string_II) == ";") { // ? Parancs vége
var = 0;
}else if(inputStringOK.substring(string_I, string_II) == ",") { // ? Elválasztó jel
string_I++;
string_II += 2;
}else{
var = 0;
comandError = 1;
}
}else if(inputStringOK.substring(string_I, string_II) == "SP") { // SINGLE_PLAY, file num
mp3Comand_ = SINGLE_PLAY;
mp3Comand_I++;
string_I += 2;
string_II++;
if(inputStringOK.substring(string_I, string_II) == ";") { // ? Parancs vége
var = 0;
}else if(inputStringOK.substring(string_I, string_II) == ",") { // ? Elválasztó jel
comandError = endDigit();
if(comandError) var = 0;
}else{
var = 0;
comandError = 1;
}
}else if(inputStringOK.substring(string_I, string_II) == "RP") { // REPEAT_SINGLE_PLAY, file num
mp3Comand_ = REPEAT_SINGLE_PLAY;
mp3Comand_I++;
string_I += 2;
string_II++;
if(inputStringOK.substring(string_I, string_II) == ";") { // ? Parancs vége
var = 0;
}else if(inputStringOK.substring(string_I, string_II) == ",") { // ? Elválasztó jel
comandError = endDigit();
if(comandError) var = 0;
}else{
var = 0;
comandError = 1;
}
}else if(inputStringOK.substring(string_I, string_II) == "NE") { // NEXT_PLAY
mp3Comand_ = NEXT_PLAY;
mp3Comand_I++;
string_I += 2;
string_II++;
if(inputStringOK.substring(string_I, string_II) == ";") { // ? Parancs vége
var = 0;
}else if(inputStringOK.substring(string_I, string_II) == ",") { // ? Elválasztó jel
string_I++;
string_II += 2;
}else{
var = 0;
comandError = 1;
}
}else if(inputStringOK.substring(string_I, string_II) == "ST") { // STOP_PLAY
mp3Comand_ = STOP_PLAY;
mp3Comand_I++;
string_I += 2;
string_II++;
if(inputStringOK.substring(string_I, string_II) == ";") { // ? Parancs vége
var = 0;
}else if(inputStringOK.substring(string_I, string_II) == ",") { // ? Elválasztó jel
string_I++;
string_II += 2;
}else{
var = 0;
comandError = 1;
}
}else if(inputStringOK.substring(string_I, string_II) == "PA") { // PAUSE_PLAY
mp3Comand_ = PAUSE_PLAY;
mp3Comand_I++;
string_I += 2;
string_II++;
if(inputStringOK.substring(string_I, string_II) == ";") { // ? Parancs vége
var = 0;
}else if(inputStringOK.substring(string_I, string_II) == ",") { // ? Elválasztó jel
string_I++;
string_II += 2;
}else{
var = 0;
comandError = 1;
}
}else if(inputStringOK.substring(string_I, string_II) == "VO") { // VOLUME_PLAY, volume (0 - 31)
mp3Comand_ = VOLUME_PLAY;
mp3Comand_I++;
string_I += 2;
string_II++;
if(inputStringOK.substring(string_I, string_II) == ";") { // ? Parancs vége
var = 0;
}else if(inputStringOK.substring(string_I, string_II) == ",") { // ? Elválasztó jel
comandError = endDigit();
if(comandError) var = 0;
}else{
var = 0;
comandError = 1;
}
}
}
if(comandError){
return 0; // Error!
}
// Parancs végrehalytása
if(mp3Comand_ == SINGLE_PLAY){
mp3.set_mode(SINGLE_PLAY);
if(mp3Source = SOURCE_SD){
mp3.play_sd(mp3Comand_N);
}else if(mp3Source = SOURCE_USB){
mp3.play_usb_disk(mp3Comand_N);
}else if(mp3Source = SOURCE_FLASH){
mp3.play_spi_flash(mp3Comand_N);
}
}else if(mp3Comand_ == REPEAT_SINGLE_PLAY){
mp3.set_mode(REPEAT_SINGLE_PLAY);
if(mp3Source = SOURCE_SD){
mp3.play_sd(mp3Comand_N);
}else if(mp3Source = SOURCE_USB){
mp3.play_usb_disk(mp3Comand_N);
}else if(mp3Source = SOURCE_FLASH){
mp3.play_spi_flash(mp3Comand_N);
}
}else if(mp3Comand_ == NEXT_PLAY){
mp3.next();
}else if(mp3Comand_ == STOP_PLAY){
mp3.stop();
}else if(mp3Comand_ == PAUSE_PLAY){
mp3.pause();
}else if(mp3Comand_ == VOLUME_PLAY){
mp3.volume((u8)mp3Comand_N);
}
return 1; // OK
}
//-------------------------------------- completePlays() -------------------------------
// A tárolókon levő Összes MP3 lejátszása
void completePlays(u8 _source){
switch (_source) {
case SOURCE_SD:
if(Number_USB >= fi){
mp3.play_usb_disk(fi);
fi ++;
}else{
// play music in sd, '0001' for first music * /
fi = 1;
source = SOURCE_USB;
}
break;
case SOURCE_USB:
if(Number_SD >= fi){
mp3.play_sd(fi);
fi ++;
}else{
fi = 1;
source = SOURCE_FLASH;
}
break;
case SOURCE_FLASH:
if(Number_FLASH >= fi){
mp3.play_spi_flash(fi);
fi ++;
}else{
fi = 1;
source = SOURCE_SD;
}
break;
}
}
//-------------------------------------- MyCheckSource() -------------------------------
// Létrző tárolók lekérdezése, Print Filek száma
void MyCheckSource(u8 volume, u16 time){
mp3.set_mode(SINGLE_PLAY);
mp3.volume(volume);
mp3.play_sd(1);
delay(time);
Number_SD = mp3.Check_Total_Number_SD();
Serial.print(F(" File Number SD = "));
Serial.println(Number_SD);
mp3.stop();
mp3.play_usb_disk(1);
delay(time);
Number_USB = mp3.Check_Total_Number_USB();
Serial.print(F(" File Number USB = "));
Serial.println(Number_USB);
mp3.stop();
mp3.play_spi_flash(1);
delay(time);
Number_FLASH = mp3.Check_Total_Number_FLASH();
Serial.print(F(" File Number FLASH= "));
Serial.println(Number_FLASH);
mp3.stop();
}
//---------------------------------------- serialEvent() -------------------------------------------------
/*
SerialEvent occurs whenever a new data comes in the
hardware serial RX. This routine is run between each
time loop() runs, so using delay inside loop can delay
response. Multiple bytes of data may be available.
http://forum.arduino.cc/index.php?topic=290459.0
SerialEvent jelentkezik, amikor új adat érkezik a
hardver soros RX. Ez a rutin fut egymás között
időhurok () fut, így a késés cikluson belül késleltetheti
válasz. Több bájt adat rendelkezésre állhatnak. */
void serialEvent() {
if(!stringComplete){ // Ha az előző adatot már feldogozta
while (Serial.available()) {
// get the new byte:
char inChar = (char)Serial.read();
// add it to the inputString:
if(isLowerCase(inChar)) inChar -= 32;
inputString += inChar;
// if the incoming character is a newline, set a flag
// so the main loop can do something about it:
if (inChar == '\n') { // ? New Line
stringComplete = true;
}
}
}
}