From: Thomas Pietrzak Date: Tue, 11 Sep 2012 10:36:36 +0000 (+0000) Subject: Add support for angles on hardware, fixed PWM frequency X-Git-Url: https://git.thomaspietrzak.com/?a=commitdiff_plain;h=13489c50c3abe76e1fa312d6af6325947d97fb82;p=tactonlibrary.git Add support for angles on hardware, fixed PWM frequency git-svn-id: svn+ssh://thomaspietrzak.com/var/svn/rep@99 47cf9a05-e0a8-4ed5-9e9b-101a649bc004 --- diff --git a/Arduino/wristbandTactons/wristbandTactons.ino b/Arduino/wristbandTactons/wristbandTactons.ino index 0e4e14b..a0e4df7 100644 --- a/Arduino/wristbandTactons/wristbandTactons.ino +++ b/Arduino/wristbandTactons/wristbandTactons.ino @@ -29,13 +29,13 @@ void loop() { byte index; unsigned long timestamp; - unsigned int freq; + unsigned int param; if (Serial.available() > 0) { if (command == 0) command = Serial.read(); - switch(command) + switch(command) { //set the timestamp to 0, and watch for scheduled tactons case 'S': @@ -154,12 +154,52 @@ void loop() case 'F': if (Serial.available() >= 2) { - freq = (((unsigned long)Serial.read()) << 8) | \ + param = (((unsigned long)Serial.read()) << 8) | \ (((unsigned long)Serial.read())); - manager.setFrequency(freq); + manager.setFrequency(param); command = 0; } break; + //sets an angle in degrees + case 'a': + if (Serial.available() >= 2) + { + param = (((unsigned long)Serial.read()) << 8) | \ + (((unsigned long)Serial.read())); + manager.setAngle(param); + command = 0; + } + break; + //sets a series on angles + // sNDnA1A2...AN + // N = nb angles (1 byte) + // D = duration in ms of each frame (2 bytes) + // n = number of frames (2 bytes) + // A1, A2, ..., AN : angles (2 bytes per tactor) + case 's': + if (nbf == 0 && Serial.available() >= 1) + nbf = (unsigned int) Serial.read(); + if (nbf > 0) + { + //DO NOT OVERFLOW max(nbf): 60 + while (posbuf < 2 * nbf + 4 && Serial.available() > 0) + { + buffer[posbuf] = Serial.read(); + //Serial.print(buffer[posbuf], HEX); + posbuf++; + } + if (posbuf >= 2 * nbf + 4) + { + manager.setAngleSequence(nbf, \ + (buffer[0] << 8) | buffer[1], \ + (buffer[2] << 8) | buffer[3], \ + buffer + 4); + posbuf = 0; + command = 0; + nbf = 0; + } + } + break; //stop any vibration case 'B': manager.stop(); diff --git a/TactonDebug/MagicCircle/magiccircle.cpp b/TactonDebug/MagicCircle/magiccircle.cpp index ed257ea..8b883c7 100644 --- a/TactonDebug/MagicCircle/magiccircle.cpp +++ b/TactonDebug/MagicCircle/magiccircle.cpp @@ -77,7 +77,7 @@ void MagicCircle::setDirection(const QString &direction) void MagicCircle::setSpeed(int speed) { - _timer.setInterval(_stepsize / speed * 1000); + _timer.setInterval(_stepsize / speed * 1000.0); } void MagicCircle::setResolution(int res) @@ -93,6 +93,9 @@ void MagicCircle::setFrequency(int frequency) void MagicCircle::tactileFeedback(float angle) { + _tactonPlayer->playAngle(angle);// * M_PI / 180); + return; + unsigned char amplitudes[4]; memset(amplitudes, 0, 4); diff --git a/TactonDebug/TactonDebug.suo b/TactonDebug/TactonDebug.suo index d16f968..a15620d 100644 Binary files a/TactonDebug/TactonDebug.suo and b/TactonDebug/TactonDebug.suo differ diff --git a/TactonDebug/TactonDebug/tactondebug.cpp b/TactonDebug/TactonDebug/tactondebug.cpp index d25c25b..e370ca7 100644 --- a/TactonDebug/TactonDebug/tactondebug.cpp +++ b/TactonDebug/TactonDebug/tactondebug.cpp @@ -1,5 +1,6 @@ #include "tactondebug.h" +#include int TactonDebug::MaxFrequency = 400; int TactonDebug::MaxAmplitude = 255; int TactonDebug::MaxDuration = 2000; @@ -14,6 +15,7 @@ TactonDebug::TactonDebug(QWidget *parent, Qt::WFlags flags) catch(...) { _tactonPlayer = NULL; + qDebug() << "No wristband found"; } setupUi(this); diff --git a/TactonLibrary.suo b/TactonLibrary.suo index e0587c7..74821d4 100644 Binary files a/TactonLibrary.suo and b/TactonLibrary.suo differ diff --git a/TactonPlayer/Tacton.hpp b/TactonPlayer/Tacton.hpp index d71859a..31cf631 100644 --- a/TactonPlayer/Tacton.hpp +++ b/TactonPlayer/Tacton.hpp @@ -14,10 +14,14 @@ #ifdef __MACOSX__ #define EXPORTED #else -#define EXPORTED __declspec(dllexport) + #ifdef _WINDLL + #define EXPORTED __declspec(dllexport) + #else + #define EXPORTED __declspec(dllimport) + #endif #endif -class Tacton +class EXPORTED Tacton { public: //creates a tacton using the raw data format @@ -26,46 +30,46 @@ class Tacton //2 for the duration //2 for the frequency //1 for the amplitude - EXPORTED Tacton(unsigned int nbframes, unsigned char *desc); + Tacton(unsigned int nbframes, unsigned char *desc); //creates a tacton using the CSV format //1 field for the number of frames, then 4 fields per frame //1 for the pattern //1 for the duration //1 for the frequency //1 for the amplitude - EXPORTED Tacton(char *desc); - EXPORTED Tacton(unsigned int nbframes, unsigned char *patterns, unsigned int *durations, unsigned int *frequencies, unsigned char *amplitudes); + Tacton(char *desc); + Tacton(unsigned int nbframes, unsigned char *patterns, unsigned int *durations, unsigned int *frequencies, unsigned char *amplitudes); //create a simple vibration - EXPORTED Tacton(char *pattern, unsigned int duration, unsigned int frequency, unsigned char amplitude); - EXPORTED Tacton(const char *pattern, unsigned int duration, unsigned int frequency, unsigned char amplitude); - EXPORTED Tacton(unsigned char pattern, unsigned int duration, unsigned int frequency, unsigned char amplitude); - EXPORTED Tacton(const Tacton &t); - EXPORTED ~Tacton(); + Tacton(char *pattern, unsigned int duration, unsigned int frequency, unsigned char amplitude); + Tacton(const char *pattern, unsigned int duration, unsigned int frequency, unsigned char amplitude); + Tacton(unsigned char pattern, unsigned int duration, unsigned int frequency, unsigned char amplitude); + Tacton(const Tacton &t); + ~Tacton(); //changes the pattern using raw format - EXPORTED void setPattern(char pattern); - EXPORTED void setPattern(unsigned int frame, char pattern); + void setPattern(char pattern); + void setPattern(unsigned int frame, char pattern); //changes the pattern using string format - EXPORTED void setPattern(char *pattern); - EXPORTED void setPattern(const char *pattern); - EXPORTED void setPattern(unsigned int frame, char *pattern); - EXPORTED void setPattern(unsigned int frame, const char *pattern); + void setPattern(char *pattern); + void setPattern(const char *pattern); + void setPattern(unsigned int frame, char *pattern); + void setPattern(unsigned int frame, const char *pattern); //changes the duration - EXPORTED void setDuration(unsigned int duration); - EXPORTED void setDuration(unsigned int frame, unsigned int duration); + void setDuration(unsigned int duration); + void setDuration(unsigned int frame, unsigned int duration); //changes the frequency - EXPORTED void setFrequency(unsigned int frequency); - EXPORTED void setFrequency(unsigned int frame, unsigned int frequency); + void setFrequency(unsigned int frequency); + void setFrequency(unsigned int frame, unsigned int frequency); //changes the amplitude - EXPORTED void setAmplitude(unsigned int amplitude); - EXPORTED void setAmplitude(unsigned int frame, unsigned int amplitude); + void setAmplitude(unsigned int amplitude); + void setAmplitude(unsigned int frame, unsigned int amplitude); //returns the number of frames - EXPORTED const unsigned int getNbFrames() const; + const unsigned int getNbFrames() const; //returns the raw code (useful to send to the serial port) - EXPORTED void *rawCode() const; + void *rawCode() const; private: unsigned int _nbframes; diff --git a/TactonPlayer/TactonPlayer.cpp b/TactonPlayer/TactonPlayer.cpp index 0868721..0b46a7c 100644 --- a/TactonPlayer/TactonPlayer.cpp +++ b/TactonPlayer/TactonPlayer.cpp @@ -144,6 +144,42 @@ void TactonPlayer::setAmplitudes(unsigned char nbtactors, unsigned char *amplitu delete[] buffer; } +void TactonPlayer::playAngle(unsigned int angle) +{ + if (!_comport) + return; + + unsigned char *buffer = new unsigned char[3]; + buffer[0] = 'a'; + buffer[1] = (unsigned char)((angle & 0x0000ff00) >> 8); + buffer[2] = (unsigned char)(angle & 0x000000ff); + _comport->WriteData(buffer, 3); + delete[] buffer; +} + +//plays an animation alterning a series of angles +void TactonPlayer::playAngleSequence(unsigned char nbangles, unsigned int frameduration, unsigned int nbframes, unsigned int *angles) +{ + if (!_comport) + return; + + unsigned char *buffer = new unsigned char[6 + 2 * nbangles]; + buffer[0] = 's'; + buffer[1] = nbangles; + buffer[2] = (unsigned char)((frameduration & 0x0000ff00) >> 8); + buffer[3] = (unsigned char)(frameduration & 0x000000ff); + buffer[4] = (unsigned char)((nbframes & 0x0000ff00) >> 8); + buffer[5] = (unsigned char)(nbframes & 0x000000ff); + + for (int i = 0 ; i < nbangles ; i++) + { + buffer[6 + 2 * i] = (angles[i] & 0x0000ff00) >> 8; + buffer[6 + 2 * i + 1] = angles[i] & 0x000000ff; + } + _comport->WriteData(buffer, 6 + 2 * nbangles); + delete[] buffer; +} + void TactonPlayer::stopBuzz() { if (!_comport) diff --git a/TactonPlayer/TactonPlayer.hpp b/TactonPlayer/TactonPlayer.hpp index a5be777..c79e2ae 100644 --- a/TactonPlayer/TactonPlayer.hpp +++ b/TactonPlayer/TactonPlayer.hpp @@ -5,41 +5,51 @@ #define EXPORTED #else #include -#define EXPORTED __declspec(dllexport) + #ifdef _WINDLL + #define EXPORTED __declspec(dllexport) + #else + #define EXPORTED __declspec(dllimport) + #endif #endif -class TactonPlayer +class EXPORTED TactonPlayer { public: - EXPORTED TactonPlayer(const char *port = "COM5"); - EXPORTED ~TactonPlayer(); + TactonPlayer(const char *port = "COM5"); + ~TactonPlayer(); //set the timestamp to 0, and watch for scheduled tactons - EXPORTED void start(); + void start(); //stop watching scheduled tactons, and erase registered tactons - EXPORTED void stop(); + void stop(); //register a tacton - EXPORTED void regist(const Tacton &t); + void regist(const Tacton &t); //register a list of tactons in a file, returns the number of tactons loaded - EXPORTED unsigned int registFile(char *filename); + unsigned int registFile(char *filename); //play a registered tacton - EXPORTED void play(unsigned char index); + void play(unsigned char index); //play a specified tacton - EXPORTED void play(const Tacton &t); + void play(const Tacton &t); //schedule the play of a registered tacton - EXPORTED void schedule(unsigned char index, unsigned long timestamp); + void schedule(unsigned char index, unsigned long timestamp); //sets an amplitude for each vibrator - EXPORTED void setAmplitudes(unsigned char nbtactors, unsigned char *amplitudes); + void setAmplitudes(unsigned char nbtactors, unsigned char *amplitudes); //sets a frequency for all vibrator - EXPORTED void setFrequency(unsigned int frequency); + void setFrequency(unsigned int frequency); //stop any vibration - EXPORTED void stopBuzz(); + void stopBuzz(); + + //sets a vibration on a virtual circle around 4 vibrators + void playAngle(unsigned int angle); + + //plays an animation alterning a series of angles + void playAngleSequence(unsigned char nbangles, unsigned int frameduration, unsigned int nbframes, unsigned int *angles); //read on the serial port - EXPORTED void debugRead(char *res, int nb) const; + void debugRead(char *res, int nb) const; private: Serial *_comport;