#import "PhidgetMotorController.h"

CPhidgetMotorControlHandle motor;

@implementation PhidgetMotorController

- (IBAction)accel0:(id)sender
{
	CPhidgetMotorControl_setAcceleration(motor,0,[sender intValue]);
}

- (IBAction)accel1:(id)sender
{
	CPhidgetMotorControl_setAcceleration(motor,1,[sender intValue]);
}

- (IBAction)speed0:(id)sender
{
	CPhidgetMotorControl_setVelocity(motor,0,[sender intValue]);
}

- (IBAction)speed1:(id)sender
{
	CPhidgetMotorControl_setVelocity(motor,1,[sender intValue]);
}


int gotAttach(CPhidgetHandle phid, void *context) {
	[(id)context phidgetAdded];
	return 0;
}

int gotDetach(CPhidgetHandle phid, void *context) {
	[(id)context phidgetRemoved];
	return 0;
}

int gotInputChange(CPhidgetMotorControlHandle phid, void *context, int ind, int val) {
	[(id)context InputChange:ind:val];
	return 0;
}

int gotMotorChange(CPhidgetMotorControlHandle phid, void *context, int ind, double val) {
	[(id)context MotorChange:ind:val];
	return 0;
}

int gotCurrentChange(CPhidgetMotorControlHandle phid, void *context, int ind, double val) {
	[(id)context CurrentChange:ind:val];
	return 0;
}

- (void)phidgetAdded
{
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
	
	int serial, version;
	int numMotors, numInputs;
	
	CPhidget_getSerialNumber((CPhidgetHandle)motor, &serial);
	CPhidget_getDeviceVersion((CPhidgetHandle)motor, &version);
	CPhidgetMotorControl_getInputCount(motor, &numInputs);
	CPhidgetMotorControl_getMotorCount(motor, &numMotors);
	
	[connectedField setTitleWithMnemonic:@"PhidgetMotorControl Connected"];
	[serialField setTitleWithMnemonic:[NSString stringWithFormat:@"%d", serial]];
	[versionField setTitleWithMnemonic:[NSString stringWithFormat:@"%d", version]];
	[numInputsField setTitleWithMnemonic:[NSString stringWithFormat:@"%i", numInputs]];
	[numMotorsField setTitleWithMnemonic:[NSString stringWithFormat:@"%i", numMotors]];

	[pool release];
}

- (void)phidgetRemoved
{
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
	[connectedField setTitleWithMnemonic:@"PhidgetMotorControl Removed"];
	[serialField setTitleWithMnemonic:@""];
	[versionField setTitleWithMnemonic:@""];
	[numInputsField setTitleWithMnemonic:@""];
	[numMotorsField setTitleWithMnemonic:@""];
	[pool release];
}

- (void)InputChange:(int)Index:(int)State
{
	[[inputs cellWithTag:Index] setState:State];
}

- (void)MotorChange:(int)Index:(double)Value
{
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
	switch(Index) {
		case 0:
			[speed0Field setTitleWithMnemonic:[NSString stringWithFormat:@"%f", Value]];
			break;
		case 1:
			[speed1Field setTitleWithMnemonic:[NSString stringWithFormat:@"%f", Value]];
			break;
	}
	[pool release];
}

- (void)CurrentChange:(int)Index:(double)Value
{
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
	switch(Index) {
		case 0:
			[current0Field setTitleWithMnemonic:[NSString stringWithFormat:@"%f", Value]];
			break;
		case 1:
			[current1Field setTitleWithMnemonic:[NSString stringWithFormat:@"%f", Value]];
			break;
	}
	[pool release];
}

/*
* This gets run when the GUI gets displayed
*/
- (void)awakeFromNib
{
	int serial = -1, remote = 0;
	NSArray *args = [[NSProcessInfo processInfo] arguments];
	if([args count] > 1)
	{
		if([[args objectAtIndex:1] isEqualToString:@"remote"])
			remote = 1;
		serial = strtol([[args objectAtIndex:[args count]-1] UTF8String], NULL, 10);
		if(serial == 0) serial = -1;
	}
	
	[mainWindow setDelegate:self];
	
	CPhidgetMotorControl_create(&motor);
	
	CPhidgetMotorControl_set_OnInputChange_Handler(motor, &gotInputChange, self);
	CPhidgetMotorControl_set_OnVelocityChange_Handler(motor, &gotMotorChange, self);
	CPhidgetMotorControl_set_OnCurrentChange_Handler(motor, &gotCurrentChange, self);
	CPhidget_set_OnAttach_Handler((CPhidgetHandle)motor, gotAttach, self);
	CPhidget_set_OnDetach_Handler((CPhidgetHandle)motor, gotDetach, self);
	
	if(remote)
		CPhidget_openRemote((CPhidgetHandle)motor, serial, NULL, NULL);
	else
		CPhidget_open((CPhidgetHandle)motor, serial);
}

- (void)windowWillClose:(NSNotification *)aNotification {
	CPhidget_close((CPhidgetHandle)motor);
	CPhidget_delete((CPhidgetHandle)motor);
	motor = NULL;
	[NSApp terminate:self];
}

@end
