At first I was stumped by the RemovableMedia Insert and Eject events, until I read here that they don’t get fired until the filesystem is mounted and unmounted. So I decided to go ahead with the example provided, and to use a thread to poll via the PersistentStorage.DetectSDCard() method.
I’m having all kinds of problems with that approach. While it seems to work once, provided that the SD is inserted before I start the program, it does not work when I am running and try to remove/reinsert multiple times.
I’ve attached my code below – can anyone see something that I’m doing that’s obviously wrong?
using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using GHIElectronics.NETMF.FEZ;
using GHIElectronics.NETMF.IO;
using Microsoft.SPOT.IO;
using System.IO;
namespace FezSdXmlTest
{
public class Program
{
public static void Main()
{
SdTest test = new SdTest();
test.Run();
// these events don't work as one might expect
RemovableMedia.Insert += new InsertEventHandler(RemovableMedia_Insert);
RemovableMedia.Eject += new EjectEventHandler(RemovableMedia_Eject);
while( true) {
Thread.Sleep( 100);
}
}
/// <summary>
/// Doesn't work!
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
static void RemovableMedia_Eject(object sender, MediaEventArgs e)
{
}
/// <summary>
/// Doesn't work!
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
static void RemovableMedia_Insert(object sender, MediaEventArgs e)
{
}
public class SdTest
{
PersistentStorage sd = null;
public void Run()
{
RemovableMedia.Insert += new InsertEventHandler(RemovableMedia_Insert);
RemovableMedia.Eject += new EjectEventHandler(RemovableMedia_Eject);
new Thread( SdMountThread).Start();
}
void SdMountThread()
{
while( true) {
try {
bool sd_exists = PersistentStorage.DetectSDCard();
if( sd_exists) {
Thread.Sleep( 50);
sd_exists = PersistentStorage.DetectSDCard();
}
if( sd_exists && sd == null) {
Debug.Print( "SD card inserted -- mounting SD filesystem");
sd = new PersistentStorage( "SD");
Debug.Print( "Mounting filesystem");
sd.MountFileSystem();
} else if( !sd_exists && sd != null) {
Debug.Print( "SD card removed -- unmounting filesystem");
try {
sd.UnmountFileSystem();
} catch( Exception) {
Debug.Print( "SD filesystem not unmounted cleanly");
}
sd.Dispose();
sd = null;
}
} catch( Exception ex) {
Debug.Print( ex.Message);
if( sd != null) {
sd.Dispose();
sd = null;
}
}
Thread.Sleep( 100);
}
}
/// <summary>
/// Doesn't work as expected -- gets fired after we unmount the filesystem
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void RemovableMedia_Eject(object sender, MediaEventArgs e)
{
Debug.Print( "RemovableMedia Eject event fired");
/*
try {
sd.UnmountFileSystem();
} catch( Exception ex) {
Debug.Print( "Could not unmount filesystem: " + ex.Message);
}
*/
}
/// <summary>
/// Doesn't work as expected
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void RemovableMedia_Insert(object sender, MediaEventArgs e)
{
Debug.Print( "RemovableMedia Insert event fired");
try {
if( VolumeInfo.GetVolumes()[0].IsFormatted) {
Debug.Print("Available folders:");
string[] strs = Directory.GetDirectories(e.Volume.RootDirectory);
for (int i = 0; i < strs.Length; i++)
Debug.Print(strs[i]);
Debug.Print("Available files:");
strs = Directory.GetFiles(e.Volume.RootDirectory);
for (int i = 0; i < strs.Length; i++)
Debug.Print(strs[i]);
} else {
Debug.Print( "SD card is not formatted");
}
} catch( Exception ex) {
Debug.Print( "Could not mount filesystem: " + ex.Message);
}
}
}
}
}