⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mavusb.cpp

📁 基于EP7312的MP3播放器源代码,包括MCU和PC端代码.
💻 CPP
📖 第 1 页 / 共 5 页
字号:
            //
            // If this is a blank line then ignore it.
            //
            if(pcBuffer[ulOffset] == '\0')
            {
                continue;
            }
        }

        //
        // Otherwise, this is a PLS play list.
        //
        else
        {
            //
            // If this line does not start with "File" then ignore it.
            //
            if((pcBuffer[ulOffset] != 'F') ||
               (pcBuffer[ulOffset + 1] != 'i') ||
               (pcBuffer[ulOffset + 2] != 'l') ||
               (pcBuffer[ulOffset + 3] != 'e'))
            {
                continue;
            }

            //
            // Following "File" must be a sequence of digits.
            //
            for(ulOffset += 4; pcBuffer[ulOffset]; ulOffset++)
            {
                //
                // Stop looking at this line if this character is not a digit.
                //
                if((pcBuffer[ulOffset] < '0') || (pcBuffer[ulOffset] > '9'))
                {
                    break;
                }
            }

            //
            // Following the digits must be a '='; if not, then ignore this
            // line.
            //
            if(pcBuffer[ulOffset] != '=')
            {
                continue;
            }

            //
            // Skip the '='.
            //
            ulOffset++;
        }

        //
        // Download this file.
        //
        Download(hWnd, pcBuffer + ulOffset);
    }

    //
    // Free the memory buffer.
    //
    GlobalFree(pcBuffer);

    //
    // Close the host file.
    //
    CloseHandle(hFile);

    //
    // Success.
    //
    return(TRUE);
}

//****************************************************************************
//
// Determines how to download the specified file.
//
//****************************************************************************
BOOL
DoDownload(HWND hWnd, char *pcFileName)
{
    unsigned long ulLength;

    //
    // Get the length of the file name.
    //
    ulLength = strlen(pcFileName);

    //
    // See if the name of this file ends in ".m3u".
    //
    if((pcFileName[ulLength - 4] == '.') &&
       (pcFileName[ulLength - 3] == 'm') &&
       (pcFileName[ulLength - 2] == '3') &&
       (pcFileName[ulLength - 1] == 'u'))
    {
        //
        // Download the files specified in this M3U play list.
        //
        return(DownloadPlayList(hWnd, pcFileName, TRUE));
    }

    //
    // See if the name of this file ends in ".pls".
    //
    else if((pcFileName[ulLength - 4] == '.') &&
            (pcFileName[ulLength - 3] == 'p') &&
            (pcFileName[ulLength - 2] == 'l') &&
            (pcFileName[ulLength - 1] == 's'))
    {
        //
        // Download the files specified in this PLS play list.
        //
        return(DownloadPlayList(hWnd, pcFileName, FALSE));
    }

    //
    // Otherwise, this is a media file.
    //
    else
    {
        //
        // Simply download all other files.
        //
        return(Download(hWnd, pcFileName));
    }
}

//****************************************************************************
//
// The thread which transfers a file from the Internet audio player.
//
//****************************************************************************
DWORD WINAPI
UploadThread(void *pvParameter)
{
    unsigned long ulWrote, ulSize;

    //
    // Read data from the device file and write it to the host file.
    //
    while(1)
    {
        //
        // Read data from the device file.
        //
        ulSize = Maverick_Read(g_pvData, 1024 * 1024);
        if(!ulSize)
        {
            break;
        }

        //
        // Write data to the host file.
        //
        WriteFile(g_hFile, g_pvData, ulSize, &ulWrote, NULL);
    }

    //
    // Close the file on the device.
    //
    Maverick_Close();

    //
    // Close the host file.
    //
    CloseHandle(g_hFile);

    //
    // Free the file data buffer.
    //
    GlobalFree(g_pvData);

    //
    // Success.
    //
    return(TRUE);
}

//****************************************************************************
//
// The dialog box procedure for the upload progress indicator.
//
//****************************************************************************
BOOL APIENTRY
UploadProgressDlgProc(HWND hDlg, UINT message, UINT wParam, LONG lParam)
{
    //
    // Determine what to do based on the message we received.
    //
    switch(message)
    {
        //
        // The dialog is being initialized.
        //
        case WM_INITDIALOG:
        {
            char pcMessage[256];

            //
            // Schedule a WM_TIMER message to be sent to ourself in 100ms.
            //
            SetTimer(hDlg, 0, 100, NULL);

            //
            // Format the upload message in the dialog box.
            //
            wsprintf(pcMessage, "Uploading \"%s\"", g_pcFileName);

            //
            // Put the download message in the appropriate field of the dialog
            // box.
            //
            SetDlgItemText(hDlg, IDC_FILENAME, pcMessage);

            //
            // Indicate that the first control in the dialog box should receive
            // the keyboard focus.
            //
            return(TRUE);
        }

        //
        // A command button or menu item was selected.
        //
        case WM_TIMER:
        {
            DWORD dwExitCode;
            unsigned long ulCount;

            //
            // Get the exit code from the upload thread.
            //
            GetExitCodeThread(g_hThread, &dwExitCode);

            //
            // See if the upload thread is still active.
            //
            if(dwExitCode != STILL_ACTIVE)
            {
                //
                // The upload thread has completed, so dismiss the progress
                // dialog box.
                //
                EndDialog(hDlg, TRUE);

                //
                // We've handled this message.
                //
                return(TRUE);
            }

            //
            // Get the current count of bytes transferred from the Maverick
            // USB driver.
            //
            ulCount = Maverick_GetTransferCount();

            //
            // Set the position of the progress indicator based on the number
            // of bytes transferred.
            //
            SendMessage(GetDlgItem(hDlg, IDC_PROGRESS), PBM_SETPOS,
                        (ulCount * 100) / g_ulLength, 0);

            //
            // Schedule another WM_TIMER message to be sent in 100ms.
            //
            SetTimer(hDlg, 0, 100, NULL);

            //
            // We're done handling this message.
            //
            return(TRUE);
        }
    }

    //
    // Indicate that we did not handle the message.
    //
    return(FALSE);
}

//****************************************************************************
//
// Uploads a file from the Internet audio player.
//
//****************************************************************************
BOOL
Upload(HWND hWnd, char *pcFileName, char *pcDeviceName)
{
    DWORD dwThreadID;
    unsigned short pusDeviceName[128];

    //
    // Allocate memory for the file data.
    //
    g_pvData = GlobalAlloc(GMEM_FIXED, 1024 * 1024);
    if(!g_pvData)
    {
        return(FALSE);
    }

    //
    // Convert the file name from ASCII to Unicode.
    //
    MultiByteToWideChar(CP_ACP, 0, pcDeviceName, strlen(pcDeviceName) + 1,
                        pusDeviceName, 128);

    //
    // Open the file on the device.
    //
    if(Maverick_Open(g_ulDriveNum, pusDeviceName, 1) == 0)
    {
        GlobalFree(g_pvData);
        return(FALSE);
    }

    //
    // Create the host file.
    //
    g_hFile = CreateFile(pcFileName, GENERIC_READ | GENERIC_WRITE, 0, NULL,
                         CREATE_NEW, 0, NULL);
    if(g_hFile == INVALID_HANDLE_VALUE)
    {
        GlobalFree(g_pvData);
        Maverick_Close();
        return(FALSE);
    }

    //
    // Get the length of the file.
    //
    g_ulLength = Maverick_Length();

    //
    // Save the name of the device file.
    //
    g_pcFileName = pcDeviceName;

    //
    // Reset the transfer count in the Maverick USB driver.
    //
    Maverick_ResetTransferCount();

    //
    // Create the thread which will actually upload the data from the device.
    //
    g_hThread = CreateThread(NULL, 0, UploadThread, NULL, 0, &dwThreadID);

    //
    // Create the progress indicator dialog box.
    //
    DialogBox(g_hInstance, MAKEINTRESOURCE(IDD_PROGRESS), hWnd,
              UploadProgressDlgProc);

    //
    // We've successfully uploaded the file.
    //
    return(TRUE);
}

//****************************************************************************
//
// The thread which updates the software on the Internet audio player.
//
//****************************************************************************
DWORD WINAPI
UpdateThread(void *pvParameter)
{
    DWORD *pdwResult;
    unsigned long ulRead;

    //
    // Convert the parameter in a usable pointer.
    //
    pdwResult = (DWORD *)pvParameter;

    //
    // Create the "file" for the new code image.
    //
    if(Maverick_Create(0, L"", g_ulLength) == 0)
    {
        GlobalFree(g_pvData);
        CloseHandle(g_hFile);
        *pdwResult = FALSE;
        return(FALSE);
    }

    //
    // Indicate that we are currently programming the FLASH.
    //
    SetDlgItemText(g_hDlg, IDC_UPDATE_PROGRESS, "Programming the FLASH...");

    //
    // While there is more data in the host file, read data from the host and
    // write it to the device file.
    //
    while(g_ulLength)
    {
        //
        // Read data from the host file.
        //
        ReadFile(g_hFile, g_pvData, 1024 * 1024, &ulRead, NULL);

        //
        // Write data to the device file.
        //
        Maverick_Write(g_pvData, ulRead);

        //
        // Decrement the number of bytes still to be read.
        //
        g_ulLength -= ulRead;
    }

    //
    // Close the file on the device.
    //
    Maverick_Close();

    //
    // Close the host file.
    //
    CloseHandle(g_hFile);

    //
    // Free the file data buffer.
    //
    GlobalFree(g_pvData);

    //
    // Indicate that we are restarting the player.
    //
    SetDlgItemText(g_hDlg, IDC_UPDATE_PROGRESS, "Restarting the player...");

    //
    // Now, disconnect from the player.  This will cause it to reboot and run
    // the newly downloaded software image.
    //
    Maverick_CloseDevice();

    //
    // It takes a couple of seconds for the player to restart and reconnect to
    // the USB, so wait for 3 seconds.
    //
    SleepEx(3000, FALSE);

    //
    // Make a couple of attempts at reconnecting to the player.  We don't know
    // exactly how long it will take for the player to restart and reconnect
    // to the USB.
    //
    for(ulRead = 0; ulRead < 10; ulRead++)
    {
        //
        // Delay for 500ms.
        //
        SleepEx(500, FALSE);

        //
        // Attempt to reconnect to the player.
        //
        if(Maverick_OpenDevice() != 0)
        {
            //
            // We were able to reconnect, so the result is success.
            //
            *pdwResult = TRUE;
            return(TRUE);
        }

        //
        // Close the device just in case.
        //
        Maverick_CloseDevice();
    }

    //
    // We were unable to reconnect, so the result is failure.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -