Browse Source

First commit

master
Gustavo Martinez 2 years ago
commit
8d97173657
5 changed files with 109 additions and 0 deletions
  1. +1
    -0
      .gitignore
  2. +49
    -0
      led.cpp
  3. +25
    -0
      led.h
  4. +26
    -0
      main.cpp
  5. +8
    -0
      makefile

+ 1
- 0
.gitignore View File

@ -0,0 +1 @@
led

+ 49
- 0
led.cpp View File

@ -0,0 +1,49 @@
/* led.cpp */
#include"led.h"
#include<iostream>
#include<fstream>
#include<string>
#include<sstream>
#define LED_PATH "/sys/class/leds/beaglebone:green:usr"
CLED::CLED(int ledNumber){
this->ledNumber = ledNumber; //Solo para argumentos que sean numeros
std::ostringstream s;
s << LED_PATH << ledNumber;
path =std::string(s.str()); //convert to string
}
void CLED::WriteLED(std::string filename, std::string value){
std::ofstream fs;
fs.open((path+filename).c_str());
fs << value;
fs.close();
}
void CLED::RemoveTrigger(){
WriteLED("/trigger", "none");
}
void CLED::TurnOn(){
std::cout << "LED Turn On" << ledNumber << "on" << std::endl;
RemoveTrigger();
WriteLED("/brightness", "1");
}
void CLED::TurnOff(){
std::cout << "LED Turn Off" << ledNumber << "off" << std::endl;
RemoveTrigger();
WriteLED("/brightness", "0");
}
void CLED::BlinkLED(){
std::cout << "LED blinking" << ledNumber << "blinking" << std::endl;
WriteLED("/trigger", "timer");
WriteLED("/delay_on", "50");
WriteLED("/delay_off", "50");
}
CLED::~CLED(){
std::cout << "destroying LED " << path << std::endl;
}

+ 25
- 0
led.h View File

@ -0,0 +1,25 @@
/* class LED for manage BBB leds
* Gustavo Martnez Mondragn 14/03/2022*/
#ifndef LED_H
#define LED_H
#include<iostream>
#include<fstream>
#include<string>
#include<sstream>
class CLED{
private:
std::string path;
int ledNumber;
virtual void WriteLED(std::string filename, std::string value);
virtual void RemoveTrigger();
public:
CLED(int ledNumber); //class creator
virtual ~CLED(); //class deconstructor
virtual void TurnOn();
virtual void TurnOff();
virtual void BlinkLED();
};
#endif

+ 26
- 0
main.cpp View File

@ -0,0 +1,26 @@
#include"led.h"
#include<string>
using namespace std;
int main(int argc, char* argv[]){
CLED led1 = CLED(3);
string cmd(argv[1]);
if(argc!=2)
std::cout << "Incorrect number of arguments" << std::endl;
if(cmd=="on")
led1.TurnOn();
else if(cmd=="off")
led1.TurnOff();
else if(cmd=="blink")
led1.BlinkLED();
else
cout << "Invalid command" << endl;
return 0;
}

+ 8
- 0
makefile View File

@ -0,0 +1,8 @@
all:
g++ main.cpp -o led led.cpp
clean:
rm led
debug:
g++ -g main.cpp -o led led.cpp

Loading…
Cancel
Save