Skip to content

Commit

Permalink
Read Nunchuk and Nunchuk demo
Browse files Browse the repository at this point in the history
  • Loading branch information
ppibburr committed Aug 6, 2016
1 parent 8180811 commit fb00b80
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
47 changes: 47 additions & 0 deletions ext/wiimote.cc
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,27 @@ unsigned short int WiiMote::getButtons(void)
return m_state.buttons;
}

unsigned short int WiiMote::getNunchukButtons(void)
{
return m_state.ext.nunchuk.buttons;
}

unsigned char WiiMote::getAcc( int id )
{
return m_state.acc[ id ];
}

unsigned char WiiMote::getNunchukAcc( int id )
{
return m_state.ext.nunchuk.acc[ id ];
}

unsigned char WiiMote::getNunchukJoyStick( int id )
{
return m_state.ext.nunchuk.stick[ id ];
}


bool WiiMote::irValid( int i )
{
return m_state.ir_src[ i ].valid != 0;
Expand Down Expand Up @@ -180,6 +196,8 @@ VALUE WiiMote::registerRubyClass(void)
rb_define_const( cRubyClass, "BTN_1", INT2NUM( CWIID_BTN_1 ) );
rb_define_const( cRubyClass, "BTN_B", INT2NUM( CWIID_BTN_B ) );
rb_define_const( cRubyClass, "BTN_A", INT2NUM( CWIID_BTN_A ) );
rb_define_const( cRubyClass, "NUNCHUK_BTN_C", INT2NUM( CWIID_NUNCHUK_BTN_C ) );
rb_define_const( cRubyClass, "NUNCHUK_BTN_Z", INT2NUM( CWIID_NUNCHUK_BTN_Z ) );
rb_define_const( cRubyClass, "BTN_MINUS", INT2NUM( CWIID_BTN_MINUS ) );
rb_define_const( cRubyClass, "BTN_HOME", INT2NUM( CWIID_BTN_HOME ) );
rb_define_const( cRubyClass, "BTN_LEFT", INT2NUM( CWIID_BTN_LEFT ) );
Expand Down Expand Up @@ -216,8 +234,14 @@ VALUE WiiMote::registerRubyClass(void)
RUBY_METHOD_FUNC( wrapSetRumble ), 1 );
rb_define_method( cRubyClass, "buttons",
RUBY_METHOD_FUNC( wrapGetButtons ), 0 );
rb_define_method( cRubyClass, "nunchuk_buttons",
RUBY_METHOD_FUNC( wrapGetNunchukButtons ), 0 );
rb_define_method( cRubyClass, "acc",
RUBY_METHOD_FUNC( wrapGetAcc ), 0 );
rb_define_method( cRubyClass, "nunchuk_acc",
RUBY_METHOD_FUNC( wrapGetNunchukAcc ), 0 );
rb_define_method( cRubyClass, "nunchuk_stick",
RUBY_METHOD_FUNC( wrapGetNunchukJoyStick ), 0 );
rb_define_method( cRubyClass, "ir",
RUBY_METHOD_FUNC( wrapGetIR ), 0 );
rb_define_method( cRubyClass, "motionplus",
Expand Down Expand Up @@ -337,6 +361,8 @@ VALUE WiiMote::wrapGetButtons( VALUE rbSelf )
return INT2NUM( (*self)->getButtons() );
}



VALUE WiiMote::wrapGetAcc( VALUE rbSelf )
{
WiiMotePtr *self; Data_Get_Struct( rbSelf, WiiMotePtr, self );
Expand Down Expand Up @@ -366,3 +392,24 @@ VALUE WiiMote::wrapGetMotionPlus( VALUE rbSelf )
INT2NUM( (*self)->getMotionPlus( 2 ) ) );
}

VALUE WiiMote::wrapGetNunchukAcc( VALUE rbSelf )
{
WiiMotePtr *self; Data_Get_Struct( rbSelf, WiiMotePtr, self );
return rb_ary_new3( 3, INT2NUM( (*self)->getNunchukAcc( 0 ) ),
INT2NUM( (*self)->getNunchukAcc( 1 ) ),
INT2NUM( (*self)->getNunchukAcc( 2 ) ) );
}

VALUE WiiMote::wrapGetNunchukJoyStick( VALUE rbSelf )
{
WiiMotePtr *self; Data_Get_Struct( rbSelf, WiiMotePtr, self );
return rb_ary_new3( 2, INT2NUM( (*self)->getNunchukJoyStick( 0 ) ),
INT2NUM( (*self)->getNunchukJoyStick( 1 ) ));
}

VALUE WiiMote::wrapGetNunchukButtons( VALUE rbSelf )
{
WiiMotePtr *self; Data_Get_Struct( rbSelf, WiiMotePtr, self );
return INT2NUM( (*self)->getNunchukButtons() );
}

6 changes: 6 additions & 0 deletions ext/wiimote.hh
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@ public:
bool getRumble(void);
void setRumble( bool state ) throw(Error);
unsigned short int getButtons(void);
unsigned short int getNunchukButtons(void);
unsigned char getAcc( int id );
unsigned char getNunchukAcc( int id );
unsigned char getNunchukJoyStick( int id );
bool irValid( int i );
unsigned short int getIRX( int i );
unsigned short int getIRY( int i );
Expand All @@ -60,6 +63,9 @@ public:
static VALUE wrapSetRumble( VALUE rbSelf, VALUE rbState );
static VALUE wrapGetButtons( VALUE rbSelf );
static VALUE wrapGetAcc( VALUE rbSelf );
static VALUE wrapGetNunchukButtons( VALUE rbSelf );
static VALUE wrapGetNunchukAcc( VALUE rbSelf );
static VALUE wrapGetNunchukJoyStick( VALUE rbSelf );
static VALUE wrapGetIR( VALUE rbSelf );
static VALUE wrapGetMotionPlus( VALUE rbSelf );
static WiiMote *current;
Expand Down
12 changes: 12 additions & 0 deletions sample/nunchuk.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require "cwiid"

puts "Press 1+2 or sync..."

wm = WiiMote.new();

wm.rpt_mode=WiiMote::RPT_NUNCHUK | ::WiiMote::RPT_ACC | WiiMote::RPT_BTN;

loop do;
wm.get_state;
print "\rSTICK:#{wm.nunchuk_stick} BUTTONS:#{wm.nunchuk_buttons} ACC:#{wm.nunchuk_acc} ";
end

0 comments on commit fb00b80

Please sign in to comment.