From: Thomas Pietrzak Date: Thu, 29 Mar 2012 12:19:00 +0000 (+0000) Subject: Wristband v3 X-Git-Url: https://git.thomaspietrzak.com/?a=commitdiff_plain;h=e6fa431a3d754d98879cac3d518ec682c8c312ef;p=tactonlibrary.git Wristband v3 git-svn-id: svn+ssh://thomaspietrzak.com/var/svn/rep@69 47cf9a05-e0a8-4ed5-9e9b-101a649bc004 --- diff --git a/Arduino/TactonPlayer/TactonPlayerPreciseNew.cpp b/Arduino/TactonPlayer/TactonPlayerPreciseNew.cpp new file mode 100644 index 0000000..cdfa842 --- /dev/null +++ b/Arduino/TactonPlayer/TactonPlayerPreciseNew.cpp @@ -0,0 +1,84 @@ +#include "Arduino.h" +#include "TactonPlayerPreciseNew.h" + +TactonPlayerPreciseNew::TactonPlayerPreciseNew(byte nbtactors, byte *pins, byte pwmPin) +:TactonPlayer(nbtactors, pins), _pwmPin(pwmPin) +{ + pinMode(_pwmPin, OUTPUT); + init(); +} + +void TactonPlayerPreciseNew::init() const +{ + //init PWM timer + TIMSK1 = 0; + TCNT1 = 0; + TCCR1A = 0; // CTC mode + TCCR1B = (1 << WGM12) | (1 << CS10); +} + +void TactonPlayerPreciseNew::setFrequency(unsigned int frequency) +{ + TactonPlayer::setFrequency(frequency); + + unsigned long ocr = F_CPU / _frequency / 2 - 1; + byte prescalarbits = (1 << CS10); + + if (ocr > 0xffff) + { + ocr = F_CPU / _frequency / 64 / 2 - 1; + prescalarbits = (1 << CS11) | (1 << CS10); + } + /* + Serial.print("_frequency="); + Serial.print(_frequency, DEC); + Serial.print(" prescalar="); + Serial.print(prescalarbits, BIN); + Serial.print(" ocr="); + Serial.println(ocr, DEC);*/ + + TCCR1B |= prescalarbits; + OCR1A = ocr; +/* + _ccper256cv = F_CPU / 512 / _frequency; + _currentcvi = 0;*/ + +/* Serial.print("_frequency="); + Serial.print(_frequency, DEC); + Serial.print(" _ccper256cv="); + Serial.println(_ccper256cv, DEC);*/ +} + + +void TactonPlayerPreciseNew::beep(byte pattern, unsigned long duration, unsigned int frequency, byte amplitude) +{ + init(); + setFrequency(frequency); + setAmplitude(amplitude); + setDuration(duration); + setPattern(pattern); + + //set the pattern + for (int i = 0 ; i < _nbtactors ; i++) + { + if (_pattern & (1 << i)) + { + analogWrite(_pins[i], _amplitude); + } + } + + //_active = true; + + //Start the PWM + TIMSK1 = (1 << OCIE1A); + + delay(duration); + + //Stop the PWM + TIMSK1 &= ~(1 << OCIE1A); + +// TIMSK2 = (1 << OCIE2A) | (1 << TOIE2); +// TIMSK2 = (1 << TOIE2); +// sei(); +} + diff --git a/Arduino/TactonPlayer/TactonPlayerPreciseNew.h b/Arduino/TactonPlayer/TactonPlayerPreciseNew.h new file mode 100644 index 0000000..03bcbac --- /dev/null +++ b/Arduino/TactonPlayer/TactonPlayerPreciseNew.h @@ -0,0 +1,26 @@ +#ifndef _TACTONPLAYERPRECISENEW_ +#define _TACTONPLAYERPRECISENEW_ + +#include "Arduino.h" + +#include + +class TactonPlayerPreciseNew: public TactonPlayer +{ + public: + TactonPlayerPreciseNew(byte nbtactors, byte *pins, byte pwmPin); + + //8bits pattern => max 8 tactors, change type if using more + void beep(byte pattern, unsigned long duration, unsigned int frequency, byte amplitude); + + private: + void init() const; + void setFrequency(unsigned int frequency); + //void setDuration(unsigned long duration); + //void setAmplitude(byte amplitude); + //void setPattern(byte pattern); + + byte _pwmPin; +}; + +#endif diff --git a/Arduino/TactonPlayer/keywords.txt b/Arduino/TactonPlayer/keywords.txt index 585ac33..0b379ab 100644 --- a/Arduino/TactonPlayer/keywords.txt +++ b/Arduino/TactonPlayer/keywords.txt @@ -1,5 +1,7 @@ TactonPlayer KEYWORD1 +TactonManager KEYWORD1 TactonPlayerPrecise KEYWORD1 +TactonPlayerPreciseNew KEYWORD1 TactonPlayerWithAmplitude KEYWORD1 beep KEYWORD2 debug1 KEYWORD2