home *** CD-ROM | disk | FTP | other *** search
/ Virtual Reality Homebrewer's Handbook / vr.iso / vr386 / gloveptr.c < prev    next >
C/C++ Source or Header  |  1996-03-19  |  5KB  |  164 lines

  1. /* Glove pointer device */
  2.  
  3. /* Written by Dave Stampe, August 1992 */
  4.  
  5. // Pointers are an attempt to integrate device support across
  6. // REND386.  Why isn't anyone using them?
  7.  
  8. // Ported to VR-386 9/1/94 by Dave STampe
  9. // Basically a "wrapper" around SGSPPT.C and the PCDEVICE library
  10.  
  11. /*
  12.  This code is part of the VR-386 project, created by Dave Stampe.
  13.  VR-386 is a desendent of REND386, created by Dave Stampe and
  14.  Bernie Roehl.  Almost all the code has been rewritten by Dave
  15.  Stampre for VR-386.
  16.  
  17.  Copyright (c) 1994 by Dave Stampe:
  18.  May be freely used to write software for release into the public domain
  19.  or for educational use; all commercial endeavours MUST contact Dave Stampe
  20.  (dstampe@psych.toronto.edu) for permission to incorporate any part of
  21.  this software or source code into their products!  Usually there is no
  22.  charge for under 50-100 items for low-cost or shareware products, and terms
  23.  are reasonable.  Any royalties are used for development, so equipment is
  24.  often acceptable payment.
  25.  
  26.  ATTRIBUTION:  If you use any part of this source code or the libraries
  27.  in your projects, you must give attribution to VR-386 and Dave Stampe,
  28.  and any other authors in your documentation, source code, and at startup
  29.  of your program.  Let's keep the freeware ball rolling!
  30.  
  31.  DEVELOPMENT: VR-386 is a effort to develop the process started by
  32.  REND386, improving programmer access by rewriting the code and supplying
  33.  a standard API.  If you write improvements, add new functions rather
  34.  than rewriting current functions.  This will make it possible to
  35.  include you improved code in the next API release.  YOU can help advance
  36.  VR-386.  Comments on the API are welcome.
  37.  
  38.  CONTACT: dstampe@psych.toronto.edu
  39. */
  40.  
  41. #include <stdio.h>
  42. #include <stdlib.h>    /* atexit() */
  43. #include <dos.h>
  44. #include <conio.h>     /* kbhit() */
  45.  
  46. #include "vrconst.h"
  47. #include "pointint.h"
  48. #include "vr_api.h"
  49. #include "pcdevice.h"
  50. #include "intmath.h"
  51.  
  52. /************** INTERNAL GLOVE SETUP ***********/
  53.  
  54. PDRIVER *glove_device;
  55.  
  56.  
  57. /***************** GLOVE POINTER DRIVER *************/
  58.  
  59. static pconfig glove_pconfig = {
  60.     5*65536L, 5*65536L, -15*65536L,     /* position res: mm/tick in <16.16>  */
  61.     120, 120, 120, -120, -120, -120,     /* xyz ranges:                */
  62.     0, -30*65536L, 0,
  63.     0, 0, 0, 120, 11, 120,             /* some rotation (swing emulation)       */
  64.     320, 200,                 /* mouse emulation limits (writable) */
  65.     P_HASX | P_HASY | P_HASZ | P_HASRY | P_HASSCR |
  66.         P_HASGEST | P_HASFLEX | P_HASKEYS,         /* databits  */
  67.     P_CENTER | P_SCREEN, 0xFF, 11,                 /* modes, nullkey, flexnum           */
  68.     2, 50, 20,                 /* delay, idelay, reads/sec          */
  69.     P_IS3DG | P_IS3D | P_IS2D,         /* uses  */
  70.     "Default Powerglove Driver"
  71. };
  72.  
  73. static glove_data glove_new; /* glove data structure */
  74.  
  75. #define DEBOUNCE_TIME 8
  76.  
  77. static setup_glove(void) /* glove initialize (incl. int. system) */
  78. {
  79.     glove_init(1);
  80.     save_screen();
  81.     popmsg("Waiting for glove...");
  82.     tdelay(1000);
  83.  
  84.     while (!glove_ready())
  85.       if (kbhit())
  86.         {
  87.           getkey();
  88.           return -3;
  89.         }
  90.     restore_screen();
  91.     glove_read(&glove_new);
  92.     return 0;
  93. }
  94.  
  95.  
  96. static fbend[4] = { 0, 40, 90, 127 }; /* finger flexions      */
  97. static tbbend[4] = { 0, 25, 72, 127 };
  98. static ttbend[4] = { 0, 35, 80, 127 };
  99.  
  100. pconfig *glove_driver(int op, POINTER *p, int mode)
  101. {
  102.     int ft, fi, fm, fp;
  103.  
  104.     switch(op)
  105.     {
  106.     case DRIVER_CMD:
  107.     case DRIVER_RESET:
  108.         break;
  109.  
  110.     case DRIVER_INIT:
  111.         setup_glove();
  112.         break;
  113.  
  114.     case DRIVER_READ:/* pointer (2DP) read */
  115.         if (mode == P_POINTER)
  116.         {
  117.             if (glove_ready() == 0) return NULL;
  118.             glove_read(&glove_new);
  119.             p->x = glove_new.x;
  120.             p->y = glove_new.y;
  121.             p->z = glove_new.z;
  122.             p->rx = 0;
  123.             p->ry = ((long) glove_new.rot) << 16;
  124.             p->rz = 0;
  125.             p->buttons = 0;
  126.             p->keys = glove_new.keys;
  127.  
  128.             ft = (glove_new.fingers >> 6) & 3; /* finger joint angles */
  129.             fi = (glove_new.fingers >> 4) & 3;
  130.             fm = (glove_new.fingers >> 2) & 3;
  131.             fp = (glove_new.fingers >> 0) & 3;
  132.  
  133.             p->flex[0] = tbbend[ft];
  134.             p->flex[1] = ttbend[ft];
  135.             p->flex[2] = p->flex[3] = fbend[fi];
  136.             p->flex[4] = p->flex[5] = fbend[fm];
  137.             p->flex[6] = p->flex[7] = fbend[fp];
  138.             p->flex[8] = p->flex[9] = fbend[fp];
  139.  
  140.             p->gesture = (gesture_time>DEBOUNCE_TIME)?gesture_type:G_UNKNOWN ;
  141.         }
  142.         else if (mode == P_SCREEN) /* mouse read (640x480) */
  143.         {
  144.             glove_read(&glove_new);
  145.             p->x = scale_16(((long) glove_pconfig.maxsx) << 8, 127, (long)glove_new.x);
  146.             p->y = scale_16(((long) glove_pconfig.maxsy) << 8, 127, -(long)glove_new.y);
  147.             if (p->x < 0) p->x = 0;
  148.             if (p->y < 0) p->y = 0;
  149.             if (p->x > glove_pconfig.maxsx) p->x = glove_pconfig.maxsx;
  150.             if (p->y > glove_pconfig.maxsy) p->y = glove_pconfig.maxsy;
  151.             p->gesture = (gesture_time > DEBOUNCE_TIME) ? gesture_type : G_UNKNOWN;
  152.             p->buttons = (p->gesture == G_FIST) ? 1 : 0;
  153.         }
  154.         break;
  155.  
  156.     case DRIVER_CHECK:
  157.         break;
  158.     case DRIVER_QUIT:
  159.         break;
  160.     }
  161.     return &glove_pconfig;
  162. }
  163.  
  164.