
/* Generated by Interface Builder */

#import "GloveWindow.h"
#import "GloveView.h"
#import <appkit/Application.h>
#import <appkit/Button.h>
#import <appkit/Matrix.h>
#import <dpsclient/psops.h>
#import <dpsclient/wraps.h>

@implementation GloveWindow

void timeHandler(DPSTimedEntry teNumber, double now, void *userData)
{
	[(id)userData getGloveData];
}

- appDidInit: sender
{
	powerGlove = [[PowerGlove alloc] init];
	[powerGlove init];
	[powerGlove setxHyst:0 yHyst:0 zHyst:0];

	timerNum = DPSAddTimedEntry(0.10, 
		&timeHandler, 
				self,
				NX_BASETHRESHOLD);
    return self;
}


- getGloveData
{    
	int knuckle;
	
	if( [powerGlove dspIsRunning] == NO )
		fprintf(stderr, "DSP not running....\n");
		
	[[rawDataMatrix cellAt: 0: 0] setIntValue: [powerGlove gloveX]];
	[[rawDataMatrix cellAt: 1: 0] setIntValue: [powerGlove gloveY]];
	[[rawDataMatrix cellAt: 2: 0] setIntValue: [powerGlove gloveZ]];
	[[rawDataMatrix cellAt: 3: 0] setIntValue: [powerGlove gloveRot]];
	[[rawDataMatrix cellAt: 4: 0] setIntValue: [powerGlove thumb]];
	[[rawDataMatrix cellAt: 5: 0] setIntValue: [powerGlove index]];
	[[rawDataMatrix cellAt: 6: 0] setIntValue: [powerGlove middle]];
	[[rawDataMatrix cellAt: 7: 0] setIntValue: [powerGlove ring]];
	[[rawDataMatrix cellAt: 8: 0] setStringValue: 
			[powerGlove padUp] ? "Yes" : "No" ];
	[[rawDataMatrix cellAt: 9: 0] setStringValue: 
			[powerGlove padDown] ? "Yes" : "No" ];
	[[rawDataMatrix cellAt: 10: 0] setStringValue: 
			[powerGlove padLeft] ? "Yes" : "No" ];
	[[rawDataMatrix cellAt: 11: 0] setStringValue: 
			[powerGlove padRight] ? "Yes" : "No" ];
	[[rawDataMatrix cellAt: 12: 0] setStringValue: 
			[powerGlove aPressed] ? "Yes" : "No" ];
	[[rawDataMatrix cellAt: 13: 0] setStringValue: 
			[powerGlove bPressed] ? "Yes" : "No" ];
	[[rawDataMatrix cellAt: 14: 0] setStringValue: 
			[powerGlove startPressed] ? "Yes" : "No" ];
	[[rawDataMatrix cellAt: 15: 0] setStringValue: 
			[powerGlove selectPressed] ? "Yes" : "No" ];
	
	for( knuckle = 0; knuckle < 4; knuckle ++ )
		{
		[thumbFingerMatrix setState: ([powerGlove thumb] > 2 - knuckle)
			at: knuckle: 0];
		[indexFingerMatrix setState: ([powerGlove index] > 2 - knuckle)
			at: knuckle: 0];
		[middleFingerMatrix setState: 
			([powerGlove middle] > 2 - knuckle)
			at: knuckle: 0];
		[ringFingerMatrix setState: ([powerGlove ring] > 2 - knuckle)
			at: knuckle: 0];
		[littleFingerMatrix setState: ([powerGlove ring] > 2 - knuckle)
			at: knuckle: 0];
		}
	[aButton setState: 	[powerGlove aPressed]];	
	[bButton setState: 	[powerGlove bPressed]];	
	[startButton setState: 	[powerGlove startPressed]];	
	[selectButton setState: [powerGlove selectPressed]];	
	[upButton setState: 	[powerGlove padUp]];	
	[downButton setState: 	[powerGlove padDown]];	
	[leftButton setState: 	[powerGlove padLeft]];	
	[rightButton setState: 	[powerGlove padRight]];	
	
	[gloveView setX: [powerGlove gloveX]
		 y: [powerGlove gloveY]  
		 z: [powerGlove gloveZ] 
		 roll: [powerGlove gloveRot]];
	[self displayIfNeeded];
	return self;
}


@end
