The Program

To follow this discussion of the code, you should load the project into Visual Studio.

void ProgramStarted()
{
 usbHost.USBDriveConnected +=
  new UsbHost.USBDriveConnectedEventHandler(usbHost_USBDriveConnected);
 usbHost.USBDriveDisconnected +=
  new UsbHost.USBDriveDisconnectedEventHandler(usbHost_USBDriveDisconnected);
 sdCard.SDCardMounted +=
  new SDCard.SDCardMountedEventHandler(sdCard_SDCardMounted);
 sdCard.SDCardUnmounted +=
  new SDCard.SDCardUnmountedEventHandler(sdCard_SDCardUnmounted);
 ledSD.TurnRed();
 ledUSB.TurnRed();
}

The flow of the program is totally controlled by the connected and disconnected event handlers for the SD card and USB host modules:

void sdCard_SDCardMounted(SDCard sender, GT.StorageDevice storageDevice)
{
    ledSD.TurnGreen();
    sdStorageDevice = storageDevice;
    CheckAndTransfer();
}

The disconnected handlers set the appropriate LED to red and set the member variables used to refer to the storage devices of the SD Card and USB module to null.

Each of the connected handlers calls the method CheckAndTransfer after setting the LED to green and assigning the StorageDevice for the SD card to a member variable sdStorageDevice. This allows the storage devices to be referenced by the TransferFiles method, whichever event handler is calling it. The two storage device variables also act as flags to indicate whether the storage device is ready to attempt the file transfer process. It they are null, they are not ready:

private void CheckAndTransfer() { if (sdStorageDevice ...

Get Getting Started with .NET Gadgeteer now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.