Turing Joysticks

From Compsci.ca Wiki

(Difference between revisions)
Jump to: navigation, search
(Tutorial explaining how to use joysticks in Turing by Nick.)
 
(One intermediate revision not shown)
Line 1: Line 1:
-
Hello welcome to my tutorial on using joysticks/joypads. I personally have a joypad with 2 analog sticks, 12 buttons, and a D-PAD/POV which has been tested. This tutorial will require two things: Firstly, you have atleast one joystick/joypad. Secondly, you grab my revised module at the bottom as the Turing default will not work. Now for some Q and A.
+
Hello and welcome to the tutorial on using joysticks/joypads. I personally have a joypad with 2 analog sticks, 12 buttons, and a D-PAD/POV which has been tested. This tutorial will require two things:  
 +
*You have at least one joystick/joypad
 +
*Secondly, you need the revised module at the bottom as the Turing default will not work
-
==FAQS==
 
-
q)So what do you plan to accomplish in this tutorial?
+
==FAQ==
 +
*So what do you plan to accomplish in this tutorial?
 +
**I'm planning to tackle the four main parts of the joystick module, breaking it down into parts, and ultimatly sharing this method of input with the world!
-
a)Well I'm planning to tackle the four main parts of the joystick module, breaking it down into parts, and ultimatly sharing this method of input with the world!
+
*So what's so great about joysticks/joypads?
 +
**Well the mouse has upto 3 buttons and a cursor, a keyboard has keys, however a joypad (depending on the model) will have upto 32 buttons, upto 3 analog sticks, and a D-PAD/POV! that equals a ton more input and functionallity
-
q)So what's so great about joysticks/joypads?
+
*What if I don't have a joypad/joystick?
 +
**Not to worry just follow along
-
a)Well the mouse has upto 3 buttons and a cursor, a keyboard has keys, however a joypad (depending on the model) will have upto 32 buttons, upto 3 analog sticks, and a D-PAD/POV! that equals a ton more input and functionallity.
 
-
 
-
q)What if I don't have a joypad/joystick?
 
-
 
-
a)Not to worry just follow along and post/PM me with the questions (I'm sometimes busy so results not will be immediate).
 
Well now to get started!
Well now to get started!
-
 
-
==Body==
 
-
 
-
 
-
The body will contain of four main parts, Setup, Button input, Analog input, and D-PAD/POV input. The tutorial will use an example program included at the bottom and will break down the code to show what exactly is happening (grab that now and the module too).
 
==Setup==
==Setup==
-
 
-
 
First things first, since the module is not pervasive it needs to be imported, also make sure the module is in the same folder as the program using it.
First things first, since the module is not pervasive it needs to be imported, also make sure the module is in the same folder as the program using it.
-
Code:
+
<pre>
import joystick
import joystick
-
 
+
</pre>
Now that that is done varibles need to be initalized just as though mouse input or keyboard input were being used.
Now that that is done varibles need to be initalized just as though mouse input or keyboard input were being used.
-
Code:
 
<pre>type joypad : % create a joypad type
<pre>type joypad : % create a joypad type
     record
     record
Line 51: Line 43:
Well now that is done time to move on. Default values need to be assigned as a failsafe for any joysticks not detected.
Well now that is done time to move on. Default values need to be assigned as a failsafe for any joysticks not detected.
-
Code:
 
<pre>/*set default joypad varibles*/
<pre>/*set default joypad varibles*/
for i : 1 .. upper (joy)
for i : 1 .. upper (joy)
Line 64: Line 55:
What just happened is rather simple. First a for loop was created to to cover all elements of joy, then another for loop for all 32 buttons to set the array to false false. Then the D-PAD/POV' was set to the default value of 66535. Lastly the joystick capibilities was called to find maximum capiblities of the joystick/joypad. Next joystick needs to be galled upon each loop run in the main program to gather information to what buttons are being pressed, etc... just as if it were a mouse or keyboard.
What just happened is rather simple. First a for loop was created to to cover all elements of joy, then another for loop for all 32 buttons to set the array to false false. Then the D-PAD/POV' was set to the default value of 66535. Lastly the joystick capibilities was called to find maximum capiblities of the joystick/joypad. Next joystick needs to be galled upon each loop run in the main program to gather information to what buttons are being pressed, etc... just as if it were a mouse or keyboard.
-
Code:
 
<pre>joystick.GetInfo (joystick[1/2], joy ([1/2]).button)
<pre>joystick.GetInfo (joystick[1/2], joy ([1/2]).button)
joystick.Read (joystick[1/2], joy ([1/2]).pos)</pre>
joystick.Read (joystick[1/2], joy ([1/2]).pos)</pre>
Line 72: Line 62:
==Using Buttons==
==Using Buttons==
-
 
-
 
This part of the tutorial is similar to a keyboard. A keyboard has an array of keys that return true if the key is pressed (info found with Input.KeyDown). A joystick/joypad has an array of buttons that return true if the button is pressed (info found with joystick.GetInfo). Therefore:
This part of the tutorial is similar to a keyboard. A keyboard has an array of keys that return true if the key is pressed (info found with Input.KeyDown). A joystick/joypad has an array of buttons that return true if the button is pressed (info found with joystick.GetInfo). Therefore:
-
Code:
 
<pre>if joy.button(4) then
<pre>if joy.button(4) then
     put "Button 4 is being pressed"
     put "Button 4 is being pressed"
Line 85: Line 72:
==Using Analog Sticks==
==Using Analog Sticks==
-
 
This part is not to hard however the analog does not return an X/Y pixel value, so a conversion is needed. First to get the current analog position.
This part is not to hard however the analog does not return an X/Y pixel value, so a conversion is needed. First to get the current analog position.
-
Code:
 
<pre>put "analog 1 - x - ", joy (1).pos.xpos
<pre>put "analog 1 - x - ", joy (1).pos.xpos
put "analog 1 - y - ", joy (1).pos.ypos</pre>
put "analog 1 - y - ", joy (1).pos.ypos</pre>
Line 95: Line 80:
Well these values need to be converted like so.
Well these values need to be converted like so.
-
 
-
Code:
 
<pre>put "analog 1 - x - ", round (joy (joystickNO + 1).pos.xpos / joy (joystickNO + 1).caps.maxX * maxx)
<pre>put "analog 1 - x - ", round (joy (joystickNO + 1).pos.xpos / joy (joystickNO + 1).caps.maxX * maxx)
Line 107: Line 90:
==Using D-PAD/POV==
==Using D-PAD/POV==
-
 
-
 
First off, reading D-PAD/POV data.
First off, reading D-PAD/POV data.
-
Code:
 
<pre>put joy ([1/2]).pos.POV</pre>
<pre>put joy ([1/2]).pos.POV</pre>
-
 
This to needs to be converted into usable code.
This to needs to be converted into usable code.
-
Code:
 
<pre> put "D-PAD (AKA POV) - " ..
<pre> put "D-PAD (AKA POV) - " ..
if joy (    [ 1 / 2]).pos.POV = 0 then
if joy (    [ 1 / 2]).pos.POV = 0 then
Line 141: Line 119:
==Conclusion==
==Conclusion==
-
 
-
 
Well that is all there is to it.
Well that is all there is to it.
==Challenges==
==Challenges==
-
 
-
 
Here are some challenges to try out.
Here are some challenges to try out.
-
1)make a circle that moves with the analog stick
+
# Make a circle that moves with the analog stick
-
 
+
# Move a sprite around the screen with the D-PAD/POV
-
2)move a sprite around the screen with the D-PAD/POV
+
# Make a simple Simon game with the buttons
-
 
+
# Try to make a basic game where you talk to people, walking around with D-PAD/POV talking to the character with the buttons
-
3)make a simple Simon game with the buttons
+
-
 
+
-
4)try to make a basic game where you talk to people, walking around with D-PAD/POV talking to the character with the buttons
+
==Refrences==
==Refrences==
-
 
+
*Joystick info http://compsci.ca/v3/viewtopic.php?t=6373
-
 
+
*Joystick info http://compsci.ca/v3/viewtopic.php?t=169
-
1) joystick info http://compsci.ca/v3/viewtopic.php?t=6373
+
-
 
+
-
2) joystick info http://compsci.ca/v3/viewtopic.php?t=169
+
-
 
+
-
And I am terribly sorry but there was one another post that really helped me understand joysticks myself but I forget who posted it and I can no longer find the post.
+
==Downloads==
==Downloads==
-
 
[http://compsci.ca/v3/download.php?id=5324>Joystick Module]
[http://compsci.ca/v3/download.php?id=5324>Joystick Module]
[http://compsci.ca/v3/download.php?id=5325>Demonstration]
[http://compsci.ca/v3/download.php?id=5325>Demonstration]

Latest revision as of 21:56, 12 October 2008

Hello and welcome to the tutorial on using joysticks/joypads. I personally have a joypad with 2 analog sticks, 12 buttons, and a D-PAD/POV which has been tested. This tutorial will require two things:

  • You have at least one joystick/joypad
  • Secondly, you need the revised module at the bottom as the Turing default will not work


Contents

FAQ

  • So what do you plan to accomplish in this tutorial?
    • I'm planning to tackle the four main parts of the joystick module, breaking it down into parts, and ultimatly sharing this method of input with the world!
  • So what's so great about joysticks/joypads?
    • Well the mouse has upto 3 buttons and a cursor, a keyboard has keys, however a joypad (depending on the model) will have upto 32 buttons, upto 3 analog sticks, and a D-PAD/POV! that equals a ton more input and functionallity
  • What if I don't have a joypad/joystick?
    • Not to worry just follow along


Well now to get started!

Setup

First things first, since the module is not pervasive it needs to be imported, also make sure the module is in the same folder as the program using it.

import joystick

Now that that is done varibles need to be initalized just as though mouse input or keyboard input were being used.

type joypad : % create a joypad type
    record
        button : array 1 .. 32 of boolean %create 32 instances of buttons
        pos : joystick.PosRecord /*used to find all joypad intake with
         the exception of buttons*/
        caps : joystick.CapsRecord %used to find the max on joypad analog
    end record

var joy : array 1 .. 2 of joypad %create an array of joypad


What just happened? Well now to break that down.

First an instance of the joypad type containing a set of varibles was created. The first varible is an array of 32 boolean the number can be any number 1<n<32, basically it's all the buttons being used (32buttons were used to show the maximum potential). The next varible is actually taking the type of another record. The record can be found in the joystick module it is basically bringing all input except the button states. Next is another type found in the joystick module this time bringing back the maximum potential of the joystick/joypad. Then a joy varible to use the varibles was created an array can be used for multiple joysticks/joypads.

Well now that is done time to move on. Default values need to be assigned as a failsafe for any joysticks not detected.

/*set default joypad varibles*/
for i : 1 .. upper (joy)
    for ii : 1 .. upper (joy (i).button)
        joy (i).button (ii) := false
    end for
    joy (i).pos.POV := 66535
    joystick.Capabilities (i - 1, joy (i).caps)
end for


What just happened is rather simple. First a for loop was created to to cover all elements of joy, then another for loop for all 32 buttons to set the array to false false. Then the D-PAD/POV' was set to the default value of 66535. Lastly the joystick capibilities was called to find maximum capiblities of the joystick/joypad. Next joystick needs to be galled upon each loop run in the main program to gather information to what buttons are being pressed, etc... just as if it were a mouse or keyboard.

joystick.GetInfo (joystick[1/2], joy ([1/2]).button)
joystick.Read (joystick[1/2], joy ([1/2]).pos)


The GetInfo procedure will return upto 32 button values. The Read procedure will return with Analog and D-PAD/POV information.

Using Buttons

This part of the tutorial is similar to a keyboard. A keyboard has an array of keys that return true if the key is pressed (info found with Input.KeyDown). A joystick/joypad has an array of buttons that return true if the button is pressed (info found with joystick.GetInfo). Therefore:

if joy.button(4) then
    put "Button 4 is being pressed"
end if


Hopefully that is understandable. And that is all for buttons.

Using Analog Sticks

This part is not to hard however the analog does not return an X/Y pixel value, so a conversion is needed. First to get the current analog position.

put "analog 1 - x - ", joy (1).pos.xpos
put "analog 1 - y - ", joy (1).pos.ypos


Well these values need to be converted like so.

put "analog 1 - x - ", round (joy (joystickNO + 1).pos.xpos / joy (joystickNO + 1).caps.maxX * maxx)
put "analog 1 - y - ", round (joy (joystickNO + 1).pos.ypos / joy (joystickNO + 1).caps.maxY * maxy)


This must be done everytime but it shouldn't be to hard to create a function to do this.

The axis use a name for each x and y direction: analog 1 x - xpos, analog 1 y - ypos, analog 2 x - zpos, analog 2 y - rpos, analog 3 x - upos, analog 3 y - vpos.

Using D-PAD/POV

First off, reading D-PAD/POV data.

put joy ([1/2]).pos.POV

This to needs to be converted into usable code.

 put "D-PAD (AKA POV) - " ..
if joy (    [ 1 / 2]).pos.POV = 0 then
    put "up"
elsif joy (    [ 1 / 2]).pos.POV = 4500 then
    put "up right"
elsif joy (    [ 1 / 2 ]).pos.POV = 9000 then
    put "right"
elsif joy (    [ 1 / 2 ]).pos.POV = 13500 then
    put "down right"
elsif joy (    [ 1 / 2 ]).pos.POV = 18000 then
    put "down"
elsif joy (    [ 1 / 2  ]).pos.POV = 22500 then
    put "down left"
elsif joy (    [ 1 / 2 ]).pos.POV = 27000 then
    put "left"
elsif joy (    [ 1 / 2  ]).pos.POV = 31500 then
    put "up left"
else
    put "none"
end if


Conclusion

Well that is all there is to it.

Challenges

Here are some challenges to try out.

  1. Make a circle that moves with the analog stick
  2. Move a sprite around the screen with the D-PAD/POV
  3. Make a simple Simon game with the buttons
  4. Try to make a basic game where you talk to people, walking around with D-PAD/POV talking to the character with the buttons

Refrences

Downloads

>Joystick Module

>Demonstration

Personal tools