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

📄 mavusb.cpp

📁 基于EP7312的MP3播放器源代码,包括MCU和PC端代码.
💻 CPP
📖 第 1 页 / 共 5 页
字号:
//****************************************************************************
//
// MAVUSB.CPP - Maverick(tm) Internet Audio Player Interface application.
//
// Copyright (c) 2000,2001 Cirrus Logic, Inc.
//
//****************************************************************************
#include "stdafx.h"
#include <afxres.h>
#include <commctrl.h>
#include <commdlg.h>
#include <shellapi.h>
#include "auddat.h"
#include "mavcmd.h"
#include "resource.h"
#include "../../player/audible.h"
#include "../../player/usbven.h"

//****************************************************************************
//
// The length of each month of the year, in days.  February is consider to be
// 29 days and the code explicitly handles making sure that it is 28 days in
// non-leap years.
//
//****************************************************************************
const unsigned char ucMonthLength[12] = { 31, 29, 31, 30, 31, 30,
                                          31, 31, 30, 31, 30, 31 };

//****************************************************************************
//
// The string representation of the days of the week.
//
//****************************************************************************
const char pcWeekdays[] = "SunMonTueWedThuFriSat";

//****************************************************************************
//
// The string representation of the months of the year.
//
//****************************************************************************
const char pcMonths[] = "JanFebMarAprMayJunJulAugSepOctNovDec";

//****************************************************************************
//
// The instance handle of the application.
//
//****************************************************************************
HINSTANCE g_hInstance;

//****************************************************************************
//
// The currently displayed drive number.
//
//****************************************************************************
unsigned long g_ulDriveNum = 0;

//****************************************************************************
//
// The handle of the list view window.
//
//****************************************************************************
HWND g_hListView;

//****************************************************************************
//
// The handle of the update progress dialog box.
//
//****************************************************************************
HWND g_hDlg;

//****************************************************************************
//
// The handle of the thread created for downloading or uploading file data.
//
//****************************************************************************
HANDLE g_hThread;

//****************************************************************************
//
// The handle of the file being downloaded to or uploaded from the device.
//
//****************************************************************************
HANDLE g_hFile;

//****************************************************************************
//
// The length of the file being downloaded to or uploaded from the device.
//
//****************************************************************************
unsigned long g_ulLength;

//****************************************************************************
//
// The device name of the file being downloaded to or uploaded from the
// device.
//
//****************************************************************************
char *g_pcFileName;

//****************************************************************************
//
// The buffer containing the current block of data being transferred to or
// from the device.
//
//****************************************************************************
void *g_pvData;

//****************************************************************************
//
// TimeToSeconds converts the specified time as month, day, year, hour,
// minute, and second into the number of seconds since January 1, 1970.
//
//****************************************************************************
unsigned long
TimeToSeconds(unsigned long ulYear, unsigned long ulMonth, unsigned long ulDay,
              unsigned long ulHour, unsigned long ulMinute,
              unsigned long ulSecond)
{
    unsigned long ulRet, ulIdx;

    //
    // Compute the number of days that have past in this year.
    //
    ulRet = ulDay - 1;
    ulIdx = ulMonth - 1;
    while(ulIdx--)
    {
        ulRet += ucMonthLength[ulIdx];
    }

    //
    // If this is a non-leap year and the day is past February 28th, then
    // subtract the count of days by one (since we treat each year as if it
    // were a leap year).
    //
    if(((ulYear & 3) != 0) && (ulRet >= (31 + 28)))
    {
        ulRet--;
    }

    //
    // Add the days for the years that have past.
    //
    ulIdx = ulYear - 1970;
    ulRet += (ulIdx * 365) + ((ulIdx + 1) / 4);

    //
    // Convert the days to hours and add the current hour.
    //
    ulRet = (ulRet * 24) + ulHour;

    //
    // Convert the hours to minutes and add the current minute.
    //
    ulRet = (ulRet * 60) + ulMinute;

    //
    // Convert the minutes to seconds and add the current second.
    //
    ulRet = (ulRet * 60) + ulSecond;

    //
    // Return the converted time.
    //
    return(ulRet);
}

//****************************************************************************
//
// TimeFromSeconds converts the specified time in seconds since January 1,
// 1970 into a time structure, giving the corresponding month, day, year,
// hour, minute, second, and weekday.
//
//****************************************************************************
void
TimeFromSeconds(unsigned long ulSeconds, unsigned long *pulYear,
                unsigned long *pulMonth, unsigned long *pulDay,
                unsigned long *pulHour, unsigned long *pulMinute,
                unsigned long *pulSecond, unsigned long *pulWeekday)
{
    long lYear, lIdx = 0;

    //
    // Extract the number of seconds from the time.
    //
    *pulSecond = ulSeconds % 60;
    ulSeconds /= 60;

    //
    // Extract the number of minutes from the time.
    //
    *pulMinute = ulSeconds % 60;
    ulSeconds /= 60;

    //
    // Extract the number of hours from the time.
    //
    *pulHour = ulSeconds % 24;
    ulSeconds /= 24;

    //
    // We now have days, so add the number of days between January 1, 1900 and
    // January 1, 1970.
    //
    ulSeconds += (70 * 365) + 18;

    //
    // The day of the week just happens to work out this way.
    //
    *pulWeekday = ulSeconds % 7;

    //
    // Compute the number of years in terms of group of years from leap year to
    // leap year.
    //
    lYear = 4 * (ulSeconds / ((365 * 4) + 1));
    ulSeconds %= (365 * 4) + 1;

    //
    // If there are more than 365 days left in the current count of days, then
    // subtract the days from the remaining non-leap years.
    //
    if(ulSeconds >= 366)
    {
        lYear += (ulSeconds - 1) / 365;
        ulSeconds = (ulSeconds - 1) % 365;
    }

    //
    // Save the computed year.
    //
    *pulYear = 1900 + lYear;

    //
    // If this is a non-leap year and the day is past February 28th, then
    // increment the count of days by one (i.e. act as if each year is a leap
    // year).
    //
    if(((lYear & 3) != 0) && (ulSeconds >= (31 + 28)))
    {
        ulSeconds++;
    }

    //
    // Subtract days for each month till we find the current month.
    //
    while(ulSeconds >= ucMonthLength[lIdx])
    {
        ulSeconds -= ucMonthLength[lIdx++];
    }

    //
    // Save the computed month and day.
    //
    *pulDay = ulSeconds + 1;
    *pulMonth = lIdx + 1;
}

//****************************************************************************
//
// Checks the clock on the player and updates it if it is "too far" out of
// sync with the host clock.
//
//****************************************************************************
void
CheckTime(void)
{
    SYSTEMTIME sLocalTime;
    unsigned long ulLength, ulTime, ulPlayerTime;

    //
    // Get the current time from the player.
    //
    if(Maverick_GetDescriptor(USB_Vendor_Descriptor_CurrentTime, 0,
                              (char *)&ulPlayerTime, &ulLength) == 0)
    {
        //
        // We could not get the current time from the player, so do nothing
        // further.
        //
        return;
    }

    //
    // Get the current host time.
    //
    GetLocalTime(&sLocalTime);

    //
    // Convert the current host time into seconds.
    //
    ulTime = TimeToSeconds(sLocalTime.wYear, sLocalTime.wMonth,
                           sLocalTime.wDay, sLocalTime.wHour,
                           sLocalTime.wMinute, sLocalTime.wSecond);

    //
    // See if the current player time is within 10 seconds on either side of
    // the current host time.
    //
    if(((ulTime < ulPlayerTime) && ((ulPlayerTime - ulTime) > 10)) ||
       ((ulTime > ulPlayerTime) && ((ulTime - ulPlayerTime) > 10)))
    {
        //
        // The current player time is more than 10 seconds different than the
        // current host time, so change the player time to the current host
        // time.
        //
        Maverick_SetDescriptor(USB_Vendor_Descriptor_CurrentTime, 0,
                               (char *)&ulTime, 4);
    }
}

//****************************************************************************
//
// Converts a value into it's comma separated string representation.
//
//****************************************************************************
char *
GetValueString(ULONG ulValue)
{
    static char pcBuf[16];
    unsigned long ulLoop, ulIdx;

    //
    // Start filling the string buffer from the beginning.
    //
    ulIdx = 0;

    //
    // Start with the most significant digit and work our way down to the least
    // significant digit.
    //
    for(ulLoop = 1000000000; ulLoop; ulLoop /= 10)
    {
        //
        // If the value is greater than the current digit, or we are at the
        // first digit, then we need to add a character to our string buffer.
        //
        if((ulValue >= ulLoop) || (ulLoop == 1))
        {
            //
            // Place the character for the current digit into the string
            // buffer.
            //
            pcBuf[ulIdx++] = '0' + (char)((ulValue / ulLoop) % 10);

            //
            // If the current digit is the billions, millions, or thousands,
            // then we need to add a comma after the digit.
            //
            if((ulLoop == 1000000000) || (ulLoop == 1000000) ||
               (ulLoop == 1000))
            {
                //
                // Place a comma into the string buffer.
                //
                pcBuf[ulIdx++] = ',';
            }
        }
    }

    //
    // NULL terminate the string in the buffer.
    //
    pcBuf[ulIdx] = '\0';

    //
    // Return a pointer to the string.
    //
    return(pcBuf);
}

//****************************************************************************
//
// Converts a time specified as the number of seconds since January 1, 1970
// into a string representation.
//
//****************************************************************************
char *
GetTimeString(ULONG ulSeconds)
{
    static char pcBuf[32];
    unsigned long ulYear, ulMonth, ulDay, ulHour, ulMin, ulSec, ulWeekday;
    unsigned long ulIdx, bIsAM;

    //
    // Convert the number of seconds into the date and time.
    //
    TimeFromSeconds(ulSeconds, &ulYear, &ulMonth, &ulDay, &ulHour, &ulMin,
                    &ulSec, &ulWeekday);

    //
    // The starting string offset is zero.
    //
    ulIdx = 0;

    //
    // Put the string representation of the day of the week into the buffer.
    //
    memcpy(pcBuf + ulIdx, pcWeekdays + (ulWeekday * 3), 3);
    ulIdx += 3;

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

    //
    // Put the string representation of the month into the buffer.
    //
    memcpy(pcBuf + ulIdx, pcMonths + ((ulMonth - 1) * 3), 3);
    ulIdx += 3;

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

    //
    // Put the string representation of the day into the buffer.
    //
    if(ulDay > 9)
    {
        pcBuf[ulIdx++] = '0' + (char)(ulDay / 10);
    }
    pcBuf[ulIdx++] = '0' + (char)(ulDay % 10);

    //
    // Add a command and space after the day.
    //
    pcBuf[ulIdx++] = ',',
    pcBuf[ulIdx++] = ' ';

    //
    // Put the string representation of the year into the buffer.
    //
    pcBuf[ulIdx++] = '0' + (char)(ulYear / 1000);
    pcBuf[ulIdx++] = '0' + (char)((ulYear / 100) % 10);
    pcBuf[ulIdx++] = '0' + (char)((ulYear / 10) % 10);
    pcBuf[ulIdx++] = '0' + (char)(ulYear % 10);

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

    //
    // See if the hour is "am" or "pm".
    //
    if(ulHour < 12)
    {
        //
        // The hour is "am".
        //
        bIsAM = 1;

        //
        // If the hour is 0, then it is actually 12am.
        //
        if(ulHour == 0)
        {
            ulHour = 12;
        }
    }
    else
    {
        //
        // The hour is "pm".
        //
        bIsAM = 0;

        //
        // If the hour is not 12, then it is actually 12 hours before (i.e.
        // convert military time to civilian time).
        //
        if(ulHour != 12)
        {
            ulHour -= 12;
        }
    }

    //
    // Put the string representation of the hour into the buffer.

⌨️ 快捷键说明

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