There are now a number of ways of controlling the Raspberry Pi general purpose I/O.
Libraries such as wiringPi and pigpio are written in the C programming language, but can also be used with other languages including Gambas.
I have covered wiringPi previously, and this short post is just a basic introduction to using the pigpio library with Gambas.
The pigpio library has dozens of functions enabling the Gambas programmer to interface and control simple serial comms, I2C, SPI and PWM, in addition to the basic digital inputs and outputs.
But before we can use this functionality, we need to download the source files and build the library.
Open a terminal on your Pi and type:-
Locate the pigpio.zip file in the file manager (pcmanfm) then right-click and Extract.
Open the extracted folder in file manager, then hit F4 to open a terminal (within this folder) and type:-
Once completed, type:-
sudo make install
If there are no errors, this completes the installation.
Using the library in Gambas
Open Gambas and create a new project.
Add a button and two labels to the form.
In the Form class, declare the pigpio library:-
Declare the initialise and terminate functions:-
Public Extern gpioInitialise() As Integer 'call at start of program to initialise
Public Extern gpioTerminate() 'call at end of program to release resources and terminate threads
Add functions to return library version and hardware revision details:-
Public Extern gpioVersion() As Integer
Public Extern gpioHardwareRevision() As Integer
Add digital I/O set mode, read and write functions:-
Public Extern gpioSetMode(iPin As Integer, iMode As Integer) As Integer
Public Extern gpioRead(iPin As Integer) As Integer
Public Extern gpioWrite(iPin As Integer, iLevel As Integer) As Integer
Declare mode constants:-
Const PI_INPUT As Integer = 0
Const PI_OUTPUT As Integer = 1
We need to initialise pigpio when our program starts...
Public Sub Form_Open()
Dim intReply As Integer
intReply = gpioInitialise()
If intReply < 0 Then
Label1.Text = "Failed: must be run as root"
Button1.Enabled = False
Label1.Text = "Initialised: pigpio version no: " & intReply
Me.Text = "Pi hardware rev: " & Hex$(gpioHardwareRevision())
Me.Text &= ", pigpio vers: " & gpioVersion()
Me.Tag = 0
...and terminate properly when the program stops...
Public Sub Form_Close()
Now we can use the button click event to write values to the output pins and read them back:-
Public Sub Button1_Click()
'write then read digital IO
Dim intPin As Integer
Dim intMode As Integer
Dim intLevel As Integer
Dim intOut As Integer
Label2.Text = ""
If Me.tag = 0 Then
Me.Tag = 1
Me.Tag = 0
intOut = Me.Tag 'alter 1's & 0's sequence each time button pressed
For intPin = 0 To 31
intMode = gpioSetMode(intPin, PI_OUTPUT)
If intOut = 0 Then 'alternate 1's and 0's outputs
intOut = 1
intOut = 0
intLevel = gpioRead(intPin)
Label2.Text &= intLevel & ", "
Now try to run the project in the Gambas IDE. It will probably report:-
"Failed: must be run as root"
...because you are not running as root. So close Gambas and restart by opening a terminal and typing:-
Hopefully, the program will now run properly.
In this basic example I've illustrated how to reference the pigpio library, and declare & implement a few simple library functions. Please refer to the pigpio website for a full list of functions.
At first glance, the pigpio library has more functionality and is easier to use than wiringPi.