Imran's personal blog

August 7, 2013

Making BeagleBone Black more like Arduino

Filed under: Uncategorized — ipeerbhai @ 4:55 am

Hi all,

I just spent some time writing my “hello world” code for BBB.  I really like arduino’s simple library for pin access, and find the file concept for BBB a bit too convoluted.  I wrote a little C++ wrapper to create something like Arduino’s pinmode/DigitalRead/DigitalWrite methods.

Here’s the code:

// GPIO.cpp is a wrapper for BeagleBone to make BeagleBone GPIO somewhat like Arduino.
// There are some huge assumptions I make in this concept.
// 1. The user knows how to calculate pins from the funky beaglebone Muxing scheme.
// 2. The user knows some things about arduino…

// License:
// You may use this file in your project in any way you like for non-commercial use. I request that you attribute me, Imran Peerbhai,
// in your application’s credits.
// Commercial use:
// this file may be used commercially without monetary payment in exchange for the following considerations:
// A. My name must be on the application in some method visible to the end user.
// B. You must provide me a copy of your application’s source code.
// C. You must provide me with contact information to license any of your source code.

// Warrantee:
// This code may not work in all cases. Use at your own risk.

#include <stdio.h>
#include <unistd.h>
//——————————————————————————————————————————
//——————————————————————————————————————————
// Class definition.
//——————————————————————————————————————————
//——————————————————————————————————————————

class WrappedGPIO
{
//——————————————————————————————————————————
//——————————————————————————————————————————
public:
int exportHelper( int , const char * );
//——————————————————————————————————————————
// Function:
// Asks the kernel to give me this pin in user mode.
// Parameters:
// int nPin — the pin number, in GPIO terms, to export.
// Returns:
// 0 — S_OK
// <0 — S_ERROR
// >0 — S_WARNING
int exportPin( int );
//——————————————————————————————————————————
// Parameters:
// int nPin — the pin number, in GPIO terms, to export.
// Returns:
// 0 — S_OK
// <0 — S_ERROR
// >0 — S_WARNING
int unexport( int ); // will release the pin back to the kernel.
//——————————————————————————————————————————
// Parameters:
// int nPin — the pin number, in GPIO terms, to export.
// int nDirection. 0 — output, 1 — input.
// Returns:
// 0 — S_OK
// <0 — S_ERROR
// >0 — S_WARNING
int pinMode( int, int); // Will set either a read or write direction.
//——————————————————————————————————————————
// Parameters:
// int nPin — the pin number, in GPIO terms, to export.
// int nValue — the value, either 0 or 1.
// Returns:
// 0 — S_OK
// <0 — S_ERROR
// >0 — S_WARNING

int digitalWrite( int, int ); // Will write “high” or low to a pin that’s been exported.
//——————————————————————————————————————————
// Parameters:
// int nPin — the pin number, in GPIO terms, to export.
// Returns:
// 0 — LOW
// <0 — S_ERROR
// 1 — HIGH
int digitalRead( int ); // Will read the digital value from a pin that’s been exported.
}; // end class

//——————————————————————————————————————————
//——————————————————————————————————————————
// Code area
//——————————————————————————————————————————
//——————————————————————————————————————————

//——————————————————————————————————————————
int WrappedGPIO::exportHelper( int nPin, const char *pExportFileName )
{
FILE *pExportFile = NULL; // Will use this pointer to get the handle from FOPEN for the hard-coded file path.
const char *pFileName = “/sys/class/gpio/export”; // Per BBB reference.

// Open the file for write, and write the pin number to it.
pExportFile = fopen( pExportFileName, “w” );
if ( NULL == pExportFile )
{
// Uh -oh, we couldn’t open the file for write.
printf(“Could not access export subsystem for pin %d.”, nPin );
return( -1 );
}
fprintf( pExportFile, “%d”, nPin); // write the GPIO pin number to the export file so the kernel can export it.
fclose( pExportFile ); // It’s done!
return(0); // all good.
}

//——————————————————————————————————————————
int WrappedGPIO::exportPin( int nPin )
{
FILE *pExportFile = NULL; // Will use this pointer to get the handle from FOPEN for the hard-coded file path.
const char *pExportFileName = “/sys/class/gpio/export”; // Per BBB reference.
return( exportHelper( nPin, pExportFileName) );
}

//——————————————————————————————————————————
int WrappedGPIO::unexport( int nPin )
{
FILE *pExportFile = NULL; // Will use this pointer to get the handle from FOPEN for the hard-coded file path.
const char *pExportFileName = “/sys/class/gpio/unexport”; // Per BBB reference.
return( exportHelper( nPin, pExportFileName) );
}

//——————————————————————————————————————————
int WrappedGPIO::pinMode( int nPin, int nPinMode )
{
FILE *pDirectionFile = NULL; // The direction file for the pin.
char strGPIODirFileName[50]; // A buffer to generate the “gpio” direction absolute file name.
sprintf( strGPIODirFileName, “/sys/class/gpio/gpio%d/direction”, nPin ); // write out the concatenated file name.

pDirectionFile = fopen( strGPIODirFileName, “w” ); // Open the direction file for writing.
if ( NULL == pDirectionFile )
{
// Uh-oh, can’t write the file…
printf( “Could not open %s for write”, strGPIODirFileName);
return( -1 );
}
// check to see the direction and write out the correct value
if ( nPinMode == 0 )
{
// We’re in “output” mode.
fprintf( pDirectionFile, “out” );
}
else
{
// We’re in the “in” mode.
fprintf( pDirectionFile, “in” );
}
fclose( pDirectionFile );
return ( 0 );

}
//——————————————————————————————————————————
int WrappedGPIO::digitalWrite( int nPin, int nValue )
{
FILE *pValueFile = NULL; // The direction file for the pin.
char strGPIOValueFileName[50]; // A buffer to generate the “gpio” direction absolute file name.
sprintf( strGPIOValueFileName, “/sys/class/gpio/gpio%d/value”, nPin );

// Open the file and write the digital value.
pValueFile = fopen( strGPIOValueFileName, “w” ); // open the value file for write
if ( NULL == pValueFile )
{
// Uh-oh, couldn’t open the file to write.
printf( “could not open %s for write.”, strGPIOValueFileName);
return( -1 );
}
fprintf( pValueFile, “%d”, nValue);
fclose( pValueFile );
return ( 0 );
}
//——————————————————————————————————————————
int WrappedGPIO::digitalRead( int nPin )
{
FILE *pValueFile = NULL; // The direction file for the pin.
int nReadValue = -1; // will hold the read value.
char strGPIOValueFileName[50]; // A buffer to generate the “gpio” direction absolute file name.
sprintf( strGPIOValueFileName, “/sys/class/gpio/gpio%d/value”, nPin );

// Open the file and write the digital value.
pValueFile = fopen( strGPIOValueFileName, “r” ); // open the value file for write
if ( NULL == pValueFile )
{
// Uh-oh, couldn’t open the file to write.
printf( “could not open %s for write.”, strGPIOValueFileName);
return( -1 );
}
nReadValue = fgetc( pValueFile );
fclose( pValueFile );
return ( nReadValue );
}
//——————————————————————————————————————————
int main()
{
// This is a test now…
printf( “Entering loop \n”);
WrappedGPIO myGPIO;
myGPIO.exportPin( 44 );
myGPIO.pinMode( 44, 0 );

while( 1 )
{
printf( “Writing 1 to pin 44 \n”);
myGPIO.digitalWrite( 44, 1 );
usleep(1000000);
printf( “Writing 0 to pin 44 \n”);
myGPIO.digitalWrite( 44, 0);
usleep(1000000);
}
return(0);
}

 

About these ads

1 Comment »

  1. […] GPIO.cpp is a wrapper for BeagleBone to make BeagleBone GPIO somewhat like Arduino. // There are some huge assumptions I make in this concept. // 1. The user knows how to calculate pins from the funky beaglebone …  […]

    Pingback by Making BeagleBone Black more like Arduino | Ras... — August 8, 2013 @ 4:01 am | Reply


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

The Rubric Theme. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: