#Events: a little surprise from UCCNC
Posted: Mon May 29, 2017 11:00 am
Over the development of UCCNC, first came the #Events tag in macros. This was made for putting special code in macros to handle forms events or other functions/subroutines. An other development was about the macro compilation. In the past every time a macro was called, it was first compiled. It was very slow. Now if Precompile switch is on the macros are compiled on program startup, or if not, they are compiled on first run. Of course, if a macro is changed, it will be compiled again.
These two developments have a little but very useful side-effect: we can use static variables after the #Events tag. These variables store their values until the macro is recompiled or UCCNC exits. A small example:
Running this macro more times you can see in the status window the increasing value.
Now we have unlimited possibilities. Let's make a tri-state button: every press executes the next function in cycle:
In the past you had to use #variables or - as the worst way - you could write values to the profile and read back. The first was only for numeric values and could interfere with the running g-code, the second is slow and messes up the profile file.
These two developments have a little but very useful side-effect: we can use static variables after the #Events tag. These variables store their values until the macro is recompiled or UCCNC exits. A small example:
- Code: Select all
AS3.Additemtolistbeginning("Value: " + value, 2);
++value;
#Events
static int value = 0;
Running this macro more times you can see in the status window the increasing value.
Now we have unlimited possibilities. Let's make a tri-state button: every press executes the next function in cycle:
- Code: Select all
switch (state)
{
case 1:
AS3.Additemtolistbeginning("First state", 2);
++state;
break;
case 2:
AS3.Additemtolistbeginning("Second state", 2);
++state;
break;
case 3:
AS3.Additemtolistbeginning("Third state", 2);
state = 1;
break;
}
#Events
static int state = 1;
In the past you had to use #variables or - as the worst way - you could write values to the profile and read back. The first was only for numeric values and could interfere with the running g-code, the second is slow and messes up the profile file.