Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SPI.h>
- #include <SdFat.h>
- #include <Adafruit_VS1053.h>
- #include <SFEMP3Shield.h>
- SFEMP3Shield MP3player;
- SdFat sd;
- #define RESET 8 // VS1053 reset pin (output)
- #define CS 6 // VS1053 chip select pin (output)
- #define DCS 7 // VS1053 Data/command select pin (output)
- #define CARDCS 9 // Card chip select pin
- #define DREQ 2 // VS1053 Data request, ideally an Interrupt pin
- Adafruit_VS1053_FilePlayer musicPlayer = Adafruit_VS1053_FilePlayer(RESET, CS, DCS, DREQ, CARDCS);
- SdFile recording; // the file we will save our recording to
- #define RECBUFFSIZE 128 // 64 or 128 bytes.
- uint8_t recording_buffer[RECBUFFSIZE];
- int ledpin = A5;
- int ledState = LOW;
- unsigned long previousMillis = 0;
- const long interval = 500;
- int hook = A3;
- #include <Keypad.h>
- const byte ROWS = 4; //four rows
- const byte COLS = 3; //three columns
- char keys[ROWS][COLS] = {
- {'1','2','3'},
- {'4','5','6'},
- {'7','8','9'},
- {'*','0','#'}};
- byte rowPins[ROWS] = {A2, A1, A0, A4};
- byte colPins[COLS] = {5, 4, 3};
- Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS );
- #define Number_Length 11
- char Data[Number_Length];
- byte data_count = 0, master_count = 0;
- char customKey;
- boolean stoptone = false;
- boolean done = false;
- String number;
- boolean mail = false;
- boolean recording_on = false;
- void setup(){
- Serial.begin(9600);
- // if(!SD.begin(9, SPI_HALF_SPEED)) SD.initErrorHalt();
- // if (!SD.chdir("/")) SD.errorHalt("sd.chdir");
- // if (!musicPlayer.begin()) {Serial.println("VS1053 not found");
- // while (1); // don't do anything more}
- if (!SD.begin(CARDCS)) {
- Serial.println("SD failed, or not present");
- while (1); // don't do anything more
- }
- Serial.println("SD OK!");
- pinMode(hook, INPUT);
- pinMode(ledpin, OUTPUT);
- MP3player.begin();
- MP3player.setVolume(0,30);}
- uint8_t isRecording = false;
- void loop(){
- MP3player.available();
- int hook_state = digitalRead(hook);
- unsigned long currentMillis = millis();
- if (hook_state == HIGH){
- MP3player.stopTrack();
- clearData();
- if (currentMillis - previousMillis >= interval && mail == true){
- previousMillis = currentMillis;
- if (ledState == LOW) {ledState = HIGH;}
- else {ledState = LOW;}
- digitalWrite(ledpin, ledState);}
- }
- else{digitalWrite(ledpin, LOW);
- if(stoptone == false) {
- MP3player.playMP3("tone.mp3");
- }
- char key = keypad.getKey();
- if (key){
- Serial.println(key);
- stoptone = true;
- MP3player.stopTrack();
- String keychime = "/"+String(key)+".mp3";
- if(keychime == "/#.mp3"){keychime = "/a.mp3";
- if(data_count == 0){recording_on = true;}
- else{number = number+key;
- data_count++;}}
- else if(keychime == "/*.mp3"){keychime = "/b.mp3";
- number = number+key;
- data_count++;}
- else{number = number+key;
- data_count++;}
- MP3player.playMP3(keychime.c_str());
- }
- if(data_count == Number_Length-1 && done == false){
- done = true;
- MP3player.stopTrack();
- number = "/"+number.substring(0, 3)+"/"+number.substring(3, 10)+".ogg";
- Serial.println(number);
- if(recording_on == true){Record();}
- else if(SD.exists(number.c_str())) {
- MP3player.playMP3(number.c_str());
- }
- else {
- MP3player.playMP3("busy.mp3");}
- clearData();
- }}}
- void clearData(){
- while(data_count !=0){
- Data[data_count--] = 0;}
- number = "";
- stoptone = false;
- done = false;
- recording_on = false;
- return;}
- void Record(){
- int hook_state = digitalRead(hook);
- if (hook_state == HIGH){
- Serial.println("End recording");
- musicPlayer.stopRecordOgg();
- isRecording = false;
- saveRecordedData(isRecording);
- clearData();
- recording.close();
- delay(1000);
- return;}
- else if (! musicPlayer.prepareRecordOgg("v44k1q05.img")) {
- Serial.println("Couldn't load plugin!");
- while (1);}
- MP3player.playMP3("/record.mp3");
- Serial.println("Begin recording");
- isRecording = true;
- Serial.print("Recording to "); Serial.println(number);
- recording = sd.open(number, FILE_WRITE);
- if (! recording) {
- Serial.println("Couldn't open file to record!");
- while (1);
- }
- musicPlayer.startRecordOgg(true);
- if (isRecording)
- saveRecordedData(isRecording);
- }
- uint16_t saveRecordedData(boolean isrecord) {
- uint16_t written = 0;
- // read how many words are waiting for us
- uint16_t wordswaiting = musicPlayer.recordedWordsWaiting();
- // try to process 256 words (512 bytes) at a time, for best speed
- while (wordswaiting > 256) {
- //Serial.print("Waiting: "); Serial.println(wordswaiting);
- // for example 128 bytes x 4 loops = 512 bytes
- for (int x=0; x < 512/RECBUFFSIZE; x++) {
- // fill the buffer!
- for (uint16_t addr=0; addr < RECBUFFSIZE; addr+=2) {
- uint16_t t = musicPlayer.recordedReadWord();
- //Serial.println(t, HEX);
- recording_buffer[addr] = t >> 8;
- recording_buffer[addr+1] = t;
- }
- if (! recording.write(recording_buffer, RECBUFFSIZE)) {
- Serial.print("Couldn't write "); Serial.println(RECBUFFSIZE);
- while (1);
- }
- }
- // flush 512 bytes at a time
- recording.flush();
- written += 256;
- wordswaiting -= 256;
- }
- wordswaiting = musicPlayer.recordedWordsWaiting();
- if (!isrecord) {
- Serial.print(wordswaiting); Serial.println(" remaining");
- // wrapping up the recording!
- uint16_t addr = 0;
- for (int x=0; x < wordswaiting-1; x++) {
- // fill the buffer!
- uint16_t t = musicPlayer.recordedReadWord();
- recording_buffer[addr] = t >> 8;
- recording_buffer[addr+1] = t;
- if (addr > RECBUFFSIZE) {
- if (! recording.write(recording_buffer, RECBUFFSIZE)) {
- Serial.println("Couldn't write!");
- while (1);
- }
- recording.flush();
- addr = 0;
- }
- }
- if (addr != 0) {
- if (!recording.write(recording_buffer, addr)) {
- Serial.println("Couldn't write!"); while (1);
- }
- written += addr;
- }
- musicPlayer.sciRead(VS1053_SCI_AICTRL3);
- if (! (musicPlayer.sciRead(VS1053_SCI_AICTRL3) & _BV(2))) {
- recording.write(musicPlayer.recordedReadWord() & 0xFF);
- written++;
- }
- recording.flush();
- }
- return written;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement