AdaFruit Wave Shield WaveHC dap

De MCHobby - Wiki
Sauter à la navigation Sauter à la recherche

Get more RAM & Flash!

Before you try to play audio, you'll want to free up some Arduino RAM, so that you don't end up with a nasty stack-overflow. Running out of RAM is hard to debug and frustrating, and likely if you're using a '168

Follow these instructions on how to get more RAM by reducing the input Serial library buffer. You dont need to do this if you're using an ATmega328

Note that the library is pretty big (about 10K) so if you want to do a lot more, I suggest upgrading to an ATmega328. The shield was designed with the expectation that this upgrade would be available.

A tour of dap_hc.pde

This is a tutorial of the waveHC library by going through dap_hc.pde

Its detailed and a little daunting. But stick with it since much of the code is going to be duplicated from this sketch to others!

Make sure you install the library by downloading it from the link above and sticking WaveHC folder in the libraries folder. The zip also contains dap_hc.pde

In case you need the sketch we're referring to here, its at the bottom of the page

Initialize the card

The hard work of playing music is all done right on the Arduino. This lets us skip having an MP3 decoder or other dedicated chip. That means more control and flexibility, but more firmware! Lets take a tour through the canonical sketch "dapHC.pde" this is a Digital Audio Player (dap) sketch using the Wave HC librarry. We used to use the Adafruit AF_Wave library but Mr. Fat16 did a fantastic job rewriting our code and making it faster, smaller and better. So we're going to use his library in this tutorial :)

Download the dapHC.pde sketch and read along! The first thing we need are some objects. The tough part is talking to the card. All cards are manufactured and formatted a little different. And the formatting has many layers - the card format - the partition format and the filesystem formatting. At the beginning of the sketch we have the #include to get the library header files in and an object for storing information about the card card, partition volume vol and filesystem root. We also have a directory buffer, for information on any folder/directories and an object for storing information about a single wave file wave

These are all pretty much manditory unless perhaps you dont want directory traversal in which case you can probably skip dirBuf

#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

uint8_t dirLevel; // indent level for file/dir names    (for prettyprinting)
dir_t dirBuf;     // buffer for directory reads

WaveHC wave;      // This is the only wave (audio) object, since we will only play one at a time

First thing that must be done is initializing the SD card for reading. This is a multistep process. In the setup loop you can see the multiple checks we do as we proceed through the initialization process

Here are the steps:

  1. Wake up and print out the to Serial Monitor that we're running (OPTIONAL)
  2. Check how much free RAM we have after we have used the buffer for storing Wave audio data, make sure its more than 100 bytes and keep an eye on it as you modify your code. This test can be removed, its for your use (OPTIONAL)
  3. Set the pin modes for the DAC control lines. These should not be changed unless you've modified them in the library as well. Its probably best to keep them as-is
  4. Initialize the SD card and see if it responds. We try to talk to it at 8MHz. If you have a waveshield 1.0 you may need to use 4MHz mode so comment out one line and uncommment the other to swap which method is used. If the card fails to initialize, print out an error and halt.
  5. Allow partial block reads. Some SD cards don't like this so if you're having problems, comment this out first! (OPTIONAL)
  6. Try to find a FAT partition in the first 5 slots. You did format the card to FAT format, right? If it cant find a FAT partition it will print out that it failed, so make sure you format it again if its giving you trouble
  7. Print out what kind of FAT partition was found (OPTIONAL)
  8. Try to open up the root directory. If this doesnt work, something is messed up with the formatting. Try to format it again!
  9. Finally, print out the files found, one after the other in the directories on the card. This is great for debugging and will show you what you've got on there. Since we dont have long filename access and use the 'base' 8.3 format to define files, you'll need to see what the files are named on the partition and this helps a lot (OPTIONAL)
 void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps for debugging
  
  putstring_nl("\nWave test!");  // say we woke up!
  
  putstring("Free RAM: ");       // This can help with debugging, running out of RAM is bad
  Serial.println(freeRam());  
 
  // Set the output pins for the DAC control. This pins are defined in the library
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  
  //  if (!card.init(true)) { //play with 4 MHz spi if 8MHz isn't working for you
  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!
  }
  
  // Lets tell the user about what we found
  putstring("Using partition ");
  Serial.print(part, DEC);
  putstring(", type is FAT");
  Serial.println(vol.fatType(),DEC);     // FAT16 or FAT32?
  
  // 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!
  }
  
  // Whew! We got past the tough parts.
  putstring_nl("Files found:");
  dirLevel = 0;
  // Print out all of the files in all the directories.
  lsR(root);
}