/*
 * init.c
 * Simon Newton <newtons _at_ iinet.net> 2003
 * turns the colour wheel stepper motor (bits 5 & 6), until bit 6 goes low
 * (ir beam is broken). Then turns the specified number of steps (OFFSET) 
 * to an initial position
 * 
 * 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
 *
 */

#include <stdio.h> 
#include <stdlib.h>
#include <asm/io.h>

#define PORT 0x378
#define PORT1 0x379

/* the number of steps to turn from the detected position to 
 * the inital position. This hardly matters, as long as the person 
 * writing the sequences knows where the wheel starts
 */
#define OFFSET 5
void turn_motor( int op1, int op2, int op3, int speed, int reps) ;

struct timespec tiempo;

int main (int argc, char **argv) {
	int i, data1, data2;
	
	/* data to write out to turn colour wheel stepper */
	data1 = 0; 
	data2 = 32;
			
   	tiempo.tv_sec = 0;
	tiempo.tv_nsec = 0;

	if (ioperm(PORT,1,1) || ioperm(PORT1,1,1))
		fprintf(stderr, "Couldn't get the port at %x\n", PORT), exit(1);

	i = inb(PORT1) ;
	
	/* wait until we detect it
	 * should prob have a limit on how
	 * long we're prepared to wait
	 * ~ 200 steps
	 */
	while(i != 56) {
		outb(data1,PORT);
		nanosleep(&tiempo,NULL);
		outb(data2,PORT);
		nanosleep(&tiempo,NULL);

		i = inb(PORT1) ;
	}

	for (i=0; i<OFFSET; i++) {
		outb(data1,PORT);
		nanosleep(&tiempo,NULL);
		outb(data2,PORT);
		nanosleep(&tiempo,NULL);
	}
}

