Refactor code quite heavily.
[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::readData(){
60 //        qDebug("readData...");
61     QByteArray data = socket->readAll();
62 //        qDebug("read %d bytes.", data.size());
63
64 /*
65     for(int i=0; i < data.size(); i++){
66         qDebug("%d: %d", i, ((signed char)data.at(i)));
67     }
68 */
69
70     /*
71      * Actually it seems like that the first three bytes are used for
72      * identifying the "type" of data sent. However, for now using the
73      * first seems to suffice.
74      */
75     if(data.at(0) == 5){
76         /*
77          * Joystick movement
78          *
79          * X-Axis: positive values -> right, negative values -> left
80          * Y-Axis: positive values -> down, negative values -> up
81          */
82
83         setX((int)(signed char) data.at(4));
84         setY((int)(signed char) data.at(5));
85
86         emit(stickMoved(_x, _y));
87
88         /*
89          * Emulate a digital joystick.
90          */
91         if(_up && (_y > -threshold())){
92             setUp(false);
93         }else if(!_up && (_y < -threshold())){
94             setUp(true);
95         }
96
97         if(_down && (_y < threshold())){
98             setDown(false);
99         }else if(!_down && (_y > threshold())){
100             setDown(true);
101         }
102
103         if(_left && (_x > -threshold())){
104             setLeft(false);
105         }else if(!_left && (_x < -threshold())){
106             setLeft(true);
107         }
108
109         if(_right && (_x < threshold())){
110             setRight(false);
111         }else if(!_right && (_x > threshold())){
112             setRight(true);
113         }
114     }else if(data.at(0) == 8){ 
115         /*
116          * Button presses
117          *
118          * A -> 0, B -> 1, C -> 2, D ->3
119          * At index 3 to 6 (inclusive)
120          */
121
122         char buttonMap = 0;
123
124         for(int i = 3; i <= 6; i++){
125             for(int b = 0; b <= 3; b++){
126                 if(data.at(i) == b){
127                     buttonMap ^= (1 << b);
128                 }
129             }
130         }
131
132 //            qDebug("Button map: %d", buttonMap);
133         emit(buttonsChanged(buttonMap & 0x01, buttonMap & 0x02, buttonMap & 0x04, buttonMap & 0x08));
134
135         for(int i = 0; i <= 3; i++){
136             if(((buttonMap | oldButtonMap) & (1 << i)) > 0){
137                 bool val = (buttonMap & (1 << i)) > 0;
138                 switch (i){
139                 case 0:
140                     setA(val);
141                     break;
142                 case 1:
143                     setB(val);
144                     break;
145                 case 2:
146                     setC(val);
147                     break;
148                 case 3:
149                     setD(val);
150                     break;
151                 }
152             }
153         }
154
155         oldButtonMap = buttonMap;
156     }
157 }