#import "PhidgetTextLEDController.h"

CPhidgetTextLEDHandle led;

@implementation PhidgetTextLEDController

- (IBAction)row1Changed:(id)sender
{
	CPhidgetTextLED_setDisplayString(led,0,(char *)[[row1Box  stringValue] UTF8String]);
}

- (IBAction)row2Changed:(id)sender
{
	CPhidgetTextLED_setDisplayString(led,1,(char *)[[row2Box  stringValue] UTF8String]);
}

- (IBAction)row3Changed:(id)sender
{
	CPhidgetTextLED_setDisplayString(led,3,(char *)[[row3Box  stringValue] UTF8String]);
}

- (IBAction)row4Changed:(id)sender
{
	CPhidgetTextLED_setDisplayString(led,4,(char *)[[row4Box  stringValue] UTF8String]);
}

- (IBAction)brightnessChanged:(id)sender
{
	CPhidgetTextLED_setBrightness(led,[sender intValue]);
}

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

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


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

	CPhidget_getSerialNumber((CPhidgetHandle)led, &serial);
	CPhidget_getDeviceVersion((CPhidgetHandle)led, &version);
	
	[connectedField setTitleWithMnemonic:@"PhidgetTextLCD Connected"];
	[serialField setTitleWithMnemonic:[NSString stringWithFormat:@"%d", serial]];
	[versionField setTitleWithMnemonic:[NSString stringWithFormat:@"%d", version]];
	
	[row1Box setEditable:true];
	[row2Box setEditable:true];
	[row3Box setEditable:true];
	[row4Box setEditable:true];
	
	CPhidgetTextLED_setBrightness(led, 100);

	[pool release];
}

/*
* check if it's the servo we've been controlling and if so disable the controls
*/
- (void)phidgetRemoved
{
	NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
	[connectedField setTitleWithMnemonic:@"PhidgetTextLED Removed"];
	[serialField setTitleWithMnemonic:@""];
	[versionField setTitleWithMnemonic:@""];
	[row1Box setEditable:false];
	[row2Box setEditable:false];
	[row3Box setEditable:false];
	[row4Box setEditable:false];
	[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];
	
	CPhidget_enableLogging(PHIDGET_LOG_VERBOSE, NULL);
	
	CPhidgetTextLED_create(&led);
	
	CPhidget_set_OnAttach_Handler((CPhidgetHandle)led, gotAttach, self);
	CPhidget_set_OnDetach_Handler((CPhidgetHandle)led, gotDetach, self);
	
	if(remote)
		CPhidget_openRemote((CPhidgetHandle)led, serial, NULL, NULL);
	else
		CPhidget_open((CPhidgetHandle)led, serial);
}

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

@end
