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