Add copyright and license info etc.
[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){qDebug("BtConnector Constructor");}
33     ~BtConnector(){if(socket != NULL) delete socket;}
34
35     Q_INVOKABLE void connect(QString address, int port){
36         qDebug("Trying to connect to: %s--%d", address.toUtf8().constData(), port);
37
38         if(socket != NULL)
39             delete socket;
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 public slots:
53     void disconnect(){
54         if(socket == NULL)
55             return;
56
57         if(socket->isOpen())
58             socket->close();
59
60         delete socket;
61     }
62
63 signals:
64     void connected();
65     void disconnected();
66     void error(QBluetoothSocket::SocketError errorCode);
67
68     void stickMoved(int x, int y);
69     void buttonsChanged(bool a, bool b, bool c, bool d);
70
71 private slots:
72     void readData(){
73 //        qDebug("readData...");
74         QByteArray data = socket->readAll();
75 //        qDebug("read %d bytes.", data.size());
76
77 /*
78         for(int i=0; i < data.size(); i++){
79             qDebug("%d: %d", i, ((signed char)data.at(i)));
80         }
81 */
82
83         /*
84          * Actually it seems like that the first three bytes are used for
85          * identifying the "type" of data sent. However, for now using the
86          * first seems to suffice.
87          */
88         if(data.at(0) == 5){
89             // Joystick movement
90             emit(stickMoved((int)(signed char) data.at(4), (int)(signed char) data.at(5)));
91         }else if(data.at(0) == 8){
92             // Button press
93             /*
94              * A -> 0, B -> 1, C -> 2, D ->3
95              * At index 3 to 6 (inclusive)
96              */
97
98             char buttonMap = 0;
99
100             for(int i = 3; i <= 6; i++){
101                 for(int b = 0; b <= 3; b++){
102                     if(data.at(i) == b){
103                         buttonMap ^= (1 << b);
104                     }
105                 }
106             }
107
108             qDebug("Button map: %d", buttonMap);
109             emit(buttonsChanged(buttonMap & 0x01, buttonMap & 0x02, buttonMap & 0x04, buttonMap & 0x08));
110         }
111     }
112
113 private:
114     QBluetoothSocket *socket;
115
116 };
117
118 #endif // BTCONNECTOR_H