Class: Wii::WiimoteManager

Inherits:
Object
  • Object
show all
Defined in:
ext/wii4r/wiimotemanager.c

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeObject

WiimoteManager.new

Returns a new empty WiimoteManager.



47
48
49
50
51
# File 'ext/wii4r/wiimotemanager.c', line 47

static VALUE rb_cm_init(VALUE self) {
  VALUE ary = rb_ary_new();
  rb_iv_set(self, "@wiimotes", ary);
  return self;
}

Class Method Details

.newObject



28
29
30
31
32
33
34
35
36
37
38
# File 'ext/wii4r/wiimotemanager.c', line 28

static VALUE rb_cm_new(VALUE self) {
  connman * conn;
  VALUE m = rb_const_get(wii_mod, rb_intern("MAX_WIIMOTES"));
  int max = NUM2INT(m);
  VALUE obj = Data_Make_Struct(self, connman, NULL, free, conn);
  if(!conn) rb_raise(gen_exp_class, "not enough memory");
  conn->wms = wiiuse_init(max);
  conn->n = max; 
  rb_obj_call_init(obj, 0, 0);
  return obj;
}

Instance Method Details

#cleanup!Object

manager.cleanup!

Disconnect all the wiimotes (see Wiimote) managed by self and dealloc all structures.



78
79
80
81
82
83
84
85
86
87
88
# File 'ext/wii4r/wiimotemanager.c', line 78

static VALUE rb_cm_cleanup(VALUE self) {
  connman * conn;
  Data_Get_Struct(self, connman, conn);
  if(!conn) rb_raise(gen_exp_class, "WiimoteManager not properly initialized, cannot do cleanup");
  if(!(conn->wms)) return Qnil;
  wiiuse_cleanup(conn->wms, conn->n);
  conn->wms = NULL;
  VALUE ary = rb_iv_get(self, "@wiimotes");
  rb_funcall(ary, rb_intern("clear"), 0, NULL);
  return Qnil;
}

#connectObject

manager.connect -> int

Tries to connect to all found wiimotes (see Wiimote) and returns the number of successfull connections.



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
# File 'ext/wii4r/wiimotemanager.c', line 132

static VALUE rb_cm_connect(VALUE self) {
  connman *conn;
  Data_Get_Struct(self, connman, conn);
  if(!conn) rb_raise(gen_exp_class, "WiimoteManager not properly initialized, cannot do connect");
  
  int i = 0, led = 1, connected = 0, found = 0;
  VALUE wm = Qnil, exp = Qnil;
  VALUE max = rb_const_get(wii_mod, rb_intern("MAX_WIIMOTES"));
  VALUE timeout = rb_const_get(wii_mod, rb_intern("TIMEOUT"));
  
  found = wiiuse_find(conn->wms, NUM2INT(max), NUM2INT(timeout));
  if(!found) return INT2NUM(0); 
  connected = wiiuse_connect(conn->wms, NUM2INT(max));

  for(; i < NUM2INT(max); i++) {
    if(wm_connected(conn->wms[i])) {
      switch(led) {
        case 1:
          wiiuse_set_leds(conn->wms[i], WIIMOTE_LED_1);
          led++;
          break;
        case 2:
          wiiuse_set_leds(conn->wms[i], WIIMOTE_LED_2);
          led++;
          break;
        case 3:
          wiiuse_set_leds(conn->wms[i], WIIMOTE_LED_3);
          led++;
          break;
        case 4:
          wiiuse_set_leds(conn->wms[i], WIIMOTE_LED_4);
          break;  
      }
      wm = Data_Wrap_Struct(wii_class, NULL, free_wiimote, conn->wms[i]);
      rb_obj_call_init(wm, 0, 0);
      switch(conn->wms[i]->exp.type) {
        case EXP_NUNCHUK:
          exp = Data_Wrap_Struct(nun_class, NULL, NULL, &(conn->wms[i]->exp.nunchuk));
          break;
        case EXP_CLASSIC:
          exp = Data_Wrap_Struct(cc_class, NULL, NULL, &(conn->wms[i]->exp.classic));
          break;
        case EXP_GUITAR_HERO_3:
          exp = Data_Wrap_Struct(gh3_class, NULL, NULL, &(conn->wms[i]->exp.gh3));
          break;
      }
      set_expansion(wm, exp);
      rb_ary_push(rb_iv_get(self, "@wiimotes"), wm);
    }
  }
  return INT2NUM(connected);
}

#connectedObject

manager.connected -> int

Returns the number of wiimotes (see Wiimote) connected and managed by self.

wm = WiimoteManager.new wm.connected #=> 0



64
65
66
67
68
# File 'ext/wii4r/wiimotemanager.c', line 64

static VALUE rb_cm_connected(VALUE self) {
  VALUE wm = rb_iv_get(self, "@wiimotes");
  VALUE size = rb_funcall(wm, rb_intern("size"), 0, NULL);
  return size;
}

#each_wiimoteObject

manager.each_wiimote { |wiimote| block } -> nil

Calls block once for each Wiimote connected to self, passing a Wiimote object as a parameter. wm.each_wiimote { |wiimote| puts wiimote }



293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
# File 'ext/wii4r/wiimotemanager.c', line 293

static VALUE rb_cm_each(VALUE self) {
  if(rb_block_given_p()) {
    VALUE connected = rb_funcall(self, rb_intern("connected"), 0, NULL);
    VALUE wiimotes = rb_iv_get(self, "@wiimotes");
    VALUE argv[1];
    VALUE wm;
    
    int i = 0;
    
    for(; i < NUM2INT(connected); i++) {
      argv[0] = INT2NUM(i);
      wm = rb_ary_aref(1, argv, wiimotes);
      rb_yield(wm);
    }
  } 
  return Qnil;
}

#foundObject

manager.found -> int

Returns the number of wiimotes (see Wiimote) found in bluetooth range but not already connected.



114
115
116
117
118
119
120
121
122
# File 'ext/wii4r/wiimotemanager.c', line 114

static VALUE rb_cm_found(VALUE self) {
  connman * conn;
  Data_Get_Struct(self, connman, conn);
  if(!conn) rb_raise(gen_exp_class, "WiimoteManager not properly initialized, cannot do found");
  VALUE timeout = rb_const_get(wii_mod, rb_intern("TIMEOUT"));
  VALUE max = rb_const_get(wii_mod, rb_intern("MAX_WIIMOTES"));
  int found = wiiuse_find(conn->wms, NUM2INT(max), NUM2INT(timeout));
  return INT2NUM(found);
}

#pollObject

manager.poll { |(wiimote, event)| block } -> nil

Invokes block once per event captured by the WiimoteManager class. wiimote is the Wiimote which caused the event event, a Symbol who represents the type of the event caused.

wm.poll { |(wiimote, event)| if event == :generic puts “generic event occurred” end }



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
# File 'ext/wii4r/wiimotemanager.c', line 199

static VALUE rb_cm_poll(VALUE self) {
  if(rb_block_given_p()) {
    VALUE connected = rb_funcall(self, rb_intern("connected"), 0, NULL);
    
    if(NUM2INT(connected) > 0) {
      connman *conn;
      Data_Get_Struct(self, connman, conn);
      if(!conn) rb_raise(gen_exp_class, "WiimoteManager not properly initialized, cannot do poll");
      
      VALUE wiimotes = rb_iv_get(self, "@wiimotes");
      
      int i = 0;
      VALUE ary = Qnil, wm = Qnil, event_name = Qnil, exp = Qnil;
      VALUE argv[1];
      wiimote * wmm;
      
      VALUE max = rb_const_get(wii_mod, rb_intern("MAX_WIIMOTES"));
      if(wiiuse_poll(conn->wms, NUM2INT(max))) {
        for(; i < NUM2INT(connected); i++) {
          argv[0] = INT2NUM(i);
          wm = rb_ary_aref(1, argv, wiimotes);
          Data_Get_Struct(wm, wiimote, wmm);

          if(wmm && wmm->event != WIIUSE_NONE) {
            ary = rb_ary_new();
            rb_ary_push(ary, wm);
            switch(wmm->event) {
              case WIIUSE_EVENT:
                event_name = ID2SYM(rb_intern("generic"));
                break;
              case WIIUSE_STATUS:
                event_name = ID2SYM(rb_intern("status"));
                break;
              case WIIUSE_DISCONNECT:
                event_name = ID2SYM(rb_intern("disconnected"));
                break;
              case WIIUSE_UNEXPECTED_DISCONNECT:
                event_name = ID2SYM(rb_intern("unexpected_disconnect"));
                break;
              case WIIUSE_READ_DATA:
                event_name = ID2SYM(rb_intern("read"));
                break;
              case WIIUSE_NUNCHUK_INSERTED:
                event_name = ID2SYM(rb_intern("nunchuk_inserted"));
                exp = Data_Wrap_Struct(nun_class, NULL, NULL, &(wmm->exp.nunchuk));
                set_expansion(wm, exp);
                break;
              case WIIUSE_NUNCHUK_REMOVED:
                event_name = ID2SYM(rb_intern("nunchuk_removed"));
                set_expansion(wm, Qnil);
                break;
              case WIIUSE_CLASSIC_CTRL_INSERTED:
                event_name = ID2SYM(rb_intern("classic_inserted"));
                exp = Data_Wrap_Struct(cc_class, NULL, NULL, &(wmm->exp.classic));
                set_expansion(wm, exp);
                break;
              case WIIUSE_CLASSIC_CTRL_REMOVED:
                event_name = ID2SYM(rb_intern("classic_removed"));
                set_expansion(wm, Qnil);
                break;
              case WIIUSE_GUITAR_HERO_3_CTRL_INSERTED:
                event_name = ID2SYM(rb_intern("guitarhero3_inserted"));
                exp = Data_Wrap_Struct(gh3_class, NULL, NULL, &(wmm->exp.gh3));
                set_expansion(wm, exp);
                break;
              case WIIUSE_GUITAR_HERO_3_CTRL_REMOVED:
                event_name = ID2SYM(rb_intern("guitarhero3_removed"));
                set_expansion(wm, Qnil);
                break;
              case WIIUSE_CONNECT:
                event_name = ID2SYM(rb_intern("connected"));
                break;
            }
            rb_ary_push(ary, event_name);
            rb_yield(ary);
          }
        }
      }
    }
  }
  return Qnil;
}

#positionsObject

manager.positions -> array

Returns an array containing the actual position (x, y) of all wiimotes (see Wiimote) connected with self



320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# File 'ext/wii4r/wiimotemanager.c', line 320

static VALUE rb_cm_pos(VALUE self) {
  VALUE wiimotes = rb_iv_get(self, "@wiimotes");
  VALUE ary = rb_ary_new();
  VALUE wsize = rb_funcall(wiimotes, rb_intern("size"), 0, NULL);
  int size = NUM2INT(wsize);
  short i;
  VALUE argv[1], wm, pos;
  
  for(i = 0; i < size; i++) {
    argv[0] = INT2NUM(i);
    wm = rb_ary_aref(1, argv, wiimotes);
    pos = rb_funcall(wm, rb_intern("position"), 0, NULL);
    rb_ary_push(ary, pos);
  }
  return ary;
}

#wiimotesObject

manager.wiimotes -> array

Returns an array containing the wiimotes (see Wiimote) connected to self. May be an empty array.

wm = WiimoteManager.new wm.wiimotes #=> []



101
102
103
104
# File 'ext/wii4r/wiimotemanager.c', line 101

static VALUE rb_cm_wiimotes(VALUE self) {
  VALUE wii = rb_iv_get(self, "@wiimotes");
  return wii;
}