-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWiimote.cpp
135 lines (119 loc) · 3.28 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include "Wiimote.h"
#include <iostream>
#include <SDL2/SDL.h>
#include "ControllerIntf.h"
#include "ShipIntf.h"
int Wiimote::index = 0;
WiimoteMap Wiimote::m_wiimoteMap;
//setup a constant for the equivalent of BD_ADDR_ANY
//because the one in bluetooth.h is stupid
const bdaddr_t Wiimote::kBdAddrAny = {{0, 0, 0, 0, 0, 0}};
Wiimote::Wiimote(ControllerIntf *wiimoteHandler)
:m_wiimoteHandler(wiimoteHandler),
m_pWiimote(NULL),
m_index(index++),
m_bdaddr(kBdAddrAny)
{
std::cout << "Put wiimote into discovery mode (press 1+2)" << std::endl;
m_pWiimote = cwiid_open(&m_bdaddr, CWIID_FLAG_MESG_IFC);
if(m_pWiimote == NULL)
{
std::cerr << "No connection. Quitting" << std::endl;
throw NO_CONNECTION;
}
else
{
std::cout << "Connected" << std::endl;
cwiid_command(m_pWiimote, CWIID_CMD_LED, CWIID_LED1_ON);
}
if (cwiid_set_mesg_callback(m_pWiimote, cwiid_callback))
{
std::cerr << "Error setting callback. Exitting" << std::endl;
cwiid_close(m_pWiimote);
m_pWiimote = NULL;
throw CALLBACK_SETUP_ERROR;
}
else
{
m_wiimoteMap[m_pWiimote] = this;
}
std::cout << "WiiMote " << m_index << " connected" << std::endl;
cwiid_command(m_pWiimote, CWIID_CMD_RPT_MODE, CWIID_RPT_BTN);
}
void Wiimote::WiimoteCallback(
int mesg_count,
union cwiid_mesg mesg_array[],
struct timespec */*timestamp*/)
{
for (int i=0; i < mesg_count; ++i)
{
switch (mesg_array[i].type)
{
case CWIID_MESG_BTN:
{
Uint32 buttons = mesg_array[i].btn_mesg.buttons;
Uint32 command = 0;
//TBD translate button to commands in the ShipIntf
if(buttons & CWIID_BTN_LEFT)
{
//Nothing yet
}
if(buttons & CWIID_BTN_UP)
{
command |= SHIP_CCW;
}
if(buttons & CWIID_BTN_DOWN)
{
command |= SHIP_CW;
}
if(buttons & CWIID_BTN_2)
{
command |= SHIP_FORWARD;
}
if(buttons & CWIID_BTN_1)
{
command |= SHIP_STOP;
}
if(buttons & CWIID_BTN_A)
{
command |= SHIP_SHOOT;
}
if(buttons & CWIID_BTN_B)
{
command |= SHIP_BOMB;
}
m_wiimoteHandler->ButtonPushed(m_index,command);
if(buttons & CWIID_BTN_HOME)
{
m_wiimoteHandler->ButtonHome(m_index);
}
break;
}
default:
break;
}
}
}
/*static*/
void Wiimote::cwiid_callback
(
cwiid_wiimote_t *wiimote,
int mesg_count,
union cwiid_mesg mesg_array[],
struct timespec *timestamp)
{
//This probably should be mutex protected
//search the static list for the wiimote to call back on
WiimoteMap::iterator iter = m_wiimoteMap.find(wiimote);
if(iter != m_wiimoteMap.end())
{
iter->second->WiimoteCallback(mesg_count,
mesg_array,
timestamp);
}
}
Wiimote::~Wiimote()
{
m_wiimoteMap.erase(m_pWiimote);
cwiid_close(m_pWiimote);
}