Crea sito

5.1 add a custom function to AVIL in 5 steps



You can add your custom function in the source code of AVIL an call it from an AVIL program.
In a custom function you can use the system's interface to print stuff or to get input from user.
This tutorial is for add a function on Arduino, if you want to add a function for the PC version (or other) you just have to do the same things on files "pcSys.h" and "pcSys.cpp".

Step 1:


add the function's prototype in "arduinoSys.h" as boolean function.
A custom function must always return a boolean value, true if everything it's ok, false to notify an execution's error to the interpreter.


Step 2:


add (in "arduinoSys.cpp") the function's reference in the "systemPrograms" array in the form of:

{"functionAlias", &functionName}


Step 3:


add the function's implementation in "arduinoSys.cpp"

You can now call your function from an AVIL program by its alias:

Step 4:


Add input arguments:

If your function expects an input argument (or more) you can get it in it's implementation using the overloaded IOdata class method "getArg"

where "index" is the argument's position in the AVIL's call and "value" is the container passed by reference (you can pass as argument a bool, an int or a string).
So if you call your function like this:

You can get the arguments 5 and "hello" in the function's implementation like this:

As you can see you can use the system's interface to print stuff.
A custom function must always return a boolean value, true if everything it's ok, false to notify an execution's error to the interpreter.

Step 5:


Write to output buffer:

If you want your function produce a result you can use in an AVIL program after a call's execution you have to write it in the output buffer using the overloaded IOdata class method "setOutVal":

Where index is the output buffer index (you have max MAX_OUT_VALS_NUM output buffers as defined here)
You can write in the output buffer a string, an integer or a boolean.

You can then get the value written in the output buffer ("ciao!" in this case) from an AVIL program like this:

the line of code number 10 will print as expected "ciao!".