From f9e70b0d9f05081ef185f5af7a4b57e29f5ebcfa Mon Sep 17 00:00:00 2001 From: Thomas Pietrzak Date: Tue, 14 Jun 2011 17:06:24 +0000 Subject: [PATCH] Tactons through library git-svn-id: svn+ssh://thomaspietrzak.com/var/svn/rep@23 47cf9a05-e0a8-4ed5-9e9b-101a649bc004 --- Haptic Metronome/Haptic Metronome.suo | Bin 48128 -> 47616 bytes .../Haptic Metronome/Haptic Metronome.vcxproj | 8 +- .../Haptic Metronome.vcxproj.filters | 12 -- include/MetronomeHaptic.hpp | 5 +- include/Serial.hpp | 35 ----- include/SerialWindows.hpp | 35 ----- src/MetronomeHaptic.cpp | 71 +-------- src/PaintGame.cpp | 2 +- src/Serial.cpp | 10 -- src/SerialWindows.cpp | 143 ------------------ 10 files changed, 11 insertions(+), 310 deletions(-) delete mode 100644 include/Serial.hpp delete mode 100644 include/SerialWindows.hpp delete mode 100644 src/Serial.cpp delete mode 100644 src/SerialWindows.cpp diff --git a/Haptic Metronome/Haptic Metronome.suo b/Haptic Metronome/Haptic Metronome.suo index 78d1928eb70c21c149b796fcacfc0be99fc6df3f..57a05efe38d0732fe8572609b0448e5dc2963944 100644 GIT binary patch delta 1407 zcmb7EZA?>V6n@{^Te*d*(3OIPjiP{*!N%8y@-eA4&b45xV}K*BWkWk)6H_m2?Fi{+ z$24>cau9VPQ;3NgvrTw)pT>1zOZc%2OuEb;{xJU0A9HScXG)gQom(#T*AjQ{eRI$A zJm)><+cvY5xebccK{*4v;Q^br*Ma;`5t9V&p zfuwDEIHD_NQkIh!3emp=7hGLc2%@P;RzNa21$2#>6@iJl^PEtGXib92b5ISHo${6` zDIeplLzKJBh(&Ygl#bA#P#_iu;?)vZD7MFwJgG=4ENFae1b@j38k&XnT18+|$rTEP zw(Oh~WUOQld6o1M59uc@q?7C;6{MWh$cx>1x|xF+zD(VWj$Pm@w+kk;^tj^BCzU(F znqLcnit8|5DL^SNK(NxpLY0>}x)-L*jx}}vk$|wNsu2vHQpUt;p3(;J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5ct1?ARI=s8#;r(2}}*bL@7=#>faC`qHda+kWII2iYs!O@Zizje2a zv@~JGa-e@(8t4@9BXQHrED9XIIk*AD9~3wr(uhi&rp~=SEP*zou9-`s}Ls#WcZ{m9b8O< yXLZ24(P|j;2yEH&t!(9Q;PWAvO)UWfk2Q(P$FCAjc=Tw%QShc3{`KmFq`v{73jhNE 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; - } -} -- 2.30.2