Add buzz functionality: play different amplitudes with no time limit.
authorThomas Pietrzak <thomas.pietrzak@gmail.com>
Fri, 27 Jul 2012 14:35:36 +0000 (14:35 +0000)
committerThomas Pietrzak <thomas.pietrzak@gmail.com>
Fri, 27 Jul 2012 14:35:36 +0000 (14:35 +0000)
git-svn-id: svn+ssh://thomaspietrzak.com/var/svn/rep@76 47cf9a05-e0a8-4ed5-9e9b-101a649bc004

Arduino/TactonPlayer/TactonManager.cpp
Arduino/TactonPlayer/TactonManager.h
Arduino/TactonPlayer/TactonPlayer.h
Arduino/TactonPlayer/TactonPlayerPreciseNew.cpp
Arduino/TactonPlayer/TactonPlayerPreciseNew.h
Arduino/wristbandTactons/wristbandTactons.ino

index a0d315b0a177e110a3c9fb4604be949968222793..6bee80ac9073eb0fe173748ae7df0a64ddfe67db 100644 (file)
@@ -116,4 +116,20 @@ void TactonManager::checkPlay()
        }*/\r
 }\r
 \r
-               
\ No newline at end of file
+void TactonManager::buzz(unsigned int nbf, byte *desc)\r
+{\r
+       byte *amplitudes = (byte *)malloc(nbf * sizeof(byte));\r
+       if (amplitudes == NULL) \r
+               return;\r
+               \r
+       for (unsigned int i = 0 ; i < nbf ; i++)\r
+               amplitudes[i] = desc[i + 1];\r
+\r
+       _player->buzz(desc[0], nbf, amplitudes);\r
+       free(amplitudes);\r
+}\r
+\r
+void TactonManager::stop()\r
+{\r
+       _player->stop();\r
+}\r
index b37701f305f67aa5fa22a0c8304040ea8b5ebdcc..240a2acb11d472eaad1df6d46cace785b9f7f93a 100644 (file)
@@ -22,6 +22,10 @@ class TactonManager
                void addPlay(byte index, unsigned long timestamp);\r
                void checkPlay();\r
                void setOffset(unsigned long timestamp);\r
+               \r
+               void buzz(unsigned int nbf, byte *desc);\r
+               void stop();\r
+\r
 \r
                void clear();\r
                \r
index 28fed8de4b2dca0426d4bb1a769261a14ba3abad..1c12f4104f14fa3c56991ad4d6c99d30b439ebbe 100644 (file)
@@ -12,7 +12,14 @@ class TactonPlayer
 {\r
        public:\r
                TactonPlayer(byte nbtactors, byte *pins);\r
-                               \r
+\r
+               //Same frequency for all the vibrators, different amplitude.\r
+               //Vibrates until stop\r
+               void buzz(unsigned int frequency, byte nbtactors, byte amplitudes[]);\r
+\r
+               //Stop any vibration\r
+               void stop();\r
+\r
                //8bits pattern => max 8 tactors, change type if using more\r
                virtual void beep(byte pattern, unsigned long duration, unsigned int frequency, byte amplitude) = 0;\r
 /*             \r
index ac92dd6fd1855a03183093750d36d19d81f6e82e..55550cda963da59b6ed01e019b2eaf8758a61cf6 100644 (file)
@@ -57,6 +57,33 @@ void TactonPlayerPreciseNew::setFrequency(unsigned int frequency)
        Serial.println(_ccper256cv, DEC);*/\r
 }\r
 \r
+//Start a vibration of a given frequency, with different amplitudes for each vibrator\r
+void TactonPlayerPreciseNew::buzz(unsigned int frequency, byte nbtactors, byte *amplitudes)\r
+{\r
+       if (nbtactors != _nbtactors)\r
+               return;\r
+               \r
+       sei();\r
+       init();\r
+       \r
+       //set the amplitudes\r
+       for (int i = 0 ; i < _nbtactors ; i++)\r
+               analogWrite(_pins[i], amplitudes[i]);\r
+}\r
+\r
+//Stop any vibration\r
+void TactonPlayerPreciseNew::stop()\r
+{\r
+       TCCR1A = 0;\r
+       //set duty cycle to 0\r
+       OCR1A = 0;\r
+       OCR1B = 0;\r
+       //clear the pattern\r
+       for (int i = 0 ; i < _nbtactors ; i++)\r
+               digitalWrite(_pins[i], LOW);\r
+}\r
+\r
+//Play a Tacton for a specified duration, frequency and amplitude\r
 void TactonPlayerPreciseNew::beep(byte pattern, unsigned long duration, unsigned int frequency, byte amplitude)\r
 {\r
        sei();\r
index 03bcbac406a1326c4317df6516161097805d8a4e..80d1005873a9ddd1914aff8f0154193bc2822ec1 100644 (file)
@@ -9,7 +9,14 @@ class TactonPlayerPreciseNew: public TactonPlayer
 {\r
        public:\r
                TactonPlayerPreciseNew(byte nbtactors, byte *pins, byte pwmPin);\r
-                               \r
+               \r
+               //Same frequency for all the vibrators, different amplitude.\r
+               //Vibrates until stop\r
+               void TactonPlayerPreciseNew::buzz(unsigned int frequency, byte nbtactors, byte *amplitudes);\r
+\r
+               //Stop any vibration\r
+               void stop();\r
+               \r
                //8bits pattern => max 8 tactors, change type if using more\r
                void beep(byte pattern, unsigned long duration, unsigned int frequency, byte amplitude);\r
 \r
index 658eeea178b7e143611e291368b3ddf797bae2c7..014691a935cf3a47543a7c031c30b3a04333783a 100644 (file)
@@ -2,7 +2,7 @@
 #include <TactonPlayerPreciseNew.h>
 #include <Tacton.h>
 byte pins[] = {
-  3, 11, 5, 6};
+  6, 5, 11, 3};
 byte pwmPin = 9;
 
 TactonPlayerPreciseNew player(4, pins, 9);
@@ -125,6 +125,35 @@ void loop()
         command = 0;
       }
       break;
+    //sets a frequency for all the vibrators and an amplitude for each one
+    // BnFa1a2...an
+    // n = nb tactors
+    // F = frequency
+    // A1, A2, ..., an : amplitudes
+    case 'B':
+      if (nbf == 0 && Serial.available() >= 2)
+        nbf = (((unsigned int) Serial.read()) << 8) | ((unsigned int) Serial.read());
+      if (nbf > 0)
+      {
+        //DO NOT OVERFLOW max(nbf): 60
+        while (posbuf < nbf + 1 && Serial.available() > 0)
+        {
+          buffer[posbuf] = Serial.read();
+          posbuf++;
+        }
+        if (posbuf >= nbf + 1)
+        {
+          manager.buzz(nbf, buffer);
+          posbuf = 0;
+          command = 0;
+          nbf = 0;
+        }
+      }
+      break;
+    //stop any vibration
+    case 'A':
+      manager.stop();
+      break;
     //unknown command: do nothing
     default:
       command = 0;