#include <mitkWiiMoteThread.h>
Public Types | |
typedef itk::ReceptorMemberCommand < mitk::WiiMoteAddOn > | ReceptorCommand |
typedef ReceptorCommand::Pointer | ReceptorCommandPointer |
Public Member Functions | |
WiiMoteThread () | |
~WiiMoteThread () | |
virtual void | SetSleepTime (int _arg) |
virtual int | GetSleepTime () const |
void | Run () |
void | StartWiiMote () |
void | StopWiiMote () |
void | ReconnectWiiMote () |
void | DetectWiiMotes () |
void | WiiMoteIRInput () |
void | WiiMoteButtonPressed (int buttonType) |
void | WiiMoteCalibrationInput () |
Static Public Member Functions | |
static void | OnStateChange (wiimote &remote, state_change_flags changed, const wiimote_state &newState) |
static ITK_THREAD_RETURN_TYPE | StartWiiMoteThread (void *data) |
Definition at line 21 of file mitkWiiMoteThread.h.
typedef itk::ReceptorMemberCommand<mitk::WiiMoteAddOn> mitk::WiiMoteThread::ReceptorCommand |
Definition at line 26 of file mitkWiiMoteThread.h.
typedef ReceptorCommand::Pointer mitk::WiiMoteThread::ReceptorCommandPointer |
Definition at line 29 of file mitkWiiMoteThread.h.
mitk::WiiMoteThread::WiiMoteThread | ( | ) |
Definition at line 23 of file mitkWiiMoteThread.cpp.
References wiimote::CallbackTriggerFlags, wiimote::ChangedCallback, CONNECTED, IR_CHANGED, and OnStateChange().
: m_MultiThreader(itk::MultiThreader::New()) , m_WiiMoteThreadFinished(itk::FastMutexLock::New()) , m_StopWiiMote(false) , m_ThreadID(-1) , m_LastRecordTime(0) , m_ReadDataOnce(false) //, m_SkipTimeSteps(4) //, m_CurrentTimeStep(0) , m_SleepTime(SLEEPDEFAULT) , m_InCalibrationMode(false) { // uses state-change callback to get notified of extension-related // events OnStateChange must be static, otherwise the this-operator // will be used and this callback can't convert it into the typedef // defined in the wiiyourself library m_WiiMote.ChangedCallback = &mitk::WiiMoteThread::OnStateChange; // allows flags to trigger a state change m_WiiMote.CallbackTriggerFlags = (state_change_flags) (CONNECTED | IR_CHANGED); }
mitk::WiiMoteThread::~WiiMoteThread | ( | ) |
Definition at line 45 of file mitkWiiMoteThread.cpp.
{ }
void mitk::WiiMoteThread::DetectWiiMotes | ( | ) |
TODO Detects all available Wiimotes.
Definition at line 199 of file mitkWiiMoteThread.cpp.
{ //unsigned detected = 0; //unsigned connectionTries = 0; //while(detected < 3 || connectionTrys < 5) //{ // ++connectionTrys; // // wiimote* newWiiMote; // if(!newWiiMote->Connect(wiimote::FIRST_AVAILABLE)) // break; // MITK_INFO << "New WiiMote detected :: Assigned ID: " << detected; // m_WiiMotes[detected++] = newWiiMote; //} }
virtual int mitk::WiiMoteThread::GetSleepTime | ( | ) | const [virtual] |
void mitk::WiiMoteThread::OnStateChange | ( | wiimote & | remote, |
state_change_flags | changed, | ||
const wiimote_state & | newState | ||
) | [static] |
Allows to set report types, detects extensions and responds to connect/disconnect
events of extension, such as MotionPlus.
NOTE: don't access the public state from the 'remote' object here, as it will be out-of-date (it's only updated via RefreshState() calls, and these are reserved for the main application so it can be sure the values stay consistent between calls). Instead query 'new_state' only.
remote | the old state of the connected Wii remote |
changed | the state change of the Wii remote |
newState | the new state, after the change is applied (i.e. new extension connected) |
Definition at line 49 of file mitkWiiMoteThread.cpp.
Referenced by WiiMoteThread().
{
// TODO check for extension and configure
}
void mitk::WiiMoteThread::ReconnectWiiMote | ( | ) |
Reconnects the Wiimote in case the connection is lost.
Definition at line 191 of file mitkWiiMoteThread.cpp.
References wiimote::FIRST_AVAILABLE, and MITK_INFO.
{ if(m_WiiMote.Connect(wiimote::FIRST_AVAILABLE)) { MITK_INFO << "WiiMote reconnected.."; } }
void mitk::WiiMoteThread::Run | ( | ) |
Starts the thread for the Wiimote.
Definition at line 54 of file mitkWiiMoteThread.cpp.
{ m_ThreadID = m_MultiThreader->SpawnThread(this->StartWiiMoteThread, this); }
virtual void mitk::WiiMoteThread::SetSleepTime | ( | int | _arg ) | [virtual] |
void mitk::WiiMoteThread::StartWiiMote | ( | ) |
Connects the Wiimote and allows access to its functionality.
Definition at line 76 of file mitkWiiMoteThread.cpp.
References CONNECTED, wiimote::FIRST_AVAILABLE, wiimote::IN_BUTTONS_ACCEL_IR, mitk::Key_A, mitk::Key_Home, MITK_INFO, and NO_CHANGE.
Referenced by StartWiiMoteThread().
{ // mutex lock should be inside each method, but since // the driver is external, one would have to change it // transfers the execution rights to the WiiMote thread m_WiiMoteThreadFinished->Lock(); // is needed in case the Wiimote was deactivated // while the application is running this->m_StopWiiMote = false; // transfers the execution rights back to the main thread m_WiiMoteThreadFinished->Unlock(); while(CONNECTED && !m_StopWiiMote) { // connection process if(m_WiiMote.Connect(wiimote::FIRST_AVAILABLE)) { m_WiiMoteThreadFinished->Lock(); MITK_INFO << "WiiMote connected.."; // 0x00 none // 0x0f all leds // 0x01 first led // 0x02 second led // 0x03 third led // 0x04 fourth led m_WiiMote.SetLEDs(0x01); // use a non-extension report mode (this gives us back the IR dot sizes) m_WiiMote.SetReportType(wiimote::IN_BUTTONS_ACCEL_IR); m_WiiMoteThreadFinished->Unlock(); break; } } // update and settings process while(!m_WiiMote.Button.Minus() && !m_StopWiiMote ) { m_WiiMoteThreadFinished->Lock(); while(m_WiiMote.RefreshState() == NO_CHANGE) { itksys::SystemTools::Delay(1); } m_WiiMoteThreadFinished->Unlock(); m_WiiMoteThreadFinished->Lock(); if(!m_InCalibrationMode) { this->WiiMoteIRInput(); } else { this->WiiMoteCalibrationInput(); } m_WiiMoteThreadFinished->Unlock(); if(m_WiiMote.Button.Home()) this->WiiMoteButtonPressed(mitk::Key_Home); if(m_WiiMote.Button.A()) { this->WiiMoteButtonPressed(mitk::Key_A); // necessary to avoid sending the movementvector // instead of the raw coordinates for the calibration if(this->m_InCalibrationMode) { this->m_InCalibrationMode = false; } else { this->m_InCalibrationMode = true; } itksys::SystemTools::Delay(3000); } if(m_WiiMote.ConnectionLost()) { m_WiiMoteThreadFinished->Lock(); this->ReconnectWiiMote(); m_WiiMoteThreadFinished->Unlock(); } //m_WiiMoteThreadFinished->Lock(); //m_WiiMote.SetRumble(m_WiiMote.Button.B()); //m_WiiMoteThreadFinished->Unlock(); } //if(m_WiiMote.IsConnected() && m_StopWiiMote) //{ //clean up m_WiiMoteThreadFinished->Lock(); m_WiiMote.Disconnect(); m_WiiMoteThreadFinished->Unlock(); MITK_INFO << "WiiMote disconnected.."; //} return; }
ITK_THREAD_RETURN_TYPE mitk::WiiMoteThread::StartWiiMoteThread | ( | void * | data ) | [static] |
Helper function, because the itk::MultiThreader can only
start a new thread with a static member function.
Definition at line 59 of file mitkWiiMoteThread.cpp.
References StartWiiMote().
{ // extract this pointer from Thread Info structure struct itk::MultiThreader::ThreadInfoStruct * pInfo = (struct itk::MultiThreader::ThreadInfoStruct*)pInfoStruct; if (pInfo == NULL) return ITK_THREAD_RETURN_VALUE; if (pInfo->UserData == NULL) return ITK_THREAD_RETURN_VALUE; mitk::WiiMoteThread* wiiMoteThread = static_cast<mitk::WiiMoteThread*>(pInfo->UserData); if (wiiMoteThread != NULL) wiiMoteThread->StartWiiMote(); return ITK_THREAD_RETURN_VALUE; }
void mitk::WiiMoteThread::StopWiiMote | ( | ) |
Stops the running thread.
Definition at line 186 of file mitkWiiMoteThread.cpp.
{
this->m_StopWiiMote = true;
}
void mitk::WiiMoteThread::WiiMoteButtonPressed | ( | int | buttonType ) |
Reads incoming data from buttons. After processing the data
(e.g. computations, assigning commands...) fires Wiimote events accordingly.
buttonType | the type of button, that was used to trigger this event |
Definition at line 272 of file mitkWiiMoteThread.cpp.
References mitk::BS_NoButton, mitk::CallbackFromGUIThread::CallThisFromGUIThread(), mitk::CallbackFromGUIThread::GetInstance(), mitk::WiiMoteAddOn::GetInstance(), mitk::WiiMoteButtonEvent::MakeObject(), and mitk::WiiMoteAddOn::WiiMoteButtonPressed().
{ m_Command = ReceptorCommand::New(); m_Command->SetCallbackFunction(mitk::WiiMoteAddOn::GetInstance(), &mitk::WiiMoteAddOn::WiiMoteButtonPressed); mitk::WiiMoteButtonEvent e(mitk::Type_WiiMoteButton, mitk::BS_NoButton, mitk::BS_NoButton, buttonType); mitk::CallbackFromGUIThread::GetInstance()->CallThisFromGUIThread(m_Command, e.MakeObject()); }
void mitk::WiiMoteThread::WiiMoteCalibrationInput | ( | ) |
Reads incoming data from the IR camera. Afterwards the raw x and y coordinates
are stored in an event and fired as an event.
Definition at line 281 of file mitkWiiMoteThread.cpp.
References mitk::CallbackFromGUIThread::CallThisFromGUIThread(), mitk::CallbackFromGUIThread::GetInstance(), mitk::WiiMoteAddOn::GetInstance(), IR_CHANGED, mitk::WiiMoteCalibrationEvent::MakeObject(), and mitk::WiiMoteAddOn::WiiMoteCalibrationInput().
{ if(IR_CHANGED && m_WiiMote.IR.Dot[0].bVisible) { m_Command = ReceptorCommand::New(); m_Command->SetCallbackFunction(mitk::WiiMoteAddOn::GetInstance(), &mitk::WiiMoteAddOn::WiiMoteCalibrationInput); mitk::WiiMoteCalibrationEvent e(m_WiiMote.IR.Dot[0].RawX,m_WiiMote.IR.Dot[0].RawY); mitk::CallbackFromGUIThread::GetInstance()->CallThisFromGUIThread(m_Command, e.MakeObject()); } itksys::SystemTools::Delay(m_SleepTime); }
void mitk::WiiMoteThread::WiiMoteIRInput | ( | ) |
Reads incoming data from the IR camera. After processing the data
(e.g. computations, assigning commands...) fires Wiimote events accordingly.
Definition at line 218 of file mitkWiiMoteThread.cpp.
References mitk::CallbackFromGUIThread::CallThisFromGUIThread(), mitk::CallbackFromGUIThread::GetInstance(), mitk::WiiMoteAddOn::GetInstance(), IR_CHANGED, mitk::WiiMoteIREvent::MakeObject(), TIMELIMIT, and mitk::WiiMoteAddOn::WiiMoteInput().
{ if(IR_CHANGED && m_WiiMote.IR.Dot[0].bVisible) { //m_CurrentTimeStep++; //if(m_CurrentTimeStep > m_SkipTimeSteps) //{ m_Command = ReceptorCommand::New(); m_Command->SetCallbackFunction(mitk::WiiMoteAddOn::GetInstance(), &mitk::WiiMoteAddOn::WiiMoteInput); double tempTime(itksys::SystemTools::GetTime()); float inputCoordinates[2] = {m_WiiMote.IR.Dot[0].RawX, m_WiiMote.IR.Dot[0].RawY}; mitk::Point2D tempPoint(inputCoordinates); // if the last read data is not valid, // because the thread was not started if(!m_ReadDataOnce) { m_ReadDataOnce = true; } else // there is old data available - calculate the movement and send event { // this time constraint allows the user to move the camera // with the IR sender (by switching the IR source on and off) // similiar to a mouse (e.g. if you lift the mouse from the // surface and put it down again on another position, there // was no input. Now the input starts again and the movement // will begin from the last location of the mouse, although // the physical position of the mouse changed.) if ((tempTime-m_LastRecordTime) < TIMELIMIT) { mitk::Vector2D resultingVector(m_LastReadData-tempPoint); mitk::WiiMoteIREvent e(resultingVector, tempTime); mitk::CallbackFromGUIThread::GetInstance()->CallThisFromGUIThread(m_Command, e.MakeObject()); } } m_LastRecordTime = tempTime; m_LastReadData = tempPoint; itksys::SystemTools::Delay(m_SleepTime); //m_CurrentTimeStep = 0; //} } //read data //MITK_INFO << "Pitch: " << m_WiiMote.Acceleration.Orientation.Pitch; //MITK_INFO << "Roll: " << m_WiiMote.Acceleration.Orientation.Roll; //MITK_INFO << "X: " << m_WiiMote.Acceleration.Orientation.X; //MITK_INFO << "Y: " << m_WiiMote.Acceleration.Orientation.Y; //MITK_INFO << "Z: " << m_WiiMote.Acceleration.Orientation.Z; }