LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Numeric control resets itself with keypress event

I have a problem with an application I've written which I can't figure out. I have a numeric control with a callback function, which provides different responses depending on the event which occurs. The code is as follows:

int CVICALLBACK size_adjust (int panel, int control, int event, void *callbackData, int eventData1, int eventData2)
{
int i = 0, j = 0;

switch (event) {
case EVENT_KEYPRESS:
if (eventData1 == 1280 && control == PANEL_SLEWTO) {
if (workaxis < 2) i = adjust (control);
else i = 1;
if (i) {
SetActiveCtrl (panelHandle, PANEL_SLEWBUTTON);
FakeKeystroke (1280);
}
}
break;
case EVENT_COMMIT:
if (workaxis > 1) break;
adjust (control);
break;
}
return 0;
}

int adjust (int controla)
{
int ivalue = 0;
float value = 0.00, dumb = 0.000, dumber = 0.000;

GetCtrlVal (panelHandle, controla, &value);
dumb = value / 0.00400;
ivalue = RoundRealToNearestInteger (dumb);
dumber = ivalue * 0.004;
SetCtrlVal (panelHandle, controla, dumber);
return 1;
}

My version of CVI is 5.0.1, on a machine with Win95. The numeric is set for float variables, with min and max set to infinety. Range checking is set to notify, and control mode is set to hot.

This numeric is used to set a target position for stepper motors. The system has six motors, with different resolutions, and the positions fo the first two sometimes need to be adjusted. In the callback, workaxis can be 0 to 5. If I have workaxis set anywhere from 2 to 5, and I enter a value in the numeric from the keyboard, and hit return, the program works properly. The active control is set to the UIR command button which causes the motor to start moving, and the fake key stroke causes a commit event, and the motor moves. This is the equivalent to entering a value in the numeric control and clicking on the command button.

The problem is when workaxis is 0 or 1. When I enter a value in the numeric (using the keyboard) and hit return, the value is changed back to whatever it was before I changed it. The function adjust runs and the active control is set to the "go" button, and the fake keystroke occurs, but no movement takes place, because the value which adjust initially reads is the same as the current position of the motor. My code all runs, it's just that the numeric doesn't keep the value I entered prior to hitting enter. If I change the value of the numeric with the keyboard, and then hit the "go" button with a mouse click, the motor moves to the new position. If I change the value of the numeric with the up/down arrow controls of the numeric, and hit return, the motor moves properly. It's only when I enter a value in the numeric from the keyboard and hit enter, that the value is changed back to the original value prior to the callback code running. Again, if workaxis is greater than 1, entering the numeric value from the keyboard and hitting enter causes the motor to move, just as intended.

By the way, if workaxis is less than two, and I enter a value from the keyboard which needs adjusting, and then click somewhere else with the mouse, the value is properly adjusted and the adjusted value displayed in the numeric.

Can you see any reason for this behavior?

Thank you
0 Kudos
Message 1 of 3
(3,142 Views)
Yes, the reason is that the KEYPRESS event is triggered before the control changes its value, so that (in case you need it) you could swallow this event. So when your function is triggered and you retrieve the control's value, you get the one previous to the keypress and your motor doesn't move at all.

Try modifying your function to use EVENT_VAL_CHANGED instead of keypress event: this event is triggered after the control has changed its value but before the commit event (which btw is still triggered, so in my opinion you could avoid the fakekeystroke: try putting a breakpoint inside the EVENT_COMMIT case and see if it is executed twice).

Hope this helps
Roberto


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 2 of 3
(3,142 Views)
Thank you for your reply. Your explanation made me realize why the numeric reverted to it�s original value, based on how I was using the callback. What I need is for the callback to finish prior to using the GetCtrlVal call (in my original code, if the workaxis was greater than 1, this is what happened, and is why the callback worked in that case, and not when workaxis was less than 1). I�m not sure using VAL_CHANGED will work for me because I also use the increase/decrease arrows on the numeric. However, I have come up with a kind of clunky solution which works, and with a little more thought I can probably do better. However, I wouldn�t have recognized the need for the original callback to finish without your reply.

Thanks again
sbvande
0 Kudos
Message 3 of 3
(3,142 Views)