home *** CD-ROM | disk | FTP | other *** search
- JOYSTICK.ZIP Wed 01-08-1992
-
-
- C Game Adapter Library
-
- Rev 1.2
-
- by
-
- Ed Torgerson
-
- CIS 70313,456
- GEnie E.TORGERSON1.
- BIX etorgerson
-
-
- A mixed language C and Assembler library of routines for
- accessing the IBM PC game adapter at port 201h.
- Object-Ready, Event-ready design featuring inter-module
- data hiding. (should be easy to change it to a C++ class)
-
- The low level module is mostly asm statements, but the
- provided small model library module should allow anyone
- with TURBOC 2.0, TURBO C++, or Borland C++ to use it,
- with or without an assembler. If you need a straight
- C replacement for my low level routines, look at JOY.ARC
- in the Borland C Forum in BPROGB on Compuserve.
-
- Pseudocode/Structured English modules are also included
- to aid in adapting to other languages.
-
- /*-------------------------------------------------------------------*/
-
- Files:
- joystklo.c low level generic game adapter port routines
- joystkmd.c joystick-specific middle level functions
- built on top of joystklo.c
- joystick.h header file for joystklo.c, joystkmd.c
- and joystk_m.lib
- joystk_s.lib an already compiled small model library
- consisting of joystklo.c and joystkmd.c
- testlow.c a small demonstration of joystklo.c
- testmid.c a small demonstration of joystkmd.c
- joystklo.se pseudocode/structured english
- joystkmd.se pseudocode/structured english
-
- /*-------------------------------------------------------------------*/
-
- JOYSTKLO.C provides generic low level game-adapter port functions to:
-
- 1. detect the presence of a game adapter
- 2. verify which axes are functional
- 3. read current button status
- 4. read changed button status
- 5. read current resistive axis position
- 6. allow change of active axes within this system
- as long as they have been found functional.
-
-
- JOYSTKMD.C provides middle level joystick-specific functions to:
-
- 1. detect and initialize a joystick coordinate system.
- 2. read changed joystick position
- 3. provide a rough translation from the top-left origin
- joystick coordinate system to a balanced 0,0
- centered cartesian system
- 4. get/reset joystick center coords
- 5. get/reset joystick minimum coords
- 6. get/reset joystick maximum coords
- 7. get/set an ignore threshold (tolerance) for
- reading changed joystick positions
-
- /*-------------------------------------------------------------------*/
-
- Background:
-
- What I wanted was a decent usable library of functions
- that did a little more than just read the raw port input.
- I don't really use the adapter port that much, but I like
- to cover my bases when it comes to my library. I've tried
- to make it fairly simple, but fairly versatile.
-
- The low level module is not joystick specific, so you
- should be able to use it with any other periferal that uses
- the game adapter. It's coded with assembly statements to make
- the loops a little faster which should be more useful inside
- timer interrupt 1Ch. I didn't really try too hard to optimize
- it, so you should be able to throw it into pure assembly and
- knock a couple of cycles off of it fairly easily.
-
- I did try to make the middle level module flexible enough
- to use a joystick as a practical pointing device in either a
- polled or an event-driven architecture. You may want to
- change the ignore threshold system that I've set up to
- compensate for the non-linear scaling of the joystick. You
- may also want to take a look at my cartesian coordinate
- system. I've set it up for a center-oriented scale. Although
- I can't imagine why you might want a different orientation,
- I don't claim to be omniscient, so the translation is performed
- by a seperate function. If your preference differs from mine,
- you can easily just plug in your own function.
-
- I've done limited testing on an old IBM XT 5160 model 267
- with 11/08/82 BIOS as well as my PS/2 like 386 AT and haven't
- found anything, yet..........................................
-
- If you find this library useful, donate to your favorite cause.
-
- I just want to thank Gary Blaine for putting JOY.ARC out here,
- my function for reading position is based on his.
-
- These modules are hereby released to the public domain
- by Ed Torgerson : CIS 70313,456, GEnie E.TORGERSON1.
-
- Any constructive correspondence is welcome.
-
- /*-------------------------------------------------------------------*/
-
- port 201h :
-
- bit JOYSTICK PADDLE
- --- -------- ------
- 7 B button 2 D button
- 6 B button 1 C button
- 5 A button 2 B button
- 4 A button 1 A button
- 3 B y-axis D Coordinate
- 2 B x-axis C Coordinate
- 1 A y-axis B Coordinate
- 0 A x-axis A Coordinate
-
- /*-------------------------------------------------------------------*/
- /*--------------------- Low Level Functions -------------------------*/
- /*-------------------------------------------------------------------*/
-
- JstDetect :
-
- purpose: Detect active game port axes.
-
- syntax: int JstDetect ( void ) ;
-
- remarks: performs check by reading port 201h, triggering the
- one-shot timers and immediately checking for bits
- that have changed. It also sets up valid and
- active axes.
-
- returns: bit mask of valid axes in original port format.
- values can be easily checked by ANDing this value
- with the defined macros:
-
- JOYSTICK_A, JOYSTICK_B
- JAX_AXIS, JAY_AXIS, JBX_AXIS, JBY_AXIS
-
- /*-------------------------------------------------------------------*/
-
- JstGetButton :
-
- purpose: returns the current button value.
-
- syntax: int JstGetButton ( void ) ;
-
- remarks: This is not just a straight read of port 201h.
- It is intended to provide quick determination
- of the current status using macros.
-
- returns: bit mask of current button state.
- values can be easily checked by ANDing this
- value with the defined macros:
-
- JA1_BUTTON_DOWN, JA1_BUTTON_UP,
- JA2_BUTTON_DOWN, JA2_BUTTON_UP
- JB1_BUTTON_DOWN, JB1_BUTTON_UP,
- JB2_BUTTON_DOWN, JB2_BUTTON_UP
- J_ANY_BUTTON_DOWN
- J_A_BUTTON_DOWN, J_B_BUTTON_DOWN
- J_BUTTON_1_DOWN, J_BUTTON_2_DOWN
-
- bit STATE JOYSTICK PADDLE
- --- ----- -------- ------
- 7 down B button 2 D button
- 6 down B button 1 C button
- 5 down A button 2 B button
- 4 down A button 1 A button
- 3 up B button 2 D button
- 2 up B button 1 C button
- 1 up A button 2 B button
- 0 up A button 1 A button
-
- /*-------------------------------------------------------------------*/
-
- JstGetChangedButton
-
- purpose: returns any change in button state since
- the last call to either JstGetChangedButton
- or JstGetButton.
-
- syntax: int JstGetChangedButton( void ) ;
-
- remarks: see JstGetButton
-
- returns: see JstGetButton
-
- /*-------------------------------------------------------------------*/
-
- JstGetPosition :
-
- purpose: returns the position of the given axis.
-
- syntax: int JstGetPosition ( int axis ) ;
-
- remarks: 'axis' is a bit mask of a single active axis
- in original port format and may be specified
- with one of the following defined macros:
-
- JAX_AXIS, JAY_AXIS, JBX_AXIS, JBY_AXIS
-
- returns: the position of the given axis in the original
- coordinate system. The values for a 100k
- joystick should be between 4 and 400, but may
- exceed these limits, especially with the
- adjustment sliders on most joysticks.
-
- /*-------------------------------------------------------------------*/
-
- JstGetValidAxes
-
- purpose: returns a bit mask of the valid axes found by
- JstDetect() in original port format.
-
- syntax: int JstGetValidAxes ( void ) ;
-
- remarks: intended to be use before an attempt to set the
- current active axis mask with JstSetActiveAxes().
-
- returns: bit mask of valid axes in original port format.
- Values can be easily checked by ANDing this value
- with the defined macros:
-
- JOYSTICK_A, JOYSTICK_B
- JAX_AXIS, JAY_AXIS, JBX_AXIS, JBY_AXIS
-
- /*-------------------------------------------------------------------*/
-
- JstGetActiveAxes
-
- purpose: returns a bit mask of the currently active axes
- used in error checking by JstGetPosition() in
- original port format.
-
- syntax: int JstGetActiveAxes ( void ) ;
-
- remarks: This mask is used to screen out possible calls
- to time axes that may be valid, but are not
- in use, thus saving precious cycles.
-
- returns: bit mask of currently active axes in original port
- format. Values can be easily checked by ANDing this
- value with the defined macros:
-
- JOYSTICK_A, JOYSTICK_B
- JAX_AXIS, JAY_AXIS, JBX_AXIS, JBY_AXIS
-
-
- /*-------------------------------------------------------------------*/
-
- JstSetActiveAxes
-
- purpose: sets valid axes for read operations by
- JstGetPosition().
-
- syntax: int JstSetActiveAxes ( unsigned mask ) ;
-
- remarks: The value is written only if the entire given
- mask is valid. Validity may be determined
- beforehand by getting a mask of valid axes
- with JstGetValidAxes().
-
- returns: 1 on success, 0 if mask is invalid.
-
- /*-------------------------------------------------------------------*/
- /*------------------- Middle Level Functions ------------------------*/
- /*-------------------------------------------------------------------*/
-
- JstInitialize
-
- purpose: to detect and initialize joysticks
-
- syntax: int JstInitialize ( void (*NotifyUserFunc)(int) ) ;
-
- remarks: This function first calls JstDetect to determine
- if a joystick(s) is installed and set up valid and
- active joystick axis pairs. It then calls the
- given user notification function NotifyUserFunc()
- to prompt the user to enter the center, minimum,
- and maximum joystick coordinate pairs. At any
- point during the user data input step, the user
- may quit the process by pressing escape. This
- function then calculates the starting ignore
- threshold (tolerance) and quasi linear scale for
- calls to JstGetChangedPosition().
-
- NotifyUserFunc is a pointer to a user provided
- function which prompts the user for input during
- the intialization process. It takes one parameter,
- an integer to determine what message to display.
- The function's format should be as follows...
-
- void NotifyUserFunc( int msg )
- BEGIN
- switch (msg)
- case -2: "warning: joystick installation incomplete"
- case -1: "joystick installation terminated by user"
- case 0: "no joystick detected"
- case 1: "press escape to quit"
- case 2: "center joystick and press button"
- case 3: "move joystick to top-left and press button 1"
- case 4: "move joystick to lower-right and press button 2"
- case -3: return
- default : return
- END
-
- returns: 1 on success, 0 on failure
-
- /*-------------------------------------------------------------------*/
-
- JstGetChangedPosition
-
- purpose: return position of given joystick if it has
- changed past the tolerance scale (ignore threshold).
-
- syntax: int JstGetChangedPosition ( int stick,
- struct JoystickPosition *stickpos ) ;
-
- remarks: the tolerance scale is set to decrease from
- the maximum value as set by JstSetTolerance()
- and JstInitialize() at the bottom-right corner
- of the joystick through three asymetric steps
- set towards the upper-left corner. This scale
- is intended to produces a more linear action
- across the span of the joystick with a minimum
- of calculation (time).
-
- The value of 'stick' may be easily set with the
- defined macros JOYSTICK_A and JOYSTICK_B. The
- joystick position (if changed) is returned in
- the JoystickPosition structure 'stickpos'.
- The values in stickpos are undefined if the
- position has not changed.
-
- returns: 1 if 'stickpos' contains a new position.
- 0 if no change.
-
- /*-------------------------------------------------------------------*/
-
- JstGetTolerance
-
- purpose: return maxmimum value on the tolerance scale
- (ignore changed position threshold).
-
- syntax: int JstGetTolerance ( void ) ;
-
- remarks: this value is originally set as the combined
- maximum joystick coordinates divided by 64 which
- should generally yield a value around 10.
-
- returns: positive integer
-
- /*-------------------------------------------------------------------*/
-
- JstSetTolerance
-
- purpose: sets the tolerance value (ignore changed position
- threshold) for the top of the scale at the
- joystick's bottom-right for JstGetChangedPosition()
-
- syntax: void JstSetTolerance ( int tolerance ) ;
-
- remarks: this value is originally set as the combined
- maximum joystick coordinates divided by 64 which
- should generally yield a value around 10.
-
- returns: void
-
- /*-------------------------------------------------------------------*/
-
- JstSetCenter
-
- purpose: grabs the current coordinates of the given joystick
- and set the center of the cartesian system to that
- point. It also resets the tolerance scale.
-
- syntax: int JstSetCenter ( int stick ) ;
-
- remarks: The value of 'stick' may be easily set with the
- defined macros JOYSTICK_A and JOYSTICK_B.
-
- returns: 1 on success, 0 if invalid joystick.
-
- /*-------------------------------------------------------------------*/
-
- JstSetMin
-
- purpose: grabs the current coordinates of the given joystick
- and stores it as the minimum joystick position.
- It also resets the tolerance scale.
-
- syntax: int JstSetMin ( int stick ) ;
-
- remarks: The value of 'stick' may be easily set with the
- defined macros JOYSTICK_A and JOYSTICK_B.
-
- returns: 1 on success, 0 if invalid joystick.
-
- /*-------------------------------------------------------------------*/
-
- JstSetMax
-
- purpose: grabs the current coordinates of the given joystick
- and stores it as the maximum joystick position.
- It also resets the tolerance scale.
-
- syntax: int JstSetMax ( int stick ) ;
-
- remarks: The value of 'stick' may be easily set with the
- defined macros JOYSTICK_A and JOYSTICK_B.
-
- returns: 1 on success, 0 if invalid joystick.
-
- /*-------------------------------------------------------------------*/
-
- JstGetCenter
-
- purpose: returns the current coordinates of the set
- center position of the given joystick.
-
- syntax: void JstGetCenter( int stick,
- struct JoystickPosition *jpos ) ;
-
- remarks: The value of 'stick' may be easily set with the
- defined macros JOYSTICK_A and JOYSTICK_B.
-
- returns: void
-
- /*-------------------------------------------------------------------*/
-
- JstGetMin
-
- purpose: returns the current coordinates of the set
- minimum position of the given joystick.
-
- syntax: void JstGetMin ( int stick,
- struct JoystickPosition *jpos ) ;
-
- remarks: The value of 'stick' may be easily set with the
- defined macros JOYSTICK_A and JOYSTICK_B.
-
- returns: void
-
- /*-------------------------------------------------------------------*/
-
- JstGetMax
-
- purpose: returns the current coordinates of the set
- center position of the given joystick.
-
- syntax: void JstGetMax ( int stick,
- struct JoystickPosition *jpos ) ;
-
- remarks: The value of 'stick' may be easily set with the
- defined macros JOYSTICK_A and JOYSTICK_B.
-
- returns: void
-
- /*-------------------------------------------------------------------*/
-
- JstXlatToCart
-
- purpose: translate the given coordinates from real to
- cartesian coordinates.
-
- syntax: void JstXlatToCart ( int stick,
- struct JoystickPosition *stickpos ) ;
-
- remarks: 'stickpos' contains the real coordinates to be
- translated and returns cartesian values based on
- the set center of the joystick 'stick'.
-
- returns: void
-
- /*-------------------------------------------------------------------*/
-
- /*-- end joystick.doc */
-
-