From d649742c41fb619ba3ea5eb42069368fd67dc05f Mon Sep 17 00:00:00 2001 From: Thomas Pietrzak Date: Wed, 3 Aug 2011 18:28:19 +0000 Subject: [PATCH] New firmwares, with two kind of versions git-svn-id: svn+ssh://thomaspietrzak.com/var/svn/rep@41 47cf9a05-e0a8-4ed5-9e9b-101a649bc004 --- Arduino/Tacton/keywords.txt | 3 - Arduino/TactonManager/keywords.txt | 7 - Arduino/{Tacton => TactonPlayer}/Tacton.cpp | 4 +- Arduino/{Tacton => TactonPlayer}/Tacton.h | 2 +- .../TactonManager.cpp | 10 + .../TactonManager.h | 0 Arduino/TactonPlayer/TactonPlayer.cpp | 43 +--- Arduino/TactonPlayer/TactonPlayer.h | 26 ++- Arduino/TactonPlayer/TactonPlayerPrecise.cpp | 201 ++++++++++++++++++ Arduino/TactonPlayer/TactonPlayerPrecise.h | 32 +++ .../TactonPlayerWithAmplitude.cpp | 71 +++++++ .../TactonPlayer/TactonPlayerWithAmplitude.h | 19 ++ Arduino/TactonPlayer/keywords.txt | 14 +- Arduino/wristbandTactons/wristbandTactons.pde | 19 +- 14 files changed, 384 insertions(+), 67 deletions(-) delete mode 100644 Arduino/Tacton/keywords.txt delete mode 100644 Arduino/TactonManager/keywords.txt rename Arduino/{Tacton => TactonPlayer}/Tacton.cpp (91%) rename Arduino/{Tacton => TactonPlayer}/Tacton.h (84%) rename Arduino/{TactonManager => TactonPlayer}/TactonManager.cpp (88%) rename Arduino/{TactonManager => TactonPlayer}/TactonManager.h (100%) create mode 100644 Arduino/TactonPlayer/TactonPlayerPrecise.cpp create mode 100644 Arduino/TactonPlayer/TactonPlayerPrecise.h create mode 100644 Arduino/TactonPlayer/TactonPlayerWithAmplitude.cpp create mode 100644 Arduino/TactonPlayer/TactonPlayerWithAmplitude.h diff --git a/Arduino/Tacton/keywords.txt b/Arduino/Tacton/keywords.txt deleted file mode 100644 index 4cc4b36..0000000 --- a/Arduino/Tacton/keywords.txt +++ /dev/null @@ -1,3 +0,0 @@ -Tacton KEYWORD1 -play KEYWORD2 -isValid KEYWORD2 \ No newline at end of file diff --git a/Arduino/TactonManager/keywords.txt b/Arduino/TactonManager/keywords.txt deleted file mode 100644 index ca231bc..0000000 --- a/Arduino/TactonManager/keywords.txt +++ /dev/null @@ -1,7 +0,0 @@ -TactonManager KEYWORD1 -get KEYWORD2 -add KEYWORD2 -clear KEYWORD2 -play KEYWORD2 -addPlay KEYWORD2 -checkPlay KEYWORD2 \ No newline at end of file diff --git a/Arduino/Tacton/Tacton.cpp b/Arduino/TactonPlayer/Tacton.cpp similarity index 91% rename from Arduino/Tacton/Tacton.cpp rename to Arduino/TactonPlayer/Tacton.cpp index 259b30a..efa41f7 100644 --- a/Arduino/Tacton/Tacton.cpp +++ b/Arduino/TactonPlayer/Tacton.cpp @@ -25,9 +25,11 @@ Tacton::~Tacton() free(_amplitudes); } -void Tacton::play(const TactonPlayer &player) const +void Tacton::play(TactonPlayer &player) const { if (isValid()) + { for (int i = 0 ; i < _nbframes ; i++) player.beep(_patterns[i], (long)_durations[i], _frequencies[i], _amplitudes[i]); + } } diff --git a/Arduino/Tacton/Tacton.h b/Arduino/TactonPlayer/Tacton.h similarity index 84% rename from Arduino/Tacton/Tacton.h rename to Arduino/TactonPlayer/Tacton.h index 8b9b289..cc1bf57 100644 --- a/Arduino/Tacton/Tacton.h +++ b/Arduino/TactonPlayer/Tacton.h @@ -11,7 +11,7 @@ class Tacton Tacton(unsigned int nbframes, byte *desc); ~Tacton(); - void play(const TactonPlayer &player) const; + void play(TactonPlayer &player) const; boolean isValid() const { return _patterns && _durations && _frequencies && _amplitudes; } diff --git a/Arduino/TactonManager/TactonManager.cpp b/Arduino/TactonPlayer/TactonManager.cpp similarity index 88% rename from Arduino/TactonManager/TactonManager.cpp rename to Arduino/TactonPlayer/TactonManager.cpp index 91e7093..f630288 100644 --- a/Arduino/TactonManager/TactonManager.cpp +++ b/Arduino/TactonPlayer/TactonManager.cpp @@ -1,6 +1,16 @@ #include "WProgram.h" #include "TactonManager.h" +void* operator new(size_t n, void * p) { + return p; +} +void* operator new(size_t n) { + return malloc(n); +} +void operator delete (void * p) { + free(p); +}; + TactonManager::TactonManager(TactonPlayer *player) : _nbplays(0), _nbtactons(0), _player(player) { diff --git a/Arduino/TactonManager/TactonManager.h b/Arduino/TactonPlayer/TactonManager.h similarity index 100% rename from Arduino/TactonManager/TactonManager.h rename to Arduino/TactonPlayer/TactonManager.h diff --git a/Arduino/TactonPlayer/TactonPlayer.cpp b/Arduino/TactonPlayer/TactonPlayer.cpp index 731bfcb..c88455c 100644 --- a/Arduino/TactonPlayer/TactonPlayer.cpp +++ b/Arduino/TactonPlayer/TactonPlayer.cpp @@ -2,41 +2,14 @@ #include "TactonPlayer.h" TactonPlayer::TactonPlayer(byte nbtactors, byte *pins) -:_nbtactors(nbtactors), _pins(pins) { - for (int i = 0 ; i < nbtactors ; i++) - pinMode(pins[i], OUTPUT); + _nbtactors = min(MAXTACTORS, nbtactors); + _pins = pins; + + for (int i = 0 ; i < _nbtactors ; i++) + { + pinMode(pins[i], OUTPUT); + } } -void TactonPlayer::beep(byte pattern, long duration, unsigned int frequency, byte amplitude) const -{ - int i; - long del = (long)(1000000 / ((long)frequency)); - long looptime = (long)((duration * 1000) / (del * 2)); - long udel = del % 1000; - long mdel = del / 1000; - for (i = 0 ; i < looptime ; i++) - { - for (int j = 0 ; j < 8 ; j++) - if (j < _nbtactors && (pattern & (0x01 << j))) - analogWrite(_pins[j], amplitude); - //delayMicroseconds is not accurate over 16383µs - if (del > 10000) - { - delayMicroseconds(udel); - delay(mdel); - } - else - delayMicroseconds(del); - for (int j = 0 ; j < 8 ; j++) - if (j < _nbtactors && (pattern & (0x01 << j))) - analogWrite(_pins[j], 0); - if (del > 10000) - { - delayMicroseconds(udel); - delay(mdel); - } - else - delayMicroseconds(del); - } -} +void __cxa_pure_virtual(void) {}; \ No newline at end of file diff --git a/Arduino/TactonPlayer/TactonPlayer.h b/Arduino/TactonPlayer/TactonPlayer.h index 2fa99b2..a5283dc 100644 --- a/Arduino/TactonPlayer/TactonPlayer.h +++ b/Arduino/TactonPlayer/TactonPlayer.h @@ -3,22 +3,38 @@ #include "WProgram.h" +//if using more than 8, change type of pattern +#define MAXTACTORS 8 + +extern "C" void __cxa_pure_virtual(void); + class TactonPlayer { public: TactonPlayer(byte nbtactors, byte *pins); - + //8bits pattern => max 8 tactors, change type if using more - void beep(byte pattern, long duration, unsigned int frequency, byte amplitude) const; - + virtual void beep(byte pattern, unsigned long duration, unsigned int frequency, byte amplitude) = 0; +/* void debug1() { analogWrite(_pins[0], 255); delay(100); analogWrite(_pins[0], 0); delay(100);} void debug2() { analogWrite(_pins[1], 255); delay(100); analogWrite(_pins[1], 0); delay(100); } void debug3() { analogWrite(_pins[2], 255); delay(100); analogWrite(_pins[2], 0); delay(100); } void debug4() { analogWrite(_pins[3], 255); delay(100); analogWrite(_pins[3], 0); delay(100); } - - private: + */ + protected: + virtual void init() const {} + + virtual void setFrequency(unsigned int frequency) { _frequency = frequency; } + virtual void setDuration(unsigned long duration) { _duration = duration; } + virtual void setAmplitude(byte amplitude) { _amplitude = amplitude; } + virtual void setPattern(byte pattern) { _pattern = pattern; } + byte _nbtactors; byte *_pins; + byte _pattern; + unsigned long _duration; + unsigned int _frequency; + byte _amplitude; }; #endif diff --git a/Arduino/TactonPlayer/TactonPlayerPrecise.cpp b/Arduino/TactonPlayer/TactonPlayerPrecise.cpp new file mode 100644 index 0000000..c0bc127 --- /dev/null +++ b/Arduino/TactonPlayer/TactonPlayerPrecise.cpp @@ -0,0 +1,201 @@ +#include "WProgram.h" +#include "TactonPlayerPrecise.h" + +#include "pins_arduino.h" + +volatile unsigned long _toggle_count; +//volatile bool _active = false; + +/* +volatile unsigned long _ccper256cv; // (F_CPU) / (512 * _frequency) +volatile unsigned long _cv; // (F_CPU * _duration) / (256000 * _ccper256cv) +volatile unsigned long _currentcvi; // 0 <= _ccper256cv <= _ccper256cv +volatile unsigned long _currentcv; // 0 <= _currentcv <= _cv +volatile bool _currentstate; +*/ + +uint8_t TactonPlayerPrecise::_portB = 0; +uint8_t TactonPlayerPrecise::_portC = 0; +uint8_t TactonPlayerPrecise::_portD = 0; + + +TactonPlayerPrecise::TactonPlayerPrecise(byte nbtactors, byte *pins) +:TactonPlayer(nbtactors, pins) +{ +} + +void TactonPlayerPrecise::init() const +{ + //init timers + TIMSK1 = 0; + TCNT1 = 0; + TCCR1A = 0; // CTC mode + TCCR1B = (1 << WGM12) | (1 << CS10); +} + +void TactonPlayerPrecise::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 TactonPlayerPrecise::setDuration(unsigned long duration) +{ + TactonPlayer::setDuration(duration); + _toggle_count = 2 * _frequency * _duration / 1000; + +/* _cv = (F_CPU / 1000 * _duration) / 256 / _ccper256cv; + _currentcv = 0; + +/* Serial.print("_duration="); + Serial.print(_duration, DEC); + Serial.print(" _cv="); + Serial.println(_cv, DEC);*/ +} + +void TactonPlayerPrecise::setAmplitude(byte amplitude) +{ + TactonPlayer::setAmplitude(amplitude); +// OCR2A = amplitude; + +/* Serial.print("_amplitude="); + Serial.print(_amplitude, DEC);*/ +} + +void TactonPlayerPrecise::setPattern(byte pattern) +{ + TactonPlayer::setPattern(pattern); + + _portB = _portC = _portD = 0; + for (int i = 0 ; i < _nbtactors ; i++) + { + pinMode(_pins[i], OUTPUT); + volatile uint8_t * port = portOutputRegister(digitalPinToPort(_pins[i])); + if (_pattern & (1 << i)) + { + if (port == &PORTB) + _portB |= digitalPinToBitMask(_pins[i]); + else if (port == &PORTC) + _portC |= digitalPinToBitMask(_pins[i]); + else if (port == &PORTD) + _portD |= digitalPinToBitMask(_pins[i]); + } + } +} + +void TactonPlayerPrecise::tooglePins() +{ + PORTB ^= _portB; + PORTC ^= _portC; + PORTD ^= _portD; +} + +void TactonPlayerPrecise::turnOnPins() +{ + PORTB |= _portB; + PORTC |= _portC; + PORTD |= _portD; +} + +void TactonPlayerPrecise::turnOffPins() +{ + PORTB &= ~_portB; + PORTC &= ~_portC; + PORTD &= ~_portD; +} + +void TactonPlayerPrecise::beep(byte pattern, unsigned long duration, unsigned int frequency, byte amplitude) +{ + init(); + setFrequency(frequency); +// setAmplitude(amplitude); + setDuration(duration); + setPattern(pattern); + //_active = true; + turnOnPins(); + TIMSK1 = (1 << OCIE1A); +// TIMSK2 = (1 << OCIE2A) | (1 << TOIE2); +// TIMSK2 = (1 << TOIE2); + sei(); +} + + +ISR(TIMER1_COMPA_vect) +{ + if (_toggle_count > 0) + { + _toggle_count--; + TactonPlayerPrecise::tooglePins(); + } + else + { + TIMSK1 &= ~(1 << OCIE1A); + TactonPlayerPrecise::turnOffPins(); + } +} + +/* +ISR(TIMER2_OVF_vect) +{ + _currentcvi++; + if (_currentcvi >= _ccper256cv) + { + _currentstate = !_currentstate; + _currentcvi = 0; + _currentcv++; + if (_currentcv >= _cv) + { + //TIMSK2 &= ~(1 << TOIE2); + TIMSK2 &= ~((1 << OCIE2A) | (1 << TOIE2)); + TactonPlayer::turnOffPins(); + return; + } + } + if (_currentstate) + TactonPlayer::tooglePins(); + +// count1++; +// if (_active) +// { +// TactonPlayer::tooglePins(); +// count2++; +// } +// else +// TactonPlayer::turnOffPins(); +} + +ISR(TIMER2_COMPA_vect) +{ +// if (_active) + if (_currentstate) + TactonPlayer::tooglePins(); +// else +// TactonPlayer::turnOffPins(); +} +*/ diff --git a/Arduino/TactonPlayer/TactonPlayerPrecise.h b/Arduino/TactonPlayer/TactonPlayerPrecise.h new file mode 100644 index 0000000..4a1fa9e --- /dev/null +++ b/Arduino/TactonPlayer/TactonPlayerPrecise.h @@ -0,0 +1,32 @@ +#ifndef _TACTONPLAYERPRECISE_ +#define _TACTONPLAYERPRECISE_ + +#include "WProgram.h" + +#include + +class TactonPlayerPrecise: public TactonPlayer +{ + public: + TactonPlayerPrecise(byte nbtactors, byte *pins); + + //8bits pattern => max 8 tactors, change type if using more + void beep(byte pattern, unsigned long duration, unsigned int frequency, byte amplitude); + + inline static void tooglePins(); + inline static void turnOnPins(); + inline static void turnOffPins(); + + private: + void init() const; + void setFrequency(unsigned int frequency); + void setDuration(unsigned long duration); + void setAmplitude(byte amplitude); + void setPattern(byte pattern); + + static uint8_t _portB; + static uint8_t _portC; + static uint8_t _portD; +}; + +#endif diff --git a/Arduino/TactonPlayer/TactonPlayerWithAmplitude.cpp b/Arduino/TactonPlayer/TactonPlayerWithAmplitude.cpp new file mode 100644 index 0000000..9c4a98a --- /dev/null +++ b/Arduino/TactonPlayer/TactonPlayerWithAmplitude.cpp @@ -0,0 +1,71 @@ +#include "WProgram.h" +#include "TactonPlayerWithAmplitude.h" + +TactonPlayerWithAmplitude::TactonPlayerWithAmplitude(byte nbtactors, byte *pins) +:TactonPlayer(nbtactors, pins) +{ +} + +void TactonPlayerWithAmplitude::init() const +{ + //set counter 1 as Fast PWM + TCCR1A |= (1 << WGM10); + TCCR1A &= ~(1 << WGM11); + TCCR1B |= (1 << WGM12); + TCCR1B &= ~(1 << WGM13); + //set counter 2 as Fast PWM + TCCR2A |= ((1 << WGM20) & (1 << WGM21)); + TCCR2B &= ~(1 << WGM22); +} + +void TactonPlayerWithAmplitude::beep(byte pattern, unsigned long duration, unsigned int frequency, byte amplitude) +{ + init(); + setFrequency(frequency); + setAmplitude(amplitude); + setDuration(duration); + setPattern(pattern); + + int i; + long del = (long)(1000000 / ((long)frequency)); + long looptime = (long)((duration * 1000) / (del * 2)); + long udel = del % 1000; + long mdel = del / 1000; + + Serial.print("del="); + Serial.print(del); + Serial.print(" udel="); + Serial.print(udel); + Serial.print(" mdel="); + Serial.print(mdel); + Serial.print(" looptime="); + Serial.println(looptime); + + for (i = 0 ; i < looptime ; i++) + { + for (int j = 0 ; j < MAXTACTORS ; j++) + if (j < _nbtactors && (pattern & (1 << j))) + analogWrite(_pins[j], amplitude); + //delayMicroseconds is not accurate over 16383µs + if (del > 16000) + { + delayMicroseconds(udel); + if (mdel > 0) + delay(mdel); + } + else + delayMicroseconds(del); + + for (int j = 0 ; j < MAXTACTORS ; j++) + if (j < _nbtactors && (pattern & (1 << j))) + analogWrite(_pins[j], 0); + if (del > 16000) + { + delayMicroseconds(udel); + if (mdel > 0) + delay(mdel); + } + else + delayMicroseconds(del); + } +} diff --git a/Arduino/TactonPlayer/TactonPlayerWithAmplitude.h b/Arduino/TactonPlayer/TactonPlayerWithAmplitude.h new file mode 100644 index 0000000..fe21abb --- /dev/null +++ b/Arduino/TactonPlayer/TactonPlayerWithAmplitude.h @@ -0,0 +1,19 @@ +#ifndef _TACTONPLAYERWITHAMPLITUDE_ +#define _TACTONPLAYERWITHAMPLITUDE_ + +#include "WProgram.h" + +#include + +class TactonPlayerWithAmplitude: public TactonPlayer +{ + public: + TactonPlayerWithAmplitude(byte nbtactors, byte *pins); + + //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; +}; + +#endif diff --git a/Arduino/TactonPlayer/keywords.txt b/Arduino/TactonPlayer/keywords.txt index 2e2fd22..585ac33 100644 --- a/Arduino/TactonPlayer/keywords.txt +++ b/Arduino/TactonPlayer/keywords.txt @@ -1,6 +1,18 @@ TactonPlayer KEYWORD1 +TactonPlayerPrecise KEYWORD1 +TactonPlayerWithAmplitude KEYWORD1 beep KEYWORD2 debug1 KEYWORD2 debug2 KEYWORD2 debug3 KEYWORD2 -debug4 KEYWORD2 \ No newline at end of file +debug4 KEYWORD2 +Tacton KEYWORD1 +play KEYWORD2 +isValid KEYWORD2 +TactonManager KEYWORD1 +get KEYWORD2 +add KEYWORD2 +clear KEYWORD2 +play KEYWORD2 +addPlay KEYWORD2 +checkPlay KEYWORD2 \ No newline at end of file diff --git a/Arduino/wristbandTactons/wristbandTactons.pde b/Arduino/wristbandTactons/wristbandTactons.pde index 58c35d1..cc8b724 100644 --- a/Arduino/wristbandTactons/wristbandTactons.pde +++ b/Arduino/wristbandTactons/wristbandTactons.pde @@ -1,21 +1,11 @@ #include -#include +#include #include byte pins[] = { 3, 11, 5, 9}; -TactonPlayer player(4, pins); +TactonPlayerWithAmplitude player(4, pins); TactonManager manager(&player); -void* operator new(size_t n, void * p) { - return p; -} -void* operator new(size_t n) { - return malloc(n); -} -void operator delete (void * p) { - free(p); -}; - byte command = 0; byte posbuf = 0; unsigned int nbf = 0; @@ -26,10 +16,11 @@ boolean active = false; void setup() { Serial.begin(57600); - player.debug1(); +// player.init(); +/* player.debug1(); player.debug2(); player.debug3(); - player.debug4(); + player.debug4();*/ } void loop() -- 2.30.2