/** * Reduce the number of data to output. Inform if the data is interesting or not. * @return TRUE if data is newed ; FALSE if data is useless. */ bool WmDevice::reduceSentData( CWiimote &wm ) { bool hasChanged=false ; int timeOut=0 ; // If( current state != previous state ) // Save & send. // else // Wait 10 ms before to send data. m_t2 = m_time.elapsed() ; timeOut = m_t2 - m_t1 ; // TimeOut ? if( timeOut > PLUGIN_WM_TIMEOUT_BEFORE_SENDDATA ) hasChanged = true ; if( !hasChanged ) { // Action Wiimote ? if( wm.GetEvent()!=CWiimote::EVENT_NONE && (wm.GetEvent()!=m_previousEvent || wm.Buttons.isJustChanged()) ) hasChanged = true ; } if( !hasChanged ) { // Action Nunchuk ? int exType = wm.ExpansionDevice.GetType(); if( exType == wm.ExpansionDevice.TYPE_NUNCHUK ) { if( wm.ExpansionDevice.Nunchuk.Buttons.isJustChanged() ) hasChanged = true ; } } // Has changed ? if( hasChanged ) { // YES, update data. m_t1 = m_t2 ; m_previousEvent = wm.GetEvent() ; return true ; } else // NO, no update. return false ; }
void HandleStatus(CWiimote &wm) { printf("\n"); printf("--- CONTROLLER STATUS [wiimote id %i] ---\n\n", wm.GetID()); printf("attachment: %i\n", wm.ExpansionDevice.GetType()); printf("speaker: %i\n", wm.isUsingSpeaker()); printf("ir: %i\n", wm.isUsingIR()); printf("leds: %i %i %i %i\n", wm.isLEDSet(1), wm.isLEDSet(2), wm.isLEDSet(3), wm.isLEDSet(4)); printf("battery: %f %%\n", wm.GetBatteryLevel()); }
void WiimoteManager::handle_event(CWiimote& wm) { std::lock_guard<std::mutex> lock(m_mutex); char prefixString[64]; sprintf(prefixString, "Controller [%i]: ", wm.GetID()); if(wm.Buttons.isJustPressed(CButtons::BUTTON_UP)) { //g_orientation = glm::vec3(); } if(wm.Buttons.isJustPressed(CButtons::BUTTON_RIGHT)) { log_info("activating gyro"); wm.EnableMotionPlus(CWiimote::ON); wm.ExpansionDevice.MotionPlus.Gyroscope.SetGyroThreshold(1); } if(wm.Buttons.isJustPressed(CButtons::BUTTON_LEFT)) { wm.EnableMotionPlus(CWiimote::OFF); } if(wm.Buttons.isJustPressed(CButtons::BUTTON_A)) { m_accumulated = glm::vec3(0.0f, 0.0f, 0.0f); m_gyro_orientation = glm::quat(); m_gyro_pitch = 0.0f; m_gyro_yaw = 0.0f; m_gyro_roll = 0.0f; } if(wm.isUsingACC()) { // We are not using WiiCs orientation calculation as it doesn't // work properly when roll and ptich are in effect at the same // time glm::vec3 gravity; wm.Accelerometer.GetGravityVector(gravity.x, gravity.y, gravity.z); // convert WiiC gravity vector into OpenGL coordinate system std::swap(gravity.y, gravity.z); gravity.z = -gravity.z; float alpha = 0.05f; m_smoothed_gravity = alpha * gravity + (1.0f - alpha) * m_smoothed_gravity; gravity = m_smoothed_gravity; // calculate the roll float roll = atan2f(gravity.y, gravity.x) - glm::half_pi<float>(); glm::quat roll_rot = glm::quat(glm::vec3(0.0f, 0.0f, roll)); // remove the roll from the gravity vector, so that pitch can be calculated properly glm::quat rot_undo = glm::normalize(glm::quat(glm::vec3(0.0f, 0.0f, -roll))); gravity = rot_undo * gravity; // calculate pitch float pitch = atan2f(gravity.z, gravity.y); glm::quat pitch_rot = glm::quat(glm::vec3(pitch, 0.0f, 0.0f)); m_accel_orientation = pitch_rot * roll_rot; m_accel_roll = roll; m_accel_pitch = pitch; m_yaw = glm::yaw(m_gyro_orientation); m_pitch = glm::pitch(m_gyro_orientation); m_roll = glm::roll(m_gyro_orientation); //m_accel_orientation = glm::quat(glm::vec3(0.0f, glm::radians(glm::yaw(m_gyro_orientation)), 0.0f)) * m_accel_orientation; //std::cout << m_yaw << std::endl; //printf("%8.2f %8.2f %8.2f %8.2f %8.2f\n", glm::degrees(pitch), glm::degrees(roll), gravity.x, gravity.y, gravity.z); } // if the Motion Plus is turned on then print angles if(wm.isUsingMotionPlus()) { float pitch, yaw, roll; wm.ExpansionDevice.MotionPlus.Gyroscope.GetRates(roll, pitch, yaw); // convert into radians roll = glm::radians(roll * 0.01f); pitch = glm::radians(pitch * -0.01f); yaw = glm::radians(yaw * 0.01f); //glm::vec3 rot_x = m_gyro_orientation * glm::vec3(1.0f, 0.0f, 0.0f); glm::vec3 rot = m_gyro_orientation * glm::vec3(0.0f, 0.0f, 1.0f); //glm::vec3 rot_z = m_gyro_orientation * glm::vec3(0.0f, 0.0f, 1.0f); m_gyro_pitch = m_accel_pitch; m_gyro_yaw = atan2(rot.x, rot.z); m_gyro_roll = m_accel_roll; //glm::quat pitch(glm::vec3(m_gyro_pitch, 0.0f, 0.0f)); //glm::quat yaw(glm::vec3(0.0f, m_gyro_yaw, 0.0f)); //glm::quat roll(glm::vec3(0.0f, 0.0f, m_gyro_roll)); //return yaw * pitch * roll; m_gyro_orientation = m_gyro_orientation * glm::quat(glm::vec3(pitch, yaw, roll)); //glm::quat qpitch(glm::vec3(m_accel_pitch, 0.0f, 0.0f)); //glm::quat qyaw(glm::vec3(0.0f, m_gyro_yaw, 0.0f)); //glm::quat qroll(glm::vec3(0.0f, 0.0f, m_accel_roll)); //m_orientation = /*qyaw */ qpitch * qroll; //glm::vec3 angles = glm::eulerAngles(g_wiimote.get_orientation()); //glm::vec3 accum = g_wiimote.get_accumulated(); //printf("wiimote: %8.2f %8.2f %8.2f -- ", glm::degrees(angles.x), glm::degrees(angles.y), glm::degrees(angles.z)); //printf("%8.2f %8.2f %8.2f\n", glm::degrees(accum.x), glm::degrees(accum.y), glm::degrees(accum.z)); } if(wm.isUsingIR()) { std::vector<CIRDot>::iterator i; int x, y; int index; printf("%s Num IR Dots: %i\n", prefixString, wm.IR.GetNumDots()); printf("%s IR State: %u\n", prefixString, wm.IR.GetState()); std::vector<CIRDot>& dots = wm.IR.GetDots(); for(index = 0, i = dots.begin(); i != dots.end(); ++index, ++i) { if((*i).isVisible()) { (*i).GetCoordinate(x, y); printf("%s IR source %i: (%i, %i)\n", prefixString, index, x, y); wm.IR.GetCursorPosition(x, y); printf("%s IR cursor: (%i, %i)\n", prefixString, x, y); printf("%s IR z distance: %f\n", prefixString, wm.IR.GetDistance()); } } } if (false) { printf("a.p: %8.2f a.r: %8.2f -- g.p: %8.2f g.y: %8.2f g.r: %8.2f\n", glm::degrees(m_accel_pitch), glm::degrees(m_accel_roll), glm::degrees(m_gyro_pitch), glm::degrees(m_gyro_yaw), glm::degrees(m_gyro_roll)); } }
void WiimoteInputManager::handleStatus(CWiimote &wiimote, unsigned int controllerNumber) { std::cout << "Wiimote Status for Wiimote: " << controllerNumber << std::endl; std::cout << "battery level: " << wiimote.GetBatteryLevel() << "%" << std::endl; }
void WiimoteInputManager::handleEvent(CWiimote& wiimote, unsigned int controllerNumber) { { // pressed if(wiimote.Buttons.isJustPressed(CButtons::BUTTON_A)) notifyButtonListeners(WIIMOTE_EVENT_BUTTON_A_PRESSED, ButtonEvent(ButtonEvent::PRESSED), controllerNumber); if(wiimote.Buttons.isJustPressed(CButtons::BUTTON_B)) notifyButtonListeners(WIIMOTE_EVENT_BUTTON_B_PRESSED, ButtonEvent(ButtonEvent::PRESSED), controllerNumber); if(wiimote.Buttons.isJustPressed(CButtons::BUTTON_ONE)) notifyButtonListeners(WIIMOTE_EVENT_BUTTON_1_PRESSED, ButtonEvent(ButtonEvent::PRESSED), controllerNumber); if(wiimote.Buttons.isJustPressed(CButtons::BUTTON_TWO)) notifyButtonListeners(WIIMOTE_EVENT_BUTTON_2_PRESSED, ButtonEvent(ButtonEvent::PRESSED), controllerNumber); if(wiimote.Buttons.isJustPressed(CButtons::BUTTON_LEFT)) notifyButtonListeners(WIIMOTE_EVENT_BUTTON_LEFT_PRESSED, ButtonEvent(ButtonEvent::PRESSED), controllerNumber); if(wiimote.Buttons.isJustPressed(CButtons::BUTTON_RIGHT)) notifyButtonListeners(WIIMOTE_EVENT_BUTTON_RIGHT_PRESSED, ButtonEvent(ButtonEvent::PRESSED), controllerNumber); if(wiimote.Buttons.isJustPressed(CButtons::BUTTON_UP)) notifyButtonListeners(WIIMOTE_EVENT_BUTTON_UP_PRESSED, ButtonEvent(ButtonEvent::PRESSED), controllerNumber); if(wiimote.Buttons.isJustPressed(CButtons::BUTTON_DOWN)) notifyButtonListeners(WIIMOTE_EVENT_BUTTON_DOWN_PRESSED, ButtonEvent(ButtonEvent::PRESSED), controllerNumber); if(wiimote.Buttons.isJustPressed(CButtons::BUTTON_MINUS)) notifyButtonListeners(WIIMOTE_EVENT_BUTTON_MINUS_PRESSED, ButtonEvent(ButtonEvent::PRESSED), controllerNumber); if(wiimote.Buttons.isJustPressed(CButtons::BUTTON_PLUS)) notifyButtonListeners(WIIMOTE_EVENT_BUTTON_PLUS_PRESSED, ButtonEvent(ButtonEvent::PRESSED), controllerNumber); if(wiimote.Buttons.isJustPressed(CButtons::BUTTON_HOME)) { notifyButtonListeners(WIIMOTE_EVENT_BUTTON_HOME_PRESSED, ButtonEvent(ButtonEvent::PRESSED), controllerNumber); trackIR = !trackIR; } // Released if(wiimote.Buttons.isReleased(CButtons::BUTTON_A)) notifyButtonListeners(WIIMOTE_EVENT_BUTTON_A_RELEASED, ButtonEvent(ButtonEvent::RELEASED), controllerNumber); if(wiimote.Buttons.isReleased(CButtons::BUTTON_B)) notifyButtonListeners(WIIMOTE_EVENT_BUTTON_B_RELEASED, ButtonEvent(ButtonEvent::RELEASED), controllerNumber); if(wiimote.Buttons.isReleased(CButtons::BUTTON_ONE)) notifyButtonListeners(WIIMOTE_EVENT_BUTTON_1_RELEASED, ButtonEvent(ButtonEvent::RELEASED), controllerNumber); if(wiimote.Buttons.isReleased(CButtons::BUTTON_TWO)) notifyButtonListeners(WIIMOTE_EVENT_BUTTON_2_RELEASED, ButtonEvent(ButtonEvent::RELEASED), controllerNumber); if(wiimote.Buttons.isReleased(CButtons::BUTTON_LEFT)) notifyButtonListeners(WIIMOTE_EVENT_BUTTON_LEFT_RELEASED, ButtonEvent(ButtonEvent::RELEASED), controllerNumber); if(wiimote.Buttons.isReleased(CButtons::BUTTON_RIGHT)) notifyButtonListeners(WIIMOTE_EVENT_BUTTON_RIGHT_RELEASED, ButtonEvent(ButtonEvent::RELEASED), controllerNumber); if(wiimote.Buttons.isReleased(CButtons::BUTTON_UP)) notifyButtonListeners(WIIMOTE_EVENT_BUTTON_UP_RELEASED, ButtonEvent(ButtonEvent::RELEASED), controllerNumber); if(wiimote.Buttons.isReleased(CButtons::BUTTON_DOWN)) notifyButtonListeners(WIIMOTE_EVENT_BUTTON_DOWN_RELEASED, ButtonEvent(ButtonEvent::RELEASED), controllerNumber); if(wiimote.Buttons.isReleased(CButtons::BUTTON_MINUS)) notifyButtonListeners(WIIMOTE_EVENT_BUTTON_MINUS_RELEASED, ButtonEvent(ButtonEvent::RELEASED), controllerNumber); if(wiimote.Buttons.isReleased(CButtons::BUTTON_PLUS)) notifyButtonListeners(WIIMOTE_EVENT_BUTTON_PLUS_RELEASED, ButtonEvent(ButtonEvent::RELEASED), controllerNumber); if(wiimote.Buttons.isReleased(CButtons::BUTTON_HOME)) notifyButtonListeners(WIIMOTE_EVENT_BUTTON_HOME_RELEASED, ButtonEvent(ButtonEvent::RELEASED), controllerNumber); // held if(wiimote.Buttons.isHeld(CButtons::BUTTON_A)) notifyButtonListeners(WIIMOTE_EVENT_BUTTON_A_HELD, ButtonEvent(ButtonEvent::HELD), controllerNumber); if(wiimote.Buttons.isHeld(CButtons::BUTTON_B)) notifyButtonListeners(WIIMOTE_EVENT_BUTTON_B_HELD, ButtonEvent(ButtonEvent::HELD), controllerNumber); if(wiimote.Buttons.isHeld(CButtons::BUTTON_ONE)) notifyButtonListeners(WIIMOTE_EVENT_BUTTON_1_HELD, ButtonEvent(ButtonEvent::HELD), controllerNumber); if(wiimote.Buttons.isHeld(CButtons::BUTTON_TWO)) notifyButtonListeners(WIIMOTE_EVENT_BUTTON_2_HELD, ButtonEvent(ButtonEvent::HELD), controllerNumber); if(wiimote.Buttons.isHeld(CButtons::BUTTON_LEFT)) notifyButtonListeners(WIIMOTE_EVENT_BUTTON_LEFT_HELD, ButtonEvent(ButtonEvent::HELD), controllerNumber); if(wiimote.Buttons.isHeld(CButtons::BUTTON_RIGHT)) notifyButtonListeners(WIIMOTE_EVENT_BUTTON_RIGHT_HELD, ButtonEvent(ButtonEvent::HELD), controllerNumber); if(wiimote.Buttons.isHeld(CButtons::BUTTON_UP)) notifyButtonListeners(WIIMOTE_EVENT_BUTTON_UP_HELD, ButtonEvent(ButtonEvent::HELD), controllerNumber); if(wiimote.Buttons.isHeld(CButtons::BUTTON_DOWN)) notifyButtonListeners(WIIMOTE_EVENT_BUTTON_DOWN_HELD, ButtonEvent(ButtonEvent::HELD), controllerNumber); if(wiimote.Buttons.isHeld(CButtons::BUTTON_MINUS)) notifyButtonListeners(WIIMOTE_EVENT_BUTTON_MINUS_HELD, ButtonEvent(ButtonEvent::HELD), controllerNumber); if(wiimote.Buttons.isHeld(CButtons::BUTTON_PLUS)) notifyButtonListeners(WIIMOTE_EVENT_BUTTON_PLUS_HELD, ButtonEvent(ButtonEvent::HELD), controllerNumber); if(wiimote.Buttons.isHeld(CButtons::BUTTON_HOME)) notifyButtonListeners(WIIMOTE_EVENT_BUTTON_HOME_HELD, ButtonEvent(ButtonEvent::HELD), controllerNumber); } // buttons if(wiimote.isUsingACC()) { } // if the Motion Plus is turned on then print angles if(wiimote.isUsingMotionPlus()) { } // if(IR tracking is on then print the coordinates if(wiimote.isUsingIR() && trackIR) { { int x, y; wiimote.IR.GetCursorPosition(x, y); // std::cout << "cursor position :" << x << " | " << y << std::endl; float relativeX = (float)x / 10000.0f; float relativeY = (float)y / 10000.0f; float eps = 0.01; if (relativeX < eps || relativeY < eps) { relativeX = lastPointerX; relativeY = lastPointerY; } lastPointerX = relativeX; lastPointerY = relativeY; notifyAnalogListeners(WIIMOTE_EVENT_CURSOR_RELATIVE_X_Y, WiimoteAnalogEvent(controllerNumber, relativeX, 1.0f - relativeY, 0)); } { // code derived from the sample int constants[] = {WIIMOTE_EVENT_DOT_0_X_Y_SIZE, WIIMOTE_EVENT_DOT_1_X_Y_SIZE, WIIMOTE_EVENT_DOT_2_X_Y_SIZE, WIIMOTE_EVENT_DOT_3_X_Y_SIZE }; int i = 0; std::vector<CIRDot>& dots = wiimote.IR.GetDots(); for(std::vector<CIRDot >::iterator it = dots.begin(); it != dots.end(); it++) { int x, y; // (*it).GetCoordinate(x, y); (*it).GetRawCoordinate(x, y); float size = 0; if ((*it).isVisible()) size = (*it).GetSize(); notifyAnalogListeners(constants[i], WiimoteAnalogEvent(controllerNumber, x, y, size)); i++; } } } int exType = wiimote.ExpansionDevice.GetType(); if(exType == wiimote.ExpansionDevice.TYPE_NUNCHUK) { CNunchuk &nc = wiimote.ExpansionDevice.Nunchuk; if(nc.Buttons.isPressed(CNunchukButtons::BUTTON_C)) notifyButtonListeners(WIIMOTE_EVENT_BUTTON_C_PRESSED, ButtonEvent(ButtonEvent::PRESSED), controllerNumber); if(nc.Buttons.isPressed(CNunchukButtons::BUTTON_Z)) notifyButtonListeners(WIIMOTE_EVENT_BUTTON_Z_PRESSED, ButtonEvent(ButtonEvent::PRESSED), controllerNumber); if(nc.Buttons.isReleased(CNunchukButtons::BUTTON_C)) notifyButtonListeners(WIIMOTE_EVENT_BUTTON_C_RELEASED, ButtonEvent(ButtonEvent::RELEASED), controllerNumber); if(nc.Buttons.isReleased(CNunchukButtons::BUTTON_Z)) notifyButtonListeners(WIIMOTE_EVENT_BUTTON_Z_RELEASED, ButtonEvent(ButtonEvent::RELEASED), controllerNumber); if(nc.Buttons.isHeld(CNunchukButtons::BUTTON_C)) notifyButtonListeners(WIIMOTE_EVENT_BUTTON_C_HELD, ButtonEvent(ButtonEvent::HELD), controllerNumber); if(nc.Buttons.isHeld(CNunchukButtons::BUTTON_Z)) notifyButtonListeners(WIIMOTE_EVENT_BUTTON_Z_HELD, ButtonEvent(ButtonEvent::HELD), controllerNumber); float angle, magnitude; nc.Joystick.GetPosition(angle, magnitude); float m2 = magnitude * magnitude; if (m2 > 2.0f || magnitude == 0.0f) return; glm::vec2 direction = math::pol2Cart(math::deg2Rad(angle - 90.0f), magnitude); notifyAnalogListeners(WIIMOTE_EVENT_NUNCHUCK_ANALOG_X_Y, WiimoteAnalogEvent(controllerNumber, direction.x, -direction.y, magnitude)); } }
void HandleEvent(CWiimote &wm) { char prefixString[64]; if(wm.Buttons.isJustPressed(CButtons::BUTTON_MINUS)) { wm.SetMotionSensingMode(CWiimote::OFF); } if(wm.Buttons.isJustPressed(CButtons::BUTTON_PLUS)) { wm.SetMotionSensingMode(CWiimote::ON); } if(wm.Buttons.isJustPressed(CButtons::BUTTON_DOWN)) { wm.IR.SetMode(CIR::OFF); } if(wm.Buttons.isJustPressed(CButtons::BUTTON_UP)) { wm.IR.SetMode(CIR::ON); } if(wm.Buttons.isJustPressed(CButtons::BUTTON_RIGHT)) { wm.EnableMotionPlus(CWiimote::ON); } if(wm.Buttons.isJustPressed(CButtons::BUTTON_LEFT)) { wm.EnableMotionPlus(CWiimote::OFF); } if(wm.Buttons.isJustPressed(CButtons::BUTTON_B)) { wm.ToggleRumble(); } sprintf(prefixString, "Controller [%i]: ", wm.GetID()); if(wm.Buttons.isPressed(CButtons::BUTTON_A)) { printf("%s A pressed\n", prefixString); } if(wm.Buttons.isPressed(CButtons::BUTTON_B)) { printf("%s B pressed\n", prefixString); } if(wm.Buttons.isPressed(CButtons::BUTTON_UP)) { printf("%s Up pressed\n", prefixString); } if(wm.Buttons.isPressed(CButtons::BUTTON_DOWN)) { printf("%s Down pressed\n", prefixString); } if(wm.Buttons.isPressed(CButtons::BUTTON_LEFT)) { printf("%s Left pressed\n", prefixString); } if(wm.Buttons.isPressed(CButtons::BUTTON_RIGHT)) { printf("%s Right pressed\n", prefixString); } if(wm.Buttons.isPressed(CButtons::BUTTON_MINUS)) { printf("%s Minus pressed\n", prefixString); } if(wm.Buttons.isPressed(CButtons::BUTTON_PLUS)) { printf("%s Plus pressed\n", prefixString); } if(wm.Buttons.isPressed(CButtons::BUTTON_ONE)) { printf("%s One pressed\n", prefixString); } if(wm.Buttons.isPressed(CButtons::BUTTON_TWO)) { printf("%s Two pressed\n", prefixString); } if(wm.Buttons.isPressed(CButtons::BUTTON_HOME)) { printf("%s Home pressed\n", prefixString); } // if the accelerometer is turned on then print angles if(wm.isUsingACC()) { float pitch, roll, yaw; wm.Accelerometer.GetOrientation(pitch, roll, yaw); printf("%s wiimote roll = %f\n", prefixString, roll); printf("%s wiimote pitch = %f\n", prefixString, pitch); printf("%s wiimote yaw = %f\n", prefixString, yaw); } // if the Motion Plus is turned on then print angles if(wm.isUsingMotionPlus()) { float roll_rate, pitch_rate, yaw_rate; wm.ExpansionDevice.MotionPlus.Gyroscope.GetRates(roll_rate,pitch_rate,yaw_rate); printf("%s motion plus roll rate = %f\n", prefixString,roll_rate); printf("%s motion plus pitch rate = %f\n", prefixString,pitch_rate); printf("%s motion plus yaw rate = %f\n", prefixString,yaw_rate); } // if(IR tracking is on then print the coordinates if(wm.isUsingIR()) { std::vector<CIRDot>::iterator i; int x, y; int index; printf("%s Num IR Dots: %i\n", prefixString, wm.IR.GetNumDots()); printf("%s IR State: %u\n", prefixString, wm.IR.GetState()); std::vector<CIRDot>& dots = wm.IR.GetDots(); for(index = 0, i = dots.begin(); i != dots.end(); ++index, ++i) { if((*i).isVisible()) { (*i).GetCoordinate(x, y); printf("%s IR source %i: (%i, %i)\n", prefixString, index, x, y); wm.IR.GetCursorPosition(x, y); printf("%s IR cursor: (%i, %i)\n", prefixString, x, y); printf("%s IR z distance: %f\n", prefixString, wm.IR.GetDistance()); } } } int exType = wm.ExpansionDevice.GetType(); if(exType == wm.ExpansionDevice.TYPE_NUNCHUK) { float pitch, roll, yaw, a_pitch, a_roll; float angle, magnitude; CNunchuk &nc = wm.ExpansionDevice.Nunchuk; sprintf(prefixString, "Nunchuk [%i]: ", wm.GetID()); if(nc.Buttons.isPressed(CNunchukButtons::BUTTON_C)) { printf("%s C pressed\n", prefixString); } if(nc.Buttons.isPressed(CNunchukButtons::BUTTON_Z)) { printf("%s Z pressed\n", prefixString); } nc.Accelerometer.GetOrientation(pitch, roll, yaw); printf("%s roll = %f\n", prefixString, roll); printf("%s pitch = %f\n", prefixString, pitch); printf("%s yaw = %f\n", prefixString, yaw); nc.Joystick.GetPosition(angle, magnitude); printf("%s joystick angle = %f\n", prefixString, angle); printf("%s joystick magnitude = %f\n", prefixString, magnitude); } else if(exType == wm.ExpansionDevice.TYPE_CLASSIC) { float angle, magnitude; CClassic &cc = wm.ExpansionDevice.Classic; sprintf(prefixString, "Classic [%i]: ", wm.GetID()); if(cc.Buttons.isPressed(CClassicButtons::BUTTON_A)) { printf("%s A pressed\n", prefixString); } if(cc.Buttons.isPressed(CClassicButtons::BUTTON_B)) { printf("%s B pressed\n", prefixString); } if(cc.Buttons.isPressed(CClassicButtons::BUTTON_X)) { printf("%s X pressed\n", prefixString); } if(cc.Buttons.isPressed(CClassicButtons::BUTTON_Y)) { printf("%s Y pressed\n", prefixString); } if(cc.Buttons.isPressed(CClassicButtons::BUTTON_LEFT)) { printf("%s Left pressed\n", prefixString); } if(cc.Buttons.isPressed(CClassicButtons::BUTTON_UP)) { printf("%s Up pressed\n", prefixString); } if(cc.Buttons.isPressed(CClassicButtons::BUTTON_RIGHT)) { printf("%s Right pressed\n", prefixString); } if(cc.Buttons.isPressed(CClassicButtons::BUTTON_DOWN)) { printf("%s Down pressed\n", prefixString); } if(cc.Buttons.isPressed(CClassicButtons::BUTTON_PLUS)) { printf("%s Plus pressed\n", prefixString); } if(cc.Buttons.isPressed(CClassicButtons::BUTTON_MINUS)) { printf("%s Minus pressed\n", prefixString); } if(cc.Buttons.isPressed(CClassicButtons::BUTTON_HOME)) { printf("%s Home pressed\n", prefixString); } if(cc.Buttons.isPressed(CClassicButtons::BUTTON_ZL)) { printf("%s ZL pressed\n", prefixString); } if(cc.Buttons.isPressed(CClassicButtons::BUTTON_FULL_L)) { printf("%s ZR pressed\n", prefixString); } if(cc.Buttons.isPressed(CClassicButtons::BUTTON_FULL_R)) { printf("%s ZR pressed\n", prefixString); } printf("%s L button pressed = %f\n", prefixString, cc.GetLShoulderButton()); printf("%s R button pressed = %f\n", prefixString, cc.GetRShoulderButton()); cc.LeftJoystick.GetPosition(angle, magnitude); printf("%s left joystick angle = %f\n", prefixString, angle); printf("%s left joystick magnitude = %f\n", prefixString, magnitude); cc.RightJoystick.GetPosition(angle, magnitude); printf("%s right joystick angle = %f\n", prefixString, angle); printf("%s right joystick magnitude = %f\n", prefixString, magnitude); } else if(exType == wm.ExpansionDevice.TYPE_GUITAR_HERO_3) { float angle, magnitude; CGuitarHero3 &gh = wm.ExpansionDevice.GuitarHero3; sprintf(prefixString, "Guitar [%i]: ", wm.GetID()); if(gh.Buttons.isPressed(CGH3Buttons::BUTTON_STRUM_UP)) { printf("%s Strum Up pressed\n", prefixString); } if(gh.Buttons.isPressed(CGH3Buttons::BUTTON_STRUM_DOWN)) { printf("%s Strum Down pressed\n", prefixString); } if(gh.Buttons.isPressed(CGH3Buttons::BUTTON_YELLOW)) { printf("%s Yellow pressed\n", prefixString); } if(gh.Buttons.isPressed(CGH3Buttons::BUTTON_GREEN)) { printf("%s Green pressed\n", prefixString); } if(gh.Buttons.isPressed(CGH3Buttons::BUTTON_BLUE)) { printf("%s Blue pressed\n", prefixString); } if(gh.Buttons.isPressed(CGH3Buttons::BUTTON_RED)) { printf("%s Red pressed\n", prefixString); } if(gh.Buttons.isPressed(CGH3Buttons::BUTTON_ORANGE)) { printf("%s Orange pressed\n", prefixString); } if(gh.Buttons.isPressed(CGH3Buttons::BUTTON_PLUS)) { printf("%s Plus pressed\n", prefixString); } if(gh.Buttons.isPressed(CGH3Buttons::BUTTON_MINUS)) { printf("%s Minus pressed\n", prefixString); } printf("%s whammy bar = %f\n", prefixString, gh.GetWhammyBar()); gh.Joystick.GetPosition(angle, magnitude); printf("%s joystick angle = %f\n", prefixString, angle); printf("%s joystick magnitude = %f\n", prefixString, magnitude); } else if(exType == wm.ExpansionDevice.TYPE_BALANCE_BOARD) { CBalanceBoard &bb = wm.ExpansionDevice.BalanceBoard; float total, topLeft, topRight, bottomLeft, bottomRight; bb.WeightSensor.GetWeight(total, topLeft, topRight, bottomLeft, bottomRight); printf("balance board top left weight: %f\n", topLeft); printf("balance board top right weight: %f\n", topRight); printf("balance board bottom left weight: %f\n", bottomLeft); printf("balance board bottom right weight: %f\n", bottomRight); printf("balance board total weight: %f\n", total); } }
void HandleGH3Inserted(CWiimote &wm) { printf("GH3 inserted on controller %i.\n", wm.GetID()); }
void HandleClassicInserted(CWiimote &wm) { printf("Classic controler inserted on controller %i.\n", wm.GetID()); }
void HandleNunchukInserted(CWiimote &wm) { printf("Nunchuk inserted on controller %i.\n", wm.GetID()); }
void HandleReadData(CWiimote &wm) { printf("\n"); printf("--- DATA READ [wiimote id %i] ---\n", wm.GetID()); printf("\n"); }
void HandleDisconnect(CWiimote &wm) { printf("\n"); printf("--- DISCONNECTED [wiimote id %i] ---\n", wm.GetID()); printf("\n"); }
void HandleEvent(CWiimote &wm) { char prefixString[64]; if(wm.Buttons.isJustPressed(CButtons::BUTTON_MINUS)) wm.SetMotionSensingMode(CWiimote::OFF) ; if(wm.Buttons.isJustPressed(CButtons::BUTTON_PLUS)) wm.SetMotionSensingMode(CWiimote::ON) ; if(wm.Buttons.isJustPressed(CButtons::BUTTON_DOWN)) wm.IR.SetMode(CIR::OFF); if(wm.Buttons.isJustPressed(CButtons::BUTTON_UP)) wm.IR.SetMode(CIR::ON); if(wm.Buttons.isJustPressed(CButtons::BUTTON_B)) wm.ToggleRumble(); sprintf(prefixString, "Controller [%i]: ", wm.GetID()); if(wm.Buttons.isJustChanged()) printf("%s State of Wiimote buttons changed\n", prefixString); if(wm.Buttons.isPressed(CButtons::BUTTON_A)) printf("%s A pressed\n", prefixString); if(wm.Buttons.isPressed(CButtons::BUTTON_B)) printf("%s B pressed\n", prefixString); if(wm.Buttons.isPressed(CButtons::BUTTON_UP))printf("%s Up pressed\n", prefixString); if(wm.Buttons.isPressed(CButtons::BUTTON_DOWN))printf("%s Down pressed\n", prefixString); if(wm.Buttons.isPressed(CButtons::BUTTON_LEFT))printf("%s Left pressed\n", prefixString); if(wm.Buttons.isPressed(CButtons::BUTTON_RIGHT))printf("%s Right pressed\n", prefixString); if(wm.Buttons.isPressed(CButtons::BUTTON_MINUS))printf("%s Minus pressed\n", prefixString); if(wm.Buttons.isPressed(CButtons::BUTTON_PLUS))printf("%s Plus pressed\n", prefixString); if(wm.Buttons.isPressed(CButtons::BUTTON_ONE))printf("%s One pressed\n", prefixString); if(wm.Buttons.isPressed(CButtons::BUTTON_TWO))printf("%s Two pressed\n", prefixString); if(wm.Buttons.isPressed(CButtons::BUTTON_HOME))printf("%s Home pressed\n", prefixString); // if(the accelerometer is turned on then print angles if(wm.isUsingACC()) { float pitch, roll, yaw, a_pitch, a_roll; wm.Accelerometer.GetOrientation(pitch, roll, yaw); wm.Accelerometer.GetRawOrientation(a_pitch, a_roll); //printf("%s wiimote roll = %f [%f]\n", prefixString, roll, a_roll); //printf("%s wiimote pitch = %f [%f]\n", prefixString, pitch, a_pitch); //printf("%s wiimote yaw = %f\n", prefixString, yaw); double x, y, z, a ; wm.Accelerometer.GetGForceInG( x, y, z ) ; printf("\n%s wiimote gForce = %2.5f, %2.5f:%2.5f:%2.5f\n", prefixString, wm.Accelerometer.GetGForceInG(), x, y, z ); wm.Accelerometer.GetJerkInMS3( x, y, z ) ; printf("%s wiimote jerk = %2.5f, %2.5f:%2.5f:%2.5f\n", prefixString, wm.Accelerometer.GetJerkInMS3(), x, y, z ); wm.Accelerometer.GetVelocity( x, y, z ) ; printf("%s wiimote velocity = %2.5f, %2.5f:%2.5f:%2.5f\n", prefixString, wm.Accelerometer.GetVelocity(), x, y, z ); wm.Accelerometer.GetDistance( x, y, z ) ; a = wm.Accelerometer.GetDistance() ; traveledDistance += a ; printf("%s wiimote distance:%2.5f, %2.5f:%2.5f:%2.5f ;\n", prefixString, a, x, y, z ); printf("%s wiimote traveled distance :%2.5f ;\n", prefixString, traveledDistance ); } // if(IR tracking is on then print the coordinates if(wm.isUsingIR()) { int x, y; printf("%s Num IR Dots: %i\n", prefixString, wm.IR.GetNumDots()); printf("%s IR State: %u\n", prefixString, wm.IR.GetState()); std::vector<CIRDot*>& dots = wm.IR.GetDots(); int index=0 ; for( std::vector<CIRDot*>::iterator i=dots.begin() ; i != dots.end() ; ++i ) { if( (*i)->isVisible() ) { (*i)->GetCoordinate(x, y); printf("%s IR source %i: (%i, %i)\n", prefixString, ++index, x, y); wm.IR.GetCursorPosition(x, y); printf("%s IR cursor: (%i, %i)\n", prefixString, x, y); printf("%s IR z distance: %f\n", prefixString, wm.IR.GetDistance()); } } } int exType = wm.ExpansionDevice.GetType() ; if(exType == wm.ExpansionDevice.TYPE_NUNCHUK) { float pitch, roll, yaw, a_pitch, a_roll; float angle, magnitude; CNunchuk &nc = wm.ExpansionDevice.Nunchuk; sprintf(prefixString, "Nunchuk [%i]: ", wm.GetID()); if(nc.Buttons.isJustChanged()) printf("%s State of Wiimote buttons changed\n", prefixString); if(nc.Buttons.isPressed(CNunchukButtons::BUTTON_C))printf("%s C pressed\n", prefixString); if(nc.Buttons.isPressed(CNunchukButtons::BUTTON_Z))printf("%s Z pressed\n", prefixString); nc.Accelerometer.GetOrientation(pitch, roll, yaw); nc.Accelerometer.GetRawOrientation(a_pitch, a_roll); printf("%s roll = %f [%f]\n", prefixString, roll, a_roll); printf("%s pitch = %f [%f]\n", prefixString, pitch, a_pitch); printf("%s yaw = %f\n", prefixString, yaw); nc.Joystick.GetPosition(angle, magnitude); printf("%s joystick angle = %f\n", prefixString, angle); printf("%s joystick magnitude = %f\n", prefixString, magnitude); } else if(exType == wm.ExpansionDevice.TYPE_CLASSIC) { float angle, magnitude; CClassic &cc = wm.ExpansionDevice.Classic; sprintf(prefixString, "Classic [%i]: ", wm.GetID()); if(cc.Buttons.isPressed(CClassicButtons::BUTTON_A)) printf("%s A pressed\n", prefixString); if(cc.Buttons.isPressed(CClassicButtons::BUTTON_B)) printf("%s B pressed\n", prefixString); if(cc.Buttons.isPressed(CClassicButtons::BUTTON_X)) printf("%s X pressed\n", prefixString); if(cc.Buttons.isPressed(CClassicButtons::BUTTON_Y)) printf("%s Y pressed\n", prefixString); if(cc.Buttons.isPressed(CClassicButtons::BUTTON_LEFT))printf("%s Left pressed\n", prefixString); if(cc.Buttons.isPressed(CClassicButtons::BUTTON_UP))printf("%s Up pressed\n", prefixString); if(cc.Buttons.isPressed(CClassicButtons::BUTTON_RIGHT))printf("%s Right pressed\n", prefixString); if(cc.Buttons.isPressed(CClassicButtons::BUTTON_DOWN))printf("%s Down pressed\n", prefixString); if(cc.Buttons.isPressed(CClassicButtons::BUTTON_PLUS))printf("%s Plus pressed\n", prefixString); if(cc.Buttons.isPressed(CClassicButtons::BUTTON_MINUS))printf("%s Minus pressed\n", prefixString); if(cc.Buttons.isPressed(CClassicButtons::BUTTON_HOME))printf("%s Home pressed\n", prefixString); if(cc.Buttons.isPressed(CClassicButtons::BUTTON_ZL))printf("%s ZL pressed\n", prefixString); if(cc.Buttons.isPressed(CClassicButtons::BUTTON_FULL_L))printf("%s ZR pressed\n", prefixString); if(cc.Buttons.isPressed(CClassicButtons::BUTTON_FULL_R))printf("%s ZR pressed\n", prefixString); printf("%s L button pressed = %f\n", prefixString, cc.GetLShoulderButton()); printf("%s R button pressed = %f\n", prefixString, cc.GetRShoulderButton()); cc.LeftJoystick.GetPosition(angle, magnitude); printf("%s left joystick angle = %f\n", prefixString, angle); printf("%s left joystick magnitude = %f\n", prefixString, magnitude); cc.RightJoystick.GetPosition(angle, magnitude); printf("%s right joystick angle = %f\n", prefixString, angle); printf("%s right joystick magnitude = %f\n", prefixString, magnitude); } else if(exType == wm.ExpansionDevice.TYPE_GUITAR_HERO_3) { float angle, magnitude; CGuitarHero3 &gh = wm.ExpansionDevice.GuitarHero3; sprintf(prefixString, "Guitar [%i]: ", wm.GetID()); if(gh.Buttons.isPressed(CGH3Buttons::BUTTON_STRUM_UP)) printf("%s Strum Up pressed\n", prefixString); if(gh.Buttons.isPressed(CGH3Buttons::BUTTON_STRUM_DOWN))printf("%s Strum Down pressed\n", prefixString); if(gh.Buttons.isPressed(CGH3Buttons::BUTTON_YELLOW)) printf("%s Yellow pressed\n", prefixString); if(gh.Buttons.isPressed(CGH3Buttons::BUTTON_GREEN)) printf("%s Green pressed\n", prefixString); if(gh.Buttons.isPressed(CGH3Buttons::BUTTON_BLUE)) printf("%s Blue pressed\n", prefixString); if(gh.Buttons.isPressed(CGH3Buttons::BUTTON_RED)) printf("%s Red pressed\n", prefixString); if(gh.Buttons.isPressed(CGH3Buttons::BUTTON_ORANGE)) printf("%s Orange pressed\n", prefixString); if(gh.Buttons.isPressed(CGH3Buttons::BUTTON_PLUS)) printf("%s Plus pressed\n", prefixString); if(gh.Buttons.isPressed(CGH3Buttons::BUTTON_MINUS)) printf("%s Minus pressed\n", prefixString); printf("%s whammy bar = %f\n", prefixString, gh.GetWhammyBar()); gh.Joystick.GetPosition(angle, magnitude); printf("%s joystick angle = %f\n", prefixString, angle); printf("%s joystick magnitude = %f\n", prefixString, magnitude); } }
int main(int argc, char** argv) { //std::vector<CIRDot*> a ; //a.clear() ; CWii wii; // Defaults to 4 remotes int reloadWiimotes=0 ; int numFound=0 ; traveledDistance = 0 ; //Find the wiimote & search for up to five seconds. printf("Searching for wiimotes... Turn them on!\n"); numFound = wii.Find(5); printf("Found %d wiimotes\n", numFound); // Under Windows, even if a Wiimote is not connected but it can appear // in "Devices and printers", so it connects it ... if( numFound <= 0 ) return 0 ; // Connect to the wiimote printf( "Connecting to wiimotes...\n" ) ; std::vector<CWiimote*>& wiimotes = wii.Connect() ; printf( "Connected to %d wiimotes\n", (int)wiimotes.size() ) ; if( (int) wiimotes.size() <= 0 ) return 0 ; // Setup the wiimotes int index=0 ; for( size_t i = 0; i < wiimotes.size() ; ++i ) { // Use a reference to make working with the iterator handy. CWiimote *wiimote = wiimotes[i]; //Set Leds wiimote->SetLEDs(LED_MAP[++index]); // Continuous information. //wiimote->SetFlags( CWiimote::FLAG_CONTINUOUS, 0x0 ) ; //Rumble for 0.2 seconds as a connection ack wiimote->SetRumbleMode(CWiimote::ON); #ifndef WIN32 usleep(200000); #else Sleep(200); #endif wiimote->SetRumbleMode(CWiimote::OFF); } while( wiimotes.size() ) // Go so long as there are wiimotes left to poll { if( reloadWiimotes ) { // Regenerate the list of wiimotes wiimotes = wii.GetWiimotes(); reloadWiimotes = 0; } // (My) Bad idea. // The data are received in continuous when the accelerometer values are on. // So the received data is late ... Worse, latency appears. #ifndef WIN32 //usleep(200000); #else //Sleep(20); #endif //Poll the wiimotes to get the status like pitch or roll if( wii.Poll() ) { for( size_t i = 0; i < wiimotes.size() ; ++i ) { // Use a reference to make working with the iterator handy. CWiimote *wiimote = wiimotes[i]; CWiimoteData *wm=wiimote->copyData() ; switch(wiimote->GetEvent()) { case CWiimote::EVENT_EVENT: HandleEvent(*wiimote); break; case CWiimote::EVENT_STATUS: HandleStatus(*wiimote); break; case CWiimote::EVENT_DISCONNECT: case CWiimote::EVENT_UNEXPECTED_DISCONNECT: HandleDisconnect(*wiimote); reloadWiimotes = 1; break; case CWiimote::EVENT_READ_DATA: HandleReadData(*wiimote); break; case CWiimote::EVENT_NUNCHUK_INSERTED: HandleNunchukInserted(*wiimote); reloadWiimotes = 1; break; case CWiimote::EVENT_CLASSIC_CTRL_INSERTED: HandleClassicInserted(*wiimote); reloadWiimotes = 1; break; case CWiimote::EVENT_GUITAR_HERO_3_CTRL_INSERTED: HandleGH3Inserted(*wiimote); reloadWiimotes = 1; break; case CWiimote::EVENT_NUNCHUK_REMOVED: case CWiimote::EVENT_CLASSIC_CTRL_REMOVED: case CWiimote::EVENT_GUITAR_HERO_3_CTRL_REMOVED: printf("An expansion was removed.\n"); HandleStatus(*wiimote); reloadWiimotes = 1; break; default: break; } } } } return 0; }