Using custom buttons in a macro

This is where you talk about Macros, show examples of your macro scripting and SHARE handy segments of script code as examples.

Using custom buttons in a macro

Postby CNC22369 » Mon Sep 19, 2016 6:18 pm

I'm trying to write some macros that will allow me to use custom screenset buttons to control some external devices via selected Port/Pins. To check the feasibility of doing this, I created two custom screenset button types and a custom LED, wrote several simple macros, and setup the macros to loop (one at a time).

I was successful in getting a macro to read the state of a custom non-toggle button and to then set the custom LED accordingly (which in turn set an output Port/Pin that I had associated with the custom LED). When the button was pressed (and held) the button state changed and the LED and output pin tracked it. When the button was released, the LED and output pin returned to their original state. The macro code is:

bool button = AS3.Getbutton(1060); //read the state of custom non-toggle button 1060
AS3.SetLED(button, 950); //set custom LED 950 to state of button 1060

I was unsuccessful in getting a macro to read the state of a custom toggle button. The macro code is:

bool button = AS3.Getbuttonstate(1050); //read the state of custom toggle button 1050
AS3.SetLED(button, 950); //set custom LED 950 to state of button 1050

This code does not work (for me) -- apparently the state is not being read from the custom toggle button (the button does visibly change when pressed). But, interestingly, if I change the button number to an existing screenset toggle button (for example, 114 -- spindle on CW) the code works fine (I get the right responses).

So, I'd appreciate any advice (or examples) on how to read the state of a custom toggle button (I'm assuming some additional code is needed). I'm not a programmer, so I may be missing something important needed to make this work.

Thank you, Frank
CNC22369
 
Posts: 35
Joined: Mon Sep 19, 2016 4:05 pm

Re: Using custom buttons in a macro

Postby cncdrive » Mon Sep 19, 2016 8:17 pm

Frank,

You can use the AS3.Getbuttonstate function to read the state of the toggle type buttons,
while the AS3.Getbutton tells you if the button is being pressed.

I also think that it is better to make this not in a loop, but event driven and by that I mean that macros with numbers 20000 to 20999 calls the same macros directly,
we call these macro call button codes. So, if you place a macro on the screen and set the buttonnumber parameter to for example 20000 then that button will call the M20000 macro.
So, you can then create the M20000 macro and place your code there which you want to execute when the button is pressed.

One more thing is that to switch a toggle type button's visual state from on to off or from off to on then you have to use this function:

Function: void Switchbutton(bool Ison, int Buttonnumber)
Description: This function works with toggle type buttons and switches the button to on/off states. If the Ison parameter is true switches the button to the on state and false switches to the off state. The function can be used for example in buttons' macros to change the state of the button toggling it's visual state.
Example: AS3.Switchbutton(true, 128);

So, you can add the Switchbutton code into your macro and then you could control in your macro when you want to change the button visually to show the on and off states.
cncdrive
Site Admin
 
Posts: 4887
Joined: Tue Aug 12, 2014 11:17 pm

Re: Using custom buttons in a macro

Postby CNC22369 » Wed Sep 21, 2016 3:49 pm

Thanks for the feedback on using an event driven button rather than looping on a macro. This is certainly the way to go, but when I tried it with a non-toggle button, I got different results than when I looped the macro (see 1st post). I setup the non-toggle button as button 20600 and also named the macro 20600. When I pressed and released the button quickly, nothing happened --- I had to hold it pressed for a short bit before I got a changed state on the LED --- it feels like the event driven operation is slow to recognize a button pressing and thus missed the quick press (macro did not execute). When the LED did change and I then released the button, the LED did not change back to its original state due to the fact that the original short looping macro did not have (or need) a way to wait for the button to be released (a subsequent loop of the macro reset the LED). So I added a loop within the macro to wait for button release and then to change the LED state. I don't like to use an infinite loop, but (as a non-programmer) it was the only way I could think of to wait for the button to be released. As far as the initial button pressing response time goes, that's still an issue so I may need to use a looping macro anyway. Here's the updated event driven macro test code for a non-toggle button:

//macro runs once when button is pressed
int bnum = 20600;
bool button = AS3.Getbutton(bnum); //read state of non-toggle button
AS3.SetLED(button, 960); //set led to state of button
while(button) //wait for button to be released
{
button = AS3.Getbutton(bnum);
}
AS3.SetLED(button, 960);

FYI, I'm doing this testing on the latest UCCNC running in DEMO mode.
Also, I haven't been able to get the toggle button to work yet in either the event driven mode or loop mode using AS3.Getbuttonstate(). I'll provide more info after I experiment some more.

Comments/advice appreciated.
Frank
CNC22369
 
Posts: 35
Joined: Mon Sep 19, 2016 4:05 pm

Re: Using custom buttons in a macro

Postby cncdrive » Wed Sep 21, 2016 5:03 pm

As said the getbutton checks if the button is being pressed, to check the visual state (on/off) of toggled typed buttons you have to use the getbuttonstate.
The following is a simple on/off switching code in macro 20000 which you can use with buttonnumber = 20000:

if(AS3.Getbuttonstate(20000))
{
AS3.Switchbutton(false, 20000);
//Do something...
}
else
{
AS3.Switchbutton(true, 20000);
//Do something...
}
cncdrive
Site Admin
 
Posts: 4887
Joined: Tue Aug 12, 2014 11:17 pm

Re: Using custom buttons in a macro

Postby CNC22369 » Wed Sep 21, 2016 5:51 pm

Thanks for the snippet of macro code. I now understand how to setup a toggle button event driven macro and have written one that does what I want it to do and it works perfectly. Thanks again.
CNC22369
 
Posts: 35
Joined: Mon Sep 19, 2016 4:05 pm

Re: Using custom buttons in a macro

Postby cncdrive » Wed Sep 21, 2016 7:51 pm

I'm glad you got it, once you understand the logic of it then it becomes easy to work with. :)
cncdrive
Site Admin
 
Posts: 4887
Joined: Tue Aug 12, 2014 11:17 pm


Return to Macros

Who is online

Users browsing this forum: No registered users and 1 guest