This repository has been archived by the owner on Sep 3, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wiimote.cpp
314 lines (269 loc) · 9.93 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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
#include <iostream>
#include <vector>
#include <iomanip>
#include <locale>
#include <sstream>
#include <string>
#include <cmath>
#include <node.h>
#include <v8.h>
#include <float.h>
extern "C" {
#include <cwiid.h>
#include <bluetooth/bluetooth.h>
}
#define PI 3.14159265358979323
#define MAC 00:22:AA:8F:20:50
using namespace v8;
using namespace std;
template <typename T>
string numToStr ( T Number )
{
ostringstream ss;
ss << Number;
return ss.str();
}
cwiid_mesg_callback_t cwiid_callback;
cwiid_wiimote_t* wiimote;
struct cwiid_state state;
struct acc_cal wm_cal;
bdaddr_t mac;
string resultAcc = "";
string resultIr = "";
string resultBtn = "";
string resultStatus = "";
vector<int> buttons(11);
void cwiid_callback(cwiid_wiimote_t *wiimote, int mesg_count, union cwiid_mesg *mesg_array, struct timespec *timestamp)
{
double ax, ay, az, roll, pitch;
static enum cwiid_ext_type ext_type = CWIID_EXT_NONE;
for(int i = 0; i < mesg_count; i++)
{
switch(mesg_array[i].type)
{
case CWIID_MESG_STATUS:
resultStatus = "\"battery\": " + numToStr(100.0 * mesg_array[i].status_mesg.battery / CWIID_BATTERY_MAX);
break;
case CWIID_MESG_BTN:
resultBtn = "\"buttons\": {";
resultBtn += "\"A\": ";
resultBtn += numToStr(((&mesg_array[i].btn_mesg)->buttons & CWIID_BTN_A) ? (buttons[0] = 1) : (buttons[0] != 0 ? (buttons[0] = -1) : (buttons[0] = 0)));
resultBtn += ", ";
resultBtn += "\"B\": ";
resultBtn += numToStr(((&mesg_array[i].btn_mesg)->buttons & CWIID_BTN_B) ? (buttons[1] = 1) : (buttons[1] != 0 ? (buttons[1] = -1) : (buttons[1] = 0)));
resultBtn += ", ";
resultBtn += "\"up\": ";
resultBtn += numToStr(((&mesg_array[i].btn_mesg)->buttons & CWIID_BTN_UP) ? (buttons[2] = 1) : (buttons[2] != 0 ? (buttons[2] = -1) : (buttons[2] = 0)));
resultBtn += ", ";
resultBtn += "\"down\": ";
resultBtn += numToStr(((&mesg_array[i].btn_mesg)->buttons & CWIID_BTN_DOWN) ? (buttons[3] = 1) : (buttons[3] != 0 ? (buttons[3] = -1) : (buttons[3] = 0)));
resultBtn += ", ";
resultBtn += "\"left\": ";
resultBtn += numToStr(((&mesg_array[i].btn_mesg)->buttons & CWIID_BTN_LEFT) ? (buttons[4] = 1) : (buttons[4] != 0 ? (buttons[4] = -1) : (buttons[4] = 0)));
resultBtn += ", ";
resultBtn += "\"right\": ";
resultBtn += numToStr(((&mesg_array[i].btn_mesg)->buttons & CWIID_BTN_RIGHT) ? (buttons[5] = 1) : (buttons[5] != 0 ? (buttons[5] = -1) : (buttons[5] = 0)));
resultBtn += ", ";
resultBtn += "\"minus\": ";
resultBtn += numToStr(((&mesg_array[i].btn_mesg)->buttons & CWIID_BTN_MINUS) ? (buttons[6] = 1) : (buttons[6] != 0 ? (buttons[6] = -1) : (buttons[6] = 0)));
resultBtn += ", ";
resultBtn += "\"plus\": ";
resultBtn += numToStr(((&mesg_array[i].btn_mesg)->buttons & CWIID_BTN_PLUS) ? (buttons[7] = 1) : (buttons[7] != 0 ? (buttons[7] = -1) : (buttons[7] = 0)));
resultBtn += ", ";
resultBtn += "\"home\": ";
resultBtn += numToStr(((&mesg_array[i].btn_mesg)->buttons & CWIID_BTN_HOME) ? (buttons[8] = 1) : (buttons[8] != 0 ? (buttons[8] = -1) : (buttons[8] = 0)));
resultBtn += ", ";
resultBtn += "\"one\": ";
resultBtn += numToStr(((&mesg_array[i].btn_mesg)->buttons & CWIID_BTN_1) ? (buttons[9] = 1) : (buttons[9] != 0 ? (buttons[9] = -1) : (buttons[9] = 0)));
resultBtn += ", ";
resultBtn += "\"two\": ";
resultBtn += numToStr(((&mesg_array[i].btn_mesg)->buttons & CWIID_BTN_2) ? (buttons[10] = 1) : (buttons[10] != 0 ? (buttons[10] = -1) : (buttons[10] = 0)));
resultBtn += "}";
break;
case CWIID_MESG_ACC:
ax = ((double) (&mesg_array[i].acc_mesg)->acc[CWIID_X] - wm_cal.zero[CWIID_X]) / (wm_cal.one[CWIID_X] - wm_cal.zero[CWIID_X]);
ay = ((double) (&mesg_array[i].acc_mesg)->acc[CWIID_Y] - wm_cal.zero[CWIID_Y]) / (wm_cal.one[CWIID_Y] - wm_cal.zero[CWIID_Y]);
az = ((double) (&mesg_array[i].acc_mesg)->acc[CWIID_Z] - wm_cal.zero[CWIID_Z]) / (wm_cal.one[CWIID_Z] - wm_cal.zero[CWIID_Z]);
roll = -1 * (atan(ax / az) + (az <= 0 ? (PI * ((ax > 0) ? 1 : -1)) : 0));
pitch = atan(ay / az * cos(roll));
resultAcc = "\"acceleration\": {";
resultAcc += "\"x\": " + numToStr(ax) + ", ";
resultAcc += "\"y\": " + numToStr(ay) + ", ";
resultAcc += "\"z\": " + numToStr(az) + ", ";
resultAcc += "\"acc\": " + numToStr(sqrt(pow(ax, 2) + pow(ay, 2) + pow(az, 2))) + ", ";
resultAcc += "\"roll\": " + numToStr(roll) + ", ";
resultAcc += "\"pitch\": " + numToStr(pitch) + "}";
break;
case CWIID_MESG_IR:
if(cwiid_get_state(wiimote, &state))
{
return;
}
resultIr = "\"ir\": [";
for(int j = 0; j < CWIID_IR_SRC_COUNT; j++)
{
if(j!=0)
{
resultIr += ", ";
}
resultIr += "[" + numToStr(state.ir_src[j].pos[CWIID_X]) + ", " + numToStr(state.ir_src[j].pos[CWIID_Y]) + "]";
}
resultIr += "]";
break;
case CWIID_MESG_ERROR:
return;
break;
default:
break;
}
}
return;
}
Handle<Value> connect(const Arguments& args)
{
HandleScope scope;
String::Utf8Value param1(args[0]->ToString());
string macAdd = string(*param1);
str2ba(macAdd.c_str(), &mac);
if(!(wiimote = cwiid_open(&mac, CWIID_FLAG_MESG_IFC)))
{
return ThrowException(Exception::Error(String::New("Unable to connect to the device")));
}
else if(cwiid_set_mesg_callback(wiimote, &cwiid_callback))
{
return ThrowException(Exception::Error(String::New("Unable to configure message callback")));
}
if(cwiid_get_acc_cal(wiimote, CWIID_EXT_NONE, &wm_cal))
{
return ThrowException(Exception::Error(String::New("Unable to calibrate the acceleration sensor")));
}
cwiid_set_rpt_mode(wiimote, CWIID_RPT_STATUS);
cwiid_request_status(wiimote);
return scope.Close(Integer::New(1));
}
Handle<Value> disconnect(const Arguments& args)
{
HandleScope scope;
if(cwiid_close(wiimote))
{
return ThrowException(Exception::Error(String::New("Unable to close the connection")));
}
return scope.Close(Integer::New(0));
}
Handle<Value> rumble(const Arguments& args)
{
HandleScope scope;
int32_t in = args[0]->ToInteger()->Value();
unsigned char on = in ? 1 : 0;
if(cwiid_set_rumble(wiimote, on))
{
return ThrowException(Exception::Error(String::New("Unable to set rumble")));
}
return scope.Close(Integer::New(0));
}
Handle<Value> leds(const Arguments& args)
{
HandleScope scope;
int index = args[0]->ToInteger()->Value();
bool on = args[1]->ToBoolean()->Value();
int indexes[] = { CWIID_LED1_ON, CWIID_LED2_ON, CWIID_LED3_ON, CWIID_LED4_ON };
cwiid_get_state(wiimote, &state);
int led = state.led;
led = on ? led | indexes[index-1] : led ^ indexes[index-1];
if(cwiid_set_led(wiimote, led))
{
return ThrowException(Exception::Error(String::New("Unable to set leds")));
}
return scope.Close(Integer::New(0));
}
Handle<Value> enableModules(const Arguments& args)
{
HandleScope scope;
bool onIr = args[0]->ToBoolean()->Value();
bool onAcc = args[1]->ToBoolean()->Value();
bool onButtons = args[2]->ToBoolean()->Value();
bool onExt = args[3]->ToBoolean()->Value();
uint8_t mods = 0;
if(onIr){mods |= CWIID_RPT_IR;}
if(onAcc){mods |= CWIID_RPT_ACC;}
if(onButtons){mods |= CWIID_RPT_BTN;}
//if(onExt){mods |= CWIID_RPT_EXT;}
if(cwiid_set_rpt_mode(wiimote, mods | CWIID_RPT_STATUS))
{
return ThrowException(Exception::Error(String::New("Unable to set report mode")));
}
return scope.Close(Integer::New(0));
}
Handle<Value> getReport(const Arguments& args)
{
HandleScope scope;
string result = "{";
if(resultAcc != "")
{
result += resultAcc + ", ";
}
if(resultIr != "")
{
result += resultIr + ", ";
}
if(resultBtn != "")
{
result += resultBtn + ", ";
}
if(resultStatus != "")
{
result += resultStatus + ", ";
}
result = result.substr(0, result.size() - 2) + "}";
return scope.Close(String::New(result.c_str()));
}
Handle<Value> clearBtnBuffer(const Arguments& args)
{
HandleScope scope;
string id = "\"buttons\": {\"A\": ";
id += numToStr(buttons[0] == -1 ? (buttons[0] = 0) : buttons[0]);
id += ", ";
id += "\"B\": ";
id += numToStr(buttons[1] == -1 ? (buttons[1] = 0) : buttons[1]);
id += ", ";
id += "\"up\": ";
id += numToStr(buttons[2] == -1 ? (buttons[2] = 0) : buttons[2]);
id += ", ";
id += "\"down\": ";
id += numToStr(buttons[3] == -1 ? (buttons[3] = 0) : buttons[3]);
id += ", ";
id += "\"left\": ";
id += numToStr(buttons[4] == -1 ? (buttons[4] = 0) : buttons[4]);
id += ", ";
id += "\"right\": ";
id += numToStr(buttons[5] == -1 ? (buttons[5] = 0) : buttons[5]);
id += ", ";
id += "\"minus\": ";
id += numToStr(buttons[6] == -1 ? (buttons[6] = 0) : buttons[6]);
id += ", ";
id += "\"plus\": ";
id += numToStr(buttons[7] == -1 ? (buttons[7] = 0) : buttons[7]);
id += ", ";
id += "\"home\": ";
id += numToStr(buttons[8] == -1 ? (buttons[8] = 0) : buttons[8]);
id += ", ";
id += "\"one\": ";
id += numToStr(buttons[9] == -1 ? (buttons[9] = 0) : buttons[9]);
id += ", ";
id += "\"two\": ";
id += numToStr(buttons[10] == -1 ? (buttons[10] = 0) : buttons[10]);
id += "}";
resultBtn = id;
return scope.Close(Integer::New(0));
}
void init(v8::Handle<v8::Object> target)
{
target->Set(String::NewSymbol("connect"), FunctionTemplate::New(connect)->GetFunction());
target->Set(String::NewSymbol("disconnect"), FunctionTemplate::New(disconnect)->GetFunction());
target->Set(String::NewSymbol("rumble"), FunctionTemplate::New(rumble)->GetFunction());
target->Set(String::NewSymbol("led"), FunctionTemplate::New(leds)->GetFunction());
target->Set(String::NewSymbol("getReport"), FunctionTemplate::New(getReport)->GetFunction());
target->Set(String::NewSymbol("enableModules"), FunctionTemplate::New(enableModules)->GetFunction());
target->Set(String::NewSymbol("clearBtnBuffer"), FunctionTemplate::New(clearBtnBuffer)->GetFunction());
}
NODE_MODULE(wiimote, init);