Software Overview: Difference between revisions

From Phidgets Support
Line 19: Line 19:


== Language Support ==
== Language Support ==
To simply plug a Phidget in to your computer and have basic data output to your computer screen, you can use the [[Phidget Manager]] on Windows and Mac OSX.
To work directly with a Phidget and do things with the data you collect from it, you will need to write code.  The languages which you can choose from to directly work with a Phidget are below.


Phidgets can be programmed either by an '''event-driven''' model, or by traditional '''linear code'''.  All languages below support linear code.  Some languages support our complete API, which includes support for event-driven design.
Phidgets can be programmed either by an '''event-driven''' model, or by traditional '''linear code'''.  All languages below support linear code.  Some languages support our complete API, which includes support for event-driven design.

Revision as of 17:25, 2 November 2011

Operating System Support

Phidgets can run directly on these operating systems:

Phidgets can be driven remotely by these operating systems:

Language Support

To simply plug a Phidget in to your computer and have basic data output to your computer screen, you can use the Phidget Manager on Windows and Mac OSX.

To work directly with a Phidget and do things with the data you collect from it, you will need to write code. The languages which you can choose from to directly work with a Phidget are below.

Phidgets can be programmed either by an event-driven model, or by traditional linear code. All languages below support linear code. Some 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.

Phidgets have libraries to support Event Driven Code in the following languages:

Phidgets have libraries to support only Linear Code in the following languages:

General Phidget Programming

Different Code Styles

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 (linear code)

Choosing A Code Style

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 and Linear 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 (linear 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 linear code (see the Language Support Categories above) you can only use the linear 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.

Event Driven Code

Event driven code allows for smooth handling of complex programs. Event driven code is useful for:

  • 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 - device changes

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.

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)

In addition, 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.

All Supported Languages that handle event driven code have examples of how to write such code on the specific language pages. Event driven examples follow this structure, and places to start changing this structure are noted:

  // --- Event Functions ---

  Create any Language-Specific Functions (error handling, etc)

  Create General Attach, Detach, and Error Handling Functions
    On attach: Initialize hardware (antennas, etc)
    On detach: Reset any state variables (attached boolean, etc)

  Create Hardware-Specific Functions
    // ****** Add your own handling code for these data change events ******
    On data input change: Notify using basic output (screen message, turn on LED, etc)

  // --- Main Code ---

  Create Device Software Object
  Attach Event Functions created above to Device
  Open Device

  // ****** Change the look and structure of this loop for your GUI ******
  Loop waiting for events and user input:
    If device attached: 
        Get and Print various device statuses on request via user input
    Exit upon specific user input

  Close Device
  Delete Device


Linear Code

Linear code is often more compact and straightforward compared to event driven code. But, linear code cannot easily handle the same level of complexity that event-driven code can. Linear code is useful for:

  • Simple, single-device applications
  • Non-GUI applications (GUIs usually are event driven)
  • The user driving the device rather than listening to it

Linear code is relatively easy to design well. When the code example in the Event Driven Code section above is translated into linear code, we lose the ability to handle multiple attach and detach events (plugging and unplugging of the Phidget). We also lose the ability to have all of the sensor data or event changes come to us, instead we have to ask for them. However, the code structure is much simpler:

  Create Device Software Object
  Open Device
  Wait for Device Attachment
  Initialize any hardware (antennas, etc)

  Loop waiting for requests from user input:
    Get and Print various device statuses on request by input
    Exit upon specific user input

  Close Device
  Delete Device


If you find that in linear code you have a highly complex if loop driving your program, you should consider switching to event driven code. This type of awkward if-loop might look like this:

  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

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.