Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <PinChangeInt.h>
- #include <FatReader.h>
- #include <SdReader.h>
- #include <avr/pgmspace.h>
- #include <avr/sleep.h>
- #include "WaveUtil.h"
- #include "WaveHC.h"
- SdReader card; // This object holds the information for the card
- FatVolume vol; // This holds the information for the partition on the card
- FatReader root; // This holds the information for the filesystem on the card
- WaveHC wave; // This is the only wave (audio) object, since we will only play one at a time
- int ledPin = 6;
- int intPin = 7;
- volatile boolean triggered = false;
- /*
- * print dir_t name field. The output is 8.3 format, so like SOUND.WAV or FILENAME.DAT
- */
- void printName(dir_t &dir)
- {
- for (uint8_t i = 0; i < 11; i++) { // 8.3 format has 8+3 = 11 letters in it
- if (dir.name[i] == ' ')
- continue; // dont print any spaces in the name
- if (i == 8)
- Serial.print('.'); // after the 8th letter, place a dot
- Serial.write(dir.name[i]); // print the n'th digit
- }
- if (DIR_IS_SUBDIR(dir))
- Serial.print('/'); // directories get a / at the end
- }
- /*
- * print error message and halt if SD I/O error, great for debugging!
- */
- void sdErrorCheck(void)
- {
- if (!card.errorCode()) return;
- putstring("\n\rSD I/O error: ");
- Serial.print(card.errorCode(), HEX);
- putstring(", ");
- Serial.println(card.errorData(), HEX);
- while(1);
- }
- void beginSleep() {
- putstring_nl("Going to sleep.");
- delay(100);
- set_sleep_mode(SLEEP_MODE_PWR_DOWN);
- sleep_enable();
- digitalWrite(ledPin, LOW);
- // Debounce the interrupt
- while (digitalRead(intPin) == LOW) {
- PCintPort::attachInterrupt(intPin, &interruptHandler, RISING);
- sleep_cpu();
- delay(10);
- }
- sleep_disable();
- digitalWrite(ledPin, HIGH);
- }
- void setup() {
- Serial.begin(9600);
- pinMode(intPin, INPUT);
- pinMode(ledPin, OUTPUT);
- digitalWrite(ledPin, HIGH);
- // Audio shield
- pinMode(2, OUTPUT);
- pinMode(3, OUTPUT);
- pinMode(4, OUTPUT);
- pinMode(5, OUTPUT);
- if (!card.init()) { //play with 8 MHz spi (default faster!)
- putstring_nl("Card init. failed!"); // Something went wrong, lets print out why
- sdErrorCheck();
- while(1); // then 'halt' - do nothing!
- }
- // enable optimize read - some cards may timeout. Disable if you're having problems
- card.partialBlockRead(true);
- // Now we will look for a FAT partition!
- uint8_t part;
- for (part = 0; part < 5; part++) { // we have up to 5 slots to look in
- if (vol.init(card, part))
- break; // we found one, lets bail
- }
- if (part == 5) { // if we ended up not finding one :(
- putstring_nl("No valid FAT partition!");
- sdErrorCheck(); // Something went wrong, lets print out why
- while(1); // then 'halt' - do nothing!
- }
- // Try to open the root directory
- if (!root.openRoot(vol)) {
- putstring_nl("Can't open root dir!"); // Something went wrong,
- while(1); // then 'halt' - do nothing!
- }
- putstring_nl("Ready.");
- delay(100);
- }
- void loop()
- {
- dir_t dirBuf;
- FatReader file;
- root.rewind();
- while (root.readDir(dirBuf) > 0) {
- if (dirBuf.name[0] == '.')
- continue;
- if(file.open(vol, dirBuf) && !file.isDir()) {
- putstring("Enqueued ");
- printName(dirBuf);
- putstring_nl("");
- beginSleep();
- if (wave.create(file)) {
- wave.play();
- putstring("Playing...");
- while (wave.isplaying) {
- delay(100);
- }
- putstring_nl("Done.");
- }
- sdErrorCheck();
- }
- }
- }
- void interruptHandler() {
- PCintPort::detachInterrupt(intPin);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement