absolute movement
authorMjolnir <thomas.pietrzak@inria.fr>
Mon, 7 Sep 2015 12:51:18 +0000 (14:51 +0200)
committerMjolnir <thomas.pietrzak@inria.fr>
Mon, 7 Sep 2015 12:51:18 +0000 (14:51 +0200)
Library/LivingDevice.cpp
Library/LivingDevice.h
Library/LivingScreen.cpp
Library/LivingScreen.h

index d1c4739d45b32f02263ace05ec3dcc3b738274b5..bded0125640502e2cf7ff31e46edac51a8f6e6b5 100644 (file)
@@ -85,7 +85,7 @@ void LivingDevice::closeSerialPort()
     serialPort = NULL;
 }
 
-bool LivingDevice::sendCommand(char command)
+bool LivingDevice::sendCommand(char command) const
 {
     if (serialPort)
     {
@@ -95,7 +95,7 @@ bool LivingDevice::sendCommand(char command)
     return false;
 }
 
-bool LivingDevice::sendCommand(char command, int value)
+bool LivingDevice::sendCommand(char command, int value) const
 {
     if (serialPort)
     {
@@ -105,7 +105,7 @@ bool LivingDevice::sendCommand(char command, int value)
     return false;
 }
 
-bool LivingDevice::sendCommand(char *command, int *value, int nb)
+bool LivingDevice::sendCommand(char *command, int *value, int nb) const
 {
     QString buffer;
     if (serialPort)
@@ -118,7 +118,7 @@ bool LivingDevice::sendCommand(char *command, int *value, int nb)
     return false;
 }
 
-QString LivingDevice::readAnswer(int timeout)
+QString LivingDevice::readAnswer(int timeout) const
 {
     char buffer[128];
 
index 137bcca340768a6064645402cd598fa969535032..0cb55961ac8d9bbd564b1bec3eb9c7cc13b4a614 100644 (file)
@@ -13,7 +13,7 @@ class LIVINGDESKTOPLIBRARYSHARED_EXPORT LivingDevice
 
         virtual void calibrate() = 0;
 
-        QString readAnswer(int timeout);
+        QString readAnswer(int timeout) const;
 
     protected:
         Serial *serialPort;
@@ -25,9 +25,9 @@ class LIVINGDESKTOPLIBRARYSHARED_EXPORT LivingDevice
         void discover(QString name, int timeout=5000);
         void closeSerialPort();
 
-        bool sendCommand(char command);
-        bool sendCommand(char command, int value);
-        bool sendCommand(char *command, int *value, int nb);
+        bool sendCommand(char command) const;
+        bool sendCommand(char command, int value) const;
+        bool sendCommand(char *command, int *value, int nb) const;
 
 };
 
index 06fb71c97b03a25e46823e797c92793a8c387765..867650308169f93ab0c1e449581ca4e9568e2154 100644 (file)
@@ -1,6 +1,9 @@
 #include "LivingScreen.h"
 
 #include <QDebug>
+#include <QRegularExpression>
+#include <QRegularExpressionMatch>
+#include <QThread>
 
 LivingScreen::LivingScreen()
 : LivingDevice("LivingScreen", 5000)
@@ -27,6 +30,53 @@ void LivingScreen::translation(LivingScreen::direction d, unsigned int distance)
     sendCommand('T', dis);
 }
 
+void LivingScreen::moveto(unsigned int pos)
+{
+    sendCommand('M', pos);
+}
+
+unsigned int LivingScreen::getPosition() const
+{
+    int tries = 0;
+    do
+    {
+        if (!sendCommand('P'))
+        {
+            qDebug() << "Cannot speak with port";
+            throw "Screen port unreachable";
+        }
+        QString answer = readAnswer(2000);
+        QRegularExpression reg("Position: (\\d+)");
+        QRegularExpressionMatch match = reg.match(answer);
+        if (match.hasMatch())
+            return match.captured(1).toUInt();
+        QThread::msleep(100);
+    } while (tries++ < 5);
+
+    return 0;
+}
+
+unsigned int LivingScreen::getWidth() const
+{
+    int tries = 0;
+    do
+    {
+        if (!sendCommand('W'))
+        {
+            qDebug() << "Cannot speak with port";
+            throw "Screen port unreachable";
+        }
+        QString answer = readAnswer(2000);
+        QRegularExpression reg("Width: (\\d+)");
+        QRegularExpressionMatch match = reg.match(answer);
+        if (match.hasMatch())
+            return match.captured(1).toUInt();
+        QThread::msleep(100);
+    } while (tries++ < 5);
+
+    return 0;
+}
+
 void LivingScreen::calibrate()
 {
     sendCommand('C');
index ac468f1f4f50ca14ffaab42b03aa81ffd8a791ef..c74ca2b9b443870171490d8651fc470a2053db8b 100644 (file)
@@ -14,6 +14,14 @@ class LIVINGDESKTOPLIBRARYSHARED_EXPORT LivingScreen : public LivingDevice
 
         void rotation(direction t, unsigned int angle);
         void translation(direction d, unsigned int distance);
+        void moveto(unsigned int pos);
+
+        //Get the current position
+        //may last 1s or so
+        unsigned int getPosition() const;
+        //Get the rack width
+        //may last 1s or so
+        unsigned int getWidth() const;
 
         void calibrate();
 };