From: Thomas Pietrzak Date: Tue, 14 Jun 2011 17:06:24 +0000 (+0000) Subject: Tactons through library X-Git-Url: https://git.thomaspietrzak.com/?a=commitdiff_plain;h=f9e70b0d9f05081ef185f5af7a4b57e29f5ebcfa;p=hapticmetronome.git Tactons through library git-svn-id: svn+ssh://thomaspietrzak.com/var/svn/rep@23 47cf9a05-e0a8-4ed5-9e9b-101a649bc004 --- diff --git a/Haptic Metronome/Haptic Metronome.suo b/Haptic Metronome/Haptic Metronome.suo index 78d1928..57a05ef 100644 Binary files a/Haptic Metronome/Haptic Metronome.suo and b/Haptic Metronome/Haptic Metronome.suo differ diff --git a/Haptic Metronome/Haptic Metronome/Haptic Metronome.vcxproj b/Haptic Metronome/Haptic Metronome/Haptic Metronome.vcxproj index 861b312..1fa4b81 100644 --- a/Haptic Metronome/Haptic Metronome/Haptic Metronome.vcxproj +++ b/Haptic Metronome/Haptic Metronome/Haptic Metronome.vcxproj @@ -57,7 +57,7 @@ true - SDL.lib;SDLmain.lib;SDL_image.lib;SDL_mixer.lib;glew32.lib;opengl32.lib;glu32.lib;wiiuse.lib;Winmm.lib;%(AdditionalDependencies) + TactonPlayer.lib;SDL.lib;SDLmain.lib;SDL_image.lib;SDL_mixer.lib;glew32.lib;opengl32.lib;glu32.lib;wiiuse.lib;Winmm.lib;%(AdditionalDependencies) Windows msvcrt.lib @@ -74,7 +74,7 @@ false true true - SDL.lib;SDLmain.lib;SDL_image.lib;SDL_mixer.lib;glew32.lib;opengl32.lib;glu32.lib;wiiuse.lib;Winmm.lib;%(AdditionalDependencies) + TactonPlayer.lib;SDL.lib;SDLmain.lib;SDL_image.lib;SDL_mixer.lib;glew32.lib;opengl32.lib;glu32.lib;wiiuse.lib;Winmm.lib;%(AdditionalDependencies) Windows @@ -88,8 +88,6 @@ - - @@ -112,8 +110,6 @@ - - diff --git a/Haptic Metronome/Haptic Metronome/Haptic Metronome.vcxproj.filters b/Haptic Metronome/Haptic Metronome/Haptic Metronome.vcxproj.filters index 62ad99a..58be612 100644 --- a/Haptic Metronome/Haptic Metronome/Haptic Metronome.vcxproj.filters +++ b/Haptic Metronome/Haptic Metronome/Haptic Metronome.vcxproj.filters @@ -69,12 +69,6 @@ Header Files - - Header Files - - - Header Files - @@ -137,12 +131,6 @@ Source Files - - Source Files - - - Source Files - diff --git a/include/MetronomeHaptic.hpp b/include/MetronomeHaptic.hpp index e402770..35bc78f 100644 --- a/include/MetronomeHaptic.hpp +++ b/include/MetronomeHaptic.hpp @@ -9,8 +9,7 @@ #include #include - -#include "Serial.hpp" +#include class MetronomeHaptic : public Metronome { @@ -31,7 +30,7 @@ class MetronomeHaptic : public Metronome private: static int thread_func(void *unused); SDL_Thread *thread; - Serial *_comport; + TactonPlayer _tactonPlayer; }; #endif diff --git a/include/Serial.hpp b/include/Serial.hpp deleted file mode 100644 index f306af7..0000000 --- a/include/Serial.hpp +++ /dev/null @@ -1,35 +0,0 @@ -#ifndef _SERIAL_ -#define _SERIAL_ - -#define ARDUINO_WAIT_TIME 500 - -class Serial -{ - public: - //Initialize Serial communication with the given COM port - Serial(char *portName); - - //Close the connection - //NOTA: for some reason you can't connect again before exiting - //the program and running it again - virtual ~Serial(); - - //Read data in a buffer, if nbChar is greater than the - //maximum number of bytes available, it will return only the - //bytes available. The function return -1 when nothing could - //be read, the number of bytes actually read. - virtual int ReadData(void *buffer, unsigned int nbChar)=0; - - //Writes data from a buffer through the Serial connection - //return true on success. - virtual bool WriteData(void *buffer, unsigned int nbChar)=0; - - //Check if we are actually connected - bool IsConnected() { return _connected; } - - protected: - //Connection status - bool _connected; -}; - -#endif diff --git a/include/SerialWindows.hpp b/include/SerialWindows.hpp deleted file mode 100644 index 28eb1af..0000000 --- a/include/SerialWindows.hpp +++ /dev/null @@ -1,35 +0,0 @@ -#ifndef _SERIALWINDOWS_ -#define _SERIALWINDOWS_ - -#include "Serial.hpp" - -#include - -class SerialWindows : public Serial -{ - public: - //Initialize Serial communication with the given COM port - SerialWindows(char *portName); - //Close the connection - //NOTA: for some reason you can't connect again before exiting - //the program and running it again - ~SerialWindows(); - //Read data in a buffer, if nbChar is greater than the - //maximum number of bytes available, it will return only the - //bytes available. The function return -1 when nothing could - //be read, the number of bytes actually read. - int ReadData(void *buffer, unsigned int nbChar); - //Writes data from a buffer through the Serial connection - //return true on success. - bool WriteData(void *buffer, unsigned int nbChar); - - - private: - //Serial comm handler - HANDLE _hSerial; - //Get various information about the connection - COMSTAT _status; - //Keep track of last error - DWORD _errors; -}; -#endif diff --git a/src/MetronomeHaptic.cpp b/src/MetronomeHaptic.cpp index 1647169..2093a6a 100644 --- a/src/MetronomeHaptic.cpp +++ b/src/MetronomeHaptic.cpp @@ -4,62 +4,22 @@ #include -#ifdef WIN32 - #include "SerialWindows.hpp" -#else - #include "SerialLinux.hpp" -#endif - #define BEFOREDELAY 2000 extern Uint32 start; extern FILE *logfile; MetronomeHaptic::MetronomeHaptic(const Track &track, Uint32 nbbeats, Uint32 tempo, bool activated) -:Metronome(track, nbbeats, tempo, activated), thread(NULL) +:Metronome(track, nbbeats, tempo, activated), thread(NULL), _tactonPlayer("COM3") { - try - { - _comport = new SerialWindows("COM3"); - } - catch (char *error) - { - SDL_LogError(SDL_LOG_CATEGORY_ERROR, "%s\n", error); - _comport = new SerialWindows("COM5"); - } - unsigned char buffer1[3]; - buffer1[0] = 'N'; buffer1[1] = 0x00; buffer1[2] = 0x01; - unsigned char buffer2[6]; - buffer2[0] = 0x01; //pattern - buffer2[1] = 0; buffer2[2] = 50; //duration - buffer2[3] = 0; buffer2[4] = 250; //frequency - buffer2[5] = 255; //amplitude - - //Tacton 1 - _comport->WriteData(buffer1, 3); - _comport->WriteData(buffer2, 6); - - //Tacton 2 - buffer2[0] = 0x02; //pattern - _comport->WriteData(buffer1, 3); - _comport->WriteData(buffer2, 6); - - //Tacton 3 - buffer2[0] = 0x04; //pattern - _comport->WriteData(buffer1, 3); - _comport->WriteData(buffer2, 6); - - //Tacton 4 - buffer2[0] = 0x08; //pattern - _comport->WriteData(buffer1, 3); - _comport->WriteData(buffer2, 6); + //load the tactons + _tactonPlayer.registFile("bips.txt"); } MetronomeHaptic::~MetronomeHaptic() { - _comport->WriteData("Q", 1); + _tactonPlayer.stop(); _activated = false; - delete _comport; SDL_WaitThread(thread, NULL); } @@ -85,18 +45,10 @@ void MetronomeHaptic::init() void MetronomeHaptic::checkAt(Uint32 timestamp) { - unsigned char outBuffer[6]; - outBuffer[0] = 'P'; - //send first beeps in advance while (_iBips != _bips->end() && timestamp >= BIPTIMESTAMP(*_iBips)) { - outBuffer[1] = _currentbeat % 4; - outBuffer[2] = (BIPTIMESTAMP(*_iBips) & 0xff000000) >> 24; - outBuffer[3] = (BIPTIMESTAMP(*_iBips) & 0x00ff0000) >> 16; - outBuffer[4] = (BIPTIMESTAMP(*_iBips) & 0x0000ff00) >> 8; - outBuffer[5] = BIPTIMESTAMP(*_iBips) & 0x000000ff; - bool written = _comport->WriteData(outBuffer, 6); + _tactonPlayer.schedule(_currentbeat % 4, BIPTIMESTAMP(*_iBips)); if (logfile) fprintf(logfile, "BIP;%d;%d;%d\n", BIPTICK(*_iBips), BIPTIMESTAMP(*_iBips), 0); @@ -125,22 +77,11 @@ int MetronomeHaptic::thread_func(void *obj) MetronomeHaptic *thismetronome = static_cast(obj); // SDL_SetThreadPriority(SDL_THREAD_PRIORITY_HIGH); - - unsigned char outBuffer[6]; - outBuffer[0] = 'P'; - bool written = thismetronome->_comport->WriteData("S", 1); + thismetronome->_tactonPlayer.start(); while(thismetronome->_activated) { - // TEST - char INBUFFER[128]; - memset(INBUFFER, 0, 128); - int nbread = thismetronome->_comport->ReadData(INBUFFER, 128); - if (nbread > 0) - SDL_LogDebug(SDL_LOG_CATEGORY_APPLICATION, "%d read: %s\n", nbread, INBUFFER); - // - thismetronome->checkAt(SDL_GetTicks() - start + BEFOREDELAY); SDL_Delay(5); diff --git a/src/PaintGame.cpp b/src/PaintGame.cpp index afa03b5..791fa98 100644 --- a/src/PaintGame.cpp +++ b/src/PaintGame.cpp @@ -626,7 +626,7 @@ void loop(const Track &tr, const String &songname) Metronome *m = NULL, *m2 = NULL; try { - //m = new MetronomeHaptic(tr); + m = new MetronomeHaptic(tr); m2 = new MetronomeAudio(tr); } catch (char *error) diff --git a/src/Serial.cpp b/src/Serial.cpp deleted file mode 100644 index e527263..0000000 --- a/src/Serial.cpp +++ /dev/null @@ -1,10 +0,0 @@ -#include "Serial.hpp" - -Serial::Serial(char *) -:_connected(false) -{ -} - -Serial::~Serial() -{ -} diff --git a/src/SerialWindows.cpp b/src/SerialWindows.cpp deleted file mode 100644 index 584ff8c..0000000 --- a/src/SerialWindows.cpp +++ /dev/null @@ -1,143 +0,0 @@ -#include "SerialWindows.hpp" - -#include -using namespace std; - -#ifdef WIN32 -#include -#endif - -#include - -SerialWindows::SerialWindows(char *portName) -:Serial(portName) -{ - //Try to connect to the given port throuh CreateFile - _hSerial = CreateFile(portName, - GENERIC_READ | GENERIC_WRITE, - 0, - NULL, - OPEN_EXISTING, - FILE_FLAG_NO_BUFFERING | FILE_FLAG_WRITE_THROUGH, //FILE_ATTRIBUTE_NORMAL, - NULL); - - //Check if the connection was successfull - if(_hSerial == INVALID_HANDLE_VALUE) - { - //If not success full display an Error - if(GetLastError() == ERROR_FILE_NOT_FOUND) - SDL_LogError(SDL_LOG_CATEGORY_ERROR, "ERROR: Handle was not attached. Reason: %s not available.\n", portName); - else - cerr << "ERROR unknown" << endl; - char buffer[256]; - sprintf(buffer, "Port %s does not exist or not reachable", portName); - throw buffer; - } - else - { - /* - COMMTIMEOUTS commTimeouts; - commTimeouts.ReadIntervalTimeout = 1; - commTimeouts.ReadTotalTimeoutMultiplier = 10; - commTimeouts.ReadTotalTimeoutConstant = 100; - commTimeouts.WriteTotalTimeoutMultiplier = 10; - commTimeouts.WriteTotalTimeoutConstant = 100; - if (SetCommTimeouts(_comport, &commTimeouts) == 0) - throw "Cannot set COM port timeouts";*/ - - - //If connected we try to set the comm parameters - DCB dcbSerialParams = {0}; - - //Try to get the current - if (!GetCommState(_hSerial, &dcbSerialParams)) - //If impossible, show an error - SDL_LogError(SDL_LOG_CATEGORY_ERROR, "ERROR: failed to get current serial parameters!\n"); - else - { - //Define serial connection parameters for the arduino board - dcbSerialParams.BaudRate=CBR_57600; - dcbSerialParams.ByteSize=8; - dcbSerialParams.StopBits=ONESTOPBIT; - dcbSerialParams.Parity=NOPARITY; - - //Set the parameters and check for their proper application - if(!SetCommState(_hSerial, &dcbSerialParams)) - SDL_LogError(SDL_LOG_CATEGORY_ERROR, "ERROR: Could not set Serial Port parameters\n"); - else - { - //If everything went fine we're connected - _connected = true; - //We wait 2s as the arduino board will be reseting - SDL_Delay(ARDUINO_WAIT_TIME); - } - } - } -} - -SerialWindows::~SerialWindows() -{ - //Check if we are connected before trying to disconnect - if(_connected) - { - //We're no longer connected - _connected = false; - //Close the serial handler - CloseHandle(_hSerial); - } -} - -int SerialWindows::ReadData(void *buffer, unsigned int nbChar) -{ - //Number of bytes we'll have read - DWORD bytesRead = 0; - //Number of bytes we'll really ask to read - unsigned int toRead = 0; - - //Use the ClearCommError function to get status info on the Serial port - ClearCommError(_hSerial, &_errors, &_status); - - //Check if there is something to read - if(_status.cbInQue > 0) - { - //If there is we check if there is enough data to read the required number - //of characters, if not we'll read only the available characters to prevent - //locking of the application. - if(_status.cbInQue > nbChar) - toRead = nbChar; - else - toRead = _status.cbInQue; - - //Try to read the require number of chars, and return the number of read bytes on success - if(ReadFile(_hSerial, buffer, toRead, &bytesRead, NULL) && bytesRead != 0) - return bytesRead; - } - - //If nothing has been read, or that an error was detected return -1 - return -1; -} - - -bool SerialWindows::WriteData(void *buffer, unsigned int nbChar) -{ - DWORD bytesSend; - - //Try to write the buffer on the Serial port - if(!WriteFile(_hSerial, buffer, nbChar, &bytesSend, 0)) - { - //In case it don't work get comm error and return false - ClearCommError(_hSerial, &_errors, &_status); - - return false; - } - else - { - ClearCommError(_hSerial, &_errors, &_status); -/* if(!FlushFileBuffers(_hSerial)) - cout << "ERROR while flushing" << endl;*/ -/* stringstream s; - s << bytesSend << " SENT" << endl; - OutputDebugString(s.str().c_str());*/ - return true; - } -}