All Classes Namespaces Files Functions Variables Enumerations Enumerator
libs/controller/WiiJoystick_win.cpp
Go to the documentation of this file.
00001 
00017 #include <string>
00018 #include "WiiYourself_1_0/wiimote.h"
00019 #include "WiiJoystick.h"
00020 #include "MovingAverage.h"
00021 
00022 using namespace qrk;
00023 using namespace std;
00024 
00025 
00026 struct WiiJoystick::pImpl
00027 {
00028   string error_message_;
00029   wiimote* wii_;
00030 
00031   MovingAverage<double>* x_average_;
00032   MovingAverage<double>* y_average_;
00033   MovingAverage<double>* z_average_;
00034 
00035 
00036   pImpl(void)
00037     : error_message_("no error."), wii_(new wiimote),
00038       x_average_(new MovingAverage<double>(1)),
00039       y_average_(new MovingAverage<double>(1)),
00040       z_average_(new MovingAverage<double>(1)) {
00041   }
00042 
00043 
00044   ~pImpl(void)
00045   {
00046     // 切断処理
00047     delete wii_;
00048     wii_ = NULL;
00049   }
00050 
00051 
00052   bool connect(void)
00053   {
00054     disconnect();
00055 
00056     if (! wii_->Connect(wiimote::FIRST_AVAILABLE)) {
00057       error_message_ = "unable to connect.";
00058       return false;
00059     }
00060     wii_->SetReportType(wiimote::IN_BUTTONS_ACCEL_IR);
00061 
00062     return true;
00063   }
00064 
00065 
00066   void disconnect(void)
00067   {
00068     wii_->Disconnect();
00069   }
00070 };
00071 
00072 
00073 WiiJoystick::WiiJoystick(void) : pimpl(new pImpl)
00074 {
00075 }
00076 
00077 
00078 WiiJoystick::~WiiJoystick(void)
00079 {
00080 }
00081 
00082 
00083 bool WiiJoystick::findController(int timeout)
00084 {
00085   // !!! 実装の必要はないのか?
00086 
00087   return false;
00088 }
00089 
00090 
00091 const char* WiiJoystick::what(void) const
00092 {
00093   return pimpl->error_message_.c_str();
00094 }
00095 
00096 
00097 bool WiiJoystick::connect(int id)
00098 {
00099   (void)id;
00100 
00101   return pimpl->connect();
00102 }
00103 
00104 
00105 void WiiJoystick::disconnect(void)
00106 {
00107   pimpl->disconnect();
00108 }
00109 
00110 
00111 bool WiiJoystick::isConnected(void) const
00112 {
00113   return pimpl->wii_->IsConnected();
00114 }
00115 
00116 
00117 size_t WiiJoystick::axisNum(void) const
00118 {
00119   return 2;
00120 }
00121 
00122 
00123 int WiiJoystick::axisValue(size_t index)
00124 {
00125   // !!! 排他制御?
00126 
00127   pimpl->wii_->RefreshState();
00128 
00129   int value = 0;
00130   if (index == AxisX) {
00131     value =
00132       (pimpl->wii_->Button.Left() ? -32767 : 0) +
00133       (pimpl->wii_->Button.Right() ? +32767 : 0);
00134 
00135   } else if (index == AxisY) {
00136     value =
00137       (pimpl->wii_->Button.Up() ? +32767 : 0) +
00138       (pimpl->wii_->Button.Down() ? -32767 : 0);
00139   }
00140 
00141   return value;
00142 }
00143 
00144 
00145 size_t WiiJoystick::buttonsNum(void) const
00146 {
00147   return 7;
00148 }
00149 
00150 
00151 bool WiiJoystick::isButtonPressed(size_t index)
00152 {
00153   // !!! 排他制御
00154 
00155   pimpl->wii_->RefreshState();
00156 
00157   bool pressed = false;
00158   switch (index) {
00159   case BUTTON_A:
00160     pressed = pimpl->wii_->Button.A();
00161     break;
00162 
00163   case BUTTON_B:
00164     pressed = pimpl->wii_->Button.B();
00165     break;
00166 
00167   case BUTTON_MINUS:
00168     pressed = pimpl->wii_->Button.Minus();
00169     break;
00170 
00171   case BUTTON_PLUS:
00172     pressed = pimpl->wii_->Button.Plus();
00173     break;
00174 
00175   case BUTTON_HOME:
00176     pressed = pimpl->wii_->Button.Home();
00177     break;
00178 
00179   case BUTTON_1:
00180     pressed = pimpl->wii_->Button.One();
00181     break;
00182 
00183   case BUTTON_2:
00184     pressed = pimpl->wii_->Button.Two();
00185     break;
00186   }
00187 
00188   return pressed;
00189 }
00190 
00191 
00192 int WiiJoystick::buttonPressedTimes(size_t index)
00193 {
00194   // !!! 実装すること
00195 
00196   static_cast<void>(index);
00197 
00198   // !!!
00199   return 0;
00200 }
00201 
00202 
00203 void WiiJoystick::acceleration(Point3d<double>& acceleration,
00204                                   size_t* timestamp)
00205 {
00206   pimpl->wii_->RefreshState();
00207 
00208   acceleration.x = pimpl->wii_->Acceleration.X;
00209   acceleration.y = pimpl->wii_->Acceleration.Y;
00210   acceleration.z = pimpl->wii_->Acceleration.Z;
00211 
00212   size_t timestamp_value = 0;
00213   if (timestamp) {
00214     // !!! ハンドラ内で更新したタイムスタンプを返すようにする
00215     *timestamp = timestamp_value;
00216   }
00217 }
00218 
00219 
00220 void WiiJoystick::rotation(Angle& x_axis, Angle& y_axis, Angle& z_axis,
00221                            size_t* timestamp)
00222 {
00223   (void)x_axis;
00224   (void)y_axis;
00225   (void)z_axis;
00226   (void)timestamp;
00227 
00228   // !!!
00229 }
00230 
00231 
00232 size_t WiiJoystick::batteryPercent(void)
00233 {
00234   // !!! 排他制御
00235 
00236   pimpl->wii_->RefreshState();
00237 
00238   return pimpl->wii_->BatteryPercent;
00239 }
00240 
00241 
00242 bool WiiJoystick::irPosition(vector<ir_position>& positions)
00243 {
00244   // !!! 排他制御
00245 
00246   pimpl->wii_->RefreshState();
00247 
00248   // !!! 未実装
00249 
00250   //positions = pimpl->callback_->wii_hash_[pimpl->wii_]->ir_positions_;
00251   //return true;
00252 
00253   return false;
00254 }
00255 
00256 
00257 bool WiiJoystick::setLed(unsigned char led_value)
00258 {
00259   // ビット並びが逆だったので、反転
00260   unsigned char swapped_value =
00261     ((led_value & 0x08) ? 0x01 : 0x00) |
00262     ((led_value & 0x04) ? 0x02 : 0x00) |
00263     ((led_value & 0x02) ? 0x04 : 0x00) |
00264     ((led_value & 0x01) ? 0x08 : 0x00);
00265 
00266   pimpl->wii_->SetLEDs(swapped_value);
00267 
00268   return true;
00269 }
00270 
00271 
00272 bool WiiJoystick::setRumble(bool rumble)
00273 {
00274   pimpl->wii_->SetRumble(rumble);
00275   // !!!
00276   return true;
00277 }
00278 
00279 
00280 // 加速度の移動平均数
00281 void WiiJoystick::setAccelerationAverageSize(size_t num)
00282 {
00283   pimpl->wii_->SetAccelerationAverageSize(num);
00284 }