Main Site Documentation

RemovableMedia.Insert / Eject event


#1

Hi,

Can you help me getting the Insert / Eject event to work? Nothing appears in the output window when inserting or removing my SD-Card.

Regards


using System;
using System.Threading;
using System.IO.Ports;

using Microsoft.SPOT;
using Microsoft.SPOT.IO;
using Microsoft.SPOT.Hardware;

using GHIElectronics.NETMF.IO;
using GHIElectronics.NETMF.FEZ;

namespace MicroRemote
{
    public class Program
    {
        public static void Main()
        {
            RemovableMedia.Insert += new InsertEventHandler(RemovableMedia_Insert);
            RemovableMedia.Eject += new EjectEventHandler(RemovableMedia_Eject);

            Utility.Piezo(1000, 500);

            using (InputPort port = new InputPort((Cpu.Pin)FEZ_Pin.Digital.ButtonSelect, true, Port.ResistorMode.PullUp))
            {
                while (port.Read())
                    Thread.Sleep(100);
            }
        }

        static void RemovableMedia_Eject(object sender, MediaEventArgs e)
        {
            Debug.Print("eject");
        }

        static void RemovableMedia_Insert(object sender, MediaEventArgs e)
        {
            Debug.Print("insert");
        }
    }
}



#2

This is not supported for SD cards. You will need a thread that will check sd card availability and issue the events yourself.


#3

use this to detect if an SD card is inserted:


          while (!PersistentStorage.DetectSDCard())
          {
          Debug.Print("Please Insert SD card!");
          Thread.Sleep(1000);
          }


#4

I don’t think they have this for SD media… The GHI demo uses the persistent storage class and uses “detect media” to ascertain if a card is inserted or not… You could set up a class to poll the SD card and return the media status…

Cheers Ian

P.S. some cards are not detected… read the class help in the object browser.


#5

Ok, since I didn’t like the idea of spawning a thread for card detection I’ve worked out the following class by using InterruptPort. Maybe the InterruptPort spawns a thread anyway (and I think it does since you can enable the glitch-filter).


    /// <summary>
    /// Detects insertion / removal of SD-CARD for FEZ Cobra.
    /// Programmed by Huysentruit Wouter
    /// </summary>
    public class SdCard : IDisposable
    {
        #region Declarations

        private InterruptPort interrupt = new InterruptPort((Cpu.Pin)FEZ_Pin.Interrupt.IO33, true, Port.ResistorMode.PullUp, Port.InterruptMode.InterruptEdgeBoth);
        private PersistentStorage storage = null;
        private VolumeInfo volume = null;

        #endregion

        #region Construction / destruction

        public SdCard()
        {
            interrupt.OnInterrupt += new NativeEventHandler(sdDetectInterrupt_OnInterrupt);

            try
            {
                if (PersistentStorage.DetectSDCard())
                {
                    storage = new PersistentStorage("SD");
                    storage.MountFileSystem();
                    volume = new VolumeInfo("SD");
                }
            }
            catch (Exception)
            {
                storage = null;
                volume = null;
            }
        }

        public void Dispose()
        {
            interrupt.Dispose();

            ReleaseStorage();
        }

        #endregion

        #region Private methods

        private void ReleaseStorage()
        {
            lock (this)
            {
                if (storage != null)
                {
                    storage.UnmountFileSystem();
                    storage.Dispose();
                    storage = null;
                }
            }
        }

        private void sdDetectInterrupt_OnInterrupt(uint port, uint state, DateTime time)
        {
            // Read input again since the state argument seems unreliable
            if (interrupt.Read())
            {
                ReleaseStorage();

                OnEject(time);

                lock (this) volume = null;
            }
            else
            {
                if (PersistentStorage.DetectSDCard())
                {
                    lock (this)
                    {
                        storage = new PersistentStorage("SD");
                        storage.MountFileSystem();
                        volume = new VolumeInfo("SD");
                    }

                    OnInsert(time);
                }
            }
        }

        #endregion

        #region Exposed events

        /// <summary>
        /// Fired when inserting a valid SD card.
        /// </summary>
        public event InsertEventHandler Insert;
        private void OnInsert(DateTime time)
        {
            if (Insert != null)
                Insert(this, new MediaEventArgs(volume, time));
        }

        /// <summary>
        /// Fired when removing the SD card.
        /// </summary>
        public event EjectEventHandler Eject;
        private void OnEject(DateTime time)
        {
            if (Eject != null)
                Eject(this, new MediaEventArgs(volume, time));
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets the volume information of the SD card or null when no card was inserted.
        /// </summary>
        public VolumeInfo Volume
        {
            get { lock (this) return volume; }
        }

        /// <summary>
        /// Gets a value that indicates wheter or not the SD card is available.
        /// </summary>
        public bool Available
        {
            get { lock (this) return storage != null; }
        }

        #endregion
    }


#6

@ Wouter this looks pretty good. Would you mind if I threw a modified version this into Pyxis?


#7

Maybe on fezzer.com too


#8

No problem, go ahead. That Pyxis looks nice. Too bad I didn’t ordered a LCD with my Fez Cobra.


#9

Thanks, I’ll be sure to credit you in the code


#10

Wouter Huysentruit … I cannot find any other way to contact you.

Sorry …
But on your Fezzer Sunrise/Sunset calculation there are problems:

#1. The source code on download is lock-out eg classes are greyed out.
#2. The sunrise and sunset times are reversed. It is not the print statement in your example but comes from the source code greyed out.
#3. The sunrise if off by 8 minutes and the sunset time is off by 11 minutes by using several on-line web-based sunrise/sunset calculators.
:o :o :o