Mouse connected event not firing alongside SD card event

Hi,

I just wondered if anyone has had similar issues or if it’s a problem with my code?

When I have the SD card module included (as in the following code), the SDcard.mounted event will fire, but the mouse will only fire if I disconnect and reconnect the mouse.

When I don’t have the SD card module and the SD card event the mouse.connected event fires fine without re-inserting the mouse.

I’ve had a similar issue before so just wanted to clear this up. Also would there be a way to manually fire this event?

Thanks in advance for any help with this.

using System;
using System.Collections;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Touch;

using Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using Gadgeteer.Modules.GHIElectronics;
using Gadgeteer.Modules.Seeed;

using GHIElectronics.NETMF.USBHost;
using GHIElectronics.NETMF.USBClient;
using GHIElectronics.NETMF.System;

namespace ScreenMovement
{
    public partial class Program
    {

        USBH_Mouse mouse;

    //    float Scale = 0;  // set mouse scale and sensitivity

        static int Xmin = 0; // set the boundaries according to the image size
        static int Ymin = 0;

        int Xmax;
        int Ymax;

        int x; // mouse x coordinate
        int y; // mouse y coordinate

        GT.StorageDevice sd;

        int screenWidth;
        int screenHeight;
        
        Bitmap picture;

        void ProgramStarted()
        {
            Debug.Print("Program Started");

            screenWidth = (int)oledDisplay.Width;
            screenHeight = (int)oledDisplay.Height;

            sdCard.SDCardMounted += new SDCard.SDCardMountedEventHandler(sdCard_SDCardMounted);
        }

        void sdCard_SDCardMounted(SDCard sender, GT.StorageDevice SDCard)
        {
            Debug.Print("Card Mounted");

            oledDisplay.SimpleGraphics.DisplayRectangle(GT.Color.Red, 0, GT.Color.Red, 0, 0, (uint)screenWidth, (uint)screenHeight);

            sd = SDCard;

            string fileName = "picture_1.jpg"; // file name

            picture = sd.LoadBitmap(fileName, Bitmap.BitmapImageType.Jpeg);

            oledDisplay.SimpleGraphics.DisplayRectangle(GT.Color.Green, 0, GT.Color.Green, 0, 0, (uint)screenWidth, (uint)screenHeight);

            Debug.Print("Picture Loaded");

            usbHost.MouseConnected += new UsbHost.MouseConnectedEventHandler(usbHost_MouseConnected);
         }

        void usbHost_MouseConnected(UsbHost sender, USBH_Mouse mouse)
        {
            Debug.Print("Mouse Connected");

            mouse.SetCursor(picture.Width / 2, picture.Height / 2); // center the mouse to begin with

            this.mouse = mouse;

            mouse.MouseMove += new USBH_MouseEventHandler(mouse_MouseMove);

            showImage();
        }

        void showImage() // bring image up to start without the event
        {
            x = picture.Width / 2;
            y = picture.Height / 2;

            scrollImage(x, y);
        }

        void mouse_MouseMove(USBH_Mouse sender, USBH_MouseEventArgs args)
        {

   //         mouse.Scale(Scale);

            Xmax = picture.Width - screenWidth;
            Ymax = picture.Height - screenHeight;

            mouse.SetCursorBounds(Xmin, Xmax, Ymin, Ymax);

            Debug.Print(mouse.Cursor.X.ToString() + " , " + mouse.Cursor.Y.ToString());

            x = mouse.Cursor.X;
            y = mouse.Cursor.Y;

            scrollImage(x, y);
        }

        int scrollImage(int x, int y)
        {
          
            Bitmap croppedImage = new Bitmap(128, 128);

            croppedImage.DrawImage(0, 0, picture, x, y, screenWidth, screenHeight);

            oledDisplay.SimpleGraphics.DisplayImage(croppedImage, 0, 0);

            croppedImage.Clear();

            return (0);

        }


    }
}

I would have thought that you’d want your definition of the mouse connected event to be in ProgramStarted not in SD card mounted ? It seems really weird (and counter-intuitive) to me to have it defined where it is. Theoretically, if you never fire the SD card mount code, the mouse handler will never be linked - is that a good thing? Even if the SD card isn’t mounted, wouldn’t it be good to know that the mouse has or has not connected and whether it’s moved - even if you do nothing with the fact.

Your right Brett - that makes more sense. But it’s not a solution to the un-firing event problem.

In any order I put them the mouse connected event will not fire with the SD card? Is there a solution or a way to access USBH_Mouse mouse without the event?

I’ve had a similar issue with the camera and SD card previously.

Thanks.