Screen tanslation + discovery + home made serial lib
authorMjolnir <thomas.pietrzak@inria.fr>
Mon, 24 Aug 2015 13:44:42 +0000 (15:44 +0200)
committerMjolnir <thomas.pietrzak@inria.fr>
Mon, 24 Aug 2015 13:44:42 +0000 (15:44 +0200)
17 files changed:
ArduinoScreen/ArduinoScreen.ino
Library/LivingDesktopLibrary.pro
Library/LivingDevice.cpp [new file with mode: 0644]
Library/LivingDevice.h [new file with mode: 0644]
Library/LivingKeyboard.cpp
Library/LivingKeyboard.h
Library/LivingScreen.cpp [new file with mode: 0644]
Library/LivingScreen.h [new file with mode: 0644]
Library/Serial.cpp [new file with mode: 0644]
Library/Serial.h [new file with mode: 0644]
Library/SerialLinux.cpp [new file with mode: 0644]
Library/SerialLinux.h [new file with mode: 0644]
Library/XYPlotter.cpp
Library/XYPlotter.h
Test/mainwindow.cpp
Test/mainwindow.h
Test/mainwindow.ui

index 15901a63a3466f3e0cfab248b08c487e2b4048ac..d3772f05687126d79a99ffe3fe43702d605696d1 100644 (file)
@@ -54,14 +54,14 @@ void eval(int command, int value)
 
 void setup()
 {
+  //Communication
+  Serial.begin(9600);
+
   //Motors
   init_motors();
   
   //Calibration
   calibrate();
-
-  //Communication
-  Serial.begin(9600);
 }
 
 void loop()
index 07fb8e63d997ad97cc1a5818aaa95552f52b2f3a..dc4df607b97641133c107cb21c86e875ac79026b 100644 (file)
@@ -4,7 +4,7 @@
 #
 #-------------------------------------------------
 
-QT      += core gui serialport
+QT      += core gui
 
 TARGET = LivingDesktop
 TEMPLATE = lib
@@ -14,13 +14,21 @@ DEFINES += LIVINGDESKTOPLIBRARY_LIBRARY
 SOURCES +=\
         ErrorMessage.cpp \
         XYPlotter.cpp \
-    LivingKeyboard.cpp
+    LivingKeyboard.cpp \
+    LivingScreen.cpp \
+    LivingDevice.cpp \
+    Serial.cpp \
+    SerialLinux.cpp
 
 HEADERS +=\
         livingdesktoplibrary_global.h\
         ErrorMessage.h \
         XYPlotter.h \
-    LivingKeyboard.h
+    LivingKeyboard.h \
+    LivingScreen.h \
+    LivingDevice.h \
+    Serial.h \
+    SerialLinux.h
 
 unix {
     target.path = /usr/lib
diff --git a/Library/LivingDevice.cpp b/Library/LivingDevice.cpp
new file mode 100644 (file)
index 0000000..c136796
--- /dev/null
@@ -0,0 +1,110 @@
+#include "LivingDevice.h"
+
+#include <QDebug>
+#include <QThread>
+#include <Serial.h>
+#include <SerialLinux.h>
+
+//#define QIODEVICE_DEBUG
+
+char const *LivingDevice::serialports[] = {"/dev/ttyUSB0", "/dev/ttyUSB1", "/dev/ttyUSB2", "/dev/ttyUSB3", "/dev/ttyUSB4"};
+
+LivingDevice::LivingDevice(QString name, int timeout)
+: serialPort(NULL)
+{
+    discover(name, timeout);
+}
+
+LivingDevice::~LivingDevice()
+{
+    closeSerialPort();
+}
+
+void LivingDevice::discover(QString name, int timeout)
+{
+    qDebug() << "Discovering" << name;
+    for (int i = 0 ; i < 5 && !serialPort ; i++)
+    {
+        try{
+                serialPort = new SerialLinux(serialports[i], 9600);
+
+                qDebug() << "Trying port" << serialports[i];
+
+                //ask fo his name
+                sendCommand('I');
+                QThread::msleep(500);
+                QString answer = readAnswer();
+
+                if (answer == name)
+                    qDebug() << "Found LivingKeyboard on port" << serialports[i];
+                else
+                {
+                    qDebug() << "Not" << name;
+                    closeSerialPort();
+                }
+            }
+            catch(ErrorMessage m)
+            {
+                qDebug() << "Port " << serialports[i] << "not responding";
+                closeSerialPort();
+            }
+            catch(char *str)
+            {
+                qDebug() << "Cannot connect to port" << serialports[i];
+                closeSerialPort();
+            }
+    }
+    if (! serialPort)
+        throw(ErrorMessage("Cannot find " + name));
+}
+
+void LivingDevice::closeSerialPort()
+{
+    if (serialPort)
+        delete serialPort;
+    serialPort = NULL;
+}
+
+void LivingDevice::sendCommand(char command)
+{
+    if (serialPort)
+    {
+        char buffer[] = {command, '\n'};
+        serialPort->WriteData(buffer, 2);
+    }
+}
+
+void LivingDevice::sendCommand(char command, int value)
+{
+    if (serialPort)
+    {
+        QString buffer = command + QString::number(value) + "\n";
+        serialPort->WriteData(buffer.toStdString().data(), strlen(buffer.toStdString().data()));
+    }
+}
+
+void LivingDevice::sendCommand(char *command, int *value, int nb)
+{
+    QString buffer;
+    if (serialPort)
+    {
+        for (int i = 0 ; i < nb ; i++)
+            buffer += command[i] + QString::number(value[i]);
+        buffer += '\n';
+        serialPort->WriteData(buffer.toStdString().data(), strlen(buffer.toStdString().data()));
+    }
+}
+
+QString LivingDevice::readAnswer()
+{
+    char buffer[128];
+
+    if (serialPort)
+    {
+        int nb = serialPort->ReadData(buffer, 127);
+        buffer[nb] = '\0';
+        qDebug() << "Read" << buffer;
+        return QString(buffer);
+    }
+    return "";
+}
diff --git a/Library/LivingDevice.h b/Library/LivingDevice.h
new file mode 100644 (file)
index 0000000..c7d0ace
--- /dev/null
@@ -0,0 +1,30 @@
+#ifndef LIVINGDEVICE_H
+#define LIVINGDEVICE_H
+
+#include "livingdesktoplibrary_global.h"
+#include <Serial.h>
+#include <ErrorMessage.h>
+
+class LIVINGDESKTOPLIBRARYSHARED_EXPORT LivingDevice
+{
+    public:
+        LivingDevice(QString name, int timeout=5000);
+        virtual ~LivingDevice();
+
+        void discover(QString name, int timeout=5000);
+
+        void closeSerialPort();
+
+        void sendCommand(char command);
+        void sendCommand(char command, int value);
+        void sendCommand(char *command, int *value, int nb);
+
+        QString readAnswer();
+
+    protected:
+        Serial *serialPort;
+
+        static char const *serialports[];
+};
+
+#endif // LIVINGDEVICE_H
index 07a10cb66d598806cd218a66f8f74b7023dae927..cb733b3ceef138684829cc0a6d4a148483d36f81 100644 (file)
 #include "LivingKeyboard.h"
 
 #include <QDebug>
-#include <QThread>
-
-#define QIODEVICE_DEBUG
-
-char const *LivingKeyboard::serialports[] = {"ttyUSB0", "ttyUSB1", "ttyUSB2", "ttyUSB3", "ttyUSB4"};
 
 LivingKeyboard::LivingKeyboard()
-: serialPort(NULL)
+: LivingDevice("LivingKeyboard", 500)
 {
-    //Port discovery
-    for (int i = 0 ; i < 5 && !serialPort ; i++)
-    {
-        serialPort = new QSerialPort(serialports[i]);
-        if(serialPort->open(QIODevice::ReadWrite))
-        {
-            serialPort->setBaudRate(QSerialPort::Baud9600, QSerialPort::AllDirections);
-            serialPort->setDataBits(QSerialPort::Data8);
-            serialPort->setParity(QSerialPort::NoParity);
-            serialPort->setStopBits(QSerialPort::OneStop);
-            serialPort->setFlowControl(QSerialPort::NoFlowControl);
-            serialPort->setReadBufferSize(32);
-
-            qDebug() << "Trying port " << serialports[i];
-
-            //ask fo his name
-            serialPort->write("I\n", 2);
-            serialPort->flush();
-            //wait a little so that we get the whole string at once
-            QThread::msleep(500);
-            char buffer[32];
-            //memset(buffer,0, 32*sizeof(char));
-            if (serialPort->waitForReadyRead(10000))
-            {
-                int nb = serialPort->read(buffer, sizeof(buffer) - 1);
-                buffer[nb] = 0;
-                qDebug() << "Received " << nb << " bytes : '" << buffer << "'";
-                if (nb <= 0)
-                {
-                    qDebug() << "Communcation problem with port " << serialports[i];
-                    serialPort->close();
-                    serialPort = NULL;
-                }
-                else if (strcmp(buffer, "LivingKeyboard") == 0)
-                    qDebug() << "Found LivingKeyboard on port " << serialports[i];
-                else
-                {
-                    qDebug() << "Not LivingKeyboard";
-                    serialPort->close();
-                    serialPort = NULL;
-                }
-            }
-            else
-            {
-                qDebug() << "Port " << serialports[i] << "not responding";
-                serialPort->close();
-                serialPort = NULL;
-            }
-        }
-        else
-        {
-            serialPort->close();
-            serialPort = NULL;
-        }
-    }
-    if (! serialPort)
-        throw(ErrorMessage("Cannot find Keyboard"));
 }
 
 LivingKeyboard::~LivingKeyboard()
 {
-    if (serialPort)
-    {
-        serialPort->close();
-        delete serialPort;
-    }
 }
 
 void LivingKeyboard::rotation(LivingKeyboard::tilt t, unsigned int angle)
 {
-    if (serialPort)
-    {
-        int a = angle;
-        if (t == LEFT)
-            a *= -1;
-        QString buffer = "R" + QString::number(a) + "\n";
-        serialPort->write(buffer.toStdString().data(), strlen(buffer.toStdString().data()));
-    }
+    int a = angle;
+    if (t == LEFT)
+        a *= -1;
+    sendCommand('R', a);
 }
 
 void LivingKeyboard::translation(LivingKeyboard::direction d, unsigned int distance)
 {
-    if (serialPort)
-    {
-        int dis = distance;
-        if (d == BACKWARD)
-            dis *= -1;
-        QString buffer = "T" + QString::number(dis) + "\n";
-        serialPort->write(buffer.toStdString().data(), strlen(buffer.toStdString().data()));
-    }
+    int dis = distance;
+    if (d == BACKWARD)
+        dis *= -1;
+    sendCommand('T', dis);
 }
index 0fbe8d1b56c742f5e1b0dded421ba6680a98b000..7d7ff8ba628e2bc40d48b2ce716e0d5a09567cc4 100644 (file)
@@ -1,11 +1,9 @@
 #ifndef LIVINGKEYBOARD_H
 #define LIVINGKEYBOARD_H
 
-#include "livingdesktoplibrary_global.h"
-#include <QSerialPort>
-#include <ErrorMessage.h>
+#include <LivingDevice.h>
 
-class LivingKeyboard
+class LIVINGDESKTOPLIBRARYSHARED_EXPORT LivingKeyboard : public LivingDevice
 {
     public:
         LivingKeyboard();
@@ -17,11 +15,6 @@ class LivingKeyboard
 
         void rotation(tilt t, unsigned int angle);
         void translation(direction d, unsigned int distance);
-
-    private:
-        QSerialPort* serialPort;
-
-        static char const *serialports[];
 };
 
 #endif // LIVINGKEYBOARD_H
diff --git a/Library/LivingScreen.cpp b/Library/LivingScreen.cpp
new file mode 100644 (file)
index 0000000..1042bfa
--- /dev/null
@@ -0,0 +1,28 @@
+#include "LivingScreen.h"
+
+#include <QDebug>
+
+LivingScreen::LivingScreen()
+: LivingDevice("LivingScreen", 30000)
+{
+}
+
+LivingScreen::~LivingScreen()
+{
+}
+
+void LivingScreen::rotation(LivingScreen::direction t, unsigned int angle)
+{
+    int a = angle;
+    if (t == LEFT)
+        a *= -1;
+    sendCommand('R', a);
+}
+
+void LivingScreen::translation(LivingScreen::direction d, unsigned int distance)
+{
+    int dis = distance;
+    if (d == LEFT)
+        dis *= -1;
+    sendCommand('T', dis);
+}
diff --git a/Library/LivingScreen.h b/Library/LivingScreen.h
new file mode 100644 (file)
index 0000000..55def11
--- /dev/null
@@ -0,0 +1,19 @@
+#ifndef LIVINGSCREEN_H
+#define LIVINGSCREEN_H
+
+#include <LivingDevice.h>
+
+class LIVINGDESKTOPLIBRARYSHARED_EXPORT LivingScreen : public LivingDevice
+{
+    public:
+        LivingScreen();
+
+        ~LivingScreen();
+
+        enum direction{LEFT, RIGHT};
+
+        void rotation(direction t, unsigned int angle);
+        void translation(direction d, unsigned int distance);
+};
+
+#endif // LIVINGSCREEN_H
diff --git a/Library/Serial.cpp b/Library/Serial.cpp
new file mode 100644 (file)
index 0000000..7ef3177
--- /dev/null
@@ -0,0 +1,10 @@
+#include "Serial.h"
+
+Serial::Serial(const char *, int /*baudrate*/)
+:_connected(false), _arduinowaittime(1)
+{
+}
+
+Serial::~Serial()
+{
+}
diff --git a/Library/Serial.h b/Library/Serial.h
new file mode 100644 (file)
index 0000000..a3043c3
--- /dev/null
@@ -0,0 +1,36 @@
+#ifndef _SERIAL_
+#define _SERIAL_
+
+class Serial
+{
+    public:
+        //Initialize Serial communication with the given COM port
+        //baudrate among 300 1200 2400 4800 9600 14400 19200 38400 57600 115200
+        Serial(const char *portName, int baudrate = 57600);
+
+        //Close the connection
+        //NOTA: for some reason you can't connect again before exiting
+        //the program and running it again
+        virtual ~Serial();
+
+        //Read data in a buffer, if nbChar is greater than the
+        //maximum number of bytes available, it will return only the
+        //bytes available. The function return -1 when nothing could
+        //be read, the number of bytes actually read.
+        virtual int ReadData(void *buffer, unsigned int nbChar) const=0;
+
+        //Writes data from a buffer through the Serial connection
+        //return true on success.
+        virtual bool WriteData(const void *buffer, unsigned int nbChar) const=0;
+
+        //Check if we are actually connected
+        bool IsConnected() { return _connected; }
+
+    protected:
+        //Connection status
+        bool _connected;
+
+        int _arduinowaittime;
+};
+
+#endif // SERIAL_H
diff --git a/Library/SerialLinux.cpp b/Library/SerialLinux.cpp
new file mode 100644 (file)
index 0000000..2ce9a18
--- /dev/null
@@ -0,0 +1,154 @@
+#include "SerialLinux.h"
+
+#include <termios.h>
+#include <errno.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <string.h>
+#include <stdio.h>
+
+#include <iostream>
+using namespace std;
+
+SerialLinux::SerialLinux(const char *portName, int baudrate)
+:Serial(portName)
+{
+    _arduinowaittime = 1;
+    //Try to connect to the given port throuh CreateFile
+    _hSerial = open(portName, O_RDWR | O_NOCTTY);// | O_NDELAY);
+
+    //Check if the connection was successfull
+    if(_hSerial < 0)
+    {
+        char buffer[256];
+        sprintf(buffer, "Open error on port %s %d %s\n", portName, errno, strerror(errno));
+        throw buffer;
+    }
+
+    struct termios serialParams;
+
+    //Try to get the current
+    if (tcgetattr(_hSerial, &serialParams) < 0)
+        throw "Cannot get COM port properties";
+
+    tcflush(_hSerial, TCIFLUSH);
+
+    serialParams.c_cflag = CS8 | CREAD;
+
+    //Define serial connection parameters for the arduino board
+    switch(baudrate)
+    {
+        case 300:
+            cfsetispeed(&serialParams, B300);
+            cfsetispeed(&serialParams, B300);
+            serialParams.c_cflag |= B300;
+            break;
+        case 1200:
+            cfsetispeed(&serialParams, B1200);
+            cfsetispeed(&serialParams, B1200);
+            serialParams.c_cflag |= B1200;
+            break;
+        case 2400:
+            cfsetispeed(&serialParams, B2400);
+            cfsetispeed(&serialParams, B2400);
+            serialParams.c_cflag |= B2400;
+            break;
+        case 4800:
+            cfsetispeed(&serialParams, B4800);
+            cfsetispeed(&serialParams, B4800);
+            serialParams.c_cflag |= B4800;
+            break;
+        case 9600:
+            cfsetispeed(&serialParams, B9600);
+            cfsetispeed(&serialParams, B9600);
+            serialParams.c_cflag |= B9600;
+            break;
+/*        case 14400:
+            cfsetispeed(&serialParams, B14400);
+            cfsetispeed(&serialParams, B14400);
+            serialParams.c_cflag |= B14400;
+            break;*/
+        case 19200:
+            cfsetispeed(&serialParams, B19200);
+            cfsetispeed(&serialParams, B19200);
+            serialParams.c_cflag |= B19200;
+            break;
+        case 38400:
+            cfsetispeed(&serialParams, B38400);
+            cfsetispeed(&serialParams, B38400);
+            serialParams.c_cflag |= B38400;
+            break;
+        case 57600:
+            cfsetispeed(&serialParams, B57600);
+            cfsetispeed(&serialParams, B57600);
+            serialParams.c_cflag |= B57600;
+            break;
+        case 115200:
+            cfsetispeed(&serialParams, B115200);
+            cfsetispeed(&serialParams, B115200);
+            serialParams.c_cflag |= B115200;
+            break;
+        default:
+            cfsetispeed(&serialParams, B57600);
+            cfsetispeed(&serialParams, B57600);
+            serialParams.c_cflag |= B57600;
+            break;
+    }
+    //No parity
+    serialParams.c_cflag &= ~PARENB;
+    //One stop bit
+    serialParams.c_cflag &= ~CSTOPB;
+
+    if (tcsetattr(_hSerial, TCSANOW, &serialParams) < 0)
+        throw "ERROR: Could not set Serial Port parameters";
+    else
+    {
+        //If everything went fine we're connected
+        _connected = true;
+        //We wait 2s as the arduino board will be reseting
+        sleep(_arduinowaittime);
+        //SDL_Delay(ARDUINO_WAIT_TIME);
+    }
+}
+
+SerialLinux::~SerialLinux()
+{
+    //Check if we are connected before trying to disconnect
+    if(_connected)
+    {
+        //We're no longer connected
+        _connected = false;
+        //Close the serial handler
+        close(_hSerial);
+    }
+}
+
+int SerialLinux::ReadData(void *buffer, unsigned int nbChar) const
+{
+    //Number of bytes we'll really ask to read
+    int r = 0;
+
+    if ((r = read(_hSerial, buffer, size_t(nbChar))) < 0)
+    {
+        if (errno == EAGAIN)
+            return 0;
+        else
+            cerr << "read error " << errno << " " << strerror(errno) << endl;
+    }
+
+    return r;
+}
+
+
+bool SerialLinux::WriteData(const void *buffer, unsigned int nbChar) const
+{
+    //Try to write the buffer on the Serial port
+    if(write(_hSerial, buffer, nbChar) < 0)
+    {
+        cerr << "write error " << errno << " " << strerror(errno) << endl;
+        return false;
+    }
+    return true;
+}
diff --git a/Library/SerialLinux.h b/Library/SerialLinux.h
new file mode 100644 (file)
index 0000000..c034584
--- /dev/null
@@ -0,0 +1,34 @@
+#ifndef _SERIALMAC_
+#define _SERIALMAC_
+
+#include "Serial.h"
+
+class SerialLinux : public Serial
+{
+    public:
+        //Initialize Serial communication with the given COM port
+        SerialLinux(const char *portName, int baudrate = 57600);
+        //Close the connection
+        //NOTA: for some reason you can't connect again before exiting
+        //the program and running it again
+        ~SerialLinux();
+        //Read data in a buffer, if nbChar is greater than the
+        //maximum number of bytes available, it will return only the
+        //bytes available. The function return -1 when nothing could
+        //be read, the number of bytes actually read.
+        int ReadData(void *buffer, unsigned int nbChar) const;
+        //Writes data from a buffer through the Serial connection
+        //return true on success.
+        bool WriteData(const void *buffer, unsigned int nbChar) const;
+
+
+    private:
+        //Serial comm handler
+        int _hSerial;
+/*        //Get various information about the connection
+        COMSTAT _status;
+        //Keep track of last error
+        DWORD _errors;*/
+};
+
+#endif // SERIALMAC_H
index ed760553163a4f8e5c9f2e958af4e4ff04d5f1ce..f0c90e9884fe8903ef4da235962bb55a412f50bc 100644 (file)
@@ -1,94 +1,21 @@
 #include "XYPlotter.h"
 
-#include <QString>
 #include <QDebug>
-#include <QThread>
 
-char const *XYPlotter::serialports[] = {"ttyUSB0", "ttyUSB1", "ttyUSB2", "ttyUSB3", "ttyUSB4"};
 const int XYPlotter::toolDistance = 70;
 
 XYPlotter::XYPlotter()
-: serialPort(NULL)
+: LivingDevice("XYPlotter", 13000)
 {
-    //Port discovery
-    for (int i = 0 ; i < 5 && !serialPort ; i++)
-    {
-        serialPort = new QSerialPort(serialports[i]);
-        if(serialPort->open(QIODevice::ReadWrite))
-        {
-            serialPort->setBaudRate(QSerialPort::Baud9600, QSerialPort::AllDirections);
-            serialPort->setDataBits(QSerialPort::Data8);
-            serialPort->setParity(QSerialPort::NoParity);
-            serialPort->setStopBits(QSerialPort::OneStop);
-            serialPort->setFlowControl(QSerialPort::NoFlowControl);
-            serialPort->setReadBufferSize(32);
-
-            qDebug() << "Trying port " << serialports[i];
-
-            //wait the plotter to finish the calibration
-            QThread::sleep(13);
-            //ask fo his name
-            serialPort->write("I\n", 2);
-            serialPort->flush();
-            char buffer[32];
-            qDebug() << "Waiting for data";
-            if (serialPort->waitForReadyRead(1500))
-            {
-                int nb = serialPort->read(buffer, sizeof(buffer) - 1);
-                buffer[nb] = 0;
-                qDebug() << "Received " << nb << " bytes : '" << buffer << "'";
-                if (nb <= 0)
-                {
-                    qDebug() << "Port " << serialports[i] << "not responding";
-                    serialPort->close();
-                    serialPort = NULL;
-                }
-                else if (strcmp(buffer, "XYPlotter") == 0)
-                    qDebug() << "Found XYPlotter on port " << serialports[i];
-                else
-                {
-                    qDebug() << "Not XYPlotter";
-                    serialPort->close();
-                    serialPort = NULL;
-                }
-            }
-            else
-            {
-                qDebug() << "Port " << serialports[i] << "not responding";
-                serialPort->close();
-                serialPort = NULL;
-            }
-        }
-        else
-        {
-            serialPort->close();
-            serialPort = NULL;
-        }
-    }
-    if (! serialPort)
-        throw(ErrorMessage("Cannot find XYPlotter"));
 }
 
 XYPlotter::~XYPlotter()
 {
-    if (serialPort)
-    {
-        serialPort->close();
-        delete serialPort;
-    }
 }
 
-void XYPlotter::moveTo(unsigned int x, unsigned int y, bool toolup=true)
+void XYPlotter::moveTo(int x, int y, bool toolup=true)
 {
-    if (serialPort)
-    {
-        QString buffer = "G00X" + QString::number(x) +
-                "Y" + QString::number(y) +
-                "Z" + QString::number(toolup * toolDistance) + "\n";
-                //"F50000\n";
-        serialPort->write(buffer.toStdString().data(), strlen(buffer.toStdString().data()));
-        posX = x;
-        posY = y;
-        toolUp = toolup;
-    }
+    char commands[] = {'G', 'X', 'Y', 'Z'};
+    int values[] = {0, x, y, toolup * toolDistance};
+    sendCommand(commands, values, 4);
 }
index 69319e3a4486730c996223073bb33abb2ae999db..af8f331d5f3230ce841360b4800e2638041ad6c5 100644 (file)
@@ -1,26 +1,21 @@
 #ifndef XYPLOTTER_H
 #define XYPLOTTER_H
 
-#include "livingdesktoplibrary_global.h"
-#include <QSerialPort>
-#include <ErrorMessage.h>
+#include <LivingDevice.h>
 
-class LIVINGDESKTOPLIBRARYSHARED_EXPORT XYPlotter
+class LIVINGDESKTOPLIBRARYSHARED_EXPORT XYPlotter : public LivingDevice
 {
     public:
         XYPlotter();
         ~XYPlotter();
 
-        void moveTo(unsigned int x, unsigned int y, bool toolup);
+        void moveTo(int x, int y, bool toolup);
 
     private:
-        QSerialPort* serialPort;
-
         unsigned int posX, posY;
         bool toolUp;
 
         static const int toolDistance;
-        static char const *serialports[];
 };
 
 #endif // XYPLOTTER_H
index 49682b11232d98df61e8627643d35a9c634277f7..b7d653c0336bcf3e345b5d27cd492162fdc35543 100644 (file)
@@ -11,15 +11,31 @@ MainWindow::MainWindow(QWidget *parent)
     try
     {
         keyboard = new LivingKeyboard();
-        connect(ui->keyboardbutton, SIGNAL(clicked()), this, SLOT(translateKeyboard()));
-        connect(ui->rotation, SIGNAL(valueChanged(int)), this, SLOT(rotateKeyboard(int)));
-        ui->keyboardbutton->setEnabled(true);
+        connect(ui->keyboardmovebutton, SIGNAL(clicked()), this, SLOT(translateKeyboard()));
+        connect(ui->keyboardrotatebutton, SIGNAL(clicked()), this, SLOT(rotateKeyboard()));
+        ui->keyboardmovebutton->setEnabled(true);
+        ui->keyboardrotatebutton->setEnabled(true);
     }
     catch (ErrorMessage e)
     {
         keyboard = NULL;
         qDebug() << QString(e.what());
     }
+    qDebug("Connect screen");
+    try
+    {
+        //The screen may require a little time because of calibration (blocking process)
+        screen = new LivingScreen();
+        connect(ui->screenmovebutton, SIGNAL(clicked()), this, SLOT(translateScreen()));
+        connect(ui->screenrotatebutton, SIGNAL(clicked()), this, SLOT(rotateScreen()));
+        ui->screenmovebutton->setEnabled(true);
+        ui->screenrotatebutton->setEnabled(true);
+    }
+    catch (ErrorMessage e)
+    {
+        mouse = NULL;
+        qDebug() << QString(e.what());
+    }
     qDebug("Connect mouse");
     try
     {
@@ -49,8 +65,8 @@ MainWindow::~MainWindow()
 
 void MainWindow::moveMouse()
 {
-    unsigned int x = ui->xvalue->value();
-    unsigned int y = ui->yvalue->value();
+    int x = ui->xvalue->value();
+    int y = ui->yvalue->value();
     bool toolup = ui->toolupvalue->checkState() == Qt::Checked;
     if (mouse)
         mouse->moveTo(x, y, toolup);
@@ -58,25 +74,52 @@ void MainWindow::moveMouse()
 
 void MainWindow::translateKeyboard()
 {
-    unsigned int distance = ui->translation->value();
+    int distance = ui->keyboardtranslation->value();
     if (keyboard)
     {
         qDebug() << "translate keyboard " << QString::number(distance);
         if (distance > 0)
             keyboard->translation(LivingKeyboard::FORWARD, distance);
         else
-            keyboard->translation(LivingKeyboard::BACKWARD, distance);
+            keyboard->translation(LivingKeyboard::BACKWARD, -distance);
     }
 }
 
-void MainWindow::rotateKeyboard(int angle)
+void MainWindow::rotateKeyboard()
 {
+    int angle = ui->keyboardrotation->value();
     if (keyboard)
     {
         qDebug() << "rotate keyboard " << QString::number(angle);
         if (angle > 0)
             keyboard->rotation(LivingKeyboard::RIGHT, angle);
         else
-            keyboard->rotation(LivingKeyboard::LEFT, angle);
+            keyboard->rotation(LivingKeyboard::LEFT, -angle);
+    }
+}
+
+void MainWindow::translateScreen()
+{
+    int distance = ui->screentranslation->value();
+    if (screen)
+    {
+        qDebug() << "translate screen " << QString::number(distance);
+        if (distance > 0)
+            screen->translation(LivingScreen::RIGHT, distance);
+        else
+            screen->translation(LivingScreen::LEFT, -distance);
+    }
+}
+
+void MainWindow::rotateScreen()
+{
+    int angle = ui->screenrotation->value();
+    if (screen)
+    {
+        qDebug() << "rotate screen " << QString::number(angle);
+        if (angle > 0)
+            screen->rotation(LivingScreen::RIGHT, angle);
+        else
+            screen->rotation(LivingScreen::LEFT, -angle);
     }
 }
index e9d82e94c4d966b44c431d9aec9265ad0fa64c68..14e4847cdae5d5dec8c60ca497f514e2a141af90 100644 (file)
@@ -5,6 +5,7 @@
 
 #include <XYPlotter.h>
 #include <LivingKeyboard.h>
+#include <LivingScreen.h>
 #include <QDebug>
 
 namespace Ui {
@@ -22,12 +23,15 @@ class MainWindow : public QMainWindow
     public slots:
         void moveMouse();
         void translateKeyboard();
-        void rotateKeyboard(int angle);
+        void rotateKeyboard();
+        void translateScreen();
+        void rotateScreen();
 
     private:
         Ui::MainWindow *ui;
         XYPlotter *mouse;
         LivingKeyboard *keyboard;
+        LivingScreen *screen;
 };
 
 #endif // MAINWINDOW_H
index e1b32a7e042d8df308df1707632df0c17ae6579a..0abf002905bca581bd062d4f1505145b27eeb947 100644 (file)
@@ -7,7 +7,7 @@
     <x>0</x>
     <y>0</y>
     <width>400</width>
-    <height>388</height>
+    <height>610</height>
    </rect>
   </property>
   <property name="windowTitle">
        <item row="0" column="0">
         <widget class="QLabel" name="label_4">
          <property name="text">
-          <string>Tilt</string>
+          <string>Rotation</string>
+         </property>
+        </widget>
+       </item>
+       <item row="2" column="0">
+        <widget class="QLabel" name="label_5">
+         <property name="text">
+          <string>Translation</string>
+         </property>
+        </widget>
+       </item>
+       <item row="2" column="1">
+        <widget class="QSpinBox" name="keyboardtranslation">
+         <property name="minimum">
+          <number>-100</number>
+         </property>
+         <property name="maximum">
+          <number>100</number>
+         </property>
+         <property name="singleStep">
+          <number>10</number>
          </property>
         </widget>
        </item>
        <item row="0" column="1">
-        <widget class="QSlider" name="rotation">
+        <widget class="QSpinBox" name="keyboardrotation">
          <property name="minimum">
           <number>-180</number>
          </property>
          <property name="maximum">
           <number>180</number>
          </property>
-         <property name="orientation">
-          <enum>Qt::Horizontal</enum>
+         <property name="singleStep">
+          <number>10</number>
          </property>
         </widget>
        </item>
-       <item row="1" column="0">
-        <widget class="QLabel" name="label_5">
+       <item row="1" column="1">
+        <widget class="QPushButton" name="keyboardrotatebutton">
+         <property name="enabled">
+          <bool>false</bool>
+         </property>
          <property name="text">
-          <string>Translation$</string>
+          <string>Tilt</string>
          </property>
         </widget>
        </item>
-       <item row="1" column="1">
-        <widget class="QSpinBox" name="translation">
-         <property name="minimum">
-          <number>-100</number>
+       <item row="3" column="1">
+        <widget class="QPushButton" name="keyboardmovebutton">
+         <property name="enabled">
+          <bool>false</bool>
          </property>
-         <property name="maximum">
-          <number>100</number>
-         </property>
-         <property name="singleStep">
-          <number>10</number>
+         <property name="text">
+          <string>Move</string>
          </property>
         </widget>
        </item>
      </widget>
     </item>
     <item>
-     <widget class="QPushButton" name="keyboardbutton">
-      <property name="enabled">
-       <bool>false</bool>
+     <spacer name="verticalSpacer_2">
+      <property name="orientation">
+       <enum>Qt::Vertical</enum>
       </property>
-      <property name="text">
-       <string>Move</string>
+      <property name="sizeHint" stdset="0">
+       <size>
+        <width>20</width>
+        <height>40</height>
+       </size>
+      </property>
+     </spacer>
+    </item>
+    <item>
+     <widget class="QGroupBox" name="groupBox_3">
+      <property name="title">
+       <string>Screen</string>
       </property>
+      <layout class="QFormLayout" name="formLayout_3">
+       <item row="2" column="0">
+        <widget class="QLabel" name="label_6">
+         <property name="text">
+          <string>Translation</string>
+         </property>
+        </widget>
+       </item>
+       <item row="2" column="1">
+        <widget class="QSpinBox" name="screentranslation">
+         <property name="minimum">
+          <number>-650</number>
+         </property>
+         <property name="maximum">
+          <number>650</number>
+         </property>
+        </widget>
+       </item>
+       <item row="0" column="0">
+        <widget class="QLabel" name="label_7">
+         <property name="text">
+          <string>Rotation</string>
+         </property>
+        </widget>
+       </item>
+       <item row="0" column="1">
+        <widget class="QSpinBox" name="screenrotation">
+         <property name="minimum">
+          <number>-90</number>
+         </property>
+         <property name="maximum">
+          <number>90</number>
+         </property>
+         <property name="singleStep">
+          <number>10</number>
+         </property>
+        </widget>
+       </item>
+       <item row="1" column="1">
+        <widget class="QPushButton" name="screenrotatebutton">
+         <property name="enabled">
+          <bool>false</bool>
+         </property>
+         <property name="text">
+          <string>Rotate</string>
+         </property>
+        </widget>
+       </item>
+       <item row="3" column="1">
+        <widget class="QPushButton" name="screenmovebutton">
+         <property name="enabled">
+          <bool>false</bool>
+         </property>
+         <property name="text">
+          <string>Move</string>
+         </property>
+        </widget>
+       </item>
+      </layout>
      </widget>
     </item>
    </layout>
   <tabstop>yvalue</tabstop>
   <tabstop>toolupvalue</tabstop>
   <tabstop>mousebutton</tabstop>
-  <tabstop>rotation</tabstop>
-  <tabstop>translation</tabstop>
-  <tabstop>keyboardbutton</tabstop>
+  <tabstop>keyboardrotation</tabstop>
+  <tabstop>keyboardrotatebutton</tabstop>
+  <tabstop>keyboardtranslation</tabstop>
+  <tabstop>keyboardmovebutton</tabstop>
+  <tabstop>screenrotation</tabstop>
+  <tabstop>screenrotatebutton</tabstop>
+  <tabstop>screentranslation</tabstop>
+  <tabstop>screenmovebutton</tabstop>
  </tabstops>
  <resources/>
  <connections/>