From d528ba902a82ec1a498614cb93126e2d12fa2544 Mon Sep 17 00:00:00 2001 From: Gerardo Marx Date: Mon, 20 Apr 2020 05:09:12 +0000 Subject: [PATCH] OOP for LED class OK --- LED.cpp | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ LED.h | 25 +++++++++++++++++++++++++ main.cpp | 33 ++++++++++++++++++++++++++++++++ 3 files changed, 115 insertions(+) create mode 100644 LED.cpp create mode 100644 LED.h create mode 100644 main.cpp diff --git a/LED.cpp b/LED.cpp new file mode 100644 index 0000000..3d3078f --- /dev/null +++ b/LED.cpp @@ -0,0 +1,57 @@ +/*led.cpp*/ +#include"LED.h" +#include +#include +#include +#include + +#define LED_PATH "/sys/class/leds/beaglebone:green:usr" + +LED::LED(int number){ + this->number = number; + std::ostringstream s; + s << LED_PATH << number; // LED number to the Path + path = std::string(s.str()); // convert to string +} + +void LED::writeLED(std::string filename, std::string value){ + std::ofstream fs; + fs.open((path + filename).c_str()); + fs << value; + fs.close(); +} + +void LED::removeTrigger(){ + writeLED("/trigger", "none"); +} + +void LED::turnOn(){ + std::cout << "Turning LED" << number << "on" << std::endl; + removeTrigger(); + writeLED("/brightness", "1"); +} + +void LED::turnOff(){ + std::cout << "Turning LED" << number << "off" << std::endl; + removeTrigger(); + writeLED("/brightness", "0"); +} + +void LED::flash(std::string delayms ="50"){ + std::cout << "Making LED" << number << "flash" << std::endl; + writeLED("/trigger", "timer"); + writeLED("/delay_on", delayms); + writeLED("/delay_off", delayms); +} + +void LED::outputState(){ + std::ifstream fs; + fs.open((path + "/trigger").c_str()); + std::string line; + while(getline(fs,line)) std::cout << line < +#include +#include +#include + +class LED{ +private: + std::string path; + int number; + virtual void writeLED(std::string filename, std::string value); + virtual void removeTrigger(); +public: + LED(int number); + virtual void turnOn(); + virtual void turnOff(); + virtual void flash(std::string delayms); + virtual void outputState(); + virtual ~LED(); +}; + +#endif diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..1446e57 --- /dev/null +++ b/main.cpp @@ -0,0 +1,33 @@ +/*OOP in C++ example for LEDs + Gerardo Marx April/20/2020 +g++ main.cpp -o main LED.cpp*/ +#include +#include"LED.h" +using namespace std; + + +int main(int argc, char* argv[]){ + if(argc!=2){ + cout << "The usage is main " << endl; + cout << " is on, off flash or status" << endl; + cout << "e.g. main on" << endl; + return 2; + } + 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; +}