From: Mjolnir Date: Mon, 7 Sep 2015 12:51:18 +0000 (+0200) Subject: absolute movement X-Git-Url: https://git.thomaspietrzak.com/?a=commitdiff_plain;h=5c4583f703938db9baef97609910ba46081e7960;p=livingdesktop.git absolute movement --- diff --git a/Library/LivingDevice.cpp b/Library/LivingDevice.cpp index d1c4739..bded012 100644 --- a/Library/LivingDevice.cpp +++ b/Library/LivingDevice.cpp @@ -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]; diff --git a/Library/LivingDevice.h b/Library/LivingDevice.h index 137bcca..0cb5596 100644 --- a/Library/LivingDevice.h +++ b/Library/LivingDevice.h @@ -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; }; diff --git a/Library/LivingScreen.cpp b/Library/LivingScreen.cpp index 06fb71c..8676503 100644 --- a/Library/LivingScreen.cpp +++ b/Library/LivingScreen.cpp @@ -1,6 +1,9 @@ #include "LivingScreen.h" #include +#include +#include +#include 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'); diff --git a/Library/LivingScreen.h b/Library/LivingScreen.h index ac468f1..c74ca2b 100644 --- a/Library/LivingScreen.h +++ b/Library/LivingScreen.h @@ -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(); };