Asynchronous peephole update
authorMjolnir <thomas.pietrzak@inria.fr>
Wed, 9 Sep 2015 08:28:29 +0000 (10:28 +0200)
committerMjolnir <thomas.pietrzak@inria.fr>
Wed, 9 Sep 2015 08:28:29 +0000 (10:28 +0200)
ArduinoScreen/ArduinoScreen.ino
Library/LivingDevice.cpp
LivingDemos/MenuPrincipal.cpp
LivingDemos/Peephole.cpp
LivingDemos/Peephole.h

index 4791a65c100bcd6dd9e2caadea7a8b08cbe0d05b..a5e4e97b50c87ef44150e664be9d030b130e01e0 100644 (file)
@@ -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);
 }
 
 
index bded0125640502e2cf7ff31e46edac51a8f6e6b5..513a2d43e9659ef5a03e20bba0fede78dbf95297 100644 (file)
@@ -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 "";
index 4b77dd23bbbcb70cb9dfc38c6024dda5ce2977e4..a61de3d8c4ca197f480b506f85e2fd003968ec5c 100644 (file)
@@ -7,6 +7,7 @@
 #include <Force.h>
 #include <GestureOutput.h>
 #include <Peephole.h>
+#include <QTimer>
 
 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()));
 }
index f5aa85a5f9eba27dffe92ee257aed35b49f9d345..8522d382e71de9036ef623518d5349a09657e0cc 100644 (file)
@@ -4,10 +4,14 @@
 #include <MenuPrincipal.h>
 #include <QScrollBar>
 #include <QThread>
+#include <QTimer>
+
+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<MenuPrincipal *>(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<MenuPrincipal *>(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;
 }
index 7446d3bcad53699424c7911f7cb6c0f563d4cb25..b0acc856c476a28df2bf6327a9a1b1b408afccb6 100644 (file)
@@ -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;