/* Controla motor de passo ligado na porta paralela do computador
 *
 *  Fabricio Ferrari 1998,
 *
 * Compiled with GCC under Linux 2.0.32 RedHat5.0
 * Copyright (c) 1998, Fabricio Ferrari 
 *          
 *        7   6   5   4   3   2   1   0   I/O Port
 *      +---+---+---+---+---+---+---+---+ ========
 * Data |   |   |   |   | C4| C3| C2| C1| Base = 278/378/3BC Hex
 *      +---+---+---+---+---+---+---+---+
 */

#include "motor-passo.h" 

int Gira( long int steps, int sentido, float tempo);

int coil = 0; 


void Espera(float fator){
  unsigned long int k;
  for (k=0; k < (fator*25000); k++);
}


int main(int argc, char *argv[] ) {
  
  int i;
  float graus, passos, pausa;

  if (argc < 3) {
    printf("\n\tUse: passos graus velocidade\n");
    printf("\tonde  graus -> tamanho da volta\n");
    printf("\t velocidade -> 1 - 20\n\n");
    exit(0);
  }
  
  printf("\nFF's Parallel Port Stepper Controller\n");
  printf("======================================\n\n");

  if (ioperm(Data,3,1)) { perror("ioperm"); exit(1); }


  graus = atof(argv[1]);
  passos =  (graus / 1.8) ;
  pausa = atof(argv[2]);
  printf("\n%.2f graus -> %i passos, ", graus, (int) passos);

  if      (passos > 0) Gira( abs((int) passos), HORARIO,     pausa);
  else if (passos < 0) Gira( abs((int) passos), ANTIHORARIO, pausa);

  exit(0);
}


int Gira(long int steps, int sentido, float tempo)
{
  long int j;
  static int bobina[4] = {0x01, 0x02, 0x04, 0x08};

  outb( 0x00,Data); 
  /*  printf("\n--> %i sentido, %li steps\n", sentido,steps); */

  if (sentido == HORARIO ){
    printf("Horario\n\n");
    for (j=1; j <= steps ; j++){
      outb(bobina[coil], Data); 
      Espera(tempo);
      coil++;
      if (coil > 3) coil = 0 ; 
    } 
  }

  if (sentido == ANTIHORARIO ){
    printf("Anti-Horario\n\n");
    for (j=1 ; j <= steps ; j++){
      outb(bobina[coil], Data); 
      Espera(tempo);
      coil--;
      if (coil < 0 ) coil = 3 ;
    } 
  }
  
  outb(0x00,Data); 

}

/* +++++++++++++ End of File ++++++++++++++++ */







