-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwiimote.cpp
66 lines (58 loc) · 1.46 KB
/
wiimote.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include "wiimote.h"
WiiMote::~WiiMote(){
if(!onOff){
if(cwiid_disconnect(g_wiimote)){
std::cout<<"disconnection failed"<<std::endl;
}
else{
std::cout<<"disconnected successful"<<std::endl;
}
}
}
uint16_t WiiMote::getControls(){
if(!cwiid_get_state(g_wiimote, &g_wii_state)){
uint16_t temp = g_wii_state.buttons;
if(temp!=0){
uint16_t controls = 0;
if (temp&0x0200) controls|=0x0080;
if (temp&0x0100) controls|=0x0040;
if (temp&0x0800) controls|=0x0020;
if (temp&0x0400) controls|=0x0010;
if (temp&0x0008) controls|=0x0008;
if (temp&0x0004) controls|=0x0004;
if (temp&0x0080) controls|=0x0800;
if (temp&0x1000) controls|=0x0400;
if (temp&0x0010) controls|=0x0200;
if (temp&0x0002) controls|=0x0100;
if (temp&0x0001) controls|=0x1000;
std::cout<<"button: "<<controls<<std::endl;
return controls;
}
}
return 0x0000;
}
void WiiMote::start(){
if(onOff){
onOff = false;
std::cout<<"press keys:"<<std::endl;
// Establish a continous and non-blocking connection
g_wiimote = cwiid_connect(&g_bdaddr,
CWIID_FLAG_CONTINUOUS|CWIID_FLAG_NONBLOCK);
if(g_wiimote==NULL){
std::cout<<"connection failed"<<std::endl;
}
else{
std::cout<<"connection successful"<<std::endl;
}
cwiid_command(g_wiimote, CWIID_CMD_RPT_MODE,CWIID_RPT_IR|CWIID_RPT_ACC|CWIID_RPT_BTN);
if(!cwiid_set_rpt_mode(g_wiimote,CWIID_RPT_IR|CWIID_RPT_ACC|CWIID_RPT_BTN)){
inUse=true;
}
}
else{
inUse=true;
}
}
void WiiMote::stop(){
inUse = false;
}