Browse Source

Name of class and header is a convention, not a rule

master
parent
commit
ddb8f7ab39
4 changed files with 4 additions and 101 deletions
  1. +0
    -97
      MLed.cpp
  2. +1
    -1
      led.cpp
  3. +0
    -0
      led.h
  4. +3
    -3
      main.cpp

+ 0
- 97
MLed.cpp View File

@ -1,97 +0,0 @@
/*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;
}

LED.cpp → led.cpp View File

@ -1,5 +1,5 @@
/*led.cpp*/
#include"LED.h"
#include"led.h"
#include<iostream>
#include<fstream>
#include<string>

LED.h → led.h View File


+ 3
- 3
main.cpp View File

@ -2,14 +2,14 @@
Gerardo Marx April/20/2020
g++ main.cpp -o main LED.cpp*/
#include<string>
#include"LED.h"
#include"led.h"
using namespace std;
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 << "The usage is main mkLed" << endl;
cout << "mkLed is on, off flash or status" << endl;
cout << "e.g. main on" << endl;
return 2;
}


Loading…
Cancel
Save