Listing 3 (ShowTch.c)

/* ---------------------ShowTch.c-------------------*
 *                                                  *
 * Module:     ShowTch.c                            *
 * Purpose:    To provide a demonstration about how *
 *             to use function GetTouch().          *
 * Author:     W. Harvey Gray                       *
 * Compiler:   Microsoft 5.0                        *
 *                                                  *
 * Functions:  (extern)                             *
 *             main                                 *
 *                                                  *
 * Variables:  There are no external variables used *
 *             or defined by this module.           *
 *                                                  *
 * Copyright 1992, W. Harvey Gray. May be used      *
 *   freely, if authorship and publication are      *
 *   acknowledged.                                  *
 *                                                  *
 *--------------------------------------------------*/

#include <bios.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#include "dvrfunc.h"
#include "GetTouch.h"

/*--------------------------------------------------*
 *                static prototypes                 *
 *--------------------------------------------------*/

static void print_touch( void );
static void touch_loop( void );

/*------------------------------------------------*
 *     extern function main()                     *
 *------------------------------------------------*/

int main( int argc, char **argv ) {

/*------------------------------------------------*
 * Purpose:                                       *
 *                                                *
 *   Demonstrates a simple routine that reads a   *
 * complete touchscreen touch and prints its      *
 * statistics.                                    *
 *                                                *
 *------------------------------------------------*/

   struct infodata *pinfo;
   
   puts( "ShowTch V1.0" );
   puts( "A test program for function GetTouch." );
   
   if ( ! finddriver() ) {  /* Check for ELODEV. */
   
      puts( "\nELODEV not installed." );
      exit( 1 );
   }
   /*--------------------------------------------*
    * Check for correct device driver version.   *
    *--------------------------------------------*/
   
   pinfo : malloc( sizeof( struct infodata ) );
   driverinfo( pinfo );
   
   if ( ( pinfo->driverversionmajor * 10 +
         pinfo->driverversionminor ) < 14 ) {
   
      puts( "\nELODEV not >= version 1.4" );
      exit( 2 );
   }
   free( pinfo );
   
   /*--------------------------------------------*
    * Startup the touchscreen device driver.     *
    *--------------------------------------------*/
   
   setmode( BUFFERED | UNTOUCH | STREAM );
   opentouch();
   enabletouch();
   initdelay();
   
   /*--------------------------------------------*
    *   Set the button radius to be 4% of the    *
    * diagonal dimension of the touchscreen.     *
    *--------------------------------------------*/
   
   InitializeTouch();
   SetButtonRadius( 4096/25 );
   
   /*--------------------------------------------*
    *   Continue reading touches until the key-  *
    * board is hit.                              *
    *--------------------------------------------*/
   
   while ( ! _bios keybrd( _KEYBRD_READY ) )
      touch_loop();
                     /* Flush keyboard buffer. */
   
   while ( _bios_keybrd( _KEYBRD_READY ) )
      getch();
   
   closetouch();
   exit( 0 );
}

/*-------------------------------------------------*
 *     static function print_touch()               *
 *-------------------------------------------------*/

static void print_touch( void ) {

/*-------------------------------------------------*
 * Purpose:                                        *
 *                                                 *
 *   Print the touch statistics.                   *
 *                                                 *
 *-------------------------------------------------*/

   struct touchit *pti;
   
   pti = TouchInfo();
   printf( "\nTouch pts = %d;", pti->n );
   printf( "\n (X,Y)cen = (");
   printf( "%5d,", pti->xcen );
   printf( "%5d);", pti->ycen );
   printf( "\n Push button = %s.",
      ( (pti->button) ? "True" : "False" ) );
   
   return;
}

/*-------------------------------------------------*
 *     static function touch_loop()                *
 *-------------------------------------------------*/

static void touch_loop( void ) {

/*-------------------------------------------------*
 * Purpose:                                        *
 *                                                 *
 *   Read an entire touch and print its statistics *
 * when an untouch point is received.              *
 *                                                 *
 *-------------------------------------------------*/
   
   boolean button;
   boolean touched;
   
   struct touchxy *ptouch;
   
   /*----------------------------------------------*
    *   Service one complete touch.                *
    *----------------------------------------------*/
   
   touched = FALSE;
   do {
   
      if (( ptouch = GetTouch( &button )) !=
             (struct touchxy *) NULL ) {
         
         touched = TRUE;  /* Touch point recv'd */
         
         if ( ptouch->ut ) {   /* Is a untouch? */
         
            print_touch();
            printf(" (Press any key to stop.)" );
            break;     /* Finished with touch. */
         }
      }
   }
   while ( touched );
   
   return;
}
/* End of file. */