Fix segfault. Remove debug output. Change UI Layout.w
[qzeecontrol] / btconnector.h
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 #ifndef BTCONNECTOR_H
21 #define BTCONNECTOR_H
22
23 #include <QObject>
24 #include <QtConnectivity/QBluetoothAddress>
25 #include <QtConnectivity/QBluetoothSocket>
26
27 QTM_USE_NAMESPACE
28 class BtConnector : public QObject
29 {
30     Q_OBJECT
31 public:
32     explicit BtConnector(QObject *parent = 0){
33
34     }
35     ~BtConnector(){
36         if(socket)
37             delete socket;
38     }
39
40     Q_INVOKABLE void connect(QString address, int port){
41         qDebug("Trying to connect to: %s--%d", address.toUtf8().constData(), port);
42
43         if(socket)
44             delete socket;
45
46         socket = new QBluetoothSocket(QBluetoothSocket::RfcommSocket);
47         QObject::connect(socket, SIGNAL(connected()), this, SIGNAL(connected()));
48         QObject::connect(socket, SIGNAL(disconnected()), this, SIGNAL(disconnected()));
49         QObject::connect(socket, SIGNAL(error(QBluetoothSocket::SocketError)), this, SIGNAL(error(QBluetoothSocket::SocketError)));
50
51         qDebug("Connecting...");
52         socket->connectToService(QBluetoothAddress(address), port);
53         qDebug("Connected.");
54
55         QObject::connect(socket, SIGNAL(readyRead()), this, SLOT(readData()));
56     }
57
58 public slots:
59     void disconnect(){
60         if(!socket)
61             return;
62
63         if(socket->isOpen())
64             socket->close();
65
66         delete socket;
67         socket = 0;
68     }
69
70 signals:
71     void connected();
72     void disconnected();
73     void error(QBluetoothSocket::SocketError errorCode);
74
75     void stickMoved(int x, int y);
76     void buttonsChanged(bool a, bool b, bool c, bool d);
77
78 private slots:
79     void readData(){
80 //        qDebug("readData...");
81         QByteArray data = socket->readAll();
82 //        qDebug("read %d bytes.", data.size());
83
84 /*
85         for(int i=0; i < data.size(); i++){
86             qDebug("%d: %d", i, ((signed char)data.at(i)));
87         }
88 */
89
90         /*
91          * Actually it seems like that the first three bytes are used for
92          * identifying the "type" of data sent. However, for now using the
93          * first seems to suffice.
94          */
95         if(data.at(0) == 5){
96             // Joystick movement
97             emit(stickMoved((int)(signed char) data.at(4), (int)(signed char) data.at(5)));
98         }else if(data.at(0) == 8){
99             // Button press
100             /*
101              * A -> 0, B -> 1, C -> 2, D ->3
102              * At index 3 to 6 (inclusive)
103              */
104
105             char buttonMap = 0;
106
107             for(int i = 3; i <= 6; i++){
108                 for(int b = 0; b <= 3; b++){
109                     if(data.at(i) == b){
110                         buttonMap ^= (1 << b);
111                     }
112                 }
113             }
114
115 //            qDebug("Button map: %d", buttonMap);
116             emit(buttonsChanged(buttonMap & 0x01, buttonMap & 0x02, buttonMap & 0x04, buttonMap & 0x08));
117         }
118     }
119
120 private:
121     QBluetoothSocket *socket;
122
123 };
124
125 #endif // BTCONNECTOR_H