on e17b671 help sign in
unity8/plugins/Lights/Lights.cpp
181 lines 4.0 kB history raw
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 */
19#include "Lights.h"
20#include <QtCore/QDebug>
22extern "C" {
23#include "android-hardware.h"
24#include <string.h>
27Lights::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)
37Lights::~Lights()
39 if (m_lightDevice) {
40 hw_device_t* device = (hw_device_t*) m_lightDevice;
41 device->close(device);
42 }
45void Lights::setState(Lights::State newState)
47 if (m_state != newState) {
48 if (newState == Lights::On) {
49 turnOn();
50 } else {
51 turnOff();
52 }
53 }
56Lights::State Lights::state() const
58 return m_state;
61void Lights::setColor(const QColor &color)
63 if (m_color != color) {
64 m_color = color;
65 Q_EMIT colorChanged(m_color);
66 // FIXME: update the collor if the light is already on
67 }
70QColor Lights::color() const
72 return m_color;
75int Lights::onMillisec() const
77 return m_onMs;
80void Lights::setOnMillisec(int onMs)
82 if (m_onMs != onMs) {
83 m_onMs = onMs;
84 Q_EMIT onMillisecChanged(m_onMs);
85 // FIXME: update the property if the light is already on
86 }
89int Lights::offMillisec() const
91 return m_offMs;
94void Lights::setOffMillisec(int offMs)
96 if (m_offMs != offMs) {
97 m_offMs = offMs;
98 Q_EMIT offMillisecChanged(m_offMs);
99 // FIXME: update the property if the light is already on
100 }
103bool Lights::init()
105 if (m_lightDevice) {
106 return true;
107 }
109 int err;
110 hw_module_t* module;
112 err = hw_get_module(LIGHTS_HARDWARE_MODULE_ID, (hw_module_t const**)&module);
113 if (err == 0) {
114 hw_device_t* device;
115 err = module->methods->open(module, LIGHT_ID_NOTIFICATIONS, &device);
116 if (err == 0) {
117 m_lightDevice = (light_device_t*)device;
118 return true;
119 } else {
120 qWarning() << "Failed to access notification lights";
121 }
122 } else {
123 qWarning() << "Failed to initialize lights hardware.";
124 }
125 return false;
128void Lights::turnOn()
130 if (!init()) {
131 qWarning() << "No lights device";
132 return;
133 }
135 if (m_state == Lights::On) {
136 return;
137 }
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;
148 if (m_lightDevice->set_light(m_lightDevice, &state) != 0) {
149 qWarning() << "Failed to turn the light off";
150 } else {
151 m_state = Lights::On;
152 Q_EMIT stateChanged(m_state);
153 }
156void Lights::turnOff()
158 if (!init()) {
159 qWarning() << "No lights device";
160 return;
161 }
163 if (m_state == Lights::Off) {
164 return;
165 }
167 light_state_t state;
168 memset(&state, 0, sizeof(light_state_t));
169 state.color = 0x00000000;
170 state.flashMode = LIGHT_FLASH_NONE;
171 state.flashOnMS = 0;
172 state.flashOffMS = 0;
173 state.brightnessMode = 0;
175 if (m_lightDevice->set_light(m_lightDevice, &state) != 0) {
176 qWarning() << "Failed to turn the light off";
177 } else {
178 m_state = Lights::Off;
179 Q_EMIT stateChanged(m_state);
180 }