Inicio » OSES: Programming session

OSES: Programming session

Rationale

Of course, you need to develop software on top of the elected OS for your new shiny embedded system.

There are thousands of programming languages, but embedded systems have some special characteristics that, at first, limit the election.

This session if focused to give you some highlights about this idea. And play with C language.

Departing point: you have some skills about imperative programming.

Programming languages for embedded

It is simple, if you need a microcontroller, then try to use C/C++. If your processor is more powerful, then you can elect other thing like python, java, etc. or mix it.

Well, before playing, let’s make space in the RPI’s SD card.

Check space,

$ df

Eliminate and «purgue» packages,

$ sudo apt-get purge libreoffice*
$ sudo apt-get purge wolfram-engine
$ sudo apt-get clean
$ sudo apt-get autoremove

And check again.

Connect something to you RPi

Well, I need something interesting for this session, lets know more about IO interfaces

C/C++ and the Raspberry Pi

 

Thanks to the GNU gcc compiler collection , you are plenty of options for embedded development. From small microcontrollers until the sky … for free and open.

Now, keep in mind that there are toooonnnns of interesting open source projects that provide high-quality professional-grade libraries and tools for your own projects. Your Android or iOS smartphone is plenty of these projects. Also, commercial projects are very interesting here. Don’t discover the wheel (<- buscar expresio anglesa), please.

Well, apply now the command ldd on the executable obtained in this session.

 $ ldd ./blink

Get help using,

 $ man ldd

Activity

Develop a modular application in C for handling a Laser device.

Follow these steps:

Create a directory and move to it:

$ md laser
$ cd laser

 

Create a laser.h header file using the following listing:

/**
    @file   laser.h
    @brief  function prototypes for handling the laser
    @author Angel Perles
    @date   2016/10/26

    License ...
**/

#ifndef LASER_H
#define LASER_H

int  laser_Init(void);
void laser_On(void);
void laser_Off(void);

#endif // LASER_H

 

Implement the functionality creating the file laser_wiringpi.c , adding the following template and fullfilling the necessary code:

/**
  @file laser_wiringpi.c
  @brief Implementation of the laser control using wiringPi libraries

  Laser connected to pin GPIO0. 1=On, 0=Off
  REMEMBER TO EXPORT THE PIN
    $ gpio export 17 out

  @author Angel Perles
  @date 2016/11/1

  License ...
**/

#include <wiringPi.h>
#include "laser.h"

/*****************************************************************************/
/**
  @brief prepares the underlaying system for using the laser
  @note This function must be called before any other laser functionis called after cds_fifoPrePush
  @returns true if initializaion is OK

  Example:
  @verbatim
      if (laser_Init() != 0) {
        printf("Upps!! Something wrong initilitasing laser()\n");
        exit(1);
      }
  @endverbatim

**/
int laser_Init(void)
{    
    // FULLFILL
}

/*****************************************************************************/
/**
  @brief turns on the laser

  Example:
  @verbatim
      laser_On();
  @endverbatim
**/
void laser_On(void)
{
    // FULLFILL
}

/*****************************************************************************/
/**
  @brief turns on the laser

  Example:
  @verbatim
      laser_Off();
  @endverbatim
**/
void laser_Off(void)
{
    // FULLFILL
}

/* End of file ***************************************************************/

 

Compile this module

$ gcc -c laser_wiringpi.c

 

List directory contents

$ ls -l

 

Create a test application with name test_laser.c and copy the following code:

// File: test_laser.h
// Testing the laser module
// Remember to export the associated pin!!!

#include <stdio.h>
#include <wiringPi.h> // for delay() function

#include "laser.h"

int main(void)
{

    laser_Init();

    while(1)
    {
        printf("Laser ON\n");
        laser_On();
        delay(500);

        printf("Laser OFF\n");
        laser_Off();
        delay(500);
    }
}

 

Build the project:

$ gcc -o test_laser test_laser.c laser_wiringpi.o -lwiringPi

 

And, test it!

$ ./test_laser

 

Well, let me show you the final design: it includes a Piface Digital 2 board for controlling the laser and a digital microscope. The application has been developed in C/C++ and includes externals libraries such as WiringPi and OpenCV.

rpi_laser_microscope

 

Go multiplatform!

I think that learning a new paradigm/SDK/etc. every time a new platform appears is not a good deal for your productivity. There are approaches that tries to be «multiplatform» in order to  minimize this effect. Of course, this has some disadvantages, but this not the question now.

Some options are based on native code generation departing of the use of C/C++ compiler. For example, wxWidgets, GTK and Qt. Others options are based on virtual machines such as Java and .net/C# (not recommended). And others are interpreted languages such as Python.

I like Qt, so …

Activity

And now, your turn … with guidance. Wow! I love Qt!

qt5-laser

 

Your preferred language is …

May be you have a preferred language. If it is Java or Python, it is installed by default in the Raspbian Linux distribution. If not installed (octave,Ada, …) , you could find the instructions for installing it.

Activity

Find the appropriate editor and start playing. Show me something.

 

Task

This will be part of your marks.

Find the procedure to install the «MySQL» database manager in your RPi or in your Linux machine (real or virtual).

Create a database with a simple table with the following fields:

 

  • timestamp (time)
  • published (bool)
  • node_id (integer)
  • temperature (real)
  • humidity (real)
  • luminosity (real)

 

These fields represent data collected from sensor nodes. The «published» field indicates if the data has been published in the cloud.

Develop a C/C++ or node.js or Python application that creates fields and write them to the database.

SUBMITT YOUR WORK USING POLIFORMAT TASK ADDING AN SMALL REPORT (max. 2 pages) WITH THE STEPS