/* apccomm_pc_test.c
   Part of "APCComm" 
   Copyright (C) 2002 Ralf Hoffmann
   Contact: ralf.hoffmann@epost.de

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  
*/
/* $Id: apccomm_pc_test.c,v 1.1 2002/06/11 00:03:40 ralf Exp $ */

#include "apccomm_pc.h"
#include "apccomm_pc_tr.h"

int main( int argc, char *argv[] )
{
  int s1, s2, s3, oldv;
  int value;
  int useport = LPT1;
  int slave = 0;
  char buffer[128];

  if ( argc > 1 ) {
    for ( s1 = 1; s1 < argc; s1++ ) {
      if ( ( strncmp( argv[s1], "-h", 2 ) == 0 ) || ( strncmp( argv[s1], "--help", 6 ) == 0 ) ) {
        printf( "\nAPCComm Testprogram by Ralf Hoffmann\n" );
        printf( "  Usage: %s [Option]...\n\n", argv[0] );
        printf( "   -h, --help\t\tShow this help\n" );
        printf( "   -v, --version\tShow program-version\n" );
	printf( "   --port=<port>\tuse parallel port <port>\n" );
	printf( "   \t\t\t<port> can be lpt1 or lpt2\n" );
        printf( "   -c, --client\t\trun as slave\n" );
        printf( "  When starting without args, this testprogram runs as master\n" );
        exit( 0 );
      } else if ( ( strncmp( argv[s1], "-v", 2 ) == 0 ) || ( strncmp( argv[s1], "--version", 6 ) == 0 ) ) {
        printf( "\nAPCComm Testprogram by Ralf Hoffmann\n  Version %d.%d.%d\n", MAJOR, MINOR, PATCH );
        printf( "  Contact: ralf.hoffmann@epost.de\n" );
        exit( 0 );
      } else if ( strncmp( argv[s1], "--port=", strlen( "--port=" ) ) == 0 ) {
        if ( strcmp( argv[s1], "--port=lpt1" ) == 0 ) useport = LPT1;
	else if ( strcmp( argv[s1], "--port=lpt2" ) == 0 ) useport = LPT2;
	else {
	  printf( "No valid port, possibilities are lpt1 or lpt2\n" );
	  exit( 0 );
	}
      } else if( ( strncmp( argv[s1], "--client", strlen( "--client" ) ) == 0 ) ||
		 ( strncmp( argv[s1], "-c", strlen( "-c" ) ) == 0 ) ) {
	slave = 1;
      }
    }
  }

  if ( pin_init_user( useport ) < 0 ) {
    fprintf( stderr, "can't initialize parallelport!\n" );
    exit( 0 );
  }
  /* drop root privileges needed for access to parport */
  setuid( getuid() );
  setgid( getgid() );

  pin_output_mode( LP_DATA_PINS );
  if ( slave == 1 ) {
    do {
      s2 = ( getack() << 4 ) | getnibble();
    } while ( s2 != 31 );
    printf( "Read value=1111, ack=1\n" );
    printf( "When amiga is telling you so, press enter" );
    fflush( stdout );
    fgets( buffer, 127, stdin );
    s1 = 0;
    oldv = s2;
    value = 0;
    while ( s1 < ( 5 * 32 ) ) {
      do {
	s2 = ( getack() << 4 ) | getnibble();
	usleep( 1000 );
	s3 = ( getack() << 4 ) | getnibble();
	if ( s3 != s2 ) s2 = oldv;
      } while ( s2 == oldv );
      printf( "Read bitsettings:value=%d%d%d%d, ack=%d\n", ( s2 & 0x8 ) == 0x8, ( s2 & 0x4 ) == 0x4, ( s2 & 0x2 ) == 0x2, ( s2 & 0x1 ) == 0x1, ( s2 & 0x10 ) == 0x10 );
      value += s2;
      oldv = s2;
      s1++;
    }
    printf( "Test 1 finished\n" );
    printf( "Checksum=%d, should be %d\n", value, 5 * 496 );
    printf( "When amiga is telling you so, press enter" );
    fflush( stdout );
    fgets( buffer, 127, stdin );
    s1 = 0;
    while ( s1 < ( 5 * 5 * 2 ) ) {
      do {
	s2 = ( getack() << 4 ) | getnibble();
	usleep( 1000 );
	s3 = ( getack() << 4 ) | getnibble();
	if ( s3 != s2 ) s2 = oldv;
      } while ( s2 == oldv );
      printf( "Read bitsettings:value=%d%d%d%d, ack=%d\n", ( s2 & 0x8 ) == 0x8, ( s2 & 0x4 ) == 0x4, ( s2 & 0x2 ) == 0x2, ( s2 & 0x1 ) == 0x1, ( s2 & 0x10 ) == 0x10 );
      value += s2;
      oldv = s2;
      s1++;
    }
    printf( "Test 2 finished\n" );
  } else {
    printf( "Please start the amiga apccomm testprogramm with option -c\n" );
    printf( "Press Enter when done" );
    fflush( stdout );
    fgets( buffer, 127, stdin );
    printf( "Starting tests...\n" );
    printf( "Bitsettings:value=11, ack=1\n" );
    putvalue( 3 );
    setack( 1 );
    printf( "Now press Enter at the amiga to synchronize the client and then Enter here" );
    fflush( stdout );
    fgets( buffer, 127, stdin );
    for ( s1 = 0; s1 < 5; s1++ ) {
      for ( s2 = 0; s2 < 8; s2++ ) {
        printf( "Bitsettings:value=%d%d, ack=%d\n", ( s2 & 0x2 ) == 0x2, ( s2 & 0x1 ) == 0x1, ( s2 & 0x4 ) == 0x4 );
	putvalue( s2 & 0x3 );
	setack( s2 >> 2 );
	sleep( 1 );
      }
    }
    printf( "Test 1 finished\n" );
    printf( "Now press Enter at the amiga to synchronize the client and then Enter here" );
    fflush( stdout );
    fgets( buffer, 127, stdin );
    putvalue( 0 );
    for ( s1 = 0; s1 < 5; s1++ ) {
      printf( "Testing ack-bit = 0\n" );
      setack( 0 );
      sleep( 1 );
      printf( "Testing ack-bit = 1\n" );
      setack( 1 );
      sleep( 1 );
    }
    setack( 0 );
    for ( s2 = 0; s2 < 2; s2++ ) {
      for ( s1 = 0; s1 < 5; s1++ ) {
	printf( "Testing bit %d = 0\n", s2 );
	putvalue( 0 );
	sleep( 1 );
	printf( "Testing bit %d = 1\n", s2 );
	putvalue( 1 << s2 );
	sleep( 1 );
      }
    }
    printf( "Test 2 finished\n" );
  }
  return 0;
}

