Unity 8
 All Classes Functions
Lights.cpp
1 /*
2  * Copyright (C) 2014 Canonical, Ltd.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 3.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * Author: Renato Araujo Oliveira Filho <renato.filho@canonical.com>
17  */
18 
19 #include "Lights.h"
20 #include <QtCore/QDebug>
21 
22 extern "C" {
23 #include <hardware/hardware.h>
24 #include <hardware/lights.h>
25 #include <string.h>
26 }
27 
28 Lights::Lights(QObject* parent)
29  : QObject(parent),
30  m_lightDevice(0),
31  m_color("blue"),
32  m_state(Lights::Off),
33  m_onMs(1000),
34  m_offMs(3000)
35 {
36 }
37 
38 Lights::~Lights()
39 {
40  if (m_lightDevice) {
41  hw_device_t* device = (hw_device_t*) m_lightDevice;
42  device->close(device);
43  }
44 }
45 
46 void Lights::setState(Lights::State newState)
47 {
48  if (m_state != newState) {
49  if (newState == Lights::On) {
50  turnOn();
51  } else {
52  turnOff();
53  }
54  }
55 }
56 
57 Lights::State Lights::state() const
58 {
59  return m_state;
60 }
61 
62 void Lights::setColor(const QColor &color)
63 {
64  if (m_color != color) {
65  m_color = color;
66  Q_EMIT colorChanged(m_color);
67  // FIXME: update the collor if the light is already on
68  }
69 }
70 
71 QColor Lights::color() const
72 {
73  return m_color;
74 }
75 
76 int Lights::onMillisec() const
77 {
78  return m_onMs;
79 }
80 
81 void Lights::setOnMillisec(int onMs)
82 {
83  if (m_onMs != onMs) {
84  m_onMs = onMs;
85  Q_EMIT onMillisecChanged(m_onMs);
86  // FIXME: update the property if the light is already on
87  }
88 }
89 
90 int Lights::offMillisec() const
91 {
92  return m_offMs;
93 }
94 
95 void Lights::setOffMillisec(int offMs)
96 {
97  if (m_offMs != offMs) {
98  m_offMs = offMs;
99  Q_EMIT offMillisecChanged(m_offMs);
100  // FIXME: update the property if the light is already on
101  }
102 }
103 
104 bool Lights::init()
105 {
106  if (m_lightDevice) {
107  return true;
108  }
109 
110  int err;
111  hw_module_t* module;
112 
113  err = hw_get_module(LIGHTS_HARDWARE_MODULE_ID, (hw_module_t const**)&module);
114  if (err == 0) {
115  hw_device_t* device;
116  err = module->methods->open(module, LIGHT_ID_NOTIFICATIONS, &device);
117  if (err == 0) {
118  m_lightDevice = (light_device_t*)device;
119  return true;
120  } else {
121  qWarning() << "Failed to access notification lights";
122  }
123  } else {
124  qWarning() << "Failed to initialize lights hardware.";
125  }
126  return false;
127 }
128 
129 void Lights::turnOn()
130 {
131  if (!init()) {
132  qWarning() << "No lights device";
133  return;
134  }
135 
136  if (m_state == Lights::On) {
137  return;
138  }
139 
140  // pulse
141  light_state_t state;
142  memset(&state, 0, sizeof(light_state_t));
143  state.color = m_color.rgba();
144  state.flashMode = LIGHT_FLASH_TIMED;
145  state.flashOnMS = m_onMs;
146  state.flashOffMS = m_offMs;
147  state.brightnessMode = BRIGHTNESS_MODE_USER;
148 
149  if (m_lightDevice->set_light(m_lightDevice, &state) != 0) {
150  qWarning() << "Failed to turn the light off";
151  } else {
152  m_state = Lights::On;
153  Q_EMIT stateChanged(m_state);
154  }
155 }
156 
157 void Lights::turnOff()
158 {
159  if (!init()) {
160  qWarning() << "No lights device";
161  return;
162  }
163 
164  if (m_state == Lights::Off) {
165  return;
166  }
167 
168  light_state_t state;
169  memset(&state, 0, sizeof(light_state_t));
170  state.color = 0x00000000;
171  state.flashMode = LIGHT_FLASH_NONE;
172  state.flashOnMS = 0;
173  state.flashOffMS = 0;
174  state.brightnessMode = 0;
175 
176  if (m_lightDevice->set_light(m_lightDevice, &state) != 0) {
177  qWarning() << "Failed to turn the light off";
178  } else {
179  m_state = Lights::Off;
180  Q_EMIT stateChanged(m_state);
181  }
182 }