Functions and Libraries
Functions are blocks of code that focus on specific tasks. There are two main types of functions: those packaged in a library and those you define in your code. Programming languages such as C++, Python*, Java*, and Node.js* provide built-in functions that you can call at any time. Functions go by different names in different programming languages, such as method, subroutine, and procedure.
Libraries are collections of functions. For example, sqrt(x) is a mathematical C++ built-in function within the math.h C++ library; it facilitates the computation of the square root of a given number. Such a library includes multiple functions, such as logarithm (log10) and exponentials (exp). For Internet of Things (IoT) related projects, two main libraries are particularly useful:
- Libmraa* (MRAA) is an I/O library
- Useful Packages & Modules (UPM) is a sensor library
When working with hardware, you need tools to communicate with the various parts of your board and its connected sensors. These libraries come with definitions for the various sensors and are cross compatible with multiple boards.
To use Libmraa* (I/O library) you can install it on an UP Squared* board. Libmraa enables you to interact with the board through the functions the library provides and it has defined configurations for each board and its components. It provides an application programming interface (API) for interfacing with low-level peripherals, such as general-purpose input/output (GPIO), pulse width modulation (PWM), Bluetooth® Low Energy (Bluetooth ® LE), inter-integrated circuit (I2C) protocol, serial peripheral interface (SPI), and Universal Asynchronous Receiver/Transmitter (UART). The physical pins of chips and sensors map to Libmraa. You don’t need to know the details of how communication between the various components happens: The library takes care of which board and breakout pins are connected. Check out the pin mapping for the UP Squared on GitHub*.
The multiple API classes within Libmraa each contain numerous functions for use in IoT projects. Here is a small list of those functions:
GPIO interface to Libmraa; functions include:
- mraa_gpio_init_raw function
I2C to Libmraa; functions include:
Analog input/output (AIO) interface to Libmraa; functions include:
PWM interface to Libmraa; functions include:
SPI to Libmraa; functions include:
UART to Libmraa; functions include:
Defines the basic shared values for Libmraa; functions include:
For a full list and description of functions that each class supports, see mraa documentation.
To use all the functionality that MRAA provides, you must include mraa.h in your code. Figure 2 shows a C example for connecting an LED to D5 on your board and using GPIO functions. The outcome is a blinking LED; the sleep function controls how often the LED will be in the On and Off states.
Figure 2. C example using MRAA and general-purpose input/output
Useful Packages & Modules (UPM)
UPM provides software drivers for a variety of commonly used sensors and actuator. Drivers enable the hardware to communicate with the operating system. UPM is a level above MRAA and assists with object control of elements such as RGB LCDs and temperature sensors. The list of supported sensors is vast and includes accelerometers, buttons, color sensors, gas sensor, global positioning system (GPS), radio-frequency identification, and servos. See the full list.
Figure 3 illustrates how you can use the UPM library to read the input of a button. First, you need to include the library grove.hpp, which allows you to use the upm::GroveButton function to create an object for the button and later use the name () and value () functions to obtain the desired values from the sensors. See additional UPM examples.
Figure 3. Useful Packages & Modules example, with definitions for functions and libraries
Integrated development environments (IDEs) such as Eclipse* and Intel® System Studio come with MRAA and UPM as integrated libraries. The supported IDEs are available for download from Intel® IoT Developer Kit Integrated Development Environments. Here's how to create a new project in Intel® System Studio.
When using Eclipse*, you can choose to create a new IoT project that will include a couple of tools not available in other types of projects (Figure 4).
Figure 4. Creating a new Internet of Things project in Eclipse*
The IoT Sensor Support tool, shown in Figure 5, contains the list of sensors and actuators, with a description and picture of the hardware. Simply select all the hardware you will include in your project, and the tool automatically adds the related libraries to your code. For example, if you will be using the MMA7455 accelerometer, the tool will append to your current .ccp code with #include <mma7455.h>.
Figure 5. Internet of Things sensors and libraries included in Eclipse*
Libmraa and UPM work hand-in-hand to provide the tools you need to interact easily with the elements in your project. They remove the hassle of knowing the pins and requirements of each piece of hardware, packaging all the specifics into friendly, easy-to-use libraries. From prototyping an automatic pet feeder to creating a wearable that uses GPS, an accelerometer, and an RGB LCD to track your movements, you can develop your projects with the help of MRAA and UPM.
Both libraries are open source projects, with dedicated GitHub* repositories (see the upm and mraa repositories). Developers are welcome to assist with enhancing and writing new APIs and functions, and reviewing documentation. Contributors are required to follow a coding and documentation style. Thanks to the open source community’s efforts, new functions and libraries are added frequently for various coding languages.
Libraries and their functions help with cleaner, more efficient code that you can reuse. For built-in functions, you don’t need to worry about how the computation is performed: As long as you implement the function with the required input, it will generate the desired output. For your IoT projects, Libmraa and UPM are essential libraries.
For More Information
Product and Performance Information
Performance varies by use, configuration and other factors. Learn more at www.Intel.com/PerformanceIndex.