Class: Joystick::SixAxis

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.new(path) ⇒ Object

TODO



359
360
361
362
363
364
365
366
367
368
369
# File 'ext/joystick.c', line 359

VALUE js_six_init(VALUE klass, VALUE path)
{
	int *fh;
	if((fh = malloc(sizeof(int))) != NULL) {
		if((*fh = open(RSTRING_PTR(path), O_RDONLY)) >= 0) {	
			return Data_Wrap_Struct(klass, jssix_mark, jssix_free, fh);
		} else
			rb_raise(rb_eException, "Error opening %s", RSTRING_PTR(path));
	}
	return Qnil;	
}

Instance Method Details

#close(path) ⇒ Object

TODO



416
417
418
419
420
421
422
423
# File 'ext/joystick.c', line 416

VALUE js_six_close(VALUE klass)
{
	int *fh;
	
	Data_Get_Struct(klass, int, fh);

	return INT2FIX(close(*fh));
}

#get_sixaxisObject

TODO



377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
# File 'ext/joystick.c', line 377

VALUE js_six_get_six(VALUE klass)
{
	int *fh;
	int res;
	int x = -1;
	int y = -1;
	int z = -1;
	unsigned char buf[128];
	VALUE saxis = rb_hash_new();

	Data_Get_Struct(klass, int, fh);
	if(res = read(*fh, buf, sizeof(buf))) {
		if(res == 48) {
   			x = buf[40]<<8 | buf[41];
			y = buf[42]<<8 | buf[43];
			z = buf[44]<<8 | buf[45];
		} else if(res == 49) {
			x = buf[41]<<8 | buf[42];
			y = buf[43]<<8 | buf[44];
			z = buf[45]<<8 | buf[46];
		}

		rb_hash_aset(saxis, ID2SYM(rb_intern("x")), INT2FIX(x));	
		rb_hash_aset(saxis, ID2SYM(rb_intern("y")), INT2FIX(y));	
		rb_hash_aset(saxis, ID2SYM(rb_intern("z")), INT2FIX(z));	

		return saxis;
	} else
		rb_raise(rb_eException, "error");

	return Qnil;
}