Click here if you are stuck in someone else's frames.
Writing the Joystick Software Library

Okay, now that we've got an understanding on how we can read the joystick port let's look at writing functions that we can use in other programs (in other words, writing library functions).  Well, when dealing with the reading of the joystick pots, we can either read them one at a time, or all at once and cache their values in variables (the way our last program did).  If you are interested in writing functions that read a single port at a time, here's some sample code that allows you to do just that.

     /* This function returns the value on a single joystick pot. */

     #include <conio.h>    /* Needed for inp and outp functions. */

     #define JsA_Xaxis 0x01
     #define JsA_Yaxis 0x02
     #define JsB_Xaxis 0x04
     #define JsB_Yaxis 0x08

     unsigned int GetPotVal(char bitMask)
     {
         unsigned int retVal = 0;

         disable();                                  /* Disables interrupts */
         outp(0x201, 0);                             /* Discharge jostick caps */
         while(++retVal && (inp(0x201) & bitMask));  /* Count while capacitor is charging */ 
         enable();                                   /* Reenable interrupts */
         return retVal;
     }

To use that code in your programs just simply call it with the appropriate bit mask.  Some constants were also defined for the appropriate bit mask values.  For example:

     x = GetPotVal(JsA_Xaxis);

returns the joystick pot value for the X-axis on joystick A.  Another way is to return them all at once.  This technique is covered because when the code executes the command to discharge the joystick caps, their discharged all at once.  So it makes sense to go ahead and test all of them at once in a single count loop (like we did earlier).  Here's a function that does just that:

Previous Page | Main Page | Next page