CPPToPigpio

CPPToPigpio is a library that maps the functions used with the mbed microcontroller to be used on with the GPIO ports of a Raspberry Pi.

Installation

To install the library:

  1. After installing pigpio, download this repo as a zip file and unzip to some directory. You can download the zip file here
  2. Make sure install.sh is executable by running $ sudo chmod +x install.sh
  3. Run $ sudo ./install.sh from that directory. Should see a message that install has finished successfully.
  4. Projects should be ready to compile now. Add in include <CPPToPigpio.h> in your main file, and ensure that Build->Set Build Commands->Build in Geany contains g++ -std=c++11 -Wall -Wextra -o ‘%e’ ‘%f’ -pthread -lpigpio -lrt -lCPPToPigpio

To enable full I2C functionality (being able to use both sets of I2C pins), do the following:

  1. In Preferences->Raspberry Pi Configuration->Interfaces tab, make sure I2C is enabled. By default, this only enables I2C1 (p2/p3).
  2. Reboot the pi (you can use $ sudo shutdown -r now)
  3. Edit /boot/.config.txt with super user
    1. Add dtparam=i2c_vc=on above the line matching dtparam=i2c=on
    2. Reboot
  4. In the terminal enter i2cdirect -y 0 and then i2cdirect -y 1 to scan the two I2C buses. You should get something similar to this image: /media/uploads/karvindass/screenshot_from_2017-12-08_08.46.53.png

Testing the code

On top of writing your own code to test the library, here is an mbed repo containing some test programs - including details for setting up the testing circuits - that can be used to ensure that the library is fully functional. It also contains an example I2C driver for the MPR121 Touch Keypad."

Classes

DigitalIn

The DigitalIn interface is used to read the value of a digital input pin.

Information

By default the mode of the pin will be PullNone

API

Import library

Public Member Functions

DigitalIn (PinName pin)
Create a DigitalIn connected to the specified pin.
DigitalIn (PinName pin, PinMode mode)
Create a DigitalIn connected to the specified pin.
DigitalIn (int pin)
Create a DigitalIn connected to the specified pin.
DigitalIn (int pin, PinMode mode)
Create a DigitalIn connected to the specified pin.
int read ()
Read the input, represented as 0 or 1 (int)
operator int ()
An operator shorthand for read()
void mode (PinMode mode)
Set the input pin mode.
bool operator== (int)
Check equality of pin to other DigitalIn pin.
bool operator== ( DigitalIn &)
Check equality of pin to other DigitalIn pin.
~DigitalIn ()
Deconstructor.

Code Example

This test takes in a DigitalIn signal and outputs a PWM signal that will control a servo. The servo should go to it's "maximum" position when the button is pressed and to its "minimum" position when it is released

DigitalInServo.cpp

#include <iostream>
#include <CPPToPigpio.h>

using namespace std;

CPPToPigpio::DigitalIn switchPin(p18);
CPPToPigpio::PwmOut servoPin(p20);

int main() {
    switchPin.mode(PullDown);
    
    // 20 Hz
    servoPin.period(0.05);
    
    servoPin = 0.03;
    
    while(1) {
        if(switchPin) {
            // servo on
            servoPin = 0.04;
            cout << "servo pin high" << endl;
        }
        else{
            servoPin = 0.02;
            cout << "servo pin low" << endl;
        }
        time_sleep(1);
    }
}

DigitalOut

The DigitalOut interface is used to configure and control a digital output pin.

API

Import library

Public Member Functions

DigitalOut (PinName pin)
Create a DigitalOut connected to the specified pin.
DigitalOut (PinName pin, int value)
Create a DigitalOut connected to the specified pin.
DigitalOut (int pin)
Create a DigitalOut connected to the specified pin.
DigitalOut (int pin, int value)
Create a DigitalOut connected to the specified pin.
void write (int value)
Set the output, specified as 0 or 1 (int)
int read ()
Return the output setting, represented as 0 or 1 (int)
operator int ()
A shorthand for read()
DigitalOut & operator= (int)
A shorthand for write()
DigitalOut & operator= ( DigitalOut &rhs)
A shorthand for write()
~DigitalOut ()
Deconstructor.

Code Example

Uses a DigitalIn signal to read a button press, and lights up an LED using DigitalOut if the button is pressed.

LED.cpp

#include <CPPToPigpio.h>

using namespace std;

int main() {
    
    CPPToPigpio::DigitalIn input(p18);
    
    CPPToPigpio::DigitalOut LED(p20); 
    input.mode(PullDown);
    
    time_sleep(1);
    
    while(1) {
        LED = input;
        
        
        time_sleep(0.1);
    }
}

BusIn/BusOut

BusIn and BusOut are used to read and write digital signals on specified pin. The MBED BusIn/BusOut has a maximum limit of 16 pins per bus, and this limit is replicated by CPPToPigpio despite it being rather artificial. In the future, the limit can easily be increased by adjusting the constructors to take more than 16 pins.

BusIn

The BusIn interface is used to create a number of DigitalIn pins that can be read as one value.

Information

By default the mode of the pin will be PullNone

Information

For a code example, look at the PwmOut section

API

Import library

Public Member Functions

BusIn (PinName p0, PinName p1=NC, PinName p2=NC, PinName p3=NC, PinName p4=NC, PinName p5=NC, PinName p6=NC, PinName p7=NC, PinName p8=NC, PinName p9=NC, PinName p10=NC, PinName p11=NC, PinName p12=NC, PinName p13=NC, PinName p14=NC, PinName p15=NC)
Creates a BusIn , connected to the specific pins.
BusIn (int p0, int p1=-1, int p2=-1, int p3=-1, int p4=-1, int p5=-1, int p6=-1, int p7=-1, int p8=-1, int p9=-1, int p10=-1, int p11=-1, int p12=-1, int p13=-1, int p14=-1, int p15=-1)
Creates a BusIn , connected to the specific pins.
BusIn (int busSize, PinName pinNames[16])
Creates a BusIn , connected to the specific pins.
BusIn (int busSize, int pinNames[16])
Creates a BusIn , connected to the specific pins.
int read ()
Read value of input pins.
void mode (PinMode pull)
Sets the input pin mode for all pins in the bus.
operator int ()
A shorthand for read()
DigitalIn & operator[] (int index)
Access to particular bit in random-iterator fashion.
long long mask ()
Binary mask of bus pins connected to actual pins (not NC pins) If bus pin is in NC state make corresponding bit will be cleared (set to 0), else bit will be set to 1.
~BusIn ()
Desconstructor for object.

BusOut

The BusOut interface is used to create a number of DigitalOut pins that can be written as one value.

Information

For a code example, look at the I2C section

API

Import library

Public Member Functions

BusOut (PinName p0, PinName p1=NC, PinName p2=NC, PinName p3=NC, PinName p4=NC, PinName p5=NC, PinName p6=NC, PinName p7=NC, PinName p8=NC, PinName p9=NC, PinName p10=NC, PinName p11=NC, PinName p12=NC, PinName p13=NC, PinName p14=NC, PinName p15=NC)
Create an BusOut , connected to the specified pins.
BusOut (int p0, int p1=-1, int p2=-1, int p3=-1, int p4=-1, int p5=-1, int p6=-1, int p7=-1, int p8=-1, int p9=-1, int p10=-1, int p11=-1, int p12=-1, int p13=-1, int p14=-1, int p15=-1)
Create an BusOut , connected to the specified pins.
BusOut (int busSize, PinName pinNames[16])
Creates a BusOut , connected to the specific pins.
BusOut (int busSize, int pinNames[16])
Creates a BusOut , connected to the specific pins.
void write (int value)
Write the value to the output bus.
int read ()
Read the value currently output on the bus.
operator int ()
A shorthand for read()
DigitalOut & operator[] (int index)
Access to particular bit in random-iterator fashion.
BusOut & operator= (int v)
A shorthand for write()
long long mask ()
Binary mask of bus pins connected to actual pins (not NC pins) If bus pin is in NC state make corresponding bit will be cleared (set to 0), else bit will be set to 1.
~BusOut ()
Desconstructor for object.

I2C

The I2C interface provides I2C Master functionality.

This interface can be used for communication with a I2C devices, such as serial memories, sensors and other modules or integrated circuits.

API

Import library

Public Member Functions

I2C (PinName sda, PinName scl)
Create an I2C Master interface, connected to the specified pins.
I2C (int sda, int scl)
Create an I2C Master interface, connected to the specified pins.
void frequency (int hz)
Set the frequency of the I2C clock frequency for bit banging.
int read (unsigned address, char *buf, int length, bool repeated)
Read from an I2C slave.
int read (int ack)
Read a single byte from the I2C bus.
int write (unsigned address, char *buf, int length, bool repeated)
Write to an I2C slave.
int write (int data)
Write single byte out on the I2C bus.
void start (void)
Creates a start condition on the I2C bus.
void stop (void)
Creates a stop condition on the I2C bus.
int bang (char *inBuf, unsigned inLen, char *outBuf, unsigned outLen, bool cont)
Allows construction of arbitrary I2C command.

Code Example

Receives a value from an accelerometer and "plots" it on a 4-bit BusOut (it just shifts the 16-bit received value by 12).

I2CToBusOut.cpp

#include <iostream>
#include <CPPToPigpio.h>

#define I2C_SDA p2
#define I2C_SCL p3

using namespace std;

int main() {
    
    CPPToPigpio::I2C acc(I2C_SDA, I2C_SCL);
    
    CPPToPigpio::BusOut LEDs(p21, p20, p16, p12);
    
    char initWrite[] = {0x20, char(0xC0)};
    char dataGet[] = {0x28};
    char dataFromStuff[2];
    
    acc.write(0x35 << (1), initWrite, 2, false);
    
    time_sleep(1);
    
    while(1) {
        acc.write(0x35 << (1), dataGet, 1, false);
        acc.read(0x35 << (1) | 1, dataFromStuff, 2, false);
        
        int superAcc = dataFromStuff[0] + (dataFromStuff[1] <<(8));
        
        cout << "acc gotten is " << superAcc << endl;
        
        int writeEm = superAcc >>(12);
        
        LEDs = writeEm;
        time_sleep(1);
    }
}

PwmOut

The PwmOut interface is used to control the control the period and duty-cycle of a GPIO pin on the Pi.

API

Import library

Public Member Functions

PwmOut (PinName pin)
Create a PwmOut connected to the specified pin.
PwmOut (PinName pin, float val)
Create a PwmOut connected to the specified pin.
PwmOut (int pin)
Create a PwmOut connected to the specified pin.
PwmOut (int pin, float val)
Create a PwmOut connected to the specified pin.
void write (float val)
Set the ouput duty-cycle, specified as a percentage (float)
float read ()
Return the current output duty-cycle setting, measured as a percentage (float)
float period (float seconds)
Set the PWM period, specified in seconds (float), keeping the duty cycle the same.
float period_ms (int ms)
Set the PWM period, specified in miliseconds (int), keeping the duty cycle the same.
float period_us (int us)
Set the PWM period, specified in microseconds (int), keeping the duty cycle the same.
PwmOut & operator= (float val)
A operator shorthand for write()
PwmOut & operator= ( PwmOut &)
A operator shorthand for write()
operator float ()
An operator shorthand for read()
~PwmOut ()
Deconstructor.

Code Example

This test gets a value from a BusIn and changes a PwmOut frequency based on the BusIn value. Outputs frequency to command line as well.

BusInToPwmOut.cp

#include <iostream>
#include <CPPToPigpio.h>

using namespace std;

float starting_f = 8000.0f;

CPPToPigpio::BusIn switchBus(p25, p24, p23, p18);

float divisions[] = {1.0f, 2.0f, 4.0f, 5.0f, 8.0f, 10.0f, 16.0f, 20.0f, 25.0f, 32.0f, 40.0f, 50.0f, 80.0f, 100.0f, 160.0f, 200.0f};

CPPToPigpio::PwmOut outThing(p21);

int main(){
    
    switchBus.mode(PullDown);
    outThing.period(1.0f/starting_f);
    outThing = 0.5f;
    
    while(1) {
        int total = switchBus;
        
        outThing.period(divisions[total]/starting_f);
        
        cout << "frequency should be " << starting_f/divisions[total];
        
       time_sleep(1);
    }
}

Setting A Custom Period

Returned float is not always the same as the float passed

When setting a custom period, the returned float (the actual period being set) may not always be the custom period you are trying to set to. It will be very close, but there is a potential for slight difference because there are a finite set of periods that can be used in Pigpio. As such, internally the value closest to the passed value is chosen as the target period. This applies to all 3 different period functions.

The following periods available to be set using the 3 period functions:

0.0002s, 0.0004s, 0.0008s, 0.001s, 0.0016s, 0.002s, 0.00319s, 0.004s, 0.005s, 0.0064s, 0.008s, 0.01s, 0.0159s, 0.02s, 0.0323s, 0.04s, 0.0769s, 0.167s

Setting Duty Cycle

In order to give the ability to set duty cycle through Pigpio, internally we are converting the duty cycle to a value on the scale of 0-5000 . For instance calling write(0.4) will actually write 2000 on a Pigpio range of 0-5000 - this is equivalent to a 40% duty cycle.

Acknowledgements

This code was developed by:


Please log in to post comments.