From: Mjolnir Date: Wed, 9 Sep 2015 08:28:29 +0000 (+0200) Subject: Asynchronous peephole update X-Git-Url: https://git.thomaspietrzak.com/?a=commitdiff_plain;h=0226302d84404d56e769e952bfa14e0cd6ea9361;p=livingdesktop.git Asynchronous peephole update --- diff --git a/ArduinoScreen/ArduinoScreen.ino b/ArduinoScreen/ArduinoScreen.ino index 4791a65..a5e4e97 100644 --- a/ArduinoScreen/ArduinoScreen.ino +++ b/ArduinoScreen/ArduinoScreen.ino @@ -27,12 +27,9 @@ void eval(int command, int value) { case 'I': Serial.println(F("LivingScreen")); - //Serial.flush(); break; //relative translation case 'T': - //Serial.print("Translate "); - //Serial.println(value); translate(value); break; //absolute movement @@ -41,8 +38,6 @@ void eval(int command, int value) translate(value - pos); break; case 'R': - //Serial.print("Rotate "); - //Serial.println(value); rotate(value); break; case 'A': @@ -51,7 +46,6 @@ void eval(int command, int value) Serial.println(a); break; case 'C': - //Serial.println("Calibration"); calibrate(); break; case 'S': @@ -67,9 +61,10 @@ void eval(int command, int value) Serial.println(rackwidth); break; default: - Serial.print("Unknown command "); - Serial.write(command); - Serial.write('\n'); + //Serial.print("Unknown command "); + //Serial.write(command); + //Serial.write('\n'); + break; } } @@ -95,21 +90,21 @@ void loop() { c = Serial.read(); - if (c == '\n' || c == '\r') + if (c == '\n')// || c == '\r') { //if we have a command => evaluate it - if (command) + if (command >= 'A' && command <= 'Z') { if (sign == NEGATIVE) value = -value; eval(command, value); - //reset command - command = 0; - value = 0; - sign = POSITIVE; } + //reset command + command = 0; + value = 0; + sign = POSITIVE; } - else if (!command) + else if (!command && c >= 'A' && c <= 'Z') command = c; else if (c == '-') sign = NEGATIVE; @@ -117,7 +112,7 @@ void loop() value = value * 10 + (c - '0'); } else - delayMicroseconds(100); + delayMicroseconds(200); } diff --git a/Library/LivingDevice.cpp b/Library/LivingDevice.cpp index bded012..513a2d4 100644 --- a/Library/LivingDevice.cpp +++ b/Library/LivingDevice.cpp @@ -139,7 +139,6 @@ QString LivingDevice::readAnswer(int timeout) const buffer[nb] = '\0'; while(buffer[strlen(buffer)-1] == '\n' || buffer[strlen(buffer)-1] == '\r') buffer[strlen(buffer)-1] = '\0'; - qDebug() << "Read" << buffer; return QString(buffer); } return ""; diff --git a/LivingDemos/MenuPrincipal.cpp b/LivingDemos/MenuPrincipal.cpp index 4b77dd2..a61de3d 100644 --- a/LivingDemos/MenuPrincipal.cpp +++ b/LivingDemos/MenuPrincipal.cpp @@ -7,6 +7,7 @@ #include #include #include +#include MenuPrincipal::MenuPrincipal(QWidget *parent) : QMainWindow(parent), @@ -187,4 +188,5 @@ void MenuPrincipal::openPeephole() { Peephole * p= new Peephole(this); p->show(); + QTimer::singleShot(Peephole::updatedelay, p, SLOT(updatescreenpos())); } diff --git a/LivingDemos/Peephole.cpp b/LivingDemos/Peephole.cpp index f5aa85a..8522d38 100644 --- a/LivingDemos/Peephole.cpp +++ b/LivingDemos/Peephole.cpp @@ -4,10 +4,14 @@ #include #include #include +#include + +const int Peephole::updatedelay = 100; Peephole::Peephole(QWidget *parent) : QMainWindow(parent), screenpos(330), + previousscreenpos(330), viewpos(0), ui(new Ui::Fullscreen) { @@ -37,20 +41,28 @@ Peephole::~Peephole() delete ui; } -void Peephole::scroll(int pos) +// allows asynchronous udate +// prevents flooding the device +void Peephole::updatescreenpos() { - qDebug() << "Pos: " << pos; - MenuPrincipal * menu = dynamic_cast(this->parent()); - if (menu->getScreen()) + if (screenpos != previousscreenpos) { - unsigned int min = ui->image->horizontalScrollBar()->minimum(); - unsigned int range = ui->image->horizontalScrollBar()->maximum() - min; - if (range == 0) - return; - unsigned int dest = rackwidth * pos / range - min; - qDebug() << "Move to" << dest; - menu->getScreen()->moveto(dest); - //We should make something better to prevent from flooding the screen - QThread::msleep(20); + MenuPrincipal * menu = dynamic_cast(this->parent()); + if (menu->getScreen()) + { + menu->getScreen()->moveto(screenpos); + previousscreenpos = screenpos; + } } + QTimer::singleShot(updatedelay, this, SLOT(updatescreenpos())); +} + +void Peephole::scroll(int pos) +{ + unsigned int min = ui->image->horizontalScrollBar()->minimum(); + unsigned int range = ui->image->horizontalScrollBar()->maximum() - min; + if (range == 0) + return; + unsigned int dest = rackwidth * pos / range - min; + screenpos = dest; } diff --git a/LivingDemos/Peephole.h b/LivingDemos/Peephole.h index 7446d3b..b0acc85 100644 --- a/LivingDemos/Peephole.h +++ b/LivingDemos/Peephole.h @@ -17,12 +17,14 @@ class Peephole : public QMainWindow public: explicit Peephole(QWidget *parent = 0); ~Peephole(); + static const int updatedelay; public slots: + void updatescreenpos(); void scroll(int pos); private: - int screenpos, viewpos; + int screenpos, previousscreenpos, viewpos; unsigned int rackwidth, bgwidth; Ui::Fullscreen *ui; QGraphicsScene _background;