Alert.png

Notice: This page contains information for the legacy Phidget21 Library. Phidget21 does not support VINT Phidgets, and will not support any new Phidgets. Phidget21 will be maintained until 2020. We recommend that new projects be developed against the Phidget22 Library.


Click on the 2phidget22.jpg button in the menu bar to go to the Phidget22 version of this page.

Alert.png

Difference between revisions of "Language - Flex AS3"

From Phidgets Legacy Support
Jump to: navigation, search
 
(10 intermediate revisions by 5 users not shown)
Line 1: Line 1:
Preamble about the language and its general strengths and weaknesses.
+
[[Category:LegacyLanguage]]
 
+
{{OSLang|[[File:icon-Flex_AS3.png|64x64px|link=|alt=]]|Flex AS3 support is no longer maintained by Phidgets.}}
==Assessment for use with Phidgets==
+
Our honest opinion on how well this language is suited to controlling Phidgets. If it is a poor choice, suggest and link similar (better) languages.
+
  
 
==Support==
 
==Support==
Flex AS3 has a complete API and code samples for all Phidgets devices.
 
  
For a complete list of our supported languages and their support status, [[Levels of Support|click here]].
+
We no longer offer support for Flex AS3. 
 +
 
 +
Please select one of our [[Software Overview#Language Support|supported languages]] to use your Phidget.
  
==Restrictions==
+
==Legacy Documentation==
In this section, list any restrictions or limitations that this particular language may impose. For example, incompatibility with certain operating systems.
+
  
==Development Environments and Compilers==
+
Although we no longer offer support for Flex AS3, our old documentation is below.
  
Describe each major compiler and notable differences or important information. (eg. framework versions) If there are known issues/workarounds mention them and link to the corresponding issue at the bottom of the page.  
+
If you would like to download drivers for your own use with Flex AS3, please refer to the page for your [[Software Overview#Operating System Support|operating system]].
  
==Drivers, Libraries and Resources==
+
Other files:
Before you can run your program, you need to set up the proper environment and get the necessary files off the Phidgets website.
+
Visit the drivers section at www.phidgets.com and get the latest:
+
* [http://www.phidgets.com/drivers.php Phidget Framework]
+
You will need the Phidget Framework to use and to program with Phidgets. We also recommend that you download the following reference materials:
+
* [http://www.phidgets.com/documentation/Phidget21Flash_doc.zip API Manual]
+
* [http://www.phidgets.com/documentation/web/flashdoc/index.html API Reference]
+
* [http://www.phidgets.com/downloads/examples/Flex_2.1.8.20110615.zip Flex AS3 Sample Code]
+
* You can find a high level discussion about programming with Phidgets in general on the [[General API]] page.
+
* The [[Device Functionality]] page explains the general operational information for your device.
+
  
You may want to have these pages open while working through these instructions.
+
* [{{SERVER}}/documentation/Phidget21Flash_doc.zip Flash API Manual]
 +
* [{{SERVER}}/documentation/web/flashdoc/index.html Flash API Reference]
 +
* [{{SERVER}}/downloads/phidget21/examples/as3/Flex.zip Flex AS3 Sample Code]
  
==Getting Started==
+
===Getting Started===
  
 
The Phidget examples were written using Actionscript 3.0 under Adobe Flex Builder 3 and this tutorial assumes its use.  
 
The Phidget examples were written using Actionscript 3.0 under Adobe Flex Builder 3 and this tutorial assumes its use.  
Line 38: Line 29:
 
As an alternative, you can copy the “com” folder that is located in the Flash AS3 examples into your project source directory and it should work as well.  
 
As an alternative, you can copy the “com” folder that is located in the Flash AS3 examples into your project source directory and it should work as well.  
  
===Coding For Your Phidget===
+
====Coding For Your Phidget====
  
 
Before you can use the Phidget, you must import references from the library to the device in the main body of code. In MXML and Actionscript 3.0:
 
Before you can use the Phidget, you must import references from the library to the device in the main body of code. In MXML and Actionscript 3.0:
  
<div style="background-color: #f3f3f3; border-color: #1c9edb; border-width:1px; border-style: dashed;">
+
<div class="source"><syntaxhighlight lang=actionscript>
<font size="3">
+
<source lang=actionscript>
+
  
 
   <?xml version="1.0" encoding="utf-8"?>
 
   <?xml version="1.0" encoding="utf-8"?>
Line 57: Line 46:
 
   </mx:Application>
 
   </mx:Application>
  
</source>
+
</syntaxhighlight></div>
</font>
+
</div>
+
  
 
Afterwards, the Phidget object will need to be declared and then initialized.  
 
Afterwards, the Phidget object will need to be declared and then initialized.  
 
For example, a start function is set to execute on initialization, and then an instance of a PhidgetInterfaceKit is declared and then set inside with:  
 
For example, a start function is set to execute on initialization, and then an instance of a PhidgetInterfaceKit is declared and then set inside with:  
  
<div style="background-color: #f3f3f3; border-color: #1c9edb; border-width:1px; border-style: dashed;">
+
<div class="source"><syntaxhighlight lang=actionscript>
<font size="3">
+
<source lang=actionscript>
+
  
 
   public var phid:com.phidgets.PhidgetInterfaceKit;
 
   public var phid:com.phidgets.PhidgetInterfaceKit;
Line 74: Line 59:
 
}
 
}
  
</source>
+
</syntaxhighlight></div>
</font>
+
</div>
+
  
 
The object name for any type of Phidget is listed in the API manual. Every type of Phidget also inherits functionality from the Phidget base class.
 
The object name for any type of Phidget is listed in the API manual. Every type of Phidget also inherits functionality from the Phidget base class.
  
===Connecting to the Phidget===
+
====Connecting to the Phidget====
  
 
Next, the program needs to try and connect to the Phidget through an open call.  
 
Next, the program needs to try and connect to the Phidget through an open call.  
Line 87: Line 70:
 
We can handle this by using event driven programming and tracking the AttachEvents and DetachEvents, or checking the isAttached property.
 
We can handle this by using event driven programming and tracking the AttachEvents and DetachEvents, or checking the isAttached property.
  
<div style="background-color: #f3f3f3; border-color: #1c9edb; border-width:1px; border-style: dashed;">
+
<div class="source"><syntaxhighlight lang=actionscript>
<font size="3">
+
<source lang=actionscript>
+
  
 
   phid.open("localhost", 5001);
 
   phid.open("localhost", 5001);
  
</source>
+
</syntaxhighlight></div>
</font>
+
</div>
+
  
 
The API manual lists all of the available modes that open provides.  
 
The API manual lists all of the available modes that open provides.  
Line 103: Line 82:
 
The one connection per device limit does not apply when exclusively using the Phidget WebService, and the Actionscript libraries by extension.
 
The one connection per device limit does not apply when exclusively using the Phidget WebService, and the Actionscript libraries by extension.
  
===Event Driven Programming===
+
====Event Driven Programming====
  
 
We recommend the use of event driven programming when working with Phidgets.  
 
We recommend the use of event driven programming when working with Phidgets.  
 
In Actionscript 3.0, we hook an event handler with the following code:  
 
In Actionscript 3.0, we hook an event handler with the following code:  
  
<div style="background-color: #f3f3f3; border-color: #1c9edb; border-width:1px; border-style: dashed;">
+
<div class="source"><syntaxhighlight lang=actionscript>
<font size="3">
+
<source lang=actionscript>
+
 
   
 
   
 
   phid.addEventListener(PhidgetDataEvent.SENSOR_CHANGE, onSensorChange);
 
   phid.addEventListener(PhidgetDataEvent.SENSOR_CHANGE, onSensorChange);
  
</source>
+
</syntaxhighlight></div>
</font>
+
</div>
+
  
 
The onSensorChange method will get executed every time the PhidgetInterfaceKit reports a change on one of its analog inputs.  
 
The onSensorChange method will get executed every time the PhidgetInterfaceKit reports a change on one of its analog inputs.  
 
The values from the report can be accessed from the PhidgetDataEvent object.
 
The values from the report can be accessed from the PhidgetDataEvent object.
  
<div style="background-color: #f3f3f3; border-color: #1c9edb; border-width:1px; border-style: dashed;">
+
<div class="source"><syntaxhighlight lang=actionscript>
<font size="3">
+
<source lang=actionscript>
+
  
 
   private function onSensorChange(evt:PhidgetDataEvent):void{
 
   private function onSensorChange(evt:PhidgetDataEvent):void{
Line 129: Line 102:
 
}
 
}
  
</source>
+
</syntaxhighlight></div>
</font>
+
</div>
+
  
 
Some events such as Attach and Detach belong to the base Phidget object and thus are common to all types of Phidgets.  
 
Some events such as Attach and Detach belong to the base Phidget object and thus are common to all types of Phidgets.  
 
Please refer to the API manual for a full list of events and their usage.  
 
Please refer to the API manual for a full list of events and their usage.  
  
===Working directly with the Phidget===
+
====Working directly with the Phidget====
  
 
Some values can be directly read and set on the Phidget and used as an alternative to event driven programming.  
 
Some values can be directly read and set on the Phidget and used as an alternative to event driven programming.  
 
Simply use the instance properties or call member functions such as getSensorValue(index: int) or setOutputState(index: int, val: Boolean) for PhidgetInterfaceKits.
 
Simply use the instance properties or call member functions such as getSensorValue(index: int) or setOutputState(index: int, val: Boolean) for PhidgetInterfaceKits.
  
===Working with multiple Phidgets===
+
====Working with multiple Phidgets====
  
 
Multiple Phidgets of the same type can easily be run inside the same program.  
 
Multiple Phidgets of the same type can easily be run inside the same program.  
Line 148: Line 119:
 
If the application needs to distinguish between the devices, open can be called with the serial number of a specific Phidget.
 
If the application needs to distinguish between the devices, open can be called with the serial number of a specific Phidget.
  
===Flash Security Settings===
+
====Flash Security Settings====
  
 
During debugging or after publishing the project, you may encounter some difficulties with Flash network security settings either inside or outside of the development environment with Phidgets.  
 
During debugging or after publishing the project, you may encounter some difficulties with Flash network security settings either inside or outside of the development environment with Phidgets.  
 
Permissions for your project folder can be added through the settings manager at [http://www.macromedia.com/support/documentation/en/flashplayer/help/settings_manager04a.html here], under “Always trust files in these locations” | “Edit locations...” | “Add location...”.
 
Permissions for your project folder can be added through the settings manager at [http://www.macromedia.com/support/documentation/en/flashplayer/help/settings_manager04a.html here], under “Always trust files in these locations” | “Edit locations...” | “Add location...”.
  
===Other Phidgets===
+
====Other Phidgets====
  
 
The design given in this document can also be followed for almost all Phidgets.  
 
The design given in this document can also be followed for almost all Phidgets.  
 
For example, if you were using a PhidgetRFID instead of an PhidgetInterfacekit, you would declare a RFID object instead of a InterfaceKit object.  
 
For example, if you were using a PhidgetRFID instead of an PhidgetInterfacekit, you would declare a RFID object instead of a InterfaceKit object.  
 
The methods and events available would change but they can be accessed in a similar manner.
 
The methods and events available would change but they can be accessed in a similar manner.
 
==Building your Project==
 
Describe the different ways a project could be built using this language.
 
 
==Common Problems and Solutions/Workarounds==
 
Here you can put various frequent problems and our recommended solutions.
 

Latest revision as of 19:14, 6 June 2017

Flex AS3 support is no longer maintained by Phidgets.

Support

We no longer offer support for Flex AS3.

Please select one of our supported languages to use your Phidget.

Legacy Documentation

Although we no longer offer support for Flex AS3, our old documentation is below.

If you would like to download drivers for your own use with Flex AS3, please refer to the page for your operating system.

Other files:

Getting Started

The Phidget examples were written using Actionscript 3.0 under Adobe Flex Builder 3 and this tutorial assumes its use. Other development environments should work provided they support Actionscript 3.0, and each would be set up in a similar manner. First launch Flex and generate a new project with a descriptive name such as PhidgetTest. You will then need to add the Phidget Library to your project library path. This can be done under Project | Properties | Flex Build Path on the Library tab, by clicking the “Add SWC” button and selecting the Phidget21Library.swc from the Flex examples. As an alternative, you can copy the “com” folder that is located in the Flash AS3 examples into your project source directory and it should work as well.

Coding For Your Phidget

Before you can use the Phidget, you must import references from the library to the device in the main body of code. In MXML and Actionscript 3.0:

  <?xml version="1.0" encoding="utf-8"?>
  <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
  <mx:Script>
     <![CDATA[
        import com.phidgets.PhidgetInterfaceKit;
        import com.phidgets.events.*;
        //More code goes here
     ]]>
  </mx:Script>
  </mx:Application>

Afterwards, the Phidget object will need to be declared and then initialized. For example, a start function is set to execute on initialization, and then an instance of a PhidgetInterfaceKit is declared and then set inside with:

  public var phid:com.phidgets.PhidgetInterfaceKit;
  public function start():void{ 
     phid = new PhidgetInterfaceKit();
     //More code goes here
}

The object name for any type of Phidget is listed in the API manual. Every type of Phidget also inherits functionality from the Phidget base class.

Connecting to the Phidget

Next, the program needs to try and connect to the Phidget through an open call. Open will continuously try to connect to a Phidget, based on the parameters given, even trying to reconnect if it gets disconnected. This means that simply calling open does not guarantee you can use the Phidget immediately. We can handle this by using event driven programming and tracking the AttachEvents and DetachEvents, or checking the isAttached property.

  phid.open("localhost", 5001);

The API manual lists all of the available modes that open provides. In Flash, the parameters can be used to open the first Phidget of a type it can find or based on its serial number. One important thing to remember is that when working with Phidgets, a local connection will reserve the device until closed. This means other programs that open the Phidget may end up preventing other instances from retrieving data. The one connection per device limit does not apply when exclusively using the Phidget WebService, and the Actionscript libraries by extension.

Event Driven Programming

We recommend the use of event driven programming when working with Phidgets. In Actionscript 3.0, we hook an event handler with the following code:

 
  phid.addEventListener(PhidgetDataEvent.SENSOR_CHANGE, onSensorChange);

The onSensorChange method will get executed every time the PhidgetInterfaceKit reports a change on one of its analog inputs. The values from the report can be accessed from the PhidgetDataEvent object.

  private function onSensorChange(evt:PhidgetDataEvent):void{
     //Insert your code here
}

Some events such as Attach and Detach belong to the base Phidget object and thus are common to all types of Phidgets. Please refer to the API manual for a full list of events and their usage.

Working directly with the Phidget

Some values can be directly read and set on the Phidget and used as an alternative to event driven programming. Simply use the instance properties or call member functions such as getSensorValue(index: int) or setOutputState(index: int, val: Boolean) for PhidgetInterfaceKits.

Working with multiple Phidgets

Multiple Phidgets of the same type can easily be run inside the same program. In our case, it requires another PhidgetInterfaceKit instance to be defined and initialized. The new instance can then be set up, opened and used in the same process as the previous one. If the application needs to distinguish between the devices, open can be called with the serial number of a specific Phidget.

Flash Security Settings

During debugging or after publishing the project, you may encounter some difficulties with Flash network security settings either inside or outside of the development environment with Phidgets. Permissions for your project folder can be added through the settings manager at here, under “Always trust files in these locations” | “Edit locations...” | “Add location...”.

Other Phidgets

The design given in this document can also be followed for almost all Phidgets. For example, if you were using a PhidgetRFID instead of an PhidgetInterfacekit, you would declare a RFID object instead of a InterfaceKit object. The methods and events available would change but they can be accessed in a similar manner.