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

📄 waveformdisplay.cpp

📁 一个简单示波器的源代码
💻 CPP
📖 第 1 页 / 共 3 页
字号:
                *pulTime = (ulIndex * m_pScopeData->ulSamplePerioduS) +
                           m_pScopeData->ulSampleOffsetuS;
            }
            return(m_pDualElement[ulIndex].sSample2mVolts);
        }
    }
    else
    {
        //
        // Single channel data. We assume that the caller has already
        // ensured that the dataset contains samples for the desired
        // channel so we just return the single sample here without
        // checking the iChannel value.
        //
        if(pulTime)
        {
            *pulTime = ulIndex * m_pScopeData->ulSamplePerioduS;
        }
        return(m_pSingleElement[ulIndex].sSamplemVolts);
    }
}

//***************************************************************************
//
// Save the current data set as a Windows bitmap in the file whose name
// is provided.
//
//***************************************************************************
BOOL CWaveformDisplay::SaveAsBMP(CString *pFilename)
{
    HANDLE hDIB;
    CFile File;
    BOOL bRetcode;
    int iColors;
    int iSizeDIB;
    BITMAPFILEHEADER bfh;
    LPBITMAPINFOHEADER lpbi;

    //
    // Convert our internal bitmap to a device independent one that we can
    // then save to the file.
    //
    hDIB = CreateDIB(&iSizeDIB);

    if(hDIB)
    {
        //
        // We got the bitmap successfully so now save it.
        //
        bRetcode = File.Open((LPCTSTR)*pFilename,
                             (CFile::modeCreate | CFile::modeWrite));
        if(bRetcode)
        {
            //
            // Get a pointer to the bitmap header.
            //
            lpbi = (LPBITMAPINFOHEADER)hDIB;

            //
            // How many palette entries are there in the bitmap?
            //
            iColors = (lpbi->biBitCount <= 8) ? (1 << lpbi->biBitCount) : 0;

            //
            // Fill in the fields of the file header
            //
            bfh.bfType = ((WORD) ('M' << 8) | 'B');    // "BM"
            bfh.bfSize = iSizeDIB + sizeof(BITMAPFILEHEADER );
            bfh.bfReserved1 = 0;
            bfh.bfReserved2    = 0;
            bfh.bfOffBits = (DWORD)(sizeof(BITMAPFILEHEADER) +
                            lpbi->biSize + (iColors * sizeof(RGBQUAD)));

            //
            // Write the file header and bitmap to the file.
            //
            File.Write(&bfh, sizeof(BITMAPFILEHEADER));
            File.Write(hDIB, iSizeDIB);
            File.Close();
        }

        //
        // Free the DIB
        //
        LocalFree(hDIB);
    }

    return(bRetcode);
}

//***************************************************************************
//
// Save the current data set as a Comma Separated Value file in the file
// whose name is provided.
//
//***************************************************************************
BOOL CWaveformDisplay::SaveAsCSV(CString *pFilename)
{
    CStdioFile File;
    CString strWork;
    BOOL bRetcode;
    unsigned long ulLoop;
    unsigned long ulTime1;
    unsigned long ulTime2;
    short sSample1;
    short sSample2;

    //
    // We can't save the data if we don't have any data to save.
    //
    if(!m_pScopeData)
    {
        return(FALSE);
    }

    //
    // Open the file for writing in text mode.
    //
    bRetcode = File.Open((LPCTSTR)*pFilename, (CFile::modeCreate | CFile::modeWrite |
                         CFile::typeText));

    //
    // Did the file open correctly?
    //
    if(bRetcode)
    {
        //
        // All is well - write the file data.
        //
        File.WriteString(L"Oscilloscope Data\n");
        if(m_pScopeData->bDualChannel)
        {
            File.WriteString(L"Channel 1,, Channel 2\n");
            File.WriteString(L"Time (uS), Sample (mV), Time (uS), Sample (mV)\n");
        }
        else
        {
            File.WriteString(L"Channel 1\n");
            File.WriteString(L"Time (uS), Sample (mV)\n");
        }

        //
        // Write the sample data.
        //
        for(ulLoop = 0; ulLoop < m_pScopeData->ulTotalElements; ulLoop++)
        {
            if(m_pScopeData->bDualChannel)
            {
                sSample1 = GetSample(ulLoop, CHANNEL_1, &ulTime1);
                sSample2 = GetSample(ulLoop, CHANNEL_2, &ulTime2);
                strWork.Format(L"%6d, %6d, %6d, %6d\n", ulTime1, sSample1,
                               ulTime2, sSample2);
            }
            else
            {
                sSample1 = GetSample(ulLoop, CHANNEL_1, &ulTime1);
                strWork.Format(L"%6d, %6d\n", ulTime1, sSample1);
            }

            File.WriteString((LPCTSTR)strWork);
        }

        File.Close();
    }

    return(bRetcode);
}

//***************************************************************************
//
// Create a device independent bitmap from our offscreen drawing surface
// and return the handle to the caller.
//
//***************************************************************************
HANDLE CWaveformDisplay::CreateDIB(int *piSizeDIB)
{
    BITMAP bm;
    BITMAPINFOHEADER bi;
    LPBITMAPINFOHEADER lpbi;
    DWORD dwLen;
    HANDLE hDIB;
    CDC *pDC;
    CPalette pal;
    CPalette *pOldPal;
    int iLoop;

    //
    // Create a palette containing the colors we know are in the waveform image.
    //
    UINT nPalSize = sizeof(LOGPALETTE) + (sizeof(PALETTEENTRY) * NUM_WAVEFORM_COLORS);
    LOGPALETTE *pLP = (LOGPALETTE *) new BYTE[nPalSize];
    pLP->palVersion = 0x300;
    pLP->palNumEntries = NUM_WAVEFORM_COLORS;

    for(iLoop = 0; iLoop < NUM_WAVEFORM_COLORS; iLoop++)
    {
        pLP->palPalEntry[iLoop].peRed = GetRValue(g_colWaveformColors[iLoop]);
        pLP->palPalEntry[iLoop].peGreen = GetGValue(g_colWaveformColors[iLoop]);
        pLP->palPalEntry[iLoop].peBlue = GetBValue(g_colWaveformColors[iLoop]);
        pLP->palPalEntry[iLoop].peFlags = 0;
    }

    // Create the palette
    pal.CreatePalette( pLP );

    delete[] pLP;

    // Get bitmap information
    m_bmpWaveform.GetObject(sizeof(bm),(LPSTR)&bm);

    // Initialize the bitmapinfoheader
    bi.biSize = sizeof(BITMAPINFOHEADER);
    bi.biWidth = bm.bmWidth;
    bi.biHeight = bm.bmHeight;
    bi.biPlanes = 1;
    bi.biBitCount = 8;
    bi.biCompression = BI_RGB;
    bi.biSizeImage = 0;
    bi.biXPelsPerMeter = 0;
    bi.biYPelsPerMeter = 0;
    bi.biClrUsed = 0;
    bi.biClrImportant = 0;
    bi.biSizeImage = ((((bi.biWidth * bi.biBitCount) + 31) & ~31) / 8)
                        * bi.biHeight;

    // Compute the size of the  infoheader and the color table
    dwLen  = bi.biSize + 256 * sizeof(RGBQUAD) + bi.biSizeImage;

    // We need a device context to get the DIB from
    pDC = GetDC();
    pOldPal = pDC->SelectPalette(&pal,FALSE);
    pDC->RealizePalette();

    // Allocate enough memory to hold bitmapinfoheader and color table
    hDIB = LocalAlloc(LMEM_FIXED, dwLen);

    if (!hDIB)
    {
        pDC->SelectPalette(pOldPal,FALSE);
        ReleaseDC(pDC);
        return NULL;
    }

    //
    // Copy the bitmap info header into the new allocation.
    //
    lpbi = (LPBITMAPINFOHEADER)hDIB;
    *lpbi = bi;

    // Get the bitmap bits
    lpbi = (LPBITMAPINFOHEADER)hDIB;
    BOOL bGotBits = GetDIBits(pDC->m_hDC, (HBITMAP)m_bmpWaveform.GetSafeHandle(),
                0L,                // Start scan line
                (DWORD)bi.biHeight,        // # of scan lines
                (LPBYTE)lpbi             // address for bitmap bits
                + (bi.biSize + 256 * sizeof(RGBQUAD)),
                (LPBITMAPINFO)lpbi,        // address of bitmapinfo
                (DWORD)DIB_RGB_COLORS);        // Use RGB for color table

    if( !bGotBits )
    {
        LocalFree(hDIB);
        pDC->SelectPalette(pOldPal,FALSE);
        ReleaseDC(pDC);
        return(NULL);
    }

    pDC->SelectPalette(pOldPal,FALSE);
    ReleaseDC(pDC);

    //
    // Tell the caller the size of the bitmap in memory.
    //
    *piSizeDIB = dwLen;

    return(hDIB);
}

//***************************************************************************
//
// Does the display currently show the trigger level?
//
//***************************************************************************
BOOL CWaveformDisplay::IsTriggerLevelShown(void)
{
    return(m_bShowTrigLevel);
}

//***************************************************************************
//
// Does the display currently show the trigger position line?
//
//***************************************************************************
BOOL CWaveformDisplay::IsTriggerPosShown(void)
{
    return(m_bShowTrigPos);
}

//***************************************************************************
//
// Does the display currently show the graticule lines?
//
//***************************************************************************
BOOL CWaveformDisplay::IsGraticuleShown(void)
{
    return(m_bShowGraticule);
}

//***************************************************************************
//
// Does the display currently show the ground line?
//
//***************************************************************************
BOOL CWaveformDisplay::IsGroundShown(void)
{
    return(m_bShowGround);
}

//***************************************************************************
//
// Show or hide the trigger level marking line on the display.
//
//***************************************************************************
void CWaveformDisplay::ShowTriggerLevel(BOOL bShow, BOOL bUpdate)
{
    m_bShowTrigLevel = bShow;

    if(bUpdate)
    {
        InternalRenderWaveform();
    }
}

//***************************************************************************
//
// Show or hide the trigger position marking line on the display.
//
//***************************************************************************
void CWaveformDisplay::ShowTriggerPos(BOOL bShow, BOOL bUpdate)
{
    m_bShowTrigPos = bShow;

    if(bUpdate)
    {
        InternalRenderWaveform();
    }
}

//***************************************************************************
//
// Show or hide the graticule lines on the display.
//
//***************************************************************************
void CWaveformDisplay::ShowGraticule(BOOL bShow, BOOL bUpdate)
{
    m_bShowGraticule = bShow;

    if(bUpdate)
    {
        InternalRenderWaveform();
    }
}

//***************************************************************************
//
// Show or hide the ground marking line on the display.
//
//***************************************************************************
void CWaveformDisplay::ShowGround(BOOL bShow, BOOL bUpdate)
{
    m_bShowGround = bShow;

    if(bUpdate)
    {
        InternalRenderWaveform();
    }
}

⌨️ 快捷键说明

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