-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLedControl.cpp
More file actions
188 lines (150 loc) · 3.2 KB
/
Copy pathLedControl.cpp
File metadata and controls
188 lines (150 loc) · 3.2 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include "LedControl.h"
#include <libutils/String.h>
#include <libutils/FileUtils.h>
#include <iostream>
#include <stdexcept>
#include <algorithm>
using namespace std;
using namespace Utils;
namespace OPI
{
Led::Led(const string& path): path(path)
{
list<string> parts = String::Split( File::GetFileName( path ), ":" );
if( parts.size() > 3 )
{
// Dont bail out on this, just ignore any leds all together
return;
}
// Not all platforms are strict in using a NS
if( parts.size() == 3 )
{
this->ns = parts.front();
parts.pop_front();
}
else
{
this->ns = "Unknown";
}
if( parts.size() == 2 )
{
this->color = parts.front();
parts.pop_front();
}
else
{
this->color = "Unknown";
}
this->name = parts.front();
parts.pop_front();
this->parseTrigger();
}
list<string> Led::Triggers()
{
return this->triggers;
}
string Led::CurrentTrigger()
{
return this->set_trigger;
}
void Led::SetTrigger(const string &trigger)
{
if( find(this->triggers.begin(), this->triggers.end(), trigger ) == this->triggers.end() )
{
throw runtime_error("No such trigger available");
}
File::Write(this->path+"/trigger", trigger, File::UserRW|File::GroupRead|File::OtherRead);
this->set_trigger = trigger;
}
string Led::Color()
{
return this->color;
}
string Led::Name()
{
return this->name;
}
string Led::NS()
{
return this->ns;
}
bool Led::Brightness()
{
return this->brightness;
}
void Led::Brightness(bool bright)
{
File::Write(this->path+"/brightness", to_string(bright), File::UserRW|File::GroupRead|File::OtherRead);
this->brightness = bright;
}
void Led::dump()
{
cout << "-----------------------------"<<endl;
cout << "Name : "<< this->name << endl;
cout << "Color : "<< this->color << endl;
cout << "Ns : "<< this->ns << endl;
for( const auto& trig: this->triggers )
{
cout << trig << " ";
}
cout << endl;
cout << "Trigger "<< this->set_trigger<<endl;
}
Led::~Led() = default;
void Led::parseTrigger()
{
string trg = File::GetContentAsString( this->path+"/trigger");
list<string> tmp_triggers = String::Split( trg, " ");
for( const string& trigg: tmp_triggers )
{
if( trigg[0] == '[')
{
this->set_trigger = String::Trimmed(trigg, "[]");
}
this->triggers.push_back( String::Trimmed(trigg, "[]") );
}
}
/*
* Implementation of LedControl
*/
LedControl::LedControl(const string& basepath): basepath(basepath)
{
list<string> ledpaths = File::Glob( this->basepath+"/*");
list<Led> llist;
for( const string& led: ledpaths)
{
LedPtr l(new Led(led));
this->leds[ l->Name() ] = l;
}
}
list<string> LedControl::LedNames()
{
list<string> l;
for( const auto& led: this->leds )
{
l.push_back( led.first );
}
return l;
}
list<string> LedControl::Triggers(const string &name)
{
return this->leds[name]->Triggers();
}
string LedControl::CurrentTrigger(const string &name)
{
return this->leds[name]->CurrentTrigger();
}
void LedControl::SetTrigger(const string &name, const string &trigger)
{
this->leds[name]->SetTrigger(trigger);
}
bool LedControl::Brightness(const string &name)
{
return this->leds[name]->Brightness();
}
void LedControl::Brightness(const string &name, bool bright)
{
this->leds[name]->Brightness( bright);
}
LedControl::~LedControl() = default;
} // End NS