First prototype for sending key events.
[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     _a = false;
25     _b = false;
26     _c = false;
27     _d = false;
28
29     _x = 0;
30     _y = 0;
31     oldButtonMap = 0;
32 }
33
34 void BtConnector::connect(QString address, int port){
35     qDebug("Trying to connect to: %s--%d", address.toUtf8().constData(), port);
36
37     if(socket)
38         delete socket;
39
40     socket = new QBluetoothSocket(QBluetoothSocket::RfcommSocket);
41     QObject::connect(socket, SIGNAL(connected()), this, SIGNAL(connected()));
42     QObject::connect(socket, SIGNAL(disconnected()), this, SIGNAL(disconnected()));
43     QObject::connect(socket, SIGNAL(error(QBluetoothSocket::SocketError)), this, SIGNAL(error(QBluetoothSocket::SocketError)));
44
45     qDebug("Connecting...");
46     socket->connectToService(QBluetoothAddress(address), port);
47     qDebug("Connected.");
48
49     QObject::connect(socket, SIGNAL(readyRead()), this, SLOT(readData()));
50 }
51
52 void BtConnector::readData(){
53 //        qDebug("readData...");
54     QByteArray data = socket->readAll();
55 //        qDebug("read %d bytes.", data.size());
56
57 /*
58     for(int i=0; i < data.size(); i++){
59         qDebug("%d: %d", i, ((signed char)data.at(i)));
60     }
61 */
62
63     /*
64      * Actually it seems like that the first three bytes are used for
65      * identifying the "type" of data sent. However, for now using the
66      * first seems to suffice.
67      */
68     if(data.at(0) == 5){
69         // Joystick movement
70         _x = (int)(signed char) data.at(4);
71         _y = (int)(signed char) data.at(5);
72
73         emit(xChanged(_x));
74         emit(yChanged(_y));
75         emit(stickMoved(_x, _y));
76     }else if(data.at(0) == 8){
77         // Button press
78         /*
79          * A -> 0, B -> 1, C -> 2, D ->3
80          * At index 3 to 6 (inclusive)
81          */
82
83         char buttonMap = 0;
84
85         for(int i = 3; i <= 6; i++){
86             for(int b = 0; b <= 3; b++){
87                 if(data.at(i) == b){
88                     buttonMap ^= (1 << b);
89                 }
90             }
91         }
92
93 //            qDebug("Button map: %d", buttonMap);
94         emit(buttonsChanged(buttonMap & 0x01, buttonMap & 0x02, buttonMap & 0x04, buttonMap & 0x08));
95
96         for(int i = 0; i <= 3; i++){
97             if(((buttonMap | oldButtonMap) & (1 << i)) > 0){
98                 bool val = (buttonMap & (1 << i)) > 0;
99                 switch (i){
100                 case 0:
101                     _a = val;
102                     emit (aChanged(val));
103                     break;
104                 case 1:
105                     _b = val;
106                     emit (bChanged(val));
107                     break;
108                 case 2:
109                     _c = val;
110                     emit (cChanged(val));
111                     break;
112                 case 3:
113                     _d = val;
114                     emit (dChanged(val));
115                     break;
116                 }
117             }
118         }
119
120         oldButtonMap = buttonMap;
121     }
122 }