Refactor code. Add tabs for enabling using two remote controls at once.
[qzeecontrol] / btconnector.cpp
1 /*
2  *  Copyright 2012 Ruediger Gad
3  *
4  *  This file is part of QZeeControl.
5  *
6  *  QZeeControl is free software: you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation, either version 3 of the License, or
9  *  (at your option) any later version.
10  *
11  *  QZeeControl 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
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with QZeeControl.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include "btconnector.h"
21
22 BtConnector::BtConnector(QObject *parent)
23     : QObject(parent){
24     _threshold = 50;
25
26     _up = false;
27     _down = false;
28     _left = false;
29     _right = false;
30
31     _a = false;
32     _b = false;
33     _c = false;
34     _d = false;
35
36     _x = 0;
37     _y = 0;
38     oldButtonMap = 0;
39 }
40
41 void BtConnector::connect(QString address, int port){
42     qDebug("Trying to connect to: %s--%d", address.toUtf8().constData(), port);
43
44     if(socket)
45         delete socket;
46
47     socket = new QBluetoothSocket(QBluetoothSocket::RfcommSocket);
48     QObject::connect(socket, SIGNAL(connected()), this, SIGNAL(connected()));
49     QObject::connect(socket, SIGNAL(disconnected()), this, SIGNAL(disconnected()));
50     QObject::connect(socket, SIGNAL(error(QBluetoothSocket::SocketError)), this, SIGNAL(error(QBluetoothSocket::SocketError)));
51
52     qDebug("Connecting...");
53     socket->connectToService(QBluetoothAddress(address), port);
54     qDebug("Connected.");
55
56     QObject::connect(socket, SIGNAL(readyRead()), this, SLOT(readData()));
57 }
58
59 void BtConnector::disconnect(){
60     if(!socket)
61         return;
62
63     if(socket->isOpen())
64         socket->close();
65
66     delete socket;
67     socket = 0;
68
69     /*
70      * Explicitly set D to false in case the remote was shut off
71      * using the power key, which equals the 'D' key.
72      */
73     setD(false);
74 }
75
76 void BtConnector::readData(){
77 //        qDebug("readData...");
78     QByteArray data = socket->readAll();
79 //        qDebug("read %d bytes.", data.size());
80
81 /*
82     for(int i=0; i < data.size(); i++){
83         qDebug("%d: %d", i, ((signed char)data.at(i)));
84     }
85 */
86
87     /*
88      * Actually it seems like that the first three bytes are used for
89      * identifying the "type" of data sent. However, for now using the
90      * first seems to suffice.
91      */
92     if(data.at(0) == 5){
93         /*
94          * Joystick movement
95          *
96          * X-Axis: positive values -> right, negative values -> left
97          * Y-Axis: positive values -> down, negative values -> up
98          */
99
100         setX((int)(signed char) data.at(4));
101         setY((int)(signed char) data.at(5));
102
103         emit(stickMoved(_x, _y));
104
105         /*
106          * Emulate a digital joystick.
107          */
108         if(_up && (_y > -threshold())){
109             setUp(false);
110         }else if(!_up && (_y < -threshold())){
111             setUp(true);
112         }
113
114         if(_down && (_y < threshold())){
115             setDown(false);
116         }else if(!_down && (_y > threshold())){
117             setDown(true);
118         }
119
120         if(_left && (_x > -threshold())){
121             setLeft(false);
122         }else if(!_left && (_x < -threshold())){
123             setLeft(true);
124         }
125
126         if(_right && (_x < threshold())){
127             setRight(false);
128         }else if(!_right && (_x > threshold())){
129             setRight(true);
130         }
131     }else if(data.at(0) == 8){ 
132         /*
133          * Button presses
134          *
135          * A -> 0, B -> 1, C -> 2, D ->3
136          * At index 3 to 6 (inclusive)
137          */
138
139         char buttonMap = 0;
140
141         for(int i = 3; i <= 6; i++){
142             for(int b = 0; b <= 3; b++){
143                 if(data.at(i) == b){
144                     buttonMap ^= (1 << b);
145                 }
146             }
147         }
148
149 //            qDebug("Button map: %d", buttonMap);
150         emit(buttonsChanged(buttonMap & 0x01, buttonMap & 0x02, buttonMap & 0x04, buttonMap & 0x08));
151
152         for(int i = 0; i <= 3; i++){
153             if(((buttonMap | oldButtonMap) & (1 << i)) > 0){
154                 bool val = (buttonMap & (1 << i)) > 0;
155                 switch (i){
156                 case 0:
157                     setA(val);
158                     break;
159                 case 1:
160                     setB(val);
161                     break;
162                 case 2:
163                     setC(val);
164                     break;
165                 case 3:
166                     setD(val);
167                     break;
168                 }
169             }
170         }
171
172         oldButtonMap = buttonMap;
173     }
174 }