How does Pinguino work

From Pinguino
Jump to: navigation, search

The Pinguino IDE was created to use the Arduino instructions with Microchip PIC microcontrollers. As it was also intended to use only Open Source tools, the creators decided to use the only Open Source Compiler available for PICs - the Small Device C Compiler (SDCC). Sadly SDCC is a C compiler and C compilers cannot understand the Arduino C++ instructions. Consequently, it was necessary to create an interpreter that transforms C++ instructions into C instructions which SDCC understands.

When the Pinguino IDE starts, it first reads a database with all .pdl files in p8/pdl or p32/pdl depending on what architecture the user chose. A .pdl (or .pdl32) file (aka keywords file) is made up of lines with the following format :

[function name used in Pinguino IDE][SPACE][function name used in C library]#include[SPACE]<[name of C library]>[#define __SOMEUPPERCASEWORD__]'

Note :

  • #define and #include are optional.
  • every C++ keyword defined in a .pdl (or .pdl32) file will be highlighted in the Pinguino IDE Editor.

At the compilation/link stage, the Pinguino IDE replaces all C++ instructions from the user program (.pde extension) with their C equivalents. The whole translated program is then stored in sources/user.c.

The IDE automatically loads (#include) the concerned libraries. That's why it isn't necessary to call libraries from Pinguino's programs (unlike Arduino, but it is still possible with a library that wouldn't have .pdl files).

Optional #define(s) are also passed to the compiler.

Every #define and #include from the .pdl (or ,pdl32) files necessary for your program are added in the sources/define.h file.

For example, let's take the C++ "Serial.printf()" instruction:

In p8/pdl/serial.pdl, you can find:

Serial.printf serial_printf#include <serial.c>#define __SERIAL_PRINTF__

which means that the "Serial.printf" C equivalent is "serial_printf" which is in "serial.c". You can also see that __SERIAL_PRINTF__ is declared. It tells the compiler to include the code only if you use "Serial.printf()". Thereby, if you only use "Serial.begin()", the code for "Serial.printf() will not be included and memory space will be saved (see serial.c for more details).

  • sources/main.c (or main32.c for 32 bit) automatically includes some files such as define.h, pin.h, const.h and user.c
  • const.h contains some constants such as INPUT, OUTPUT, HIGH, LOW, etc.
  • pin.h contains some pin definition so you can use, for instance, RB0 or USERLED instead of pin numbers.