/*
 *	fr_example.c - Sample code for testing CineLink device driver
 *
 *	Copyright (C) 1990 Active Circuits, Inc.
 *	All Rights Reserved
 *
 *	HISTORY
 *		8/1/90 - Clean up and comment for release
 *              7/5/90 - First write
 */
#include <proto/exec.h>
#include <proto/dos.h>

#include <exec/types.h>
#include <exec/memory.h>
#include <exec/io.h>
#include <exec/devices.h>

#include <libraries/dos.h>

#include <CineLink/fr.h>

void cleanexit(void);
void main(int argc, char **argv);

#define NUMLINES	64

struct IOExtFr	frrequest;
struct MsgPort	*Port = NULL;
unsigned char *buffer = NULL;
USHORT	Width, Height;

void main(argc, argv)
int argc;
char **argv;
{
	unsigned char *bufptr;
	int error = 0;
	int col, i, y;

	if (!(Port = CreatePort("", 0L))) {
		exit();
	}
	frrequest.IOFr.io_Message.mn_ReplyPort = Port;

	error = OpenDevice("ACI-fr.device", 0, &frrequest, NULL);

	if (error) {
		printf("OpenDevice failed, error: %ld\n", error);
		exit (1);
	} else {
		printf("Opened fr device ok\n");
	}

	/* Do some initital setup work here:
	 *	Allocate space for our data buffer
	 */
	Width = 1024;
	Height = 1024;

	buffer = AllocMem(Width * NUMLINES, MEMF_CLEAR|MEMF_PUBLIC);

	/*
	 * This example will look for a SCSI device driver with the name
	 * "scsi.device" and open and expect to find a film recorder at
	 * SCSI unit 4. We do it Async just to show it can be done.
	 */
        frrequest.IOFr.io_Command = FRCMD_INIT;
	frrequest.IOFr.io_Data = (APTR)"scsi.device";
	frrequest.IOFr.io_Length = 4;

	printf("BeginIO for FRCMD_INIT\n");
	BeginIO(&frrequest);
	if (!CheckIO(&frrequest)) {
		printf("IO still pending\n");
	} else {
		printf("IO complete!\n");
	}
	WaitIO(&frrequest);
	/* Check to see if SCSI unit was really opened */
	error = frrequest.IOFr.io_Error;
	printf("Error was: %ld\n", error);
	if (error != Fr_Err_None) {
		cleanexit();
	}

	/*
	 * Now that we got the INIT done, we can set the params
	 * or begin an exposure (using the default fr settings)
	 * Note that if SETPARAMS isn't called, the fr_Width, fr_Height
	 * and fr_Pass fields must still contain the correct values for
	 * this image and exposure.
	 */
	frrequest.IOFr.io_Command = FRCMD_SETPARAMS;
	frrequest.fr_Film = EKT_100;
	frrequest.fr_Color = FINE_COLOR;
	frrequest.fr_Cont = HARD_CONTRAST;
	frrequest.fr_Res = FINE_RES;
	frrequest.fr_RedBal = 100;
	frrequest.fr_GrnBal = 100;
	frrequest.fr_BluBal = 100;
	frrequest.fr_Width = Width;
	frrequest.fr_Height = Height;
	frrequest.fr_Pass = RED;

	printf("BeginIO for FRCMD_SETPARAMS\n");
	BeginIO(&frrequest);
	if (!CheckIO(&frrequest)) {
		printf("IO still pending\n");
	} else {
		printf("IO complete!\n");
	}
	WaitIO(&frrequest);
	error = frrequest.IOFr.io_Error;
	printf("Error was: %ld\n", error);
	if (error != Fr_Err_None) {
		cleanexit();
	}

	/*
 	 * Now we begin an exposure
	 */
	frrequest.IOFr.io_Command = FRCMD_BEGIN;

	printf("BeginIO for FRCMD_BEGIN\n");
	BeginIO(&frrequest);
	if (CheckIO(&frrequest)) {
		printf("IO done!\n");
	} else {
		printf("IO still pending\n");
	}
	WaitIO(&frrequest);
	error = frrequest.IOFr.io_Error;
	printf("Error was: %ld\n", error);
	if (error != Fr_Err_None) {
		cleanexit();
	}

	/*
	 * Call CMD_WRITE for each color pass till we're done
	 */

	bufptr = buffer;
	/* First - fill our buffer with stuff */
	for (y = 0; y < NUMLINES; y++) {
		for (i = 0; i < Width; i++) {
			*bufptr++ = (UBYTE)(i % 256);
		}
	}

	frrequest.IOFr.io_Command = CMD_WRITE;
	frrequest.IOFr.io_Data = (APTR)buffer;
	frrequest.IOFr.io_Length = NUMLINES;

	for (col = 0; col < 3; col ++) {
		for (y = 0; y < Height;) {
			/* Do the CMD_WRITE */
			y += NUMLINES;
		}
		frrequest.fr_Pass = col;
	}

	/*
	 *
	 */
	frrequest.IOFr.io_Command = FRCMD_END;
	printf("DoIO for FRCMD_END\n");
	DoIO(&frrequest);
	error = frrequest.IOFr.io_Error;
	printf("Error was: %ld\n", error);
	if (error != Fr_Err_None) {
		cleanexit();
	}

	printf("Successful!\n");
	cleanexit();
}

void cleanexit()
{
	CloseDevice(&frrequest);
	if (Port) {
		DeletePort(Port);
		Port = NULL;
	}
	if (buffer) {
		FreeMem(buffer, Width * NUMLINES);
		buffer = NULL;
	}
	printf("Done!\n");

	exit(0);
}
