on xenial help sign in
better lights init
commitbbbbec1ffdf8c84d4f49dee8af1beb1fdc007dd2 authorNick Dedekind <nick.dedekind@canonical.com> dateFri 12 Dec 2014, 14:16 (12 years ago) parent05a92b8
1 file changed+9−24
plugins/Lights/Lights.cpp+9−24
plugins/Lights/Lights.cpp+9−24
@@ -44,12 +44,20 @@ Lights::~Lights()
4444
4545void Lights::setState(Lights::State newState)
4646{
47+ if (!init()) {
48+ qWarning() << "No lights device";
49+ return;
50+ }
51+
4752 if (m_state != newState) {
4853 if (newState == Lights::On) {
4954 turnOn();
5055 } else {
5156 turnOff();
5257 }
58+
59+ m_state = newState;
60+ Q_EMIT stateChanged(m_state);
5361 }
5462}
5563
@@ -115,6 +123,7 @@ bool Lights::init()
115123 err = module->methods->open(module, LIGHT_ID_NOTIFICATIONS, &device);
116124 if (err == 0) {
117125 m_lightDevice = (light_device_t*)device;
126+ turnOff();
118127 return true;
119128 } else {
120129 qWarning() << "Failed to access notification lights";
@@ -127,15 +136,6 @@ bool Lights::init()
127136
128137void Lights::turnOn()
129138{
130− if (!init()) {
131− qWarning() << "No lights device";
132− return;
133− }
134−
135− if (m_state == Lights::On) {
136− return;
137− }
138−
139139 // pulse
140140 light_state_t state;
141141 memset(&state, 0, sizeof(light_state_t));
@@ -147,23 +147,11 @@ void Lights::turnOn()
147147
148148 if (m_lightDevice->set_light(m_lightDevice, &state) != 0) {
149149 qWarning() << "Failed to turn the light off";
150− } else {
151− m_state = Lights::On;
152− Q_EMIT stateChanged(m_state);
153150 }
154151}
155152
156153void Lights::turnOff()
157154{
158− if (!init()) {
159− qWarning() << "No lights device";
160− return;
161− }
162−
163− if (m_state == Lights::Off) {
164− return;
165− }
166−
167155 light_state_t state;
168156 memset(&state, 0, sizeof(light_state_t));
169157 state.color = 0x00000000;
@@ -174,8 +162,5 @@ void Lights::turnOff()
174162
175163 if (m_lightDevice->set_light(m_lightDevice, &state) != 0) {
176164 qWarning() << "Failed to turn the light off";
177− } else {
178− m_state = Lights::Off;
179− Q_EMIT stateChanged(m_state);
180165 }
181166}