|
|
(227 intermediate revisions by 10 users not shown) |
Line 1: |
Line 1: |
| Phidgets can run on a variety of Operating Systems, and be driven by a variety of Programming Languages.
| | #REDIRECT [[Phidget Programming Basics]] |
| | |
| On [[OS - Windows|Windows]] and [[OS - Mac OSX|Mac OSX]], we provide graphical tools to simply plug a Phidget in to your computer and display basic data to your computer screen.
| |
| | |
| But to '''work directly''' with a Phidget and do things with the data you collect from it, you will need to write code.
| |
| | |
| To control a Phidget via code, you will need:
| |
| #The Phidget '''Core Drivers''' and libraries for [[#Operating System Support|your operating system]]
| |
| #'''Libraries''' for your [[#Language Support|programming language]]
| |
| #'''Example Code''' in your [[#Language Support|programming language]]
| |
| #An '''API Reference''' list of functions you can use in [[#Language Support|your language]]
| |
| #A list of the '''Specific API''' calls you can use with your device, as found on your [[Device Main Page|device page]]
| |
| | |
| | |
| This Software Overview is the first step in writing code to work with your Phidget.
| |
| | |
| Here you can find links to operating system pages with drivers, language pages with code libraries, and references for using Phidgets in each language. After you have the core drivers and language libraries, you can learn more about [[General Phidget Programming|designing code for your Phidget]], as well as read more in-depth into our [[General API]]
| |
| | |
| <br>
| |
| | |
| == Operating System Support ==
| |
| | |
| This section describes how to set up your operating system to write and run code for Phidgets. Getting your operating system ready is the first step toward writing code.
| |
| | |
| On the pages linked below, you will find the '''core drivers''' and libraries for using your Phidget either '''directly''' through USB or '''remotely''' via the [[Phidget Webservice]].
| |
| | |
| The Webservice controls a Phidget by connecting to a host computer and its Phidget over a network. So, the host computer must be able to run a Phidget directly.
| |
| | |
| The Windows and Mac OSX pages also include a graphical program to view and run basic tests on your Phidget. On other operating systems, only code is available, although ready-to-compile examples are provided for [[#Language Support|every language]].
| |
| | |
| Phidgets can '''run directly''' or be '''driven remotely''' on these operating systems:
| |
| | |
| * [[OS - Windows|Windows]]
| |
| * [[OS - Mac OSX|Mac OSX]]
| |
| * [[OS - Linux|Linux]] (Kernel 2.6+)
| |
| * [[OS - Windows CE|Windows CE]] ??
| |
| * [[OS - Android|Android]] (3.1 and above)
| |
| * [[Phidget SBC]]
| |
| | |
| Phidgets can be only '''driven remotely''' by these operating systems:
| |
| | |
| * [[OS - iOS|IPhone iOS]]
| |
| * [[OS - Android|Android]] (3.0 and earlier)
| |
| | |
| == Language Support ==
| |
| | |
| This section outlines what languages you can write code in for Phidgets and how to set them up.
| |
| | |
| We provide a variety of supported languages for using Phidgets. Each language has its own '''libraries''', which may be found on the language pages below. Many languages also depend on the '''core drivers''' being installed as well. These drivers can be found on the [[#Operating System Support|operating system pages]] above.
| |
| | |
| Phidgets can be controlled using our [[#General Phidget Programming|high level API]] which allows for either an [[#Event Driven Code|event driven]] style of code design, or by traditional [[#Logic Code|logic code]]. All languages below support logic code. Most - but not all - languages support our complete API, which includes support for event-driven design.
| |
| | |
| If you are flexible on what language you can use, we suggest choosing a language which supports event-driven code.
| |
| | |
| When you visit the page below for your language, make sure you download:
| |
| #Software libraries
| |
| #Example code
| |
| #The specific API
| |
| | |
| Then, for a high-level introduction to [[#General Phidget Programming|programming Phidgets]], please return here and continue reading.
| |
| | |
| Phidgets have libraries to support '''event driven design''' in the following languages:
| |
| | |
| * [[Language - Adobe Director]]
| |
| * [[Language - Android Java|Android Java]]
| |
| * [[Language - Applescript]]
| |
| * [[Language - AutoIt]]
| |
| * [[Language - C/C++|C/C++]]
| |
| * [[Language - C Sharp|C Sharp]]
| |
| * [[Language - C Sharp (.NET Compact Framework)|C Sharp (.NET)]]
| |
| * [[Language - Cocoa]]
| |
| * [[Language - Cocoa Touch]]
| |
| * [[Language - Delphi]]
| |
| * [[Language - Flash AS3]]
| |
| * [[Language - Flex AS3]]
| |
| * [[Language - iOS]]
| |
| * [[Language - Java|Java]]
| |
| * [[Language - LabVIEW]]
| |
| * [[Language - LiveCode]]
| |
| * [[Language - Max/MSP]]
| |
| * [[Language - Microsoft Robotics Studio]]
| |
| * [[Language - Python|Python]]
| |
| * [[Language - REALBasic]]
| |
| * [[Language - Simulink]]
| |
| * [[Language - Visual Basic .NET]]
| |
| * [[Language - Visual Basic 6.0]]
| |
| * [[Language - Visual Basic for Applications]]
| |
| * [[Language - Visual Basic Script]]
| |
| | |
| | |
| Phidgets have libraries to support only '''logic code''' in the following languages:
| |
| | |
| * [[Language - MATLAB|MATLAB]]
| |
| * ...
| |
| | |
| == Putting It Together ==
| |
| | |
| User and device actions can be handled by either:
| |
| *Letting the program tell you when they happen and then doing something ('''event driven''' code)
| |
| *Polling for things to happen then doing something ('''logic''' code)
| |
| | |
| The style of programming you choose (and hence the language you might prefer) would depend on what you want to do with the Phidget. The two sections, [[#Event Driven Code|Event Driven Code]] and [[#Logic Code|Logic Code]] below give benefits, drawbacks, and general examples of each style.
| |
| | |
| The styles can also mix. For example, you can take a defined set of steps at first such as turning on an LED or antenna (logic code) and then doing nothing until an output change event is fired (event code).
| |
| | |
| With languages that support both styles, you can mix and match. For languages that support only logic code (see the [[#Language Support|Language Support Categories]] above) you can only use the logic paradigm style.
| |
| | |
| Examples in pseudo-code are given below for each style type so you can see how your language choice can affect your code design.
| |
| | |
| === Logic Code ===
| |
| | |
| Logic code has use for:
| |
| * Simple, single-device applications
| |
| * Non-GUI applications (GUIs usually are event driven)
| |
| * The user driving the device rather than listening to it
| |
| | |
| Logic code is relatively easy to design well. For example, using the [[#Creating a Software Object|create]], [[#Opening a Phidget|open]], [[#Attaching a Phidget|attach]], [[#Do Things with the Phidget|do stuff]], and [[#Close the Phidget|close]] concepts introduced above, logic code to handle a Phidget might be written like this:
| |
| | |
| <br>
| |
| | |
| [[File:logic.png]]
| |
| | |
| <br>
| |
| | |
| Although this design does not explicitly capture every event that fires when data or input changes, by polling the device often enough no data will be lost.
| |
| | |
| However, logic code cannot handle constant, asynchronous events as cleanly as [[#Event Driven Code|event driven code]] can.
| |
| | |
| These designs can be mixed. So, if you find that in logic code you have a highly complex <code>if</code> loop driving your program, you should consider changing some of it to event driven code. This type of awkward if-loop might look like this:
| |
| | |
| <div style="background-color: #f3f3f3; border-color: #1c9edb; border-width:1px; border-style: dashed;">
| |
| <font size="3">
| |
| <source lang=c>
| |
| | |
| Create Device Software Object
| |
| Open Device
| |
| | |
| Loop Until Exit Requested {
| |
| if No Device Attached {
| |
| Wait For Attachment until Timeout
| |
| if Wait Timeout Reached {
| |
| break
| |
| } else {
| |
| Initialize Device
| |
| }
| |
| } else { // Device Is Attached
| |
| if Device Data Type 1 Changed {
| |
| Do Something
| |
| }
| |
| if Device Data Type 2 Changed {
| |
| Do Something Else
| |
| }
| |
| // ... More data change functions here
| |
| }
| |
| Collect User Input
| |
| }
| |
| | |
| Close Device
| |
| Delete Device
| |
| | |
| </source>
| |
| </font>
| |
| </div>
| |
| | |
| On the other hand, you can probably see that if your language does not give the option for events, you can use this structure to mimic what events would enable you to do.
| |
| | |
| === Event Driven Code ===
| |
| | |
| Event driven code allows for clean handling of complex, asynchronous programs:
| |
| *Handling multiple Phidgets
| |
| *Handling active plugging or unplugging of the Phidget (multiple attach and detach events)
| |
| *Working behind a GUI, as many GUIs are already event driven
| |
| *Capturing all sensor data - or input and output - without constantly polling
| |
| | |
| ''Without'' event driven code, you will need to constantly poll the device to see if any state has changed. If you poll at a slower rate than your input or output changes, you will not capture all data.
| |
| | |
| However, event driven code is usually not as useful or efficient for:
| |
| *Only one open and close event
| |
| *Using only one device
| |
| *Having the user (or program) ''put'' changes onto the device (in contrast to reading data ''from'' the device)
| |
| | |
| Event driven code is relatively hard to design well. It may help to draw out a '''flowchart''', '''state machine''', or at least a '''pseudo-code outline''' of your system design and all events you wish to handle before writing code.
| |
| | |
| The code examples given for each [[#Language Support|specific language]] use events if they are supported by the language.
| |
| | |
| Using the [[#Creating a Software Object|create]], [[#Opening a Phidget|open]], [[#Attaching a Phidget|attach]], [[#Do Things with the Phidget|do stuff]], and [[#Close the Phidget|close]] concepts introduced above, event code to handle a Phidget might be written like this:
| |
| | |
| [[File:event.png]]
| |