
#import "PhidgetAccelerometerController.h"
	
CPhidgetAccelerometerHandle accel;

@implementation PhidgetAccelerometerController

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

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

int gotAccel(CPhidgetAccelerometerHandle phid, void *context, int index, double value) {
	[(id)context gotAccelChange:index:value];
	return 0;
}

- (void)gotAccelChange:(int)index:(double)value
{
	int i=0;
	double out = 0;
	
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
	
	if(index == 0) {
		xaxis[6] = value;
		for(i=0;i<6;i++) {
			xaxis[i] = xaxis[i+1];
			out = out + xaxis[i];
		}
		[customView setX:out/6.0];
		[xField setFloatValue:out/6.0];
		//[customView setX:[accelInfo getVal]];
	}
	else if(index == 1){
		yaxis[6] = value;
		for(i=0;i<6;i++) {
			yaxis[i] = yaxis[i+1];
			out = out + yaxis[i];
		}
		[customView setY:out/6.0];
		[yField setFloatValue:out/6.0];
		//[customView setY:[accelInfo getVal]];
	}
	else {
		zaxis[6] = value;
		for(i=0;i<6;i++) {
			zaxis[i] = zaxis[i+1];
			out = out + zaxis[i];
		}
		[customView setZ:out/6.0];
		[zField setFloatValue:out/6.0];
		//[customView setZ:[accelInfo getVal]];
	}
	
	double xaxis1, yaxis1, zaxis1;
	CPhidgetAccelerometer_getAcceleration(accel, 0, &xaxis1);
	CPhidgetAccelerometer_getAcceleration(accel, 1, &yaxis1);
	CPhidgetAccelerometer_getAcceleration(accel, 2, &zaxis1);
	
	//printf("X: %2.4f Y: %2.4f Z: %2.4f\n",xaxis1,yaxis1,zaxis1);
	
    [customView setNeedsDisplay:YES];
	
	[pool release];
}

- (void)phidgetAdded
{
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
	
	int serial, version;
	int numAxes;
	double accelMax, accelMin;
	const char *name;

	CPhidget_getSerialNumber((CPhidgetHandle)accel, &serial);
	CPhidget_getDeviceVersion((CPhidgetHandle)accel, &version);
	CPhidget_getDeviceName((CPhidgetHandle)accel, &name);
	CPhidgetAccelerometer_getAxisCount(accel, &numAxes);
	CPhidgetAccelerometer_getAccelerationMax(accel, 0, &accelMax);
	CPhidgetAccelerometer_getAccelerationMin(accel, 0, &accelMin);
	
	[connectedField setStringValue:[NSString stringWithCString:name]];
	[serialNumberField setIntValue:serial];
	[versionField setIntValue:version];
	[numberOfAxesField setIntValue:numAxes];
	
	if(numAxes == 3)
	{
		[zLabel setHidden:FALSE];
		[zField setHidden:FALSE];
	}
	else
	{
		[zLabel setHidden:TRUE];
		[zField setHidden:TRUE];
	}
	
	if(accelMax == -accelMin)
		[rangeField setStringValue:[NSString stringWithFormat:@"+-%0.1lfg",accelMax]];
	else
		[rangeField setStringValue:[NSString stringWithFormat:@"%0.1lfg to %0.1lfg",accelMin,accelMax]];
	
	[self setPicture:version:name];
	[pool release];
	[mainWindow display];
}

- (void)phidgetRemoved
{
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
	
	[connectedField setStringValue:@"Nothing"];
	[serialNumberField setStringValue:@""];
	[versionField setStringValue:@""];
	[numberOfAxesField setStringValue:@""];
	
	[self setPicture:0:""];
	[pool release];
	[mainWindow display];
}

/*
* 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];
	
	CPhidgetAccelerometer_create(&accel);
	
	CPhidgetAccelerometer_set_OnAccelerationChange_Handler(accel, gotAccel, self);
	CPhidget_set_OnAttach_Handler((CPhidgetHandle)accel, gotAttach, self);
	CPhidget_set_OnDetach_Handler((CPhidgetHandle)accel, gotDetach, self);
	
	if(remote)
		CPhidget_openRemote((CPhidgetHandle)accel, serial, NULL, NULL);
	else
		CPhidget_openRemote((CPhidgetHandle)accel, -1, NULL, NULL);
		//CPhidget_openRemoteIP((CPhidgetHandle)accel, -1, "phidgetsbc-2.local", 5001, NULL);
		//CPhidget_open((CPhidgetHandle)accel, serial);
}

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

- (void)setPicture:(int)version:(const char *)name
{
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
	NSString *imgPath;
	
	if (!strcmp(name, "Phidget Accelerometer 2-axis")) 
	{
		imgPath = [[NSBundle bundleForClass:[self class]] pathForResource:@"1053_0" ofType:@"icns"];
	}
	else if (!strcmp(name, "Phidget Accelerometer 3-axis")) 
	{
		imgPath = [[NSBundle bundleForClass:[self class]] pathForResource:@"1059_0" ofType:@"icns"];
	}
	else imgPath = nil;
	
	NSImage *img = [[NSImage alloc]  initByReferencingFile:imgPath];
	
	//otherwise the images are just painted over each other - and the transparency causes trouble
	[pictureBox setImage:nil];
	[pictureBox display];
	if(imgPath!=nil)
		[NSApp setApplicationIconImage: img];
	[pictureBox setImage:img];
	[pictureBox display];
	
	[pool release];
}
@end
