125 lines
2.7 KiB
C++
125 lines
2.7 KiB
C++
|
||
#include <SPI.h>
|
||
|
||
#include <SD.h>
|
||
|
||
#include <NewTone.h>
|
||
|
||
long note;
|
||
long duration;
|
||
|
||
#include "IRremote.h"
|
||
|
||
IRrecv irrecv(2); // указываем вывод, к которому подключен приемник
|
||
int tonePin = 3;
|
||
|
||
decode_results results;
|
||
|
||
File myFile;
|
||
|
||
#define SizeRegisterModules 20
|
||
|
||
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);
|
||
}
|
||
|
||
void Play(String *args){
|
||
Serial.println(args[0]);
|
||
Serial.println(args[1]);
|
||
Serial.println(args[2]);
|
||
if(args[0] == "tone") {
|
||
String prom = args[1];
|
||
prom.replace("\r\n", "");
|
||
note = prom.toInt();
|
||
Serial.println(prom);
|
||
prom = args[2];
|
||
prom.replace("\r\n", "");
|
||
duration = prom.toInt();
|
||
Serial.println(note);
|
||
Serial.println(duration);
|
||
Serial.println(prom);
|
||
NewTone(6,note,duration);
|
||
note = 0;
|
||
duration = 0;
|
||
prom = "";
|
||
}
|
||
if(args[0] == "delay") {
|
||
Serial.println("delay process");
|
||
String prom = args[1];
|
||
prom.replace("\n","");
|
||
duration = prom.toInt();
|
||
Serial.println(prom);
|
||
Serial.println(duration);
|
||
delay(duration);
|
||
duration = 0;
|
||
prom = "";
|
||
}
|
||
}
|
||
|
||
char getCharCommand(){
|
||
if (myFile.available() > 0) {
|
||
char cmdraw = myFile.read();
|
||
if (cmdraw > 0) return cmdraw;
|
||
}
|
||
return '?';
|
||
}
|
||
|
||
void songone(){
|
||
myFile = SD.open("1");
|
||
if (myFile) {
|
||
Serial.println("1:");
|
||
// считываем все данные из файла:
|
||
while (myFile.available()) {
|
||
char CharCommand = getCharCommand();
|
||
if (CharCommand != ';' && CharCommand != '?'){
|
||
//Serial.print(CharCommand);
|
||
if (CharCommand == ','){
|
||
IDarg++;
|
||
}else{
|
||
ParsedCommand[IDarg] += CharCommand;
|
||
}
|
||
}else if(CharCommand == ';'){
|
||
//Serial.println(" ");
|
||
Play(ParsedCommand);
|
||
IDarg = 0;
|
||
for (int i = 0;i<=20;i++) {ParsedCommand[i]="";}
|
||
//Serial.print(">");
|
||
}
|
||
}
|
||
// закрываем файл:
|
||
myFile.close();
|
||
} else {
|
||
// если файл не открылся, отображаем сообщение об ошибке:
|
||
Serial.println("error opening-");
|
||
}
|
||
}
|
||
|
||
void setup() {
|
||
Serial.begin(9600); // выставляем скорость COM порта
|
||
irrecv.enableIRIn(); // запускаем прием
|
||
pinMode(4, OUTPUT);
|
||
if (!SD.begin(4)) {
|
||
Serial.println("SD initialization failed!");
|
||
return;
|
||
}
|
||
}
|
||
|
||
void loop() {
|
||
if ( irrecv.decode( &results )) { // если данные пришли
|
||
switch ( results.value ) {
|
||
case 16724175:
|
||
songone();
|
||
break;
|
||
}
|
||
|
||
irrecv.resume(); // принимаем следующую команду
|
||
}
|
||
}
|