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

📄 scope_control.cpp

📁 一个简单示波器的源代码
💻 CPP
📖 第 1 页 / 共 5 页
字号:
                                bDataReadOngoing = false;
                                LocalFree(pData);
                            }
                        }

                        //
                        // Free the payload.
                        //
                        LocalFree(pvPacketData);
                    }
                    break;

                    //
                    // The oscilloscope is indicating that automatic waveform
                    // capture has started.
                    //
                    case SCOPE_PKT_STARTED:
                    {
                        //
                        // Pass a message to the client.
                        //
                        PostMessage(pDevice->hwndNotify, WM_SCOPE_STARTED,
                                    0, 0);
                    }
                    break;

                    //
                    // The oscilloscope is indicating that automatic waveform
                    // capture has stopped.
                    //
                    case SCOPE_PKT_STOPPED:
                    {
                        //
                        // Pass a message to the client.
                        //
                        PostMessage(pDevice->hwndNotify, WM_SCOPE_STOPPED,
                                    0, 0);
                    }
                    break;

                    //
                    // The oscilloscope is indicating that the timebase has
                    // been changed.
                    //
                    case SCOPE_PKT_TIMEBASE_UPDATED:
                    {
                        //
                        // Pass the new trigger level to the client.
                        //
                        PostMessage(pDevice->hwndNotify,
                                    WM_SCOPE_TIMEBASE_CHANGED, 0,
                                    (LPARAM)sPacket.ulParam);
                    }
                    break;

                    //
                    // The oscilloscope is indicating that the trigger channel
                    // and/or type has changed.
                    //
                    case SCOPE_PKT_TRIGGER_TYPE:
                    {
                        //
                        // Pass the new trigger level to the client.
                        //
                        PostMessage(pDevice->hwndNotify,
                                    WM_SCOPE_TRIGGER_TYPE_CHANGED,
                                    (WPARAM)sPacket.ucParam,
                                    (LPARAM)sPacket.ulParam);
                    }
                    break;

                    //
                    // The oscilloscope is indicating that the trigger level
                    // has changed.
                    //
                    case SCOPE_PKT_TRIGGER_LEVEL:
                    {
                        //
                        // Pass the new trigger level to the client.
                        //
                        PostMessage(pDevice->hwndNotify,
                                    WM_SCOPE_TRIGGER_LEVEL_CHANGED,
                                    0, (LPARAM)sPacket.ulParam);
                    }
                    break;

                    //
                    // The oscilloscope is indicating that the trigger position
                    // has changed.
                    //
                    case SCOPE_PKT_TRIGGER_POS:
                    {
                        //
                        // Pass the new trigger level to the client.
                        //
                        PostMessage(pDevice->hwndNotify,
                                    WM_SCOPE_TRIGGER_POS_CHANGED,
                                    0, (LPARAM)sPacket.ulParam);
                    }
                    break;

                    //
                    // The oscilloscope is indicating that channel 2 has been
                    // enabled or disabled.
                    //
                    case SCOPE_PKT_CHANNEL2:
                    {
                        //
                        // Pass a message to the client.
                        //
                        PostMessage(pDevice->hwndNotify, WM_SCOPE_CHANNEL2,
                                    (WPARAM)sPacket.ucParam , 0);
                    }
                    break;

                    //
                    // The oscilloscope is informing us that the vertical scaling for one
                    // of the channels has been changed.
                    //
                    case SCOPE_PKT_SCALE:
                    {
                        //
                        // Pass a message to the client.
                        //
                        PostMessage(pDevice->hwndNotify, WM_SCOPE_SCALE_CHANGED,
                                    (WPARAM)sPacket.ucParam , (LPARAM)sPacket.ulParam);
                    }
                    break;


                    //
                    // The oscilloscope is informing us that the vertical position for one
                    // of the channels has been changed.
                    //
                    case SCOPE_PKT_POSITION:
                    {
                        //
                        // Pass a message to the client.
                        //
                        PostMessage(pDevice->hwndNotify, WM_SCOPE_POS_CHANGED,
                                    (WPARAM)sPacket.ucParam , (LPARAM)sPacket.ulParam);
                    }
                    break;

                    //
                    // All other messages drop into this case. We don't
                    // expect to see this but...
                    //
                    default:
                    {
                        //
                        // This is some other kind of packet that we don't
                        // understand so just throw it away.
                        //
                        if(pvPacketData)
                        {
                            LocalFree(pvPacketData);
                        }
                    }
                    break;
                }
            }
            else
            {
                //
                // Did the scope device disconnect?
                //
                if(eRetcode == SCOPE_DISCONNECTED)
                {
                    //
                    // Yes - tidy up before going back to search for
                    // reconnection.
                    //
                    TerminateDevice(pDevice);
                    PostMessage(pDevice->hwndNotify,
                                WM_SCOPE_DEVICE_DISCONNECTED, 0, 0);
                }
            }
        }
    }
}

//****************************************************************************
//
// Free any events that have already been created for this module.
//
//****************************************************************************
static void DestroyScopeControlEvents(void)
{
    if(devInfo.hConnectEvent)
    {
        CloseHandle(devInfo.hConnectEvent);
        devInfo.hConnectEvent = (HANDLE)NULL;
    }

    if(devInfo.hReadEvent)
    {
        CloseHandle(devInfo.hReadEvent);
        devInfo.hReadEvent = (HANDLE)NULL;
    }

    if(devInfo.hThreadEndEvent)
    {
        CloseHandle(devInfo.hThreadEndEvent);
        devInfo.hThreadEndEvent = (HANDLE)NULL;
    }

    if(devInfo.hThreadSignalEvent)
    {
        CloseHandle(devInfo.hThreadSignalEvent);
        devInfo.hThreadSignalEvent = (HANDLE)NULL;
    }
}

//****************************************************************************
//
// Initialize this module's internal data and start the read/connect
// thread.
//
//****************************************************************************
bool ScopeControlInit(HWND hwndNotify)
{
    //
    // Clear out our instance data.
    //
    memset(&devInfo, 0, sizeof(tDeviceInfo));

    //
    // Remember the window handle passed.
    //
    devInfo.hwndNotify = hwndNotify;

    //
    // If we are passed a window handle, we assume asynchronous operation
    // with notifications being sent to the host via the window. This
    // requires us to start a thread and create additional signalling
    // resources.
    //
    // If a window handle is not passed, we operate synchronously with
    // blocking reads taking place in the calling context rather than
    // using the background thread.
    //
    if(hwndNotify)
    {
        //
        // Create the events we use to support synchronous read, connect and
        // ping requests.
        //
        devInfo.hConnectEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
        devInfo.hThreadSignalEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
        devInfo.hThreadEndEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
        devInfo.hReadEvent =  CreateEvent(NULL, TRUE, FALSE, NULL);

        //
        // Were the events created successfully?
        //
        if(!devInfo.hConnectEvent || !devInfo.hThreadEndEvent ||
           !devInfo.hReadEvent || !devInfo.hThreadSignalEvent)
        {
            DestroyScopeControlEvents();
            return(false);
        }
        else
        {
            //
            // Start our read/connect thread.
            //
            devInfo.hThread = CreateThread(NULL, 0, ReadConnectThread,
                                           &devInfo, 0, &devInfo.dwThreadID);

            //
            // Was the thread created successfully?
            //
            if(devInfo.hThread)
            {
                //
                // All is well. Thread is running and we are ready for
                // operation.
                //
                return(true);
            }
            else
            {
                //
                // We couldn't create the thread so fail this call after
                // freeing the other resources we created earlier.
                //
                DestroyScopeControlEvents();
                return(false);
            }
        }
    }
    else
    {
        //
        // We are operating synchronously.
        //
        return(true);
    }
}

//****************************************************************************
//
// Shut down the scope control module, free all resources and kill the
// read/connect thread.
//
//****************************************************************************
bool ScopeControlTerm(void)
{
    DWORD dwRetcode;
    bool bRetcode;

    //
    // Tell the thread to die and wait for its response.
    //
    SetEvent(devInfo.hThreadEndEvent);
    dwRetcode = WaitForSingleObject(devInfo.hThreadSignalEvent,
                                    THREAD_END_TIMEOUT);
    if(dwRetcode)
    {
        //
        // The thread failed to signal that it is ending. There's not a lot
        // we can do here other than go on and free resources but we do
        // remember to return a bad return code to the caller anyway.
        //
        bRetcode = false;
    }
    else
    {
        bRetcode = true;
    }

    //
    // Destroy our signalling events.
    //
    DestroyScopeControlEvents();

    return(bRetcode);
}

//****************************************************************************
//
// Send a PING packet to the device and wait for its response.
//
//****************************************************************************
bool ScopeControlPing(unsigned char ucEcho1, unsigned long ulEcho2)
{
    bool bRetcode;
    unsigned char ucParam;
    unsigned char ucType;
    unsigned long ulParam;
    unsigned long ulLength;
    void *pData;

    //
    // Send a new ping packet to the device.
    //
    bRetcode = SendScopePacket(SCOPE_PKT_PING, ucEcho1, ulEcho2);
    if(bRetcode)
    {
        //
        // Are we operating synchronously or asynchronously?
        //
        if(!devInfo.hwndNotify)
        {
            //
            // We are operating synchronously so we block and wait for the
            // response from the device.
            //

            //
            // If we sent the packet successfully, wait for the scope to
            // respond.
            //
            bRetcode = ScopeWaitPacket(&ucType, &ucParam, &ulParam, &ulLength,
                                       (void **)&pData);
            if(bRetcode)
            {
                //
                // Check to make sure that the response was correct. The
                // parameters should have been echoed back to us.
                //
                if(!((ucType == SCOPE_PKT_PING_RESPONSE) &&
                   (ulLength == 0) && (ucParam == ucEcho1) && (ulParam == ulEcho2)))
                {
                    bRetcode = false;
                }

⌨️ 快捷键说明

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