LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

A button, led and switch

Hi. I'm new in the LabWindows CVI and i put to panel a toggle button, a led and a binary switch. I want to turn on the led when i pressing the button and to change the color of led from switch. I put all components to panel, i set a callback function for each, i generate all callbacks from menu and the next step i think it is to use GetCtrlVal and SetCtrlVal, but i don't know how to use this function and where to use. I need help..I had never used LabWindowsCvi before

0 Kudos
Message 1 of 16
(6,621 Views)

The case with button and the led it's solved. Now i don't know how to change the color of led using that switch..

0 Kudos
Message 2 of 16
(6,598 Views)

You are correct that GetCtrlVal and SetCtrlVal are the two functions to use here. Did you have a look at the many example programs that ship with CVI? Almost all of them use these two basic functions. Try to learn about the debug functions such that you can step through the code line by line and see how the functions affect the UI.

 

Setting the LED will look something like SetCtrlVal ( panel_handle, PANEL_LED_10, 1 ); This will switch the LED ON and accordingly change its color from OFF_COLOR to ON_COLOR. The other relevant function here is SetCtrlAttribute, e.g. SetCtrlAttribute ( panel_handle, PANEL_LED_10, ATTR_ON_COLOR, VAL_RED ); SetCtrlAttribute ( panel_handle, PANEL_LED_10, ATTR_OFF_COLOR, VAL_GREEN );

0 Kudos
Message 3 of 16
(6,593 Views)

Here is how i did it :

 

int CVICALLBACK chooseColour (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
int switchState;
switch (event)
{
case EVENT_COMMIT:
GetCtrlAttribute(panelHandle, PANEL_LED,ATTR_CTRL_VAL, &switchState);
if (switchState == 1)
{
SetCtrlAttribute(panelHandle, PANEL_LED,ATTR_CTRL_VAL, 0);
}
else
SetCtrlAttribute(panelHandle, PANEL_LED,ATTR_CTRL_VAL, 1);
break;
....

 

i'm curious about yours 🙂

0 Kudos
Message 4 of 16
(6,563 Views)

Well, your code switches the led from on to off and vice versa and can be simplified as follows:

 

int status;

case EVENT_COMMIT: GetCtrlVal (panel, control, &status); SetCtrlVal (panel, control, !status);
break;

 

Wolfgang has added the elements to change the color the led assumes while turned on (ON state): depending on your needs you may want a alarm signal (red), a OK signal (green), a warning (normally yellow) or other colors for your own purposes.



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?
0 Kudos
Message 5 of 16
(6,558 Views)

Thanks for suggestions. I succeed to change the color from binary switch, but i want to change only the color from switch ( not to activate the led) and turn on/off the led only from button. I don't know how to implement the function.. For example. I have switch off ( for switch off to have a blue color when i press the button) and when i have switch on ( for switch on, i want a green color). I don't know how to make the switch to send his status (on (blue) /off (green)) to button without to turn on the led. I implement what you said and i succed to change the color of led but i want to activate the led only from button..I think you understand what i want to do..Any advice is welcome

0 Kudos
Message 6 of 16
(6,536 Views)

I may be missing something, but the solution seems quite simple to me.

 

You have a callback for the switch into which you can manipulate the desired led color with SetCtrlAttribute as Wolfgang suggested. You must consider that a switch button has 2 stable states, and you can read the switch state with GetCtrlVal: it will return 1 when the switch is on, 0 when off, so in the commit event case read the switch button and set the led color accordingly.

 

On the other hand, you have the button callback that must switch the led on and off with SetCtrlVal.

 

I suppose you have all elementary bricks in your hand: now you must try and build up your building.



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?
0 Kudos
Message 7 of 16
(6,526 Views)

Finnaly i solved. it wasn't hard ..

 

int CVICALLBACK switchcb (int panel, int control, int event,  //switchcb - callback for switch..
void *callbackData, int eventData1, int eventData2)
{

int switchState;

switch (event)
{
case EVENT_COMMIT:
GetCtrlAttribute(panelHandle, PANEL_switch,ATTR_CTRL_VAL, &switchState);
if (switchState == 0)
{
SetCtrlAttribute(panelHandle, PANEL_LED,ATTR_ON_COLOR, VAL_BLUE);
}
else
SetCtrlAttribute(panelHandle, PANEL_LED,ATTR_ON_COLOR, VAL_GREEN);
break;
case EVENT_LEFT_CLICK:

break;
}
return 0;
}

0 Kudos
Message 8 of 16
(6,518 Views)

Here is the button callback

 

int CVICALLBACK TurnOn (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
switch (event)
{
case EVENT_COMMIT:
SetCtrlAttribute(panelHandle,PANEL_LED,ATTR_DIMMED, 0);
break;

}
return 0;
}

 

and the switcher callback

 

int CVICALLBACK ChooseColour (int panel, int control, int event,
void *callbackData, int eventData1, int eventData2)
{
int state;
switch (event)
{
case EVENT_COMMIT:
GetCtrlAttribute(panelHandle, PANEL_LED,ATTR_CTRL_VAL, &state);
SetCtrlAttribute(panelHandle, PANEL_LED,ATTR_CTRL_VAL, !state);
break;
}
return 0;

}

 

you activate the led only from the buton, and change the colour from the switcher as you said...

 

0 Kudos
Message 9 of 16
(6,504 Views)

pleasure ha scritto:

you activate the led only from the buton, and change the colour from the switcher as you said...

 


Hi pleasure,

I have to disagree with your solution: turning on a LED has nothing to do with dimming it.

 

Turning a LED on means changing its state from 0 to 1 by using SetCtrlVal (or SetCtrlAttribute with ATTR_CTRL_VAL, which is the same): what you are doing in your ChooseColour function.

 

You can edit LED properties and play with both Initial State and Initially dimmed parameters and see in the preview box which is the effect of both: each of four combinations of those attributes is valid, but it will have a different visual effect as you can see here:

LED.png

 

 

Keep in mind, additionally, that dimming a control has usually the meaning of declaring it inactive in the current situation: it is well different from an off led!



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?
0 Kudos
Message 10 of 16
(6,500 Views)