Browse Source

Class LED for usrLeds

master
commit
17ca504f60
1 changed files with 97 additions and 0 deletions
  1. +97
    -0
      MLed.cpp

+ 97
- 0
MLed.cpp View File

@ -0,0 +1,97 @@
/*OOP in C++ example for LEDs */
#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();
};
LED::LED(int number){
this->number = number;
ostringstream s;
s << LED_PATH << number; // LED number to the Path
path = string(s.str()); // convert 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;
}
int main(int argc, char* argv[]){
if(argc!=2){
cout << "The usage is main <command>" << endl;
cout << "<command> is on, off flash or status" << endl;
cout << "e.g. main on" << endl;
}
cout << "Starting program ..." << endl;
string cmd(argv[1]);
LED leds[4] = {LED(0), LED(1), LED(2), LED(3)};
for(int i=0; i<=3; i++){
if(cmd=="on")
leds[i].turnOn();
else if(cmd=="off")
leds[i].turnOff();
else if(cmd=="flash")
leds[i].flash("100");
else if(cmd=="status")
leds[i].outputState();
else
cout << "invalid command, please type main" << endl;
}
cout << "Program done, Thanks." << endl;
return 0;
}

Loading…
Cancel
Save