wii4r 0.5.0-x86-linux

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,369 @@
1
+ /*
2
+ ############################################################################
3
+ # #
4
+ # Copyright (C) 2009 by KzMz KzMz@modusbibendi.org #
5
+ # Copyright (C) 2009 by BuZz Gambino.Giorgio@gmail.com #
6
+ # #
7
+ # This program is free software; you can redistribute it and/or modify #
8
+ # it under the terms of the GNU General Public License as published by #
9
+ # the Free Software Foundation; either version 2 of the License, or #
10
+ # (at your option) any later version. #
11
+ # #
12
+ # This program is distributed in the hope that it will be useful, #
13
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of #
14
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
15
+ # GNU General Public License for more details. #
16
+ # #
17
+ # You should have received a copy of the GNU General Public License #
18
+ # along with this program; if not, write to the #
19
+ # Free Software Foundation, Inc., #
20
+ # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #
21
+ ############################################################################
22
+ */
23
+
24
+ #include "wii4r.h"
25
+
26
+ extern void set_expansion(VALUE self, VALUE exp_obj);
27
+
28
+ static VALUE rb_cm_new(VALUE self) {
29
+ connman * conn;
30
+ VALUE m = rb_const_get(wii_mod, rb_intern("MAX_WIIMOTES"));
31
+ int max = NUM2INT(m);
32
+ VALUE obj = Data_Make_Struct(self, connman, NULL, free, conn);
33
+ if(!conn) rb_raise(gen_exp_class, "not enough memory");
34
+ conn->wms = wiiuse_init(max);
35
+ conn->n = max;
36
+ rb_obj_call_init(obj, 0, 0);
37
+ return obj;
38
+ }
39
+
40
+ /*
41
+ * call-seq:
42
+ * WiimoteManager.new
43
+ *
44
+ * Returns a new empty WiimoteManager.
45
+ */
46
+
47
+ static VALUE rb_cm_init(VALUE self) {
48
+ VALUE ary = rb_ary_new();
49
+ rb_iv_set(self, "@wiimotes", ary);
50
+ return self;
51
+ }
52
+
53
+ /*
54
+ * call-seq:
55
+ * manager.connected -> int
56
+ *
57
+ * Returns the number of wiimotes (see <code>Wiimote</code>) connected and managed by <i>self</i>.
58
+ *
59
+ * wm = WiimoteManager.new
60
+ * wm.connected #=> 0
61
+ *
62
+ */
63
+
64
+ static VALUE rb_cm_connected(VALUE self) {
65
+ VALUE wm = rb_iv_get(self, "@wiimotes");
66
+ VALUE size = rb_funcall(wm, rb_intern("size"), 0, NULL);
67
+ return size;
68
+ }
69
+
70
+ /*
71
+ * call-seq:
72
+ * manager.cleanup!
73
+ *
74
+ * Disconnect all the wiimotes (see <code>Wiimote</code>) managed by <i>self</i> and dealloc all structures.
75
+ *
76
+ */
77
+
78
+ static VALUE rb_cm_cleanup(VALUE self) {
79
+ connman * conn;
80
+ Data_Get_Struct(self, connman, conn);
81
+ if(!conn) rb_raise(gen_exp_class, "WiimoteManager not properly initialized, cannot do cleanup");
82
+ if(!(conn->wms)) return Qnil;
83
+ wiiuse_cleanup(conn->wms, conn->n);
84
+ conn->wms = NULL;
85
+ VALUE ary = rb_iv_get(self, "@wiimotes");
86
+ rb_funcall(ary, rb_intern("clear"), 0, NULL);
87
+ return Qnil;
88
+ }
89
+
90
+ /*
91
+ * call-seq:
92
+ * manager.wiimotes -> array
93
+ *
94
+ * Returns an array containing the wiimotes (see <code>Wiimote</code>) connected to <i>self</i>.
95
+ * May be an empty array.
96
+ *
97
+ * wm = WiimoteManager.new
98
+ * wm.wiimotes #=> []
99
+ */
100
+
101
+ static VALUE rb_cm_wiimotes(VALUE self) {
102
+ VALUE wii = rb_iv_get(self, "@wiimotes");
103
+ return wii;
104
+ }
105
+
106
+ /*
107
+ * call-seq:
108
+ * manager.found -> int
109
+ *
110
+ * Returns the number of wiimotes (see <code>Wiimote</code>) found in bluetooth range but not already connected.
111
+ *
112
+ */
113
+
114
+ static VALUE rb_cm_found(VALUE self) {
115
+ connman * conn;
116
+ Data_Get_Struct(self, connman, conn);
117
+ if(!conn) rb_raise(gen_exp_class, "WiimoteManager not properly initialized, cannot do found");
118
+ VALUE timeout = rb_const_get(wii_mod, rb_intern("TIMEOUT"));
119
+ VALUE max = rb_const_get(wii_mod, rb_intern("MAX_WIIMOTES"));
120
+ int found = wiiuse_find(conn->wms, NUM2INT(max), NUM2INT(timeout));
121
+ return INT2NUM(found);
122
+ }
123
+
124
+ /*
125
+ * call-seq:
126
+ * manager.connect -> int
127
+ *
128
+ * Tries to connect to all found wiimotes (see <code>Wiimote</code>) and returns the number of successfull connections.
129
+ *
130
+ */
131
+
132
+ static VALUE rb_cm_connect(VALUE self) {
133
+ connman *conn;
134
+ Data_Get_Struct(self, connman, conn);
135
+ if(!conn) rb_raise(gen_exp_class, "WiimoteManager not properly initialized, cannot do connect");
136
+
137
+ int i = 0, led = 1, connected = 0, found = 0;
138
+ VALUE wm = Qnil, exp = Qnil;
139
+ VALUE max = rb_const_get(wii_mod, rb_intern("MAX_WIIMOTES"));
140
+ VALUE timeout = rb_const_get(wii_mod, rb_intern("TIMEOUT"));
141
+
142
+ found = wiiuse_find(conn->wms, NUM2INT(max), NUM2INT(timeout));
143
+ if(!found) return INT2NUM(0);
144
+ connected = wiiuse_connect(conn->wms, NUM2INT(max));
145
+
146
+ for(; i < NUM2INT(max); i++) {
147
+ if(wm_connected(conn->wms[i])) {
148
+ switch(led) {
149
+ case 1:
150
+ wiiuse_set_leds(conn->wms[i], WIIMOTE_LED_1);
151
+ led++;
152
+ break;
153
+ case 2:
154
+ wiiuse_set_leds(conn->wms[i], WIIMOTE_LED_2);
155
+ led++;
156
+ break;
157
+ case 3:
158
+ wiiuse_set_leds(conn->wms[i], WIIMOTE_LED_3);
159
+ led++;
160
+ break;
161
+ case 4:
162
+ wiiuse_set_leds(conn->wms[i], WIIMOTE_LED_4);
163
+ break;
164
+ }
165
+ wm = Data_Wrap_Struct(wii_class, NULL, free_wiimote, conn->wms[i]);
166
+ rb_obj_call_init(wm, 0, 0);
167
+ switch(conn->wms[i]->exp.type) {
168
+ case EXP_NUNCHUK:
169
+ exp = Data_Wrap_Struct(nun_class, NULL, NULL, &(conn->wms[i]->exp.nunchuk));
170
+ break;
171
+ case EXP_CLASSIC:
172
+ exp = Data_Wrap_Struct(cc_class, NULL, NULL, &(conn->wms[i]->exp.classic));
173
+ break;
174
+ case EXP_GUITAR_HERO_3:
175
+ exp = Data_Wrap_Struct(gh3_class, NULL, NULL, &(conn->wms[i]->exp.gh3));
176
+ break;
177
+ }
178
+ set_expansion(wm, exp);
179
+ rb_ary_push(rb_iv_get(self, "@wiimotes"), wm);
180
+ }
181
+ }
182
+ return INT2NUM(connected);
183
+ }
184
+
185
+ /*
186
+ * call-seq:
187
+ * manager.poll { |(wiimote, event)| block } -> nil
188
+ *
189
+ * Invokes <i>block</i> once per event captured by the WiimoteManager class. <code>wiimote</code> is the Wiimote which caused
190
+ * the event <code>event</code>, a Symbol who represents the type of the event caused.
191
+ *
192
+ * wm.poll { |(wiimote, event)|
193
+ * if event == :generic
194
+ * puts "generic event occurred"
195
+ * end
196
+ * }
197
+ */
198
+
199
+ static VALUE rb_cm_poll(VALUE self) {
200
+ if(rb_block_given_p()) {
201
+ VALUE connected = rb_funcall(self, rb_intern("connected"), 0, NULL);
202
+
203
+ if(NUM2INT(connected) > 0) {
204
+ connman *conn;
205
+ Data_Get_Struct(self, connman, conn);
206
+ if(!conn) rb_raise(gen_exp_class, "WiimoteManager not properly initialized, cannot do poll");
207
+
208
+ VALUE wiimotes = rb_iv_get(self, "@wiimotes");
209
+
210
+ int i = 0;
211
+ VALUE ary = Qnil, wm = Qnil, event_name = Qnil, exp = Qnil;
212
+ VALUE argv[1];
213
+ wiimote * wmm;
214
+
215
+ VALUE max = rb_const_get(wii_mod, rb_intern("MAX_WIIMOTES"));
216
+ if(wiiuse_poll(conn->wms, NUM2INT(max))) {
217
+ for(; i < NUM2INT(connected); i++) {
218
+ argv[0] = INT2NUM(i);
219
+ wm = rb_ary_aref(1, argv, wiimotes);
220
+ Data_Get_Struct(wm, wiimote, wmm);
221
+
222
+ if(wmm && wmm->event != WIIUSE_NONE) {
223
+ ary = rb_ary_new();
224
+ rb_ary_push(ary, wm);
225
+ switch(wmm->event) {
226
+ case WIIUSE_EVENT:
227
+ event_name = ID2SYM(rb_intern("generic"));
228
+ break;
229
+ case WIIUSE_STATUS:
230
+ event_name = ID2SYM(rb_intern("status"));
231
+ break;
232
+ case WIIUSE_DISCONNECT:
233
+ event_name = ID2SYM(rb_intern("disconnected"));
234
+ break;
235
+ case WIIUSE_UNEXPECTED_DISCONNECT:
236
+ event_name = ID2SYM(rb_intern("unexpected_disconnect"));
237
+ break;
238
+ case WIIUSE_READ_DATA:
239
+ event_name = ID2SYM(rb_intern("read"));
240
+ break;
241
+ case WIIUSE_NUNCHUK_INSERTED:
242
+ event_name = ID2SYM(rb_intern("nunchuk_inserted"));
243
+ exp = Data_Wrap_Struct(nun_class, NULL, NULL, &(wmm->exp.nunchuk));
244
+ set_expansion(wm, exp);
245
+ break;
246
+ case WIIUSE_NUNCHUK_REMOVED:
247
+ event_name = ID2SYM(rb_intern("nunchuk_removed"));
248
+ set_expansion(wm, Qnil);
249
+ break;
250
+ case WIIUSE_CLASSIC_CTRL_INSERTED:
251
+ event_name = ID2SYM(rb_intern("classic_inserted"));
252
+ exp = Data_Wrap_Struct(cc_class, NULL, NULL, &(wmm->exp.classic));
253
+ set_expansion(wm, exp);
254
+ break;
255
+ case WIIUSE_CLASSIC_CTRL_REMOVED:
256
+ event_name = ID2SYM(rb_intern("classic_removed"));
257
+ set_expansion(wm, Qnil);
258
+ break;
259
+ case WIIUSE_GUITAR_HERO_3_CTRL_INSERTED:
260
+ event_name = ID2SYM(rb_intern("guitarhero3_inserted"));
261
+ exp = Data_Wrap_Struct(gh3_class, NULL, NULL, &(wmm->exp.gh3));
262
+ set_expansion(wm, exp);
263
+ break;
264
+ case WIIUSE_GUITAR_HERO_3_CTRL_REMOVED:
265
+ event_name = ID2SYM(rb_intern("guitarhero3_removed"));
266
+ set_expansion(wm, Qnil);
267
+ break;
268
+ case WIIUSE_CONNECT:
269
+ event_name = ID2SYM(rb_intern("connected"));
270
+ break;
271
+ }
272
+ rb_ary_push(ary, event_name);
273
+ rb_yield(ary);
274
+ }
275
+ }
276
+ }
277
+ }
278
+ }
279
+ return Qnil;
280
+ }
281
+
282
+ /*
283
+ * call-seq:
284
+ * manager.each_wiimote { |wiimote| block } -> nil
285
+ *
286
+ * Calls <i>block</i> once for each Wiimote connected to <i>self</i>, passing a Wiimote object as a parameter.
287
+ * wm.each_wiimote { |wiimote|
288
+ * puts wiimote
289
+ * }
290
+ *
291
+ */
292
+
293
+ static VALUE rb_cm_each(VALUE self) {
294
+ if(rb_block_given_p()) {
295
+ VALUE connected = rb_funcall(self, rb_intern("connected"), 0, NULL);
296
+ VALUE wiimotes = rb_iv_get(self, "@wiimotes");
297
+ VALUE argv[1];
298
+ VALUE wm;
299
+
300
+ int i = 0;
301
+
302
+ for(; i < NUM2INT(connected); i++) {
303
+ argv[0] = INT2NUM(i);
304
+ wm = rb_ary_aref(1, argv, wiimotes);
305
+ rb_yield(wm);
306
+ }
307
+ }
308
+ return Qnil;
309
+ }
310
+
311
+
312
+ /*
313
+ * call-seq:
314
+ * manager.positions -> array
315
+ *
316
+ * Returns an array containing the actual position (x, y) of all wiimotes (see <code>Wiimote</code>) connected with <i>self</i>
317
+ *
318
+ */
319
+
320
+ static VALUE rb_cm_pos(VALUE self) {
321
+ VALUE wiimotes = rb_iv_get(self, "@wiimotes");
322
+ VALUE ary = rb_ary_new();
323
+ VALUE wsize = rb_funcall(wiimotes, rb_intern("size"), 0, NULL);
324
+ int size = NUM2INT(wsize);
325
+ short i;
326
+ VALUE argv[1], wm, pos;
327
+
328
+ for(i = 0; i < size; i++) {
329
+ argv[0] = INT2NUM(i);
330
+ wm = rb_ary_aref(1, argv, wiimotes);
331
+ pos = rb_funcall(wm, rb_intern("position"), 0, NULL);
332
+ rb_ary_push(ary, pos);
333
+ }
334
+ return ary;
335
+ }
336
+
337
+ /*
338
+ *
339
+ * Provides an interface for searching wiimotes and connecting to them via bluetooth.
340
+ * WiimoteManager also provides event management.
341
+ * The events that can be caused by a Wiimote device are:
342
+ * :generic -> pression of a button, accelerometer or ir event
343
+ * :status -> status response received from wiimote
344
+ * :connect -> fired when a Wiimote connects
345
+ * :disconnect -> fired when a Wiimote disconnects
346
+ * :enexpected_disconnect -> fired when a Wiimote disconnects enexpectedly
347
+ * :nunchuk_inserted -> fired when a Nunchuk is inserted in a Wiimote
348
+ * :nunchuk_removed -> fired when a Nunchuk is removed from a Wiimote
349
+ * :classic_inserted -> fired when a Classic Controller is inserted in a Wiimote
350
+ * :classic_removed -> fired when a Classic Controller is removed from a Wiimote
351
+ * :guitarhero3_inserted -> fired when a Guitar Hero 3 Controller is inserted in a Wiimote
352
+ * :guitarhero3_removed -> fired when a Guitar Hero 3 Controller is removed from a Wiimote
353
+ *
354
+ */
355
+
356
+ void init_wiimotemanager(void) {
357
+
358
+ cm_class = rb_define_class_under(wii_mod, "WiimoteManager", rb_cObject);
359
+ rb_define_singleton_method(cm_class, "new", rb_cm_new, 0);
360
+ rb_define_method(cm_class, "wiimotes", rb_cm_wiimotes, 0);
361
+ rb_define_method(cm_class, "initialize", rb_cm_init, 0);
362
+ rb_define_method(cm_class, "connected", rb_cm_connected, 0);
363
+ rb_define_method(cm_class, "cleanup!", rb_cm_cleanup, 0);
364
+ rb_define_method(cm_class, "found", rb_cm_found, 0);
365
+ rb_define_method(cm_class, "connect", rb_cm_connect, 0);
366
+ rb_define_method(cm_class, "poll", rb_cm_poll, 0);
367
+ rb_define_method(cm_class, "each_wiimote", rb_cm_each, 0);
368
+ rb_define_method(cm_class, "positions", rb_cm_pos, 0);
369
+ }
data/lib/wii4r.so ADDED
Binary file
data/wii4r.gemspec ADDED
@@ -0,0 +1,28 @@
1
+ require 'rubygems'
2
+
3
+ GEM_SPEC = Gem::Specification.new do |spec|
4
+ spec.name = "wii4r"
5
+ spec.authors = ['KzMz', 'BuZz']
6
+ spec.email = ['KzMz@modusbibendi.org', 'gambino.giorgio@gmail.com']
7
+ spec.version = "0.5.0"
8
+ spec.platform = Gem::Platform::RUBY
9
+
10
+ spec.summary = "bindings for wiiuse, a C library for controlling Wiimotes"
11
+ spec.description = "Ruby C Extension for controlling Wii Remote devices via bluetooth connection. Binding of C wiiuse library (www.wiiuse.net) "
12
+ spec.required_ruby_version = ">= 1.8.6"
13
+ spec.required_rubygems_version = ">= 1.3.5"
14
+
15
+ spec.add_dependency "rake", ">= 0.8.3", "< 0.9"
16
+ spec.add_dependency "rake-compiler", ">= 0.7.0"
17
+ spec.require_path = "lib"
18
+ spec.files = FileList["LICENSE", "Rakefile", "README.rdoc", "wii4r.gemspec", "examples/*.rb", "lib/*.*", "ext/**/*.{c, h, rb}"]
19
+
20
+ spec.has_rdoc = true
21
+ spec.rdoc_options << "--main" << "ext/wii4r/wii4r.c"
22
+ spec.extra_rdoc_files = ['ext/wii4r/wii4r.c', "ext/wii4r/wiimotemanager.c", "ext/wii4r/wiimote.c", "ext/wii4r/nunchuk.c", "ext/wii4r/classic.c", "ext/wii4r/guitarhero3.c"]
23
+
24
+ spec.homepage = "http://github.com/KzMz/wii4r"
25
+ spec.licenses = ['GPL']
26
+ end
27
+
28
+
metadata ADDED
@@ -0,0 +1,98 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wii4r
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.5.0
5
+ platform: x86-linux
6
+ authors:
7
+ - KzMz
8
+ - BuZz
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2010-03-01 00:00:00 +01:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: rake
18
+ type: :runtime
19
+ version_requirement:
20
+ version_requirements: !ruby/object:Gem::Requirement
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: 0.8.3
25
+ - - <
26
+ - !ruby/object:Gem::Version
27
+ version: "0.9"
28
+ version:
29
+ - !ruby/object:Gem::Dependency
30
+ name: rake-compiler
31
+ type: :runtime
32
+ version_requirement:
33
+ version_requirements: !ruby/object:Gem::Requirement
34
+ requirements:
35
+ - - ">="
36
+ - !ruby/object:Gem::Version
37
+ version: 0.7.0
38
+ version:
39
+ description: "Ruby C Extension for controlling Wii Remote devices via bluetooth connection. Binding of C wiiuse library (www.wiiuse.net) "
40
+ email:
41
+ - KzMz@modusbibendi.org
42
+ - gambino.giorgio@gmail.com
43
+ executables: []
44
+
45
+ extensions: []
46
+
47
+ extra_rdoc_files:
48
+ - ext/wii4r/wii4r.c
49
+ - ext/wii4r/wiimotemanager.c
50
+ - ext/wii4r/wiimote.c
51
+ - ext/wii4r/nunchuk.c
52
+ - ext/wii4r/classic.c
53
+ - ext/wii4r/guitarhero3.c
54
+ files:
55
+ - LICENSE
56
+ - Rakefile
57
+ - README.rdoc
58
+ - wii4r.gemspec
59
+ - examples/playsound.rb
60
+ - examples/poll.rb
61
+ - lib/wii4r.so
62
+ - ext/wii4r/nunchuk.c
63
+ - ext/wii4r/wiimote.c
64
+ - ext/wii4r/guitarhero3.c
65
+ - ext/wii4r/classic.c
66
+ - ext/wii4r/wiimotemanager.c
67
+ - ext/wii4r/wii4r.c
68
+ has_rdoc: true
69
+ homepage: http://github.com/KzMz/wii4r
70
+ licenses:
71
+ - GPL
72
+ post_install_message:
73
+ rdoc_options:
74
+ - --main
75
+ - ext/wii4r/wii4r.c
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: 1.8.6
83
+ version:
84
+ required_rubygems_version: !ruby/object:Gem::Requirement
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: 1.3.5
89
+ version:
90
+ requirements: []
91
+
92
+ rubyforge_project:
93
+ rubygems_version: 1.3.5
94
+ signing_key:
95
+ specification_version: 3
96
+ summary: bindings for wiiuse, a C library for controlling Wiimotes
97
+ test_files: []
98
+