Added copyright headers and COPYING file.
[harmattan/cameraplus] / src / compass.cpp
1 /*!
2  * This file is part of CameraPlus.
3  *
4  * Copyright (C) 2012 Mohammed Sameer <msameer@foolab.org>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
19  */
20
21 #include "compass.h"
22 #include <qmcompass.h>
23 #include <QDebug>
24
25 Compass::Compass(QObject *parent) :
26   QObject(parent),
27   m_compass(new MeeGo::QmCompass(this)),
28   m_degree(-1),
29   m_valid(false) {
30
31   m_compass->setUseDeclination(true);
32
33   QObject::connect(m_compass, SIGNAL(dataAvailable(const MeeGo::QmCompassReading&)),
34                    this, SLOT(dataAvailable(const MeeGo::QmCompassReading&)));
35
36   if (m_compass->requestSession(MeeGo::QmSensor::SessionTypeListen)
37       == MeeGo::QmSensor::SessionTypeNone) {
38     qDebug() << "Failed to get listen session:" << m_compass->lastError();
39   }
40 }
41
42 Compass::~Compass() {
43   m_compass->stop();
44 }
45
46 bool Compass::isActive() const {
47   return m_compass->isRunning();
48 }
49
50 void Compass::setActive(bool active) {
51   if (active == isActive()) {
52     return;
53   }
54
55   if (active) {
56     m_compass->start();
57   }
58   else {
59     m_compass->stop();
60
61     m_valid = false;
62     emit directionValidChanged();
63   }
64
65   emit activeChanged();
66 }
67
68 int Compass::direction() const {
69   return m_degree;
70 }
71
72 bool Compass::isDirectionValid() const {
73   return m_valid;
74 }
75
76 void Compass::dataAvailable(const MeeGo::QmCompassReading& value) {
77   bool degreeChanged = (m_degree != value.degrees);
78   bool validityChanged = m_valid != (value.level >= 2);
79
80   m_degree = value.degrees;
81   m_valid = (value.level >= 2);
82
83   if (validityChanged) {
84     emit directionValidChanged();
85   }
86
87   if (degreeChanged) {
88     emit directionChanged();
89   }
90 }