00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include "WiimoteManager.hpp"
00028 #include "ControllerException.hpp"
00029
00030 #include <boost/bind.hpp>
00031
00032
00033 namespace kn {
00034
00035 WiimoteManager::WiimoteManager(void){
00036 wiimotes=0;
00037 wiimoteobservers=0;
00038 treatevents = false;
00039 maxnumberofwiimotes = 0;
00040 numberofwiimotes = 0;
00041 thrd = 0;
00042 }
00043
00044 WiimoteManager::~WiimoteManager(void){
00045 stop();
00046 }
00047
00048 void WiimoteManager::launch(const unsigned int& maxtime,
00049 const unsigned int& maxwiimotes){
00050
00051 maxnumberofwiimotes = maxwiimotes;
00052
00053
00054 wiimotes = wiiuse_init(maxnumberofwiimotes);
00055
00056
00057 numberofwiimotes = wiiuse_find(wiimotes, maxnumberofwiimotes, maxtime);
00058 if (!numberofwiimotes) throw ControllerException("No wiimote found","WiimoteManager");
00059
00060
00061 int connected = wiiuse_connect(wiimotes, numberofwiimotes);
00062 if (!connected) throw ControllerException("Unable to connect wiimotes","WiimoteManager");
00063
00064 wiimoteobservers = new Wiimote[numberofwiimotes];
00065
00066 for(unsigned int i = 0; i < numberofwiimotes; ++i)
00067 wiimoteobservers[i] = Wiimote(wiimotes[i]);
00068
00069
00070 treatevents=true;
00071 thrd = new boost::thread(boost::bind(&WiimoteManager::handleEvents, this));
00072 }
00073
00074 void WiimoteManager::stop(void){
00075 if(thrd){
00076 treatevents = false;
00077 thrd->join();
00078 delete thrd;
00079 thrd = 0;
00080 }
00081
00082 if(wiimoteobservers) delete[] wiimoteobservers;
00083 wiimoteobservers = 0;
00084
00085 if(wiimotes) wiiuse_cleanup(wiimotes, maxnumberofwiimotes);
00086 wiimotes = 0;
00087
00088 maxnumberofwiimotes = numberofwiimotes = 0;
00089
00090 }
00091
00092
00093 Wiimote& WiimoteManager::getWiimote(const unsigned int& i){
00094 if(i >= numberofwiimotes) throw ControllerException("No such wiimote","getWiimote");
00095 return wiimoteobservers[i];
00096 }
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110 void WiimoteManager::handle_event(struct wiimote_t* wm, const unsigned int& id){
00111
00112
00113 if (IS_PRESSED(wm, WIIMOTE_BUTTON_A)) wiimoteobservers[id].addEvent(EVENT_BUTTON_A_DOWN);
00114 if (IS_RELEASED(wm, WIIMOTE_BUTTON_A)) wiimoteobservers[id].addEvent(EVENT_BUTTON_A_UP);
00115
00116 if (IS_PRESSED(wm, WIIMOTE_BUTTON_B)) wiimoteobservers[id].addEvent(EVENT_BUTTON_B_DOWN);
00117 if (IS_RELEASED(wm, WIIMOTE_BUTTON_B)) wiimoteobservers[id].addEvent(EVENT_BUTTON_B_UP);
00118
00119 if (IS_PRESSED(wm, WIIMOTE_BUTTON_UP)) wiimoteobservers[id].addEvent(EVENT_BUTTON_UP_DOWN);
00120 if (IS_RELEASED(wm, WIIMOTE_BUTTON_UP)) wiimoteobservers[id].addEvent(EVENT_BUTTON_UP_UP);
00121
00122 if (IS_PRESSED(wm, WIIMOTE_BUTTON_DOWN)) wiimoteobservers[id].addEvent(EVENT_BUTTON_DOWN_DOWN);
00123 if (IS_RELEASED(wm, WIIMOTE_BUTTON_DOWN)) wiimoteobservers[id].addEvent(EVENT_BUTTON_DOWN_UP);
00124
00125 if (IS_PRESSED(wm, WIIMOTE_BUTTON_LEFT)) wiimoteobservers[id].addEvent(EVENT_BUTTON_LEFT_DOWN);
00126 if (IS_RELEASED(wm, WIIMOTE_BUTTON_LEFT)) wiimoteobservers[id].addEvent(EVENT_BUTTON_LEFT_UP);
00127
00128 if (IS_PRESSED(wm, WIIMOTE_BUTTON_RIGHT)) wiimoteobservers[id].addEvent(EVENT_BUTTON_RIGHT_DOWN);
00129 if (IS_RELEASED(wm, WIIMOTE_BUTTON_RIGHT)) wiimoteobservers[id].addEvent(EVENT_BUTTON_RIGHT_UP);
00130
00131 if (IS_PRESSED(wm, WIIMOTE_BUTTON_MINUS)) wiimoteobservers[id].addEvent(EVENT_BUTTON_MINUS_DOWN);
00132 if (IS_RELEASED(wm, WIIMOTE_BUTTON_MINUS)) wiimoteobservers[id].addEvent(EVENT_BUTTON_MINUS_UP);
00133
00134 if (IS_PRESSED(wm, WIIMOTE_BUTTON_PLUS)) wiimoteobservers[id].addEvent(EVENT_BUTTON_PLUS_DOWN);
00135 if (IS_RELEASED(wm, WIIMOTE_BUTTON_PLUS)) wiimoteobservers[id].addEvent(EVENT_BUTTON_PLUS_UP);
00136
00137 if (IS_PRESSED(wm, WIIMOTE_BUTTON_ONE)) wiimoteobservers[id].addEvent(EVENT_BUTTON_ONE_DOWN);
00138 if (IS_RELEASED(wm, WIIMOTE_BUTTON_ONE)) wiimoteobservers[id].addEvent(EVENT_BUTTON_ONE_UP);
00139
00140 if (IS_PRESSED(wm, WIIMOTE_BUTTON_TWO)) wiimoteobservers[id].addEvent(EVENT_BUTTON_TWO_DOWN);
00141 if (IS_RELEASED(wm, WIIMOTE_BUTTON_TWO)) wiimoteobservers[id].addEvent(EVENT_BUTTON_TWO_UP);
00142
00143 if (IS_PRESSED(wm, WIIMOTE_BUTTON_HOME)) wiimoteobservers[id].addEvent(EVENT_BUTTON_HOME_DOWN);
00144 if (IS_RELEASED(wm, WIIMOTE_BUTTON_HOME)) wiimoteobservers[id].addEvent(EVENT_BUTTON_HOME_UP);
00145
00146
00147
00148 if(wiimoteobservers[id].getNunchuk().isConnected()){
00149 struct nunchuk_t* nc = (nunchuk_t*)&wm->exp.nunchuk;
00150
00151 if (IS_PRESSED(nc, NUNCHUK_BUTTON_C)) wiimoteobservers[id].getNunchuk().addEvent(EVENT_BUTTON_C_DOWN);
00152 if (IS_RELEASED(nc, NUNCHUK_BUTTON_C)) wiimoteobservers[id].getNunchuk().addEvent(EVENT_BUTTON_C_UP);
00153
00154 if (IS_PRESSED(nc, NUNCHUK_BUTTON_Z)) wiimoteobservers[id].getNunchuk().addEvent(EVENT_BUTTON_Z_DOWN);
00155 if (IS_RELEASED(nc, NUNCHUK_BUTTON_Z)) wiimoteobservers[id].getNunchuk().addEvent(EVENT_BUTTON_Z_UP);
00156 }
00157 }
00158
00159
00160 void WiimoteManager::handleEvents(void){
00161 while(treatevents){
00162
00163 if (wiiuse_poll(wiimotes, numberofwiimotes))
00164 for(unsigned int i = 0; i < numberofwiimotes; ++i){
00165 switch (wiimotes[i]->event) {
00166 case WIIUSE_EVENT:
00167
00168 handle_event(wiimotes[i],i);
00169 break;
00170
00171 case WIIUSE_STATUS:
00172
00173
00174 break;
00175
00176 case WIIUSE_DISCONNECT:
00177 case WIIUSE_UNEXPECTED_DISCONNECT:
00178
00179 break;
00180
00181 case WIIUSE_NUNCHUK_INSERTED:
00182 case WIIUSE_NUNCHUK_REMOVED:
00183
00184 break;
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196 default:
00197 break;
00198
00199
00200 }
00201 }
00202 }
00203 }
00204
00205
00206 }