You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

79 lines
1.9 KiB

2 years ago
2 years ago
  1. /* led.cpp */
  2. #include"led.h"
  3. #include<iostream>
  4. #include<fstream>
  5. #include<string>
  6. #include<sstream>
  7. #define LED_PATH "/sys/class/leds/beaglebone:green:usr"
  8. CLED::CLED(int ledNumber){
  9. this->ledNumber = ledNumber; //Solo para argumentos que sean numeros
  10. std::ostringstream s;
  11. s << LED_PATH << ledNumber;
  12. path =std::string(s.str()); //convert to string
  13. }
  14. void CLED::WriteLED(std::string filename, std::string value){
  15. std::ofstream fs;
  16. fs.open((path+filename).c_str());
  17. fs << value;
  18. fs.close();
  19. }
  20. void CLED::RemoveTrigger(){
  21. WriteLED("/trigger", "none");
  22. }
  23. void CLED::TurnOn(){
  24. std::cout << "LED" << ledNumber << "on" << std::endl;
  25. RemoveTrigger();
  26. WriteLED("/brightness", "1");
  27. }
  28. void CLED::TurnOff(){
  29. std::cout << "LED" << ledNumber << "off" << std::endl;
  30. RemoveTrigger();
  31. WriteLED("/brightness","0");
  32. }
  33. void CLED::blink(){
  34. std::cout << "LED" << ledNumber << "blinking" << std::endl;
  35. WriteLED("/trigger", "timer");
  36. WriteLED("/delay_on", "50");
  37. WriteLED("/delay_off", "50");
  38. }
  39. CLED::~CLED(){
  40. std::cout << "destroying LED " << path << std::endl;
  41. }
  42. int main(int argc, char* argv[]){
  43. if(argc!=2){
  44. std::cout << "Para usarse es: makeLEDS <comand>" << std::endl;
  45. std::cout << "los comandos son on, off y blink" << std::endl;
  46. }
  47. std::cout << "Starting app" << std::endl;
  48. std::string cmd(argv[1]);
  49. // se crean 4 objetos de LED y se ponen en el array para controlar todos los LEDS.
  50. CLED leds[4]= {CLED(0), CLED(1), CLED(2), CLED(3) };
  51. for(int i=0; i<=3; i++){
  52. if(cmd=="on")leds[i].TurnOn();
  53. else if(cmd=="off")leds[i].TurnOff();
  54. else if(cmd=="blink")leds[i].blink();
  55. else{std::cout << "comando invalido" << std::endl;
  56. std::cout << "Para encender el LED utilice: ./leds on" << std::endl;
  57. std::cout << "Para apagar el LED utilice: ./leds off" << std::endl;
  58. std::cout << "Para hacer parpadear el LED utilice: sudo ./leds blink" << std::endl;
  59. }
  60. }
  61. std::cout << "programa terminado" << std::endl;
  62. return 0;
  63. }