1.1.1 Callback function design method
In the LabWindows/CVI programming system, a program can be divided into several program modules, each of which is used to implement a specific function. These modules can be subroutines or callback functions. A LabWindows/CVI application consists of a main function and several other functions. The main function calls other functions. Other functions can also call each other, and some commonly used functions can be written into functions for other modules to call. To improve code utilization and reduce the amount of work written by the program. In fact, the main program is the entry point of the user function logic, and any C language program needs to enter the message loop of the program through the main function.
The callback function is a very important means in the design of the system framework. The so-called callback function (callback) refers to a function called by a function pointer. The callback function can be designed by the user and called by the system, mainly for intercepting messages, obtaining system information, or handling exception events. The callback function must follow the pre-specified parameter format and delivery method, otherwise it will cause the program or system to crash. When programming with LabWindows/CVI, the framework is used to determine the main processing flow, and some specific implementations are given to the user. Using a callback function is actually passing the address of a function (this function is a callback function) as a parameter to another function when calling a function. Another function calls the callback function with the passed address to process the message or perform a certain operation when needed. For example, the qsort function in the C library can receive a function pointer as a parameter to determine the sorting strategy. The method used is the callback function. For example, when using Windows for system message processing, if the user registers a callback function, the callback function is called when the message is triggered in the system, so that the user logic can be executed.
In LabWindows/CVI, the system message loop is responded to in the form of a callback function. The callback function responds to all events generated in the User Interface Library, and its callback function prototype definition is stored in the userint.h header file. Callback functions can be installed on panels, menus, controls, etc. For specific interface objects, LabWindows/CVI will assign appropriate callback functions to make the program work. Including system idle (Idle) events and end-task events can be responded to and executed through the main callback function.
In the LabWindows/CVI system, events are generated and passed to the callback function through the GUI interface. If the callback function receives a mouse click (EVENT_LEFT_CLICK) event from the user interface, along with some related information, it can be recorded, including the X-axis (eventData2), Y-axis (eventData1) coordinates, panel, and control of the mouse in the callback function. Control) information, and can pass user-defined data through callback data.
The callback function macro in LabWindows/CVI is defined as CVICALLBACK stored in the cvidefs.h header file, defined as: #define CVICDECL __cdecl
#define CVICALLBACK CVICDECL
CVICALLBACK is often used to define function pointers,
Such as: typedef void (CVICALLBACK * MenuDimmerCallbackPtr) (int menuBar, int panel);
It's worth noting that the CVICALLBACK macro definition takes precedence over functions when compiling to ensure any user interface library functions.
The number is compiled in cdecl mode, even under the stdcall calling convention.
In LabWindows/CVI, five types of objects can trigger callback functions through events, namely control trigger, panel trigger, menu trigger, timer trigger, and main callback function trigger. The callback function trigger priority is defined as follows.
Control trigger priority:
â— control callback function
â— panel callback function (keyboard and mouse events)
â—Main callback function
Panel trigger priority:
â— panel callback function
â—Main callback function
Menu trigger priority:
â— menu item callback function
â—Main callback function
Timer trigger priority:
â— control callback function
The main callback function triggers the priority:
â—Main callback function
It's worth noting that the EVENT_COMMIT event is stored in the user event queue, via the GetUserEvent function.
Passed to all callback functions.
1.1.2 Callback function programming
(1) Panel design
Write a pseudo-random signal generator program and display the generated data in the Graph control, and display the file name of the generated program in the String control. In order to center the entire panel, double-click the panel to bring up the Edit Panel dialog box, select Auto-Center VerTIcally (when loaded) and Auto-Center horizontally (when loaded), and click the "Other Attributes..." button to select Movable, Can Minimize, TItle Bar Visible, Use Windows Visual Styles for Controls item. The panel design is shown in Figure 1-1. The main control attribute settings in the panel are shown in Table 1-1.
Figure 1-1 Callback function panel
Table 1-1 Control property setting table
(2) program source code
/ / header file declaration, the system automatically added
#include "ansi_c.h"
#include 《cvirte.h》
#include "userint.h"
#include "callback function.h"
//Global static variable
staTIc int panelHandle;
/ / Main function
Int main (int argc, char *argv[])
{
/ / Initialize the LabWindows / CVI runtime library engine
If (InitCVIRTE (0, argv, 0) == 0)
/ / If the return value is 0, the initialization fails, returning -1
Return –1;
/ / Load the panel, return the panel handle
If ((panelHandle = LoadPanel (0, "callback function .uir", PANEL)) " 0)
/ / If the load panel fails, return -1
Return –1;
/ / Get the string in *argv[], which is the file name
SetCtrlVal (panelHandle, PANEL_STRING, argv[0]);
/ / Display panel
DisplayPanel (panelHandle);
/ / Run the user interface
RunUserInterface ();
/ / Delete the panel handle
DiscardPanel (panelHandle);
/ / The main function is executed successfully, returning 0
Return 0;
}
/ / panel callback function
Int CVICALLBACK PanelCB (int panel, int event, void *callbackData,
Int eventData1, int eventData2)
{
Switch (event)
{
// panel response event
Case EVENT_CLOSE:
// call the EVENT_COMMIT event of the exit button
QuitCallback (panelHandle, PANEL_QUITBUTTON, EVENT_COMMIT, 0, 0, 0);
Break;
}
/ / function returns a value, 0 means success
Return 0;
}
//Exit button
Int CVICALLBACK QuitCallback (int panel, int control, int event,
Void *callbackData, int eventData1, int eventData2)
{
If (event == EVENT_COMMIT)
{
/ / Exit the user interface
QuitUserInterface (0);
}
Return 0;
}
//Show button
Int CVICALLBACK OkCallback (int panel, int control, int event,
Void *callbackData, int eventData1, int eventData2)
{
/ / Define local variables
Int i;
Double datapoints[100];
Switch (event)
{
Case EVENT_COMMIT:
// Generate 100 random numbers into the array datapoints
For (i = 0; i "100; i++)
{
Datapoints[i] = rand() / 32767.0 * 100.0;
}
/ / Clear the waveform drawn in the previous Graph
DeleteGraphPlot (panelHandle, PANEL_GRAPH, -1, VAL_IMMEDIATE_DRAW);
// draw the waveform in the Graph
PlotY (panelHandle, PANEL_GRAPH, datapoints, 100, VAL_DOUBLE, VAL_THIN_LINE,
VAL_EMPTY_SQUARE, VAL_SOLID, 1, VAL_RED);
Break;
}
Return 0;
}
3: Program comments
1 main function
Every C program must start with a main function, return to the main function after calling other function flows, and end the entire program in the main function. In fact, the main function can be placed anywhere in the program: some programmers like to put it first, while others put it at the end, no matter where it is placed, the following points are appropriate. .
In C, the main function can take three arguments: argc, argv, and env.
Argc : An integer type indicating the number of command line arguments passed to the main function, which is normally 1.
*argv[] : An array of two-dimensional strings. In LabWindows/CVI, argv[0] is the file name of the program runtime, which is related to the compilation settings. There are two options under the menu Build→ConfiguraTIon, namely: Release and Debug. When selecting Release, argv[0] adds ".exe" to the current project name; when Debug is selected, argv[0] adds "_dbg.exe" to the current project name. Argv[argc] is NULL.
*env: A two-dimensional array of strings, which is an environment variable. In LabWindows/CVI, env[] is generally an empty string and is omitted.
LabWindows/CVI always passes these three parameters to the main function when starting. The order of the parameters is: argc, argv, env, which can be explained in the user program or not. If some or all parameters are specified, they are It becomes a local variable of the main main function. The main main function is declared in the following ways:
Main (void)
Main (int argc, char *argv[])
Main (int argc, char *argv[], char *env[])
2 InitCVIRTE function
Initialize the LabWindows/CVI runtime (library) engine. Called when using the external compiler Visual C++, Borland C++ Builder, if you do not use an external compiler, it will not affect the normal operation of the program. The function prototype is:
Int InitCVIRTE (void *HInstance, char *Argv[], void *Reserved);
*HInstance: should be 0 for the main function; hInstance for the WinMain function; for DllMain it should be
hInstDLL.
*Argv[] : Corresponds to the *argv[] parameter of the main function.
*Reserved: Reserved parameter, set to 0.
Generally, when using the main function, WinMain function, DllMain function, the parameter setting of the InitCVIRTE function is slightly different.
Similarly, the specific calling method is as follows:
Main function
Int main (int argc, char *argv[])
{
If (InitCVIRTE (0, argv, 0) == 0)
Return –1; /* out of memory */ //user program
Return 0;
} WinMain function
Int __stdcall WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int
nCmdShow)
{
If (InitCVIRTE (hInstance, 0, 0) == 0)
Return –1; /* out of memory */ //user program
Return 0;
} DllMain function
Int __stdcall DllMain (void *hinstDLL, int fdwReason, void *lpvReserved)
{
If (fdwReason == DLL_PROCESS_ATTACH)
{
If (InitCVIRTE (hinstDLL, 0, 0) == 0)
Return 0;
/ / User ATTACH program
}
Else if (fdwReason == DLL_PROCESS_DETACH)
{
/ / User DETACH program
CloseCVIRTE ();
}
Return 1;
}
The LabWindows/CVI runtime library engine is primarily used for program publishing and is installed separately on other computers. The runtime libraries include: User Interface Library, Advanced Analysis Library, Formatting and I/O Library, Utility Library, ANSI C Library, RS-232 Library, TCP Support Library, Internet Library, Network Variable Library, DDE Support Library, ActiveX Library, DIAdem Connectivity Library, TDM Streaming Library, .NET Library, etc.
3 The LoadPanel function loads the user interface file (*.uir) or the text user interface (*.tui) into memory. After loading, the panel is not visible and you need to call the DisplayPanel function to display the panel.
The function prototype is: int LoadPanel (int Parent_Panel_Handle, char Filename[], int Panel_Resource_ID); Parent_Panel_Handle : parent panel handle. If it is 0, it means that the loaded panel is the top-level window; if it is a panel handle, it means that the loaded panel is the sub-panel of the panel.
Filename[] : The filename of the user interface file (*.uir) or the text user interface (*.tui). Can contain all path names or only a simple file name, if it is a simple file name, it must be in the same directory as the project file. Panel_Resource_ID : Panel constant name.
Return value: panel handle.
4 The DisplayPanel function displays the panels loaded in memory. The function prototype is:
Int DisplayPanel (int Panel_Handle);
Panel_Handle : The panel handle.
5 RunUserInterface function
Run the user interface and respond to callback function events. RunUserInterface always runs after the program starts until it is called
Returned only when the QuitUserInterface function. The function prototype is:
Int RunUserInterface (void);
Return Value: Returns the value set by the user in the parameters of the QuitUserInterface function.
6 QuitUserInterface function
The QuitUserInterface function does not directly terminate the running of the program. Instead, it causes the RunUserInterface function to return a specific value and enters the terminating program to run the process.
Static int panelHandle;
Int main (int argc, char *argv[])
{
Int status;
If (InitCVIRTE (0, argv, 0) == 0)
Return –1;
If ((panelHandle = LoadPanel (0, "sample.uir", PANEL)) 0)
Return –1;
DisplayPanel (panelHandle);
/ / The return value status is 10, namely: the parameter setting value of the QuitUserInterface function
Status = RunUserInterface ();
DiscardPanel (panelHandle);
Return 0;
}
//Exit button
Int CVICALLBACK QuitCallback (int panel, int control, int event, void *callbackData, int eventData1, int
eventData2)
{
Switch (event)
{
Case EVENT_COMMIT:
// The parameter value of QuitUserInterface is 10, which is the return value of RunUserInterface
QuitUserInterface (10);
Break;
}
Return 0;
}
7 The DiscardPanel function releases the panel resources, including the subpanels under the parent panel. The function prototype is:
Int DiscardPanel (int Panel_Handle);
Panel_Handle : The panel handle.
8 SetCtrlVal function sets the control value. The function prototype is:
Int SetCtrlVal (int Panel_Handle, int Control_ID, ...);
Panel_Handle : The panel handle.
Control_ID: Control constant, usually declared in the header file. …:Settings.
The 9 rand function produces a pseudo-random number between 0 and 32767. The function prototype is:
Int rand (void);
Return value: pseudo-random number.
10 DeleteGraphPlot function deletes the drawing drawn by the control. The function prototype is:
Int DeleteGraphPlot (int Panel_Handle, int Control_ID, int Plot_Handle, int Refresh); Panel_Handle : Panel handle.
Control_ID: Control constant.
Plot_Handle : A drawing handle indicating the graphic to be deleted. If it is –1, all graphics are deleted.
Refresh: Refresh method. There are three main refresh methods, including: VAL_DELAYED_DRAW, VAL_IMMEDIATE_ DRAW, VAL_NO_DRAW.
11 The PlotY function draws a graph along the X-axis, where the Y-axis is the data point. The function prototype is:
Int PlotY (int Panel_Handle, int Control_ID, void *Y_Array, int Number_of_Points, float Y_Data_Type[], int Plot_Style, int Point_Style, int Line_Style, int Point_Frequency, int Color);
Panel_Handle : Panel handle, which refers to the panel in which the control is located.
Control_ID: Control constant.
*Y_Array: An array of data points for drawing graphics whose data type is the type specified by Y_Data_Type[]. Number_of_Points : the number of data points to draw the graph, the number of data points contained in *Y_Array should be not less than
Number of data points specified by Number_of_Points. Y_Data_Type[] : The data type whose data type is shown in Table 1-2.
Table 1-2 Y_Data_Type data type table
Table 1-3 Plot_Style Curve Type Table
Point_Style: The data point type. The type of data point determines VAL_CONNECTED_POINTS or VAL_SCATTER
The type of tag, the default value is VAL_EMPTY_SQUARE. The main types are shown in Table 1-4.
Table 1-4 Point_Style data point type table
Note: In LabWindows/CVI 8.0 or higher, VAL_EMPTY_SQUARE_WITH_CROSS cannot be automatically switched. You need to enter this value manually.
Line_Style : Line type. The main types are shown in Table 1-5.
Table 1-5 Line_Style line style table
Point_Frequency : The frequency at which data points are drawn when the curve type is VAL_CONNECTED_POINTS or VAL_SCATTER. The default is 1.
Color: The color value. For a 4-byte integer RGB value, expressed in hexadecimal as 0x00RRGGBB, you can use the MakeColor function to customize the color.
Return value: The handle to draw the graphic. A positive value indicates that the curve is drawn successfully, and a negative value indicates that an error has occurred. If the Graph's ATTR_DATA_MODE property is set to VAL_DISCARD, the return value is 0.
The call of the 12 function for the control, its callback function prototype is:
Int CVICALLBACK ControlCallback(int panel, int control, int event, void *callbackData, int eventData1, int eventData2);
Panel: The handle of the panel where the control is located.
Control: control constant.
Event: The event that the control responds to.
*callbackData : Callback data.
eventData1 : Corresponds to the setting value of the specific control response event.
eventData2 : Corresponds to the setting value of the specific control response event.
The program calls the QuitCallback function in the EVENT_CLOSE event of the panel. The calling format is:
QuitCallback (panelHandle, PANEL_QUITBUTTON, EVENT_COMMIT, 0, 0, 0);
That is, the EVENT_COMMIT event (left click event) of the PANEL_QUITBUTTON constant (exit button) in the panel where the handle of the panelHandle is located.
The transfer of parameters in the 13 callback function for the exit button, its callback function is:
Int CVICALLBACK QuitCallback (int panel, int control, int event,
Void *callbackData, int eventData1, int eventData2)
If (event == EVENT_COMMIT)
QuitUserInterface (0); } return 0;
}
When a left click event occurs, a constant value is passed to the event parameter, and if the value is EVENT_COMMIT, the function is executed. Its function can also be written in the standard LabWindows/CVI form, the two functions are exactly the same, but the form is different.
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;
}
14 Hotkey settings in the panel
The display and exit buttons in the panel are represented by display (S) and exit (Q). The design is indicated by display (__S) and exit (__Q), indicating that the program can be controlled by keyboard or mouse. To display the graph, you can press the Alt + S key combination. If you want to exit the program, you can press the Alt + Q key. Generally, the combination of the Alt key and the letter key is called the hot key. The shortcut key (Shortcut Key) uses a slightly different form of Ctrl and letter combination, such as the cut operation in Word. If you use the shortcut key to complete, just press Ctrl + X, if you use hotkey To do this, first press the Alt + E key to activate the edit menu, then press the T key to complete the cut operation. The hot key generally requires the key value to be visible in the interface, and the shortcut key can be applied without being visible. The commonality of the two is that a specific task is combined through a few special keys on the keyboard, which is more common in menu design and can greatly improve work efficiency.
(4) Operation effect chart
Click the Debug Project button in the toolbar to start the program. The effect is shown in Figure 1-2.
Figure 1-2 Operation effect diagram
Dongguan Yijia Optoelectronics Co., Ltd. , https://www.everbestlcdlcms.com