Arduino Forum

Using Arduino => Installation & Troubleshooting => Topic started by: pi3orionis on Jul 18, 2011, 11:47 pm

Title: Bug in SD library? File.available() issue
Post by: pi3orionis on Jul 18, 2011, 11:47 pm
Hello,

Hopefully this is the right forum for this... I'm using an Arduino Uno paired with SparkFun's MP3 player shield. The File.available() function in the SD library for Arduino 022 seems to be reporting spurious information, at least for me. Originally in my loop, I had it walk through reading files off the SD card in 32 byte chunks and using available() to track how many bytes remained in the file. But after much confusion and serial output debugging, I was able to nail down that available() was not reporting accurately. It would decrement properly with each read() but it never started in the right place! For one of my files it always reported it as only 7734 bytes long before the first read() (which is a tenth of my file's size) and for the second file it consistently reported a negative number.

File.size() works just fine for me. I rewrote my code to use that function and then track how many bytes I read out of the file within my sketch. So my program is running great now (and actually playing the whole file out through audio when it should). But I figured I should report this. could this something going wrong that's unique to my SD card? (I know those can be quirky sometimes.) Or is this an actual bug in Arduino's SD library?

Thanks
Title: Re: Bug in SD library? File.available() issue
Post by: smeezekitty on Jul 19, 2011, 12:50 am
Sounds like its overflowing. According to the doc, it returns an int which will overflow at 32767 or 65535 depending if its signed or unsigned.
Title: Re: Bug in SD library? File.available() issue
Post by: pi3orionis on Jul 19, 2011, 04:12 pm
Ah. Yeah that would explain it. I wonder why it isn't coded to use long instead of int? Oh well. Like I said, size() works fine and it only takes a little bit of extra code to rely on that function instead.
Title: Re: Bug in SD library? File.available() issue
Post by: robtillaart on Jul 19, 2011, 05:30 pm

Reported as issue - http://code.google.com/p/arduino/issues/detail?id=571 -

Title: Re: Bug in SD library? File.available() issue
Post by: pi3orionis on Jul 19, 2011, 05:46 pm
Cool, thanks for logging that! I suppose I should have thought about finding and putting that in some sort of bug reporting repository. At least it looks like the fix will be easy enough for the next patch.