External Libraries

Basic Operation

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:

"IncludeFiles"files to include
"Definitions"definition of macros
"Declarations"function declarations
"Utilites"utility functions and variables
"Initializations"program initialization
"Loop"execute at each sampling instant
"PreLoop"execute before "Loop"
"PostLoop"execute after "Loop"
"UntimedLoop"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.
In[1]:=
Click for copyable input
The code in the includes and utilities segments.
In[2]:=
Click for copyable input
In[3]:=
Click for copyable input

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.
In[4]:=
Click for copyable input

The systems simply passes the input through. Thus the output of sys is also or

The system.
In[6]:=
Click for copyable input

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.
In[7]:=
Click for copyable input

After the code is embedded the buzzer will sound at each button press.

Load the package.
In[8]:=
Click for copyable input
Embed the code.
In[9]:=
Click for copyable input
Out[9]=

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 .
In[10]:=
Click for copyable input
In[11]:=
Click for copyable input
Embed code that returns the values over a serial port.
In[12]:=
Click for copyable input
Out[12]=

We can now use the device framework to read in the values.

A function to parse the incoming data.
In[13]:=
Click for copyable input
Open a serial connection and set up a scheduled task to read the data.
In[14]:=
Click for copyable input
Out[14]=
In[15]:=
Click for copyable input
Remove the task and close the connection after some time.
In[17]:=
Click for copyable input

We can do some post analysis of the data that was collected.

Generate a histogram.
In[19]:=
Click for copyable input
Out[19]=
Estimate the parameters of the distribution.
In[20]:=
Click for copyable input
Out[20]=