Macros – Introduction
Macros are used to add user-defined functionality to drivers. They are written in the TipControl language and then added to a driver under a user-defined name. Once added, you can call them through drv.<driverName>.<macroName>
, for example, drv.led.redToBlue()
where redToBlue
could be a macro for gradually turning the LED from red to blue. The same macro can be added to another driver under a different name. Macros can take parameters and are invoked just like the pre-built driver functions.
Macros – Managment
The best way to manage (add, rename, remove, save) your macros is through the TC Designer. Navigate to the menu via GoTo -> Macro or use the top menu to open the macro editor. Under ‘Location’, you can choose either a driver from the current device where a macro is already installed or select ‘Home’ to store your macros locally. Right-clicking on a macro allows you to rename or remove it, while left-clicking loads the macro into the code editor. Using the “Install Macro to” field, you can select the device, driver, name, and optional parameters before installing a macro. The save button lets you store your current macro locally.
Macros – the Parameters
Macro parameters are entered by their name, separated by commas, e.g., x, y
. You don’t need to specify a type for the parameter definition. The last parameter can be extended by using ...
, for example, contact(message, names...)
. If you add a *
before a parameter, it means that the parameter is passed directly to the macro, not copied. Thus, changes made to this parameter will affect the variable used when calling the function.
Macros – Coding
Macros are coded in the TipControl language. The me
keyword in a macro refers to the driver it is installed on. For example, if the macro is installed on a digitalIO(led), you can turn it on using me.on()
. The self
keyword is used for recursive calls within the macro.
Here’s a small example of calculating the distance between points (x1, y1) and (x2, y2). In the macro editor, the code would look like this:
{
dx = lib.math.abs(x1-x2);
dy = lib.math.abs(y1-y2);
distance = lib.math.sqrt(dx*dx+dy*dy);
return(distance);
}
or simplified just
{
return(lib.math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)));
}
now select a device and driver, perhaps call the macro distance and in parameters enter x1,x2,y1,y2. After hitting install the macro should be installed.