Separate setAmplitudes and setFrequency. No more flicker in the vibrations
authorThomas Pietrzak <thomas.pietrzak@gmail.com>
Wed, 1 Aug 2012 11:39:33 +0000 (11:39 +0000)
committerThomas Pietrzak <thomas.pietrzak@gmail.com>
Wed, 1 Aug 2012 11:39:33 +0000 (11:39 +0000)
git-svn-id: svn+ssh://thomaspietrzak.com/var/svn/rep@82 47cf9a05-e0a8-4ed5-9e9b-101a649bc004

Arduino/TactonPlayer/TactonPlayerPreciseNew.cpp
Arduino/wristbandTactons/wristbandTactons.ino
TactonDebug/MagicCircle/magiccircle.cpp
TactonDebug/MagicCircle/magiccircle.h
TactonDebug/MagicCircle/magiccircle.ui
TactonDebug/TactonDebug.suo
TactonPlayer/TactonPlayer.cpp
TactonPlayer/TactonPlayer.hpp

index b06183f658fb7f0c583c8a16e95b6ec227022215..42922793e428ef32e14547b270e026159afa9198 100644 (file)
@@ -62,6 +62,12 @@ void TactonPlayerPreciseNew::buzz(unsigned int frequency, byte nbtactors, byte *
 {\r
        if (nbtactors != _nbtactors)\r
                return;\r
+\r
+       //clear\r
+       TCCR1A = 0;\r
+       //set duty cycle to 0\r
+       OCR1A = 0;\r
+       OCR1B = 0;\r
                \r
        sei();\r
        init();\r
index 883d53a582cf06d27d9da63e5d08e4431b4d8c63..a22abe3ab77f4dfc94a19b6833857934b2b28e12 100644 (file)
@@ -29,6 +29,7 @@ void loop()
 {
   byte index;
   unsigned long timestamp;
+  unsigned int freq;
 
   if (Serial.available() > 0)
   {
@@ -125,33 +126,42 @@ void loop()
         command = 0;
       }
       break;
-    //sets a frequency for all the vibrators and an amplitude for each one
-    // BnFFa1a2...an
+    //sets an amplitude for each vibrator
+    // Ana1a2...an
     // n = nb tactors (1 byte)
-    // FF = frequency (2 bytes)
     // A1, A2, ..., an : amplitudes (1 byte per tactor)
-    case 'B':
+    case 'A':
       if (nbf == 0 && Serial.available() >= 1)
         nbf = (unsigned int) Serial.read();
       if (nbf > 0)
       {
         //DO NOT OVERFLOW max(nbf): 60
-        while (posbuf < nbf + 2 && Serial.available() > 0)
+        while (posbuf < nbf && Serial.available() > 0)
         {
           buffer[posbuf] = Serial.read();
           posbuf++;
         }
-        if (posbuf >= nbf + 2)
+        if (posbuf >= nbf)
         {
-          manager.buzz(nbf, buffer);
+          manager.setAmplitudes(nbf, buffer);
           posbuf = 0;
           command = 0;
           nbf = 0;
         }
       }
       break;
+    //sets the frequency
+    case 'F':
+      if (Serial.available() >= 2)
+      {
+        freq = (((unsigned long)Serial.read()) << 8) | \
+            (((unsigned long)Serial.read()));
+        manager.setFrequency(freq);
+        command = 0;
+      }
+      break;
     //stop any vibration
-    case 'A':
+    case 'B':
       manager.stop();
       command = 0;
       break;
index 306cd3db7b080a6ede0566c594cf94b52211122e..8d19d8d7aa2a37eecd5c94088244621c01e59e1c 100644 (file)
@@ -22,6 +22,7 @@ MagicCircle::MagicCircle(QWidget *parent, Qt::WFlags flags)
        setDirection(_direction->currentText());\r
        setResolution(_resolution->value());\r
        setSpeed(_speed->value());\r
+       setFrequency(_frequency->value());\r
 \r
        connect(_play, SIGNAL(clicked(bool)), this, SLOT(play(bool)));\r
        connect(_stop, SIGNAL(clicked(bool)), this, SLOT(stop(bool)));\r
@@ -29,6 +30,7 @@ MagicCircle::MagicCircle(QWidget *parent, Qt::WFlags flags)
        connect(_direction, SIGNAL(editTextChanged(const QString &)), this, SLOT(setDirection(const QString &)));\r
        connect(_speed, SIGNAL(valueChanged(int)), this, SLOT(setSpeed(int)));\r
        connect(_resolution, SIGNAL(valueChanged(int)), this, SLOT(setResolution(int)));\r
+       connect(_frequency, SIGNAL(valueChanged(int)), this, SLOT(setFrequency(int)));\r
 \r
        connect(this, SIGNAL(setAngle(float)), SLOT(tactileFeedback(float)));\r
        connect(this, SIGNAL(setAngle(float)), SLOT(visualFeedback(float)));\r
@@ -84,6 +86,11 @@ void MagicCircle::setResolution(int res)
        setSpeed(_speed->value());\r
 }\r
 \r
+void MagicCircle::setFrequency(int frequency)\r
+{\r
+       _tactonPlayer->setFrequency(frequency);\r
+}\r
+\r
 void MagicCircle::tactileFeedback(float angle)\r
 {\r
        unsigned char amplitudes[4];\r
@@ -101,7 +108,7 @@ void MagicCircle::tactileFeedback(float angle)
        else\r
                amplitudes[2] = - 255 * cos(angle * M_PI / 180);\r
 \r
-       _tactonPlayer->buzz(_frequency->value(), 4, amplitudes);\r
+       _tactonPlayer->setAmplitudes(4, amplitudes);\r
 }\r
 \r
 void MagicCircle::visualFeedback(float)\r
index a8be0b534eabbefaa3864cb6ab3926fe9b48ef2e..3002144ea5c852dc260e0224bf94f9e6ce62ba91 100644 (file)
@@ -24,6 +24,7 @@ class MagicCircle : public QMainWindow, Ui::MagicCircleClass
                void setDirection(const QString &);\r
                void setSpeed(int);\r
                void setResolution(int);\r
+               void setFrequency(int);\r
 \r
                void tactileFeedback(float);\r
                void visualFeedback(float);\r
index cc56a5d3a2c742a5c8fe567c4a7f77829e74b862..b6829a595b6cb8c8e044d8d07426ff263cecc547 100644 (file)
          <number>1</number>\r
         </property>\r
         <property name="maximum">\r
-         <number>720</number>\r
+         <number>3000</number>\r
         </property>\r
         <property name="value">\r
-         <number>30</number>\r
+         <number>180</number>\r
         </property>\r
        </widget>\r
       </item>\r
@@ -90,7 +90,7 @@
          <number>360</number>\r
         </property>\r
         <property name="value">\r
-         <number>16</number>\r
+         <number>360</number>\r
         </property>\r
        </widget>\r
       </item>\r
          <number>400</number>\r
         </property>\r
         <property name="value">\r
-         <number>250</number>\r
+         <number>300</number>\r
         </property>\r
        </widget>\r
       </item>\r
index 1f131fa54fbafb1a1acf1e3a5440e4abd5524ce0..a996f4d29d6fedd621735021324e5ea2c645031e 100644 (file)
Binary files a/TactonDebug/TactonDebug.suo and b/TactonDebug/TactonDebug.suo differ
index 23f716edf37c29afc76bfa22e7baddc0ab8106df..975fcebbb30edd9fb1626f917697f55fc14732f9 100644 (file)
@@ -119,18 +119,28 @@ void TactonPlayer::schedule(unsigned char index, unsigned long timestamp)
        _comport->WriteData(buffer, 6);\r
 }\r
 \r
-void TactonPlayer::buzz(unsigned int frequency, unsigned int nbtactors, unsigned char *amplitudes)\r
+void TactonPlayer::setFrequency(unsigned int frequency)\r
 {\r
        if (!_comport)\r
                return;\r
 \r
-       unsigned char *buffer = new unsigned char[4 + nbtactors];\r
-       buffer[0] = 'B';\r
+       unsigned char buffer[3];\r
+       buffer[0] = 'F';\r
+       buffer[1] = (unsigned char)((frequency & 0x0000ff00) >> 8);\r
+       buffer[2] = (unsigned char)(frequency & 0x000000ff);\r
+       _comport->WriteData(buffer, 3);\r
+}\r
+\r
+void TactonPlayer::setAmplitudes(unsigned int nbtactors, unsigned char *amplitudes)\r
+{\r
+       if (!_comport)\r
+               return;\r
+\r
+       unsigned char *buffer = new unsigned char[2 + nbtactors];\r
+       buffer[0] = 'A';\r
        buffer[1] = nbtactors;\r
-       buffer[2] = (unsigned char)((frequency & 0x0000ff00) >> 8);\r
-       buffer[3] = (unsigned char)(frequency & 0x000000ff);\r
-       memcpy(buffer + 4, amplitudes, nbtactors);\r
-       _comport->WriteData(buffer, 4 + nbtactors);\r
+       memcpy(buffer + 2, amplitudes, nbtactors);\r
+       _comport->WriteData(buffer, 2 + nbtactors);\r
        delete[] buffer;\r
 }\r
 \r
@@ -139,7 +149,7 @@ void TactonPlayer::stopBuzz()
        if (!_comport)\r
                return;\r
 \r
-       _comport->WriteData("A", 1);\r
+       _comport->WriteData("B", 1);\r
 }\r
 \r
 void TactonPlayer::debugRead(char *res, int nb) const\r
index 8e520c3a0960618af60a2bf2c53a38b1ebe5eba5..a7dd0ce78d197f19228c238bde5e98e9fb8ca944 100644 (file)
@@ -31,9 +31,10 @@ class TactonPlayer
                        //schedule the play of a registered tacton\r
                        EXPORTED void schedule(unsigned char index, unsigned long timestamp);\r
 \r
-                       //sets a given frequency for all vibrators, and an amplitude for each vibrator\r
-                       //vibrates until explicitely stop\r
-                       EXPORTED void buzz(unsigned int frequency, unsigned int nbtactors, unsigned char *amplitudes);\r
+                       //sets an amplitude for each vibrator\r
+                       EXPORTED void setAmplitudes(unsigned int nbtactors, unsigned char *amplitudes);\r
+                       //sets a frequency for all vibrator\r
+                       EXPORTED void setFrequency(unsigned int frequency);\r
                        //stop any vibration\r
                        EXPORTED void stopBuzz();\r
 \r