Refactor code quite heavily.
[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         if(!socket)
78             return;
79
80         if(socket->isOpen())
81             socket->close();
82
83         delete socket;
84         socket = 0;
85     }
86
87 signals:
88     void connected();
89     void disconnected();
90     void error(QBluetoothSocket::SocketError errorCode);
91
92     void stickMoved(int x, int y);
93     void buttonsChanged(bool a, bool b, bool c, bool d);
94
95     void thresholdChanged(int val);
96
97     void upChanged(bool val);
98     void downChanged(bool val);
99     void leftChanged(bool val);
100     void rightChanged(bool val);
101
102     void aChanged(bool val);
103     void bChanged(bool val);
104     void cChanged(bool val);
105     void dChanged(bool val);
106
107     void xChanged(int val);
108     void yChanged(int val);
109
110 private slots:
111     void readData();
112
113 private:
114     QBluetoothSocket *socket;
115
116     int _threshold;
117
118     bool _up;
119     bool _down;
120     bool _left;
121     bool _right;
122
123     bool _a;
124     bool _b;
125     bool _c;
126     bool _d;
127
128     int _x;
129     int _y;
130
131     char oldButtonMap;
132
133     void setA(bool val){
134         _a = val;
135         aChanged(_a);
136     }
137     void setB(bool val){
138         _b = val;
139         bChanged(_b);
140     }
141     void setC(bool val){
142         _c = val;
143         cChanged(_c);
144     }
145     void setD(bool val){
146         _d = val;
147         dChanged(_d);
148     }
149
150     void setX(int val){
151         _x = val;
152         xChanged(_x);
153     }
154     void setY(int val){
155         _y = val;
156         yChanged(_y);
157     }
158
159     void setUp(bool val){
160         _up = val;
161         upChanged(_up);
162     }
163     void setDown(bool val){
164         _down = val;
165         downChanged(_down);
166     }
167     void setLeft(bool val){
168         _left = val;
169         leftChanged(_left);
170     }
171     void setRight(bool val){
172         _right = val;
173         rightChanged(_right);
174     }
175
176 };
177
178 #endif // BTCONNECTOR_H