Changes

Students/Ian Petrov

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