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

📄 mavusb.cpp

📁 基于EP7312的MP3播放器源代码,包括MCU和PC端代码.
💻 CPP
📖 第 1 页 / 共 5 页
字号:
    //
    if(ulHour > 9)
    {
        pcBuf[ulIdx++] = '0' + (char)(ulHour / 10);
    }
    pcBuf[ulIdx++] = '0' + (char)(ulHour % 10);

    //
    // Add a colon after the hour.
    //
    pcBuf[ulIdx++] = ':';

    //
    // Put the string representation of the minute into the buffer.
    //
    pcBuf[ulIdx++] = '0' + (char)(ulMin / 10);
    pcBuf[ulIdx++] = '0' + (char)(ulMin % 10);

    //
    // Add a colon after the minute.
    //
    pcBuf[ulIdx++] = ':';

    //
    // Put the string representation of the second into the buffer.
    //
    pcBuf[ulIdx++] = '0' + (char)(ulSec / 10);
    pcBuf[ulIdx++] = '0' + (char)(ulSec % 10);

    //
    // Add a space after the second.
    //
    pcBuf[ulIdx++] = ' ';

    //
    // Add a "am" if it is morning or "pm" if it is evening.
    //
    if(bIsAM)
    {
        pcBuf[ulIdx++] = 'a';
    }
    else
    {
        pcBuf[ulIdx++] = 'p';
    }
    pcBuf[ulIdx++] = 'm';

    //
    // Null terminate the string.
    //
    pcBuf[ulIdx++] = '\0';

    //
    // Return the string representation of the given time.
    //
    return(pcBuf);
}

//****************************************************************************
//
// Loads the list of files on the internet audio player and adds an icon to
// the list view control for each file.
//
//****************************************************************************
void
LoadFileList(void)
{
    LVITEM sItem;
    long lIdx;
    unsigned short pusBuffer[256];
    char pcBuffer[256];

    //
    // Remove all the items from the list view control/
    //
    ListView_DeleteAllItems(g_hListView);

    //
    // Fill in the members of the LVITEM structure which are common to all the
    // items.
    //
    sItem.mask = LVIF_IMAGE | LVIF_TEXT | LVIF_PARAM;
    sItem.iSubItem = 0;
    sItem.state = 0;
    sItem.stateMask = 0;
    sItem.pszText = pcBuffer;
    sItem.cchTextMax = 0;
    sItem.iImage = 0;
    sItem.iIndent = 0;

    //
    // Open the root directory on the device.
    //
    if(Maverick_OpenDir(g_ulDriveNum, L"\\") == 0)
    {
        return;
    }

    //
    // Loop through all the files in the root directory of the device.
    //
    lIdx = 0;
    while(Maverick_ReadDir(pusBuffer, 1) != 0)
    {
        //
        // Convert the device name from Unicode to ASCII.
        //
        WideCharToMultiByte(CP_ACP, 0, pusBuffer, wcslen(pusBuffer) + 1,
                            pcBuffer, 256, NULL, NULL);

        //
        // Fill in the item number.
        //
        sItem.iItem = lIdx;
        sItem.lParam = lIdx++;

        //
        // Add this file to the list view.
        //
        ListView_InsertItem(g_hListView, &sItem);

        //
        // Open the file.
        //
        Maverick_Open(g_ulDriveNum, pusBuffer, 1);

        //
        // Get the length of the file and set the length into the list view.
        //
        ListView_SetItemText(g_hListView, lIdx - 1, 1,
                             GetValueString(Maverick_Length()));

        //
        // Get the modification date/time of the file and set it into the list
        // view.
        //
        ListView_SetItemText(g_hListView, lIdx - 1, 2,
                             GetTimeString(Maverick_GetDate()));

        //
        // Close the file.
        //
        Maverick_Close();
    }

    //
    // Close the directory on the device.
    //
    Maverick_CloseDir();
}

//****************************************************************************
//
// The thread which transfers a file to the Internet audio player.
//
//****************************************************************************
DWORD WINAPI
DownloadThread(void *pvParameter)
{
    unsigned long ulRead, ulLength;

    //
    // Get the length of the file.
    //
    ulLength = g_ulLength;

    //
    // While there is more data in the host file, read data from the host and
    // write it to the device file.
    //
    while(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.
        //
        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);

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

//****************************************************************************
//
// The dialog box procedure for the download progress indicator.
//
//****************************************************************************
BOOL APIENTRY
DownloadProgressDlgProc(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 download message in the dialog box.
            //
            wsprintf(pcMessage, "Downloading \"%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 download thread.
            //
            GetExitCodeThread(g_hThread, &dwExitCode);

            //
            // See if the download thread is still active.
            //
            if(dwExitCode != STILL_ACTIVE)
            {
                //
                // The download 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);
}

//****************************************************************************
//
// Download a file to the Internet audio player.
//
//****************************************************************************
BOOL
Download(HWND hWnd, char *pcFileName)
{
    DWORD dwThreadID;
    char *pcDeviceName;
    unsigned short pusFileName[128];

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

    //
    // Open the host file.
    //
    g_hFile = CreateFile(pcFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
                         OPEN_EXISTING, 0, NULL);
    if(g_hFile == INVALID_HANDLE_VALUE)
    {
        GlobalFree(g_pvData);
        return(FALSE);
    }

    //
    // Get the length of the file.
    //
    g_ulLength = GetFileSize(g_hFile, NULL);

    //
    // Find the last '\' character in the name of the host file.  The remainder
    // of the host file name will become the device file name.
    //
    pcDeviceName = strrchr(pcFileName, '\\');
    if(pcDeviceName)
    {
        g_pcFileName = pcDeviceName + 1;
    }
    else
    {
        g_pcFileName = pcFileName;
    }

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

    //
    // Attempt to open the specified file on the device.
    //
    if(Maverick_Open(g_ulDriveNum, pusFileName, 6) == 0)
    {
        GlobalFree(g_pvData);
        CloseHandle(g_hFile);
        return(FALSE);
    }

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

    //
    // Create the thread which will actually download the data to the device.
    //
    g_hThread = CreateThread(NULL, 0, DownloadThread, NULL, 0, &dwThreadID);

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

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

//****************************************************************************
//
// Downloads the set of files specified in a play list.
//
//****************************************************************************
BOOL
DownloadPlayList(HWND hWnd, char *pcFileName, unsigned long bIsM3U)
{
    HANDLE hFile;
    char *pcBuffer, *pcLine;
    unsigned long ulOffset, ulLength;

    //
    // Open the host file.
    //
    hFile = CreateFile(pcFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
                       OPEN_EXISTING, 0, NULL);
    if(hFile == INVALID_HANDLE_VALUE)
    {
        return(FALSE);
    }

    //
    // Get the length of the file.
    //
    ulLength = GetFileSize(hFile, NULL);

    //
    // Allocate memory for the file data.
    //
    pcBuffer = (char *)GlobalAlloc(GMEM_FIXED, ulLength);
    if(!pcBuffer)
    {
        CloseHandle(hFile);
        return(FALSE);
    }

    //
    // Read data from the host file.
    //
    ReadFile(hFile, pcBuffer, ulLength, &ulOffset, NULL);

    //
    // Loop through the entire buffer, replacing carriage return and line feed
    // characters with NULLs.
    //
    for(ulOffset = 0; ulOffset < ulLength; ulOffset++)
    {
        //
        // See if this is a carriage return or line feed character.
        //
        if((pcBuffer[ulOffset] == '\r') || (pcBuffer[ulOffset] == '\n'))
        {
            //
            // Replace this character with a NULL.
            //
            pcBuffer[ulOffset] = '\0';
        }
    }

    //
    // Look through each line of the file.
    //
    for(ulOffset = 0; ulOffset < ulLength;
        ulOffset += strlen(pcBuffer + ulOffset) + 1)
    {
        pcLine = pcBuffer + ulOffset;

        //
        // See if this is a M3U play list.
        //
        if(bIsM3U)
        {
            //
            // If this is a comment line then ignore it.
            //
            if(pcBuffer[ulOffset] == '#')
            {
                continue;
            }

⌨️ 快捷键说明

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