Sometimes, you may wish to use an existing library for the devices that are connected to the microcontroller or for the microcontroller itself.
Such libraries can be included by specifying their paths in the "Libraries" property as MicrocontrollerEmbedCode[…,<|…,"Libraries"->paths,…|>].
The code from the external library for a device can be injected alongside the generated code by specifying the input or output channel to be of type "ExternalLibrary" and adding additional channel specifications as suboptions. Thus an input or output channel that uses functionality from an external library is specified as "ExternalLibrary"-><|libkey1->val1,…|>.
If the library is being used by the microcontroller itself the specifications for the code from the external library must be specified in the the second microcontroller argument as MicrocontrollerEmbedCode[sys,<|…,libkeyi->valuei,…|>,…].
The keys libkeyi for external libraries specify at which segments of the program the external library code needs to be injected are the following:
files to include
definition of macros
utility functions and variables
execute at each sampling instant
execute before "Loop"
execute after "Loop"
execute repeatedly whenever the processor is free
The possible suboptions for an external library.
The location of segments can be seen from the general layout of the program.
Pololu Library Example
The Zumo robot from Pololu has a pushbutton and a buzzer. They also provide libraries to interact with them. Follow the standard procedure for installing the libraries for Arduino. This will allow you to run the Arduino examples that are provided and verify that it is working correctly.
For this example we only need the contents of the folders PushButton and ZumoBuzzer. For our purposes these folders can in any location in the file system.
The library locations.
The code in the includes and utilities segments.
The button library has a isPressed function that can be used to check if the button it pressed or not. That can be used to get a or reading using the library depending on the button state.
The input specification.
The systems simply passes the input through. Thus the output of sys is also or
The buzzer library has a playNote function that can be used to play a specified note. The note is defined in the library's header file. The output is set to the variable playQ, and the note is played only if the output of sys is .
The output specification.
After the code is embedded the buzzer will sound at each button press.
Load the package.
Embed the code.
Arduino Library Example
The Arduino language has a function called random to generate psuedo-random numbers. Just as in the previous example, we have to specify the library location and other segments of the code.
Use the Arduino function random to generate random numbers between and .
Embed code that returns the values over a serial port.
We can now use the device framework to read in the values.
A function to parse the incoming data.
Open a serial connection and set up a scheduled task to read the data.
Remove the task and close the connection after some time.
We can do some post analysis of the data that was collected.