/*
	max547Driver.cpp
	-----------

	Drive code that handles interface to extra MAX547 DAC channel for each arm. For symmetry
	reason, each MAX547 board only provides a single DAC output.

   */

#include <stdio.h>
#include <stdlib.h>
#include <iostream.h>
#include <math.h>
#include <termios.h>
#include <sys/sched.h>
#include <sys/types.h>
#include <unistd.h>

#include "driver/PrTimer.h"
#include "driver/PrSTGDefn.h"
#include "max547Driver.h"
#include "Definitions.h"

#define DELAY_1MS		7159		// 1 ms delay if timer crystal is 7.159 MHz

#define HZ  4000

#define BUFFER_SIZE		120*HZ	// buffer to hold 30 seconds of data	
#define BUFFER_COL		10	

#define START_COUNT	4000
void main(void)
{
	ServoToGo *stgPtr = new ServoToGo(STG2_ARM_IRQ, STG2_ARM_ADDRESS);

	max547Driver *dacRight = new max547Driver(stgPtr);

	PrTimer timer(HZ);

	timer.start();

	int dataOut = -START_COUNT;
	static int c = 0;
	
	while(!tcischars(1))
	{
		stgPtr->RawDAC(0,1000);
		stgPtr->RawDAC(1,-1000);
		stgPtr->RawDAC(2,1000);
		stgPtr->RawDAC(3,-1000);
		stgPtr->RawDAC(4,1000);
		stgPtr->RawDAC(5,-1000);
		stgPtr->RawDAC(6,1000);
		stgPtr->RawDAC(7,-1000);
		dacRight->writeData(dataOut);	

		dataOut += 5;

		if(dataOut > START_COUNT)
			dataOut = -START_COUNT;
#if 0
		if(c == 20)
		{
			printf("out: %d \n", dataOut);
			c = 0;
		}
		c++;
#endif
		timer.wait();
	}

	delete dacRight;
	delete stgPtr;
}
