#import "PhidggetTemperatureSensorController.h"

CPhidgetTemperatureSensorHandle temp;

@implementation PhidggetTemperatureSensorController

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

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

- (IBAction)choseThermocoupleType:(id)sender
{
	if([[sender titleOfSelectedItem] isEqualToString:@"K"])
		CPhidgetTemperatureSensor_setThermocoupleType(temp, 0, PHIDGET_TEMPERATURE_SENSOR_K_TYPE);
	else if([[sender titleOfSelectedItem] isEqualToString:@"J"])
		CPhidgetTemperatureSensor_setThermocoupleType(temp, 0, PHIDGET_TEMPERATURE_SENSOR_J_TYPE);
	else if([[sender titleOfSelectedItem] isEqualToString:@"E"])
		CPhidgetTemperatureSensor_setThermocoupleType(temp, 0, PHIDGET_TEMPERATURE_SENSOR_E_TYPE);
	else if([[sender titleOfSelectedItem] isEqualToString:@"T"])
		CPhidgetTemperatureSensor_setThermocoupleType(temp, 0, PHIDGET_TEMPERATURE_SENSOR_T_TYPE);
}

int gotTemperature(CPhidgetTemperatureSensorHandle phid, void *context, int ind, double val) {
	[(id)context gotTemperatureChange:ind:val];
	return 0;
}

- (void)gotTemperatureChange:(int)Index:(double)Value
{
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
	double ambient;
	
	CPhidgetTemperatureSensor_getAmbientTemperature(temp, &ambient);
	
	[temp1Field setTitleWithMnemonic:[NSString stringWithFormat:@"%0.4f", Value]];
	[temp0Field setTitleWithMnemonic:[NSString stringWithFormat:@"%0.3f", ambient]];
		
	[pool release];
}

- (void)phidgetAdded
{
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
	int serial, version;
	int numInputs;

	CPhidget_getSerialNumber((CPhidgetHandle)temp, &serial);
	CPhidget_getDeviceVersion((CPhidgetHandle)temp, &version);
	CPhidgetTemperatureSensor_getTemperatureInputCount(temp,&numInputs);
	
	[connectedField setTitleWithMnemonic:@"PhidgetTemperatureSensor Connected"];
	[serialField setTitleWithMnemonic:[NSString stringWithFormat:@"%d", serial]];
	[versionField setTitleWithMnemonic:[NSString stringWithFormat:@"%d", version]];
	[numInputsField setTitleWithMnemonic:[NSString stringWithFormat:@"%i", numInputs]];
	
	CPhidgetTemperatureSensor_setTemperatureChangeTrigger(temp,0,0);

	[pool release];
}

- (void)phidgetRemoved
{
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

	[connectedField setTitleWithMnemonic:@"PhidgetTemperatureSensor Removed"];
	[serialField setTitleWithMnemonic:@""];
	[versionField setTitleWithMnemonic:@""];
	[numInputsField setTitleWithMnemonic:@""];

	[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];
	
	CPhidgetTemperatureSensor_create(&temp);
	
	CPhidgetTemperatureSensor_set_OnTemperatureChange_Handler(temp, gotTemperature, self);
	CPhidget_set_OnAttach_Handler((CPhidgetHandle)temp, gotAttach, self);
	CPhidget_set_OnDetach_Handler((CPhidgetHandle)temp, gotDetach, self);
	
	if(remote)
		CPhidget_openRemote((CPhidgetHandle)temp, serial, NULL, NULL);
	else
		CPhidget_open((CPhidgetHandle)temp, serial);
}

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

@end