From e25cc52e4593cf933f70202424de7e93c08709b6 Mon Sep 17 00:00:00 2001 From: Thomas Pietrzak Date: Fri, 17 Jun 2011 21:49:57 +0000 Subject: [PATCH] fix low frequencies bug git-svn-id: svn+ssh://thomaspietrzak.com/var/svn/rep@37 47cf9a05-e0a8-4ed5-9e9b-101a649bc004 --- Arduino/Tacton/Tacton.cpp | 48 +------------------------- Arduino/Tacton/Tacton.h | 3 -- Arduino/TactonManager/TactonManager.h | 1 - Arduino/TactonPlayer/TactonPlayer.cpp | 25 +++++++++++--- Arduino/TactonPlayer/TactonPlayer.h | 10 +++--- TactonLibrary.suo | Bin 16896 -> 22528 bytes TactonPlayer/TactonPlayer.cpp | 7 +++- TactonPlayer/TactonPlayer.hpp | 13 ++++--- 8 files changed, 40 insertions(+), 67 deletions(-) diff --git a/Arduino/Tacton/Tacton.cpp b/Arduino/Tacton/Tacton.cpp index 11bf828..259b30a 100644 --- a/Arduino/Tacton/Tacton.cpp +++ b/Arduino/Tacton/Tacton.cpp @@ -2,11 +2,6 @@ #include "Tacton.h" -/*Tacton::Tacton() -:_nbframes(0), _patterns(NULL), _durations(NULL), _frequencies(NULL), _amplitudes(NULL) -{ -}*/ - 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))) { @@ -22,18 +17,6 @@ Tacton::Tacton(unsigned int nbframes, byte *desc) } } -/*Tacton::Tacton(const Tacton &tacton) -:_nbframes(tacton._nbframes), _patterns((byte *)malloc(tacton._nbframes * sizeof(byte))), _durations((unsigned int *)malloc(tacton._nbframes * sizeof(unsigned int))), _frequencies((unsigned int *)malloc(tacton._nbframes * sizeof(unsigned int))), _amplitudes((byte *)malloc(tacton._nbframes * sizeof(byte))) -{ - for(int i = 0 ; i < tacton._nbframes ; i++) - { - _patterns[i] = tacton._patterns[i]; - _durations[i] = tacton._durations[i]; - _frequencies[i] = tacton._frequencies[i]; - _amplitudes[i] = tacton._amplitudes[i]; - } -}*/ - Tacton::~Tacton() { free(_patterns); @@ -46,34 +29,5 @@ void Tacton::play(const TactonPlayer &player) const { if (isValid()) for (int i = 0 ; i < _nbframes ; i++) - player.beep(_patterns[i], _durations[i], _frequencies[i], _amplitudes[i]); + player.beep(_patterns[i], (long)_durations[i], _frequencies[i], _amplitudes[i]); } - -/*Tacton& Tacton::operator=(const Tacton &t) -{ - if (this == &t) - return *this; - if (_patterns) - free(_patterns); - if (_durations) - free(_durations); - if (_frequencies) - free(_frequencies); - if (_amplitudes) - free(_amplitudes); - _nbframes = t._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)); - for(int i = 0 ; i < _nbframes ; i++) - { - _patterns[i] = t._patterns[i]; - _durations[i] = t._durations[i]; - _frequencies[i] = t._frequencies[i]; - _amplitudes[i] = t._amplitudes[i]; - } - - return *this; -}*/ - \ No newline at end of file diff --git a/Arduino/Tacton/Tacton.h b/Arduino/Tacton/Tacton.h index 8ef8ae1..8b9b289 100644 --- a/Arduino/Tacton/Tacton.h +++ b/Arduino/Tacton/Tacton.h @@ -8,14 +8,11 @@ class Tacton { public: -// Tacton(); Tacton(unsigned int nbframes, byte *desc); -// Tacton(const Tacton &tacton); ~Tacton(); void play(const TactonPlayer &player) const; -// Tacton & operator=(const Tacton &t); boolean isValid() const { return _patterns && _durations && _frequencies && _amplitudes; } private: diff --git a/Arduino/TactonManager/TactonManager.h b/Arduino/TactonManager/TactonManager.h index 5d89970..32a3bc7 100644 --- a/Arduino/TactonManager/TactonManager.h +++ b/Arduino/TactonManager/TactonManager.h @@ -14,7 +14,6 @@ class TactonManager public: TactonManager(TactonPlayer *player); -// void add(const Tacton &t); void add(unsigned int nbframes, byte *desc); const Tacton *get(byte numtacton) const; diff --git a/Arduino/TactonPlayer/TactonPlayer.cpp b/Arduino/TactonPlayer/TactonPlayer.cpp index b8a7a3f..731bfcb 100644 --- a/Arduino/TactonPlayer/TactonPlayer.cpp +++ b/Arduino/TactonPlayer/TactonPlayer.cpp @@ -8,20 +8,35 @@ TactonPlayer::TactonPlayer(byte nbtactors, byte *pins) pinMode(pins[i], OUTPUT); } -void TactonPlayer::beep(byte pattern, unsigned int duration, unsigned int frequency, byte amplitude) const +void TactonPlayer::beep(byte pattern, long duration, unsigned int frequency, byte amplitude) const { int i; - long del = (long)(1000000 / frequency); - long looptime = (long)((((long)duration) * 1000) / (del * 2)); + 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(del); + //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); - delayMicroseconds(del); + if (del > 10000) + { + delayMicroseconds(udel); + delay(mdel); + } + else + delayMicroseconds(del); } } diff --git a/Arduino/TactonPlayer/TactonPlayer.h b/Arduino/TactonPlayer/TactonPlayer.h index 775f9f6..2fa99b2 100644 --- a/Arduino/TactonPlayer/TactonPlayer.h +++ b/Arduino/TactonPlayer/TactonPlayer.h @@ -9,12 +9,12 @@ class TactonPlayer TactonPlayer(byte nbtactors, byte *pins); //8bits pattern => max 8 tactors, change type if using more - void beep(byte pattern, unsigned int duration, unsigned int frequency, byte amplitude) const; + void beep(byte pattern, long duration, unsigned int frequency, byte amplitude) const; - void debug1() { analogWrite(_pins[0], 255); delay(200); analogWrite(_pins[0], 0); delay(100);} - void debug2() { analogWrite(_pins[1], 255); delay(200); analogWrite(_pins[1], 0); delay(100); } - void debug3() { analogWrite(_pins[2], 255); delay(200); analogWrite(_pins[2], 0); delay(100); } - void debug4() { analogWrite(_pins[3], 255); delay(200); analogWrite(_pins[3], 0); delay(100); } + 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: byte _nbtactors; diff --git a/TactonLibrary.suo b/TactonLibrary.suo index 56817b7501a4b43fffefc50d462c1de5a6857055..3ff37c824e1e6f1d5a7141f5a45aed07eb681823 100644 GIT binary patch delta 1422 zcmc(fe@t6d6vyu=Efm&QSXn`!3@EjB=9;v<()M##&s1NqH$JtdAOK2Xwv>wzE%P##!qV#idx^AUNR!Nd28F8N(Vz)e zvvqrjGhP=Dwmwku)J7TJ2~I^qbe0FgCpcm=^?AinF@ibuqZ znBrXc6JD&Tz1p~&_&a;MI^C3{Vmp%Gs^Guu3G(nu%_kKv4DuUFxnJ+YUNM%h_x!G3 z?;O178~?QS&!;WxpKa>krCm?zqheA_3mG8DnuR6 zdHXrpwwov0P2A%&|I2IZtc0+s1I`X&i))^zT0Ul%`w%a>-cV1wGel`*9p=W)04`)T z5BDwc8Dtu2OQ;#|b>fmh0H17uW5G6BUR`si759Mv}4tN(_ z0PlhK!3W?&@DaEO7QrQe_L?RB+P^1XP$;0F=rz4T4E=Y_gVtvev?ZR*VJNaeh7c6x zx!#k!+SaamJv&H^gpE3*?CHW@OhHVp0x9V(I|aE(U(XS<(J0&gzFP+-TDMT%Edw`2 z<7Nn8@)gI8kMpwlH8D2_! zLJun?rUt`Os}pxdh?o!)6J?=G!-9=F7hSmk|7kFp+kCm_o_l6)@5~)3LDDoO?xfN7 z+ny%YTs+gH*EXk{GLei3OO!a~?V;|*;oukO{#V>%CwJCLmaCsaZeR6`9M zgIYKaE~o=H)Pn~a-~^lmFEoM={LloaAOOt}gcfLpHfV(G zF?Nve_QtygJpi{ZsX4s- diff --git a/TactonPlayer/TactonPlayer.cpp b/TactonPlayer/TactonPlayer.cpp index acebf80..b739d6e 100644 --- a/TactonPlayer/TactonPlayer.cpp +++ b/TactonPlayer/TactonPlayer.cpp @@ -94,4 +94,9 @@ void TactonPlayer::schedule(unsigned char index, unsigned long timestamp) buffer[4] = (unsigned char)((timestamp & 0x0000ff00) >> 8); buffer[5] = (unsigned char)(timestamp & 0x000000ff); _comport->WriteData(buffer, 6); -} \ No newline at end of file +} + +void TactonPlayer::debugRead(char *res, int nb) const +{ + _comport->ReadData(res, nb); +} diff --git a/TactonPlayer/TactonPlayer.hpp b/TactonPlayer/TactonPlayer.hpp index 48296a9..49621a7 100644 --- a/TactonPlayer/TactonPlayer.hpp +++ b/TactonPlayer/TactonPlayer.hpp @@ -14,16 +14,19 @@ class TactonPlayer void __declspec(dllexport) stop(); //register a tacton - void __declspec(dllexport) regist(const Tacton &t); + __declspec(dllexport) void regist(const Tacton &t); //register a list of tactons in a file, returns the number of tactons loaded - unsigned int __declspec(dllexport) registFile(char *filename); + __declspec(dllexport) unsigned int registFile(char *filename); //play a registered tacton - void __declspec(dllexport) play(unsigned char index); + __declspec(dllexport) void play(unsigned char index); //play a specified tacton - void __declspec(dllexport) play(const Tacton &t); + __declspec(dllexport) void play(const Tacton &t); //schedule the play of a registered tacton - void __declspec(dllexport) schedule(unsigned char index, unsigned long timestamp); + __declspec(dllexport) void schedule(unsigned char index, unsigned long timestamp); + + //read on the serial port + __declspec(dllexport) void debugRead(char *res, int nb) const; private: Serial *_comport; -- 2.30.2