package com.phidgets
{
	import com.phidgets.events.PhidgetDataEvent;
	
	/*
		Class: PhidgetServo
		A class for controlling a PhidgetServo.
		See your product manual for more specific API details, supported functionality, units, etc.
		
		Topic: Events
		Events supported by PhidgetServo. Pass these constants to the addEventListener() function when adding event listeners to a Phidget object.
		
		PhidgetDataEvent.POSITION_CHANGE	- position change
	*/
	public class PhidgetServo extends Phidget
	{
		private var numServos:int;
		private var positionMin:Number;
		private var positionMax:Number;
		
		private var positions:Array;
		private var motorEngagedState:Array;
		
		public function PhidgetServo(){
			super("PhidgetServo");
		}
		
		override protected function initVars():void{
			positionMin = com.phidgets.Constants.PUNK_NUM;
			positionMax = com.phidgets.Constants.PUNK_NUM;
			numServos = com.phidgets.Constants.PUNK_INT;
			positions = new Array(4);
			motorEngagedState = new Array(4);
		}
		
		override protected function onSpecificPhidgetData(setThing:String, index:int, value:String):void{
			switch(setThing)
			{
				case "NumberOfMotors":
					numServos = int(value);
					keyCount++;
					break;
				case "PositionMin":
					positionMin = Number(value);
					keyCount++;
					break;
				case "PositionMax":
					positionMax = Number(value);
					keyCount++;
					break;
				case "Engaged":
					if(motorEngagedState[index] == undefined)
						keyCount++;
					motorEngagedState[index] = value;
					break;
				case "Position":
					if(positions[index] == undefined)
						keyCount++;
					positions[index] = Number(value);
					if(isAttached)
						dispatchEvent(new PhidgetDataEvent(PhidgetDataEvent.POSITION_CHANGE,this,Number(positions[index]),index));
					break;
			}
		}
		
		//Getters
		/*
			Property: MotorCount
			Gets the number of motors supported by this controller.
		*/
		public function get MotorCount():int{
			if(numServos == com.phidgets.Constants.PUNK_INT)
				throw new PhidgetError(com.phidgets.Constants.EPHIDGET_UNKNOWNVAL);
			return numServos;
		}
		/*
			Function: getPosition
			Gets the current position of a motor.
			
			Parameters:
				index - motor index
		*/
		public function getPosition(index:int):Number{
			return Number(indexArray(positions, index, numServos, com.phidgets.Constants.PUNK_NUM));
		}
		/*
			Function: getEngaged
			Gets the engaged (powered) state of a motor.
			
			Parameters:
				index - motor index
		*/
		public function getEngaged(index:int):Boolean{
			return intToBool(int(indexArray(motorEngagedState, index, numServos, com.phidgets.Constants.PUNK_BOOL)));
		}
		/*
			Function: getPositionMin
			Gets the minimum position supported by a motor
			
			Parameters:
				index - motor index
		*/
		public function getPositionMin(index:int):Number{
			if(positionMin == com.phidgets.Constants.PUNK_NUM)
				throw new PhidgetError(com.phidgets.Constants.EPHIDGET_UNKNOWNVAL);
			return positionMin;
		}
		/*
			Function: getPositionMax
			Gets the maximum position supported by a motor.
			
			Parameters:
				index - motor index
		*/
		public function getPositionMax(index:int):Number{
			if(positionMax == com.phidgets.Constants.PUNK_NUM)
				throw new PhidgetError(com.phidgets.Constants.EPHIDGET_UNKNOWNVAL);
			return positionMax;
		}
		
		//Setters
		/*
			Function: setPosition
			Sets the position of a motor. If the motor is not engaged, this will engage it.
			
			Parameters:
				index - motor index
				val - position
		*/
		public function setPosition(index:int, val:Number):void{ 
			_phidgetSocket.setKey(makeIndexedKey("Position", index, numServos), val.toString(), true);
		}
		/*
			Function: setEngaged
			Sets the engaged (powered) state of a motor.
			
			Parameters:
				index - motor index
				val - engaged state
		*/
		public function setEngaged(index:int, val:Boolean):void{ 
			_phidgetSocket.setKey(makeIndexedKey("Engaged", index, numServos), boolToInt(val).toString(), true);
		}
	}
}