Sometimes its the little things..
[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 threshold READ threshold NOTIFY thresholdChanged WRITE setThreshold)
33
34     Q_PROPERTY(bool up READ up NOTIFY upChanged)
35     Q_PROPERTY(bool down READ down NOTIFY downChanged)
36     Q_PROPERTY(bool left READ left NOTIFY leftChanged)
37     Q_PROPERTY(bool right READ right NOTIFY rightChanged)
38
39     Q_PROPERTY(bool a READ a NOTIFY aChanged)
40     Q_PROPERTY(bool b READ b NOTIFY bChanged)
41     Q_PROPERTY(bool c READ c NOTIFY cChanged)
42     Q_PROPERTY(bool d READ d NOTIFY dChanged)
43
44     Q_PROPERTY(int x READ x NOTIFY xChanged)
45     Q_PROPERTY(int y READ y NOTIFY yChanged)
46 public:
47     explicit BtConnector(QObject *parent = 0);
48
49     ~BtConnector(){
50         if(socket)
51             delete socket;
52     }
53
54     Q_INVOKABLE void connect(QString address, int port);
55
56     int threshold(void){return _threshold;}
57     void setThreshold(int val){
58         _threshold = val;
59         thresholdChanged(_threshold);
60     }
61
62     bool up(){return _up;}
63     bool down(){return _down;}
64     bool left(){return _left;}
65     bool right(){return _right;}
66
67     bool a(){return _a;}
68     bool b(){return _b;}
69     bool c(){return _c;}
70     bool d(){return _d;}
71
72     int x(){return _x;}
73     int y(){return _y;}
74
75 public slots:
76     void disconnect();
77
78 signals:
79     void connected();
80     void disconnected();
81     void error(QBluetoothSocket::SocketError errorCode);
82
83     void stickMoved(int x, int y);
84     void buttonsChanged(bool a, bool b, bool c, bool d);
85
86     void thresholdChanged(int val);
87
88     void upChanged(bool val);
89     void downChanged(bool val);
90     void leftChanged(bool val);
91     void rightChanged(bool val);
92
93     void aChanged(bool val);
94     void bChanged(bool val);
95     void cChanged(bool val);
96     void dChanged(bool val);
97
98     void xChanged(int val);
99     void yChanged(int val);
100
101 private slots:
102     void readData();
103
104 private:
105     QBluetoothSocket *socket;
106
107     int _threshold;
108
109     bool _up;
110     bool _down;
111     bool _left;
112     bool _right;
113
114     bool _a;
115     bool _b;
116     bool _c;
117     bool _d;
118
119     int _x;
120     int _y;
121
122     char oldButtonMap;
123
124     void setA(bool val){
125         _a = val;
126         aChanged(_a);
127     }
128     void setB(bool val){
129         _b = val;
130         bChanged(_b);
131     }
132     void setC(bool val){
133         _c = val;
134         cChanged(_c);
135     }
136     void setD(bool val){
137         _d = val;
138         dChanged(_d);
139     }
140
141     void setX(int val){
142         _x = val;
143         xChanged(_x);
144     }
145     void setY(int val){
146         _y = val;
147         yChanged(_y);
148     }
149
150     void setUp(bool val){
151         _up = val;
152         upChanged(_up);
153     }
154     void setDown(bool val){
155         _down = val;
156         downChanged(_down);
157     }
158     void setLeft(bool val){
159         _left = val;
160         leftChanged(_left);
161     }
162     void setRight(bool val){
163         _right = val;
164         rightChanged(_right);
165     }
166
167 };
168
169 #endif // BTCONNECTOR_H