Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Derek Molloy
committed
Mar 25, 2015
1 parent
34f6e51
commit 20bea25
Showing
7 changed files
with
148 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#include "LED.h" | ||
|
||
LED::LED(int number){ | ||
this->number = number; | ||
// much easier with C++11 using to_string(number) | ||
ostringstream s; // using a stream to contruct the path | ||
s << LED_PATH << number; //append LED number to LED_PATH | ||
path = string(s.str()); //convert back from stream to string | ||
} | ||
|
||
void LED::writeLED(string filename, string value){ | ||
ofstream fs; | ||
fs.open((path + filename).c_str()); | ||
fs << value; | ||
fs.close(); | ||
} | ||
|
||
void LED::removeTrigger(){ | ||
writeLED("/trigger", "none"); | ||
} | ||
|
||
void LED::turnOn(){ | ||
cout << "Turning LED" << number << " on." << endl; | ||
removeTrigger(); | ||
writeLED("/brightness", "1"); | ||
} | ||
|
||
void LED::turnOff(){ | ||
cout << "Turning LED" << number << " off." << endl; | ||
removeTrigger(); | ||
writeLED("/brightness", "0"); | ||
} | ||
|
||
void LED::flash(string delayms = "50"){ | ||
cout << "Making LED" << number << " flash." << endl; | ||
writeLED("/trigger", "timer"); | ||
writeLED("/delay_on", delayms); | ||
writeLED("/delay_off", delayms); | ||
} | ||
|
||
void LED::outputState(){ | ||
ifstream fs; | ||
fs.open( (path + "/trigger").c_str()); | ||
string line; | ||
while(getline(fs,line)) cout << line << endl; | ||
fs.close(); | ||
} | ||
|
||
LED::~LED(){ | ||
cout << "destroying the LED with path: " << path << endl; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** Simple On-board LED flashing program - written by Derek Molloy | ||
* simple OOP struture for the Exploring BeagleBone book | ||
* | ||
* This program uses all four LEDS and can be executed in three ways: | ||
* makeLEDs on | ||
* makeLEDs off | ||
* makeLEDs flash (flash at time delay intervals) | ||
* makeLEDs status (get the trigger status) | ||
* | ||
* Written by Derek Molloy for the book "Exploring BeagleBone: Tools and | ||
* Techniques for Building with Embedded Linux" by John Wiley & Sons, 2014 | ||
* ISBN 9781118935125. Please see the file README.md in the repository root | ||
* directory for copyright and GNU GPLv3 license information. */ | ||
|
||
#include<iostream> | ||
#include<fstream> | ||
#include<string> | ||
#include<sstream> | ||
using namespace std; | ||
|
||
#define LED_PATH "/sys/class/leds/beaglebone:green:usr" | ||
|
||
class LED{ | ||
private: | ||
string path; | ||
int number; | ||
virtual void writeLED(string filename, string value); | ||
virtual void removeTrigger(); | ||
public: | ||
LED(int number); | ||
virtual void turnOn(); | ||
virtual void turnOff(); | ||
virtual void flash(string delayms); | ||
virtual void outputState(); | ||
virtual ~LED(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
#!/bin/bash | ||
echo "Building the hello.cgi C++ CGI Program" | ||
g++ hello.cpp -o hello.cgi | ||
|
||
echo "Building the getLED.cgi C++ CGI Program" | ||
g++ getLED.cpp LED.cpp -o getLED.cgi -lcgicc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#!/bin/bash | ||
echo "Copying the cgi files to the /usr/lib/cgi-bin/" | ||
sudo cp *.cgi /usr/lib/cgi-bin/ | ||
sudo chmod +s /usr/lib/cgi-bin/getLED.cgi |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* C++ CGI BeagleBone GET example -- Written by Derek Molloy (www.derekmolloy.ie) | ||
You must set the sticky bit for this script in order that it can | ||
access the on-board LED sysfs file system. | ||
*/ | ||
|
||
#include <iostream> // for the input/output | ||
#include <stdlib.h> // for the getenv call | ||
#include <sys/sysinfo.h> // for the system uptime call | ||
|
||
#include <cgicc/Cgicc.h> // the cgicc headers | ||
#include <cgicc/CgiDefs.h> | ||
#include <cgicc/HTTPHTMLHeader.h> | ||
#include <cgicc/HTMLClasses.h> | ||
|
||
#include "LED.h" // the LED class from Chapter 5 of the book | ||
|
||
using namespace std; | ||
using namespace cgicc; | ||
|
||
int main(){ | ||
Cgicc form; | ||
LED *led3 = new LED(3); | ||
|
||
char *value = getenv("REMOTE_ADDR"); // The remote address CGI environment variable | ||
cout << "Content-type:text/html\r\n\r\n"; // Generate the HTML output | ||
cout << "<html><head>\n"; | ||
cout << "<title>EBB C++ GET Example</title>\n"; | ||
cout << "</head><body>\n"; | ||
cout << "<h1>BeagleBone GET Example</h1>\n"; | ||
|
||
form_iterator it = form.getElement("command"); | ||
string cmd(**it); | ||
if (!it->isEmpty() && it!=(*form).end()) { | ||
cout << "<div> The LED command is " << cmd << ".</div>"; | ||
|
||
/** This code sets the USR3 LED state using the LED class **/ | ||
if(cmd=="on") led3->turnOn(); | ||
else if(cmd=="off") led3->turnOff(); | ||
else if(cmd=="flash") led3->flash("100"); | ||
else if(cmd=="status"){ | ||
cout << "<div>"; | ||
led3->outputState(); | ||
cout << "</div>"; | ||
} | ||
else cout << "<div> Invalid command! </div>"; | ||
} | ||
else{ | ||
cout << "<div> The LED command is missing or invalid.</div>"; | ||
} | ||
cout << "<div> The CGI REMOTE_ADDR environment variable is " << value << "</div>"; | ||
cout << "</body></html>\n"; | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters