Joysticks

From CCRMA Wiki
Jump to: navigation, search

Mapping Joysticks

This bit of code will spit out whatever codes are being transmitted from your controller to ChucK. It's also important to test letting buttons go up and down separately to see how your controller deals with that, if it's important to you. I used it to figure out how to interpret the input from my controller or joystick.

Hid js;
HidMsg msg;

if( !js.openJoystick( 0 ) ) me.exit();
<<< "Ready?", "" >>>;

while(true)
  {
    // wait for event
    js => now;
    while( js.recv( msg ) )
      {
	js => now;
	<<< "\n\nI LIKE TO","MOVE IT MOVE IT" >>>;
	while( js.recv(msg))
	  {
	    if( msg.isAxisMotion())
	      {
		<<< "axismotion" >>>;
		<<< "axis: ", msg.which, " position: ", msg.axisPosition >>>;
	      }
	    if( msg.isButtonDown())
	      {
		<<< "buttondown: ", msg.which >>>;
	      }
	    if( msg.isButtonUp())
	      {
		<<< "buttonup: ", msg.which >>>;
	      }
	    if(msg.isHatMotion() )
	      {
		<<< "hat: ", msg.idata >>>;
	      }
	  }
      }
  }

What to do with the input now?

The most effective method I found was to have the main loop waiting for joystick events. Once an event was triggered, trigger another event that some sporked function will take care of.

Resources Used