Browse Source

first commit

master
Daniel 2 years ago
commit
9ab9cc69b6
4 changed files with 119 additions and 0 deletions
  1. +75
    -0
      led.cpp
  2. +25
    -0
      led.h
  3. +11
    -0
      main.cpp
  4. +8
    -0
      makefile

+ 75
- 0
led.cpp View File

@ -0,0 +1,75 @@
/* 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" << ledNumber << "on" << std::endl;
RemoveTrigger();
WriteLED("/brightness", "1");
}
void CLED::TurnOff(){
std::cout << "LED" << ledNumber << "off" << std::endl;
RemoveTrigger();
WriteLED("/brightness","0");
}
void CLED::blink(){
std::cout << "LED" << ledNumber << "blinking" << std::endl;
WriteLED("/trigger", "timer");
WriteLED("/delay_on", "50");
WriteLED("/delay_off", "50");
}
CLED::~CLED(){
std::cout << "destroying LED " << path << std::endl;
}
int main(int argc, char* argv[]){
if(argc!=2){
std::cout << "Para usarse es: makeLEDS <comand>" << std::endl;
std::cout << "los comandos son on, off y blink" << std::endl;
}
std::cout << "Starting app" << std::endl;
std::string cmd(argv[1]);
// se crean 4 objetos de LED y se ponen en el array para controlar todos los LEDS.
CLED leds[4]= {CLED(0), CLED(1), CLED(2), CLED(3) };
for(int i=0; i<=3; i++){
if(cmd=="on")leds[i].TurnOn();
else if(cmd=="off")leds[i].TurnOff();
else if(cmd=="blink")leds[i].blink();
else{std::cout << "comando invalido" << std::endl;}
}
std::cout << "programa terminado" << std::endl;
return 0;
}

+ 25
- 0
led.h View File

@ -0,0 +1,25 @@
/* class LED for manage BBB leds
* Daniel Llamas 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 void TurnOn();
virtual void TurnOff();
virtual void blink();
virtual ~CLED(); //class deconstructor
};
#endif

+ 11
- 0
main.cpp View File

@ -0,0 +1,11 @@
//#include<string>
using namespace std;
int main(int argc, char* argv[]){
CLED led1 = CLED(3);
led1.TurnOn();
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