From 607ee8b9b82f6a7b56f7ea127e5a66cf79eafe8a Mon Sep 17 00:00:00 2001 From: Thomas Pietrzak Date: Fri, 27 Jul 2012 14:35:36 +0000 Subject: [PATCH] Add buzz functionality: play different amplitudes with no time limit. git-svn-id: svn+ssh://thomaspietrzak.com/var/svn/rep@76 47cf9a05-e0a8-4ed5-9e9b-101a649bc004 --- Arduino/TactonPlayer/TactonManager.cpp | 18 ++++++++++- Arduino/TactonPlayer/TactonManager.h | 4 +++ Arduino/TactonPlayer/TactonPlayer.h | 9 +++++- .../TactonPlayer/TactonPlayerPreciseNew.cpp | 27 ++++++++++++++++ Arduino/TactonPlayer/TactonPlayerPreciseNew.h | 9 +++++- Arduino/wristbandTactons/wristbandTactons.ino | 31 ++++++++++++++++++- 6 files changed, 94 insertions(+), 4 deletions(-) diff --git a/Arduino/TactonPlayer/TactonManager.cpp b/Arduino/TactonPlayer/TactonManager.cpp index a0d315b..6bee80a 100644 --- a/Arduino/TactonPlayer/TactonManager.cpp +++ b/Arduino/TactonPlayer/TactonManager.cpp @@ -116,4 +116,20 @@ void TactonManager::checkPlay() }*/ } - \ No newline at end of file +void TactonManager::buzz(unsigned int nbf, byte *desc) +{ + byte *amplitudes = (byte *)malloc(nbf * sizeof(byte)); + if (amplitudes == NULL) + return; + + for (unsigned int i = 0 ; i < nbf ; i++) + amplitudes[i] = desc[i + 1]; + + _player->buzz(desc[0], nbf, amplitudes); + free(amplitudes); +} + +void TactonManager::stop() +{ + _player->stop(); +} diff --git a/Arduino/TactonPlayer/TactonManager.h b/Arduino/TactonPlayer/TactonManager.h index b37701f..240a2ac 100644 --- a/Arduino/TactonPlayer/TactonManager.h +++ b/Arduino/TactonPlayer/TactonManager.h @@ -22,6 +22,10 @@ class TactonManager void addPlay(byte index, unsigned long timestamp); void checkPlay(); void setOffset(unsigned long timestamp); + + void buzz(unsigned int nbf, byte *desc); + void stop(); + void clear(); diff --git a/Arduino/TactonPlayer/TactonPlayer.h b/Arduino/TactonPlayer/TactonPlayer.h index 28fed8d..1c12f41 100644 --- a/Arduino/TactonPlayer/TactonPlayer.h +++ b/Arduino/TactonPlayer/TactonPlayer.h @@ -12,7 +12,14 @@ class TactonPlayer { public: TactonPlayer(byte nbtactors, byte *pins); - + + //Same frequency for all the vibrators, different amplitude. + //Vibrates until stop + void buzz(unsigned int frequency, byte nbtactors, byte amplitudes[]); + + //Stop any vibration + void stop(); + //8bits pattern => max 8 tactors, change type if using more virtual void beep(byte pattern, unsigned long duration, unsigned int frequency, byte amplitude) = 0; /* diff --git a/Arduino/TactonPlayer/TactonPlayerPreciseNew.cpp b/Arduino/TactonPlayer/TactonPlayerPreciseNew.cpp index ac92dd6..55550cd 100644 --- a/Arduino/TactonPlayer/TactonPlayerPreciseNew.cpp +++ b/Arduino/TactonPlayer/TactonPlayerPreciseNew.cpp @@ -57,6 +57,33 @@ void TactonPlayerPreciseNew::setFrequency(unsigned int frequency) Serial.println(_ccper256cv, DEC);*/ } +//Start a vibration of a given frequency, with different amplitudes for each vibrator +void TactonPlayerPreciseNew::buzz(unsigned int frequency, byte nbtactors, byte *amplitudes) +{ + if (nbtactors != _nbtactors) + return; + + sei(); + init(); + + //set the amplitudes + for (int i = 0 ; i < _nbtactors ; i++) + analogWrite(_pins[i], amplitudes[i]); +} + +//Stop any vibration +void TactonPlayerPreciseNew::stop() +{ + TCCR1A = 0; + //set duty cycle to 0 + OCR1A = 0; + OCR1B = 0; + //clear the pattern + for (int i = 0 ; i < _nbtactors ; i++) + digitalWrite(_pins[i], LOW); +} + +//Play a Tacton for a specified duration, frequency and amplitude void TactonPlayerPreciseNew::beep(byte pattern, unsigned long duration, unsigned int frequency, byte amplitude) { sei(); diff --git a/Arduino/TactonPlayer/TactonPlayerPreciseNew.h b/Arduino/TactonPlayer/TactonPlayerPreciseNew.h index 03bcbac..80d1005 100644 --- a/Arduino/TactonPlayer/TactonPlayerPreciseNew.h +++ b/Arduino/TactonPlayer/TactonPlayerPreciseNew.h @@ -9,7 +9,14 @@ class TactonPlayerPreciseNew: public TactonPlayer { public: TactonPlayerPreciseNew(byte nbtactors, byte *pins, byte pwmPin); - + + //Same frequency for all the vibrators, different amplitude. + //Vibrates until stop + void TactonPlayerPreciseNew::buzz(unsigned int frequency, byte nbtactors, byte *amplitudes); + + //Stop any vibration + void stop(); + //8bits pattern => max 8 tactors, change type if using more void beep(byte pattern, unsigned long duration, unsigned int frequency, byte amplitude); diff --git a/Arduino/wristbandTactons/wristbandTactons.ino b/Arduino/wristbandTactons/wristbandTactons.ino index 658eeea..014691a 100644 --- a/Arduino/wristbandTactons/wristbandTactons.ino +++ b/Arduino/wristbandTactons/wristbandTactons.ino @@ -2,7 +2,7 @@ #include #include 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; -- 2.30.2