MicrocontrollerMicros

Yield Function, Printable Class and Mapping Arrays : Useful But Unknown Features from Arduino Core

Arduino core, the source code of Arduino API functions and classes, has three useful features that can be used effectively. As the Arduino core documentation doesn’t mention them (at least until the time of publishing this micro-blog), these features are not well-known for arduino developers. Let’s discuss each feature of them one by one.

Printable Class : How to Make Any Object Printable

First of all, print() and println() methods which are used in objects like Serial instance from HardwareSerial class are originally inherited from a class called Print (it not a direct inheritance but through Stream class which is inherited by HardwareSerial). To make these two methods capable of printing any object from a new class, the main Print class inherits another class called Printable which consists of only one public method called printTo(). This method is virtual, thus every new class inherits Printable, defined by developer, can have its own definition of the method. Review the inheritance and virtual function concepts from GeeksforGeeks tutorials.

On the other hand, the Print class has a special overloaded version for print() and println() methods which use the printTo() method.

To sum up, the following demo sketch makes the usage of printable clear.

Printable class Arduino example

It’s also useful to know that IPAddress.h uses the same trick to print the IP address in IPAddress.cpp.

Yield Function: A Function that Keeps Running While Arduino in a Delay

While browsing the Arduino core source files, I found a weird file called hooks.c with very few lines of code inside.

Reading the comments included in the file, it seems that the weak function yield() is intended to be used for scheduling aspects. By weak we mean that it can be redefined in another source file. However,for AVR based Arduino board this function is not used except for one place; inside the delay() function in wiring.c source file .

A useful usage of yield() is to be redefined in the main application code with some sensitive operation while the Arduino makes nothing except for waiting the delay to end, or even implementing some simple scheduler.

Mapping Arrays: Arrays that Map The Arduino Pins to According Physical Ones

Arduino team simplifys the way to access the MCU ports/GPIO by defining arrays in the flash memory (PROGMEM) to store permanently a map between the arduino pin number and the according physical pin number in the port register and the according port address. These arrays can be found in the  pins_arduino.h header file.

Arduino.h file defines some macros to access these arrays stored in flash via  pgm_read_word.

Now, each function needs to access these registers can simply call the mapping macros. I.e. pinMode, digitalWrite, (name yours), ..etc.

Yahya Tawil

Embedded Hardware Engineer interested in open hardware and was born in the same year as Linux. Yahya is the editor-in-chief of Atadiat and believes in the importance of sharing free, practical, spam-free and high quality written content with others. His experience with Embedded Systems includes developing firmware with bare-metal C and Arduino, designing PCB&schematic and content creation.

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.