-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
90 lines (80 loc) · 2.08 KB
/
Copy pathmain.cpp
File metadata and controls
90 lines (80 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
//Example of interacting with user LEDs using cpp Language
//Salvador Medina Alonzo
//04/05/2020
#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 flashing();
virtual ~LED();
};
LED::LED(int number){
this->number = number;
ostringstream ledString;
ledString << LED_PATH << number;
path = string(ledString.str());
}
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 << "LED" << number << " on" << endl;
removeTrigger();
writeLED("/trigger", "1");
}
void LED::turnOff(){
cout << "LED" << number << " off" << endl;
removeTrigger();
writeLED("/trigger", "0");
}
void LED::flashing(){
cout << "LED" << number << " flashing" << endl;
removeTrigger();
writeLED("/trigger", "timer");
writeLED("/delay_on", "50");
writeLED("/delay_off", "50");
}
LED::~LED(){
cout << "removing LED..." << number << endl;
}
//Main function:
int main(int argc, char* argv[]){
if(argc!=2){
cout << "Error" << endl;
cout << "Try tiping: on, off or flashing" << endl;
return 2;
}
cout << "Starting LED app" << 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=="flashing")
leds[i].flashing();
else
cout << "Wrong command passed!!!" << endl;
//cout << "Try tiping: on, off or flashing" << endl;
}
cout << "Script done" << endl;
}