From: Ruediger Gad Date: Thu, 12 Apr 2012 11:25:32 +0000 (+0200) Subject: Rework BtConnector: add properties for buttons. Partially move code to *.cpp file. X-Git-Url: https://vcs.maemo.org/git/?p=qzeecontrol;a=commitdiff_plain;h=8adb57186f09029533e23bce90fbbc5a58cdba1b Rework BtConnector: add properties for buttons. Partially move code to *.cpp file. --- diff --git a/QZeeControl.pro.user b/QZeeControl.pro.user index 61540ad..64a2f6c 100644 --- a/QZeeControl.pro.user +++ b/QZeeControl.pro.user @@ -1,6 +1,6 @@ - + ProjectExplorer.Project.ActiveTarget diff --git a/btconnector.cpp b/btconnector.cpp index 4ca1bd1..bfa75b8 100644 --- a/btconnector.cpp +++ b/btconnector.cpp @@ -19,3 +19,104 @@ #include "btconnector.h" +BtConnector::BtConnector(QObject *parent) + : QObject(parent){ + _a = false; + _b = false; + _c = false; + _d = false; + + _x = 0; + _y = 0; + oldButtonMap = 0; +} + +void BtConnector::connect(QString address, int port){ + qDebug("Trying to connect to: %s--%d", address.toUtf8().constData(), 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))); + + qDebug("Connecting..."); + socket->connectToService(QBluetoothAddress(address), port); + qDebug("Connected."); + + QObject::connect(socket, SIGNAL(readyRead()), this, SLOT(readData())); +} + +void BtConnector::readData(){ +// qDebug("readData..."); + QByteArray data = socket->readAll(); +// qDebug("read %d bytes.", data.size()); + +/* + 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)); + + for(int i = 0; i <= 3; i++){ + if(buttonMap | (oldButtonMap << i)){ + bool val = (buttonMap ^ (1 << i)); + switch (i){ + case 0: + _a = val; + emit (aChanged(val)); + break; + case 1: + _b = val; + emit (bChanged(val)); + break; + case 2: + _c = val; + emit (cChanged(val)); + break; + case 3: + _d = val; + emit (dChanged(val)); + break; + } + } + } + + oldButtonMap = buttonMap; + } +} diff --git a/btconnector.h b/btconnector.h index f5a3541..0c5683d 100644 --- a/btconnector.h +++ b/btconnector.h @@ -29,35 +29,27 @@ class BtConnector : public QObject { Q_OBJECT + 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); - - 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))); + Q_INVOKABLE void connect(QString address, int port); - qDebug("Connecting..."); - socket->connectToService(QBluetoothAddress(address), port); - qDebug("Connected."); - - 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;} @@ -82,61 +74,28 @@ signals: void stickMoved(int x, int y); void buttonsChanged(bool a, bool b, bool c, bool d); - void xChanged(int); - void yChanged(int); + void aChanged(bool val); + void bChanged(bool val); + void cChanged(bool val); + void dChanged(bool val); -private slots: - void readData(){ -// qDebug("readData..."); - QByteArray data = socket->readAll(); -// qDebug("read %d bytes.", data.size()); + void xChanged(int val); + void yChanged(int 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)); - } - } +private slots: + void readData(); private: QBluetoothSocket *socket; + bool _a; + bool _b; + bool _c; + bool _d; + int _x; int _y; + char oldButtonMap; }; #endif // BTCONNECTOR_H diff --git a/qml/QZeeControl/MainPage.qml b/qml/QZeeControl/MainPage.qml index b86c1b7..c03ab78 100644 --- a/qml/QZeeControl/MainPage.qml +++ b/qml/QZeeControl/MainPage.qml @@ -168,5 +168,10 @@ Page { labelC.color = c ? "red" : "blue" labelD.color = d ? "red" : "blue" } + + onAChanged: console.log("A changed to: " + val) + onBChanged: console.log("B changed to: " + val) + onCChanged: console.log("C changed to: " + val) + onDChanged: console.log("D changed to: " + val) } }