{
Q_OBJECT
+ Q_PROPERTY(int threshold READ threshold NOTIFY thresholdChanged WRITE setThreshold)
+
+ Q_PROPERTY(bool up READ up NOTIFY upChanged)
+ Q_PROPERTY(bool down READ down NOTIFY downChanged)
+ Q_PROPERTY(bool left READ left NOTIFY leftChanged)
+ Q_PROPERTY(bool right READ right NOTIFY rightChanged)
+
+ Q_PROPERTY(bool a READ a NOTIFY aChanged)
+ Q_PROPERTY(bool b READ b NOTIFY bChanged)
+ Q_PROPERTY(bool c READ c NOTIFY cChanged)
+ Q_PROPERTY(bool d READ d NOTIFY dChanged)
+
Q_PROPERTY(int x READ x NOTIFY xChanged)
Q_PROPERTY(int y READ y NOTIFY yChanged)
public:
- explicit BtConnector(QObject *parent = 0){
- _x = 0;
- _y = 0;
- }
+ explicit BtConnector(QObject *parent = 0);
+
~BtConnector(){
if(socket)
delete socket;
}
- Q_INVOKABLE void connect(QString address, int port){
- qDebug("Trying to connect to: %s--%d", address.toUtf8().constData(), port);
+ Q_INVOKABLE void connect(QString address, int port);
- if(socket)
- delete socket;
-
- socket = new QBluetoothSocket(QBluetoothSocket::RfcommSocket);
- QObject::connect(socket, SIGNAL(connected()), this, SIGNAL(connected()));
- QObject::connect(socket, SIGNAL(disconnected()), this, SIGNAL(disconnected()));
- QObject::connect(socket, SIGNAL(error(QBluetoothSocket::SocketError)), this, SIGNAL(error(QBluetoothSocket::SocketError)));
+ int threshold(void){return _threshold;}
+ void setThreshold(int val){
+ _threshold = val;
+ thresholdChanged(_threshold);
+ }
- qDebug("Connecting...");
- socket->connectToService(QBluetoothAddress(address), port);
- qDebug("Connected.");
+ bool up(){return _up;}
+ bool down(){return _down;}
+ bool left(){return _left;}
+ bool right(){return _right;}
- QObject::connect(socket, SIGNAL(readyRead()), this, SLOT(readData()));
- }
+ bool a(){return _a;}
+ bool b(){return _b;}
+ bool c(){return _c;}
+ bool d(){return _d;}
int x(){return _x;}
int y(){return _y;}
public slots:
- void disconnect(){
- if(!socket)
- return;
-
- if(socket->isOpen())
- socket->close();
-
- delete socket;
- socket = 0;
- }
+ void disconnect();
signals:
void connected();
void stickMoved(int x, int y);
void buttonsChanged(bool a, bool b, bool c, bool d);
- void xChanged(int);
- void yChanged(int);
+ void thresholdChanged(int val);
-private slots:
- void readData(){
-// qDebug("readData...");
- QByteArray data = socket->readAll();
-// qDebug("read %d bytes.", data.size());
+ void upChanged(bool val);
+ void downChanged(bool val);
+ void leftChanged(bool val);
+ void rightChanged(bool val);
-/*
- for(int i=0; i < data.size(); i++){
- qDebug("%d: %d", i, ((signed char)data.at(i)));
- }
-*/
-
- /*
- * Actually it seems like that the first three bytes are used for
- * identifying the "type" of data sent. However, for now using the
- * first seems to suffice.
- */
- if(data.at(0) == 5){
- // Joystick movement
- _x = (int)(signed char) data.at(4);
- _y = (int)(signed char) data.at(5);
-
- emit(xChanged(_x));
- emit(yChanged(_y));
- emit(stickMoved(_x, _y));
- }else if(data.at(0) == 8){
- // Button press
- /*
- * A -> 0, B -> 1, C -> 2, D ->3
- * At index 3 to 6 (inclusive)
- */
-
- char buttonMap = 0;
-
- for(int i = 3; i <= 6; i++){
- for(int b = 0; b <= 3; b++){
- if(data.at(i) == b){
- buttonMap ^= (1 << b);
- }
- }
- }
-
-// qDebug("Button map: %d", buttonMap);
- emit(buttonsChanged(buttonMap & 0x01, buttonMap & 0x02, buttonMap & 0x04, buttonMap & 0x08));
- }
- }
+ void aChanged(bool val);
+ void bChanged(bool val);
+ void cChanged(bool val);
+ void dChanged(bool val);
+
+ void xChanged(int val);
+ void yChanged(int val);
+
+private slots:
+ void readData();
private:
QBluetoothSocket *socket;
+ int _threshold;
+
+ bool _up;
+ bool _down;
+ bool _left;
+ bool _right;
+
+ bool _a;
+ bool _b;
+ bool _c;
+ bool _d;
+
int _x;
int _y;
+
+ char oldButtonMap;
+
+ void setA(bool val){
+ _a = val;
+ aChanged(_a);
+ }
+ void setB(bool val){
+ _b = val;
+ bChanged(_b);
+ }
+ void setC(bool val){
+ _c = val;
+ cChanged(_c);
+ }
+ void setD(bool val){
+ _d = val;
+ dChanged(_d);
+ }
+
+ void setX(int val){
+ _x = val;
+ xChanged(_x);
+ }
+ void setY(int val){
+ _y = val;
+ yChanged(_y);
+ }
+
+ void setUp(bool val){
+ _up = val;
+ upChanged(_up);
+ }
+ void setDown(bool val){
+ _down = val;
+ downChanged(_down);
+ }
+ void setLeft(bool val){
+ _left = val;
+ leftChanged(_left);
+ }
+ void setRight(bool val){
+ _right = val;
+ rightChanged(_right);
+ }
+
};
#endif // BTCONNECTOR_H