📄 asyncdem.c
字号:
case EVENT_COMMIT:
/* Update UIR */
GetCtrlVal (panel, PANEL_DELAY, &g_msDelay);
SetAxisRange (panel, PANEL_TIMESTRIPCHART, VAL_NO_CHANGE, 0.0, 1.0,
VAL_MANUAL, 0.0,
(g_msDelay < 10) ? 20 : g_msDelay * 2.0);
/* Update timer */
if ((g_timerId) && (g_activeTimer == CVI_TIMER))
SetCtrlAttribute (panel, g_timerId, ATTR_INTERVAL,
((double)g_msDelay) / 1000.0);
if ((g_timerId) && (g_activeTimer == ASYNC_TIMER))
SetAsyncTimerAttribute (g_timerId, ASYNC_ATTR_INTERVAL,
((double)g_msDelay) / 1000.0);
break;
}
return 0;
}
/*---------------------------------------------------------------------------*/
/* Respond to the GUI to set the current timer type. */
/*---------------------------------------------------------------------------*/
int CVICALLBACK SetWhichTimerToUse (int panel, int control, int event,
void *callbackData, int eventData1,
int eventData2)
{
int timerRunning = g_timerId;
switch (event)
{
case EVENT_COMMIT:
if (timerRunning)
ShutdownTimer (panel);
if (g_activeTimer == CVI_TIMER)
g_activeTimer = ASYNC_TIMER;
else if (g_activeTimer == ASYNC_TIMER)
g_activeTimer = CVI_TIMER;
if (timerRunning)
StartupTimer (panel);
break;
}
return 0;
}
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;
}
/*---------------------------------------------------------------------------*/
/* This is the Async. timer callback routine. */
/* */
/* !!! Remember that this callback may be run in a different thread !!! */
/* */
/* 1) Multithreading means that your global variables are not protected from */
/* being written to by the main thread and this callback at the same time.*/
/* You will have to protect them by making them thread-safe. */
/* */
/* 2) You should only call multithread-safe libraries in this routine. */
/* Generally, you will 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;
/* Like a regular UI timer, the async. timer's callback is passed the */
/* total elapsed time and the time since the last tick. */
if ((double*)eventData1)
currentTime = *((double*)eventData1);
if ((double*)eventData2)
deltaTime = *((double*)eventData2);
if (event == EVENT_TIMER_TICK)
DoWork ((int)(currentTime * 1000));
/* We will get an EVENT_DISCARD when the timer is destroyed. */
if (event == EVENT_DISCARD)
g_timerId = 0;
return 0;
}
/*---------------------------------------------------------------------------*/
/* This function will do most of the work in response to an async. timer tick*/
/*---------------------------------------------------------------------------*/
static int DoWork (int currentTime)
{
int index;
double timeValue[1];
double dataValue[1];
/* If timer was stopped, start out the first point at zero */
if (g_timerWasStopped)
{
timeValue[0] = 0;
g_timerWasStopped = 0;
}
else
{
timeValue[0] = currentTime - GetTimeOfLastCall();;
}
CmtWriteTSQData (g_timerQueueHdl, timeValue, 1, TSQ_INFINITE_TIMEOUT,
NULL);
SetTimeOfLastCall (currentTime);
/* Lets simulate some acquisition */
dataValue[0] = 10.0 * sin (currentTime * 0.314 / g_msDelay);
CmtWriteTSQData (g_dataQueueHdl, dataValue, 1, TSQ_INFINITE_TIMEOUT,
NULL);
index = GetIndex ();
index ++;
SetIndex (index);
return 0;
}
/*---------------------------------------------------------------------------*/
/* This routine responds to UI timer ticks in the main thread to periodically*/
/* update the GUI with information on the other timers. */
/*---------------------------------------------------------------------------*/
int CVICALLBACK MonitorTimersCallback (int panel, int control, int event,
void *callbackData, int eventData1,
int eventData2)
{
switch (event)
{
case EVENT_TIMER_TICK:
SetCtrlVal (panel, PANEL_RUNNINGLED, (g_timerId > 0));
if (g_timerId)
{
SetCtrlVal (panel, PANEL_LASTWRITTEN, GetIndex());
UpdateUIRWithTime (panel);
UpdateUIRWithData (panel);
}
break;
}
return 0;
}
/*---------------------------------------------------------------------------*/
/* This function will be called by the monitor CVI timer callback to update */
/* the UIR with time information on the other timers. */
//---------------------------------------------------------------------------*/
static int UpdateUIRWithTime (int panelHandle)
{
int nItems;
int nItemsRead;
double *timeValues;
CmtGetTSQAttribute (g_timerQueueHdl, ATTR_TSQ_ITEMS_IN_QUEUE, &nItems);
if (nItems > 0)
{
if ((timeValues = (double *) malloc (nItems *sizeof(double))) != NULL)
{
nItemsRead = CmtReadTSQData (g_timerQueueHdl, timeValues, nItems,
TSQ_INFINITE_TIMEOUT, 0);
if (nItemsRead > 0)
{
PlotStripChart (panelHandle, PANEL_TIMESTRIPCHART,
(double*)timeValues, nItemsRead, 0,
0, VAL_DOUBLE);
SetCtrlVal(panelHandle, PANEL_DELTAWRITTEN, nItemsRead);
}
}
free (timeValues);
}
return 0;
}
/*---------------------------------------------------------------------------*/
/* This function will be called by the monitor CVI timer callback to update */
/* the UIR with data information on the other timers. */
//---------------------------------------------------------------------------*/
static int UpdateUIRWithData (int panelHandle)
{
int nItems;
int nItemsRead;
double *dataValues;
CmtGetTSQAttribute (g_dataQueueHdl, ATTR_TSQ_ITEMS_IN_QUEUE, &nItems);
if (nItems > 0)
{
if ((dataValues = (double *) malloc (nItems *sizeof(double))) != NULL)
{
nItemsRead = CmtReadTSQData (g_dataQueueHdl, dataValues, nItems,
TSQ_INFINITE_TIMEOUT, 0);
if (nItemsRead > 0)
{
PlotStripChart (panelHandle, PANEL_DATASTRIPCHART,
(double*)dataValues, nItemsRead, 0,
0, VAL_DOUBLE);
SetCtrlVal(panelHandle, PANEL_DELTAWRITTEN, nItemsRead);
}
}
free (dataValues);
}
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -