topical media & game development
actionscript-phidget-flex-PhidgetServoExample.mx
actionscript-phidget-flex-PhidgetServoExample.mx
[swf]
flex
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="350" height="264">
<mx:Script>
<![CDATA[
import com.phidgets.PhidgetServo;
import com.phidgets.events.*;
public var phid:com.phidgets.PhidgetServo;
public function connect():void{
phid = new PhidgetServo();
phid.addEventListener(PhidgetEvent.CONNECT, onConnect);
phid.addEventListener(PhidgetEvent.DETACH, onDetach);
phid.addEventListener(PhidgetEvent.ATTACH, onAttach);
phid.addEventListener(PhidgetEvent.DISCONNECT, onDisconnect);
phid.addEventListener(PhidgetDataEvent.POSITION_CHANGE, onPositionChange);
phid.open(addressText.text, int(portText.text), passwordText.text);
}
public function setServo(id:String):void {
switch(id)
{
case "servo0": phid.setPosition(0, servo0.value); break;
case "servo1": phid.setPosition(1, servo1.value); break;
case "servo2": phid.setPosition(2, servo2.value); break;
case "servo3": phid.setPosition(3, servo3.value); break;
}
}
private function onAttach(evt:PhidgetEvent):void{
statusText.text = "Attached";
phidgetNameText.text = phid.Name;
versionText.text = phid.Version.toString();
serialText.text = phid.serialNumber.toString();
numServos.text = phid.MotorCount.toString();
}
private function onDetach(evt:PhidgetEvent):void{
statusText.text = "Detached";
}
private function onDisconnect(evt:PhidgetEvent):void{
connectedText.text = "Disconnected";
}
private function onConnect(evt:PhidgetEvent):void{
connectedText.text = "Connected";
}
private function onPositionChange(evt:PhidgetDataEvent):void{
switch(evt.Index){
case 0: servoPosition0.text = int(evt.Data).toString(); break;
case 1: servoPosition1.text = int(evt.Data).toString(); break;
case 2: servoPosition2.text = int(evt.Data).toString(); break;
case 3: servoPosition3.text = int(evt.Data).toString(); break;
}
}
]]>
</mx:Script>
<mx:Button x="218" y="230" label="Connect" click="connect()"/>
<mx:TextInput x="83" y="202" id="addressText" text="localhost" width="107"/>
<mx:TextInput x="238" y="202" id="portText" width="60" text="5001"/>
<mx:TextInput x="103" y="230" id="passwordText" width="107"/>
<mx:Text x="21" y="204" text="Address:"/>
<mx:Text x="198" y="204" text="Port:"/>
<mx:Text x="34" y="232" text="Password:"/>
<mx:Text x="178" y="10" text="Phidget Status:"/>
<mx:Text x="10" y="10" text="Server Status:"/>
<mx:Text x="277" y="10" id="statusText" text="Unknown"/>
<mx:Text x="102" y="10" text="Unknown" id="connectedText"/>
<mx:Text x="10" y="54" text="Serial Number:"/>
<mx:Text x="11" y="72" text="Version:"/>
<mx:Text x="10" y="36" text="Phidget:"/>
<mx:Text x="102" y="36" text="Unknown" id="phidgetNameText"/>
<mx:Text x="102" y="54" text="Unknown" id="serialText"/>
<mx:Text x="102" y="72" text="Unknown" id="versionText"/>
<mx:HSlider x="21" y="98" width="259" id="servo0" minimum="-23" maximum="232" change="setServo(servo0.id)" liveDragging="true"/>
<mx:Text x="288" y="98" text="?" id="servoPosition0"/>
<mx:HSlider x="21" y="124" width="259" id="servo1" minimum="-23" maximum="232" change="setServo(servo1.id)" liveDragging="true"/>
<mx:Text x="288" y="124" text="?" id="servoPosition1"/>
<mx:HSlider x="21" y="150" width="259" id="servo2" minimum="-23" maximum="232" change="setServo(servo2.id)" liveDragging="true"/>
<mx:Text x="288" y="150" text="?" id="servoPosition2"/>
<mx:HSlider x="21" y="176" width="259" id="servo3" minimum="-23" maximum="232" change="setServo(servo3.id)" liveDragging="true"/>
<mx:Text x="288" y="176" text="?" id="servoPosition3"/>
<mx:Text x="178" y="72" text="Number of Servos:"/>
<mx:Text x="295" y="72" text="?" id="numServos"/>
</mx:Application>
(C) Æliens
27/08/2009
You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.