00001 #include "WiiuseMotionReceiver.h"
00002 #include "framedata/SignalFrameData.h"
00003
00004 WiiuseMotionReceiver::~WiiuseMotionReceiver()
00005 {
00006 if (isRunning())
00007 stop();
00008 }
00009
00010 void WiiuseMotionReceiver::start() throw(Error)
00011 {
00012
00013 int found = 0, connected = 0;
00014
00015 m_wiimotes = wiiuse_init(1);
00016 found = wiiuse_find(m_wiimotes, 1, 5);
00017 if (found == 0) {
00018 throw Error("in WiiuseMotionReceiver::start(): Found no Wiimote");
00019 }
00020 connected = wiiuse_connect(m_wiimotes, 1);
00021 if (connected == 0) {
00022 throw Error("in WiiuseMotionReceiver::start(): Found a Wiimote but could not connect");
00023 }
00024
00025 wiiuse_motion_sensing(m_wiimotes[0], 1);
00026
00027 wiiuse_set_leds(m_wiimotes[0], WIIMOTE_LED_1);
00028
00029 wiiuse_set_flags(m_wiimotes[0], WIIUSE_CONTINUOUS | WIIUSE_SMOOTHING, 0);
00030 }
00031
00032 void WiiuseMotionReceiver::stop()
00033 {
00034
00035 setRunning(false);
00036 wiiuse_cleanup(m_wiimotes, 1);
00037 m_wiimotes = NULL;
00038 }
00039
00040 void WiiuseMotionReceiver::run()
00041 {
00042 setRunning(true);
00043 while (m_wiimotes != NULL && isRunning()) {
00044 if (wiiuse_poll(m_wiimotes, 1)) {
00045 switch (m_wiimotes[0]->event) {
00046 case WIIUSE_EVENT:
00047 if (IS_PRESSED(m_wiimotes[0], WIIMOTE_BUTTON_A)) {
00048 setRunning(false);
00049 } else if (WIIUSE_USING_ACC(m_wiimotes[0])) {
00050 Vector3d v;
00051 v.set(0, m_wiimotes[0]->gforce.x);
00052 v.set(1, m_wiimotes[0]->gforce.y);
00053 v.set(2, m_wiimotes[0]->gforce.z);
00054 notify(v);
00055 }
00056 break;
00057 case WIIUSE_DISCONNECT:
00058 setRunning(false);
00059 break;
00060 default:
00061 break;
00062 }
00063 }
00064 }
00065 }