#include "Tacton.h"\r
\r
\r
-/*Tacton::Tacton()\r
-:_nbframes(0), _patterns(NULL), _durations(NULL), _frequencies(NULL), _amplitudes(NULL)\r
-{\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
}\r
}\r
\r
-/*Tacton::Tacton(const Tacton &tacton)\r
-:_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)))\r
-{\r
- for(int i = 0 ; i < tacton._nbframes ; i++)\r
- {\r
- _patterns[i] = tacton._patterns[i];\r
- _durations[i] = tacton._durations[i];\r
- _frequencies[i] = tacton._frequencies[i];\r
- _amplitudes[i] = tacton._amplitudes[i];\r
- }\r
-}*/\r
-\r
Tacton::~Tacton()\r
{\r
free(_patterns);\r
{ \r
if (isValid())\r
for (int i = 0 ; i < _nbframes ; i++)\r
- player.beep(_patterns[i], _durations[i], _frequencies[i], _amplitudes[i]);\r
+ player.beep(_patterns[i], (long)_durations[i], _frequencies[i], _amplitudes[i]);\r
}\r
-\r
-/*Tacton& Tacton::operator=(const Tacton &t) \r
-{\r
- if (this == &t)\r
- return *this;\r
- if (_patterns)\r
- free(_patterns);\r
- if (_durations)\r
- free(_durations);\r
- if (_frequencies)\r
- free(_frequencies);\r
- if (_amplitudes)\r
- free(_amplitudes);\r
- _nbframes = t._nbframes;\r
- _patterns = (byte *)malloc(_nbframes * sizeof(byte));\r
- _durations = (unsigned int *)malloc(_nbframes * sizeof(unsigned int));\r
- _frequencies = (unsigned int *)malloc(_nbframes * sizeof(unsigned int));\r
- _amplitudes = (byte *)malloc(_nbframes * sizeof(byte));\r
- for(int i = 0 ; i < _nbframes ; i++)\r
- {\r
- _patterns[i] = t._patterns[i];\r
- _durations[i] = t._durations[i];\r
- _frequencies[i] = t._frequencies[i];\r
- _amplitudes[i] = t._amplitudes[i];\r
- }\r
-\r
- return *this;\r
-}*/\r
-
\ No newline at end of file
class Tacton \r
{\r
public:\r
-// Tacton();\r
Tacton(unsigned int nbframes, byte *desc);\r
-// Tacton(const Tacton &tacton);\r
~Tacton();\r
\r
void play(const TactonPlayer &player) const;\r
\r
-// Tacton & operator=(const Tacton &t);\r
boolean isValid() const { return _patterns && _durations && _frequencies && _amplitudes; }\r
\r
private:\r
public:\r
TactonManager(TactonPlayer *player);\r
\r
-// void add(const Tacton &t);\r
void add(unsigned int nbframes, byte *desc);\r
const Tacton *get(byte numtacton) const;\r
\r
pinMode(pins[i], OUTPUT); \r
}\r
\r
-void TactonPlayer::beep(byte pattern, unsigned int duration, unsigned int frequency, byte amplitude) const\r
+void TactonPlayer::beep(byte pattern, long duration, unsigned int frequency, byte amplitude) const\r
{\r
int i;\r
- long del = (long)(1000000 / frequency);\r
- long looptime = (long)((((long)duration) * 1000) / (del * 2));\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(del);\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
- delayMicroseconds(del);\r
+ if (del > 10000)\r
+ {\r
+ delayMicroseconds(udel);\r
+ delay(mdel);\r
+ }\r
+ else\r
+ delayMicroseconds(del);\r
}\r
}\r
TactonPlayer(byte nbtactors, byte *pins);\r
\r
//8bits pattern => max 8 tactors, change type if using more\r
- void beep(byte pattern, unsigned int duration, unsigned int frequency, byte amplitude) const;\r
+ void beep(byte pattern, long duration, unsigned int frequency, byte amplitude) const;\r
\r
- void debug1() { analogWrite(_pins[0], 255); delay(200); analogWrite(_pins[0], 0); delay(100);}\r
- void debug2() { analogWrite(_pins[1], 255); delay(200); analogWrite(_pins[1], 0); delay(100); }\r
- void debug3() { analogWrite(_pins[2], 255); delay(200); analogWrite(_pins[2], 0); delay(100); }\r
- void debug4() { analogWrite(_pins[3], 255); delay(200); analogWrite(_pins[3], 0); delay(100); }\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
byte _nbtactors;\r
buffer[4] = (unsigned char)((timestamp & 0x0000ff00) >> 8);\r
buffer[5] = (unsigned char)(timestamp & 0x000000ff);\r
_comport->WriteData(buffer, 6);\r
-}
\ No newline at end of file
+}\r
+\r
+void TactonPlayer::debugRead(char *res, int nb) const\r
+{\r
+ _comport->ReadData(res, nb);\r
+}\r
void __declspec(dllexport) stop();\r
\r
//register a tacton\r
- void __declspec(dllexport) regist(const Tacton &t);\r
+ __declspec(dllexport) void regist(const Tacton &t);\r
//register a list of tactons in a file, returns the number of tactons loaded\r
- unsigned int __declspec(dllexport) registFile(char *filename);\r
+ __declspec(dllexport) unsigned int registFile(char *filename);\r
\r
//play a registered tacton\r
- void __declspec(dllexport) play(unsigned char index);\r
+ __declspec(dllexport) void play(unsigned char index);\r
//play a specified tacton\r
- void __declspec(dllexport) play(const Tacton &t);\r
+ __declspec(dllexport) void play(const Tacton &t);\r
//schedule the play of a registered tacton\r
- void __declspec(dllexport) schedule(unsigned char index, unsigned long timestamp);\r
+ __declspec(dllexport) void schedule(unsigned char index, unsigned long timestamp);\r
+\r
+ //read on the serial port\r
+ __declspec(dllexport) void debugRead(char *res, int nb) const;\r
\r
private:\r
Serial *_comport;\r