Esempio n. 1
0
/* Connect is designed to be run in a different thread as it only 
   exits if wiiremote is either disabled or a connection is made*/
bool CWiiRemote::Connect()
{
#ifndef _DEBUG
  cwiid_set_err(ErrorCallback);
#endif
  while (!m_connected)
  {
    g_Ping->Send(m_Socket, m_MyAddr);
    int flags = 0;
    ToggleBit(flags, CWIID_FLAG_MESG_IFC);
    ToggleBit(flags, CWIID_FLAG_REPEAT_BTN);

    m_wiiremoteHandle = cwiid_connect(&m_btaddr, flags);
    if (m_wiiremoteHandle != NULL)
    {
      SetupWiiRemote();
      // get battery state etc.
      cwiid_state wiiremote_state;
      int err = cwiid_get_state(m_wiiremoteHandle, &wiiremote_state);
      if (!err)
      {
        char Mesg[1024];
        sprintf(Mesg, "%i%% battery remaining", static_cast<int>(((float)(wiiremote_state.battery)/CWIID_BATTERY_MAX)*100.0));
        CPacketNOTIFICATION notification("Wii Remote connected", Mesg, ICON_PNG, g_BluetoothIconPath.c_str());
        notification.Send(m_Socket, m_MyAddr);
      }
      else
      {
        printf("Problem probing for status of WiiRemote; cwiid_get_state returned non-zero\n");
        CPacketLOG log(LOGNOTICE, "Problem probing for status of WiiRemote; cwiid_get_state returned non-zero");
        log.Send(m_Socket, m_MyAddr);
        CPacketNOTIFICATION notification("Wii Remote connected", "", ICON_PNG, g_BluetoothIconPath.c_str());
        notification.Send(m_Socket, m_MyAddr);
      }
#ifdef CWIID_OLD
      /* CheckIn to say that this is the last msg, If this isn't called it could give issues if we Connects -> Disconnect and then try to connect again 
         the CWIID_OLD hack would automaticly disconnect the wiiremote as the lastmsg is too old. */
      CheckIn();
#endif      
      m_connected = true;

      CPacketLOG log(LOGNOTICE, "Sucessfully connected a WiiRemote");
      log.Send(m_Socket, m_MyAddr);
      return true;
    }
    //Here's a good place to have a quit flag check...

  }
  return false;
}
 void WiiTrackExtension::cwiidConnect()
 {
   bdaddr_t bdaddr;        /* bluetooth device address */
   bdaddr = *BDADDR_ANY;
   
   // connect
   if (!(m_wiimote = cwiid_connect(&bdaddr, 0))) {
     qDebug() << "Unable to connect to wiimote";
     return;
   } else {
     qDebug() << "Connected to wiimote";
     m_actions.at(StartIndex)->setEnabled(false);
     m_actions.at(StopIndex)->setEnabled(true);
   }
   
   // Set the callback function
   if (cwiid_set_mesg_callback(m_wiimote, cwiid_callback)) {
     qDebug() << "Unable to set message callback";
     cwiidDisconnect(); 
   }
   
   // set the report mode
   unsigned char rpt_mode = 0;
   toggle_bit(rpt_mode, CWIID_RPT_IR);
   cwiidSetReportMode(m_wiimote, rpt_mode);
   
   // enable messaging
   if (cwiid_enable(m_wiimote, CWIID_FLAG_MESG_IFC)) {
     qDebug() << "Error enabling messages";
   }
     
   m_lastDistance = 0.0;
   m_lastDot1x = 0.0;
   m_lastDot1y = 0.0;
   m_lastDot2x = 0.0;
   m_lastDot2y = 0.0;
 }