Add properties for x and y. Why the hell is the cursor not being shown?
[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
32     Q_PROPERTY(int x READ x NOTIFY xChanged)
33     Q_PROPERTY(int y READ y NOTIFY yChanged)
34 public:
35     explicit BtConnector(QObject *parent = 0){
36         _x = 0;
37         _y = 0;
38     }
39     ~BtConnector(){
40         if(socket)
41             delete socket;
42     }
43
44     Q_INVOKABLE void connect(QString address, int port){
45         qDebug("Trying to connect to: %s--%d", address.toUtf8().constData(), port);
46
47         if(socket)
48             delete socket;
49
50         socket = new QBluetoothSocket(QBluetoothSocket::RfcommSocket);
51         QObject::connect(socket, SIGNAL(connected()), this, SIGNAL(connected()));
52         QObject::connect(socket, SIGNAL(disconnected()), this, SIGNAL(disconnected()));
53         QObject::connect(socket, SIGNAL(error(QBluetoothSocket::SocketError)), this, SIGNAL(error(QBluetoothSocket::SocketError)));
54
55         qDebug("Connecting...");
56         socket->connectToService(QBluetoothAddress(address), port);
57         qDebug("Connected.");
58
59         QObject::connect(socket, SIGNAL(readyRead()), this, SLOT(readData()));
60     }
61
62     int x(){return _x;}
63     int y(){return _y;}
64
65 public slots:
66     void disconnect(){
67         if(!socket)
68             return;
69
70         if(socket->isOpen())
71             socket->close();
72
73         delete socket;
74         socket = 0;
75     }
76
77 signals:
78     void connected();
79     void disconnected();
80     void error(QBluetoothSocket::SocketError errorCode);
81
82     void stickMoved(int x, int y);
83     void buttonsChanged(bool a, bool b, bool c, bool d);
84
85     void xChanged(int);
86     void yChanged(int);
87
88 private slots:
89     void readData(){
90 //        qDebug("readData...");
91         QByteArray data = socket->readAll();
92 //        qDebug("read %d bytes.", data.size());
93
94 /*
95         for(int i=0; i < data.size(); i++){
96             qDebug("%d: %d", i, ((signed char)data.at(i)));
97         }
98 */
99
100         /*
101          * Actually it seems like that the first three bytes are used for
102          * identifying the "type" of data sent. However, for now using the
103          * first seems to suffice.
104          */
105         if(data.at(0) == 5){
106             // Joystick movement
107             _x = (int)(signed char) data.at(4);
108             _y = (int)(signed char) data.at(5);
109
110             emit(xChanged(_x));
111             emit(yChanged(_y));
112             emit(stickMoved(_x, _y));
113         }else if(data.at(0) == 8){
114             // Button press
115             /*
116              * A -> 0, B -> 1, C -> 2, D ->3
117              * At index 3 to 6 (inclusive)
118              */
119
120             char buttonMap = 0;
121
122             for(int i = 3; i <= 6; i++){
123                 for(int b = 0; b <= 3; b++){
124                     if(data.at(i) == b){
125                         buttonMap ^= (1 << b);
126                     }
127                 }
128             }
129
130 //            qDebug("Button map: %d", buttonMap);
131             emit(buttonsChanged(buttonMap & 0x01, buttonMap & 0x02, buttonMap & 0x04, buttonMap & 0x08));
132         }
133     }
134
135 private:
136     QBluetoothSocket *socket;
137
138     int _x;
139     int _y;
140 };
141
142 #endif // BTCONNECTOR_H