/*!
  \example controller/samples/wiiJoystickSample.cpp

  Wii コントローラの情報を出力するサンプル

  \verbatim
!!! 実行結果の例 \endverbatim \n

  \author Satofumi KAMIMURA

  $Id: wiiJoystickSample.cpp 1392 2009-10-10 10:59:39Z satofumi $
*/

#include <SDL.h>
#include "WiiJoystick.h"
#include "delay.h"

using namespace qrk;
using namespace std;


int main(int argc, char *argv[]) {

  WiiJoystick joystick;
  if (! joystick.connect()) {
    printf("WiiJoystick::connect: %s\n", joystick.what());
    exit(1);
  }
  printf("O.K.\n");

  size_t buttons_num = joystick.buttonsNum();
  while (1) {

    // バッテリー情報
    printf("%d[%%], ", joystick.batteryPercent());

    // 軸情報
    int x_axis = joystick.axisValue(WiiJoystick::AxisX);
    int y_axis = joystick.axisValue(WiiJoystick::AxisY);
    printf("%6d, %6d: ", x_axis, y_axis);

    // ボタン情報
    for (size_t i = 0; i < buttons_num; ++i) {
      bool is_pressed = joystick.isButtonPressed(i);
      printf("%d, ", is_pressed ? 1 : 0);
    }

    // 加速度情報
    Point3d<double> acceleration;
    joystick.acceleration(acceleration);
    printf("%.2f, %.2f, %.2f, ",
           acceleration.x, acceleration.y, acceleration.z);

    // IR 情報
    vector<WiiJoystick::ir_position> ir_positions;
    joystick.irPosition(ir_positions);
    if (! ir_positions.empty()) {
      printf("\n");
    }
    for (vector<WiiJoystick::ir_position>::iterator it =
           ir_positions.begin(); it != ir_positions.end(); ++it) {
      printf("(%.2f, %.2f, %d), ", it->position.x, it->position.y, it->size);
    }

    printf("\n");

    delay(100);
  }

  return 0;
}
