From a84610a58b18c34035f2a42a3f15a7ed9765791a Mon Sep 17 00:00:00 2001 From: Mjolnir Date: Mon, 24 Aug 2015 15:44:42 +0200 Subject: [PATCH] Screen tanslation + discovery + home made serial lib --- ArduinoScreen/ArduinoScreen.ino | 6 +- Library/LivingDesktopLibrary.pro | 14 ++- Library/LivingDevice.cpp | 110 ++++++++++++++++++++++ Library/LivingDevice.h | 30 ++++++ Library/LivingKeyboard.cpp | 93 ++----------------- Library/LivingKeyboard.h | 11 +-- Library/LivingScreen.cpp | 28 ++++++ Library/LivingScreen.h | 19 ++++ Library/Serial.cpp | 10 ++ Library/Serial.h | 36 ++++++++ Library/SerialLinux.cpp | 154 +++++++++++++++++++++++++++++++ Library/SerialLinux.h | 34 +++++++ Library/XYPlotter.cpp | 83 +---------------- Library/XYPlotter.h | 11 +-- Test/mainwindow.cpp | 61 ++++++++++-- Test/mainwindow.h | 6 +- Test/mainwindow.ui | 144 ++++++++++++++++++++++++----- 17 files changed, 630 insertions(+), 220 deletions(-) create mode 100644 Library/LivingDevice.cpp create mode 100644 Library/LivingDevice.h create mode 100644 Library/LivingScreen.cpp create mode 100644 Library/LivingScreen.h create mode 100644 Library/Serial.cpp create mode 100644 Library/Serial.h create mode 100644 Library/SerialLinux.cpp create mode 100644 Library/SerialLinux.h diff --git a/ArduinoScreen/ArduinoScreen.ino b/ArduinoScreen/ArduinoScreen.ino index 15901a6..d3772f0 100644 --- a/ArduinoScreen/ArduinoScreen.ino +++ b/ArduinoScreen/ArduinoScreen.ino @@ -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() diff --git a/Library/LivingDesktopLibrary.pro b/Library/LivingDesktopLibrary.pro index 07fb8e6..dc4df60 100644 --- a/Library/LivingDesktopLibrary.pro +++ b/Library/LivingDesktopLibrary.pro @@ -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 index 0000000..c136796 --- /dev/null +++ b/Library/LivingDevice.cpp @@ -0,0 +1,110 @@ +#include "LivingDevice.h" + +#include +#include +#include +#include + +//#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 index 0000000..c7d0ace --- /dev/null +++ b/Library/LivingDevice.h @@ -0,0 +1,30 @@ +#ifndef LIVINGDEVICE_H +#define LIVINGDEVICE_H + +#include "livingdesktoplibrary_global.h" +#include +#include + +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 diff --git a/Library/LivingKeyboard.cpp b/Library/LivingKeyboard.cpp index 07a10cb..cb733b3 100644 --- a/Library/LivingKeyboard.cpp +++ b/Library/LivingKeyboard.cpp @@ -1,103 +1,28 @@ #include "LivingKeyboard.h" #include -#include - -#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); } diff --git a/Library/LivingKeyboard.h b/Library/LivingKeyboard.h index 0fbe8d1..7d7ff8b 100644 --- a/Library/LivingKeyboard.h +++ b/Library/LivingKeyboard.h @@ -1,11 +1,9 @@ #ifndef LIVINGKEYBOARD_H #define LIVINGKEYBOARD_H -#include "livingdesktoplibrary_global.h" -#include -#include +#include -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 index 0000000..1042bfa --- /dev/null +++ b/Library/LivingScreen.cpp @@ -0,0 +1,28 @@ +#include "LivingScreen.h" + +#include + +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 index 0000000..55def11 --- /dev/null +++ b/Library/LivingScreen.h @@ -0,0 +1,19 @@ +#ifndef LIVINGSCREEN_H +#define LIVINGSCREEN_H + +#include + +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 index 0000000..7ef3177 --- /dev/null +++ b/Library/Serial.cpp @@ -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 index 0000000..a3043c3 --- /dev/null +++ b/Library/Serial.h @@ -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 index 0000000..2ce9a18 --- /dev/null +++ b/Library/SerialLinux.cpp @@ -0,0 +1,154 @@ +#include "SerialLinux.h" + +#include +#include +#include +#include +#include +#include +#include +#include + +#include +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 index 0000000..c034584 --- /dev/null +++ b/Library/SerialLinux.h @@ -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 diff --git a/Library/XYPlotter.cpp b/Library/XYPlotter.cpp index ed76055..f0c90e9 100644 --- a/Library/XYPlotter.cpp +++ b/Library/XYPlotter.cpp @@ -1,94 +1,21 @@ #include "XYPlotter.h" -#include #include -#include -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); } diff --git a/Library/XYPlotter.h b/Library/XYPlotter.h index 69319e3..af8f331 100644 --- a/Library/XYPlotter.h +++ b/Library/XYPlotter.h @@ -1,26 +1,21 @@ #ifndef XYPLOTTER_H #define XYPLOTTER_H -#include "livingdesktoplibrary_global.h" -#include -#include +#include -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 diff --git a/Test/mainwindow.cpp b/Test/mainwindow.cpp index 49682b1..b7d653c 100644 --- a/Test/mainwindow.cpp +++ b/Test/mainwindow.cpp @@ -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); } } diff --git a/Test/mainwindow.h b/Test/mainwindow.h index e9d82e9..14e4847 100644 --- a/Test/mainwindow.h +++ b/Test/mainwindow.h @@ -5,6 +5,7 @@ #include #include +#include #include 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 diff --git a/Test/mainwindow.ui b/Test/mainwindow.ui index e1b32a7..0abf002 100644 --- a/Test/mainwindow.ui +++ b/Test/mainwindow.ui @@ -7,7 +7,7 @@ 0 0 400 - 388 + 610 @@ -107,40 +107,60 @@ - Tilt + Rotation + + + + + + + Translation + + + + + + + -100 + + + 100 + + + 10 - + -180 180 - - Qt::Horizontal + + 10 - - + + + + false + - Translation$ + Tilt - - - - -100 + + + + false - - 100 - - - 10 + + Move @@ -148,13 +168,82 @@ - - - false + + + Qt::Vertical - - Move + + + 20 + 40 + + + + + + + + Screen + + + + + Translation + + + + + + + -650 + + + 650 + + + + + + + Rotation + + + + + + + -90 + + + 90 + + + 10 + + + + + + + false + + + Rotate + + + + + + + false + + + Move + + + + @@ -166,9 +255,14 @@ yvalue toolupvalue mousebutton - rotation - translation - keyboardbutton + keyboardrotation + keyboardrotatebutton + keyboardtranslation + keyboardmovebutton + screenrotation + screenrotatebutton + screentranslation + screenmovebutton -- 2.30.2