Reputation: 29
I'm trying to make a program for beaglebone to let me control the gpio pins. I tried to use sprintf() but doesn't accept input as I know.
I have to re-write couple files in the beaglebone like
gpio export active the pin gpio gpio$pin/direction pin mode in/out gpio gpio$pin/value pin value 1/0
guys..!! just need a idea how to accomplish my goal.
I'm newbie in c++. any information or comment I'll appreciated thks guys for your time.
Upvotes: 2
Views: 17421
Reputation: 9
An enhanced version uses Cpp and is available in chapter 6 of Derek Molloy's book. Read from page 214:
~/derek/chp06/GPIO$ cat simple.cpp
/* A Simple GPIO application
* Written by Derek Molloy for the book "Exploring BeagleBone: Tools and
* Techniques for Building with Embedded Linux" by John Wiley & Sons, 2018
* ISBN 9781119533160. Please see the file README.md in the repository root
* directory for copyright and GNU GPLv3 license information. */
#include<iostream>
#include<unistd.h> //for usleep
#include"GPIO.h"
using namespace exploringBB;
using namespace std;
int main(){
GPIO outGPIO(44), inGPIO(45);
// Basic Output - Flash the LED 10 times, once per second
outGPIO.setDirection(OUTPUT);
//for (int i=0; i<10; i++)
while(1){
outGPIO.setValue(HIGH);
usleep(1000); //micro-second sleep 0.5 seconds
outGPIO.setValue(LOW);
usleep(1000);
}
// Basic Input example
inGPIO.setDirection(INPUT);
cout << "The value of the input is: "<< inGPIO.getValue() << endl;
// Fast write to GPIO 1 million times
outGPIO.streamOpen();
for (int i=0; i<1000000; i++){
outGPIO.streamWrite(HIGH);
outGPIO.streamWrite(LOW);
}
outGPIO.streamClose();
return 0;
}
Compile and run as
∼/exploringbb/chp06/GPIO$ sudo ./simple
Upvotes: 0
Reputation: 3568
Here is a tutorial on using c++ to control the LEDs: http://derekmolloy.ie/beaglebone-controlling-the-on-board-leds-using-c/
Halfway down the page is the C++ code. Take this implementation, but instead of writing to the LED device files, write the appropriate information to the GPIO device files, like in this manual:
http://elinux.org/images/3/33/GPIO_Programming_on_the_Beaglebone.pdf
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main(){
std::fstream fs;
fs.open("/sys/kernel/debug/omap_mux/gpmc_ad4");
fs << "7";
fs.close();
fs.open("/sys/class/gpio/export");
fs << "32";
fs.close();
fs.open("/sys/class/gpio/gpio32/direction");
fs << "out";
fs.close();
fs.open("/sys/class/gpio/gpio32/value");
fs << "1"; // "0" for off
fs.close();
// select whether it is on, off or flash
return 0;
}
Upvotes: 5