SimonSlowfood
Posts: 3
Joined: Tue May 14, 2013 8:58 am

C++ API for I2C, SPI, GPIO, etc.

Tue May 14, 2013 12:48 pm

Hi,

we developed a C++ API to easily interact with typical embedded Linux interfaces. In the first place we wrote the code for our Open Source embedded Linux board called Gnublin. But it turned out, that the API also works on the Raspberry Pi.
Check out our git repository:
https://github.com/embeddedprojects/gnublin-api
We encourage you to test it on your RPi and give us some feedback!

For detailed information check out our documentation

Here is some example code, to give you a brief look, how the API works:
Simple gpio access:

Code: Select all

#define BOARD RASPBERRY_PI

#include "gnublin.h"
 
int main()
{
   gnublin_gpio gpio;
 
   gpio.pinMode(3,OUTPUT);
 
   while(1){
     gpio.digitalWrite(3,HIGH);
     sleep(2);
     gpio.digitalWrite(3,LOW);
     sleep(2);
   }
}

Simple access to the i2c bus

Code: Select all

#define BOARD RASPBERRY_PI
 
#include "gnublin.h"
 
 
int main()
{
   gnublin_i2c i2c;
 
   i2c.setAddress(0x42); // set the address of the slave you want to read/write
 
   char buffer[8];
   char RxBuf[8];
 
   buffer[0]=0x22;
 
   i2c.send(buffer,5);
   i2c.send(0x12, buffer, 2);   //send 2 bytes from buffer to register-address 0x12
 
   i2c.receive(RxBuf, 3);       // read 3 bytes and store them in RxBuf
   i2c.receive(0x23, RxBuf, 3);  // read from  tegister-address 0x23 3 bytes and store them in RxBuf
 
}

micclfr
Posts: 1
Joined: Wed Jul 31, 2013 4:27 pm

Re: C++ API for I2C, SPI, GPIO, etc.

Wed Jul 31, 2013 4:36 pm

Hi,

very good an complete API.
I wanted to use in Under Mono.
Quite easy to do, using SWIG 2.0 : sudo apt-get install swig2.0, which builds a native wrapper around the C+code and exposes Csharp classes.

An example :

Code: Select all

        static void Main(string[] args)
        {
            gnublin_module_relay relay = new gnublin_module_relay();
            relay.setAddress(0x20);
            gnublin_gpio gpio = new gnublin_gpio();

            SWIGTYPE_p_std__string output = libgnublin_wrap.numberToString(libgnublin_wrap.OUT);

            gpio.pinMode(3, output);

            while (true)
            {
                for (int i = 1; i <= 8; i++)
                {
                    relay.switchPin(i, 1);
                    Thread.Sleep(100);
                }
                for (int i = 1; i <= 8; i++)
                {
                    relay.switchPin(i, 0);
                    Thread.Sleep(100);
                }


            }
you just get to use that somewhat ugly construction (could be hidden !) :
SWIGTYPE_p_std__string output = libgnublin_wrap.numberToString(libgnublin_wrap.OUT);

works well then.
Great to connect a Pi to Homeseer for example ( you can even write a plugin for HS 3 quite easily)

Return to “C/C++”