Remove some obsolete code.
[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         // Joystick movement
77         _x = (int)(signed char) data.at(4);
78         _y = (int)(signed char) data.at(5);
79
80         emit(xChanged(_x));
81         emit(yChanged(_y));
82         emit(stickMoved(_x, _y));
83
84         if(_up && (_y > -threshold())){
85             setUp(false);
86         }else if(!_up && (_y < -threshold())){
87             setUp(true);
88         }
89
90         if(_down && (_y < threshold())){
91             setDown(false);
92         }else if(!_down && (_y > threshold())){
93             setDown(true);
94         }
95
96         if(_left && (_x > -threshold())){
97             setLeft(false);
98         }else if(!_left && (_x < -threshold())){
99             setLeft(true);
100         }
101
102         if(_right && (_x < threshold())){
103             setRight(false);
104         }else if(!_right && (_x > threshold())){
105             setRight(true);
106         }
107     }else if(data.at(0) == 8){
108         // Button press
109         /*
110          * A -> 0, B -> 1, C -> 2, D ->3
111          * At index 3 to 6 (inclusive)
112          */
113
114         char buttonMap = 0;
115
116         for(int i = 3; i <= 6; i++){
117             for(int b = 0; b <= 3; b++){
118                 if(data.at(i) == b){
119                     buttonMap ^= (1 << b);
120                 }
121             }
122         }
123
124 //            qDebug("Button map: %d", buttonMap);
125         emit(buttonsChanged(buttonMap & 0x01, buttonMap & 0x02, buttonMap & 0x04, buttonMap & 0x08));
126
127         for(int i = 0; i <= 3; i++){
128             if(((buttonMap | oldButtonMap) & (1 << i)) > 0){
129                 bool val = (buttonMap & (1 << i)) > 0;
130                 switch (i){
131                 case 0:
132                     _a = val;
133                     emit (aChanged(val));
134                     break;
135                 case 1:
136                     _b = val;
137                     emit (bChanged(val));
138                     break;
139                 case 2:
140                     _c = val;
141                     emit (cChanged(val));
142                     break;
143                 case 3:
144                     _d = val;
145                     emit (dChanged(val));
146                     break;
147                 }
148             }
149         }
150
151         oldButtonMap = buttonMap;
152     }
153 }