From: Mjolnir Date: Fri, 4 Sep 2015 11:37:01 +0000 (+0200) Subject: Gesture output demo + arcs with mouse + free com port on release X-Git-Url: https://git.thomaspietrzak.com/?a=commitdiff_plain;h=3db528ccf569e7cdc7278dc65367f90c5e38b361;p=livingdesktop.git Gesture output demo + arcs with mouse + free com port on release --- diff --git a/Library/LivingDevice.cpp b/Library/LivingDevice.cpp index 5bccf37..d1c4739 100644 --- a/Library/LivingDevice.cpp +++ b/Library/LivingDevice.cpp @@ -11,7 +11,7 @@ char const *LivingDevice::serialports[] = {"/dev/ttyUSB0", "/dev/ttyUSB1", "/dev bool LivingDevice::usedports[] = {false, false, false, false, false}; LivingDevice::LivingDevice(QString name, int timeout) -: serialPort(NULL) +: serialPort(NULL), portnum(-1) { discover(name, timeout); } @@ -51,7 +51,8 @@ void LivingDevice::discover(QString name, int timeout) if (answer == name) { qDebug() << "Found " << name << " on port" << serialports[i]; - usedports[i] = true; + portnum = i; + usedports[portnum] = true; } else { @@ -77,7 +78,10 @@ void LivingDevice::discover(QString name, int timeout) void LivingDevice::closeSerialPort() { if (serialPort) + { delete serialPort; + usedports[portnum] = false; + } serialPort = NULL; } diff --git a/Library/LivingDevice.h b/Library/LivingDevice.h index a820cfe..137bcca 100644 --- a/Library/LivingDevice.h +++ b/Library/LivingDevice.h @@ -17,6 +17,7 @@ class LIVINGDESKTOPLIBRARYSHARED_EXPORT LivingDevice protected: Serial *serialPort; + int portnum; static char const *serialports[]; static bool usedports[]; diff --git a/Library/XYPlotter.cpp b/Library/XYPlotter.cpp index 74a7759..77af070 100644 --- a/Library/XYPlotter.cpp +++ b/Library/XYPlotter.cpp @@ -1,6 +1,7 @@ #include "XYPlotter.h" #include +#include const int XYPlotter::toolDistance = 70; @@ -18,6 +19,33 @@ void XYPlotter::moveTo(int x, int y, bool toolup) char commands[] = {'G', 'X', 'Y', 'Z'}; int values[] = {0, x, y, toolup * toolDistance}; sendCommand(commands, values, 4); + posX = x; + posY = y; +} + +void XYPlotter::movearc(int centerx, int centery, int angle, bool toolup) +{ + + int g; + if (angle > 0) + g = 2; + else + g = 3; + + qreal a = angle * M_PI / 180.; + qreal deltax = int(posX) - centerx; + qreal deltay = int(posY) - centery; + qreal currentangle = qAtan2(deltay, deltax); + qreal radius = qSqrt(qPow(deltax, 2.0) + qPow(deltay, 2.0)); + int destx = centerx + radius * cos(currentangle + a); + int desty = centery + radius * sin(currentangle + a); + + char commands[] = {'G', 'X', 'Y', 'Z', 'I', 'J'}; + int values[] = {g, destx, desty, toolup * toolDistance, -deltax, -deltay}; + sendCommand(commands, values, 6); + + posX = destx; + posY = desty; } void XYPlotter::calibrate() diff --git a/Library/XYPlotter.h b/Library/XYPlotter.h index b178f8c..83491cd 100644 --- a/Library/XYPlotter.h +++ b/Library/XYPlotter.h @@ -10,6 +10,7 @@ class LIVINGDESKTOPLIBRARYSHARED_EXPORT XYPlotter : public LivingDevice ~XYPlotter(); void moveTo(int x, int y, bool toolup=true); + void movearc(int centerx, int centery, int angle, bool toolup=true); unsigned int getX() { return posX; } unsigned int getY() { return posY; } diff --git a/LivingDemos/Force.h b/LivingDemos/Force.h index 7102a62..bf78cd6 100644 --- a/LivingDemos/Force.h +++ b/LivingDemos/Force.h @@ -19,7 +19,7 @@ class Force : public QMainWindow explicit Force(QWidget *parent = 0); ~Force(); - public slots: + private slots: void moveMouse(); private: diff --git a/LivingDemos/GestureOutput.cpp b/LivingDemos/GestureOutput.cpp index 59d33fd..18de6fc 100644 --- a/LivingDemos/GestureOutput.cpp +++ b/LivingDemos/GestureOutput.cpp @@ -1,14 +1,100 @@ #include "GestureOutput.h" #include "ui_gestureoutput.h" +#include +#include + GestureOutput::GestureOutput(QWidget *parent) : QDialog(parent), ui(new Ui::GestureOutput) { ui->setupUi(this); + setAttribute(Qt::WA_DeleteOnClose); + + connect(ui->gestureL, SIGNAL(clicked()), this, SLOT(gestureL())); + connect(ui->gestureM, SIGNAL(clicked()), this, SLOT(gestureM())); + connect(ui->gestureN, SIGNAL(clicked()), this, SLOT(gestureN())); + connect(ui->gestureU, SIGNAL(clicked()), this, SLOT(gestureU())); + connect(ui->move, SIGNAL(clicked()), this, SLOT(move())); + + MenuPrincipal * menu = dynamic_cast(parent); + if (!menu->getMouse()) + qDebug() << "Warning: mouse not connected"; + else + { + menu->getMouse()->moveTo(500, 500); + ui->gestureL->setEnabled(true); + ui->gestureM->setEnabled(true); + ui->gestureN->setEnabled(true); + ui->gestureU->setEnabled(true); + ui->move->setEnabled(true); + } } GestureOutput::~GestureOutput() { delete ui; } + +void GestureOutput::gestureL() +{ + MenuPrincipal * menu = dynamic_cast(this->parent()); + if (menu->getMouse()) + { + unsigned int x = menu->getMouse()->getX(); + unsigned int y = menu->getMouse()->getY(); + menu->getMouse()->moveTo(x, y - 100); + menu->getMouse()->moveTo(x + 50, y - 100); + } +} + +void GestureOutput::gestureM() +{ + MenuPrincipal * menu = dynamic_cast(this->parent()); + if (menu->getMouse()) + { + unsigned int x = menu->getMouse()->getX(); + unsigned int y = menu->getMouse()->getY(); + menu->getMouse()->moveTo(x, y + 100); + menu->getMouse()->moveTo(x + 50, y + 50); + menu->getMouse()->moveTo(x + 100, y + 100); + menu->getMouse()->moveTo(x + 100, y); + } +} + +void GestureOutput::gestureN() +{ + MenuPrincipal * menu = dynamic_cast(this->parent()); + if (menu->getMouse()) + { + unsigned int x = menu->getMouse()->getX(); + unsigned int y = menu->getMouse()->getY(); + menu->getMouse()->moveTo(x, y + 100); + menu->getMouse()->moveTo(x + 50, y); + menu->getMouse()->moveTo(x + 50, y + 100); + } +} + +void GestureOutput::gestureU() +{ + MenuPrincipal * menu = dynamic_cast(this->parent()); + if (menu->getMouse()) + { + unsigned int x = menu->getMouse()->getX(); + unsigned int y = menu->getMouse()->getY(); + menu->getMouse()->moveTo(x, y - 100); + menu->getMouse()->movearc(x + 50, y - 100, -180); + menu->getMouse()->moveTo(x + 100, y); + } +} + +void GestureOutput::move() +{ + MenuPrincipal * menu = dynamic_cast(this->parent()); + if (menu->getMouse()) + { + unsigned int x = ui->xvalue->value(); + unsigned int y = ui->yvalue->value(); + menu->getMouse()->moveTo(x, y); + } +} diff --git a/LivingDemos/GestureOutput.h b/LivingDemos/GestureOutput.h index ce6f181..e97e7a4 100644 --- a/LivingDemos/GestureOutput.h +++ b/LivingDemos/GestureOutput.h @@ -15,6 +15,13 @@ class GestureOutput : public QDialog explicit GestureOutput(QWidget *parent = 0); ~GestureOutput(); + private slots: + void gestureL(); + void gestureM(); + void gestureN(); + void gestureU(); + void move(); + private: Ui::GestureOutput *ui; }; diff --git a/LivingDemos/LivingDemos.pro b/LivingDemos/LivingDemos.pro index 05b1084..73d9e2f 100644 --- a/LivingDemos/LivingDemos.pro +++ b/LivingDemos/LivingDemos.pro @@ -26,13 +26,16 @@ SOURCES += main.cpp\ MenuPrincipal.cpp \ Options.cpp \ Tidy.cpp \ - Force.cpp + Force.cpp \ + GestureOutput.cpp HEADERS += MenuPrincipal.h \ Options.h \ Tidy.h \ - Force.h + Force.h \ + GestureOutput.h FORMS += menuprincipal.ui \ options.ui \ - fullscreen.ui + fullscreen.ui \ + gestureoutput.ui diff --git a/LivingDemos/MenuPrincipal.cpp b/LivingDemos/MenuPrincipal.cpp index f7d78b6..a2edcd3 100644 --- a/LivingDemos/MenuPrincipal.cpp +++ b/LivingDemos/MenuPrincipal.cpp @@ -5,6 +5,7 @@ #include #include +#include MenuPrincipal::MenuPrincipal(QWidget *parent) : QMainWindow(parent), @@ -21,6 +22,7 @@ MenuPrincipal::MenuPrincipal(QWidget *parent) : //demos connect(ui->tidy, SIGNAL(clicked()), this, SLOT(openTidy())); connect(ui->force, SIGNAL(clicked()), this, SLOT(openForce())); + connect(ui->gestureoutput, SIGNAL(clicked()), this, SLOT(openGestureOutput())); connectKeyboard(); connectScreen(); @@ -171,3 +173,9 @@ void MenuPrincipal::openForce() Force *f = new Force(this); f->show(); } + +void MenuPrincipal::openGestureOutput() +{ + GestureOutput * g= new GestureOutput(this); + g->show(); +} diff --git a/LivingDemos/MenuPrincipal.h b/LivingDemos/MenuPrincipal.h index b7a6e43..ba2bbca 100644 --- a/LivingDemos/MenuPrincipal.h +++ b/LivingDemos/MenuPrincipal.h @@ -48,6 +48,7 @@ class MenuPrincipal : public QMainWindow //run demos void openTidy(); void openForce(); + void openGestureOutput(); private: Ui::MenuPrincipal *ui; diff --git a/LivingDemos/gestureoutput.ui b/LivingDemos/gestureoutput.ui index 575b262..b7e9b3a 100644 --- a/LivingDemos/gestureoutput.ui +++ b/LivingDemos/gestureoutput.ui @@ -1,38 +1,224 @@ + - - - GestureOutput 0 0 - 400 - 300 + 505 + 459 Dialog - - - - 30 - 240 - 341 - 32 - - - - Qt::Horizontal - - - QDialogButtonBox::Cancel|QDialogButtonBox::Ok - - + + + + + + + false + + + + 0 + 0 + + + + + 30 + + + + M + + + + + + + false + + + + 0 + 0 + + + + + 30 + + + + N + + + + + + + false + + + + 0 + 0 + + + + + 30 + + + + L + + + + + + + false + + + + 0 + 0 + + + + + 30 + + + + U + + + + + + + + + Qt::Vertical + + + QSizePolicy::Maximum + + + + 20 + 40 + + + + + + + + + + QLayout::SetMinAndMaxSize + + + QFormLayout::AllNonFixedFieldsGrow + + + + + X + + + + + + + 2000 + + + 100 + + + + + + + Y + + + + + + + 2000 + + + 100 + + + + + + + + + false + + + + 0 + 0 + + + + Move + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + + + + 0 + 0 + + + + Qt::Horizontal + + + QDialogButtonBox::Close + + + + + + - + + gestureM + gestureL + gestureN + gestureU + diff --git a/LivingDemos/menuprincipal.ui b/LivingDemos/menuprincipal.ui index 9cfa8b0..ca9aaa0 100644 --- a/LivingDemos/menuprincipal.ui +++ b/LivingDemos/menuprincipal.ui @@ -52,7 +52,7 @@ - + 0 @@ -65,7 +65,7 @@ - Demo 3 + Gesture Output @@ -178,7 +178,7 @@ tidy force - pushButton_2 + gestureoutput pushButton_4 options