New firmwares, with two kind of versions
authorThomas Pietrzak <thomas.pietrzak@gmail.com>
Wed, 3 Aug 2011 18:28:19 +0000 (18:28 +0000)
committerThomas Pietrzak <thomas.pietrzak@gmail.com>
Wed, 3 Aug 2011 18:28:19 +0000 (18:28 +0000)
git-svn-id: svn+ssh://thomaspietrzak.com/var/svn/rep@41 47cf9a05-e0a8-4ed5-9e9b-101a649bc004

18 files changed:
Arduino/Tacton/Tacton.cpp [deleted file]
Arduino/Tacton/Tacton.h [deleted file]
Arduino/Tacton/keywords.txt [deleted file]
Arduino/TactonManager/TactonManager.cpp [deleted file]
Arduino/TactonManager/TactonManager.h [deleted file]
Arduino/TactonManager/keywords.txt [deleted file]
Arduino/TactonPlayer/Tacton.cpp [new file with mode: 0644]
Arduino/TactonPlayer/Tacton.h [new file with mode: 0644]
Arduino/TactonPlayer/TactonManager.cpp [new file with mode: 0644]
Arduino/TactonPlayer/TactonManager.h [new file with mode: 0644]
Arduino/TactonPlayer/TactonPlayer.cpp
Arduino/TactonPlayer/TactonPlayer.h
Arduino/TactonPlayer/TactonPlayerPrecise.cpp [new file with mode: 0644]
Arduino/TactonPlayer/TactonPlayerPrecise.h [new file with mode: 0644]
Arduino/TactonPlayer/TactonPlayerWithAmplitude.cpp [new file with mode: 0644]
Arduino/TactonPlayer/TactonPlayerWithAmplitude.h [new file with mode: 0644]
Arduino/TactonPlayer/keywords.txt
Arduino/wristbandTactons/wristbandTactons.pde

diff --git a/Arduino/Tacton/Tacton.cpp b/Arduino/Tacton/Tacton.cpp
deleted file mode 100644 (file)
index 259b30a..0000000
+++ /dev/null
@@ -1,33 +0,0 @@
-#include "WProgram.h"\r
-#include "Tacton.h"\r
-\r
-\r
-Tacton::Tacton(unsigned int nbframes, byte *desc)\r
-:_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)))\r
-{\r
-       if (isValid())\r
-       {\r
-               for(int i = 0 ; i < nbframes ; i++)\r
-               {\r
-                       _patterns[i] = desc[6*i];\r
-                       _durations[i] = (((unsigned int)(desc[6*i+1])) << 8) | ((unsigned int)(desc[6*i+2]));\r
-                       _frequencies[i] = (((unsigned int)(desc[6*i+3])) << 8) | ((unsigned int)(desc[6*i+4]));\r
-                       _amplitudes[i] = desc[6*i+5];\r
-               }\r
-       }\r
-}\r
-\r
-Tacton::~Tacton()\r
-{\r
-       free(_patterns);\r
-       free(_durations);\r
-       free(_frequencies);\r
-       free(_amplitudes);\r
-}\r
-               \r
-void Tacton::play(const TactonPlayer &player) const\r
-{            \r
-       if (isValid())\r
-               for (int i = 0 ; i < _nbframes ; i++)\r
-                       player.beep(_patterns[i], (long)_durations[i], _frequencies[i], _amplitudes[i]);\r
-}\r
diff --git a/Arduino/Tacton/Tacton.h b/Arduino/Tacton/Tacton.h
deleted file mode 100644 (file)
index 8b9b289..0000000
+++ /dev/null
@@ -1,26 +0,0 @@
-#ifndef _TACTON_\r
-#define _TACTON_\r
-\r
-#include "WProgram.h"\r
-\r
-#include <TactonPlayer.h>\r
-\r
-class Tacton \r
-{\r
-       public:\r
-               Tacton(unsigned int nbframes, byte *desc);\r
-               ~Tacton();\r
-               \r
-               void play(const TactonPlayer &player) const;\r
-               \r
-               boolean isValid() const { return _patterns && _durations && _frequencies && _amplitudes; }\r
-\r
-       private:\r
-               unsigned int _nbframes;\r
-               byte *_patterns;\r
-               unsigned int *_durations;\r
-               unsigned int *_frequencies;\r
-               byte *_amplitudes;\r
-};\r
-\r
-#endif\r
diff --git a/Arduino/Tacton/keywords.txt b/Arduino/Tacton/keywords.txt
deleted file mode 100644 (file)
index 4cc4b36..0000000
+++ /dev/null
@@ -1,3 +0,0 @@
-Tacton KEYWORD1\r
-play   KEYWORD2\r
-isValid        KEYWORD2
\ No newline at end of file
diff --git a/Arduino/TactonManager/TactonManager.cpp b/Arduino/TactonManager/TactonManager.cpp
deleted file mode 100644 (file)
index 91e7093..0000000
+++ /dev/null
@@ -1,109 +0,0 @@
-#include "WProgram.h"\r
-#include "TactonManager.h"\r
-\r
-TactonManager::TactonManager(TactonPlayer *player)\r
-: _nbplays(0), _nbtactons(0), _player(player)\r
-{\r
-}\r
-\r
-void TactonManager::add(unsigned int nbframes, byte *desc)\r
-{\r
-       if (_nbtactons < MAXTACTONS)\r
-       {\r
-               _tactons[_nbtactons] = new Tacton(nbframes, desc);\r
-               if (_tactons[_nbtactons])\r
-               {\r
-                       Serial.println("Tacton added");\r
-                       _nbtactons++;\r
-               }\r
-       }\r
-}\r
-\r
-const Tacton *TactonManager::get(byte numtacton) const\r
-{\r
-       if (numtacton < _nbtactons)\r
-               return _tactons[numtacton];\r
-       else\r
-               return NULL;\r
-}\r
-\r
-void TactonManager::clear()\r
-{\r
-       for (int i = 0 ; i < _nbtactons ; i++)\r
-               delete _tactons[i];\r
-       _nbtactons = 0;\r
-       _nbplays = 0;\r
-}\r
-\r
-void TactonManager::addPlay(byte index, unsigned long timestamp)\r
-{\r
-       if (index >= _nbtactons || _nbplays >= MAXPLAYBUFFER)\r
-               return;\r
-       _playindex[_nbplays] = index;\r
-       _playtimestamps[_nbplays] = timestamp;\r
-       _nbplays++;     \r
-}\r
-\r
-void TactonManager::play(byte index)\r
-{\r
-       if (index < _nbtactons)\r
-               _tactons[index]->play(*_player);\r
-}\r
-\r
-void TactonManager::play(unsigned int nbframes, byte *desc)\r
-{\r
-       Tacton t(nbframes, desc);\r
-       t.play(*_player);\r
-}\r
-\r
-void TactonManager::setOffset(unsigned long timestamp)\r
-{\r
-       _offset = timestamp;\r
-}\r
-\r
-void TactonManager::checkPlay()\r
-{\r
-       unsigned long now = millis() - _offset;\r
-/*     if (_nbplays == 0 || _playtimestamps[0] > now)\r
-               return;\r
-       Serial.print("Play at ");\r
-       Serial.println(now, DEC);\r
-       play(_playindex[0]);\r
-       //shift other plays\r
-       for (int i = 0 ; i < _nbplays ; i++)\r
-       {\r
-               _playindex[i] = _playindex[i + i];\r
-               _playtimestamps[i] = _playtimestamps[i + i];            \r
-       }\r
-       _nbplays--;*/\r
-\r
-       int i = 0, j = 0;\r
-       while (_nbplays > 0 && i < MAXPLAYBUFFER && _playtimestamps[i] < now)\r
-       {\r
-               //TEST\r
-               Serial.print("Play ");\r
-               Serial.println(_playtimestamps[i], DEC);\r
-               Serial.print(" at ");\r
-               Serial.println(now, DEC);\r
-               //\r
-               play(_playindex[i]);\r
-               i++;\r
-               _nbplays--;\r
-       }\r
-       if (i == 0)\r
-               return;\r
-       //shift not played tactons\r
-       for (j = 0 ; i + j < MAXPLAYBUFFER ; j++)\r
-       {\r
-               _playindex[j] = _playindex[j + i];\r
-               _playtimestamps[j] = _playtimestamps[j + i];            \r
-       }\r
-       //erase shifted values\r
-/*     for (i = j ; i < MAXPLAYBUFFER ; i++)\r
-       {\r
-               _playindex[i] = 0xff;\r
-               _playtimestamps[i] = 0xffffffff;\r
-       }*/\r
-}\r
-\r
-               
\ No newline at end of file
diff --git a/Arduino/TactonManager/TactonManager.h b/Arduino/TactonManager/TactonManager.h
deleted file mode 100644 (file)
index 32a3bc7..0000000
+++ /dev/null
@@ -1,38 +0,0 @@
-#ifndef _TACTONMANAGER_\r
-#define _TACTONMANAGER_\r
-\r
-#include "WProgram.h"\r
-\r
-#include <Tacton.h>\r
-#include <TactonPlayer.h>\r
-\r
-#define MAXTACTONS             16\r
-#define MAXPLAYBUFFER  10\r
-\r
-class TactonManager\r
-{\r
-       public:\r
-               TactonManager(TactonPlayer *player);\r
-               \r
-               void add(unsigned int nbframes, byte *desc);\r
-               const Tacton *get(byte numtacton) const;\r
-               \r
-               void play(byte index);\r
-               void play(unsigned int nbframes, byte *desc);\r
-               void addPlay(byte index, unsigned long timestamp);\r
-               void checkPlay();\r
-               void setOffset(unsigned long timestamp);\r
-\r
-               void clear();\r
-               \r
-       private:\r
-               byte _nbplays;\r
-               byte _nbtactons;\r
-               Tacton *_tactons[MAXTACTONS];\r
-               byte _playindex[MAXPLAYBUFFER];\r
-               unsigned long _playtimestamps[MAXPLAYBUFFER];\r
-               TactonPlayer *_player;\r
-               unsigned long _offset;\r
-};\r
-\r
-#endif\r
diff --git a/Arduino/TactonManager/keywords.txt b/Arduino/TactonManager/keywords.txt
deleted file mode 100644 (file)
index ca231bc..0000000
+++ /dev/null
@@ -1,7 +0,0 @@
-TactonManager  KEYWORD1\r
-get    KEYWORD2\r
-add    KEYWORD2\r
-clear  KEYWORD2\r
-play   KEYWORD2\r
-addPlay        KEYWORD2\r
-checkPlay      KEYWORD2
\ No newline at end of file
diff --git a/Arduino/TactonPlayer/Tacton.cpp b/Arduino/TactonPlayer/Tacton.cpp
new file mode 100644 (file)
index 0000000..efa41f7
--- /dev/null
@@ -0,0 +1,35 @@
+#include "WProgram.h"\r
+#include "Tacton.h"\r
+\r
+\r
+Tacton::Tacton(unsigned int nbframes, byte *desc)\r
+:_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)))\r
+{\r
+       if (isValid())\r
+       {\r
+               for(int i = 0 ; i < nbframes ; i++)\r
+               {\r
+                       _patterns[i] = desc[6*i];\r
+                       _durations[i] = (((unsigned int)(desc[6*i+1])) << 8) | ((unsigned int)(desc[6*i+2]));\r
+                       _frequencies[i] = (((unsigned int)(desc[6*i+3])) << 8) | ((unsigned int)(desc[6*i+4]));\r
+                       _amplitudes[i] = desc[6*i+5];\r
+               }\r
+       }\r
+}\r
+\r
+Tacton::~Tacton()\r
+{\r
+       free(_patterns);\r
+       free(_durations);\r
+       free(_frequencies);\r
+       free(_amplitudes);\r
+}\r
+               \r
+void Tacton::play(TactonPlayer &player) const\r
+{            \r
+       if (isValid())\r
+       {\r
+               for (int i = 0 ; i < _nbframes ; i++)\r
+                       player.beep(_patterns[i], (long)_durations[i], _frequencies[i], _amplitudes[i]);\r
+       }\r
+}\r
diff --git a/Arduino/TactonPlayer/Tacton.h b/Arduino/TactonPlayer/Tacton.h
new file mode 100644 (file)
index 0000000..cc1bf57
--- /dev/null
@@ -0,0 +1,26 @@
+#ifndef _TACTON_\r
+#define _TACTON_\r
+\r
+#include "WProgram.h"\r
+\r
+#include <TactonPlayer.h>\r
+\r
+class Tacton \r
+{\r
+       public:\r
+               Tacton(unsigned int nbframes, byte *desc);\r
+               ~Tacton();\r
+               \r
+               void play(TactonPlayer &player) const;\r
+               \r
+               boolean isValid() const { return _patterns && _durations && _frequencies && _amplitudes; }\r
+\r
+       private:\r
+               unsigned int _nbframes;\r
+               byte *_patterns;\r
+               unsigned int *_durations;\r
+               unsigned int *_frequencies;\r
+               byte *_amplitudes;\r
+};\r
+\r
+#endif\r
diff --git a/Arduino/TactonPlayer/TactonManager.cpp b/Arduino/TactonPlayer/TactonManager.cpp
new file mode 100644 (file)
index 0000000..f630288
--- /dev/null
@@ -0,0 +1,119 @@
+#include "WProgram.h"\r
+#include "TactonManager.h"\r
+\r
+void* operator new(size_t n, void * p) { \r
+  return p; \r
+}\r
+void* operator new(size_t n) { \r
+  return malloc(n); \r
+}\r
+void operator delete (void * p) { \r
+  free(p); \r
+};\r
+\r
+TactonManager::TactonManager(TactonPlayer *player)\r
+: _nbplays(0), _nbtactons(0), _player(player)\r
+{\r
+}\r
+\r
+void TactonManager::add(unsigned int nbframes, byte *desc)\r
+{\r
+       if (_nbtactons < MAXTACTONS)\r
+       {\r
+               _tactons[_nbtactons] = new Tacton(nbframes, desc);\r
+               if (_tactons[_nbtactons])\r
+               {\r
+                       Serial.println("Tacton added");\r
+                       _nbtactons++;\r
+               }\r
+       }\r
+}\r
+\r
+const Tacton *TactonManager::get(byte numtacton) const\r
+{\r
+       if (numtacton < _nbtactons)\r
+               return _tactons[numtacton];\r
+       else\r
+               return NULL;\r
+}\r
+\r
+void TactonManager::clear()\r
+{\r
+       for (int i = 0 ; i < _nbtactons ; i++)\r
+               delete _tactons[i];\r
+       _nbtactons = 0;\r
+       _nbplays = 0;\r
+}\r
+\r
+void TactonManager::addPlay(byte index, unsigned long timestamp)\r
+{\r
+       if (index >= _nbtactons || _nbplays >= MAXPLAYBUFFER)\r
+               return;\r
+       _playindex[_nbplays] = index;\r
+       _playtimestamps[_nbplays] = timestamp;\r
+       _nbplays++;     \r
+}\r
+\r
+void TactonManager::play(byte index)\r
+{\r
+       if (index < _nbtactons)\r
+               _tactons[index]->play(*_player);\r
+}\r
+\r
+void TactonManager::play(unsigned int nbframes, byte *desc)\r
+{\r
+       Tacton t(nbframes, desc);\r
+       t.play(*_player);\r
+}\r
+\r
+void TactonManager::setOffset(unsigned long timestamp)\r
+{\r
+       _offset = timestamp;\r
+}\r
+\r
+void TactonManager::checkPlay()\r
+{\r
+       unsigned long now = millis() - _offset;\r
+/*     if (_nbplays == 0 || _playtimestamps[0] > now)\r
+               return;\r
+       Serial.print("Play at ");\r
+       Serial.println(now, DEC);\r
+       play(_playindex[0]);\r
+       //shift other plays\r
+       for (int i = 0 ; i < _nbplays ; i++)\r
+       {\r
+               _playindex[i] = _playindex[i + i];\r
+               _playtimestamps[i] = _playtimestamps[i + i];            \r
+       }\r
+       _nbplays--;*/\r
+\r
+       int i = 0, j = 0;\r
+       while (_nbplays > 0 && i < MAXPLAYBUFFER && _playtimestamps[i] < now)\r
+       {\r
+               //TEST\r
+               Serial.print("Play ");\r
+               Serial.println(_playtimestamps[i], DEC);\r
+               Serial.print(" at ");\r
+               Serial.println(now, DEC);\r
+               //\r
+               play(_playindex[i]);\r
+               i++;\r
+               _nbplays--;\r
+       }\r
+       if (i == 0)\r
+               return;\r
+       //shift not played tactons\r
+       for (j = 0 ; i + j < MAXPLAYBUFFER ; j++)\r
+       {\r
+               _playindex[j] = _playindex[j + i];\r
+               _playtimestamps[j] = _playtimestamps[j + i];            \r
+       }\r
+       //erase shifted values\r
+/*     for (i = j ; i < MAXPLAYBUFFER ; i++)\r
+       {\r
+               _playindex[i] = 0xff;\r
+               _playtimestamps[i] = 0xffffffff;\r
+       }*/\r
+}\r
+\r
+               
\ No newline at end of file
diff --git a/Arduino/TactonPlayer/TactonManager.h b/Arduino/TactonPlayer/TactonManager.h
new file mode 100644 (file)
index 0000000..32a3bc7
--- /dev/null
@@ -0,0 +1,38 @@
+#ifndef _TACTONMANAGER_\r
+#define _TACTONMANAGER_\r
+\r
+#include "WProgram.h"\r
+\r
+#include <Tacton.h>\r
+#include <TactonPlayer.h>\r
+\r
+#define MAXTACTONS             16\r
+#define MAXPLAYBUFFER  10\r
+\r
+class TactonManager\r
+{\r
+       public:\r
+               TactonManager(TactonPlayer *player);\r
+               \r
+               void add(unsigned int nbframes, byte *desc);\r
+               const Tacton *get(byte numtacton) const;\r
+               \r
+               void play(byte index);\r
+               void play(unsigned int nbframes, byte *desc);\r
+               void addPlay(byte index, unsigned long timestamp);\r
+               void checkPlay();\r
+               void setOffset(unsigned long timestamp);\r
+\r
+               void clear();\r
+               \r
+       private:\r
+               byte _nbplays;\r
+               byte _nbtactons;\r
+               Tacton *_tactons[MAXTACTONS];\r
+               byte _playindex[MAXPLAYBUFFER];\r
+               unsigned long _playtimestamps[MAXPLAYBUFFER];\r
+               TactonPlayer *_player;\r
+               unsigned long _offset;\r
+};\r
+\r
+#endif\r
index 731bfcb3714fb7c200121f4177a65a38c2379a10..c88455c00fdc9ab348f2ca76322f54a75303788e 100644 (file)
@@ -2,41 +2,14 @@
 #include "TactonPlayer.h"\r
 \r
 TactonPlayer::TactonPlayer(byte nbtactors, byte *pins)\r
-:_nbtactors(nbtactors), _pins(pins)\r
 {\r
-       for (int i = 0 ; i < nbtactors ; i++)\r
-               pinMode(pins[i], OUTPUT);     \r
+       _nbtactors = min(MAXTACTORS, nbtactors);\r
+       _pins = pins;\r
+       \r
+       for (int i = 0 ; i < _nbtactors ; i++)\r
+       {\r
+               pinMode(pins[i], OUTPUT);\r
+       }       \r
 }\r
 \r
-void TactonPlayer::beep(byte pattern, long duration, unsigned int frequency, byte amplitude) const\r
-{\r
-    int i;\r
-    long del = (long)(1000000 / ((long)frequency));\r
-    long looptime = (long)((duration * 1000) / (del * 2));\r
-       long udel = del % 1000;\r
-       long mdel = del / 1000;\r
-    for (i = 0 ; i < looptime ; i++)\r
-    {\r
-               for (int j = 0 ; j < 8 ; j++)\r
-                       if (j < _nbtactors && (pattern & (0x01 << j)))\r
-                               analogWrite(_pins[j], amplitude);\r
-               //delayMicroseconds is not accurate over 16383µs\r
-               if (del > 10000)\r
-               {\r
-                       delayMicroseconds(udel);\r
-                       delay(mdel);\r
-               }\r
-               else\r
-                       delayMicroseconds(del);\r
-               for (int j = 0 ; j < 8 ; j++)\r
-                       if (j < _nbtactors && (pattern & (0x01 << j)))\r
-                               analogWrite(_pins[j], 0);\r
-               if (del > 10000)\r
-               {\r
-                       delayMicroseconds(udel);\r
-                       delay(mdel);\r
-               }\r
-               else\r
-                       delayMicroseconds(del);\r
-    }\r
-}\r
+void __cxa_pure_virtual(void) {};
\ No newline at end of file
index 2fa99b2034f9e0198b2b03ed90c9e8c72fe04950..a5283dc986e6b9652c96519c814afa09121bb8d4 100644 (file)
@@ -3,22 +3,38 @@
 \r
 #include "WProgram.h"\r
 \r
+//if using more than 8, change type of pattern\r
+#define MAXTACTORS 8\r
+\r
+extern "C" void __cxa_pure_virtual(void); \r
+\r
 class TactonPlayer \r
 {\r
        public:\r
                TactonPlayer(byte nbtactors, byte *pins);\r
-\r
+                               \r
                //8bits pattern => max 8 tactors, change type if using more\r
-               void beep(byte pattern, long duration, unsigned int frequency, byte amplitude) const;\r
-               \r
+               virtual void beep(byte pattern, unsigned long duration, unsigned int frequency, byte amplitude) = 0;\r
+/*             \r
                void debug1() { analogWrite(_pins[0], 255); delay(100); analogWrite(_pins[0], 0); delay(100);}\r
                void debug2() { analogWrite(_pins[1], 255); delay(100); analogWrite(_pins[1], 0); delay(100); }\r
                void debug3() { analogWrite(_pins[2], 255); delay(100); analogWrite(_pins[2], 0); delay(100); }\r
                void debug4() { analogWrite(_pins[3], 255); delay(100); analogWrite(_pins[3], 0); delay(100); }\r
-       \r
-       private:                \r
+       */\r
+       protected:\r
+               virtual void init() const {}\r
+               \r
+               virtual void setFrequency(unsigned int frequency) { _frequency = frequency; }\r
+               virtual void setDuration(unsigned long duration) { _duration = duration; }\r
+               virtual void setAmplitude(byte amplitude) { _amplitude = amplitude; }\r
+               virtual void setPattern(byte pattern) { _pattern = pattern; }\r
+\r
                byte _nbtactors;\r
                byte *_pins;\r
+               byte _pattern;\r
+               unsigned long _duration;\r
+               unsigned int _frequency;\r
+               byte _amplitude;\r
 };\r
 \r
 #endif\r
diff --git a/Arduino/TactonPlayer/TactonPlayerPrecise.cpp b/Arduino/TactonPlayer/TactonPlayerPrecise.cpp
new file mode 100644 (file)
index 0000000..c0bc127
--- /dev/null
@@ -0,0 +1,201 @@
+#include "WProgram.h"\r
+#include "TactonPlayerPrecise.h"\r
+\r
+#include "pins_arduino.h"\r
+\r
+volatile unsigned long _toggle_count;\r
+//volatile bool _active = false;\r
+\r
+/*\r
+volatile unsigned long _ccper256cv; // (F_CPU) / (512 * _frequency)\r
+volatile unsigned long _cv; // (F_CPU * _duration) / (256000 * _ccper256cv)\r
+volatile unsigned long _currentcvi; // 0 <= _ccper256cv <= _ccper256cv\r
+volatile unsigned long _currentcv; // 0 <= _currentcv <= _cv\r
+volatile bool _currentstate;\r
+*/\r
+\r
+uint8_t TactonPlayerPrecise::_portB = 0;\r
+uint8_t TactonPlayerPrecise::_portC = 0;\r
+uint8_t TactonPlayerPrecise::_portD = 0;\r
+\r
+\r
+TactonPlayerPrecise::TactonPlayerPrecise(byte nbtactors, byte *pins)\r
+:TactonPlayer(nbtactors, pins)\r
+{\r
+}\r
+\r
+void TactonPlayerPrecise::init() const\r
+{\r
+       //init timers\r
+       TIMSK1 = 0;\r
+       TCNT1 = 0;\r
+       TCCR1A = 0; // CTC mode\r
+       TCCR1B = (1 << WGM12) | (1 << CS10);\r
+}\r
+\r
+void TactonPlayerPrecise::setFrequency(unsigned int frequency)\r
+{\r
+       TactonPlayer::setFrequency(frequency);\r
+       \r
+       unsigned long ocr = F_CPU / _frequency / 2 - 1;\r
+       byte prescalarbits = (1 << CS10);\r
+       \r
+       if (ocr > 0xffff)\r
+       {\r
+               ocr = F_CPU / _frequency / 64 / 2 - 1;\r
+               prescalarbits = (1 << CS11) | (1 << CS10);\r
+       }\r
+       /*\r
+       Serial.print("_frequency=");\r
+       Serial.print(_frequency, DEC);\r
+       Serial.print(" prescalar=");\r
+       Serial.print(prescalarbits, BIN);\r
+       Serial.print(" ocr=");\r
+       Serial.println(ocr, DEC);*/\r
+\r
+       TCCR1B |= prescalarbits;\r
+       OCR1A = ocr;\r
+/*\r
+       _ccper256cv = F_CPU / 512 / _frequency;\r
+       _currentcvi = 0;*/\r
+\r
+/*     Serial.print("_frequency=");\r
+       Serial.print(_frequency, DEC);\r
+       Serial.print(" _ccper256cv=");\r
+       Serial.println(_ccper256cv, DEC);*/\r
+}\r
+\r
+void TactonPlayerPrecise::setDuration(unsigned long duration)\r
+{\r
+       TactonPlayer::setDuration(duration);\r
+       _toggle_count = 2 * _frequency * _duration / 1000;\r
+\r
+/*     _cv = (F_CPU / 1000 * _duration) / 256 / _ccper256cv;\r
+       _currentcv = 0;\r
+\r
+/*     Serial.print("_duration=");\r
+       Serial.print(_duration, DEC);\r
+       Serial.print(" _cv=");\r
+       Serial.println(_cv, DEC);*/\r
+}\r
+\r
+void TactonPlayerPrecise::setAmplitude(byte amplitude)\r
+{\r
+       TactonPlayer::setAmplitude(amplitude);\r
+//     OCR2A = amplitude;\r
+       \r
+/*     Serial.print("_amplitude=");\r
+       Serial.print(_amplitude, DEC);*/\r
+}\r
+\r
+void TactonPlayerPrecise::setPattern(byte pattern)\r
+{\r
+       TactonPlayer::setPattern(pattern);\r
+\r
+       _portB = _portC = _portD = 0;\r
+       for (int i = 0 ; i < _nbtactors ; i++)\r
+       {\r
+               pinMode(_pins[i], OUTPUT);\r
+               volatile uint8_t * port = portOutputRegister(digitalPinToPort(_pins[i]));\r
+               if (_pattern & (1 << i))\r
+               {\r
+                       if (port == &PORTB)\r
+                               _portB |= digitalPinToBitMask(_pins[i]);\r
+                       else if (port == &PORTC)\r
+                               _portC |= digitalPinToBitMask(_pins[i]);\r
+                       else if (port == &PORTD)\r
+                               _portD |= digitalPinToBitMask(_pins[i]);\r
+               }\r
+       }       \r
+}\r
+\r
+void TactonPlayerPrecise::tooglePins()\r
+{\r
+       PORTB ^= _portB;\r
+       PORTC ^= _portC;\r
+       PORTD ^= _portD;\r
+}\r
+\r
+void TactonPlayerPrecise::turnOnPins()\r
+{\r
+       PORTB |= _portB;\r
+       PORTC |= _portC;\r
+       PORTD |= _portD;\r
+}\r
+\r
+void TactonPlayerPrecise::turnOffPins()\r
+{\r
+       PORTB &= ~_portB;\r
+       PORTC &= ~_portC;\r
+       PORTD &= ~_portD;\r
+}\r
+\r
+void TactonPlayerPrecise::beep(byte pattern, unsigned long duration, unsigned int frequency, byte amplitude)\r
+{\r
+       init();\r
+       setFrequency(frequency);\r
+//     setAmplitude(amplitude);\r
+       setDuration(duration);\r
+       setPattern(pattern);\r
+       //_active = true;\r
+       turnOnPins();\r
+       TIMSK1 = (1 << OCIE1A);\r
+//     TIMSK2 = (1 << OCIE2A) | (1 << TOIE2);\r
+//     TIMSK2 = (1 << TOIE2);\r
+       sei();\r
+}\r
+\r
+\r
+ISR(TIMER1_COMPA_vect)\r
+{\r
+       if (_toggle_count > 0)\r
+       {\r
+               _toggle_count--;\r
+               TactonPlayerPrecise::tooglePins();\r
+       }\r
+       else\r
+       {\r
+               TIMSK1 &= ~(1 << OCIE1A);\r
+               TactonPlayerPrecise::turnOffPins();\r
+       }\r
+}\r
+\r
+/*\r
+ISR(TIMER2_OVF_vect)\r
+{\r
+       _currentcvi++;\r
+       if (_currentcvi >= _ccper256cv)\r
+       {\r
+               _currentstate = !_currentstate;\r
+               _currentcvi = 0;\r
+               _currentcv++;\r
+               if (_currentcv >= _cv)\r
+               {\r
+                       //TIMSK2 &= ~(1 << TOIE2);\r
+                       TIMSK2 &= ~((1 << OCIE2A) | (1 << TOIE2));\r
+                       TactonPlayer::turnOffPins();\r
+                       return;\r
+               }\r
+       }\r
+       if (_currentstate)\r
+               TactonPlayer::tooglePins();\r
+\r
+//     count1++;\r
+//     if (_active)\r
+//     {\r
+//             TactonPlayer::tooglePins();\r
+//             count2++;\r
+//     }\r
+//     else\r
+//             TactonPlayer::turnOffPins();\r
+}\r
+\r
+ISR(TIMER2_COMPA_vect)\r
+{\r
+//     if (_active)\r
+       if (_currentstate)\r
+               TactonPlayer::tooglePins();\r
+//     else\r
+//             TactonPlayer::turnOffPins();\r
+}\r
+*/     \r
diff --git a/Arduino/TactonPlayer/TactonPlayerPrecise.h b/Arduino/TactonPlayer/TactonPlayerPrecise.h
new file mode 100644 (file)
index 0000000..4a1fa9e
--- /dev/null
@@ -0,0 +1,32 @@
+#ifndef _TACTONPLAYERPRECISE_\r
+#define _TACTONPLAYERPRECISE_\r
+\r
+#include "WProgram.h"\r
+\r
+#include <TactonPlayer.h>\r
+\r
+class TactonPlayerPrecise: public TactonPlayer\r
+{\r
+       public:\r
+               TactonPlayerPrecise(byte nbtactors, byte *pins);\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
+               inline static void tooglePins();\r
+               inline static void turnOnPins();\r
+               inline static void turnOffPins();\r
+\r
+       private:\r
+               void init() const;              \r
+               void setFrequency(unsigned int frequency);\r
+               void setDuration(unsigned long duration);\r
+               void setAmplitude(byte amplitude);\r
+               void setPattern(byte pattern);\r
+\r
+               static uint8_t _portB;\r
+               static uint8_t _portC;\r
+               static uint8_t _portD;\r
+};\r
+\r
+#endif\r
diff --git a/Arduino/TactonPlayer/TactonPlayerWithAmplitude.cpp b/Arduino/TactonPlayer/TactonPlayerWithAmplitude.cpp
new file mode 100644 (file)
index 0000000..9c4a98a
--- /dev/null
@@ -0,0 +1,71 @@
+#include "WProgram.h"\r
+#include "TactonPlayerWithAmplitude.h"\r
+\r
+TactonPlayerWithAmplitude::TactonPlayerWithAmplitude(byte nbtactors, byte *pins)\r
+:TactonPlayer(nbtactors, pins)\r
+{\r
+}\r
+\r
+void TactonPlayerWithAmplitude::init() const\r
+{\r
+       //set counter 1 as Fast PWM\r
+       TCCR1A |= (1 << WGM10);\r
+       TCCR1A &= ~(1 << WGM11);\r
+       TCCR1B |= (1 << WGM12);\r
+       TCCR1B &= ~(1 << WGM13);\r
+       //set counter 2 as Fast PWM\r
+       TCCR2A |= ((1 << WGM20) & (1 << WGM21));\r
+       TCCR2B &= ~(1 << WGM22);\r
+}\r
+\r
+void TactonPlayerWithAmplitude::beep(byte pattern, unsigned long duration, unsigned int frequency, byte amplitude)\r
+{\r
+       init();\r
+       setFrequency(frequency);\r
+       setAmplitude(amplitude);\r
+       setDuration(duration);\r
+       setPattern(pattern);\r
+\r
+    int i;\r
+    long del = (long)(1000000 / ((long)frequency));\r
+    long looptime = (long)((duration * 1000) / (del * 2));\r
+       long udel = del % 1000;\r
+       long mdel = del / 1000;\r
+       \r
+       Serial.print("del=");\r
+       Serial.print(del);\r
+       Serial.print(" udel=");\r
+       Serial.print(udel);\r
+       Serial.print(" mdel=");\r
+       Serial.print(mdel);\r
+       Serial.print(" looptime=");\r
+       Serial.println(looptime);\r
+       \r
+    for (i = 0 ; i < looptime ; i++)\r
+    {\r
+               for (int j = 0 ; j < MAXTACTORS ; j++)\r
+                       if (j < _nbtactors && (pattern & (1 << j)))\r
+                               analogWrite(_pins[j], amplitude);\r
+               //delayMicroseconds is not accurate over 16383µs\r
+               if (del > 16000)\r
+               {\r
+                       delayMicroseconds(udel);\r
+                       if (mdel > 0)\r
+                               delay(mdel);\r
+               }\r
+               else\r
+                       delayMicroseconds(del);\r
+                       \r
+               for (int j = 0 ; j < MAXTACTORS ; j++)\r
+                       if (j < _nbtactors && (pattern & (1 << j)))\r
+                               analogWrite(_pins[j], 0);\r
+               if (del > 16000)\r
+               {\r
+                       delayMicroseconds(udel);\r
+                       if (mdel > 0)\r
+                               delay(mdel);\r
+               }\r
+               else\r
+                       delayMicroseconds(del);\r
+    }\r
+}\r
diff --git a/Arduino/TactonPlayer/TactonPlayerWithAmplitude.h b/Arduino/TactonPlayer/TactonPlayerWithAmplitude.h
new file mode 100644 (file)
index 0000000..fe21abb
--- /dev/null
@@ -0,0 +1,19 @@
+#ifndef _TACTONPLAYERWITHAMPLITUDE_\r
+#define _TACTONPLAYERWITHAMPLITUDE_\r
+\r
+#include "WProgram.h"\r
+\r
+#include <TactonPlayer.h>\r
+\r
+class TactonPlayerWithAmplitude: public TactonPlayer\r
+{\r
+       public:\r
+               TactonPlayerWithAmplitude(byte nbtactors, byte *pins);\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
+       private:\r
+               void init() const;\r
+};\r
+\r
+#endif\r
index 2e2fd225fa0df1ede1c1d83ed24e010154315859..585ac33b3a41584ace32dd5e3d310712ca92dca3 100644 (file)
@@ -1,6 +1,18 @@
 TactonPlayer   KEYWORD1\r
+TactonPlayerPrecise    KEYWORD1\r
+TactonPlayerWithAmplitude      KEYWORD1\r
 beep   KEYWORD2\r
 debug1 KEYWORD2\r
 debug2 KEYWORD2\r
 debug3 KEYWORD2\r
-debug4 KEYWORD2
\ No newline at end of file
+debug4 KEYWORD2\r
+Tacton KEYWORD1\r
+play   KEYWORD2\r
+isValid        KEYWORD2\r
+TactonManager  KEYWORD1\r
+get    KEYWORD2\r
+add    KEYWORD2\r
+clear  KEYWORD2\r
+play   KEYWORD2\r
+addPlay        KEYWORD2\r
+checkPlay      KEYWORD2
\ No newline at end of file
index 58c35d1305befae760c5c3f137137640e2b63ae2..cc8b724b8de035b86bf9766496692a371212a45d 100644 (file)
@@ -1,21 +1,11 @@
 #include <TactonManager.h>
-#include <TactonPlayer.h>
+#include <TactonPlayerWithAmplitude.h>
 #include <Tacton.h>
 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()