Changes

Students/Ian Petrov

4,536 bytes added, 09:09, 20 March 2010
no edit summary
Code
----
[quote]<pre>[<span style="color=: #7E7E7E];">//Ian Petrov VIS147A Final[Ian&nbsp;Petrov&nbsp;VIS147A&nbsp;Final</color]span>[<span style="color=: #7E7E7E];">//code edited from Adafruit Wave Shield Site[code&nbsp;edited&nbsp;from&nbsp;Adafruit&nbsp;Wave&nbsp;Shield&nbsp;Site</color]span>#include include&nbsp;<FatReader.h>#include include&nbsp;<SdReader.h>#include include&nbsp;<avr/pgmspace.h>#include [include&nbsp;<span style="color=: #006699];">"WaveUtil.h"[</color]span>#include [include&nbsp;<span style="color=: #006699];">"WaveHC.h"[</color]span>
SdReader cardSdReader&nbsp;    [card;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color=: #7E7E7E];">// This object holds the information for the card[</color]span>FatVolume volFatVolume&nbsp;    [vol;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color=: #7E7E7E];">// This holds the information for the partition on the card[</color]span>FatReader rootFatReader&nbsp;root;   [&nbsp;&nbsp;&nbsp;<span style="color=: #7E7E7E];">// This holds the information for the filesystem on the card[</color]span>FatReader fFatReader&nbsp;      [f;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color=: #7E7E7E];">// This holds the information for the file we're play[</color]span>
WaveHC waveWaveHC&nbsp;      [wave;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color=: #7E7E7E];">// This is the only wave (audio) object, since we will only play one at a time[</color]span>
#define DEBOUNCE 100  [define&nbsp;DEBOUNCE&nbsp;100&nbsp;&nbsp;<span style="color=: #7E7E7E];">// button debouncer[</color]span>
[<span style="color=: #7E7E7E];">// this handy function will return the number of bytes currently free in RAM&nbsp;this&nbsp;handy&nbsp;function&nbsp;will&nbsp;return&nbsp;the&nbsp;number&nbsp;of&nbsp;bytes&nbsp;currently&nbsp;free&nbsp;in&nbsp;RAM, great for debugging&nbsp;great&nbsp;for&nbsp;debugging!   [&nbsp;&nbsp;&nbsp;</color]span>[<span style="color=: #CC6600];">int[</color] span> freeRam([<span style="color=: #CC6600];">void[</color]span>)
{
  extern [&nbsp;&nbsp;extern&nbsp;<span style="color=: #CC6600];">int[</color] span> __bss_end;   extern [&nbsp;&nbsp;extern&nbsp;<span style="color=: #CC6600];">int[</color] span> *__brkval;   [&nbsp;&nbsp;<span style="color=: #CC6600];">int[</color] span> free_memory;   [&nbsp;&nbsp;<span style="color=: #CC6600];">if[</color]span>(([<span style="color=: #CC6600];">int[</color]span>)__brkval == 0) {    free_memory &nbsp;&nbsp;&nbsp;&nbsp;free_memory&nbsp;= &nbsp;(([<span style="color=: #CC6600];">int[</color]span>)&free_memory) - (([<span style="color=: #CC6600];">int[</color]span>)&__bss_end);   &nbsp;&nbsp;}  [&nbsp;&nbsp;<span style="color=: #CC6600];">else[</color] span> {    free_memory &nbsp;&nbsp;&nbsp;&nbsp;free_memory&nbsp;= &nbsp;(([<span style="color=: #CC6600];">int[</color]span>)&free_memory) - (([<span style="color=: #CC6600];">int[</color]span>)__brkval);   &nbsp;&nbsp;}  [&nbsp;&nbsp;<span style="color=: #CC6600];">return[</color] span> free_memory; } &nbsp;
[<span style="color=: #CC6600];">void[</color] span> sdErrorCheck([<span style="color=: #CC6600];">void[</color]span>)
{
  [&nbsp;&nbsp;<span style="color=: #CC6600];">if[</color] span> (!card.errorCode()) [<span style="color=: #CC6600];">return[</color]span>;  putstring&nbsp;&nbsp;putstring([<span style="color=: #006699];">"\n\rSD I/O error: "[</color]span>);  [&nbsp;&nbsp;<span style="color=: #CC6600][;"><b]>Serial[</b][></color]span>.[<span style="color=: #CC6600];">print[</color]span>(card.errorCode(), [<span style="color=: #006699];">HEX[</color]span>);  putstring&nbsp;&nbsp;putstring([<span style="color=: #006699];">", "[</color]span>);  [&nbsp;&nbsp;<span style="color=: #CC6600][;"><b]>Serial[</b][></color]span>.[<span style="color=: #CC6600];">println[</color]span>(card.errorData(), [<span style="color=: #006699];">HEX[</color]span>);  [&nbsp;&nbsp;<span style="color=: #CC6600];">while[</color]span>(1);
}
[<span style="color=: #CC6600];">void[</span> <span style="color] [color=: #CC6600][;"><b]>setup[</b][></color]span>() {  [&nbsp;&nbsp;<span style="color=: #7E7E7E];">// set up serial port[</color]span>  [&nbsp;&nbsp;<span style="color=: #CC6600][;"><b]>Serial[</b][></color]span>.[<span style="color=: #CC6600];">begin[</color]span>(9600);  putstring_nl&nbsp;&nbsp;putstring_nl([<span style="color=: #006699];">"WaveHC with 6 buttons"[</color]span>);  &nbsp;&nbsp;   putstring&nbsp;&nbsp;&nbsp;putstring([<span style="color=: #006699];">"Free RAM: "[</color]span>); [<span style="color=: #7E7E7E];">// This can help with debugging, running out of RAM is bad[</color]span>  [&nbsp;&nbsp;<span style="color=: #CC6600][;"><b]>Serial[</b][></color]span>.[<span style="color=: #CC6600];">println[</color]span>(freeRam()); [<span style="color=: #7E7E7E];">// if this is under 150 bytes it may spell trouble![</color]span>  &nbsp;&nbsp;  [&nbsp;&nbsp;<span style="color=: #7E7E7E];">// Set the output pins for the DAC control. This pins are defined in the library[</color]span>  [&nbsp;&nbsp;<span style="color=: #CC6600];">pinMode[</color]span>(2, [<span style="color=: #006699];">OUTPUT[</color]span>);  [&nbsp;&nbsp;<span style="color=: #CC6600];">pinMode[</color]span>(3, [<span style="color=: #006699];">OUTPUT[</color]span>);  [&nbsp;&nbsp;<span style="color=: #CC6600];">pinMode[</color]span>(4, [<span style="color=: #006699];">OUTPUT[</color]span>);  [&nbsp;&nbsp;<span style="color=: #CC6600];">pinMode[</color]span>(5, [<span style="color=: #006699];">OUTPUT[</color]span>); &nbsp;  [&nbsp;&nbsp;<span style="color=: #7E7E7E];">// pin13 LED[</color]span>  [&nbsp;&nbsp;<span style="color=: #CC6600];">pinMode[</color]span>(13, [<span style="color=: #006699];">OUTPUT[</color]span>); &nbsp;  [&nbsp;&nbsp;<span style="color=: #7E7E7E];">// enable pull-up resistors on switch pins (analog inputs)[</color]span>  [&nbsp;&nbsp;<span style="color=: #CC6600];">digitalWrite[</color]span>(14, [<span style="color=: #006699];">HIGH[</color]span>);  [&nbsp;&nbsp;<span style="color=: #CC6600];">digitalWrite[</color]span>(15, [<span style="color=: #006699];">HIGH[</color]span>);  [&nbsp;&nbsp;<span style="color=: #CC6600];">digitalWrite[</color]span>(16, [<span style="color=: #006699];">HIGH[</color]span>);  [&nbsp;&nbsp;<span style="color=: #CC6600];">digitalWrite[</color]span>(17, [<span style="color=: #006699];">HIGH[</color]span>);  [&nbsp;&nbsp;<span style="color=: #CC6600];">digitalWrite[</color]span>(18, [<span style="color=: #006699];">HIGH[</color]span>);  [&nbsp;&nbsp;<span style="color=: #CC6600];">digitalWrite[</color]span>(19, [<span style="color=: #006699];">HIGH[</color]span>); &nbsp;  [&nbsp;&nbsp;<span style="color=: #7E7E7E];">// if (!card.init(true)) { //play with 4 MHz spi if 8MHz isn't working for you[</color]span>  [&nbsp;&nbsp;<span style="color=: #CC6600];">if[</color] span> (!card.init()) { [<span style="color=: #7E7E7E];">//play with 8 MHz spi (default faster!) [</color]span>    putstring_nl&nbsp;&nbsp;&nbsp;&nbsp;putstring_nl([<span style="color=: #006699];">"Card init. failed!"[</color]span>); [<span style="color=: #7E7E7E];">// Something went wrong, lets print out why[</color]span>    sdErrorCheck&nbsp;&nbsp;&nbsp;&nbsp;sdErrorCheck();    [&nbsp;&nbsp;&nbsp;&nbsp;<span style="color=: #CC6600];">while[</color]span>(1); [<span style="color=: #7E7E7E];">// then 'halt' - do nothing![</color]span>  &nbsp;&nbsp;}  &nbsp;&nbsp;  [&nbsp;&nbsp;<span style="color=: #7E7E7E];">// enable optimize read - some cards may timeout. Disable if you're having problems[</color]span>  card&nbsp;&nbsp;card.partialBlockRead([<span style="color=: #CC6600];">true[</color]span>); &nbsp;[<span style="color=: #7E7E7E];">// Now we will look for a FAT partition&nbsp;Now&nbsp;we&nbsp;will&nbsp;look&nbsp;for&nbsp;a&nbsp;FAT&nbsp;partition![</color]span>  uint8_t part&nbsp;&nbsp;uint8_t&nbsp;part;  [&nbsp;&nbsp;<span style="color=: #CC6600];">for[</color] span> (part = 0; part < 5; part++) { [<span style="color=: #7E7E7E];">// we have up to 5 slots to look in[</color]span>    [&nbsp;&nbsp;&nbsp;&nbsp;<span style="color=: #CC6600];">if[</color] span> (vol.init(card, part))       [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color=: #CC6600];">break[</color]span>; [<span style="color=: #7E7E7E];">// we found one, lets bail[</color]span>  &nbsp;&nbsp;}  [&nbsp;&nbsp;<span style="color=: #CC6600];">if[</color] span> (part == 5) { [<span style="color=: #7E7E7E];">// if we ended up not finding one :([</color]span>    putstring_nl&nbsp;&nbsp;&nbsp;&nbsp;putstring_nl([<span style="color=: #006699];">"No valid FAT partition!"[</color]span>);    sdErrorCheck&nbsp;&nbsp;&nbsp;&nbsp;sdErrorCheck();      [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color=: #7E7E7E];">// Something went wrong, lets print out why[</color]span>    [&nbsp;&nbsp;&nbsp;&nbsp;<span style="color=: #CC6600];">while[</color]span>(1); [<span style="color=: #7E7E7E];">// then 'halt' - do nothing![</color]span>  &nbsp;&nbsp;}  &nbsp;&nbsp;  [&nbsp;&nbsp;<span style="color=: #7E7E7E];">// Lets tell the user about what we found[</color]span>  putstring&nbsp;&nbsp;putstring([<span style="color=: #006699];">"Using partition "[</color]span>);  [&nbsp;&nbsp;<span style="color=: #CC6600][;"><b]>Serial[</b][></color]span>.[<span style="color=: #CC6600];">print[</color]span>(part, [<span style="color=: #006699];">DEC[</color]span>);  putstring&nbsp;&nbsp;putstring([<span style="color=: #006699];">", type is FAT"[</color]span>);  [&nbsp;&nbsp;<span style="color=: #CC6600][;"><b]>Serial[</b][></color]span>.[<span style="color=: #CC6600];">println[</color]span>(vol.fatType(),[<span style="color=: #006699];">DEC[</color]span>); [<span style="color=: #7E7E7E];">// FAT16 or FAT32?[</color]span>  &nbsp;&nbsp;  [&nbsp;&nbsp;<span style="color=: #7E7E7E];">// Try to open the root directory[</color]span>  [&nbsp;&nbsp;<span style="color=: #CC6600];">if[</color] span> (!root.openRoot(vol)) {    putstring_nl&nbsp;&nbsp;&nbsp;&nbsp;putstring_nl([<span style="color=: #006699];">"Can't open root dir!"[</color]span>); [<span style="color=: #7E7E7E];">// Something went wrong,[</color]span>    [&nbsp;&nbsp;&nbsp;&nbsp;<span style="color=: #CC6600];">while[</color]span>(1); [<span style="color=: #7E7E7E];">// then 'halt' - do nothing![</color]span>  &nbsp;&nbsp;}  &nbsp;&nbsp;  [&nbsp;&nbsp;<span style="color=: #7E7E7E];">// Whew! We got past the tough parts.[</color]span>  putstring_nl&nbsp;&nbsp;putstring_nl([<span style="color=: #006699];">"Ready!"[</color]span>);
}
[<span style="color=: #CC6600];">void[</color] span> playcomplete([<span style="color=: #CC6600];">char[</color] span> good) {  [&nbsp;&nbsp;<span style="color=: #7E7E7E];">// call our helper to find and play this name[</color]span>  playfile&nbsp;&nbsp;playfile([<span style="color=: #006699];">"GOOD.WAV"[</color]span>);  [&nbsp;&nbsp;<span style="color=: #CC6600];">while[</color] span> (wave.isplaying) {  [&nbsp;&nbsp;<span style="color=: #7E7E7E];">// do nothing while its playing[</color]span>  &nbsp;&nbsp;}  [&nbsp;&nbsp;<span style="color=: #7E7E7E];">// now its done playing[</color]span>
}
[<span style="color=: #CC6600];">void[</span> <span style="color] [color=: #CC6600][;"><b]>loop[</b][></color]span>() {  [&nbsp;&nbsp;<span style="color=: #7E7E7E];">//putstring("."); // uncomment this to see if the loop isnt running [</color]span>    &nbsp;&nbsp;&nbsp;&nbsp;  [&nbsp;&nbsp;<span style="color=: #CC6600];">switch[</color] span> (check_switches()) { [<span style="color=: #7E7E7E];">//this is the loop which cycles the arnold sounds based[</color]span>    [&nbsp;&nbsp;&nbsp;&nbsp;<span style="color=: #CC6600];">case[</color] span> 1: [<span style="color=: #7E7E7E];">//on which switch you connect to.[</color]span>      playcomplete&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;playcomplete([<span style="color=: #006699];">"STOP.WAV"[</color]span>);      [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color=: #CC6600];">break[</color]span>;    [&nbsp;&nbsp;&nbsp;&nbsp;<span style="color=: #CC6600];">case[</color] span> 2:      playcomplete&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;playcomplete([<span style="color=: #006699];">"SOB.WAV"[</color]span>);      [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color=: #CC6600];">break[</color]span>;    [&nbsp;&nbsp;&nbsp;&nbsp;<span style="color=: #CC6600];">case[</color] span> 3:      playcomplete&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;playcomplete([<span style="color=: #006699];">"DONT.WAV"[</color]span>);      [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color=: #CC6600];">break[</color]span>;    [&nbsp;&nbsp;&nbsp;&nbsp;<span style="color=: #CC6600];">case[</color] span> 4:      playcomplete&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;playcomplete([<span style="color=: #006699];">"LACK.WAV"[</color]span>);      [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color=: #CC6600];">break[</color]span>;    [&nbsp;&nbsp;&nbsp;&nbsp;<span style="color=: #CC6600];">case[</color] span> 5:      playcomplete&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;playcomplete([<span style="color=: #006699];">"FIST.WAV"[</color]span>);  &nbsp;&nbsp;}
}
[<span style="color=: #CC6600];">byte[</color] span> check_switches()
{
  [&nbsp;&nbsp;<span style="color=: #CC6600];">static[</span> <span style="color] [color=: #CC6600];">byte[</color] span> previous[6];  [&nbsp;&nbsp;<span style="color=: #CC6600];">static[</span> <span style="color] [color=: #CC6600];">long[</color] span> time[6];  [&nbsp;&nbsp;<span style="color=: #CC6600];">byte[</color] span> reading;  [&nbsp;&nbsp;<span style="color=: #CC6600];">byte[</color] span> pressed;  [&nbsp;&nbsp;<span style="color=: #CC6600];">byte[</color] span> index;  pressed &nbsp;&nbsp;pressed&nbsp;= 0&nbsp;0;
  [&nbsp;&nbsp;<span style="color=: #CC6600];">for[</color] span> ([<span style="color=: #CC6600];">byte[</color] span> index = 0; index < 6; ++index) {    reading &nbsp;&nbsp;&nbsp;&nbsp;reading&nbsp;= [&nbsp;<span style="color=: #CC6600];">digitalRead[</color]span>(14 + index);    [&nbsp;&nbsp;&nbsp;&nbsp;<span style="color=: #CC6600];">if[</color] span> (reading == [<span style="color=: #006699];">LOW[</color] span> && previous[index] == [<span style="color=: #006699];">HIGH[</color] span> && [<span style="color=: #CC6600];">millis[</color]span>() - time[index] > DEBOUNCE)    &nbsp;&nbsp;&nbsp;&nbsp;{      [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color=: #7E7E7E];">// switch pressed[</color]span>      time&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;time[index] &nbsp;= [&nbsp;<span style="color=: #CC6600];">millis[</color]span>();      pressed &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pressed&nbsp;= index &nbsp;index&nbsp;+ 1&nbsp;1;      [&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span style="color=: #CC6600];">break[</color]span>;    &nbsp;&nbsp;&nbsp;&nbsp;}    previous&nbsp;&nbsp;&nbsp;&nbsp;previous[index] &nbsp;= reading&nbsp;reading;  &nbsp;&nbsp;}  [&nbsp;&nbsp;<span style="color=: #7E7E7E];">// return switch number (1 - 6)[</color]span>  [&nbsp;&nbsp;<span style="color=: #CC6600];">return[</color] span> (pressed);
}
[<span style="color=: #7E7E7E];">// Plays a full file from beginning to end with no pause&nbsp;Plays&nbsp;a&nbsp;full&nbsp;file&nbsp;from&nbsp;beginning&nbsp;to&nbsp;end&nbsp;with&nbsp;no&nbsp;pause.[</color]span>[<span style="color=: #CC6600];">void[</color] span> playcomplete([<span style="color=: #CC6600];">char[</color] span> *name) {  [&nbsp;&nbsp;<span style="color=: #7E7E7E];">// call our helper to find and play this name[</color]span>  playfile&nbsp;&nbsp;playfile(name);  [&nbsp;&nbsp;<span style="color=: #CC6600];">while[</color] span> (wave.isplaying) {  [&nbsp;&nbsp;<span style="color=: #7E7E7E];">// do nothing while its playing[</color]span>  &nbsp;&nbsp;}  [&nbsp;&nbsp;<span style="color=: #7E7E7E];">// now its done playing[</color]span>
}
[<span style="color=: #CC6600];">void[</color] span> playfile([<span style="color=: #CC6600];">char[</color] span> *name) {  [&nbsp;&nbsp;<span style="color=: #7E7E7E];">// see if the wave object is currently doing something[</color]span>  [&nbsp;&nbsp;<span style="color=: #CC6600];">if[</color] span> (wave.isplaying) {[<span style="color=: #7E7E7E];">// already playing something, so stop it![</color]span>    wave&nbsp;&nbsp;&nbsp;&nbsp;wave.[<span style="color=: #CC6600];">stop[</color]span>(); [<span style="color=: #7E7E7E];">// stop it[</color]span>  &nbsp;&nbsp;}  [&nbsp;&nbsp;<span style="color=: #7E7E7E];">// look in the root directory and open the file[</color]span>  [&nbsp;&nbsp;<span style="color=: #CC6600];">if[</color] span> (!f.open(root, name)) {    putstring&nbsp;&nbsp;&nbsp;&nbsp;putstring([<span style="color=: #006699];">"Couldn't open file "[</color]span>); [<span style="color=: #CC6600][;"><b]>Serial[</b][></color]span>.[<span style="color=: #CC6600];">print[</color]span>(name); [<span style="color=: #CC6600];">return[</color]span>;  &nbsp;&nbsp;}  [&nbsp;&nbsp;<span style="color=: #7E7E7E];">// OK read the file and turn it into a wave object[</color]span>  [&nbsp;&nbsp;<span style="color=: #CC6600];">if[</color] span> (!wave.create(f)) {    putstring_nl&nbsp;&nbsp;&nbsp;&nbsp;putstring_nl([<span style="color=: #006699];">"Not a valid WAV"[</color]span>); [<span style="color=: #CC6600];">return[</color]span>;  &nbsp;&nbsp;}  &nbsp;&nbsp;  [&nbsp;&nbsp;<span style="color=: #7E7E7E];">// ok time to play! start playback[</color]span>  wave&nbsp;&nbsp;wave.play();
}
[</quote]pre>
25
edits