
/* Ansteuerung der Motorplatine mit L298
 *
 * 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 Motorplatine\n");
  fprintf(stderr,"No Parameters!\n");
  fprintf(stderr,"\n");
  fprintf(stderr,"Usage: motor <Parameter>\n");
  fprintf(stderr,"\n");
  fprintf(stderr,"Parameter:\n");
  fprintf(stderr,"   -mxr:   Motor x rechtsdrehend\n");
  fprintf(stderr,"   -mxl:   Motor x linksdrehend\n");
  fprintf(stderr,"   -mxs:   Motor x stopp\n");
  fprintf(stderr,"           x = 1 ... 4\n");
  fprintf(stderr,"Es muessen immer die Parameter aller\n");
  fprintf(stderr,"verwendeter Motoren angegeben werden!\n");
  fprintf(stderr,"   -off:   alle Motoren aus\n");
  fprintf(stderr,"\n");
  }

int main(int argc, char *argv[])
  {
  unsigned char mot = 0, 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],"-off") == 0)
      {
      /* Alles ausschalten */
      rval = 0;
      }
    else if ((argv[n][0] == '-') && (argv[n][1] == 'm'))
      {
      mot = atoi(&argv[n][2]);   /* Zahl hinter dem "-m" umwandeln */
      if ((1 <= mot) && (mot <= 4)) /* erweiterbar auf 4 Motoren */
        {
        switch (argv[n][3])
          {
          case 'l': val = 1; break; /* 01 */
          case 'r': val = 2; break; /* 10 */
          case 's': val = 0; break; /* 00 */
          default: fprintf(stderr,"Wrong Parameter: %s\n",argv[n]);
          }
        switch (mot)
          {
          case 1: rval = rval | val; break;
          case 2: rval = rval | (val << 2); break;
          case 3: rval = rval | (val << 4); break;
          case 4: rval = rval | (val << 6); break;
          }
        }
      printf("%x %x\n",val, rval);

      }
    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); 
  }


