I don't like working with SD cards...

A project log for OPL Studio - OPL2 / OPL3 MIDI Interface

a MIDI interface for the OPL2 Audio Board and OPL3 Duo!

Maarten JanssenMaarten Janssen 05/16/2021 at 11:530 Comments

The title says it al, I don't like working with SD cards and the Arduino SD library. It's alwyas like Russian Roulette whether a card will initialize or not. I've spend too much time messing arounds with different SD cards, inserting them, removing them, reformatting, trying different breakout boards, rewiring and I could go on. Eventually I'm blaming it on my breadboard and all the long wires flapping around. I hope that when I build this on a real PCB things will be more stable.

So the file chooser dialog box is here! It was a lot of work to get this one implemented. I restared the controller code for the SD card 3 times before I had something that was performing good enough and would not claim all of the Teensy's memory. 

Initially I though to walk through the directory on the SD card and return file properties to the UI when it needed to render. This prooved to be really, really slow! Another problem was that the FAT file system is index based so my files and folders would be read in the order I had put them on the card. What I wanted was to show the files sorted in a natural way, folders first then files sorted alphabetically and respecting numbers. This meant reading out the contents of a directory and storing all the entries in a large String array and sorting it. To keep an eye on memory usage I limited to number of entries per directory to 255 and I'm only using the DOS 8+3 filenames. If memory allows it maybe I will allow for more files and long filenames later...

I'm also allowing to filter on specific filetype(s) when reading a directory to make it more easy to find files and to not run into this 255 entry limit, hopefully. After retrieving the filtered items from the folder I'm sorting the array of item names multiple times (Bubble sort FTW cause I'm too lazy) before the files are rendered in the file chooser.

This process can take several seconds if you have a folder with many items so while the directory contents are read I show an hourglass icon to indicate that the UI is blocked. This is also where I still need to add the MIDI update code so it will not appear to hang.

I happy with how the file chooser turned out. I've started to put it to use in the Patch Editor module where I'm now able to load the first type of instrument bank: The Adlib BNK file. I's already great to be able to switch from the default instruments to another set but I will add support for more file types and also saving instrument patches I still need to start on.