📄 asyncdem.c
字号:
//------------------------------------------------------------------------------// Demo for asynctmr instrument driver//------------------------------------------------------------------------------#include <ansi_c.h>#include <userint.h>#include <utility.h>#include "asynctmr.h"#include "asyncdem.h"#define ASYNC_TIMER 0#define CVI_TIMER 1//------------------------------------------------------------------------------// Local variables//------------------------------------------------------------------------------static int panelHandle = 0;static int activeTimer = CVI_TIMER;static int timerId = 0;// Worker variablesstatic volatile int timeArrayLastWritten = 0;static volatile int timeOfLastCall = 0;static int timeArraySize = 1000;static double timeArray[1000];static int timerWasStopped = 0;static volatile int dataArrayLastWritten = 0;static int dataArraySize = 1000;static double dataArray[1000];//------------------------------------------------------------------------------// Prototypes//------------------------------------------------------------------------------static int StartupTimer(void);static int ShutdownTimer(void);static int UpdateUIRWithTime(void);static int UpdateUIRWithData(void);static int DoWork(int);static int msDelay; //------------------------------------------------------------------------------// main//------------------------------------------------------------------------------int main (int argc, char *argv[]){ if (InitCVIRTE (0, argv, 0) == 0) return -1; /* out of memory */ //Load and Display Panel if ((panelHandle = LoadPanel (0, "asyncdem.uir", PANEL)) < 0) return -1; DisplayPanel (panelHandle); // Tell CVI to not poll for UIR so much SetSleepPolicy (VAL_SLEEP_MORE); // Setup UIR GetCtrlVal(panelHandle, PANEL_DELAY, &msDelay); SetAxisRange (panelHandle, PANEL_TIMESTRIPCHART, VAL_NO_CHANGE, 0.0, 1.0, VAL_MANUAL, 0.0, (msDelay<10)?20:msDelay*2.0); GetCtrlVal(panelHandle, PANEL_WHICHTIMER, &activeTimer); RunUserInterface (); // Cleanup ShutdownTimer(); return 0;}//------------------------------------------------------------------------------// QuitCallback//------------------------------------------------------------------------------int CVICALLBACK QuitCallback (int panel, int control, int event, void *callbackData, int eventData1, int eventData2){ switch (event) { case EVENT_COMMIT: QuitUserInterface (0); break; } return 0;}//------------------------------------------------------------------------------// Async Timer Callback Routine//------------------------------------------------------------------------------// !!! Remember that this callback is being run in a different thread !!!//// 1) Multithreading means that your variables are not protected from getting // written to by the main thread and this callback at the same time.//// 2) You are limited on what LW/CVI functions you can call when using this // inside the LW/CVI IDE. The LW/CVI libraries are not multithread safe in the// IDE. You should try to limit yourself to hardware type functionality only////------------------------------------------------------------------------------int CVICALLBACK MyTimerCallback (int reserved, int theTimerId, int event, void *callbackData, int eventData1, int eventData2){ double currentTime = 0.0; double deltaTime = 0.0; if ((double*)eventData1) currentTime = *((double*)eventData1); if ((double*)eventData2) deltaTime = *((double*)eventData2); if (event==EVENT_TIMER_TICK) { DoWork((int)(currentTime*1000)); // This is what will be done during the async callback } if (event==EVENT_DISCARD) { timerId = 0; } return 0;}//------------------------------------------------------------------------------// Init routine for starting thread//------------------------------------------------------------------------------static int StartupTimer(void){ int count = -1; int enabled = 1; // Delete timer if already running ShutdownTimer(); // Start New Timer if (activeTimer == CVI_TIMER) { timerId = NewCtrl (panelHandle, CTRL_TIMER, "", 0, 0); if (timerId<=0) { MessagePopup("CVI Timer", "CVI timer could not be created"); timerId = 0; return -1; } SetCtrlAttribute (panelHandle, timerId, ATTR_INTERVAL, ((double)msDelay)/1000.0); SetCtrlAttribute (panelHandle, timerId, ATTR_ENABLED, enabled); SetCtrlAttribute (panelHandle, timerId, ATTR_CALLBACK_FUNCTION_POINTER, MyTimerCallback); SetCtrlAttribute (panelHandle, timerId, ATTR_CALLBACK_DATA, NULL); } if (activeTimer == ASYNC_TIMER) { timerId = NewAsyncTimer(((double)msDelay)/1000.0, count, enabled, MyTimerCallback, NULL); if (timerId<=0) { MessagePopup("Async Timer", "Async timer could not be created"); timerId = 0; return -1; } } return 0;}//------------------------------------------------------------------------------// Clean up routine for Windows handles and thread//------------------------------------------------------------------------------static int ShutdownTimer(void){ // Delete Timer if already running if (timerId>0) { // Stop timer if (activeTimer == CVI_TIMER) { DiscardCtrl (panelHandle, timerId); timerId = 0; } if (activeTimer == ASYNC_TIMER) { DiscardAsyncTimer(-1); timerId = 0; } } return 0;}//------------------------------------------------------------------------------// TimerStartStop//------------------------------------------------------------------------------int CVICALLBACK TimerStartStop (int panel, int control, int event, void *callbackData, int eventData1, int eventData2){ int startStop; switch (event) { case EVENT_COMMIT: GetCtrlVal (panelHandle, PANEL_TIMERSTARTSTOP, &startStop); if (startStop) { timeOfLastCall = 0; StartupTimer(); } else { ShutdownTimer(); } break; } return 0;}//------------------------------------------------------------------------------// UpdateUIRWithTime - This function will be called by a CVI Timer callback//------------------------------------------------------------------------------static int UpdateUIRWithTime(void){ static lastIndex = 0; int lastWritten = timeArrayLastWritten; int pointsToPlot1; int pointsToPlot2; static int stripChart = PANEL_TIMESTRIPCHART; static int written = PANEL_LASTWRITTEN; static int deltawritten = PANEL_DELTAWRITTEN; if (lastIndex!=lastWritten) { SetCtrlVal(panelHandle, written, lastWritten); if (lastIndex < lastWritten) { pointsToPlot1 = lastWritten - lastIndex; pointsToPlot2 = 0; PlotStripChart (panelHandle, stripChart, (double *)timeArray, pointsToPlot1, lastIndex, 0, VAL_DOUBLE); } else { pointsToPlot1 = timeArraySize - 1 - lastIndex; pointsToPlot2 = lastWritten+1; if (pointsToPlot1 > 0) PlotStripChart (panelHandle, stripChart, (double*)timeArray, pointsToPlot1, lastIndex, 0, VAL_DOUBLE); PlotStripChart (panelHandle, stripChart, (double*)timeArray, pointsToPlot2, 0 , 0, VAL_DOUBLE); } SetCtrlVal(panelHandle, deltawritten, pointsToPlot1 + pointsToPlot2); lastIndex = lastWritten; } return 0;}//------------------------------------------------------------------------------// UpdateUIRWithData - This function will be called by a CVI Timer callback//------------------------------------------------------------------------------static int UpdateUIRWithData(void){ static lastIndex = 0; int lastWritten = dataArrayLastWritten; int pointsToPlot1; int pointsToPlot2; static int stripChart = PANEL_DATASTRIPCHART; static int written = PANEL_LASTWRITTEN; static int deltawritten = PANEL_DELTAWRITTEN; if (lastIndex!=lastWritten) { SetCtrlVal(panelHandle, written, lastWritten); if (lastIndex < lastWritten) { pointsToPlot1 = lastWritten - lastIndex; pointsToPlot2 = 0; PlotStripChart (panelHandle, stripChart, (double *)dataArray, pointsToPlot1, lastIndex, 0, VAL_DOUBLE); } else { pointsToPlot1 = dataArraySize - 1 - lastIndex; pointsToPlot2 = lastWritten+1; PlotStripChart (panelHandle, stripChart, (double*)dataArray, pointsToPlot1, lastIndex, 0, VAL_DOUBLE); PlotStripChart (panelHandle, stripChart, (double*)dataArray, pointsToPlot2, 0 , 0, VAL_DOUBLE); } lastIndex = lastWritten; } return 0;}//------------------------------------------------------------------------------// MonitorThreadCallback//------------------------------------------------------------------------------int CVICALLBACK MonitorThreadCallback (int panel, int control, int event, void *callbackData, int eventData1, int eventData2){ switch (event) { case EVENT_TIMER_TICK: SetCtrlVal(panelHandle, PANEL_RUNNINGLED, (timerId>0)); if (timerId) { UpdateUIRWithTime(); UpdateUIRWithData(); } break; } return 0;}//------------------------------------------------------------------------------// ChangeDelay//------------------------------------------------------------------------------int CVICALLBACK ChangeDelay (int panel, int control, int event, void *callbackData, int eventData1, int eventData2){ switch (event) { case EVENT_COMMIT: // Update UIR GetCtrlVal(panelHandle, PANEL_DELAY, &msDelay); SetAxisRange (panelHandle, PANEL_TIMESTRIPCHART, VAL_NO_CHANGE, 0.0, 1.0, VAL_MANUAL, 0.0, (msDelay<10)?20:msDelay*2.0); // Update timer if ((timerId) && (activeTimer == CVI_TIMER)) { SetCtrlAttribute(panelHandle, timerId, ATTR_INTERVAL, ((double)msDelay)/1000.0); } if ((timerId) && (activeTimer == ASYNC_TIMER)) { SetAsyncTimerAttribute(timerId, ASYNC_ATTR_INTERVAL, ((double)msDelay)/1000.0); } break; } return 0;}//------------------------------------------------------------------------------// DoWork !!! Remember that this function is being run in a different thread !!!//------------------------------------------------------------------------------// !!! Remember that this function is being run in a different thread !!!// // 1) Multithreading means that your variables are not protected from getting // written to by the main thread and this callback at the same time.//// 2) You are limited on what LW/CVI functions you can call when using this // inside the LW/CVI IDE. The LW/CVI libraries are not multithread safe in the// IDE. You should try to limit yourself to hardware type functionality only//------------------------------------------------------------------------------static int DoWork(int currentTime){ int index; int written, bytes, i; char charBuf[100]; // Lets keep track of when I get here if (timeArrayLastWritten < timeArraySize-1) index = timeArrayLastWritten + 1; else index = 0; // If timer was stopped, start out the first point at zero if (timerWasStopped) { timeArray[index] = 0; timerWasStopped = 0; } else timeArray[index] = currentTime - timeOfLastCall; timeArrayLastWritten = index; timeOfLastCall = currentTime; // Lets simulate some process control if (dataArrayLastWritten < dataArraySize-1) index = dataArrayLastWritten + 1; else index = 0; dataArray[index] = 10.0 * sin(currentTime*0.314/msDelay); dataArrayLastWritten = index; return 0; }int CVICALLBACK SetWhichTimerToUse (int panel, int control, int event, void *callbackData, int eventData1, int eventData2){ int timerRunning = timerId; switch (event) { case EVENT_COMMIT: if (timerRunning) ShutdownTimer(); if (activeTimer == CVI_TIMER) activeTimer = ASYNC_TIMER; else if (activeTimer == ASYNC_TIMER) activeTimer = CVI_TIMER; if (timerRunning) StartupTimer(); break; } return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -