02-27-2012 04:15 AM
here i have some 192 switches and 192 leds corresponding to each switch,
if a switch is on(say 10th) correspondingly led(10th) should glow.
i want to use the same function for all switche led pairs.
all i could do was to check for the on switch in a for loop and then set the particular led on, but i find it very inefficient way of doing it.
int CVICALLBACK switchOnOff (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2) is the function.
can i use any of these arguments to extract which switch(unique id for that switch) called the function so that i can switch corresponding led glow?
Solved! Go to Solution.
02-27-2012 04:58 AM - edited 02-27-2012 05:02 AM
Looping through controls is indeed a inefficient way of doing what you want!
With such a great number of controls, you could assign an appropriate callbackData to each of them when loading the panel. Next you could create an array of control IDs with all the LEDs and operate in the switch callback to turn on and off the corresponding led. A small example will be more clear:
At panel load:
int leds[200]; SetCtrlAttribute (panelHandle, PANEL_SWITCH1, ATTR_CALLBACK_DATA, (void *)1); leds[1] = PANEL_LED1; SetCtrlAttribute (panelHandle, PANEL_SWITCH2, ATTR_CALLBACK_DATA, (void *)2); leds[2] = PANEL_LED2; ... SetCtrlAttribute (panelHandle, PANEL_SWITCH192, ATTR_CALLBACK_DATA, (void *)192); leds[192] = PANEL_LED192;
In the switch callback:
int CVICALLBACK switchOnOff (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
int sts; if (event != EVENT_COMMIT) return 0; // Get the status of the switch GetCtrlVal (panel, control, &sts); // Set the led to the proper status SetCtrlVal (panel, leds[(int)callbackData], sts);
return 0;
}
For this solution to work, leds[] array must be global so that it is available to the control callback.
An additional improvement could be to modify the panel loading so that swithces and leds are created programmatically. You could have a single switch/led pair set in the UIR editor and next have a loop that duplicate the controls, sets callbackData and loads the array of led ids.
02-27-2012 05:15 AM
thanks for the answer. but as iam very new to cvi i did not understand what you meant by "at panel load"
is it main? or some other function?
02-27-2012 05:18 AM - edited 02-27-2012 05:19 AM
It has occurred to me now that the the array is not needed at all: you could simply assign to each switch the corresponding led ID in the callbackData, so that the code is even simpler than my previous one:
// At panel load: SetCtrlAttribute (panelHandle, PANEL_SWITCH1, ATTR_CALLBACK_DATA, (void *)PANEL_LED1); ... SetCtrlAttribute (panelHandle, PANEL_SWITCH192, ATTR_CALLBACK_DATA, (void *)PANEL_LED192); // In the control callback GetCtrlVal (panel, control, &sts); SetCtrlVal (panel, (int)callbackData, sts);
To answer your last question, "at panel load" means immediately after the call to LoadPanel.
02-27-2012 05:22 AM
i do not call anything to load the panel..
i just run and the panel gets loaded.
i may be so foolish but i have started to use it just before 3 days.
02-27-2012 06:07 AM
If you look in the code you should see a LoadPanel () instruction somewhere: you may have not explicitly put it in and let CVI add it for you while generating the code for the panel you have created.
If you are so new to CVI you should consider spending some time reading the Getting started manual or going through the tutorial: making on some solid basis can help you a lot in your further activity.
02-27-2012 06:43 AM
thanks...