The new Raspberry Pi B Model B+ is here - Pick one up in the store and check our detailed guide!

read/write SD files with WaveHC + SdFat

by botbilder on Fri Jan 07, 2011 6:26 pm

I have my Wave shield running great with my Uno using the WaveHC library, playing sound clips. But now I need to write some logging data to SD files in addition to reading -- not at the same time, of course.

Has anyone else faced this problem?

I thought of using EEPROM for logging, but I'll probably need more than 1K of data, and I like the convenience of having it on the SD card.

The WaveHC library uses FatReader and friends, which do not support writing. The SdFat library supports read/write, but it conflicts with several definitions in WaveHC -- different version of FatStructs.h, duplicate FreeRam(), etc.

I tried hacking WaveHC to co-exist with the built-in "SD" library new in Arduino version 22, which is a simplified wrapper around SdFat. In my sketch I used FatReader to play WAV files, and SdFile to write log files. I got it to compile and run, but it seems very inefficient to duplicate SD classes in flash memory. I had to reduce the play buffer size to 256 bytes just to have enough RAM to run.

I'm thinking it would be better to make a new version of WaveHC that uses the SdFat classes (like Sd2Card, SdVolume, and SdFile) natively for reading, rather than its own classes SdReader, FatVolume, and FatReader. I'm curious if anyone has gone through this exercise before.
botbilder
 
Posts: 18
Joined: Thu Dec 09, 2010 1:07 am

Re: read/write SD files with WaveHC + SdFat

by adafruit on Fri Jan 07, 2011 7:13 pm

check out our data logging shield and tutorials, they'll show you how to read and write with an SDfat library!
User avatar
adafruit
 
Posts: 11634
Joined: Thu Apr 06, 2006 3:21 pm
Location: nyc

Re: read/write SD files with WaveHC + SdFat

by botbilder on Fri Jan 07, 2011 7:41 pm

Thanks!

I can see how to use SdFat for read/write. I think my problem is more about how to use the interrupt-driven Wave-playing code in the WaveHC lib along with SdFat, since the two libs are somewhat incompatible.

I'll make an attempt at converting WaveHC to use SdFat and see how it goes.
botbilder
 
Posts: 18
Joined: Thu Dec 09, 2010 1:07 am

Re: read/write SD files with WaveHC + SdFat

by botbilder on Sat Jan 08, 2011 6:00 am

OK, I've got it working now a couple of different ways with the SD/SdFat lib in Arduino 22 on an Uno. Here's what I did in case other people find this information useful.

Option 1: Tweaked the WaveHC lib just enough so it could co-exist with SD lib. Used the SdFat version of FatStructs.h, fixed the DIR_IS_* macros/funcs, and removed duplicate defs of FreeRam and other funcs in WaveUtil.h. My sketch uses the normal SdReader classes for reading/playing WAV files, and the SD "File" class for writing log files.

Option 2: Made a new lib called "WaveSD" based on WaveHC. Ripped out FatStructs.h, WaveUtil.*, and the SdReader/FatReader/FatVolume classes, leaving only WaveSD.* files, *Pin*.h, and mpcdac.h. Changed all the WAV-playing code to use the equivalent classes in SdFat. My sketch uses SdFile both for reading/playing WAV files and writing log files.

To gauge the impact of each option, I made two versions of the "daphc" example sketch, and measured the compiled size and initial FreeRam() readings. To maximize RAM, I changed the WAV PLAYBUFFLEN to 256 bytes rather than 512, and I changed the RX_BUFFER_SIZE in HardwareSerial.cpp to 16 instead of 128.

Results:

Option 1: daphc sketch, size=14686, freeram=527

Option 2: daphc sketch, size=15274, freeram=674

For comparison, the original daphc sketch with the original WaveHC lib: size=9554, freeram=1314.

Since I care most about free RAM, I'm going with option 2.
Last edited by botbilder on Sun Jan 09, 2011 10:26 pm, edited 1 time in total.
botbilder
 
Posts: 18
Joined: Thu Dec 09, 2010 1:07 am

Re: read/write SD files with WaveHC + SdFat

by adamharv on Sat Jan 08, 2011 8:02 pm

I'm running into a similar conflict while trying to interface the Wave Shield and a serial camera with an SD card. Instead of using the WaveHC library, I'm using WaveRP. But the conflicts sound the same. I've been trying to modify the WaveRP lib, to work with the SD/SdFat lib, but it's a little over my head still. Would you mind sharing your code for this? Would be really helpful. It would be great if the Wave Shield functionality could be compatible with the newer SD/SdFat libraries. Is there an updated version of the waverp lib? I'm using the code from here ( http://code.google.com/p/waverp/ ) which was last updated in 2009.
adamharv
 
Posts: 3
Joined: Sun Dec 05, 2010 5:49 pm

Re: read/write SD files with WaveHC + SdFat

by botbilder on Sat Jan 08, 2011 9:21 pm

I took a look at WaveRP, and it had a few of the same issues as WaveHC. I'm guessing it was built with a slightly older version of SdFat, so the changes were minor.

See attached file "WaveRP.zip". I got it to compile with V22 SD/SdFat, but didn't test it since I don't have the recording circuit.

If you have the latest Arduino 22 software (from http://www.arduino.cc/en/Main/Software), it includes the SdFat lib inside the "utility" folder of the SD lib. To avoid potential conflicts, I made sure to remove my copy of the SdFat folder from the "libraries" subdir of my sketch folder.

To test this modded WaveRP, replace your current copy of WaveRP with this folder, move "WaveRecordPlay.pde" to your sketch folder, and it should compile. From there you should be able to figure out how to mod your project by looking at the diffs in WaveRecordPlay, like changing "freeRam()" to "FreeRam()" and changing around the #includes at the top of the file.

Good luck!
Attachments
WaveRP.zip
mods to WaveRP to work with SD/SdFat lib in Arduino 22
(12.92 KiB) Downloaded 125 times
botbilder
 
Posts: 18
Joined: Thu Dec 09, 2010 1:07 am

Re: read/write SD files with WaveHC + SdFat

by fat16lib on Mon Jan 10, 2011 12:25 pm

I have been planing to update WaveRP for newer versions of SdFat. I started developing the new version of WaveRP to support a new Adafruit product. That product is delayed so I am posting a beta of WaveRP here.

Please try it and let me know about problems. My email address is in the ReadMe file.
Attachments
waverp_beta.zip
(348.75 KiB) Downloaded 118 times
fat16lib
 
Posts: 593
Joined: Wed Dec 24, 2008 12:54 pm

Re: read/write SD files with WaveHC + SdFat

by frsc on Sat Aug 13, 2011 7:09 am

Hi,

I'm facing the same problem as botbuilder and adamharv but I do not need the recording features of WaveRP.
Botbuilder would you mind sharing your WaveSD lib and an according example sketch? That would be great as it saves me a lot of work.
Thank you in advance.

frsc
frsc
 
Posts: 2
Joined: Sat Aug 13, 2011 7:03 am

Re: read/write SD files with WaveHC + SdFat

by botbilder on Sat Aug 13, 2011 5:20 pm

Here you go.

Extract the "WaveSD" folder and sub-folders, and put it in your Arduino\libraries folder. You may have to create the "libraries" folder if it doesn't exist.

You'll also need the "SdFat" library in your "libraries" folder.

There are two sample sketches in the WaveSD "examples" folder:

1) daphc_rewrite is just the normal daphc changed to work with WaveSD
2) rw_test shows how you can play a sound file and write to a log file (not at the same time, of course)
Attachments
WaveSD.zip
WaveSD lib with two sample sketches
(11.68 KiB) Downloaded 152 times
botbilder
 
Posts: 18
Joined: Thu Dec 09, 2010 1:07 am

Re: read/write SD files with WaveHC + SdFat

by frsc on Sun Aug 21, 2011 7:12 am

Thanks a lot. This took me a large step further.
frsc
 
Posts: 2
Joined: Sat Aug 13, 2011 7:03 am

Re: read/write SD files with WaveHC + SdFat

by julym on Fri Jan 13, 2012 5:58 pm

Botbilder, I was using the WaveSD library with the new SdFat libray but I am getting the following errors when I try to run the included examples:

For the rw_test example I get the following error: class Sd2Card' has no member named 'readEnd
For the daphc_rewrite example I am getting the following error: no matching function for call to 'SdFile::printDirName(dir_t&, int)'

Could you post the SdFat Library that you used with the WaveSD Library please.


thanks
julym
 
Posts: 2
Joined: Fri Jan 13, 2012 5:52 pm

Re: read/write SD files with WaveHC + SdFat

by botbilder on Fri Jan 13, 2012 7:02 pm

Looks like the latest SDFat library has some changes that are incompatible with the version I used, which was from 10/10/10. Here's the direct link to download that version.
botbilder
 
Posts: 18
Joined: Thu Dec 09, 2010 1:07 am

Re: read/write SD files with WaveHC + SdFat

by julym on Sat Jan 14, 2012 2:13 pm

Thanks a lot.
julym
 
Posts: 2
Joined: Fri Jan 13, 2012 5:52 pm

Re: read/write SD files with WaveHC + SdFat

by didi on Thu Aug 16, 2012 1:52 am

Hello,

Thanks for sharing this code!

I have a little question:
What should be changed in the code of WaveSD and SDFat in order to get it work with Arduino 1.0?
It works fine on arduino 0022, but my original project code doesn't. So instead of trying to recode the whole project to 0022, I was wondering whereas it was possible to adapt the WaveSD and SDFat lib.to make it arduino 1.0 compliant.

Thanks in Advance!
didi
 
Posts: 1
Joined: Thu Aug 16, 2012 1:35 am

Re: read/write SD files with WaveHC + SdFat

by botbilder on Thu Aug 16, 2012 3:29 am

It looks like the SDFat library has been updated to work with Arduino 1.0 -- downloads are here. No idea about WaveSD.
botbilder
 
Posts: 18
Joined: Thu Dec 09, 2010 1:07 am