Unity 8
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 "android-hardware.h"
24 #include <string.h>
25 }
26 
27 Lights::Lights(QObject* parent)
28  : QObject(parent),
29  m_lightDevice(0),
30  m_color("blue"),
31  m_state(Lights::Off),
32  m_onMs(1000),
33  m_offMs(3000)
34 {
35 }
36 
37 Lights::~Lights()
38 {
39  if (m_lightDevice) {
40  hw_device_t* device = (hw_device_t*) m_lightDevice;
41  device->close(device);
42  }
43 }
44 
45 void Lights::setState(Lights::State newState)
46 {
47  if (!init()) {
48  qWarning() << "No lights device";
49  return;
50  }
51 
52  if (m_state != newState) {
53  if (newState == Lights::On) {
54  turnOn();
55  } else {
56  turnOff();
57  }
58 
59  m_state = newState;
60  Q_EMIT stateChanged(m_state);
61  }
62 }
63 
64 Lights::State Lights::state() const
65 {
66  return m_state;
67 }
68 
69 void Lights::setColor(const QColor &color)
70 {
71  if (m_color != color) {
72  m_color = color;
73  Q_EMIT colorChanged(m_color);
74  // FIXME: update the color if the light is already on
75  }
76 }
77 
78 QColor Lights::color() const
79 {
80  return m_color;
81 }
82 
83 int Lights::onMillisec() const
84 {
85  return m_onMs;
86 }
87 
88 void Lights::setOnMillisec(int onMs)
89 {
90  if (m_onMs != onMs) {
91  m_onMs = onMs;
92  Q_EMIT onMillisecChanged(m_onMs);
93  // FIXME: update the property if the light is already on
94  }
95 }
96 
97 int Lights::offMillisec() const
98 {
99  return m_offMs;
100 }
101 
102 void Lights::setOffMillisec(int offMs)
103 {
104  if (m_offMs != offMs) {
105  m_offMs = offMs;
106  Q_EMIT offMillisecChanged(m_offMs);
107  // FIXME: update the property if the light is already on
108  }
109 }
110 
111 bool Lights::init()
112 {
113  if (m_lightDevice) {
114  return true;
115  }
116 
117  int err;
118  hw_module_t* module;
119 
120  err = hw_get_module(LIGHTS_HARDWARE_MODULE_ID, (hw_module_t const**)&module);
121  if (err == 0) {
122  hw_device_t* device;
123  err = module->methods->open(module, LIGHT_ID_NOTIFICATIONS, &device);
124  if (err == 0) {
125  m_lightDevice = (light_device_t*)device;
126  turnOff();
127  return true;
128  } else {
129  qWarning() << "Failed to access notification lights";
130  }
131  } else {
132  qWarning() << "Failed to initialize lights hardware.";
133  }
134  return false;
135 }
136 
137 void Lights::turnOn()
138 {
139  // pulse
140  light_state_t state;
141  memset(&state, 0, sizeof(light_state_t));
142  state.color = m_color.rgba();
143  state.flashMode = LIGHT_FLASH_TIMED;
144  state.flashOnMS = m_onMs;
145  state.flashOffMS = m_offMs;
146  state.brightnessMode = BRIGHTNESS_MODE_USER;
147 
148  if (m_lightDevice->set_light(m_lightDevice, &state) != 0) {
149  qWarning() << "Failed to turn the light off";
150  }
151 }
152 
153 void Lights::turnOff()
154 {
155  light_state_t state;
156  memset(&state, 0, sizeof(light_state_t));
157  state.color = 0x00000000;
158  state.flashMode = LIGHT_FLASH_NONE;
159  state.flashOnMS = 0;
160  state.flashOffMS = 0;
161  state.brightnessMode = 0;
162 
163  if (m_lightDevice->set_light(m_lightDevice, &state) != 0) {
164  qWarning() << "Failed to turn the light off";
165  }
166 }