Alert.png

Notice: This page contains information for the legacy Phidget21 Library.

Phidget21 is out of support. Bugfixes may be considered on a case by case basis.

Phidget21 does not support VINT Phidgets, or new USB Phidgets released after 2020. We maintain a selection of legacy devices for sale that are supported in Phidget21.

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

Language - Flash AS3: Difference between revisions

From Phidgets Legacy Support
No edit summary
No edit summary
 
(127 intermediate revisions by 5 users not shown)
Line 1: Line 1:
[[Category:Language]]
{{OSLang|[[File:icon-Flash AS3.png|64x64px|link=|alt=]]|Flash, developed by [http://www.adobe.com Adobe Systems] is used to build and deploy dynamic multimedia applications to the Internet.}}
__TOC__
__TOC__


==Introduction==
==Introduction==


{{LanguageSupport|C/C++|the complete Phidget API, including events|all Phidget devices.|Windows (environments include [[#Visual Studio | Visual Studio]], [[#Borland | Borland]], [[#Cygwin/MinGW | Cygwin, and MinGW]]), [[#Mac OS X | Mac OS X]], and [[#Linux | Linux]]|}}
{{LanguageSupport|Flash ActionScript|Phidgets <i>only</i> over the Phidget WebService, and it is unlike the majority of the other programming languages we support where the device can be used without the Phidget WebService. The complete Phidget API, including events are supported|all Phidget devices.|[[#Windows | Windows]] and [[#OS X | OS X]]|
 
Only ActionScript 3 is supported. Interaction with Phidgets is made possible as the library uses web sockets to communicate with Phidgets over the PhidgetWebService.}}


==Quick Downloads==
==Quick Downloads==
Just need the Flash ActionScript documentation, drivers, libraries, and examples?  Here they are:
{{QuickDownloads|Flash ActionScript|
 
{{APIQuickDownloads|{{SERVER}}/documentation/Phidget21Flash_doc.zip ActionScript 3}}
===API Documentation===
{{ExtraAPIQuickDownloads|{{SERVER}}/documentation/web/flashdoc/index.html|HTML Version of}}|
*[http://www.phidgets.com/documentation/Phidget21_C_Doc.zip API Manual]
{{ExampleQuickDownloads|{{SERVER}}/downloads/phidget21/examples/as3/Flash.zip| (same file as ActionScript Libraries below)}}|
 
{{ExtraLibraryQuickDownloads|{{SERVER}}/downloads/phidget21/examples/as3/Flash.zip|ActionScript| (same file as Examples above)}}
===Example Code===
{{WindowsQuickDownloads}}
* [http://www.phidgets.com/downloads/examples/VCpp_2.1.8.20110615.zip Visual Studio 2005/2008/2010 Project Examples]
{{MacQuickDownloads}}}}
* [http://www.phidgets.com/downloads/examples/phidget21-c-examples_2.1.8.20110615.tar.gz Generic Examples]
 
===Libraries and Drivers:===
*[http://www.phidgets.com/downloads/libraries/Phidget-x86_2.1.8.20111220.exe 32 bit Windows Drivers Installer]
*[http://www.phidgets.com/downloads/libraries/Phidget-x64_2.1.8.20111220.exe 64 bit Windows Drivers Installer]
*[http://www.phidgets.com/downloads/libraries/phidget21-x86_2.1.8.20110615.zip 32 bit Windows Drivers and Libraries]
*[http://www.phidgets.com/downloads/libraries/phidget21-x64_2.1.8.20110615.zip 64 bit Windows Drivers and Libraries]
*[http://www.phidgets.com/downloads/libraries/phidget21bcc_2.1.8.20110615.zip Borland Windows Libraries]
*[http://www.phidgets.com/downloads/libraries/Phidget_2.1.8.20111103.dmg Mac OS X Drivers Installer]
*[http://www.phidgets.com/downloads/libraries/libphidget_2.1.8.20111028.tar.gz Linux Libraries (Source Code)]


==Getting started with Flash ActionScript==
==Getting started with Flash ActionScript==
Line 30: Line 24:


Instructions are divided up by operating system. Choose:
Instructions are divided up by operating system. Choose:
*[[#Windows(2000/XP/Vista/7)|Windows 2000 / XP / Vista / 7]]
*[[#Windows (2000/XP/Vista/7)|Windows 2000 / XP / Vista / 7]]
*[[#Mac OS X |Mac OS X]]
*[[#OS X |OS X]]
*[[#Linux | Linux]] (including PhidgetSBC)


==Windows(2000/XP/Vista/7)==
==Windows (XP/Vista/7/8)==


===Description of Library Files===
===Description of Library Files===
Flash ActionScript programs on Windows depend on three files, which the installers in [[#Libraries and Drivers|Quick Downloads]] put onto your system:
Flash ActionScript on Windows depend on the following files and folders. The installers in the [[#Libraries and Drivers | Quick Downloads]] section put only the {{Code|phidget21.dll}} and {{Code|PhidgetWebservice21.exe}} into your system. You will need to manually put the {{Code|com}} folder into your system.
* <b>{{Code|phidget21.dll}}</b> contains the actual Phidget library, which is used at run-time.  By default, it is placed in {{Code|C:\Windows\System32}}.
* <b>{{Code|phidget21.dll}}</b> contains the actual Phidget library, which is used at run-time.  This needs to be installed on the computer that the Phidget is connected. By default, it is placed in {{Code|C:\Windows\System32}}.
* <b>{{Code|phidget21.lib}}</b> is used by your compiler to link to the dll. Your compiler has to know where this file is, by default our installer puts {{Code|phidget21.lib}} into {{Code|C:\Program Files\Phidgets}}, so you can either point your compiler to that location, or copy and link to it in a directory for your project workspace. {{Code|phidget21.lib}} is written to be compatible with most compilers - but your specific compiler may need a different format. Check our documentation for your specific compiler for details. Please note that we provide versions of the {{Code|phidget21.lib}} that are specifically optimized for 32-bit or 64-bit systems. If you are using a 64 bit versions of Windows, the {{Code|phidget21.lib}} is placed in {{Code|C:\Program Files\Phidgets}}; The 32 bit version of {{Code|phidget21.lib}} is placed in {{Code|C:\Program Files\Phidgets\x86}}.  
* <b>{{Code|PhidgetWebservice21.exe}}</b> allows for controlling Phidgets remotely across the network. This needs to be installed on the computer that the Phidget is connected.
* <b>{{Code|phidget21.h}}</b> lists all the Phidget API function calls available to your code. Your compiler also has to know where this file is.  By default, our installer puts {{Code|phidget21.h}} into {{Code|C:\Program Files\Phidgets}} so you can either point your compiler to that location, or copy and link to it in a directory for your project workspace.  
* <b>{{Code|com}}</b> folder is the Phidget ActionScript library. The computer that is used for Flash development will need this folder. It is to be manually placed in the same directory as your project root.


If you do not want to use our installer, you can download all three [http://www.phidgets.com/downloads/libraries/phidget21-x86_2.1.8.20110615.zip files] and manually install them where you want; refer to our [[Manual Installation Instructions | Manual Installation Instructions]].
Unlike the majority of the [[Software_Overview#Language_Support |programming languages]] we support (where applications can directly connect to the Phidgets), Flash can only connect to the Phidgets over the PhidgetWebService. There are potentially three roles that a computer can act as: host, developer, and an end user. It is possible for a single computer to act as more than one of these roles at the same time:


* <b>Host</b>: The computer that the Phidget is attached to, and can broadcast device information to any computer over the network. The {{Code|phidget21.dll}} and {{Code|PhidgetWebservice21.exe}} must be installed on the host. The host must also have the PhidgetWebService started in order for it and other computers to connect to the Phidgets attached to the host.
* <b>Developer</b>: The computer that is used to develop Flash applications. This computer needs the {{Code|com}} folder in the root directory of your project. The {{Code|phidget21.dll}} and {{Code|PhidgetWebservice21.exe}} are only needed if the Phidget is directly attached to the computer.
* <b>End user</b>: The computer that is used to run the compiled flash application (i.e., {{Code|.swf}}).The {{Code|phidget21.dll}} and {{Code|PhidgetWebservice21.exe}} are only needed if the Phidget is directly connected to the computer. If the computer is used for developing Flash applications, then it will need the {{Code|com}} folder in the root directory of your project.


Running the examples and writing your own code can be fairly compiler-specific, so we include instructions for [[#Visual Studio 2005/2008/2010 | Visual Studio 2005/2008/2010]], [[#Visual Studio 2003 | Visual Studio 2003]], [[#Visual Studio C++ 6.0 | Visual Studio 6]], [[#Borland| Borland]], [[#Cygwin/MinGW | Cygwin/MinGW]], and [[#Dev C++ | Dev C++]].
Here is a table summarizing what files/folders are needed for each computer role:


===Flash Professional===
{| style="border:1px solid darkgray;" cellpadding="7px;"
|-style="background: #f0f0f0" align=center
! Computer Role
! {{Code|phidget21.dll}}
! {{Code|Phidget21WebService.dll}}
! {{Code|com}} folder
|-


====Use Our Examples====
|Host
|style="background: #5EC155" align=center|[[Image:Checkmark.png|link=]]
|style="background: #5EC155" align=center|[[Image:Checkmark.png|link=]]
|style="background: #5EC155" align=center|
|-


To run the examples, you first download the [http://www.phidgets.com/downloads/examples/VCpp_2.1.8.20110615.zip examples] and unpack them into a folder.  To load all projects in Visual Studio, go to File &rarr; Open &rarr; Project &rarr; Solution, and open {{Code|Visual Studio Phidgets Examples.sln}} in the {{Code|VCpp}} folder of the examples.
|Developer
|style="background: #5EC155" align=center|
|style="background: #5EC155" align=center|
|style="background: #5EC155" align=center|[[Image:Checkmark.png|link=]]
|-


Since the examples were written in Visual Studio 2005, if you are opening the examples in Visual Studio 2008/2010, you will need to go through the Visual Studio Conversion Wizard to open and convert the 2005 project.
|End User
|style="background: #5EC155" align=center|
|style="background: #5EC155" align=center|
|style="background: #5EC155" align=center|
|-


[[File:VS2005 Conversion Wizard.PNG|link=|alt=Conversion Wizard]]
|}


This will load all of the examples available for Flash ActionScript, and then you can set your main project to be the one that matches your device.  If you aren't sure what the software example for your device is called, check the software object listed in the [[Device List | Getting Started Guide for your Device]].
Please see the [[Phidget WebService | Phidget WebService]] page for a high-level introduction to our WebService.


The only thing left to do is to run the examples! Click on Debug &rarr; Start Debugging. Please note that the projects, by default try to find the {{Code|phidget21.h}} and {{Code|phidget21.lib}} in the {{Code|$(SystemDrive)\Program Files\Phidgets}}. If you have these files installed in another location, please change the path to the file's location accordingly. Please see the [[#Write Your Own Code | Write Your Own Code]] section for details.  
If you do not want to use our installer on Windows, you can download the [{{SERVER}}/downloads/phidget21/libraries/windows/Phidget-x86.zip {{Code|phidget21.dll}}] and manually install them where you want; refer to our [[OS_-_Windows#Manual_File_Installation | Manual Installation Instructions]].


[[File:VS2005 Run.PNG|link=|alt=Run]]
===Flash Professional===


Once you have the Flash ActionScript examples running, we have a [[#Follow the Examples|teaching]] section below to help you follow them.
Adobe Flash professional allows you to develop in ActionScript and control Phidgets over the WebService.  We support ActionScript 3.0.


=====Write Your Own Code=====
====Use Our Examples====


When you are building a project from scratch, or adding Phidget function calls to an existing project, you'll need to configure your development environment to properly link the Phidget ActionScript library. To begin:
This section will assume that the device is plugged into the host computer, and that the development computer has Flash Professional installed.


1. Generate a new Visual C++: Win32 Console Application project with a descriptive name such as PhidgetTest.
As the Flash ActionScript library only supports communication with Phidgets through the PhidgetWebService, begin by [[Phidget WebService#Using The WebService|starting the WebService]] on the host computer with the default port (5001).


[[File:VS2005 New Project.PNG|link=|alt=New Project]]
To run the examples on a development computer, download the [{{SERVER}}/downloads/phidget21/examples/as3/Flash.zip Flash examples] and unpack them into a folder.  Here, you will find a HelloWorld example which is very basic but which will run with any Phidget.  You will also find more in-depth example programs for all devices.  {{FindYourDevice}}


2. Next, select Console Application.
When you have found your example, open that {{Code|.fla}} file in the Adobe Professional Flash environment. The only thing left to do is to run the examples! Click on Control &rarr; Test Movie.  


[[File:VS2005 New Project 2.PNG|link=|alt=New Project]]
[[File:Flash Test Movie.png|link=|alt=Run]]


3. Open the project properties window.
Once you have the Flash ActionScript examples running, we have a [[#Follow the Examples|teaching]] section below to help you follow them.


4. Navigate to Configuration Properties &rarr; C/C++.
You may also run the examples by navigating to Control &rarr; Test Scene. If you are running the examples with Debug &rarr; Debug Movie, you will have to change the Flash Global Security Settings in order for the example to run. More information will be provided about the Flash Global Security Settings in the [[#Running Compiled Code | Running Compiled Code]] section.


5. Add {{Code|"C:\Program Files\Phidgets"}} to the additional directories field. This step will find the {{Code|phidget21.h}} file in the corresponding directory. If the file is placed in another location, please adjust the path  to the file's location accordingly.
====Write Your Own Code====


[[File:VS2005 Header.PNG|link=|alt=Header File]]
When you are building a project from scratch, or adding Phidget function calls to an existing project, you'll need to configure your development environment to properly link the Phidget ActionScript library. To begin:  


6. Navigate to Configuration Properties &rarr; Linker &rarr; Input.
1. Place a copy of the [{{SERVER}}/downloads/phidget21/examples/as3/Flash.zip {{Code|com}}] folder in the root directory of your Flash project.


7. Edit the additional dependencies and add {{Code|"C:\Program  Files\Phidgets\phidget21.lib"}}. This step will find the {{Code|phidget21.lib}} file in the corresponding directory. If the file is placed in another location, please adjust the path  to the file's location accordingly.
2. Generate a new ActionScript 3 Flash file.


[[File:VS2005 Library.PNG|link=|alt=Library File]]
[[File:Flash New Project.png|link=|alt=New Project]]


8. The project now has access to the Phidget function calls and you are ready to begin coding.
3. Then, in your code, you will need to include the Phidget ActionScript library. Navigate to Window &rarr; Actions to bring up the Actions window and enter in the following:
 
Then, in your code, you will need to include the Phidget ActionScript library:


<div class="source">
<div class="source">
<syntaxhighlight lang=cpp>
<syntaxhighlight lang=actionscript>
 
import com.phidgets.*;
  #include <phidget21.h>
import com.phidgets.events.*;
 
</syntaxhighlight>
</syntaxhighlight>
</div>
</div>


The project now has access to the Phidget function calls and you are ready to begin coding.


The same [[#Follow the Examples|teaching]] section which describes the examples also has further resources for programming your Phidget.
The same [[#Follow the Examples|teaching]] section which describes the examples also has further resources for programming your Phidget.


====Running Compiled Code====


==Mac OS X==
Running a compiled {{Code|.swf}} application on an end user computer will prompt the Flash player to display a dialog box mentioning that the application will block all communications with the Internet.


C/C++ has excellent support on Mac OS X through the gcc compiler.
[[File:Flash Player Security Warning.png|link=|alt=Flash Player Security Warning]]


The first step in using C/C++ on Mac is to install the Phidget C/C++ library. Compile and install them as explained on the [[Device List|getting started guide for your device]]. Then, the [[OS - Mac OS X]] page also describes the different Phidget files, their installed locations, and their roles.  
1. Click on the {{Code|Settings}} button to bring up the Flash Global Security Settings Manager in your default web browser. Alternatively, you can access the manager with the following URL: [http://www.macromedia.com/support/documentation/en/flashplayer/help/settings_manager04a.html http://www.macromedia.com/support/documentation/en/flashplayer/help/settings_manager04a.html].


{{ContentNeeded|The information we say that we say on the OS - Mac OS X page (i.e. the different files, locations, and roles) should actually be added there}}
[[File:Flash Global Security Settings 1.png|link=|alt=Flash Global Security Settings]]


===Use Our Examples===
2. In the Global Security Settings tab, navigate to {{Code|Edit locations ...}} &rarr; {{Code|Add locations}}.


After installing the Phidget C/C++ library for Mac OS X as above, you're ready to download the [http://www.phidgets.com/downloads/examples/phidget21-c-examples_2.1.8.20111028.tar.gz examples]. Afterwards, unzip the file. To run the example code, you'll need to find the source code ''for your specific device''.  Then, compile the code under your platform and run it.
[[File:Flash Global Security Settings 2.png|link=|alt=Browse for File/Folder]]


The examples assume that the compiled libraries have been set up properly. To set them up on Mac OS X, follow the ''Getting Started'' page for [[Device List | your specific device]]
3. Then, browse and add the application or the folder containing the application.


To compile, link the Phidget C/c++ library, and build an executable binary on Mac OS X, do (for example, depending on the Headers location):
[[File:Flash Global Security Settings 3.png|link=|alt=Added file to the trusted locations]]


<div class="source">
This will allow the Flash Player to allow the application to accept any communication with the Internet.
<syntaxhighlight lang=bash>


  gcc example.c -o example -framework Phidget21 -I/Library/Frameworks/Phidget21.framework/Headers
==OS X==
</syntaxhighlight>
</div>


Flash ActionScript has excellent support on OS X over the PhidgetWebService.


After using gcc, you will have an executable named {{Code|example}} that you can run.
The first step in using Flash ActionScript on Mac is to install Adobe Flash Professional.


===Write Your Own Code===
Once you have the Flash environment installed, setting up a project is exactly the same as on Widows.  Please refer to the [[#Windows (2000/XP/Vista/7)|Windows]] section for more information on this subject.


When writing your code from scratch, you must include a reference to the library header:
==Follow the Examples==


<div class="source">
By following the instructions for your operating system and compiler above, you probably now have a working example and want to understand it better so you can change it to do what you want.  This [[#Follow the Examples|teaching]] section has resources for you to learn from the examples and write your own.
<syntaxhighlight lang=cpp>


  #include <phidget21.h>
Your main reference for writing ActionScript code will be our ActionScript API information, with syntax for all of our functions:


</syntaxhighlight>
{{UsingAPhidgetInCodeGeneral|both of which are available in C/C++|[{{SERVER}}/documentation/Phidget21Flash_doc.zip ActionScript API]}}
</div>


Then, you would compile your completed C/C++ code the same way as shown in the [[#Use Our Examples 7|Use Our Example]] section above.
===Code Snippets===


To learn how to write your own code for your Phidget, and to learn more about our API, we have a [[#Follow the Examples|teaching]] section to help you follow the provided C/C++ examples.  Even more help and references are provided from there.
Specific calls in ActionScript will differ in syntax from those on the [[General Phidget Programming]] page, but the concepts stay the same.  


==Linux==
It may help to have the [[General Phidget Programming]] page and this section open at the same time, because they parallel each other and you can refer to the ActionScript syntax.  However, ''many'' additional concepts are covered on the General Phidget Programming page on a high level, such as using multiple Phidgets, handling errors, and different styles of programming.  Remember that Actionscript cannot open Phidgets directly - rather, it must use a form of remote open to use the WebService.


C/C++ has support on Linux through the gcc compiler. 
====Step One: Initialize and Open====


The first step in using C/C++ on Linux is to install the Phidget libraries.  Compile and install them as explained on the main [[OS - Linux | Linux page]]. That Linux page also describes the different Phidget files, their installed locations, and their roles.
Before you can use the Phidget, you must include a reference to the library in the action frame (Window | Actions). In Actionscript 3.0, the inclusion code would look like this:


===Use Our Examples===
<div class="source"><syntaxhighlight lang=actionscript3>
import com.phidgets.PhidgetInterfaceKit;
import com.phidgets.events.*;
</syntaxhighlight></div>


After installing the Phidget libraries for Linux as above, you're ready to download and run the examples:
Now you are ready to declare, initialize, and open your Phidget. For example, we can declare and initialize a Phidget Interface Kit with:
*[http://www.phidgets.com/downloads/examples/phidget21-c-examples_2.1.8.20111028.tar.gz Generic C/C++ Examples]


To run the example code, you'll need to download and unpack the examples, and then find the source code for your device. The source file will be named the same as the ''software object'' for your device. If you are not sure what the software object for your device is, it can be found in the Software/API section on the [[#Device List|Product Page for your device]].  Then, compile the code under your platform and run it.  When compiling, you need to link to the Phidget library.
<div class="source"><syntaxhighlight lang=actionscript3>
public var phid:com.phidgets.PhidgetInterfaceKit;
phid = new PhidgetInterfaceKit();
phid.open("localhost", 5001);
</syntaxhighlight></div>


To compile, link the Phidget libraries and build a binary executable on Linux, do the following in a terminal in the directory with {{Code|example.c}}:
The object name for any type of Phidget is listed in the API manual. Every type of Phidget (Interface Kit, Temperature Sensor, Spatial, etc.) also inherits functionality from the Phidget base class.


<div style="background-color: #f3f3f3; border-color: #1c9edb; border-width:1px; border-style: dashed;">
The Open function will continuously try to connect to a Phidget, based on the parameters given, even trying to reconnect if it gets disconnected. The [[Phidget WebService]] as used by ActionScript allows a single Phidget to be opened by multiple applications - this is something that cannot be done with the regular, direct interface.  
<font size="3">
<source lang=bash>
  gcc example.c -o example -lphidget21
</source>
</font>
</div>


In this case, {{Code|example.c}} would be the '''.c''' file specific to your device.  After using gcc, you will have an executable named {{Code|example}} that you can run.
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.


On Linux, if you have not set up [[OS - Linux | your udev rules for USB access]], you will need to run the program '''as root''':
====Step Two: Wait for Attachment (plugging in) of the Phidget====


<div style="background-color: #f3f3f3; border-color: #1c9edb; border-width:1px; border-style: dashed;">
Simply calling open does not guarantee you can use the Phidget immediately. It needs to be plugged in (attached). If it becomes unplugged, it will be 'detached'.  We can handle this by using event driven programming and tracking the AttachEvents and DetachEvents, or checking the isAttached property and waiting until it is true.
<font size="3">
<source lang=bash>


    sudo ./example
Our examples provide code snippets for attach event functions and how to hook them in to the Phidget for use.


</source>
====Step Three: Do Things with the Phidget====
</font>
</div>


===Write Your Own Code===
We recommend the use of [[General Phidget Programming#Event Driven Code|event driven programming]] when working with Phidgets. In Actionscript 3.0, we hook an event handler with the following code:


When writing your code from scratch, you start it as you would any C/C++ code on Linux, such as within a text editor like Emacs, Vi, Gedit, or Kate.  In your '''{{Code|.c}}''' source code file, you must include a reference to the library header:
<div class="source"><syntaxhighlight lang=actionscript3>
phid.addEventListener(PhidgetDataEvent.SENSOR_CHANGE, onSensorChange);
    function onSensorChange(evt:PhidgetDataEvent):void{
    trace (evt.Data); //Echo
}
</syntaxhighlight></div>


<div style="background-color: #f3f3f3; border-color: #1c9edb; border-width:1px; border-style: dashed;">
With this method, the code inside the {{Code|onSensorChange}} function (which you also need to define - check out our examples for ways to do this) will get executed every time the PhidgetInterfaceKit reports a change on one of its analog inputs.
<font size="3">
<source lang=cpp>


  #include <phidget21.h>
The values from the report can be accessed from the PhidgetDataEvent object properties. Some events such as [[#Step Two: Wait for Attachment (plugging in) of the Phidget|Attach and Detach as discussed above]] belong to the base Phidget object and thus are common to all types of Phidgets.  Others, like this one for the analog sensor change on the Interface Kit, are specific to the Phidget board.  Please refer to the API manual for a full list of events and their usage.


</source>
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 {{Code|getSensorValue(index: int)}} or {{Code|setOutputState(index: int, val: Boolean)}} for Phidget Interface Kits, for example.
</font>
</div>


Then, you would compile your completed C/C++ code the same way as the examples above.
====Step Four: Close and Delete====


To learn how to write your own code for your Phidget, and to learn more about our API, we have a [[#Follow the Examples|teaching]] section to help you follow the provided C/C++ examples and which has resources such as the API reference.
Just like the open call from Step One, you can close the Phidget when you are finished with it in your code.


==Follow the Examples==
====Flash Security Settings====


By following the instructions for your operating system and compiler above, you probably now have a working example and want to understand it better so you can change it to do what you want.  This [[#Follow the Examples|teaching]] section has resources for you to learn from the examples and write your own.
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.


Next comes our C/C++ API information, with syntax for all of our functions:
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 http://www.macromedia.com/support/documentation/en/flashplayer/help/settings_manager04a.html], under “Always trust files in these locations” &rarr; “Edit locations...” &rarr; “Add location...”.


{{UsingAPhidgetInCodeGeneral|both of which are available in C/C++|[http://www.phidgets.com/documentation/Phidget21_C_Doc.zip C/C++ API]}}
{{MoreHowTos}}


===Example Flow===
==Common Problems and Solutions/Workarounds==


{{ExamplePseudocode|In C/C++, you can name these '''event''' functions whatever you like.  You will then pass them as function pointers to the Phidget library below in the Main Code section.  This hooks them into the actual events when they occur. <br>
Problem: My compiled application is experiencing the following security error upon launching: "SecurityError: Error #2010: Local-with-filesystem SWF files are not permitted to use sockets".
In the example code, the event functions common to all Phidgets are called things like '''AttachHandler()''' and '''DetachHandler()''', etc.<br><br>
Some event functions will be specific to each device, like when a tag is read on an RFID board, or when a sensor value changes on an Interface Kit.
Other functions are given in the examples to show you more detail on using your Phidget.  For example, '''DeviceInitialize()''' will show what needs to be set up for your Phidget before using it.
|Creating a Phidget software object in C is specific to the Phidget.  For a Phidget Spatial, for example, this would involve creating an object with the {{Code|CPhidgetSpatialHandle}} type, and then initializing it using the {{Code|CPhidgetSpatial_create function}}.  The examples show how to do this and other API functions.<br><br>
Other C calls follow a similar syntax - {{Code|CPhidgetXXX_function}}, where XXX is the name of your device, and function is an action available from the API for your specific Phidget.|
[http://www.phidgets.com/documentation/Phidget21_C_Doc.zip C/C++ API]}}


==Common Problems and Solutions/Workarounds==
[[File:Socket Runtime Error.png|link=|alt=Socket Error]]


Here you can put various frequent problems and our recommended solutions.
Solution: The symptom of this problem is similar to the one that is discussed in steps 1 - 3 of the [[#Running Compiled Code | Running Compiled Code]] section. Please see that section for a remedy. To access the Flash Global Security Settings Manager, go to [http://www.macromedia.com/support/documentation/en/flashplayer/help/settings_manager04a.html http://www.macromedia.com/support/documentation/en/flashplayer/help/settings_manager04a.html].

Latest revision as of 19:11, 6 June 2017

Flash, developed by Adobe Systems is used to build and deploy dynamic multimedia applications to the Internet.

Introduction

If this is your first time working with a Phidget, we suggest starting with the Getting Started page for your specific device. This can be found in the user guide for your device. That page will walk you through installing drivers and libraries for your operating system, and will then bring you back here to use Flash ActionScript specifically.

Flash ActionScript is capable of using Phidgets only over the Phidget WebService, and it is unlike the majority of the other programming languages we support where the device can be used without the Phidget WebService. The complete Phidget API, including events are supported. We also provide example code in Flash ActionScript for all Phidget devices.

Flash ActionScript can be developed with Windows and OS X.

Only ActionScript 3 is supported. Interaction with Phidgets is made possible as the library uses web sockets to communicate with Phidgets over the PhidgetWebService.

You can compare Flash ActionScript with our other supported languages.

Quick Downloads

Just need the Flash ActionScript documentation, drivers, libraries, and examples? Here they are:

Documentation

Example Code

Libraries and Drivers

Getting started with Flash ActionScript

If you are new to writing code for Phidgets, we recommend starting by running, then modifying existing examples. This will allow you to:

  • Make sure your libraries are properly linked
  • Go from source code to a test application as quickly as possible
  • Ensure your Phidget is hooked up properly

Instructions are divided up by operating system. Choose:

Windows (XP/Vista/7/8)

Description of Library Files

Flash ActionScript on Windows depend on the following files and folders. The installers in the Quick Downloads section put only the phidget21.dll and PhidgetWebservice21.exe into your system. You will need to manually put the com folder into your system.

  • phidget21.dll contains the actual Phidget library, which is used at run-time. This needs to be installed on the computer that the Phidget is connected. By default, it is placed in C:\Windows\System32.
  • PhidgetWebservice21.exe allows for controlling Phidgets remotely across the network. This needs to be installed on the computer that the Phidget is connected.
  • com folder is the Phidget ActionScript library. The computer that is used for Flash development will need this folder. It is to be manually placed in the same directory as your project root.

Unlike the majority of the programming languages we support (where applications can directly connect to the Phidgets), Flash can only connect to the Phidgets over the PhidgetWebService. There are potentially three roles that a computer can act as: host, developer, and an end user. It is possible for a single computer to act as more than one of these roles at the same time:

  • Host: The computer that the Phidget is attached to, and can broadcast device information to any computer over the network. The phidget21.dll and PhidgetWebservice21.exe must be installed on the host. The host must also have the PhidgetWebService started in order for it and other computers to connect to the Phidgets attached to the host.
  • Developer: The computer that is used to develop Flash applications. This computer needs the com folder in the root directory of your project. The phidget21.dll and PhidgetWebservice21.exe are only needed if the Phidget is directly attached to the computer.
  • End user: The computer that is used to run the compiled flash application (i.e., .swf).The phidget21.dll and PhidgetWebservice21.exe are only needed if the Phidget is directly connected to the computer. If the computer is used for developing Flash applications, then it will need the com folder in the root directory of your project.

Here is a table summarizing what files/folders are needed for each computer role:

Computer Role phidget21.dll Phidget21WebService.dll com folder
Host Checkmark.png Checkmark.png
Developer Checkmark.png
End User

Please see the Phidget WebService page for a high-level introduction to our WebService.

If you do not want to use our installer on Windows, you can download the phidget21.dll and manually install them where you want; refer to our Manual Installation Instructions.

Flash Professional

Adobe Flash professional allows you to develop in ActionScript and control Phidgets over the WebService. We support ActionScript 3.0.

Use Our Examples

This section will assume that the device is plugged into the host computer, and that the development computer has Flash Professional installed.

As the Flash ActionScript library only supports communication with Phidgets through the PhidgetWebService, begin by starting the WebService on the host computer with the default port (5001).

To run the examples on a development computer, download the Flash examples and unpack them into a folder. Here, you will find a HelloWorld example which is very basic but which will run with any Phidget. You will also find more in-depth example programs for all devices. The source file will be named the same as the software object for your device. If you are not sure what the software object for your device is, find your Phidget on our webpage, and then check the API documentation for it.

When you have found your example, open that .fla file in the Adobe Professional Flash environment. The only thing left to do is to run the examples! Click on Control → Test Movie.

Run

Once you have the Flash ActionScript examples running, we have a teaching section below to help you follow them.

You may also run the examples by navigating to Control → Test Scene. If you are running the examples with Debug → Debug Movie, you will have to change the Flash Global Security Settings in order for the example to run. More information will be provided about the Flash Global Security Settings in the Running Compiled Code section.

Write Your Own Code

When you are building a project from scratch, or adding Phidget function calls to an existing project, you'll need to configure your development environment to properly link the Phidget ActionScript library. To begin:

1. Place a copy of the com folder in the root directory of your Flash project.

2. Generate a new ActionScript 3 Flash file.

New Project

3. Then, in your code, you will need to include the Phidget ActionScript library. Navigate to Window → Actions to bring up the Actions window and enter in the following:

import com.phidgets.*;
import com.phidgets.events.*;

The project now has access to the Phidget function calls and you are ready to begin coding.

The same teaching section which describes the examples also has further resources for programming your Phidget.

Running Compiled Code

Running a compiled .swf application on an end user computer will prompt the Flash player to display a dialog box mentioning that the application will block all communications with the Internet.

Flash Player Security Warning

1. Click on the Settings button to bring up the Flash Global Security Settings Manager in your default web browser. Alternatively, you can access the manager with the following URL: http://www.macromedia.com/support/documentation/en/flashplayer/help/settings_manager04a.html.

Flash Global Security Settings

2. In the Global Security Settings tab, navigate to Edit locations ...Add locations.

Browse for File/Folder

3. Then, browse and add the application or the folder containing the application.

Added file to the trusted locations

This will allow the Flash Player to allow the application to accept any communication with the Internet.

OS X

Flash ActionScript has excellent support on OS X over the PhidgetWebService.

The first step in using Flash ActionScript on Mac is to install Adobe Flash Professional.

Once you have the Flash environment installed, setting up a project is exactly the same as on Widows. Please refer to the Windows section for more information on this subject.

Follow the Examples

By following the instructions for your operating system and compiler above, you probably now have a working example and want to understand it better so you can change it to do what you want. This teaching section has resources for you to learn from the examples and write your own.

Your main reference for writing ActionScript code will be our ActionScript API information, with syntax for all of our functions:

  • ActionScript API (This is the complete set of functions you have available for all Phidgets)
  • Device Specific APIs - The one for your Phidget can be found in its user guide.

To learn the details behind opening, configuring, using, and closing your Phidget, try the General Phidget Programming page. That page also describes using the Phidget in an event-driven manner and in a traditional manner, both of which are available in C/C++.

Code Snippets

Specific calls in ActionScript will differ in syntax from those on the General Phidget Programming page, but the concepts stay the same.  

It may help to have the General Phidget Programming page and this section open at the same time, because they parallel each other and you can refer to the ActionScript syntax.  However, many additional concepts are covered on the General Phidget Programming page on a high level, such as using multiple Phidgets, handling errors, and different styles of programming. Remember that Actionscript cannot open Phidgets directly - rather, it must use a form of remote open to use the WebService.

Step One: Initialize and Open

Before you can use the Phidget, you must include a reference to the library in the action frame (Window | Actions). In Actionscript 3.0, the inclusion code would look like this:

import com.phidgets.PhidgetInterfaceKit;
import com.phidgets.events.*;

Now you are ready to declare, initialize, and open your Phidget. For example, we can declare and initialize a Phidget Interface Kit with:

public var phid:com.phidgets.PhidgetInterfaceKit;
phid = new PhidgetInterfaceKit();
phid.open("localhost", 5001);

The object name for any type of Phidget is listed in the API manual. Every type of Phidget (Interface Kit, Temperature Sensor, Spatial, etc.) also inherits functionality from the Phidget base class.

The Open function will continuously try to connect to a Phidget, based on the parameters given, even trying to reconnect if it gets disconnected. The Phidget WebService as used by ActionScript allows a single Phidget to be opened by multiple applications - this is something that cannot be done with the regular, direct interface.

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.

Step Two: Wait for Attachment (plugging in) of the Phidget

Simply calling open does not guarantee you can use the Phidget immediately. It needs to be plugged in (attached). If it becomes unplugged, it will be 'detached'. We can handle this by using event driven programming and tracking the AttachEvents and DetachEvents, or checking the isAttached property and waiting until it is true.

Our examples provide code snippets for attach event functions and how to hook them in to the Phidget for use.

Step Three: Do Things with the Phidget

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);	
    function onSensorChange(evt:PhidgetDataEvent):void{	
    trace (evt.Data); //Echo	
}

With this method, the code inside the onSensorChange function (which you also need to define - check out our examples for ways to do this) 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 properties. Some events such as Attach and Detach as discussed above belong to the base Phidget object and thus are common to all types of Phidgets. Others, like this one for the analog sensor change on the Interface Kit, are specific to the Phidget board. Please refer to the API manual for a full list of events and their usage.

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 Phidget Interface Kits, for example.

Step Four: Close and Delete

Just like the open call from Step One, you can close the Phidget when you are finished with it in your code.

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 http://www.macromedia.com/support/documentation/en/flashplayer/help/settings_manager04a.html, under “Always trust files in these locations” → “Edit locations...” → “Add location...”.

More How-To's

The General Phidget Programming page gives more information about:

Common Problems and Solutions/Workarounds

Problem: My compiled application is experiencing the following security error upon launching: "SecurityError: Error #2010: Local-with-filesystem SWF files are not permitted to use sockets".

Socket Error

Solution: The symptom of this problem is similar to the one that is discussed in steps 1 - 3 of the Running Compiled Code section. Please see that section for a remedy. To access the Flash Global Security Settings Manager, go to http://www.macromedia.com/support/documentation/en/flashplayer/help/settings_manager04a.html.