/*
 *          __
 *         / /_______________  ____  ___
 *        / __/ ___/ ___/ __ \/ __ \/ _ \
 *       / /_(__  ) /__/ /_/ / /_/ /  __/
 *       \__/____/\___/\____/ .___/\___/
 *                         /_/
 *
 * michael stevens
 * 
 * test gameport driver
 * returns status of the registry
 *
 * info about the gameport can be found at
 *  http://www.ctv.es/pckits/tpjoystick.html
 * 
 *  tscope keys / bits / pins on the gameport (0x201)
 *      (bits are counted from 0)
 *  G1 -> bit4 -> pin2  (0=high)
 *  G2 -> bit5 -> pin7  (0=high)
 *  G3 -> bit6 -> pin10 (0=high)
 *  G4 -> bit7 -> pin14 (0=high)
 *
 *  pins 1,8,9,15: +5 V (power)
 *  pins 4,5,12: Ground
 */

#include <tscope.h>
#include <tscope/internal.h>

int main()
{
    // initialize the screen
    ts_scrsize(SIZE1);
    ts_defkey(KEY_SPACE);
    ts_textmode(0);

    // open gameport without tscope     
    if (ioperm(GAMEPORT, 1, 1))
        ts_fatal("error opening gameport");

    // some instructions
    ts_printf_centre(0, YMAX - 20, "testgport.c");
    ts_printf(-XMAX + 20, -YMAX + 170,
              "If a button is ON when pressed, use a G1-G4 definition");
    ts_printf(-XMAX + 20, -YMAX + 160,
              "If a button is OFF when pressed, use a IG1-IG4 definition");
    ts_printf(-XMAX + 20, -YMAX + 120,
              "If no box is connected, the values are undefined");

    ts_printf(-XMAX + 20, -YMAX + 80, "Read the source of this program");
    ts_printf(-XMAX + 20, -YMAX + 70, "for more information about");
    ts_printf(-XMAX + 20, -YMAX + 60, "the gameport.");

    ts_printf(-XMAX + 20, -YMAX + 20, "Press space to exit.");

    // poll port
    int resp, boxbin, boxhex;
    do {

        // pressing the spacebar stops the program
        resp = ts_respstatus();

        // read gameport status
        boxbin = 0;
        boxhex = inb(GAMEPORT);

        // convert hexadecimal value

        if ((boxhex & 16) >> 4)
            ts_fgcolor(RED);
        else
            ts_fgcolor(WHITE);
        ts_printf_centre(0, 40, "G1 (pin 02 / bit 4)");

        if ((boxhex & 32) >> 5)
            ts_fgcolor(RED);
        else
            ts_fgcolor(WHITE);
        ts_printf_centre(0, 30, "G2 (pin 07 / bit 5)");

        if ((boxhex & 64) >> 6)
            ts_fgcolor(RED);
        else
            ts_fgcolor(WHITE);
        ts_printf_centre(0, 20, "G3 (pin 10 / bit 6)");

        if ((boxhex & 128) >> 7)
            ts_fgcolor(RED);
        else
            ts_fgcolor(WHITE);
        ts_printf_centre(0, 10, "G4 (pin 14 / bit 6)");
    } while (resp != 1);

    // close port and exit
    ioperm(GAMEPORT, 1, 0);
    return 0;
}

END_OF_MAIN();


top
Persoonlijke pagina Universiteit GentTscope
Allegro | Cygwin | Gcc
© See license.html for copyright information