home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_07_08 / v7n8030a.txt < prev    next >
Text File  |  1989-06-28  |  3KB  |  131 lines

  1. /* Demonstration program:  serial poll of HP-IB device
  2.  * J. M. Anderson, F&M College, 1989
  3.  */
  4. #include <stdio.h>
  5. #include <strings.h>
  6. #include <unix.h>
  7. #include "HPIB.h"
  8.  
  9. #define PENPRESS (tstatus & 0x04)    /* status bit 2 indicates pen press */
  10. #define MENUPICK (tstatus & 0x80)    /* status bit 7 indicates menu pick */
  11.  
  12. void terminate()
  13. /* A function which waits for some event before allowing
  14.    the program to halt.  In this case, "Q" or "q" key pressed.
  15.    This allows one to read what's in the "console" window before it
  16.    is dismissed.
  17. */
  18. {
  19.     EventRecord myEvent;
  20.     int done;
  21.     int charCode;
  22.     
  23.     done = 0;
  24.     puts("Press 'Q' or 'q' to QUIT");
  25.     do
  26.     {
  27.         if (GetNextEvent(keyDownMask, &myEvent))
  28.         {
  29.             charCode = myEvent.message & charCodeMask;
  30.             if ((charCode == 'Q') || (charCode == 'q')) done = TRUE;
  31.         }
  32.     } while (!done);
  33. }
  34.  
  35. int process()
  36. {
  37.     /* Process the interrupt.
  38.      * This function is called when an interrupt
  39.      * is detected on the bus.
  40.      *
  41.      * For the purpose of this demo, SRQ must be asserted by the
  42.      * tablet for pen press or menu pick.
  43.      */
  44.      
  45.     int bstatus, tstatus;    /* bus status, tablet status */
  46.     int x, y, p, k;            /* pen position, menu key */
  47.     
  48.     /* See if interrupt was caused by an SRQ */
  49.     ieeewt("spoll\n");
  50.     ieeescnf("%d", &bstatus);
  51.     if (bstatus != 0)            /* all we want is an SRQ */
  52.     {
  53.         /* Poll the tablet to find its status;
  54.          * At this point, if there were more than one device on the bus,
  55.          * we would have a loop to poll all devices.  As it is, we have
  56.          * only one device (the graphics tablet), whose address is 06.
  57.          */
  58.         ieeewt("spoll 06\n");
  59.         ieeescnf("%d", &tstatus);
  60.         if PENPRESS
  61.         {
  62.             ieeewt("output 06;OD\n");
  63.             ieeewt("enter 06\n");
  64.             ieeescnf("%d,%d,%d",&x, &y, &p);
  65.                 /* beep high for stylus press on tablet */
  66.             ieeewt("output 06;BP 48,150,4\n");
  67.             printf("\ndigitized point: %d %d\n",x, y);
  68.             return(0);
  69.         }
  70.         
  71.         if MENUPICK
  72.         {
  73.             ieeewt("output 06;RS1\n");
  74.             ieeewt("enter 06\n");
  75.             ieeescnf("%d", &k);
  76.                 /* beep low for stylus press on menu square */
  77.             ieeewt("output 06;BP 8,150,4\n");
  78.             printf("Menu square %d touched.\n", k);
  79.             return(1);
  80.         }
  81.     }
  82. }
  83.         
  84. main()
  85. {
  86.     char response[256];
  87.     int done;            /* stop polling loop */
  88.  
  89.     /* Try to open driver to HPIB bus controller */
  90.     if (ieeeinit() != noErr)
  91.     {
  92.         printf("ieeeinit failed!\n");
  93.         exit();
  94.     }
  95.     
  96.     /* Clear the bus and devices
  97.      * These steps are possibly gratuitous; they help illustrate
  98.      * obtaining responses from the bus controller and the tablet 
  99.      */
  100.     ieeewt("clear\n");
  101.     
  102.     /* Read the MacDriver488 ID */
  103.     ieeewt("hello\n");
  104.     ieeerd(response);
  105.     printf("%s\n", response);
  106.     
  107.     /* initialize the tablet */
  108.     ieeewt("output 06;IN\n");
  109.     ieeewt("output 06;OI\n");
  110.     ieeewt("enter 06\n");
  111.     ieeerd(response);
  112.     printf("%s\n", response);
  113.  
  114.     /* enable interrupt checking and establish interrupt service routine */
  115.     /* set up for single-point digitizing */
  116.     ieeewt("output 06;DF;SG\n");
  117.     /* set up for SRQ on pen-press or menu-pick; 132 = 0x84: menu or pen */
  118.     ieeewt("output 06;IM ,132\n");
  119.     ieeewt("arm srq\n");
  120.     
  121.     done = 0;
  122.     /* now we wait for something to happen; i.e., we wait for an SRQ */
  123.     do 
  124.     {
  125.          if (ckactive()) done = process();
  126.     } while (!done);
  127.     
  128.     /* interrupt processing is done; let's quit */
  129.     terminate();
  130. }
  131.