/*********************************************************************
* Function: void APP_HostMSDDataLoggerTasks(void);
*
* Overview: Keeps the demo running.
*
* PreCondition: The demo should have been initialized via
*   the APP_HostMSDDataLoggerInitialize()  
*
* Input: None
*
* Output: None
*
********************************************************************/
void APP_HostMSDDataLoggerTasks()
{
    if(FILEIO_MediaDetect(&gUSBDrive, &deviceAddress) == false)
    {
        //The device has been removed.  Now we should wait for a new
        //  device to be attached.
        demoState = WAITING_FOR_ATTACH;
    }
    
    switch( demoState)
    {
        case WAITING_FOR_ATTACH:
            break;

        case MOUNTING_DRIVE:
        {           
            // Attempt to mount the drive described by gUSBDrive as drive 'A'
            // The deviceAddress parameter describes the USB address of the device; it is initialized by the application in the 
            // USB_ApplicationEventHandler function when a new device is detected.
            if( FILEIO_DriveMount ('A', &gUSBDrive, &deviceAddress) == FILEIO_ERROR_NONE)
            {
                demoState = OPENING_FILE;
            }
            break;
        }

        case OPENING_FILE:
            // Opening a file with the FILEIO_OPEN_WRITE option allows us to write to the file.
            // Opening a file with the FILEIO_OPEN_CREATE file will create the file if it does not already exist.
            // Opening a file with the FILEIO_OPEN_TRUNCATE file will truncate it to a 0-length file if it already exists.
            if(FILEIO_Open(&myFile, "LOG.CSV", FILEIO_OPEN_WRITE | FILEIO_OPEN_CREATE | FILEIO_OPEN_TRUNCATE) == FILEIO_RESULT_SUCCESS)
            {
                //Opening the file failed.  Since we can't write to the
                //  device, abort the write attempt and wait for the device
                //  to be removed.
                demoState = WRITING_TO_FILE;

                blinkCount = 0;
                sampleRequested = false;
                TIMER_RequestTick(&APP_HostMSDDataLoggerTickHandler, 50);
                LED_On(LED_USB_HOST_MSD_DATA_LOGGER);
                ADC_ChannelEnable(ADC_USB_HOST_MSD_DATA_SOURCE);
                break;
            }
            break;

        case WRITING_TO_FILE:
            if(sampleRequested == true)
            {
                uint16_t  adcResult;
                int charCount;

                sampleRequested = false;
                
                adcResult = ADC_Read10bit(ADC_USB_HOST_MSD_DATA_SOURCE);

                charCount = sprintf(printBuffer, "%d\r\n", adcResult);
                
                //Write some data to the new file.
                FILEIO_Write(printBuffer, 1, charCount, &myFile);
            }

            if(BUTTON_IsPressed(BUTTON_USB_HOST_MSD_DATA_LOGGER) == true)
            {
                demoState = CLOSING_FILE;
            }
            break;

        case CLOSING_FILE:
            //Always make sure to close the file so that the data gets
            //  written to the drive.
            FILEIO_Close(&myFile);
            TIMER_CancelTick(&APP_HostMSDDataLoggerTickHandler);

            demoState = UNMOUNT_DRIVE;
            break;

        case UNMOUNT_DRIVE:
            // Unmount the drive since it is no longer in use.
            FILEIO_DriveUnmount ('A');

            //Now that we are done writing, we can do nothing until the
            //  drive is removed.
            demoState = WAITING_FOR_DETACH;
            break;

        case WAITING_FOR_DETACH:
            LED_Off(LED_USB_HOST_MSD_DATA_LOGGER);
            break;

        default:
            break;
    }
}
/*********************************************************************
* Function: void APP_HostMSDSimpleTasks(void);
*
* Overview: Keeps the demo running.
*
* PreCondition: The demo should have been initialized via
*   the APP_HostMSDSimpleInitialize()  
*
* Input: None
*
* Output: None
*
********************************************************************/
void APP_HostMSDSimpleTasks()
{
    switch(demoState)
    {
        case WAITING_FOR_ATTACH:
            break;

        case WRITING_TO_DEVICE:
            if(FILEIO_MediaDetect(&gUSBDrive, &deviceAddress) == true)
            {
                //Now that we've found a device, we can try to write to it.
                demoState = WRITING_TO_DEVICE;
            }
            else
            {
                break;
            }

            // Attempt to mount the drive described by gUSBDrive as drive 'A'
            // The deviceAddress parameter describes the USB address of the device; it is initialized by the application in the 
            // USB_ApplicationEventHandler function when a new device is detected.
            if(FILEIO_DriveMount ('A', &gUSBDrive, &deviceAddress) == FILEIO_ERROR_NONE)
            {
                // Opening a file with the FILEIO_OPEN_WRITE option allows us to write to the file.
                // Opening a file with the FILEIO_OPEN_CREATE file will create the file if it does not already exist.
                // Opening a file with the FILEIO_OPEN_TRUNCATE file will truncate it to a 0-length file if it already exists.
                if(FILEIO_Open(&myFile, "TEST.TXT", FILEIO_OPEN_WRITE | FILEIO_OPEN_CREATE | FILEIO_OPEN_TRUNCATE) == FILEIO_RESULT_FAILURE)
                {
                    //Opening the file failed.  Since we can't write to the
                    //  device, abort the write attempt and wait for the device
                    //  to be removed.
                    demoState = WAITING_FOR_DETACH;
                    break;
                }

                //Write some data to the new file.
                FILEIO_Write("This is a test.", 1, 15, &myFile);

                //Always make sure to close the file so that the data gets
                //  written to the drive.
                FILEIO_Close(&myFile);

                // Unmount the drive since it is no longer in use.
                FILEIO_DriveUnmount ('A');

                //Now that we are done writing, we can do nothing until the
                //  drive is removed.
                demoState = WAITING_FOR_DETACH;
            }
            break;

        case WAITING_FOR_DETACH:
            if(FILEIO_MediaDetect(&gUSBDrive, &deviceAddress) == false)
            {
                //The device has been removed.  Now we should wait for a new
                //  device to be attached.
                demoState = WAITING_FOR_ATTACH;
            }
            break;
    }
}