
/* Ansteuerung der parallelen Relaisplatine
 *
 * 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.
 *
 * Compile  with -O or -O2 or similar. The functions are
 * defined as inline macros, and will not be  substituted  in
 * without  optimization  enabled,  causing unresolved refer­
 * ences at link time.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/io.h>
#include <unistd.h> 

#define DEBUG 1

/* Bassisadressen der Parallelports */
#define LPT1 0x378
#define LPT2 0x278
#define LPT3 0x3BC

/* actuell verwendeter Port */
#define BASEPORT LPT1

void usage(void)
  {
  fprintf(stderr,"Ansteuerprogramm fuer die parallel Relaiskarte\n");
  fprintf(stderr,"No Parameters!\n");
  fprintf(stderr,"\n");
  fprintf(stderr,"Usage: relaisp <Parameter>\n");
  fprintf(stderr,"\n");
  fprintf(stderr,"Parameter:\n");
  fprintf(stderr,"   -stat:  Status der Relais als Dezimalzahl\n");
  fprintf(stderr,"           Bit=1: Relais an, Bit=0: Relais aus\n");
  fprintf(stderr,"           keine weiteren Parameter moeglich\n");
  fprintf(stderr,"   -off:   alle Relais aus\n");
  fprintf(stderr,"   -on:    alle Relais an\n");
  fprintf(stderr,"   -sx:    Relais x einschalten (1 <= x <= 8)\n");
  fprintf(stderr,"   -rx:    Relais x ausschalten (1 <= x <= 8)\n");
  fprintf(stderr,"\n");
  fprintf(stderr,"Beispiel:\n");
  fprintf(stderr,"   relaisp -off -s1 -s3: Relais 1 und 3 einschalten\n");
  fprintf(stderr,"   relaisp -s4 -r3:      Relais 4 ein- und 3 ausschalten\n");
  fprintf(stderr,"   relaisp -on -r6:      alle Relais ausser 6 einschalten\n");
  fprintf(stderr,"\n");
  }

int main(int argc, char *argv[])
  {
  unsigned char val = 0, rval = 0, n;

  if ((argc <= 1) || (strcmp(argv[1],"--help") == 0))
    {
    usage();
    exit(1);
    }

  /* Ports freigeben */
  if (ioperm(BASEPORT, 3, 1)) 
    { perror("Error: cannot access ioport"); exit(1); } 

  /* Parameter auswerten */
  for (n = 1; n < argc; n++)
    {
    if (strcmp(argv[n],"-stat") == 0)
      {
      /* Status liefern */
      rval = inb(BASEPORT);
      printf("Status: %d (%2X)\n", rval, rval);
      exit(0);
      }
    else if (strcmp(argv[n],"-off") == 0)
      {
      /* Alles ausschalten */
      rval = 0;
      }
    else if (strcmp(argv[n],"-on") == 0)
      {
      /* Alles einschalten */
      rval = 255;
      }
    else if ((argv[n][0] == '-') && (argv[n][1] == 's'))
      {
      val = atoi(&argv[n][2]);   /* Zahl hinter dem "-s" umwandeln */
      if ((1 <= val) && (val <= 8))
        {
        val = 1 << (val-1);
        rval = rval | val;
        }
      }
    else if ((argv[n][0] == '-') && (argv[n][1] == 'r'))
      {
      val = atoi(&argv[n][2]);   /* Zahl hinter dem "-r" umwandeln */
      if ((1 <= val) && (val <= 8))
        {
        val = ~(1 << (val-1));
        rval = rval & val;
        }
      }
    else
      {
      fprintf(stderr,"Wrong Parameter: %s\n",argv[n]);
      }
    }

  /* Set the data signals of the port */ 
#ifdef DEBUG
   printf("Debug: Writing to PORT %X.\n", BASEPORT);
   printf("Debug: Setting Data Port to %X.\n", rval);
#endif
  outb(rval, BASEPORT); 
  
  /* Ports wieder freigeben */
  if (ioperm(BASEPORT, 3, 0)) 
    { perror("Error: cannot access ioport"); exit(1); } 
  exit(0); 
  }


