From: Thomas Pietrzak Date: Wed, 3 Aug 2011 18:28:19 +0000 (+0000) Subject: New firmwares, with two kind of versions X-Git-Url: https://git.thomaspietrzak.com/?a=commitdiff_plain;h=d649742c41fb619ba3ea5eb42069368fd67dc05f;p=tactonlibrary.git New firmwares, with two kind of versions git-svn-id: svn+ssh://thomaspietrzak.com/var/svn/rep@41 47cf9a05-e0a8-4ed5-9e9b-101a649bc004 --- diff --git a/Arduino/Tacton/Tacton.cpp b/Arduino/Tacton/Tacton.cpp deleted file mode 100644 index 259b30a..0000000 --- a/Arduino/Tacton/Tacton.cpp +++ /dev/null @@ -1,33 +0,0 @@ -#include "WProgram.h" -#include "Tacton.h" - - -Tacton::Tacton(unsigned int nbframes, byte *desc) -:_nbframes(nbframes), _patterns((byte *)malloc(nbframes * sizeof(byte))), _durations((unsigned int *)malloc(nbframes * sizeof(unsigned int))), _frequencies((unsigned int *)malloc(nbframes * sizeof(unsigned int))), _amplitudes((byte *)malloc(nbframes * sizeof(byte))) -{ - if (isValid()) - { - for(int i = 0 ; i < nbframes ; i++) - { - _patterns[i] = desc[6*i]; - _durations[i] = (((unsigned int)(desc[6*i+1])) << 8) | ((unsigned int)(desc[6*i+2])); - _frequencies[i] = (((unsigned int)(desc[6*i+3])) << 8) | ((unsigned int)(desc[6*i+4])); - _amplitudes[i] = desc[6*i+5]; - } - } -} - -Tacton::~Tacton() -{ - free(_patterns); - free(_durations); - free(_frequencies); - free(_amplitudes); -} - -void Tacton::play(const 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/Tacton/Tacton.h deleted file mode 100644 index 8b9b289..0000000 --- a/Arduino/Tacton/Tacton.h +++ /dev/null @@ -1,26 +0,0 @@ -#ifndef _TACTON_ -#define _TACTON_ - -#include "WProgram.h" - -#include - -class Tacton -{ - public: - Tacton(unsigned int nbframes, byte *desc); - ~Tacton(); - - void play(const TactonPlayer &player) const; - - boolean isValid() const { return _patterns && _durations && _frequencies && _amplitudes; } - - private: - unsigned int _nbframes; - byte *_patterns; - unsigned int *_durations; - unsigned int *_frequencies; - byte *_amplitudes; -}; - -#endif 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/TactonManager.cpp b/Arduino/TactonManager/TactonManager.cpp deleted file mode 100644 index 91e7093..0000000 --- a/Arduino/TactonManager/TactonManager.cpp +++ /dev/null @@ -1,109 +0,0 @@ -#include "WProgram.h" -#include "TactonManager.h" - -TactonManager::TactonManager(TactonPlayer *player) -: _nbplays(0), _nbtactons(0), _player(player) -{ -} - -void TactonManager::add(unsigned int nbframes, byte *desc) -{ - if (_nbtactons < MAXTACTONS) - { - _tactons[_nbtactons] = new Tacton(nbframes, desc); - if (_tactons[_nbtactons]) - { - Serial.println("Tacton added"); - _nbtactons++; - } - } -} - -const Tacton *TactonManager::get(byte numtacton) const -{ - if (numtacton < _nbtactons) - return _tactons[numtacton]; - else - return NULL; -} - -void TactonManager::clear() -{ - for (int i = 0 ; i < _nbtactons ; i++) - delete _tactons[i]; - _nbtactons = 0; - _nbplays = 0; -} - -void TactonManager::addPlay(byte index, unsigned long timestamp) -{ - if (index >= _nbtactons || _nbplays >= MAXPLAYBUFFER) - return; - _playindex[_nbplays] = index; - _playtimestamps[_nbplays] = timestamp; - _nbplays++; -} - -void TactonManager::play(byte index) -{ - if (index < _nbtactons) - _tactons[index]->play(*_player); -} - -void TactonManager::play(unsigned int nbframes, byte *desc) -{ - Tacton t(nbframes, desc); - t.play(*_player); -} - -void TactonManager::setOffset(unsigned long timestamp) -{ - _offset = timestamp; -} - -void TactonManager::checkPlay() -{ - unsigned long now = millis() - _offset; -/* if (_nbplays == 0 || _playtimestamps[0] > now) - return; - Serial.print("Play at "); - Serial.println(now, DEC); - play(_playindex[0]); - //shift other plays - for (int i = 0 ; i < _nbplays ; i++) - { - _playindex[i] = _playindex[i + i]; - _playtimestamps[i] = _playtimestamps[i + i]; - } - _nbplays--;*/ - - int i = 0, j = 0; - while (_nbplays > 0 && i < MAXPLAYBUFFER && _playtimestamps[i] < now) - { - //TEST - Serial.print("Play "); - Serial.println(_playtimestamps[i], DEC); - Serial.print(" at "); - Serial.println(now, DEC); - // - play(_playindex[i]); - i++; - _nbplays--; - } - if (i == 0) - return; - //shift not played tactons - for (j = 0 ; i + j < MAXPLAYBUFFER ; j++) - { - _playindex[j] = _playindex[j + i]; - _playtimestamps[j] = _playtimestamps[j + i]; - } - //erase shifted values -/* for (i = j ; i < MAXPLAYBUFFER ; i++) - { - _playindex[i] = 0xff; - _playtimestamps[i] = 0xffffffff; - }*/ -} - - \ No newline at end of file diff --git a/Arduino/TactonManager/TactonManager.h b/Arduino/TactonManager/TactonManager.h deleted file mode 100644 index 32a3bc7..0000000 --- a/Arduino/TactonManager/TactonManager.h +++ /dev/null @@ -1,38 +0,0 @@ -#ifndef _TACTONMANAGER_ -#define _TACTONMANAGER_ - -#include "WProgram.h" - -#include -#include - -#define MAXTACTONS 16 -#define MAXPLAYBUFFER 10 - -class TactonManager -{ - public: - TactonManager(TactonPlayer *player); - - void add(unsigned int nbframes, byte *desc); - const Tacton *get(byte numtacton) const; - - void play(byte index); - void play(unsigned int nbframes, byte *desc); - void addPlay(byte index, unsigned long timestamp); - void checkPlay(); - void setOffset(unsigned long timestamp); - - void clear(); - - private: - byte _nbplays; - byte _nbtactons; - Tacton *_tactons[MAXTACTONS]; - byte _playindex[MAXPLAYBUFFER]; - unsigned long _playtimestamps[MAXPLAYBUFFER]; - TactonPlayer *_player; - unsigned long _offset; -}; - -#endif 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/TactonPlayer/Tacton.cpp b/Arduino/TactonPlayer/Tacton.cpp new file mode 100644 index 0000000..efa41f7 --- /dev/null +++ b/Arduino/TactonPlayer/Tacton.cpp @@ -0,0 +1,35 @@ +#include "WProgram.h" +#include "Tacton.h" + + +Tacton::Tacton(unsigned int nbframes, byte *desc) +:_nbframes(nbframes), _patterns((byte *)malloc(nbframes * sizeof(byte))), _durations((unsigned int *)malloc(nbframes * sizeof(unsigned int))), _frequencies((unsigned int *)malloc(nbframes * sizeof(unsigned int))), _amplitudes((byte *)malloc(nbframes * sizeof(byte))) +{ + if (isValid()) + { + for(int i = 0 ; i < nbframes ; i++) + { + _patterns[i] = desc[6*i]; + _durations[i] = (((unsigned int)(desc[6*i+1])) << 8) | ((unsigned int)(desc[6*i+2])); + _frequencies[i] = (((unsigned int)(desc[6*i+3])) << 8) | ((unsigned int)(desc[6*i+4])); + _amplitudes[i] = desc[6*i+5]; + } + } +} + +Tacton::~Tacton() +{ + free(_patterns); + free(_durations); + free(_frequencies); + free(_amplitudes); +} + +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/TactonPlayer/Tacton.h b/Arduino/TactonPlayer/Tacton.h new file mode 100644 index 0000000..cc1bf57 --- /dev/null +++ b/Arduino/TactonPlayer/Tacton.h @@ -0,0 +1,26 @@ +#ifndef _TACTON_ +#define _TACTON_ + +#include "WProgram.h" + +#include + +class Tacton +{ + public: + Tacton(unsigned int nbframes, byte *desc); + ~Tacton(); + + void play(TactonPlayer &player) const; + + boolean isValid() const { return _patterns && _durations && _frequencies && _amplitudes; } + + private: + unsigned int _nbframes; + byte *_patterns; + unsigned int *_durations; + unsigned int *_frequencies; + byte *_amplitudes; +}; + +#endif diff --git a/Arduino/TactonPlayer/TactonManager.cpp b/Arduino/TactonPlayer/TactonManager.cpp new file mode 100644 index 0000000..f630288 --- /dev/null +++ b/Arduino/TactonPlayer/TactonManager.cpp @@ -0,0 +1,119 @@ +#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) +{ +} + +void TactonManager::add(unsigned int nbframes, byte *desc) +{ + if (_nbtactons < MAXTACTONS) + { + _tactons[_nbtactons] = new Tacton(nbframes, desc); + if (_tactons[_nbtactons]) + { + Serial.println("Tacton added"); + _nbtactons++; + } + } +} + +const Tacton *TactonManager::get(byte numtacton) const +{ + if (numtacton < _nbtactons) + return _tactons[numtacton]; + else + return NULL; +} + +void TactonManager::clear() +{ + for (int i = 0 ; i < _nbtactons ; i++) + delete _tactons[i]; + _nbtactons = 0; + _nbplays = 0; +} + +void TactonManager::addPlay(byte index, unsigned long timestamp) +{ + if (index >= _nbtactons || _nbplays >= MAXPLAYBUFFER) + return; + _playindex[_nbplays] = index; + _playtimestamps[_nbplays] = timestamp; + _nbplays++; +} + +void TactonManager::play(byte index) +{ + if (index < _nbtactons) + _tactons[index]->play(*_player); +} + +void TactonManager::play(unsigned int nbframes, byte *desc) +{ + Tacton t(nbframes, desc); + t.play(*_player); +} + +void TactonManager::setOffset(unsigned long timestamp) +{ + _offset = timestamp; +} + +void TactonManager::checkPlay() +{ + unsigned long now = millis() - _offset; +/* if (_nbplays == 0 || _playtimestamps[0] > now) + return; + Serial.print("Play at "); + Serial.println(now, DEC); + play(_playindex[0]); + //shift other plays + for (int i = 0 ; i < _nbplays ; i++) + { + _playindex[i] = _playindex[i + i]; + _playtimestamps[i] = _playtimestamps[i + i]; + } + _nbplays--;*/ + + int i = 0, j = 0; + while (_nbplays > 0 && i < MAXPLAYBUFFER && _playtimestamps[i] < now) + { + //TEST + Serial.print("Play "); + Serial.println(_playtimestamps[i], DEC); + Serial.print(" at "); + Serial.println(now, DEC); + // + play(_playindex[i]); + i++; + _nbplays--; + } + if (i == 0) + return; + //shift not played tactons + for (j = 0 ; i + j < MAXPLAYBUFFER ; j++) + { + _playindex[j] = _playindex[j + i]; + _playtimestamps[j] = _playtimestamps[j + i]; + } + //erase shifted values +/* for (i = j ; i < MAXPLAYBUFFER ; i++) + { + _playindex[i] = 0xff; + _playtimestamps[i] = 0xffffffff; + }*/ +} + + \ No newline at end of file diff --git a/Arduino/TactonPlayer/TactonManager.h b/Arduino/TactonPlayer/TactonManager.h new file mode 100644 index 0000000..32a3bc7 --- /dev/null +++ b/Arduino/TactonPlayer/TactonManager.h @@ -0,0 +1,38 @@ +#ifndef _TACTONMANAGER_ +#define _TACTONMANAGER_ + +#include "WProgram.h" + +#include +#include + +#define MAXTACTONS 16 +#define MAXPLAYBUFFER 10 + +class TactonManager +{ + public: + TactonManager(TactonPlayer *player); + + void add(unsigned int nbframes, byte *desc); + const Tacton *get(byte numtacton) const; + + void play(byte index); + void play(unsigned int nbframes, byte *desc); + void addPlay(byte index, unsigned long timestamp); + void checkPlay(); + void setOffset(unsigned long timestamp); + + void clear(); + + private: + byte _nbplays; + byte _nbtactons; + Tacton *_tactons[MAXTACTONS]; + byte _playindex[MAXPLAYBUFFER]; + unsigned long _playtimestamps[MAXPLAYBUFFER]; + TactonPlayer *_player; + unsigned long _offset; +}; + +#endif 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()