LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

how to identify which switch in the panel called the function?

Solved!
Go to solution

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?

0 Kudos
Message 1 of 7
(3,632 Views)

Looping through controls is indeed a inefficient way of doing what you want! Smiley Wink

 

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.

 



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
Message 2 of 7
(3,629 Views)

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?

0 Kudos
Message 3 of 7
(3,623 Views)
Solution
Accepted by narendhiran

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.

 



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
Message 4 of 7
(3,621 Views)

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.

0 Kudos
Message 5 of 7
(3,615 Views)

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.



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
Message 6 of 7
(3,608 Views)

thanks...

0 Kudos
Message 7 of 7
(3,604 Views)