/*
 * main.c -- test program for libsball, opens sball port and displays sball status
 *           as text in real-time.  Does not do anything fancy yet.
 * 
 *   Copyright 1997 John E. Stone (j.stone@acm.org)
 *
 *   $Id: main.c,v 1.4 2001/05/17 20:21:59 johns Exp $
 */

#include <stdio.h>
#include <stdlib.h>
#include "sball.h"

int main(int argc, char **argv) {
  char * sballname; 
  SBallHandle sball;
  int nullsize;
  int done = 0;

#if defined(WIN32) || defined(_MSC_VER)
  printf("Spaceball Win32 test program, by John Stone (j.stone@acm.org)\n");
#else
  printf("Spaceball Unix test program, by John Stone (j.stone@acm.org)\n");
#endif

  if (argc != 2) {
    printf("Usage error, no Spaceball port specified!\n");
    printf("  Usage: testsball portname\n");
    printf("  Possible port names are:\n");

#if defined(WIN32) || defined(_MSC_VER)
    printf("    1 2 3 4 5, which correspond to com1, com2, com3 etc.\n");
#else
    printf("    /dev/ttya, /dev/ttyb, /dev/ttyc, /dev/ttyd,\n");
    printf("    /dev/term/a, /dev/term/b, /dev/cua, /dev/cub\n"); 
#endif

    return -1; /* same as a call to exit() */
  } 

  sballname = argv[1];

  printf("Opening Spaceball: using port %s for I/O\n", sballname);

  if ((sball = sball_open(sballname)) == NULL) {
    printf("Could not open Spaceball comm port!\n");
    return -1; /* same as a call to exit() */
  } 
  else {
    printf("Sucessfully opened Spaceball port.\n");
    printf("Monitoring Spaceball, press button 1 on Spaceball, or ctrl-c to exit.\n\n"); 
  }

  nullsize = 65;
  printf("Setting default null-region to 65 on all 6 axes...\n");
  sball_set_nullregion(sball, nullsize, nullsize, nullsize, 
                          nullsize, nullsize, nullsize);

  printf("Buttons -- 1) Quit\n");
  printf("           2) Software Init \n"); 
  printf("           3) Disable Null Region\n"); 
  printf("           4) Enable Null Region\n"); 

  while (!done) {
    int tx, ty, tz, rx, ry, rz, buttons;

    if (sball_getstatus(sball, &tx, &ty, &tz, &rx, &ry, &rz, &buttons)) {
      done = buttons & SBALL_BUTTON_1;  

      if (buttons & SBALL_BUTTON_2) {
        printf("\n doing soft sball init.\n");
        sball_init(sball);
      }
      if (buttons & SBALL_BUTTON_3) {
        printf("\nSetting null-region to 0 on all 6 axes...\n");
        nullsize = 0;
        sball_set_nullregion(sball, nullsize, nullsize, nullsize, 
                                nullsize, nullsize, nullsize);
      }
      if (buttons & SBALL_BUTTON_4) {
        printf("\nSetting null-region to 65 on all 6 axes...\n");
        nullsize = 65;
        sball_set_nullregion(sball, nullsize, nullsize, nullsize, 
                                nullsize, nullsize, nullsize);
      }

      if (buttons & SBALL_BUTTON_REZERO) {
        printf("\nSpaceball re-zero button pressed, hardware recalibrated\n");
      }

      if (buttons & SBALL_BUTTON_PICK) {
        printf("\nSpaceball pick button pressed\n");
      }
     
      printf("tx:%6d ty:%6d tz:%6d rx:%6d ry:%6d rz:%6d buttons:0x%03x\r",
             tx, ty, tz, rx, ry, rz, buttons);
      fflush(stdout);
    }
  }

  printf("\nSpaceball test complete.\n");
  printf("Closing down Spaceball port.\n");

  sball_close(sball);

  return 0;
}

