X-Git-Url: http://git.maemo.org/git/?p=qzeecontrol;a=blobdiff_plain;f=btconnector.h;h=cbbf00217f30bf182ff5dadc20221c184342c56f;hp=3b78c74bab176a02bc76642f87907f79c2de36a9;hb=HEAD;hpb=c39bc8a55b9ab23a03101a9d29c511e8fab2cc22 diff --git a/btconnector.h b/btconnector.h index 3b78c74..cbbf002 100644 --- a/btconnector.h +++ b/btconnector.h @@ -28,37 +28,52 @@ QTM_USE_NAMESPACE class BtConnector : public QObject { Q_OBJECT -public: - explicit BtConnector(QObject *parent = 0){qDebug("BtConnector Constructor");} - ~BtConnector(){if(socket != NULL) delete socket;} - Q_INVOKABLE void connect(QString address, int port){ - qDebug("Trying to connect to: %s--%d", address.toUtf8().constData(), port); + 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); - if(socket != NULL) + ~BtConnector(){ + 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."); + Q_INVOKABLE void connect(QString address, int port); - QObject::connect(socket, SIGNAL(readyRead()), this, SLOT(readData())); + int threshold(void){return _threshold;} + void setThreshold(int val){ + _threshold = val; + thresholdChanged(_threshold); } -public slots: - void disconnect(){ - if(socket == NULL) - return; + bool up(){return _up;} + bool down(){return _down;} + bool left(){return _left;} + bool right(){return _right;} - if(socket->isOpen()) - socket->close(); + bool a(){return _a;} + bool b(){return _b;} + bool c(){return _c;} + bool d(){return _d;} - delete socket; - } + int x(){return _x;} + int y(){return _y;} + +public slots: + void disconnect(); signals: void connected(); @@ -68,51 +83,87 @@ signals: void stickMoved(int x, int y); void buttonsChanged(bool a, bool b, bool c, bool d); -private slots: - void readData(){ -// qDebug("readData..."); - QByteArray data = socket->readAll(); -// qDebug("read %d bytes.", data.size()); + void thresholdChanged(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 - emit(stickMoved((int)(signed char) data.at(4), (int)(signed char) data.at(5))); - }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 upChanged(bool val); + void downChanged(bool val); + void leftChanged(bool val); + void rightChanged(bool val); + + 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