on xenial help sign in
better lights init
bbbbec1Nick Dedekind
plugins/Lights/Lights.cpp+9−24
@@ -44,12 +44,20 @@ Lights::~Lights()
44
44
45void Lights::setState(Lights::State newState)
45void Lights::setState(Lights::State newState)
46{
46{
47+ if (!init()) {
48+ qWarning() << "No lights device";
49+ return;
50+ }
51+
47 if (m_state != newState) {
52 if (m_state != newState) {
48 if (newState == Lights::On) {
53 if (newState == Lights::On) {
49 turnOn();
54 turnOn();
50 } else {
55 } else {
51 turnOff();
56 turnOff();
52 }
57 }
58+
59+ m_state = newState;
60+ Q_EMIT stateChanged(m_state);
53 }
61 }
54}
62}
55
63
@@ -115,6 +123,7 @@ bool Lights::init()
115 err = module->methods->open(module, LIGHT_ID_NOTIFICATIONS, &device);
123 err = module->methods->open(module, LIGHT_ID_NOTIFICATIONS, &device);
116 if (err == 0) {
124 if (err == 0) {
117 m_lightDevice = (light_device_t*)device;
125 m_lightDevice = (light_device_t*)device;
126+ turnOff();
118 return true;
127 return true;
119 } else {
128 } else {
120 qWarning() << "Failed to access notification lights";
129 qWarning() << "Failed to access notification lights";
@@ -127,15 +136,6 @@ bool Lights::init()
127
136
128void Lights::turnOn()
137void Lights::turnOn()
129{
138{
130− if (!init()) {
131− qWarning() << "No lights device";
132− return;
133− }
134−
135− if (m_state == Lights::On) {
136− return;
137− }
138−
139 // pulse
139 // pulse
140 light_state_t state;
140 light_state_t state;
141 memset(&state, 0, sizeof(light_state_t));
141 memset(&state, 0, sizeof(light_state_t));
@@ -147,23 +147,11 @@ void Lights::turnOn()
147
147
148 if (m_lightDevice->set_light(m_lightDevice, &state) != 0) {
148 if (m_lightDevice->set_light(m_lightDevice, &state) != 0) {
149 qWarning() << "Failed to turn the light off";
149 qWarning() << "Failed to turn the light off";
150− } else {
151− m_state = Lights::On;
152− Q_EMIT stateChanged(m_state);
153 }
150 }
154}
151}
155
152
156void Lights::turnOff()
153void Lights::turnOff()
157{
154{
158− if (!init()) {
159− qWarning() << "No lights device";
160− return;
161− }
162−
163− if (m_state == Lights::Off) {
164− return;
165− }
166−
167 light_state_t state;
155 light_state_t state;
168 memset(&state, 0, sizeof(light_state_t));
156 memset(&state, 0, sizeof(light_state_t));
169 state.color = 0x00000000;
157 state.color = 0x00000000;
@@ -174,8 +162,5 @@ void Lights::turnOff()
174
162
175 if (m_lightDevice->set_light(m_lightDevice, &state) != 0) {
163 if (m_lightDevice->set_light(m_lightDevice, &state) != 0) {
176 qWarning() << "Failed to turn the light off";
164 qWarning() << "Failed to turn the light off";
177− } else {
178− m_state = Lights::Off;
179− Q_EMIT stateChanged(m_state);
180 }
165 }
181}
166}