<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://www.phidgets.com/docs21/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Mphi</id>
	<title>Phidgets Legacy Support - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://www.phidgets.com/docs21/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Mphi"/>
	<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs21/Special:Contributions/Mphi"/>
	<updated>2026-04-08T21:22:02Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.39.8</generator>
	<entry>
		<id>https://www.phidgets.com/docs21/index.php?title=Language_-_Ruby&amp;diff=19154</id>
		<title>Language - Ruby</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs21/index.php?title=Language_-_Ruby&amp;diff=19154"/>
		<updated>2012-04-20T17:23:06Z</updated>

		<summary type="html">&lt;p&gt;Mphi: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Language]]&lt;br /&gt;
{{OSLang|[[File:Icon-Ruby.png|64x64px|link=|alt=]]|Ruby is an interpreted and object oriented scripting language with simple syntax created by [http://en.wikipedia.org/wiki/Yukihiro_Matsumoto Yukihiro Matsumoto].}}&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
{{LanguageSupport|Ruby|the complete Phidget API, including events|all Phidget devices.|Linux and OS X|}}&lt;br /&gt;
&lt;br /&gt;
{{QuickDownloads|Ruby|&lt;br /&gt;
{{APIQuickDownloads|http://www.phidgets.com/documentation/rubydoc RubyDoc}}|&lt;br /&gt;
{{ExampleQuickDownloads|http://rubygems.org/gems/phidgets-ffi| (@RubyGems)}}|&lt;br /&gt;
{{ExtraLibraryQuickDownloads|http://rubygems.org/gems/phidgets-ffi|Ruby| (@RubyGems)}}&lt;br /&gt;
{{MacQuickDownloads}}&lt;br /&gt;
{{LinuxQuickDownloads}}&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
==Getting started with Ruby==&lt;br /&gt;
&lt;br /&gt;
If you are new to writing code for Phidgets, we recommend starting by running, then modifying existing examples. This will allow you to:&lt;br /&gt;
{{ExampleCodeReasons}}&lt;br /&gt;
&lt;br /&gt;
Instructions for OS X and Linux are similar, so they will be combined into the same section.&lt;br /&gt;
&lt;br /&gt;
The first step in using Ruby on OS X or Linux is to install the Phidget libraries.  Compile and install them as explained on the [[Device List|getting started guide for your device]].&lt;br /&gt;
&lt;br /&gt;
The next step is to install the phidgets-ffi gem. This is the library that allows you to program Phidgets with Ruby. For more information, please see phidgets-ffi at [http://rubygems.org/gems/phidgets-ffi RubyGems] and [https://github.com/kreynolds/phidgets-ffi GitHub].&lt;br /&gt;
&lt;br /&gt;
==Description of Library Files==&lt;br /&gt;
Ruby programs on OS X and Linux depend on the following files, which the installers put into your system. &lt;br /&gt;
&lt;br /&gt;
If you are using OS X, you will need the:&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|Phidget21.framework}}&amp;lt;/b&amp;gt; contains the actual Phidgets library for OS X, which is used at run-time.  &lt;br /&gt;
&lt;br /&gt;
If you are using Linux, you will need the:&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|libphidget21.so}}&amp;lt;/b&amp;gt; contains the actual Phidgets library for Linux, which is used at run-time.&lt;br /&gt;
&lt;br /&gt;
For both OS X and Linux, you will need the phidgets-ffi gem: &lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|phidgets-ffi}}&amp;lt;/b&amp;gt; is the Phidgets gem for Ruby. This gem contains the library as well as example code.&lt;br /&gt;
&lt;br /&gt;
==Installing phidgets-ffi==&lt;br /&gt;
&lt;br /&gt;
The phidgets-ffi gem relies on the [http://rubygems.org/gems/ffi ffi gem]. So, if you do not already have it installed, please do so. A version of ffi 1.0.9 or greater is needed. Type the following to install ffi:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  gem install ffi&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Next, please install the phidgets-ffi gem by typing the following into command line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  gem install phidgets-ffi&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For OS X systems, the gem will be installed into {{Code|/Library/Ruby/Gems/1.x/phidgets-ffi-x.x.x}}.&lt;br /&gt;
&lt;br /&gt;
For typical Linux systems, the gem will be installed into {{Code|var/lib/gems/1.9/gems/phidgets-ffi-x.x.x}}.&lt;br /&gt;
&lt;br /&gt;
===Use Our Examples===&lt;br /&gt;
&lt;br /&gt;
Open a command line terminal and navigate to the phidgets-ffi gem directory. {{Code|cd}} into the {{Code|examples}} folder. Here, you will find all of the examples available for Ruby.  If you aren&#039;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]].&lt;br /&gt;
&lt;br /&gt;
The easiest way to confirm that your environment is set up properly will be to compile and run the {{Code|HelloWorld}} Ruby example.&lt;br /&gt;
&lt;br /&gt;
The only thing left to do is to run the examples! Type the following into command line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  ruby HelloWorld.rb&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This program will detect for devices that are attached/detached on the computer. Go ahead, and attach or detach your devices! Here is an example output:&lt;br /&gt;
&lt;br /&gt;
[[File:Ruby Linux HelloWorld Output.PNG|link=|alt=HelloWorld Output]]&lt;br /&gt;
&lt;br /&gt;
After confirming that the {{Code|HelloWorld}} example is working, you can proceed to run the example for your device. If you aren&#039;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 ensure that you have set your start up project to be the one that matches your device before compiling. &lt;br /&gt;
&lt;br /&gt;
Once you have the Ruby examples running, we have a [[#Follow the Examples|teaching]] section below to help you follow them.&lt;br /&gt;
&lt;br /&gt;
==Write Your Own Code==&lt;br /&gt;
&lt;br /&gt;
When you are building a project from scratch, or adding Phidget function calls to an existing project, you&#039;ll need to configure your environment to properly link the Phidget Ruby libraries.&lt;br /&gt;
&lt;br /&gt;
Simply, add the following two lines to the beginning of any {{Code|.rb}} script to make use of the phidgets-ffi gem&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  require &#039;rubygems&#039;&lt;br /&gt;
  require &#039;phidgets-ffi&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The project now has access to the Phidget21 function calls and you are ready to begin coding.&lt;br /&gt;
&lt;br /&gt;
The same [[#Follow the Examples|teaching ]] section which describes the examples also has further resources for programming your Phidget.&lt;br /&gt;
&lt;br /&gt;
==Follow the Examples==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
Next, comes our API information.  These resources outline the Ruby Phidget functions:&lt;br /&gt;
&lt;br /&gt;
{{UsingAPhidgetInCodeGeneral|both of which are available in Ruby|[http://www.phidgets.com/documentation/rubydoc Ruby API]}}&lt;br /&gt;
&lt;br /&gt;
===Example Flow===&lt;br /&gt;
&lt;br /&gt;
{{ExamplePseudocode|In Ruby, you can name these &#039;&#039;&#039;event&#039;&#039;&#039; 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. &amp;lt;br&amp;gt; &lt;br /&gt;
In the example code, the event functions common to all Phidgets are called things like &#039;&#039;&#039;AttachHandler()&#039;&#039;&#039; and &#039;&#039;&#039;DetachHandler()&#039;&#039;&#039;, etc.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
Other functions are given in the examples to show you more detail on using your Phidget.  For example, &#039;&#039;&#039;DeviceInitialize()&#039;&#039;&#039; will show what needs to be set up for your Phidget before using it.&lt;br /&gt;
|Creating a Phidget software object in Ruby is specific to the Phidget.  For a Phidget Spatial, for example, this would involve creating a {{Code|Spatial}} object.  The examples show how to do this and other API functions.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The object provides device specific methods and properties which are available from the API for your specific Phidget.|&lt;br /&gt;
[http://www.phidgets.com/documentation/rubydoc Ruby API]}}&lt;br /&gt;
&lt;br /&gt;
===Code Snippets===&lt;br /&gt;
&lt;br /&gt;
Specific calls in Ruby will differ in syntax from those on the [[General Phidget Programming]] page, but the concepts stay the same.  &lt;br /&gt;
&lt;br /&gt;
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 Ruby 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.&lt;br /&gt;
&lt;br /&gt;
For example, if we were using a [[Product - 1018 - PhidgetInterfaceKit 8/8/8 | Phidget Interface Kit]] as our device, the general calls would look like this:&lt;br /&gt;
&lt;br /&gt;
====Step One: Initialize and Open====&lt;br /&gt;
&lt;br /&gt;
The Phidget constructor method will need to be called to create the Phidget object. There are two methods or programming a Phidget in Ruby: with and without a block. &lt;br /&gt;
&lt;br /&gt;
=====Block Programming=====&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  Phidgets::InterfaceKit.new do |device|&lt;br /&gt;
    ..&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=====Non Block Programming=====&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device = Phidgets::InterfaceKit.new&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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. In the above example, InterfaceKit represents the PhidgetInterfaceKit. For simplicity in explaining the programming concepts, the remainder of the code snippet section will use the InterfaceKit object.&lt;br /&gt;
&lt;br /&gt;
For the most part, the two methods behave the same, but there are some subtle differences, which will be explained in the next sections.&lt;br /&gt;
&lt;br /&gt;
====Step Two: Wait for Attachment (plugging in) of the Phidget==== &lt;br /&gt;
&lt;br /&gt;
The program needs to try and connect to the Phidget. Options can be added to the InterfaceKit constructor to connect to the first Phidget it finds, based on its serial number, label, or even connect across the network. [http://rubydoc.info/gems/phidgets-ffi/0.1.1/frames Rubydocs API documentation] lists all of the available modes that the constructor provides. &lt;br /&gt;
&lt;br /&gt;
For example, the following will try to connect to the first Phidget it finds:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device= Phidgets::InterfaceKit.new&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following will try to connect to a Phidget over the Phidget WebService with a serial number of {{Code|99999}}, and a server id of {{Code|myserver}}: &lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  options = {:serial_number =&amp;gt; 99999, :server_id =&amp;gt; ‘myserver’, :password =&amp;gt; nil}&lt;br /&gt;
  device= Phidgets::InterfaceKit.new(options)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One important thing to remember is that when working with Phidgets, a local connection will reserve the device until closed. This prevents any other instances from retrieving data from the Phidget, including other programs. The one connection per device limit does not apply when exclusively using the Phidget WebService.&lt;br /&gt;
&lt;br /&gt;
{{Code|Phidgets::InterfaceKit.new}} will tell the program to continuously try to connect to a Phidget, based on the options given(if any), even trying to reconnect if it gets disconnected. &lt;br /&gt;
&lt;br /&gt;
=====Non Block Programming=====&lt;br /&gt;
For the non block programming method, simply calling the constructor does not guarantee you can use the Phidget immediately. We can handle this by using event driven programming and  tracking the {{Code|on_attach}} and {{Code|on_detach}} events, or by calling: {{Code|wait_for_attachment}}. {{Code|wait_for_attachment}} will block indefinitely until a connection is made to the Phidget, or an optional timeout is exceeded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device= Phidgets::InterfaceKit.new&lt;br /&gt;
  device.wait_for_attachment 2000 #halt the program for up 2000 milliseconds or until the Phidget is connected&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Please also remember to call close at the end or the program to free any locks on the Phidget&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device.close&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=====Block Programming=====&lt;br /&gt;
If you are programming inside a block, {{Code|wait_for_attachment}} is automatically called. By default, it will halt the program and try to connect to the Phidget for up to 1000 milliseconds.  Afterwards, the block will yield. Finally, close is not needed as it is automatically called once the block has yield.&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  options = {:timeout =&amp;gt; 2000)&lt;br /&gt;
  Phidgets::InterfaceKit.new(options) do |device|&lt;br /&gt;
    ... &lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Sometimes, it makes more sense to handle the attachment via an event.  This would be in instances where the Phidget is being plugged and unplugged, and you want to handle these incidents.  Or, when you want to use event-driven programming because you have a GUI-driven program.  In these cases, an event-driven code snippet to handle the attachment might look something like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device.on_attach  do |device, obj|&lt;br /&gt;
    puts &amp;quot;Id: #{device.id}&amp;quot;&lt;br /&gt;
    puts &amp;quot;Serial number: #{device.serial_number}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Step Three: Do Things with the Phidget====&lt;br /&gt;
&lt;br /&gt;
You can read data and interact with your Phidget both by polling it for its current state (or to set a state), or by catching events that trigger when the data changes.&lt;br /&gt;
&lt;br /&gt;
For our [[Product - 1018 - PhidgetInterfaceKit 8/8/8 | Phidget Interface Kit]], the polling method of getting the current sensor state and setting an output state looks something like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
&lt;br /&gt;
# Get a data point from Analog Port 0&lt;br /&gt;
puts &amp;quot;Sensor value[0]: #{device.sensors[0].to_i}&amp;quot; &lt;br /&gt;
&lt;br /&gt;
# Set digital output port 0 to be on&lt;br /&gt;
device.outputs[0].state = true&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To catch data changes via events, you would use something like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device.on_sensor_change do |device, input, value, obj|&lt;br /&gt;
    puts &amp;quot;Sensor #{input.index}&#039;s value has changed to #{value}&amp;quot;&lt;br /&gt;
  end &lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Step Four: Close====&lt;br /&gt;
 &lt;br /&gt;
If you are using the block programming method, you do not need to worry about closing the Phidget; it is already taken care of when the block ends. However, if you are using the non block programming method, then you will need ensure that the Phidget is closed. At the end of your program, call {{Code|close}} to free any locks on the Phidget that the Phidget constructor call put in place!&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device.close&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;complete&#039;&#039; set of functions you have available for all Phidgets can be found in the [http://rubydoc.info/gems/phidgets-ffi/0.1.1/frames Ruby API]. You can also find more description on any device-specific function either in the [[Device List|Device API]] page for calls available only on your specific Phidget.&lt;br /&gt;
&lt;br /&gt;
====Raw FFI====&lt;br /&gt;
As an alternative to programming with the method as outlined in this document, you can also program making straight C calls through FFI. Please refer to the files in {{Code|Ruby Gems Directory/phidgets-ffi-x.x.x/lib/phidgets-ffi/ffi}} to see a list of available methods, and the [http://www.phidgets.com/documentation/Phidget21_C_Doc.zip C API] for usage. There are raw ffi examples in {{Code|Ruby Gems directory/phidgets-ffi-x.x.x/examples/raw-ffi}}.&lt;br /&gt;
&lt;br /&gt;
==Common Problems and Solutions/Workarounds==&lt;br /&gt;
&lt;br /&gt;
Here you can put various frequent problems and our recommended solutions.&lt;/div&gt;</summary>
		<author><name>Mphi</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs21/index.php?title=Language_-_Ruby&amp;diff=19130</id>
		<title>Language - Ruby</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs21/index.php?title=Language_-_Ruby&amp;diff=19130"/>
		<updated>2012-04-20T15:20:53Z</updated>

		<summary type="html">&lt;p&gt;Mphi: /* Step Four: Close */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Language]]&lt;br /&gt;
[[File:Icon-Ruby.png|64x64px||link=|alt=]]Ruby is an interpreted and object oriented scripting language with simple syntax created by [http://en.wikipedia.org/wiki/Yukihiro_Matsumoto Yukihiro Matsumoto].&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
{{LanguageSupport|Ruby|the complete Phidget API, including events|all Phidget devices.|Linux and OS X|}}&lt;br /&gt;
&lt;br /&gt;
{{QuickDownloads|Ruby|&lt;br /&gt;
{{APIQuickDownloads|http://www.phidgets.com/documentation/rubydoc RubyDoc}}|&lt;br /&gt;
{{ExampleQuickDownloads|http://rubygems.org/gems/phidgets-ffi| (@RubyGems)}}|&lt;br /&gt;
{{ExtraLibraryQuickDownloads|http://rubygems.org/gems/phidgets-ffi|Ruby| (@RubyGems)}}&lt;br /&gt;
{{MacQuickDownloads}}&lt;br /&gt;
{{LinuxQuickDownloads}}&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
==Getting started with Ruby==&lt;br /&gt;
&lt;br /&gt;
If you are new to writing code for Phidgets, we recommend starting by running, then modifying existing examples. This will allow you to:&lt;br /&gt;
{{ExampleCodeReasons}}&lt;br /&gt;
&lt;br /&gt;
Instructions for OS X and Linux are similar, so they will be combined into the same section.&lt;br /&gt;
&lt;br /&gt;
The first step in using Ruby on Mac or Linux is to install the Phidget libraries.  Compile and install them as explained on the [[Device List|getting started guide for your device]].&lt;br /&gt;
&lt;br /&gt;
The next step is to install the phidgets-ffi gem. This is the library that allows you to program Phidgets with Ruby. For more information, please see phidgets-ffi at [http://rubygems.org/gems/phidgets-ffi RubyGems] and [https://github.com/kreynolds/phidgets-ffi GitHub].&lt;br /&gt;
&lt;br /&gt;
==Description of Library Files==&lt;br /&gt;
Ruby programs on OS X and Linux depend on the following files, which the installers put into your system. &lt;br /&gt;
&lt;br /&gt;
If you are using OS X, you will need the:&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|Phidget21.framework}}&amp;lt;/b&amp;gt; contains the actual Phidgets library for OS X, which is used at run-time.  &lt;br /&gt;
&lt;br /&gt;
If you are using Linux, you will need the:&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|libphidget21.so}}&amp;lt;/b&amp;gt; contains the actual Phidgets library for Linux, which is used at run-time.&lt;br /&gt;
&lt;br /&gt;
For both OS X and Linux, you will need the phidgets-ffi gem: &lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|phidgets-ffi}}&amp;lt;/b&amp;gt; is the Phidgets gem for Ruby. This gem contains the library as well as example code.&lt;br /&gt;
&lt;br /&gt;
==Installing phidgets-ffi==&lt;br /&gt;
&lt;br /&gt;
The phidgets-ffi gem relies on the [http://rubygems.org/gems/ffi ffi gem]. So, if you do not already have it installed, please do so. A version of ffi 1.0.9 or greater is needed. Type the following to install ffi:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  gem install ffi&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Next, please install the phidgets-ffi gem by typing the following into command line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  gem install phidgets-ffi&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For OS X systems, the gem will be installed into {{Code|/Library/Ruby/Gems/1.x/phidgets-ffi-x.x.x}}.&lt;br /&gt;
&lt;br /&gt;
For typical Linux systems, the gem will be installed into {{Code|var/lib/gems/1.9/gems/phidgets-ffi-x.x.x}}.&lt;br /&gt;
&lt;br /&gt;
===Use Our Examples===&lt;br /&gt;
&lt;br /&gt;
Open a command line terminal and navigate to the phidgets-ffi gem directory. {{Code|cd}} into the {{Code|examples}} folder. Here, you will find all of the examples available for Ruby.  If you aren&#039;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]].&lt;br /&gt;
&lt;br /&gt;
The easiest way to confirm that your environment is set up properly will be to compile and run the {{Code|HelloWorld}} Ruby example.&lt;br /&gt;
&lt;br /&gt;
The only thing left to do is to run the examples! Type the following into command line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  ruby HelloWorld.rb&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This program will detect for devices that are attached/detached on the computer. Go ahead, and attach or detach your devices! Here is an example output:&lt;br /&gt;
&lt;br /&gt;
[[File:Ruby Linux HelloWorld Output.PNG|link=|alt=HelloWorld Output]]&lt;br /&gt;
&lt;br /&gt;
After confirming that the {{Code|HelloWorld}} example is working, you can proceed to run the example for your device. If you aren&#039;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 ensure that you have set your start up project to be the one that matches your device before compiling. &lt;br /&gt;
&lt;br /&gt;
Once you have the Ruby examples running, we have a [[#Follow the Examples|teaching]] section below to help you follow them.&lt;br /&gt;
&lt;br /&gt;
==Write Your Own Code==&lt;br /&gt;
&lt;br /&gt;
When you are building a project from scratch, or adding Phidget function calls to an existing project, you&#039;ll need to configure your environment to properly link the Phidget Ruby libraries.&lt;br /&gt;
&lt;br /&gt;
Simply, add the following two lines to the beginning of any {{Code|.rb}} script to make use of the phidgets-ffi gem&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  require &#039;rubygems&#039;&lt;br /&gt;
  require &#039;phidgets-ffi&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The project now has access to the Phidget21 function calls and you are ready to begin coding.&lt;br /&gt;
&lt;br /&gt;
The same [[#Follow the Examples|teaching ]] section which describes the examples also has further resources for programming your Phidget.&lt;br /&gt;
&lt;br /&gt;
==Follow the Examples==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
Next, comes our API information.  These resources outline the Ruby Phidget functions:&lt;br /&gt;
&lt;br /&gt;
{{UsingAPhidgetInCodeGeneral|both of which are available in Ruby|[http://www.phidgets.com/documentation/rubydoc Ruby API]}}&lt;br /&gt;
&lt;br /&gt;
===Example Flow===&lt;br /&gt;
&lt;br /&gt;
{{ExamplePseudocode|In Ruby, you can name these &#039;&#039;&#039;event&#039;&#039;&#039; 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. &amp;lt;br&amp;gt; &lt;br /&gt;
In the example code, the event functions common to all Phidgets are called things like &#039;&#039;&#039;AttachHandler()&#039;&#039;&#039; and &#039;&#039;&#039;DetachHandler()&#039;&#039;&#039;, etc.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
Other functions are given in the examples to show you more detail on using your Phidget.  For example, &#039;&#039;&#039;DeviceInitialize()&#039;&#039;&#039; will show what needs to be set up for your Phidget before using it.&lt;br /&gt;
|Creating a Phidget software object in Ruby is specific to the Phidget.  For a Phidget Spatial, for example, this would involve creating a {{Code|Spatial}} object.  The examples show how to do this and other API functions.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The object provides device specific methods and properties which are available from the API for your specific Phidget.|&lt;br /&gt;
[http://www.phidgets.com/documentation/rubydoc Ruby API]}}&lt;br /&gt;
&lt;br /&gt;
===Code Snippets===&lt;br /&gt;
&lt;br /&gt;
Specific calls in Ruby will differ in syntax from those on the [[General Phidget Programming]] page, but the concepts stay the same.  &lt;br /&gt;
&lt;br /&gt;
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 Ruby 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.&lt;br /&gt;
&lt;br /&gt;
For example, if we were using a [[Product - 1018 - PhidgetInterfaceKit 8/8/8 | Phidget Interface Kit]] as our device, the general calls would look like this:&lt;br /&gt;
&lt;br /&gt;
====Step One: Initialize and Open====&lt;br /&gt;
&lt;br /&gt;
The Phidget constructor method will need to be called to create the Phidget object. There are two methods or programming a Phidget in Ruby: with and without a block. &lt;br /&gt;
&lt;br /&gt;
=====Block Programming=====&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  Phidgets::InterfaceKit.new do |device|&lt;br /&gt;
    ..&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=====Non Block Programming=====&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device = Phidgets::InterfaceKit.new&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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. In the above example, InterfaceKit represents the PhidgetInterfaceKit. For simplicity in explaining the programming concepts, the remainder of the code snippet section will use the InterfaceKit object.&lt;br /&gt;
&lt;br /&gt;
For the most part, the two methods behave the same, but there are some subtle differences, which will be explained in the next sections.&lt;br /&gt;
&lt;br /&gt;
====Step Two: Wait for Attachment (plugging in) of the Phidget==== &lt;br /&gt;
&lt;br /&gt;
The program needs to try and connect to the Phidget. Options can be added to the InterfaceKit constructor to connect to the first Phidget it finds, based on its serial number, label, or even connect across the network. [http://rubydoc.info/gems/phidgets-ffi/0.1.1/frames Rubydocs API documentation] lists all of the available modes that the constructor provides. &lt;br /&gt;
&lt;br /&gt;
For example, the following will try to connect to the first Phidget it finds:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device= Phidgets::InterfaceKit.new&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following will try to connect to a Phidget over the Phidget WebService with a serial number of {{Code|99999}}, and a server id of {{Code|myserver}}: &lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  options = {:serial_number =&amp;gt; 99999, :server_id =&amp;gt; ‘myserver’, :password =&amp;gt; nil}&lt;br /&gt;
  device= Phidgets::InterfaceKit.new(options)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One important thing to remember is that when working with Phidgets, a local connection will reserve the device until closed. This prevents any other instances from retrieving data from the Phidget, including other programs. The one connection per device limit does not apply when exclusively using the Phidget WebService.&lt;br /&gt;
&lt;br /&gt;
{{Code|Phidgets::InterfaceKit.new}} will tell the program to continuously try to connect to a Phidget, based on the options given(if any), even trying to reconnect if it gets disconnected. &lt;br /&gt;
&lt;br /&gt;
=====Non Block Programming=====&lt;br /&gt;
For the non block programming method, simply calling the constructor does not guarantee you can use the Phidget immediately. We can handle this by using event driven programming and  tracking the {{Code|on_attach}} and {{Code|on_detach}} events, or by calling: {{Code|wait_for_attachment}}. {{Code|wait_for_attachment}} will block indefinitely until a connection is made to the Phidget, or an optional timeout is exceeded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device= Phidgets::InterfaceKit.new&lt;br /&gt;
  device.wait_for_attachment 2000 #halt the program for up 2000 milliseconds or until the Phidget is connected&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Please also remember to call close at the end or the program to free any locks on the Phidget&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device.close&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=====Block Programming=====&lt;br /&gt;
If you are programming inside a block, {{Code|wait_for_attachment}} is automatically called. By default, it will halt the program and try to connect to the Phidget for up to 1000 milliseconds.  Afterwards, the block will yield. Finally, close is not needed as it is automatically called once the block has yield.&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  options = {:timeout =&amp;gt; 2000)&lt;br /&gt;
  Phidgets::InterfaceKit.new(options) do |device|&lt;br /&gt;
    ... &lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Sometimes, it makes more sense to handle the attachment via an event.  This would be in instances where the Phidget is being plugged and unplugged, and you want to handle these incidents.  Or, when you want to use event-driven programming because you have a GUI-driven program.  In these cases, an event-driven code snippet to handle the attachment might look something like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device.on_attach  do |device, obj|&lt;br /&gt;
    puts &amp;quot;Id: #{device.id}&amp;quot;&lt;br /&gt;
    puts &amp;quot;Serial number: #{device.serial_number}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Step Three: Do Things with the Phidget====&lt;br /&gt;
&lt;br /&gt;
You can read data and interact with your Phidget both by polling it for its current state (or to set a state), or by catching events that trigger when the data changes.&lt;br /&gt;
&lt;br /&gt;
For our [[Product - 1018 - PhidgetInterfaceKit 8/8/8 | Phidget Interface Kit]], the polling method of getting the current sensor state and setting an output state looks something like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
&lt;br /&gt;
# Get a data point from Analog Port 0&lt;br /&gt;
puts &amp;quot;Sensor value[0]: #{device.sensors[0].to_i}&amp;quot; &lt;br /&gt;
&lt;br /&gt;
# Set digital output port 0 to be on&lt;br /&gt;
device.outputs[0].state = true&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To catch data changes via events, you would use something like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device.on_sensor_change do |device, input, value, obj|&lt;br /&gt;
    puts &amp;quot;Sensor #{input.index}&#039;s value has changed to #{value}&amp;quot;&lt;br /&gt;
  end &lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Step Four: Close====&lt;br /&gt;
 &lt;br /&gt;
If you are using the block programming method, you do not need to worry about closing the Phidget; it is already taken care of when the block ends. However, if you are using the non block programming method, then you will need take special consideration that the Phidget is closed. &lt;br /&gt;
&lt;br /&gt;
At the end of your program, don’t forget to call {{Code|close}} to free any locks on the Phidget that the Phidget constructor call put in place!&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device.close&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;complete&#039;&#039; set of functions you have available for all Phidgets can be found in the [http://rubydoc.info/gems/phidgets-ffi/0.1.1/frames Ruby API]. You can also find more description on any device-specific function either in the [[Device List|Device API]] page for calls available only on your specific Phidget.&lt;br /&gt;
&lt;br /&gt;
====Raw FFI====&lt;br /&gt;
As an alternative to programming with the method as outlined in this document, you can also program making straight C calls through FFI. Please refer to the files in {{Code|Ruby Gems Directory/phidgets-ffi-x.x.x/lib/phidgets-ffi/ffi}} to see a list of available methods, and the [http://www.phidgets.com/documentation/Phidget21_C_Doc.zip C API] for usage. There are raw ffi examples in {{Code|Ruby Gems directory/phidgets-ffi-x.x.x/examples/raw-ffi}}.&lt;br /&gt;
&lt;br /&gt;
==Common Problems and Solutions/Workarounds==&lt;br /&gt;
&lt;br /&gt;
Here you can put various frequent problems and our recommended solutions.&lt;/div&gt;</summary>
		<author><name>Mphi</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs21/index.php?title=Language_-_Ruby&amp;diff=19129</id>
		<title>Language - Ruby</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs21/index.php?title=Language_-_Ruby&amp;diff=19129"/>
		<updated>2012-04-20T15:19:51Z</updated>

		<summary type="html">&lt;p&gt;Mphi: /* Non Block Programming */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Language]]&lt;br /&gt;
[[File:Icon-Ruby.png|64x64px||link=|alt=]]Ruby is an interpreted and object oriented scripting language with simple syntax created by [http://en.wikipedia.org/wiki/Yukihiro_Matsumoto Yukihiro Matsumoto].&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
{{LanguageSupport|Ruby|the complete Phidget API, including events|all Phidget devices.|Linux and OS X|}}&lt;br /&gt;
&lt;br /&gt;
{{QuickDownloads|Ruby|&lt;br /&gt;
{{APIQuickDownloads|http://www.phidgets.com/documentation/rubydoc RubyDoc}}|&lt;br /&gt;
{{ExampleQuickDownloads|http://rubygems.org/gems/phidgets-ffi| (@RubyGems)}}|&lt;br /&gt;
{{ExtraLibraryQuickDownloads|http://rubygems.org/gems/phidgets-ffi|Ruby| (@RubyGems)}}&lt;br /&gt;
{{MacQuickDownloads}}&lt;br /&gt;
{{LinuxQuickDownloads}}&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
==Getting started with Ruby==&lt;br /&gt;
&lt;br /&gt;
If you are new to writing code for Phidgets, we recommend starting by running, then modifying existing examples. This will allow you to:&lt;br /&gt;
{{ExampleCodeReasons}}&lt;br /&gt;
&lt;br /&gt;
Instructions for OS X and Linux are similar, so they will be combined into the same section.&lt;br /&gt;
&lt;br /&gt;
The first step in using Ruby on Mac or Linux is to install the Phidget libraries.  Compile and install them as explained on the [[Device List|getting started guide for your device]].&lt;br /&gt;
&lt;br /&gt;
The next step is to install the phidgets-ffi gem. This is the library that allows you to program Phidgets with Ruby. For more information, please see phidgets-ffi at [http://rubygems.org/gems/phidgets-ffi RubyGems] and [https://github.com/kreynolds/phidgets-ffi GitHub].&lt;br /&gt;
&lt;br /&gt;
==Description of Library Files==&lt;br /&gt;
Ruby programs on OS X and Linux depend on the following files, which the installers put into your system. &lt;br /&gt;
&lt;br /&gt;
If you are using OS X, you will need the:&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|Phidget21.framework}}&amp;lt;/b&amp;gt; contains the actual Phidgets library for OS X, which is used at run-time.  &lt;br /&gt;
&lt;br /&gt;
If you are using Linux, you will need the:&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|libphidget21.so}}&amp;lt;/b&amp;gt; contains the actual Phidgets library for Linux, which is used at run-time.&lt;br /&gt;
&lt;br /&gt;
For both OS X and Linux, you will need the phidgets-ffi gem: &lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|phidgets-ffi}}&amp;lt;/b&amp;gt; is the Phidgets gem for Ruby. This gem contains the library as well as example code.&lt;br /&gt;
&lt;br /&gt;
==Installing phidgets-ffi==&lt;br /&gt;
&lt;br /&gt;
The phidgets-ffi gem relies on the [http://rubygems.org/gems/ffi ffi gem]. So, if you do not already have it installed, please do so. A version of ffi 1.0.9 or greater is needed. Type the following to install ffi:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  gem install ffi&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Next, please install the phidgets-ffi gem by typing the following into command line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  gem install phidgets-ffi&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For OS X systems, the gem will be installed into {{Code|/Library/Ruby/Gems/1.x/phidgets-ffi-x.x.x}}.&lt;br /&gt;
&lt;br /&gt;
For typical Linux systems, the gem will be installed into {{Code|var/lib/gems/1.9/gems/phidgets-ffi-x.x.x}}.&lt;br /&gt;
&lt;br /&gt;
===Use Our Examples===&lt;br /&gt;
&lt;br /&gt;
Open a command line terminal and navigate to the phidgets-ffi gem directory. {{Code|cd}} into the {{Code|examples}} folder. Here, you will find all of the examples available for Ruby.  If you aren&#039;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]].&lt;br /&gt;
&lt;br /&gt;
The easiest way to confirm that your environment is set up properly will be to compile and run the {{Code|HelloWorld}} Ruby example.&lt;br /&gt;
&lt;br /&gt;
The only thing left to do is to run the examples! Type the following into command line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  ruby HelloWorld.rb&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This program will detect for devices that are attached/detached on the computer. Go ahead, and attach or detach your devices! Here is an example output:&lt;br /&gt;
&lt;br /&gt;
[[File:Ruby Linux HelloWorld Output.PNG|link=|alt=HelloWorld Output]]&lt;br /&gt;
&lt;br /&gt;
After confirming that the {{Code|HelloWorld}} example is working, you can proceed to run the example for your device. If you aren&#039;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 ensure that you have set your start up project to be the one that matches your device before compiling. &lt;br /&gt;
&lt;br /&gt;
Once you have the Ruby examples running, we have a [[#Follow the Examples|teaching]] section below to help you follow them.&lt;br /&gt;
&lt;br /&gt;
==Write Your Own Code==&lt;br /&gt;
&lt;br /&gt;
When you are building a project from scratch, or adding Phidget function calls to an existing project, you&#039;ll need to configure your environment to properly link the Phidget Ruby libraries.&lt;br /&gt;
&lt;br /&gt;
Simply, add the following two lines to the beginning of any {{Code|.rb}} script to make use of the phidgets-ffi gem&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  require &#039;rubygems&#039;&lt;br /&gt;
  require &#039;phidgets-ffi&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The project now has access to the Phidget21 function calls and you are ready to begin coding.&lt;br /&gt;
&lt;br /&gt;
The same [[#Follow the Examples|teaching ]] section which describes the examples also has further resources for programming your Phidget.&lt;br /&gt;
&lt;br /&gt;
==Follow the Examples==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
Next, comes our API information.  These resources outline the Ruby Phidget functions:&lt;br /&gt;
&lt;br /&gt;
{{UsingAPhidgetInCodeGeneral|both of which are available in Ruby|[http://www.phidgets.com/documentation/rubydoc Ruby API]}}&lt;br /&gt;
&lt;br /&gt;
===Example Flow===&lt;br /&gt;
&lt;br /&gt;
{{ExamplePseudocode|In Ruby, you can name these &#039;&#039;&#039;event&#039;&#039;&#039; 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. &amp;lt;br&amp;gt; &lt;br /&gt;
In the example code, the event functions common to all Phidgets are called things like &#039;&#039;&#039;AttachHandler()&#039;&#039;&#039; and &#039;&#039;&#039;DetachHandler()&#039;&#039;&#039;, etc.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
Other functions are given in the examples to show you more detail on using your Phidget.  For example, &#039;&#039;&#039;DeviceInitialize()&#039;&#039;&#039; will show what needs to be set up for your Phidget before using it.&lt;br /&gt;
|Creating a Phidget software object in Ruby is specific to the Phidget.  For a Phidget Spatial, for example, this would involve creating a {{Code|Spatial}} object.  The examples show how to do this and other API functions.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The object provides device specific methods and properties which are available from the API for your specific Phidget.|&lt;br /&gt;
[http://www.phidgets.com/documentation/rubydoc Ruby API]}}&lt;br /&gt;
&lt;br /&gt;
===Code Snippets===&lt;br /&gt;
&lt;br /&gt;
Specific calls in Ruby will differ in syntax from those on the [[General Phidget Programming]] page, but the concepts stay the same.  &lt;br /&gt;
&lt;br /&gt;
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 Ruby 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.&lt;br /&gt;
&lt;br /&gt;
For example, if we were using a [[Product - 1018 - PhidgetInterfaceKit 8/8/8 | Phidget Interface Kit]] as our device, the general calls would look like this:&lt;br /&gt;
&lt;br /&gt;
====Step One: Initialize and Open====&lt;br /&gt;
&lt;br /&gt;
The Phidget constructor method will need to be called to create the Phidget object. There are two methods or programming a Phidget in Ruby: with and without a block. &lt;br /&gt;
&lt;br /&gt;
=====Block Programming=====&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  Phidgets::InterfaceKit.new do |device|&lt;br /&gt;
    ..&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=====Non Block Programming=====&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device = Phidgets::InterfaceKit.new&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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. In the above example, InterfaceKit represents the PhidgetInterfaceKit. For simplicity in explaining the programming concepts, the remainder of the code snippet section will use the InterfaceKit object.&lt;br /&gt;
&lt;br /&gt;
For the most part, the two methods behave the same, but there are some subtle differences, which will be explained in the next sections.&lt;br /&gt;
&lt;br /&gt;
====Step Two: Wait for Attachment (plugging in) of the Phidget==== &lt;br /&gt;
&lt;br /&gt;
The program needs to try and connect to the Phidget. Options can be added to the InterfaceKit constructor to connect to the first Phidget it finds, based on its serial number, label, or even connect across the network. [http://rubydoc.info/gems/phidgets-ffi/0.1.1/frames Rubydocs API documentation] lists all of the available modes that the constructor provides. &lt;br /&gt;
&lt;br /&gt;
For example, the following will try to connect to the first Phidget it finds:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device= Phidgets::InterfaceKit.new&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following will try to connect to a Phidget over the Phidget WebService with a serial number of {{Code|99999}}, and a server id of {{Code|myserver}}: &lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  options = {:serial_number =&amp;gt; 99999, :server_id =&amp;gt; ‘myserver’, :password =&amp;gt; nil}&lt;br /&gt;
  device= Phidgets::InterfaceKit.new(options)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One important thing to remember is that when working with Phidgets, a local connection will reserve the device until closed. This prevents any other instances from retrieving data from the Phidget, including other programs. The one connection per device limit does not apply when exclusively using the Phidget WebService.&lt;br /&gt;
&lt;br /&gt;
{{Code|Phidgets::InterfaceKit.new}} will tell the program to continuously try to connect to a Phidget, based on the options given(if any), even trying to reconnect if it gets disconnected. &lt;br /&gt;
&lt;br /&gt;
=====Non Block Programming=====&lt;br /&gt;
For the non block programming method, simply calling the constructor does not guarantee you can use the Phidget immediately. We can handle this by using event driven programming and  tracking the {{Code|on_attach}} and {{Code|on_detach}} events, or by calling: {{Code|wait_for_attachment}}. {{Code|wait_for_attachment}} will block indefinitely until a connection is made to the Phidget, or an optional timeout is exceeded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device= Phidgets::InterfaceKit.new&lt;br /&gt;
  device.wait_for_attachment 2000 #halt the program for up 2000 milliseconds or until the Phidget is connected&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Please also remember to call close at the end or the program to free any locks on the Phidget&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device.close&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=====Block Programming=====&lt;br /&gt;
If you are programming inside a block, {{Code|wait_for_attachment}} is automatically called. By default, it will halt the program and try to connect to the Phidget for up to 1000 milliseconds.  Afterwards, the block will yield. Finally, close is not needed as it is automatically called once the block has yield.&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  options = {:timeout =&amp;gt; 2000)&lt;br /&gt;
  Phidgets::InterfaceKit.new(options) do |device|&lt;br /&gt;
    ... &lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Sometimes, it makes more sense to handle the attachment via an event.  This would be in instances where the Phidget is being plugged and unplugged, and you want to handle these incidents.  Or, when you want to use event-driven programming because you have a GUI-driven program.  In these cases, an event-driven code snippet to handle the attachment might look something like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device.on_attach  do |device, obj|&lt;br /&gt;
    puts &amp;quot;Id: #{device.id}&amp;quot;&lt;br /&gt;
    puts &amp;quot;Serial number: #{device.serial_number}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Step Three: Do Things with the Phidget====&lt;br /&gt;
&lt;br /&gt;
You can read data and interact with your Phidget both by polling it for its current state (or to set a state), or by catching events that trigger when the data changes.&lt;br /&gt;
&lt;br /&gt;
For our [[Product - 1018 - PhidgetInterfaceKit 8/8/8 | Phidget Interface Kit]], the polling method of getting the current sensor state and setting an output state looks something like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
&lt;br /&gt;
# Get a data point from Analog Port 0&lt;br /&gt;
puts &amp;quot;Sensor value[0]: #{device.sensors[0].to_i}&amp;quot; &lt;br /&gt;
&lt;br /&gt;
# Set digital output port 0 to be on&lt;br /&gt;
device.outputs[0].state = true&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To catch data changes via events, you would use something like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device.on_sensor_change do |device, input, value, obj|&lt;br /&gt;
    puts &amp;quot;Sensor #{input.index}&#039;s value has changed to #{value}&amp;quot;&lt;br /&gt;
  end &lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Step Four: Close====&lt;br /&gt;
 &lt;br /&gt;
If you are using the block programming method, you do not need to worry about closing the Phidget. It is already taken care of when the block ends. However, if you are using the non block programming method, then you will need take special consideration that the Phidget is closed. &lt;br /&gt;
&lt;br /&gt;
At the end of your program, don’t forget to call {{Code|close}} to free any locks on the Phidget that the Phidget constructor call put in place!&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device.close&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;complete&#039;&#039; set of functions you have available for all Phidgets can be found in the [http://rubydoc.info/gems/phidgets-ffi/0.1.1/frames Ruby API]. You can also find more description on any device-specific function either in the [[Device List|Device API]] page for calls available only on your specific Phidget.&lt;br /&gt;
&lt;br /&gt;
====Raw FFI====&lt;br /&gt;
As an alternative to programming with the method as outlined in this document, you can also program making straight C calls through FFI. Please refer to the files in {{Code|Ruby Gems Directory/phidgets-ffi-x.x.x/lib/phidgets-ffi/ffi}} to see a list of available methods, and the [http://www.phidgets.com/documentation/Phidget21_C_Doc.zip C API] for usage. There are raw ffi examples in {{Code|Ruby Gems directory/phidgets-ffi-x.x.x/examples/raw-ffi}}.&lt;br /&gt;
&lt;br /&gt;
==Common Problems and Solutions/Workarounds==&lt;br /&gt;
&lt;br /&gt;
Here you can put various frequent problems and our recommended solutions.&lt;/div&gt;</summary>
		<author><name>Mphi</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs21/index.php?title=Language_-_Ruby&amp;diff=19128</id>
		<title>Language - Ruby</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs21/index.php?title=Language_-_Ruby&amp;diff=19128"/>
		<updated>2012-04-20T15:18:14Z</updated>

		<summary type="html">&lt;p&gt;Mphi: /* Step One: Initialize and Open */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Language]]&lt;br /&gt;
[[File:Icon-Ruby.png|64x64px||link=|alt=]]Ruby is an interpreted and object oriented scripting language with simple syntax created by [http://en.wikipedia.org/wiki/Yukihiro_Matsumoto Yukihiro Matsumoto].&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
{{LanguageSupport|Ruby|the complete Phidget API, including events|all Phidget devices.|Linux and OS X|}}&lt;br /&gt;
&lt;br /&gt;
{{QuickDownloads|Ruby|&lt;br /&gt;
{{APIQuickDownloads|http://www.phidgets.com/documentation/rubydoc RubyDoc}}|&lt;br /&gt;
{{ExampleQuickDownloads|http://rubygems.org/gems/phidgets-ffi| (@RubyGems)}}|&lt;br /&gt;
{{ExtraLibraryQuickDownloads|http://rubygems.org/gems/phidgets-ffi|Ruby| (@RubyGems)}}&lt;br /&gt;
{{MacQuickDownloads}}&lt;br /&gt;
{{LinuxQuickDownloads}}&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
==Getting started with Ruby==&lt;br /&gt;
&lt;br /&gt;
If you are new to writing code for Phidgets, we recommend starting by running, then modifying existing examples. This will allow you to:&lt;br /&gt;
{{ExampleCodeReasons}}&lt;br /&gt;
&lt;br /&gt;
Instructions for OS X and Linux are similar, so they will be combined into the same section.&lt;br /&gt;
&lt;br /&gt;
The first step in using Ruby on Mac or Linux is to install the Phidget libraries.  Compile and install them as explained on the [[Device List|getting started guide for your device]].&lt;br /&gt;
&lt;br /&gt;
The next step is to install the phidgets-ffi gem. This is the library that allows you to program Phidgets with Ruby. For more information, please see phidgets-ffi at [http://rubygems.org/gems/phidgets-ffi RubyGems] and [https://github.com/kreynolds/phidgets-ffi GitHub].&lt;br /&gt;
&lt;br /&gt;
==Description of Library Files==&lt;br /&gt;
Ruby programs on OS X and Linux depend on the following files, which the installers put into your system. &lt;br /&gt;
&lt;br /&gt;
If you are using OS X, you will need the:&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|Phidget21.framework}}&amp;lt;/b&amp;gt; contains the actual Phidgets library for OS X, which is used at run-time.  &lt;br /&gt;
&lt;br /&gt;
If you are using Linux, you will need the:&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|libphidget21.so}}&amp;lt;/b&amp;gt; contains the actual Phidgets library for Linux, which is used at run-time.&lt;br /&gt;
&lt;br /&gt;
For both OS X and Linux, you will need the phidgets-ffi gem: &lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|phidgets-ffi}}&amp;lt;/b&amp;gt; is the Phidgets gem for Ruby. This gem contains the library as well as example code.&lt;br /&gt;
&lt;br /&gt;
==Installing phidgets-ffi==&lt;br /&gt;
&lt;br /&gt;
The phidgets-ffi gem relies on the [http://rubygems.org/gems/ffi ffi gem]. So, if you do not already have it installed, please do so. A version of ffi 1.0.9 or greater is needed. Type the following to install ffi:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  gem install ffi&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Next, please install the phidgets-ffi gem by typing the following into command line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  gem install phidgets-ffi&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For OS X systems, the gem will be installed into {{Code|/Library/Ruby/Gems/1.x/phidgets-ffi-x.x.x}}.&lt;br /&gt;
&lt;br /&gt;
For typical Linux systems, the gem will be installed into {{Code|var/lib/gems/1.9/gems/phidgets-ffi-x.x.x}}.&lt;br /&gt;
&lt;br /&gt;
===Use Our Examples===&lt;br /&gt;
&lt;br /&gt;
Open a command line terminal and navigate to the phidgets-ffi gem directory. {{Code|cd}} into the {{Code|examples}} folder. Here, you will find all of the examples available for Ruby.  If you aren&#039;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]].&lt;br /&gt;
&lt;br /&gt;
The easiest way to confirm that your environment is set up properly will be to compile and run the {{Code|HelloWorld}} Ruby example.&lt;br /&gt;
&lt;br /&gt;
The only thing left to do is to run the examples! Type the following into command line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  ruby HelloWorld.rb&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This program will detect for devices that are attached/detached on the computer. Go ahead, and attach or detach your devices! Here is an example output:&lt;br /&gt;
&lt;br /&gt;
[[File:Ruby Linux HelloWorld Output.PNG|link=|alt=HelloWorld Output]]&lt;br /&gt;
&lt;br /&gt;
After confirming that the {{Code|HelloWorld}} example is working, you can proceed to run the example for your device. If you aren&#039;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 ensure that you have set your start up project to be the one that matches your device before compiling. &lt;br /&gt;
&lt;br /&gt;
Once you have the Ruby examples running, we have a [[#Follow the Examples|teaching]] section below to help you follow them.&lt;br /&gt;
&lt;br /&gt;
==Write Your Own Code==&lt;br /&gt;
&lt;br /&gt;
When you are building a project from scratch, or adding Phidget function calls to an existing project, you&#039;ll need to configure your environment to properly link the Phidget Ruby libraries.&lt;br /&gt;
&lt;br /&gt;
Simply, add the following two lines to the beginning of any {{Code|.rb}} script to make use of the phidgets-ffi gem&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  require &#039;rubygems&#039;&lt;br /&gt;
  require &#039;phidgets-ffi&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The project now has access to the Phidget21 function calls and you are ready to begin coding.&lt;br /&gt;
&lt;br /&gt;
The same [[#Follow the Examples|teaching ]] section which describes the examples also has further resources for programming your Phidget.&lt;br /&gt;
&lt;br /&gt;
==Follow the Examples==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
Next, comes our API information.  These resources outline the Ruby Phidget functions:&lt;br /&gt;
&lt;br /&gt;
{{UsingAPhidgetInCodeGeneral|both of which are available in Ruby|[http://www.phidgets.com/documentation/rubydoc Ruby API]}}&lt;br /&gt;
&lt;br /&gt;
===Example Flow===&lt;br /&gt;
&lt;br /&gt;
{{ExamplePseudocode|In Ruby, you can name these &#039;&#039;&#039;event&#039;&#039;&#039; 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. &amp;lt;br&amp;gt; &lt;br /&gt;
In the example code, the event functions common to all Phidgets are called things like &#039;&#039;&#039;AttachHandler()&#039;&#039;&#039; and &#039;&#039;&#039;DetachHandler()&#039;&#039;&#039;, etc.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
Other functions are given in the examples to show you more detail on using your Phidget.  For example, &#039;&#039;&#039;DeviceInitialize()&#039;&#039;&#039; will show what needs to be set up for your Phidget before using it.&lt;br /&gt;
|Creating a Phidget software object in Ruby is specific to the Phidget.  For a Phidget Spatial, for example, this would involve creating a {{Code|Spatial}} object.  The examples show how to do this and other API functions.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The object provides device specific methods and properties which are available from the API for your specific Phidget.|&lt;br /&gt;
[http://www.phidgets.com/documentation/rubydoc Ruby API]}}&lt;br /&gt;
&lt;br /&gt;
===Code Snippets===&lt;br /&gt;
&lt;br /&gt;
Specific calls in Ruby will differ in syntax from those on the [[General Phidget Programming]] page, but the concepts stay the same.  &lt;br /&gt;
&lt;br /&gt;
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 Ruby 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.&lt;br /&gt;
&lt;br /&gt;
For example, if we were using a [[Product - 1018 - PhidgetInterfaceKit 8/8/8 | Phidget Interface Kit]] as our device, the general calls would look like this:&lt;br /&gt;
&lt;br /&gt;
====Step One: Initialize and Open====&lt;br /&gt;
&lt;br /&gt;
The Phidget constructor method will need to be called to create the Phidget object. There are two methods or programming a Phidget in Ruby: with and without a block. &lt;br /&gt;
&lt;br /&gt;
=====Block Programming=====&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  Phidgets::InterfaceKit.new do |device|&lt;br /&gt;
    ..&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=====Non Block Programming=====&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device = Phidgets::InterfaceKit.new&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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. In the above example, InterfaceKit represents the PhidgetInterfaceKit. For simplicity in explaining the programming concepts, the remainder of the code snippet section will use the InterfaceKit object.&lt;br /&gt;
&lt;br /&gt;
For the most part, the two methods behave the same, but there are some subtle differences, which will be explained in the next section.&lt;br /&gt;
&lt;br /&gt;
====Step Two: Wait for Attachment (plugging in) of the Phidget==== &lt;br /&gt;
&lt;br /&gt;
The program needs to try and connect to the Phidget. Options can be added to the InterfaceKit constructor to connect to the first Phidget it finds, based on its serial number, label, or even connect across the network. [http://rubydoc.info/gems/phidgets-ffi/0.1.1/frames Rubydocs API documentation] lists all of the available modes that the constructor provides. &lt;br /&gt;
&lt;br /&gt;
For example, the following will try to connect to the first Phidget it finds:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device= Phidgets::InterfaceKit.new&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following will try to connect to a Phidget over the Phidget WebService with a serial number of {{Code|99999}}, and a server id of {{Code|myserver}}: &lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  options = {:serial_number =&amp;gt; 99999, :server_id =&amp;gt; ‘myserver’, :password =&amp;gt; nil}&lt;br /&gt;
  device= Phidgets::InterfaceKit.new(options)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One important thing to remember is that when working with Phidgets, a local connection will reserve the device until closed. This prevents any other instances from retrieving data from the Phidget, including other programs. The one connection per device limit does not apply when exclusively using the Phidget WebService.&lt;br /&gt;
&lt;br /&gt;
{{Code|Phidgets::InterfaceKit.new}} will tell the program to continuously try to connect to a Phidget, based on the options given(if any), even trying to reconnect if it gets disconnected. &lt;br /&gt;
&lt;br /&gt;
=====Non Block Programming=====&lt;br /&gt;
For the non block programming method, simply calling the constructor does not guarantee you can use the Phidget immediately. We can handle this by using event driven programming and  tracking the {{Code|on_attach}} and {{Code|on_detach}} events, or by calling: {{Code|wait_for_attachment}}. {{Code|wait_for_attachment}} will block indefinitely until a connection is made to the Phidget, or an optional timeout is exceeded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device= Phidgets::InterfaceKit.new&lt;br /&gt;
  device.wait_for_attachment 2000 #halt the program for up 2000 milliseconds or until the Phidget is connected&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Please also remember to call close at the end or the program to free any locks on the Phidget&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device.close&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=====Block Programming=====&lt;br /&gt;
If you are programming inside a block, {{Code|wait_for_attachment}} is automatically called. By default, it will halt the program and try to connect to the Phidget for up to 1000 milliseconds.  Afterwards, the block will yield. Finally, close is not needed as it is automatically called once the block has yield.&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  options = {:timeout =&amp;gt; 2000)&lt;br /&gt;
  Phidgets::InterfaceKit.new(options) do |device|&lt;br /&gt;
    ... &lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Sometimes, it makes more sense to handle the attachment via an event.  This would be in instances where the Phidget is being plugged and unplugged, and you want to handle these incidents.  Or, when you want to use event-driven programming because you have a GUI-driven program.  In these cases, an event-driven code snippet to handle the attachment might look something like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device.on_attach  do |device, obj|&lt;br /&gt;
    puts &amp;quot;Id: #{device.id}&amp;quot;&lt;br /&gt;
    puts &amp;quot;Serial number: #{device.serial_number}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Step Three: Do Things with the Phidget====&lt;br /&gt;
&lt;br /&gt;
You can read data and interact with your Phidget both by polling it for its current state (or to set a state), or by catching events that trigger when the data changes.&lt;br /&gt;
&lt;br /&gt;
For our [[Product - 1018 - PhidgetInterfaceKit 8/8/8 | Phidget Interface Kit]], the polling method of getting the current sensor state and setting an output state looks something like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
&lt;br /&gt;
# Get a data point from Analog Port 0&lt;br /&gt;
puts &amp;quot;Sensor value[0]: #{device.sensors[0].to_i}&amp;quot; &lt;br /&gt;
&lt;br /&gt;
# Set digital output port 0 to be on&lt;br /&gt;
device.outputs[0].state = true&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To catch data changes via events, you would use something like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device.on_sensor_change do |device, input, value, obj|&lt;br /&gt;
    puts &amp;quot;Sensor #{input.index}&#039;s value has changed to #{value}&amp;quot;&lt;br /&gt;
  end &lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Step Four: Close====&lt;br /&gt;
 &lt;br /&gt;
If you are using the block programming method, you do not need to worry about closing the Phidget. It is already taken care of when the block ends. However, if you are using the non block programming method, then you will need take special consideration that the Phidget is closed. &lt;br /&gt;
&lt;br /&gt;
At the end of your program, don’t forget to call {{Code|close}} to free any locks on the Phidget that the Phidget constructor call put in place!&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device.close&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;complete&#039;&#039; set of functions you have available for all Phidgets can be found in the [http://rubydoc.info/gems/phidgets-ffi/0.1.1/frames Ruby API]. You can also find more description on any device-specific function either in the [[Device List|Device API]] page for calls available only on your specific Phidget.&lt;br /&gt;
&lt;br /&gt;
====Raw FFI====&lt;br /&gt;
As an alternative to programming with the method as outlined in this document, you can also program making straight C calls through FFI. Please refer to the files in {{Code|Ruby Gems Directory/phidgets-ffi-x.x.x/lib/phidgets-ffi/ffi}} to see a list of available methods, and the [http://www.phidgets.com/documentation/Phidget21_C_Doc.zip C API] for usage. There are raw ffi examples in {{Code|Ruby Gems directory/phidgets-ffi-x.x.x/examples/raw-ffi}}.&lt;br /&gt;
&lt;br /&gt;
==Common Problems and Solutions/Workarounds==&lt;br /&gt;
&lt;br /&gt;
Here you can put various frequent problems and our recommended solutions.&lt;/div&gt;</summary>
		<author><name>Mphi</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs21/index.php?title=Language_-_Ruby&amp;diff=19125</id>
		<title>Language - Ruby</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs21/index.php?title=Language_-_Ruby&amp;diff=19125"/>
		<updated>2012-04-20T15:14:04Z</updated>

		<summary type="html">&lt;p&gt;Mphi: /* Raw FFI */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Language]]&lt;br /&gt;
[[File:Icon-Ruby.png|64x64px||link=|alt=]]Ruby is an interpreted and object oriented scripting language with simple syntax created by [http://en.wikipedia.org/wiki/Yukihiro_Matsumoto Yukihiro Matsumoto].&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
{{LanguageSupport|Ruby|the complete Phidget API, including events|all Phidget devices.|Linux and OS X|}}&lt;br /&gt;
&lt;br /&gt;
{{QuickDownloads|Ruby|&lt;br /&gt;
{{APIQuickDownloads|http://www.phidgets.com/documentation/rubydoc RubyDoc}}|&lt;br /&gt;
{{ExampleQuickDownloads|http://rubygems.org/gems/phidgets-ffi| (@RubyGems)}}|&lt;br /&gt;
{{ExtraLibraryQuickDownloads|http://rubygems.org/gems/phidgets-ffi|Ruby| (@RubyGems)}}&lt;br /&gt;
{{MacQuickDownloads}}&lt;br /&gt;
{{LinuxQuickDownloads}}&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
==Getting started with Ruby==&lt;br /&gt;
&lt;br /&gt;
If you are new to writing code for Phidgets, we recommend starting by running, then modifying existing examples. This will allow you to:&lt;br /&gt;
{{ExampleCodeReasons}}&lt;br /&gt;
&lt;br /&gt;
Instructions for OS X and Linux are similar, so they will be combined into the same section.&lt;br /&gt;
&lt;br /&gt;
The first step in using Ruby on Mac or Linux is to install the Phidget libraries.  Compile and install them as explained on the [[Device List|getting started guide for your device]].&lt;br /&gt;
&lt;br /&gt;
The next step is to install the phidgets-ffi gem. This is the library that allows you to program Phidgets with Ruby. For more information, please see phidgets-ffi at [http://rubygems.org/gems/phidgets-ffi RubyGems] and [https://github.com/kreynolds/phidgets-ffi GitHub].&lt;br /&gt;
&lt;br /&gt;
==Description of Library Files==&lt;br /&gt;
Ruby programs on OS X and Linux depend on the following files, which the installers put into your system. &lt;br /&gt;
&lt;br /&gt;
If you are using OS X, you will need the:&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|Phidget21.framework}}&amp;lt;/b&amp;gt; contains the actual Phidgets library for OS X, which is used at run-time.  &lt;br /&gt;
&lt;br /&gt;
If you are using Linux, you will need the:&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|libphidget21.so}}&amp;lt;/b&amp;gt; contains the actual Phidgets library for Linux, which is used at run-time.&lt;br /&gt;
&lt;br /&gt;
For both OS X and Linux, you will need the phidgets-ffi gem: &lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|phidgets-ffi}}&amp;lt;/b&amp;gt; is the Phidgets gem for Ruby. This gem contains the library as well as example code.&lt;br /&gt;
&lt;br /&gt;
==Installing phidgets-ffi==&lt;br /&gt;
&lt;br /&gt;
The phidgets-ffi gem relies on the [http://rubygems.org/gems/ffi ffi gem]. So, if you do not already have it installed, please do so. A version of ffi 1.0.9 or greater is needed. Type the following to install ffi:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  gem install ffi&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Next, please install the phidgets-ffi gem by typing the following into command line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  gem install phidgets-ffi&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For OS X systems, the gem will be installed into {{Code|/Library/Ruby/Gems/1.x/phidgets-ffi-x.x.x}}.&lt;br /&gt;
&lt;br /&gt;
For typical Linux systems, the gem will be installed into {{Code|var/lib/gems/1.9/gems/phidgets-ffi-x.x.x}}.&lt;br /&gt;
&lt;br /&gt;
===Use Our Examples===&lt;br /&gt;
&lt;br /&gt;
Open a command line terminal and navigate to the phidgets-ffi gem directory. {{Code|cd}} into the {{Code|examples}} folder. Here, you will find all of the examples available for Ruby.  If you aren&#039;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]].&lt;br /&gt;
&lt;br /&gt;
The easiest way to confirm that your environment is set up properly will be to compile and run the {{Code|HelloWorld}} Ruby example.&lt;br /&gt;
&lt;br /&gt;
The only thing left to do is to run the examples! Type the following into command line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  ruby HelloWorld.rb&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This program will detect for devices that are attached/detached on the computer. Go ahead, and attach or detach your devices! Here is an example output:&lt;br /&gt;
&lt;br /&gt;
[[File:Ruby Linux HelloWorld Output.PNG|link=|alt=HelloWorld Output]]&lt;br /&gt;
&lt;br /&gt;
After confirming that the {{Code|HelloWorld}} example is working, you can proceed to run the example for your device. If you aren&#039;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 ensure that you have set your start up project to be the one that matches your device before compiling. &lt;br /&gt;
&lt;br /&gt;
Once you have the Ruby examples running, we have a [[#Follow the Examples|teaching]] section below to help you follow them.&lt;br /&gt;
&lt;br /&gt;
==Write Your Own Code==&lt;br /&gt;
&lt;br /&gt;
When you are building a project from scratch, or adding Phidget function calls to an existing project, you&#039;ll need to configure your environment to properly link the Phidget Ruby libraries.&lt;br /&gt;
&lt;br /&gt;
Simply, add the following two lines to the beginning of any {{Code|.rb}} script to make use of the phidgets-ffi gem&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  require &#039;rubygems&#039;&lt;br /&gt;
  require &#039;phidgets-ffi&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The project now has access to the Phidget21 function calls and you are ready to begin coding.&lt;br /&gt;
&lt;br /&gt;
The same [[#Follow the Examples|teaching ]] section which describes the examples also has further resources for programming your Phidget.&lt;br /&gt;
&lt;br /&gt;
==Follow the Examples==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
Next, comes our API information.  These resources outline the Ruby Phidget functions:&lt;br /&gt;
&lt;br /&gt;
{{UsingAPhidgetInCodeGeneral|both of which are available in Ruby|[http://www.phidgets.com/documentation/rubydoc Ruby API]}}&lt;br /&gt;
&lt;br /&gt;
===Example Flow===&lt;br /&gt;
&lt;br /&gt;
{{ExamplePseudocode|In Ruby, you can name these &#039;&#039;&#039;event&#039;&#039;&#039; 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. &amp;lt;br&amp;gt; &lt;br /&gt;
In the example code, the event functions common to all Phidgets are called things like &#039;&#039;&#039;AttachHandler()&#039;&#039;&#039; and &#039;&#039;&#039;DetachHandler()&#039;&#039;&#039;, etc.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
Other functions are given in the examples to show you more detail on using your Phidget.  For example, &#039;&#039;&#039;DeviceInitialize()&#039;&#039;&#039; will show what needs to be set up for your Phidget before using it.&lt;br /&gt;
|Creating a Phidget software object in Ruby is specific to the Phidget.  For a Phidget Spatial, for example, this would involve creating a {{Code|Spatial}} object.  The examples show how to do this and other API functions.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The object provides device specific methods and properties which are available from the API for your specific Phidget.|&lt;br /&gt;
[http://www.phidgets.com/documentation/rubydoc Ruby API]}}&lt;br /&gt;
&lt;br /&gt;
===Code Snippets===&lt;br /&gt;
&lt;br /&gt;
Specific calls in Ruby will differ in syntax from those on the [[General Phidget Programming]] page, but the concepts stay the same.  &lt;br /&gt;
&lt;br /&gt;
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 Ruby 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.&lt;br /&gt;
&lt;br /&gt;
For example, if we were using a [[Product - 1018 - PhidgetInterfaceKit 8/8/8 | Phidget Interface Kit]] as our device, the general calls would look like this:&lt;br /&gt;
&lt;br /&gt;
====Step One: Initialize and Open====&lt;br /&gt;
&lt;br /&gt;
The Phidget constructor method will need to be called to create the Phidget object. There are two methods or programming a Phidget in Ruby: with and without a block. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  # Without a block&lt;br /&gt;
    device = Phidgets::InterfaceKit.new&lt;br /&gt;
&lt;br /&gt;
  # With a block&lt;br /&gt;
    Phidgets::InterfaceKit.new do |device|&lt;br /&gt;
      ..&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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. In the above example, InterfaceKit represents the PhidgetInterfaceKit. For simplicity in explaining the programming concepts, the remainder of the code snippet section will use the InterfaceKit object.&lt;br /&gt;
&lt;br /&gt;
For the most part, the two methods behave the same, but there are some subtle differences, which will be explained in the next section.&lt;br /&gt;
&lt;br /&gt;
====Step Two: Wait for Attachment (plugging in) of the Phidget==== &lt;br /&gt;
&lt;br /&gt;
The program needs to try and connect to the Phidget. Options can be added to the InterfaceKit constructor to connect to the first Phidget it finds, based on its serial number, label, or even connect across the network. [http://rubydoc.info/gems/phidgets-ffi/0.1.1/frames Rubydocs API documentation] lists all of the available modes that the constructor provides. &lt;br /&gt;
&lt;br /&gt;
For example, the following will try to connect to the first Phidget it finds:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device= Phidgets::InterfaceKit.new&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following will try to connect to a Phidget over the Phidget WebService with a serial number of {{Code|99999}}, and a server id of {{Code|myserver}}: &lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  options = {:serial_number =&amp;gt; 99999, :server_id =&amp;gt; ‘myserver’, :password =&amp;gt; nil}&lt;br /&gt;
  device= Phidgets::InterfaceKit.new(options)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One important thing to remember is that when working with Phidgets, a local connection will reserve the device until closed. This prevents any other instances from retrieving data from the Phidget, including other programs. The one connection per device limit does not apply when exclusively using the Phidget WebService.&lt;br /&gt;
&lt;br /&gt;
{{Code|Phidgets::InterfaceKit.new}} will tell the program to continuously try to connect to a Phidget, based on the options given(if any), even trying to reconnect if it gets disconnected. &lt;br /&gt;
&lt;br /&gt;
=====Non Block Programming=====&lt;br /&gt;
For the non block programming method, simply calling the constructor does not guarantee you can use the Phidget immediately. We can handle this by using event driven programming and  tracking the {{Code|on_attach}} and {{Code|on_detach}} events, or by calling: {{Code|wait_for_attachment}}. {{Code|wait_for_attachment}} will block indefinitely until a connection is made to the Phidget, or an optional timeout is exceeded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device= Phidgets::InterfaceKit.new&lt;br /&gt;
  device.wait_for_attachment 2000 #halt the program for up 2000 milliseconds or until the Phidget is connected&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Please also remember to call close at the end or the program to free any locks on the Phidget&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device.close&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=====Block Programming=====&lt;br /&gt;
If you are programming inside a block, {{Code|wait_for_attachment}} is automatically called. By default, it will halt the program and try to connect to the Phidget for up to 1000 milliseconds.  Afterwards, the block will yield. Finally, close is not needed as it is automatically called once the block has yield.&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  options = {:timeout =&amp;gt; 2000)&lt;br /&gt;
  Phidgets::InterfaceKit.new(options) do |device|&lt;br /&gt;
    ... &lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Sometimes, it makes more sense to handle the attachment via an event.  This would be in instances where the Phidget is being plugged and unplugged, and you want to handle these incidents.  Or, when you want to use event-driven programming because you have a GUI-driven program.  In these cases, an event-driven code snippet to handle the attachment might look something like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device.on_attach  do |device, obj|&lt;br /&gt;
    puts &amp;quot;Id: #{device.id}&amp;quot;&lt;br /&gt;
    puts &amp;quot;Serial number: #{device.serial_number}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Step Three: Do Things with the Phidget====&lt;br /&gt;
&lt;br /&gt;
You can read data and interact with your Phidget both by polling it for its current state (or to set a state), or by catching events that trigger when the data changes.&lt;br /&gt;
&lt;br /&gt;
For our [[Product - 1018 - PhidgetInterfaceKit 8/8/8 | Phidget Interface Kit]], the polling method of getting the current sensor state and setting an output state looks something like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
&lt;br /&gt;
# Get a data point from Analog Port 0&lt;br /&gt;
puts &amp;quot;Sensor value[0]: #{device.sensors[0].to_i}&amp;quot; &lt;br /&gt;
&lt;br /&gt;
# Set digital output port 0 to be on&lt;br /&gt;
device.outputs[0].state = true&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To catch data changes via events, you would use something like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device.on_sensor_change do |device, input, value, obj|&lt;br /&gt;
    puts &amp;quot;Sensor #{input.index}&#039;s value has changed to #{value}&amp;quot;&lt;br /&gt;
  end &lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Step Four: Close====&lt;br /&gt;
 &lt;br /&gt;
If you are using the block programming method, you do not need to worry about closing the Phidget. It is already taken care of when the block ends. However, if you are using the non block programming method, then you will need take special consideration that the Phidget is closed. &lt;br /&gt;
&lt;br /&gt;
At the end of your program, don’t forget to call {{Code|close}} to free any locks on the Phidget that the Phidget constructor call put in place!&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device.close&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;complete&#039;&#039; set of functions you have available for all Phidgets can be found in the [http://rubydoc.info/gems/phidgets-ffi/0.1.1/frames Ruby API]. You can also find more description on any device-specific function either in the [[Device List|Device API]] page for calls available only on your specific Phidget.&lt;br /&gt;
&lt;br /&gt;
====Raw FFI====&lt;br /&gt;
As an alternative to programming with the method as outlined in this document, you can also program making straight C calls through FFI. Please refer to the files in {{Code|Ruby Gems Directory/phidgets-ffi-x.x.x/lib/phidgets-ffi/ffi}} to see a list of available methods, and the [http://www.phidgets.com/documentation/Phidget21_C_Doc.zip C API] for usage. There are raw ffi examples in {{Code|Ruby Gems directory/phidgets-ffi-x.x.x/examples/raw-ffi}}.&lt;br /&gt;
&lt;br /&gt;
==Common Problems and Solutions/Workarounds==&lt;br /&gt;
&lt;br /&gt;
Here you can put various frequent problems and our recommended solutions.&lt;/div&gt;</summary>
		<author><name>Mphi</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs21/index.php?title=Language_-_Ruby&amp;diff=19124</id>
		<title>Language - Ruby</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs21/index.php?title=Language_-_Ruby&amp;diff=19124"/>
		<updated>2012-04-20T15:13:53Z</updated>

		<summary type="html">&lt;p&gt;Mphi: /* Step Four: Close */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Language]]&lt;br /&gt;
[[File:Icon-Ruby.png|64x64px||link=|alt=]]Ruby is an interpreted and object oriented scripting language with simple syntax created by [http://en.wikipedia.org/wiki/Yukihiro_Matsumoto Yukihiro Matsumoto].&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
{{LanguageSupport|Ruby|the complete Phidget API, including events|all Phidget devices.|Linux and OS X|}}&lt;br /&gt;
&lt;br /&gt;
{{QuickDownloads|Ruby|&lt;br /&gt;
{{APIQuickDownloads|http://www.phidgets.com/documentation/rubydoc RubyDoc}}|&lt;br /&gt;
{{ExampleQuickDownloads|http://rubygems.org/gems/phidgets-ffi| (@RubyGems)}}|&lt;br /&gt;
{{ExtraLibraryQuickDownloads|http://rubygems.org/gems/phidgets-ffi|Ruby| (@RubyGems)}}&lt;br /&gt;
{{MacQuickDownloads}}&lt;br /&gt;
{{LinuxQuickDownloads}}&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
==Getting started with Ruby==&lt;br /&gt;
&lt;br /&gt;
If you are new to writing code for Phidgets, we recommend starting by running, then modifying existing examples. This will allow you to:&lt;br /&gt;
{{ExampleCodeReasons}}&lt;br /&gt;
&lt;br /&gt;
Instructions for OS X and Linux are similar, so they will be combined into the same section.&lt;br /&gt;
&lt;br /&gt;
The first step in using Ruby on Mac or Linux is to install the Phidget libraries.  Compile and install them as explained on the [[Device List|getting started guide for your device]].&lt;br /&gt;
&lt;br /&gt;
The next step is to install the phidgets-ffi gem. This is the library that allows you to program Phidgets with Ruby. For more information, please see phidgets-ffi at [http://rubygems.org/gems/phidgets-ffi RubyGems] and [https://github.com/kreynolds/phidgets-ffi GitHub].&lt;br /&gt;
&lt;br /&gt;
==Description of Library Files==&lt;br /&gt;
Ruby programs on OS X and Linux depend on the following files, which the installers put into your system. &lt;br /&gt;
&lt;br /&gt;
If you are using OS X, you will need the:&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|Phidget21.framework}}&amp;lt;/b&amp;gt; contains the actual Phidgets library for OS X, which is used at run-time.  &lt;br /&gt;
&lt;br /&gt;
If you are using Linux, you will need the:&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|libphidget21.so}}&amp;lt;/b&amp;gt; contains the actual Phidgets library for Linux, which is used at run-time.&lt;br /&gt;
&lt;br /&gt;
For both OS X and Linux, you will need the phidgets-ffi gem: &lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|phidgets-ffi}}&amp;lt;/b&amp;gt; is the Phidgets gem for Ruby. This gem contains the library as well as example code.&lt;br /&gt;
&lt;br /&gt;
==Installing phidgets-ffi==&lt;br /&gt;
&lt;br /&gt;
The phidgets-ffi gem relies on the [http://rubygems.org/gems/ffi ffi gem]. So, if you do not already have it installed, please do so. A version of ffi 1.0.9 or greater is needed. Type the following to install ffi:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  gem install ffi&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Next, please install the phidgets-ffi gem by typing the following into command line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  gem install phidgets-ffi&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For OS X systems, the gem will be installed into {{Code|/Library/Ruby/Gems/1.x/phidgets-ffi-x.x.x}}.&lt;br /&gt;
&lt;br /&gt;
For typical Linux systems, the gem will be installed into {{Code|var/lib/gems/1.9/gems/phidgets-ffi-x.x.x}}.&lt;br /&gt;
&lt;br /&gt;
===Use Our Examples===&lt;br /&gt;
&lt;br /&gt;
Open a command line terminal and navigate to the phidgets-ffi gem directory. {{Code|cd}} into the {{Code|examples}} folder. Here, you will find all of the examples available for Ruby.  If you aren&#039;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]].&lt;br /&gt;
&lt;br /&gt;
The easiest way to confirm that your environment is set up properly will be to compile and run the {{Code|HelloWorld}} Ruby example.&lt;br /&gt;
&lt;br /&gt;
The only thing left to do is to run the examples! Type the following into command line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  ruby HelloWorld.rb&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This program will detect for devices that are attached/detached on the computer. Go ahead, and attach or detach your devices! Here is an example output:&lt;br /&gt;
&lt;br /&gt;
[[File:Ruby Linux HelloWorld Output.PNG|link=|alt=HelloWorld Output]]&lt;br /&gt;
&lt;br /&gt;
After confirming that the {{Code|HelloWorld}} example is working, you can proceed to run the example for your device. If you aren&#039;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 ensure that you have set your start up project to be the one that matches your device before compiling. &lt;br /&gt;
&lt;br /&gt;
Once you have the Ruby examples running, we have a [[#Follow the Examples|teaching]] section below to help you follow them.&lt;br /&gt;
&lt;br /&gt;
==Write Your Own Code==&lt;br /&gt;
&lt;br /&gt;
When you are building a project from scratch, or adding Phidget function calls to an existing project, you&#039;ll need to configure your environment to properly link the Phidget Ruby libraries.&lt;br /&gt;
&lt;br /&gt;
Simply, add the following two lines to the beginning of any {{Code|.rb}} script to make use of the phidgets-ffi gem&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  require &#039;rubygems&#039;&lt;br /&gt;
  require &#039;phidgets-ffi&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The project now has access to the Phidget21 function calls and you are ready to begin coding.&lt;br /&gt;
&lt;br /&gt;
The same [[#Follow the Examples|teaching ]] section which describes the examples also has further resources for programming your Phidget.&lt;br /&gt;
&lt;br /&gt;
==Follow the Examples==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
Next, comes our API information.  These resources outline the Ruby Phidget functions:&lt;br /&gt;
&lt;br /&gt;
{{UsingAPhidgetInCodeGeneral|both of which are available in Ruby|[http://www.phidgets.com/documentation/rubydoc Ruby API]}}&lt;br /&gt;
&lt;br /&gt;
===Example Flow===&lt;br /&gt;
&lt;br /&gt;
{{ExamplePseudocode|In Ruby, you can name these &#039;&#039;&#039;event&#039;&#039;&#039; 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. &amp;lt;br&amp;gt; &lt;br /&gt;
In the example code, the event functions common to all Phidgets are called things like &#039;&#039;&#039;AttachHandler()&#039;&#039;&#039; and &#039;&#039;&#039;DetachHandler()&#039;&#039;&#039;, etc.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
Other functions are given in the examples to show you more detail on using your Phidget.  For example, &#039;&#039;&#039;DeviceInitialize()&#039;&#039;&#039; will show what needs to be set up for your Phidget before using it.&lt;br /&gt;
|Creating a Phidget software object in Ruby is specific to the Phidget.  For a Phidget Spatial, for example, this would involve creating a {{Code|Spatial}} object.  The examples show how to do this and other API functions.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The object provides device specific methods and properties which are available from the API for your specific Phidget.|&lt;br /&gt;
[http://www.phidgets.com/documentation/rubydoc Ruby API]}}&lt;br /&gt;
&lt;br /&gt;
===Code Snippets===&lt;br /&gt;
&lt;br /&gt;
Specific calls in Ruby will differ in syntax from those on the [[General Phidget Programming]] page, but the concepts stay the same.  &lt;br /&gt;
&lt;br /&gt;
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 Ruby 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.&lt;br /&gt;
&lt;br /&gt;
For example, if we were using a [[Product - 1018 - PhidgetInterfaceKit 8/8/8 | Phidget Interface Kit]] as our device, the general calls would look like this:&lt;br /&gt;
&lt;br /&gt;
====Step One: Initialize and Open====&lt;br /&gt;
&lt;br /&gt;
The Phidget constructor method will need to be called to create the Phidget object. There are two methods or programming a Phidget in Ruby: with and without a block. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  # Without a block&lt;br /&gt;
    device = Phidgets::InterfaceKit.new&lt;br /&gt;
&lt;br /&gt;
  # With a block&lt;br /&gt;
    Phidgets::InterfaceKit.new do |device|&lt;br /&gt;
      ..&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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. In the above example, InterfaceKit represents the PhidgetInterfaceKit. For simplicity in explaining the programming concepts, the remainder of the code snippet section will use the InterfaceKit object.&lt;br /&gt;
&lt;br /&gt;
For the most part, the two methods behave the same, but there are some subtle differences, which will be explained in the next section.&lt;br /&gt;
&lt;br /&gt;
====Step Two: Wait for Attachment (plugging in) of the Phidget==== &lt;br /&gt;
&lt;br /&gt;
The program needs to try and connect to the Phidget. Options can be added to the InterfaceKit constructor to connect to the first Phidget it finds, based on its serial number, label, or even connect across the network. [http://rubydoc.info/gems/phidgets-ffi/0.1.1/frames Rubydocs API documentation] lists all of the available modes that the constructor provides. &lt;br /&gt;
&lt;br /&gt;
For example, the following will try to connect to the first Phidget it finds:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device= Phidgets::InterfaceKit.new&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following will try to connect to a Phidget over the Phidget WebService with a serial number of {{Code|99999}}, and a server id of {{Code|myserver}}: &lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  options = {:serial_number =&amp;gt; 99999, :server_id =&amp;gt; ‘myserver’, :password =&amp;gt; nil}&lt;br /&gt;
  device= Phidgets::InterfaceKit.new(options)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One important thing to remember is that when working with Phidgets, a local connection will reserve the device until closed. This prevents any other instances from retrieving data from the Phidget, including other programs. The one connection per device limit does not apply when exclusively using the Phidget WebService.&lt;br /&gt;
&lt;br /&gt;
{{Code|Phidgets::InterfaceKit.new}} will tell the program to continuously try to connect to a Phidget, based on the options given(if any), even trying to reconnect if it gets disconnected. &lt;br /&gt;
&lt;br /&gt;
=====Non Block Programming=====&lt;br /&gt;
For the non block programming method, simply calling the constructor does not guarantee you can use the Phidget immediately. We can handle this by using event driven programming and  tracking the {{Code|on_attach}} and {{Code|on_detach}} events, or by calling: {{Code|wait_for_attachment}}. {{Code|wait_for_attachment}} will block indefinitely until a connection is made to the Phidget, or an optional timeout is exceeded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device= Phidgets::InterfaceKit.new&lt;br /&gt;
  device.wait_for_attachment 2000 #halt the program for up 2000 milliseconds or until the Phidget is connected&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Please also remember to call close at the end or the program to free any locks on the Phidget&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device.close&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=====Block Programming=====&lt;br /&gt;
If you are programming inside a block, {{Code|wait_for_attachment}} is automatically called. By default, it will halt the program and try to connect to the Phidget for up to 1000 milliseconds.  Afterwards, the block will yield. Finally, close is not needed as it is automatically called once the block has yield.&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  options = {:timeout =&amp;gt; 2000)&lt;br /&gt;
  Phidgets::InterfaceKit.new(options) do |device|&lt;br /&gt;
    ... &lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Sometimes, it makes more sense to handle the attachment via an event.  This would be in instances where the Phidget is being plugged and unplugged, and you want to handle these incidents.  Or, when you want to use event-driven programming because you have a GUI-driven program.  In these cases, an event-driven code snippet to handle the attachment might look something like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device.on_attach  do |device, obj|&lt;br /&gt;
    puts &amp;quot;Id: #{device.id}&amp;quot;&lt;br /&gt;
    puts &amp;quot;Serial number: #{device.serial_number}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Step Three: Do Things with the Phidget====&lt;br /&gt;
&lt;br /&gt;
You can read data and interact with your Phidget both by polling it for its current state (or to set a state), or by catching events that trigger when the data changes.&lt;br /&gt;
&lt;br /&gt;
For our [[Product - 1018 - PhidgetInterfaceKit 8/8/8 | Phidget Interface Kit]], the polling method of getting the current sensor state and setting an output state looks something like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
&lt;br /&gt;
# Get a data point from Analog Port 0&lt;br /&gt;
puts &amp;quot;Sensor value[0]: #{device.sensors[0].to_i}&amp;quot; &lt;br /&gt;
&lt;br /&gt;
# Set digital output port 0 to be on&lt;br /&gt;
device.outputs[0].state = true&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To catch data changes via events, you would use something like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device.on_sensor_change do |device, input, value, obj|&lt;br /&gt;
    puts &amp;quot;Sensor #{input.index}&#039;s value has changed to #{value}&amp;quot;&lt;br /&gt;
  end &lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Step Four: Close====&lt;br /&gt;
 &lt;br /&gt;
If you are using the block programming method, you do not need to worry about closing the Phidget. It is already taken care of when the block ends. However, if you are using the non block programming method, then you will need take special consideration that the Phidget is closed. &lt;br /&gt;
&lt;br /&gt;
At the end of your program, don’t forget to call {{Code|close}} to free any locks on the Phidget that the Phidget constructor call put in place!&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device.close&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;complete&#039;&#039; set of functions you have available for all Phidgets can be found in the [http://rubydoc.info/gems/phidgets-ffi/0.1.1/frames Ruby API]. You can also find more description on any device-specific function either in the [[Device List|Device API]] page for calls available only on your specific Phidget.&lt;br /&gt;
&lt;br /&gt;
=====Raw FFI=====&lt;br /&gt;
As an alternative to programming with the method as outlined in this document, you can also program making straight C calls through FFI. Please refer to the files in {{Code|Ruby Gems Directory/phidgets-ffi-x.x.x/lib/phidgets-ffi/ffi}} to see a list of available methods, and the [http://www.phidgets.com/documentation/Phidget21_C_Doc.zip C API] for usage. There are raw ffi examples in {{Code|Ruby Gems directory/phidgets-ffi-x.x.x/examples/raw-ffi}}.&lt;br /&gt;
&lt;br /&gt;
==Common Problems and Solutions/Workarounds==&lt;br /&gt;
&lt;br /&gt;
Here you can put various frequent problems and our recommended solutions.&lt;/div&gt;</summary>
		<author><name>Mphi</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs21/index.php?title=Language_-_Ruby&amp;diff=19119</id>
		<title>Language - Ruby</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs21/index.php?title=Language_-_Ruby&amp;diff=19119"/>
		<updated>2012-04-19T21:45:50Z</updated>

		<summary type="html">&lt;p&gt;Mphi: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Language]]&lt;br /&gt;
[[File:Icon-Ruby.png|64x64px||link=|alt=]]Ruby is an interpreted and object oriented scripting language with simple syntax created by [http://en.wikipedia.org/wiki/Yukihiro_Matsumoto Yukihiro Matsumoto].&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
{{LanguageSupport|Ruby|the complete Phidget API, including events|all Phidget devices.|Linux and OS X|}}&lt;br /&gt;
&lt;br /&gt;
{{QuickDownloads|Ruby|&lt;br /&gt;
{{APIQuickDownloads|http://www.phidgets.com/documentation/rubydoc RubyDoc}}|&lt;br /&gt;
{{ExampleQuickDownloads|http://rubygems.org/gems/phidgets-ffi| (@RubyGems)}}|&lt;br /&gt;
{{ExtraLibraryQuickDownloads|http://rubygems.org/gems/phidgets-ffi|Ruby| (@RubyGems)}}&lt;br /&gt;
{{MacQuickDownloads}}&lt;br /&gt;
{{LinuxQuickDownloads}}&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
==Getting started with Ruby==&lt;br /&gt;
&lt;br /&gt;
If you are new to writing code for Phidgets, we recommend starting by running, then modifying existing examples. This will allow you to:&lt;br /&gt;
{{ExampleCodeReasons}}&lt;br /&gt;
&lt;br /&gt;
Instructions for OS X and Linux are similar, so they will be combined into the same section.&lt;br /&gt;
&lt;br /&gt;
The first step in using Ruby on Mac or Linux is to install the Phidget libraries.  Compile and install them as explained on the [[Device List|getting started guide for your device]].&lt;br /&gt;
&lt;br /&gt;
The next step is to install the phidgets-ffi gem. This is the library that allows you to program Phidgets with Ruby. For more information, please see phidgets-ffi at [http://rubygems.org/gems/phidgets-ffi RubyGems] and [https://github.com/kreynolds/phidgets-ffi GitHub].&lt;br /&gt;
&lt;br /&gt;
==Description of Library Files==&lt;br /&gt;
Ruby programs on OS X and Linux depend on the following files, which the installers put into your system. &lt;br /&gt;
&lt;br /&gt;
If you are using OS X, you will need the:&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|Phidget21.framework}}&amp;lt;/b&amp;gt; contains the actual Phidgets library for OS X, which is used at run-time.  &lt;br /&gt;
&lt;br /&gt;
If you are using Linux, you will need the:&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|libphidget21.so}}&amp;lt;/b&amp;gt; contains the actual Phidgets library for Linux, which is used at run-time.&lt;br /&gt;
&lt;br /&gt;
For both OS X and Linux, you will need the phidgets-ffi gem: &lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|phidgets-ffi}}&amp;lt;/b&amp;gt; is the Phidgets gem for Ruby. This gem contains the library as well as example code.&lt;br /&gt;
&lt;br /&gt;
==Installing phidgets-ffi==&lt;br /&gt;
&lt;br /&gt;
The phidgets-ffi gem relies on the [http://rubygems.org/gems/ffi ffi gem]. So, if you do not already have it installed, please do so. A version of ffi 1.0.9 or greater is needed. Type the following to install ffi:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  gem install ffi&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Next, please install the phidgets-ffi gem by typing the following into command line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  gem install phidgets-ffi&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For OS X systems, the gem will be installed into {{Code|/Library/Ruby/Gems/1.x/phidgets-ffi-x.x.x}}.&lt;br /&gt;
&lt;br /&gt;
For typical Linux systems, the gem will be installed into {{Code|var/lib/gems/1.9/gems/phidgets-ffi-x.x.x}}.&lt;br /&gt;
&lt;br /&gt;
===Use Our Examples===&lt;br /&gt;
&lt;br /&gt;
Open a command line terminal and navigate to the phidgets-ffi gem directory. {{Code|cd}} into the {{Code|examples}} folder. Here, you will find all of the examples available for Ruby.  If you aren&#039;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]].&lt;br /&gt;
&lt;br /&gt;
The easiest way to confirm that your environment is set up properly will be to compile and run the {{Code|HelloWorld}} Ruby example.&lt;br /&gt;
&lt;br /&gt;
The only thing left to do is to run the examples! Type the following into command line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  ruby HelloWorld.rb&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This program will detect for devices that are attached/detached on the computer. Go ahead, and attach or detach your devices! Here is an example output:&lt;br /&gt;
&lt;br /&gt;
[[File:Ruby Linux HelloWorld Output.PNG|link=|alt=HelloWorld Output]]&lt;br /&gt;
&lt;br /&gt;
After confirming that the {{Code|HelloWorld}} example is working, you can proceed to run the example for your device. If you aren&#039;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 ensure that you have set your start up project to be the one that matches your device before compiling. &lt;br /&gt;
&lt;br /&gt;
Once you have the Ruby examples running, we have a [[#Follow the Examples|teaching]] section below to help you follow them.&lt;br /&gt;
&lt;br /&gt;
==Write Your Own Code==&lt;br /&gt;
&lt;br /&gt;
When you are building a project from scratch, or adding Phidget function calls to an existing project, you&#039;ll need to configure your environment to properly link the Phidget Ruby libraries.&lt;br /&gt;
&lt;br /&gt;
Simply, add the following two lines to the beginning of any {{Code|.rb}} script to make use of the phidgets-ffi gem&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  require &#039;rubygems&#039;&lt;br /&gt;
  require &#039;phidgets-ffi&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The project now has access to the Phidget21 function calls and you are ready to begin coding.&lt;br /&gt;
&lt;br /&gt;
The same [[#Follow the Examples|teaching ]] section which describes the examples also has further resources for programming your Phidget.&lt;br /&gt;
&lt;br /&gt;
==Follow the Examples==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
Next, comes our API information.  These resources outline the Ruby Phidget functions:&lt;br /&gt;
&lt;br /&gt;
{{UsingAPhidgetInCodeGeneral|both of which are available in Ruby|[http://www.phidgets.com/documentation/rubydoc Ruby API]}}&lt;br /&gt;
&lt;br /&gt;
===Example Flow===&lt;br /&gt;
&lt;br /&gt;
{{ExamplePseudocode|In Ruby, you can name these &#039;&#039;&#039;event&#039;&#039;&#039; 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. &amp;lt;br&amp;gt; &lt;br /&gt;
In the example code, the event functions common to all Phidgets are called things like &#039;&#039;&#039;AttachHandler()&#039;&#039;&#039; and &#039;&#039;&#039;DetachHandler()&#039;&#039;&#039;, etc.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
Other functions are given in the examples to show you more detail on using your Phidget.  For example, &#039;&#039;&#039;DeviceInitialize()&#039;&#039;&#039; will show what needs to be set up for your Phidget before using it.&lt;br /&gt;
|Creating a Phidget software object in Ruby is specific to the Phidget.  For a Phidget Spatial, for example, this would involve creating a {{Code|Spatial}} object.  The examples show how to do this and other API functions.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The object provides device specific methods and properties which are available from the API for your specific Phidget.|&lt;br /&gt;
[http://www.phidgets.com/documentation/rubydoc Ruby API]}}&lt;br /&gt;
&lt;br /&gt;
===Code Snippets===&lt;br /&gt;
&lt;br /&gt;
Specific calls in Ruby will differ in syntax from those on the [[General Phidget Programming]] page, but the concepts stay the same.  &lt;br /&gt;
&lt;br /&gt;
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 Ruby 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.&lt;br /&gt;
&lt;br /&gt;
For example, if we were using a [[Product - 1018 - PhidgetInterfaceKit 8/8/8 | Phidget Interface Kit]] as our device, the general calls would look like this:&lt;br /&gt;
&lt;br /&gt;
====Step One: Initialize and Open====&lt;br /&gt;
&lt;br /&gt;
The Phidget constructor method will need to be called to create the Phidget object. There are two methods or programming a Phidget in Ruby: with and without a block. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  # Without a block&lt;br /&gt;
    device = Phidgets::InterfaceKit.new&lt;br /&gt;
&lt;br /&gt;
  # With a block&lt;br /&gt;
    Phidgets::InterfaceKit.new do |device|&lt;br /&gt;
      ..&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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. In the above example, InterfaceKit represents the PhidgetInterfaceKit. For simplicity in explaining the programming concepts, the remainder of the code snippet section will use the InterfaceKit object.&lt;br /&gt;
&lt;br /&gt;
For the most part, the two methods behave the same, but there are some subtle differences, which will be explained in the next section.&lt;br /&gt;
&lt;br /&gt;
====Step Two: Wait for Attachment (plugging in) of the Phidget==== &lt;br /&gt;
&lt;br /&gt;
The program needs to try and connect to the Phidget. Options can be added to the InterfaceKit constructor to connect to the first Phidget it finds, based on its serial number, label, or even connect across the network. [http://rubydoc.info/gems/phidgets-ffi/0.1.1/frames Rubydocs API documentation] lists all of the available modes that the constructor provides. &lt;br /&gt;
&lt;br /&gt;
For example, the following will try to connect to the first Phidget it finds:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device= Phidgets::InterfaceKit.new&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following will try to connect to a Phidget over the Phidget WebService with a serial number of {{Code|99999}}, and a server id of {{Code|myserver}}: &lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  options = {:serial_number =&amp;gt; 99999, :server_id =&amp;gt; ‘myserver’, :password =&amp;gt; nil}&lt;br /&gt;
  device= Phidgets::InterfaceKit.new(options)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One important thing to remember is that when working with Phidgets, a local connection will reserve the device until closed. This prevents any other instances from retrieving data from the Phidget, including other programs. The one connection per device limit does not apply when exclusively using the Phidget WebService.&lt;br /&gt;
&lt;br /&gt;
{{Code|Phidgets::InterfaceKit.new}} will tell the program to continuously try to connect to a Phidget, based on the options given(if any), even trying to reconnect if it gets disconnected. &lt;br /&gt;
&lt;br /&gt;
=====Non Block Programming=====&lt;br /&gt;
For the non block programming method, simply calling the constructor does not guarantee you can use the Phidget immediately. We can handle this by using event driven programming and  tracking the {{Code|on_attach}} and {{Code|on_detach}} events, or by calling: {{Code|wait_for_attachment}}. {{Code|wait_for_attachment}} will block indefinitely until a connection is made to the Phidget, or an optional timeout is exceeded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device= Phidgets::InterfaceKit.new&lt;br /&gt;
  device.wait_for_attachment 2000 #halt the program for up 2000 milliseconds or until the Phidget is connected&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Please also remember to call close at the end or the program to free any locks on the Phidget&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device.close&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=====Block Programming=====&lt;br /&gt;
If you are programming inside a block, {{Code|wait_for_attachment}} is automatically called. By default, it will halt the program and try to connect to the Phidget for up to 1000 milliseconds.  Afterwards, the block will yield. Finally, close is not needed as it is automatically called once the block has yield.&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  options = {:timeout =&amp;gt; 2000)&lt;br /&gt;
  Phidgets::InterfaceKit.new(options) do |device|&lt;br /&gt;
    ... &lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Sometimes, it makes more sense to handle the attachment via an event.  This would be in instances where the Phidget is being plugged and unplugged, and you want to handle these incidents.  Or, when you want to use event-driven programming because you have a GUI-driven program.  In these cases, an event-driven code snippet to handle the attachment might look something like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device.on_attach  do |device, obj|&lt;br /&gt;
    puts &amp;quot;Id: #{device.id}&amp;quot;&lt;br /&gt;
    puts &amp;quot;Serial number: #{device.serial_number}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Step Three: Do Things with the Phidget====&lt;br /&gt;
&lt;br /&gt;
You can read data and interact with your Phidget both by polling it for its current state (or to set a state), or by catching events that trigger when the data changes.&lt;br /&gt;
&lt;br /&gt;
For our [[Product - 1018 - PhidgetInterfaceKit 8/8/8 | Phidget Interface Kit]], the polling method of getting the current sensor state and setting an output state looks something like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
&lt;br /&gt;
# Get a data point from Analog Port 0&lt;br /&gt;
puts &amp;quot;Sensor value[0]: #{device.sensors[0].to_i}&amp;quot; &lt;br /&gt;
&lt;br /&gt;
# Set digital output port 0 to be on&lt;br /&gt;
device.outputs[0].state = true&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To catch data changes via events, you would use something like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device.on_sensor_change do |device, input, value, obj|&lt;br /&gt;
    puts &amp;quot;Sensor #{input.index}&#039;s value has changed to #{value}&amp;quot;&lt;br /&gt;
  end &lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=====Step Four: Close=====&lt;br /&gt;
 &lt;br /&gt;
If you are using the block programming method, you do not need to worry about closing the Phidget. It is already taken care of when the block ends. However, if you are using the non block programming method, then you will need take special consideration that the Phidget is closed. &lt;br /&gt;
&lt;br /&gt;
At the end of your program, don’t forget to call {{Code|close}} to free any locks on the Phidget that the Phidget constructor call put in place!&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device.close&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;complete&#039;&#039; set of functions you have available for all Phidgets can be found in the [http://rubydoc.info/gems/phidgets-ffi/0.1.1/frames Ruby API]. You can also find more description on any device-specific function either in the [[Device List|Device API]] page for calls available only on your specific Phidget.&lt;br /&gt;
&lt;br /&gt;
=====Raw FFI=====&lt;br /&gt;
As an alternative to programming with the method as outlined in this document, you can also program making straight C calls through FFI. Please refer to the files in {{Code|Ruby Gems Directory/phidgets-ffi-x.x.x/lib/phidgets-ffi/ffi}} to see a list of available methods, and the [http://www.phidgets.com/documentation/Phidget21_C_Doc.zip C API] for usage. There are raw ffi examples in {{Code|Ruby Gems directory/phidgets-ffi-x.x.x/examples/raw-ffi}}.&lt;br /&gt;
&lt;br /&gt;
==Common Problems and Solutions/Workarounds==&lt;br /&gt;
&lt;br /&gt;
Here you can put various frequent problems and our recommended solutions.&lt;/div&gt;</summary>
		<author><name>Mphi</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs21/index.php?title=Language_-_Ruby&amp;diff=19118</id>
		<title>Language - Ruby</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs21/index.php?title=Language_-_Ruby&amp;diff=19118"/>
		<updated>2012-04-19T21:43:26Z</updated>

		<summary type="html">&lt;p&gt;Mphi: /* Step Two: Wait for Attachment (plugging in) of the Phidget */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Language]]&lt;br /&gt;
[[File:Icon-Ruby.png|64x64px||link=|alt=]]Ruby is an interpreted and object oriented scripting language with simple syntax created by Yukihiro Matsumoto.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
{{LanguageSupport|Ruby|the complete Phidget API, including events|all Phidget devices.|Linux and OS X|}}&lt;br /&gt;
&lt;br /&gt;
{{QuickDownloads|Ruby|&lt;br /&gt;
{{APIQuickDownloads|http://www.phidgets.com/documentation/rubydoc RubyDoc}}|&lt;br /&gt;
{{ExampleQuickDownloads|http://rubygems.org/gems/phidgets-ffi| (@RubyGems)}}|&lt;br /&gt;
{{ExtraLibraryQuickDownloads|http://rubygems.org/gems/phidgets-ffi|Ruby| (@RubyGems)}}&lt;br /&gt;
{{MacQuickDownloads}}&lt;br /&gt;
{{LinuxQuickDownloads}}&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
==Getting started with Ruby==&lt;br /&gt;
&lt;br /&gt;
If you are new to writing code for Phidgets, we recommend starting by running, then modifying existing examples. This will allow you to:&lt;br /&gt;
{{ExampleCodeReasons}}&lt;br /&gt;
&lt;br /&gt;
Instructions for OS X and Linux are similar, so they will be combined into the same section.&lt;br /&gt;
&lt;br /&gt;
The first step in using Ruby on Mac or Linux is to install the Phidget libraries.  Compile and install them as explained on the [[Device List|getting started guide for your device]].&lt;br /&gt;
&lt;br /&gt;
The next step is to install the phidgets-ffi gem. This is the library that allows you to program Phidgets with Ruby. For more information, please see phidgets-ffi at [http://rubygems.org/gems/phidgets-ffi RubyGems] and [https://github.com/kreynolds/phidgets-ffi GitHub].&lt;br /&gt;
&lt;br /&gt;
==Description of Library Files==&lt;br /&gt;
Ruby programs on OS X and Linux depend on the following files, which the installers put into your system. &lt;br /&gt;
&lt;br /&gt;
If you are using OS X, you will need the:&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|Phidget21.framework}}&amp;lt;/b&amp;gt; contains the actual Phidgets library for OS X, which is used at run-time.  &lt;br /&gt;
&lt;br /&gt;
If you are using Linux, you will need the:&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|libphidget21.so}}&amp;lt;/b&amp;gt; contains the actual Phidgets library for Linux, which is used at run-time.&lt;br /&gt;
&lt;br /&gt;
For both OS X and Linux, you will need the phidgets-ffi gem: &lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|phidgets-ffi}}&amp;lt;/b&amp;gt; is the Phidgets gem for Ruby. This gem contains the library as well as example code.&lt;br /&gt;
&lt;br /&gt;
==Installing phidgets-ffi==&lt;br /&gt;
&lt;br /&gt;
The phidgets-ffi gem relies on the [http://rubygems.org/gems/ffi ffi gem]. So, if you do not already have it installed, please do so. A version of ffi 1.0.9 or greater is needed. Type the following to install ffi:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  gem install ffi&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Next, please install the phidgets-ffi gem by typing the following into command line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  gem install phidgets-ffi&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For OS X systems, the gem will be installed into {{Code|/Library/Ruby/Gems/1.x/phidgets-ffi-x.x.x}}.&lt;br /&gt;
&lt;br /&gt;
For typical Linux systems, the gem will be installed into {{Code|var/lib/gems/1.9/gems/phidgets-ffi-x.x.x}}.&lt;br /&gt;
&lt;br /&gt;
===Use Our Examples===&lt;br /&gt;
&lt;br /&gt;
Open a command line terminal and navigate to the phidgets-ffi gem directory. {{Code|cd}} into the {{Code|examples}} folder. Here, you will find all of the examples available for Ruby.  If you aren&#039;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]].&lt;br /&gt;
&lt;br /&gt;
The easiest way to confirm that your environment is set up properly will be to compile and run the {{Code|HelloWorld}} Ruby example.&lt;br /&gt;
&lt;br /&gt;
The only thing left to do is to run the examples! Type the following into command line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  ruby HelloWorld.rb&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This program will detect for devices that are attached/detached on the computer. Go ahead, and attach or detach your devices! Here is an example output:&lt;br /&gt;
&lt;br /&gt;
[[File:Ruby Linux HelloWorld Output.PNG|link=|alt=HelloWorld Output]]&lt;br /&gt;
&lt;br /&gt;
After confirming that the {{Code|HelloWorld}} example is working, you can proceed to run the example for your device. If you aren&#039;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 ensure that you have set your start up project to be the one that matches your device before compiling. &lt;br /&gt;
&lt;br /&gt;
Once you have the Ruby examples running, we have a [[#Follow the Examples|teaching]] section below to help you follow them.&lt;br /&gt;
&lt;br /&gt;
==Write Your Own Code==&lt;br /&gt;
&lt;br /&gt;
When you are building a project from scratch, or adding Phidget function calls to an existing project, you&#039;ll need to configure your environment to properly link the Phidget Ruby libraries.&lt;br /&gt;
&lt;br /&gt;
Simply, add the following two lines to the beginning of any {{Code|.rb}} script to make use of the phidgets-ffi gem&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  require &#039;rubygems&#039;&lt;br /&gt;
  require &#039;phidgets-ffi&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The project now has access to the Phidget21 function calls and you are ready to begin coding.&lt;br /&gt;
&lt;br /&gt;
The same [[#Follow the Examples|teaching ]] section which describes the examples also has further resources for programming your Phidget.&lt;br /&gt;
&lt;br /&gt;
==Follow the Examples==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
Next, comes our API information.  These resources outline the Ruby Phidget functions:&lt;br /&gt;
&lt;br /&gt;
{{UsingAPhidgetInCodeGeneral|both of which are available in Ruby|[http://www.phidgets.com/documentation/rubydoc Ruby API]}}&lt;br /&gt;
&lt;br /&gt;
===Example Flow===&lt;br /&gt;
&lt;br /&gt;
{{ExamplePseudocode|In Ruby, you can name these &#039;&#039;&#039;event&#039;&#039;&#039; 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. &amp;lt;br&amp;gt; &lt;br /&gt;
In the example code, the event functions common to all Phidgets are called things like &#039;&#039;&#039;AttachHandler()&#039;&#039;&#039; and &#039;&#039;&#039;DetachHandler()&#039;&#039;&#039;, etc.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
Other functions are given in the examples to show you more detail on using your Phidget.  For example, &#039;&#039;&#039;DeviceInitialize()&#039;&#039;&#039; will show what needs to be set up for your Phidget before using it.&lt;br /&gt;
|Creating a Phidget software object in Ruby is specific to the Phidget.  For a Phidget Spatial, for example, this would involve creating a {{Code|Spatial}} object.  The examples show how to do this and other API functions.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The object provides device specific methods and properties which are available from the API for your specific Phidget.|&lt;br /&gt;
[http://www.phidgets.com/documentation/rubydoc Ruby API]}}&lt;br /&gt;
&lt;br /&gt;
===Code Snippets===&lt;br /&gt;
&lt;br /&gt;
Specific calls in Ruby will differ in syntax from those on the [[General Phidget Programming]] page, but the concepts stay the same.  &lt;br /&gt;
&lt;br /&gt;
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 Ruby 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.&lt;br /&gt;
&lt;br /&gt;
For example, if we were using a [[Product - 1018 - PhidgetInterfaceKit 8/8/8 | Phidget Interface Kit]] as our device, the general calls would look like this:&lt;br /&gt;
&lt;br /&gt;
====Step One: Initialize and Open====&lt;br /&gt;
&lt;br /&gt;
The Phidget constructor method will need to be called to create the Phidget object. There are two methods or programming a Phidget in Ruby: with and without a block. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  # Without a block&lt;br /&gt;
    device = Phidgets::InterfaceKit.new&lt;br /&gt;
&lt;br /&gt;
  # With a block&lt;br /&gt;
    Phidgets::InterfaceKit.new do |device|&lt;br /&gt;
      ..&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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. In the above example, InterfaceKit represents the PhidgetInterfaceKit. For simplicity in explaining the programming concepts, the remainder of the code snippet section will use the InterfaceKit object.&lt;br /&gt;
&lt;br /&gt;
For the most part, the two methods behave the same, but there are some subtle differences, which will be explained in the next section.&lt;br /&gt;
&lt;br /&gt;
====Step Two: Wait for Attachment (plugging in) of the Phidget==== &lt;br /&gt;
&lt;br /&gt;
The program needs to try and connect to the Phidget. Options can be added to the InterfaceKit constructor to connect to the first Phidget it finds, based on its serial number, label, or even connect across the network. [http://rubydoc.info/gems/phidgets-ffi/0.1.1/frames Rubydocs API documentation] lists all of the available modes that the constructor provides. &lt;br /&gt;
&lt;br /&gt;
For example, the following will try to connect to the first Phidget it finds:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device= Phidgets::InterfaceKit.new&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following will try to connect to a Phidget over the Phidget WebService with a serial number of {{Code|99999}}, and a server id of {{Code|myserver}}: &lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  options = {:serial_number =&amp;gt; 99999, :server_id =&amp;gt; ‘myserver’, :password =&amp;gt; nil}&lt;br /&gt;
  device= Phidgets::InterfaceKit.new(options)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One important thing to remember is that when working with Phidgets, a local connection will reserve the device until closed. This prevents any other instances from retrieving data from the Phidget, including other programs. The one connection per device limit does not apply when exclusively using the Phidget WebService.&lt;br /&gt;
&lt;br /&gt;
{{Code|Phidgets::InterfaceKit.new}} will tell the program to continuously try to connect to a Phidget, based on the options given(if any), even trying to reconnect if it gets disconnected. &lt;br /&gt;
&lt;br /&gt;
=====Non Block Programming=====&lt;br /&gt;
For the non block programming method, simply calling the constructor does not guarantee you can use the Phidget immediately. We can handle this by using event driven programming and  tracking the {{Code|on_attach}} and {{Code|on_detach}} events, or by calling: {{Code|wait_for_attachment}}. {{Code|wait_for_attachment}} will block indefinitely until a connection is made to the Phidget, or an optional timeout is exceeded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device= Phidgets::InterfaceKit.new&lt;br /&gt;
  device.wait_for_attachment 2000 #halt the program for up 2000 milliseconds or until the Phidget is connected&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Please also remember to call close at the end or the program to free any locks on the Phidget&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device.close&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=====Block Programming=====&lt;br /&gt;
If you are programming inside a block, {{Code|wait_for_attachment}} is automatically called. By default, it will halt the program and try to connect to the Phidget for up to 1000 milliseconds.  Afterwards, the block will yield. Finally, close is not needed as it is automatically called once the block has yield.&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  options = {:timeout =&amp;gt; 2000)&lt;br /&gt;
  Phidgets::InterfaceKit.new(options) do |device|&lt;br /&gt;
    ... &lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Sometimes, it makes more sense to handle the attachment via an event.  This would be in instances where the Phidget is being plugged and unplugged, and you want to handle these incidents.  Or, when you want to use event-driven programming because you have a GUI-driven program.  In these cases, an event-driven code snippet to handle the attachment might look something like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device.on_attach  do |device, obj|&lt;br /&gt;
    puts &amp;quot;Id: #{device.id}&amp;quot;&lt;br /&gt;
    puts &amp;quot;Serial number: #{device.serial_number}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Step Three: Do Things with the Phidget====&lt;br /&gt;
&lt;br /&gt;
You can read data and interact with your Phidget both by polling it for its current state (or to set a state), or by catching events that trigger when the data changes.&lt;br /&gt;
&lt;br /&gt;
For our [[Product - 1018 - PhidgetInterfaceKit 8/8/8 | Phidget Interface Kit]], the polling method of getting the current sensor state and setting an output state looks something like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
&lt;br /&gt;
# Get a data point from Analog Port 0&lt;br /&gt;
puts &amp;quot;Sensor value[0]: #{device.sensors[0].to_i}&amp;quot; &lt;br /&gt;
&lt;br /&gt;
# Set digital output port 0 to be on&lt;br /&gt;
device.outputs[0].state = true&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To catch data changes via events, you would use something like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device.on_sensor_change do |device, input, value, obj|&lt;br /&gt;
    puts &amp;quot;Sensor #{input.index}&#039;s value has changed to #{value}&amp;quot;&lt;br /&gt;
  end &lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=====Step Four: Close=====&lt;br /&gt;
 &lt;br /&gt;
If you are using the block programming method, you do not need to worry about closing the Phidget. It is already taken care of when the block ends. However, if you are using the non block programming method, then you will need take special consideration that the Phidget is closed. &lt;br /&gt;
&lt;br /&gt;
At the end of your program, don’t forget to call {{Code|close}} to free any locks on the Phidget that the Phidget constructor call put in place!&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device.close&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;complete&#039;&#039; set of functions you have available for all Phidgets can be found in the [http://rubydoc.info/gems/phidgets-ffi/0.1.1/frames Ruby API]. You can also find more description on any device-specific function either in the [[Device List|Device API]] page for calls available only on your specific Phidget.&lt;br /&gt;
&lt;br /&gt;
=====Raw FFI=====&lt;br /&gt;
As an alternative to programming with the method as outlined in this document, you can also program making straight C calls through FFI. Please refer to the files in {{Code|Ruby Gems Directory/phidgets-ffi-x.x.x/lib/phidgets-ffi/ffi}} to see a list of available methods, and the [http://www.phidgets.com/documentation/Phidget21_C_Doc.zip C API] for usage. There are raw ffi examples in {{Code|Ruby Gems directory/phidgets-ffi-x.x.x/examples/raw-ffi}}.&lt;br /&gt;
&lt;br /&gt;
==Common Problems and Solutions/Workarounds==&lt;br /&gt;
&lt;br /&gt;
Here you can put various frequent problems and our recommended solutions.&lt;/div&gt;</summary>
		<author><name>Mphi</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs21/index.php?title=Language_-_Ruby&amp;diff=19117</id>
		<title>Language - Ruby</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs21/index.php?title=Language_-_Ruby&amp;diff=19117"/>
		<updated>2012-04-19T21:36:17Z</updated>

		<summary type="html">&lt;p&gt;Mphi: /* Raw FFI */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Language]]&lt;br /&gt;
[[File:Icon-Ruby.png|64x64px||link=|alt=]]Ruby is an interpreted and object oriented scripting language with simple syntax created by Yukihiro Matsumoto.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
{{LanguageSupport|Ruby|the complete Phidget API, including events|all Phidget devices.|Linux and OS X|}}&lt;br /&gt;
&lt;br /&gt;
{{QuickDownloads|Ruby|&lt;br /&gt;
{{APIQuickDownloads|http://www.phidgets.com/documentation/rubydoc RubyDoc}}|&lt;br /&gt;
{{ExampleQuickDownloads|http://rubygems.org/gems/phidgets-ffi| (@RubyGems)}}|&lt;br /&gt;
{{ExtraLibraryQuickDownloads|http://rubygems.org/gems/phidgets-ffi|Ruby| (@RubyGems)}}&lt;br /&gt;
{{MacQuickDownloads}}&lt;br /&gt;
{{LinuxQuickDownloads}}&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
==Getting started with Ruby==&lt;br /&gt;
&lt;br /&gt;
If you are new to writing code for Phidgets, we recommend starting by running, then modifying existing examples. This will allow you to:&lt;br /&gt;
{{ExampleCodeReasons}}&lt;br /&gt;
&lt;br /&gt;
Instructions for OS X and Linux are similar, so they will be combined into the same section.&lt;br /&gt;
&lt;br /&gt;
The first step in using Ruby on Mac or Linux is to install the Phidget libraries.  Compile and install them as explained on the [[Device List|getting started guide for your device]].&lt;br /&gt;
&lt;br /&gt;
The next step is to install the phidgets-ffi gem. This is the library that allows you to program Phidgets with Ruby. For more information, please see phidgets-ffi at [http://rubygems.org/gems/phidgets-ffi RubyGems] and [https://github.com/kreynolds/phidgets-ffi GitHub].&lt;br /&gt;
&lt;br /&gt;
==Description of Library Files==&lt;br /&gt;
Ruby programs on OS X and Linux depend on the following files, which the installers put into your system. &lt;br /&gt;
&lt;br /&gt;
If you are using OS X, you will need the:&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|Phidget21.framework}}&amp;lt;/b&amp;gt; contains the actual Phidgets library for OS X, which is used at run-time.  &lt;br /&gt;
&lt;br /&gt;
If you are using Linux, you will need the:&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|libphidget21.so}}&amp;lt;/b&amp;gt; contains the actual Phidgets library for Linux, which is used at run-time.&lt;br /&gt;
&lt;br /&gt;
For both OS X and Linux, you will need the phidgets-ffi gem: &lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|phidgets-ffi}}&amp;lt;/b&amp;gt; is the Phidgets gem for Ruby. This gem contains the library as well as example code.&lt;br /&gt;
&lt;br /&gt;
==Installing phidgets-ffi==&lt;br /&gt;
&lt;br /&gt;
The phidgets-ffi gem relies on the [http://rubygems.org/gems/ffi ffi gem]. So, if you do not already have it installed, please do so. A version of ffi 1.0.9 or greater is needed. Type the following to install ffi:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  gem install ffi&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Next, please install the phidgets-ffi gem by typing the following into command line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  gem install phidgets-ffi&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For OS X systems, the gem will be installed into {{Code|/Library/Ruby/Gems/1.x/phidgets-ffi-x.x.x}}.&lt;br /&gt;
&lt;br /&gt;
For typical Linux systems, the gem will be installed into {{Code|var/lib/gems/1.9/gems/phidgets-ffi-x.x.x}}.&lt;br /&gt;
&lt;br /&gt;
===Use Our Examples===&lt;br /&gt;
&lt;br /&gt;
Open a command line terminal and navigate to the phidgets-ffi gem directory. {{Code|cd}} into the {{Code|examples}} folder. Here, you will find all of the examples available for Ruby.  If you aren&#039;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]].&lt;br /&gt;
&lt;br /&gt;
The easiest way to confirm that your environment is set up properly will be to compile and run the {{Code|HelloWorld}} Ruby example.&lt;br /&gt;
&lt;br /&gt;
The only thing left to do is to run the examples! Type the following into command line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  ruby HelloWorld.rb&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This program will detect for devices that are attached/detached on the computer. Go ahead, and attach or detach your devices! Here is an example output:&lt;br /&gt;
&lt;br /&gt;
[[File:Ruby Linux HelloWorld Output.PNG|link=|alt=HelloWorld Output]]&lt;br /&gt;
&lt;br /&gt;
After confirming that the {{Code|HelloWorld}} example is working, you can proceed to run the example for your device. If you aren&#039;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 ensure that you have set your start up project to be the one that matches your device before compiling. &lt;br /&gt;
&lt;br /&gt;
Once you have the Ruby examples running, we have a [[#Follow the Examples|teaching]] section below to help you follow them.&lt;br /&gt;
&lt;br /&gt;
==Write Your Own Code==&lt;br /&gt;
&lt;br /&gt;
When you are building a project from scratch, or adding Phidget function calls to an existing project, you&#039;ll need to configure your environment to properly link the Phidget Ruby libraries.&lt;br /&gt;
&lt;br /&gt;
Simply, add the following two lines to the beginning of any {{Code|.rb}} script to make use of the phidgets-ffi gem&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  require &#039;rubygems&#039;&lt;br /&gt;
  require &#039;phidgets-ffi&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The project now has access to the Phidget21 function calls and you are ready to begin coding.&lt;br /&gt;
&lt;br /&gt;
The same [[#Follow the Examples|teaching ]] section which describes the examples also has further resources for programming your Phidget.&lt;br /&gt;
&lt;br /&gt;
==Follow the Examples==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
Next, comes our API information.  These resources outline the Ruby Phidget functions:&lt;br /&gt;
&lt;br /&gt;
{{UsingAPhidgetInCodeGeneral|both of which are available in Ruby|[http://www.phidgets.com/documentation/rubydoc Ruby API]}}&lt;br /&gt;
&lt;br /&gt;
===Example Flow===&lt;br /&gt;
&lt;br /&gt;
{{ExamplePseudocode|In Ruby, you can name these &#039;&#039;&#039;event&#039;&#039;&#039; 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. &amp;lt;br&amp;gt; &lt;br /&gt;
In the example code, the event functions common to all Phidgets are called things like &#039;&#039;&#039;AttachHandler()&#039;&#039;&#039; and &#039;&#039;&#039;DetachHandler()&#039;&#039;&#039;, etc.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
Other functions are given in the examples to show you more detail on using your Phidget.  For example, &#039;&#039;&#039;DeviceInitialize()&#039;&#039;&#039; will show what needs to be set up for your Phidget before using it.&lt;br /&gt;
|Creating a Phidget software object in Ruby is specific to the Phidget.  For a Phidget Spatial, for example, this would involve creating a {{Code|Spatial}} object.  The examples show how to do this and other API functions.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The object provides device specific methods and properties which are available from the API for your specific Phidget.|&lt;br /&gt;
[http://www.phidgets.com/documentation/rubydoc Ruby API]}}&lt;br /&gt;
&lt;br /&gt;
===Code Snippets===&lt;br /&gt;
&lt;br /&gt;
Specific calls in Ruby will differ in syntax from those on the [[General Phidget Programming]] page, but the concepts stay the same.  &lt;br /&gt;
&lt;br /&gt;
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 Ruby 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.&lt;br /&gt;
&lt;br /&gt;
For example, if we were using a [[Product - 1018 - PhidgetInterfaceKit 8/8/8 | Phidget Interface Kit]] as our device, the general calls would look like this:&lt;br /&gt;
&lt;br /&gt;
====Step One: Initialize and Open====&lt;br /&gt;
&lt;br /&gt;
The Phidget constructor method will need to be called to create the Phidget object. There are two methods or programming a Phidget in Ruby: with and without a block. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  # Without a block&lt;br /&gt;
    device = Phidgets::InterfaceKit.new&lt;br /&gt;
&lt;br /&gt;
  # With a block&lt;br /&gt;
    Phidgets::InterfaceKit.new do |device|&lt;br /&gt;
      ..&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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. In the above example, InterfaceKit represents the PhidgetInterfaceKit. For simplicity in explaining the programming concepts, the remainder of the code snippet section will use the InterfaceKit object.&lt;br /&gt;
&lt;br /&gt;
For the most part, the two methods behave the same, but there are some subtle differences, which will be explained in the next section.&lt;br /&gt;
&lt;br /&gt;
====Step Two: Wait for Attachment (plugging in) of the Phidget==== &lt;br /&gt;
&lt;br /&gt;
The program needs to try and connect to the Phidget. Options can be added to the InterfaceKit constructor to connect to the first Phidget it finds, based on its serial number, label, or even connect across the network. Rubydocs API Documentation lists all of the available modes that the constructor provides. &lt;br /&gt;
&lt;br /&gt;
For example, the following will try to connect to the first Phidget it finds:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device= Phidgets::InterfaceKit.new&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following will try to connect to a Phidget over the Phidget WebService with a serial number of {{Code|99999}}, and a server id of {{Code|myserver}}: &lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  options = {:serial_number =&amp;gt; 99999, :server_id =&amp;gt; ‘myserver’, :password =&amp;gt; nil}&lt;br /&gt;
  device= Phidgets::InterfaceKit.new(options)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One important thing to remember is that when working with Phidgets, a local connection will reserve the device until closed. This prevents any other instances from retrieving data from the Phidget, including other programs. The one connection per device limit does not apply when exclusively using the Phidget WebService.&lt;br /&gt;
&lt;br /&gt;
{{Code|Phidgets::InterfaceKit.new}} will tell the program to continuously try to connect to a Phidget, based on the options given(if any), even trying to reconnect if it gets disconnected. &lt;br /&gt;
&lt;br /&gt;
=====Non Block Programming=====&lt;br /&gt;
For the non block programming method, simply calling the constructor does not guarantee you can use the Phidget immediately. We can handle this by using event driven programming and  tracking the {{Code|on_attach}} and {{Code|on_detach}} events, or by calling: {{Code|wait_for_attachment}}. {{Code|wait_for_attachment}} will block indefinitely until a connection is made to the Phidget, or an optional timeout is exceeded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device= Phidgets::InterfaceKit.new&lt;br /&gt;
  device.wait_for_attachment 2000 #halt the program for up 2000 milliseconds or until the Phidget is connected&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Please also remember to call close at the end or the program to free any locks on the Phidget&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device.close&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=====Block Programming=====&lt;br /&gt;
If you are programming inside a block, {{Code|wait_for_attachment}} is automatically called. By default, it will halt the program and try to connect to the Phidget for up to 1000 milliseconds.  Afterwards, the block will yield. Finally, close is not needed as it is automatically called once the block has yield.&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  options = {:timeout =&amp;gt; 2000)&lt;br /&gt;
  Phidgets::InterfaceKit.new(options) do |device|&lt;br /&gt;
    ... &lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Sometimes, it makes more sense to handle the attachment via an event.  This would be in instances where the Phidget is being plugged and unplugged, and you want to handle these incidents.  Or, when you want to use event-driven programming because you have a GUI-driven program.  In these cases, an event-driven code snippet to handle the attachment might look something like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device.on_attach  do |device, obj|&lt;br /&gt;
    puts &amp;quot;Id: #{device.id}&amp;quot;&lt;br /&gt;
    puts &amp;quot;Serial number: #{device.serial_number}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Step Three: Do Things with the Phidget====&lt;br /&gt;
&lt;br /&gt;
You can read data and interact with your Phidget both by polling it for its current state (or to set a state), or by catching events that trigger when the data changes.&lt;br /&gt;
&lt;br /&gt;
For our [[Product - 1018 - PhidgetInterfaceKit 8/8/8 | Phidget Interface Kit]], the polling method of getting the current sensor state and setting an output state looks something like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
&lt;br /&gt;
# Get a data point from Analog Port 0&lt;br /&gt;
puts &amp;quot;Sensor value[0]: #{device.sensors[0].to_i}&amp;quot; &lt;br /&gt;
&lt;br /&gt;
# Set digital output port 0 to be on&lt;br /&gt;
device.outputs[0].state = true&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To catch data changes via events, you would use something like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device.on_sensor_change do |device, input, value, obj|&lt;br /&gt;
    puts &amp;quot;Sensor #{input.index}&#039;s value has changed to #{value}&amp;quot;&lt;br /&gt;
  end &lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=====Step Four: Close=====&lt;br /&gt;
 &lt;br /&gt;
If you are using the block programming method, you do not need to worry about closing the Phidget. It is already taken care of when the block ends. However, if you are using the non block programming method, then you will need take special consideration that the Phidget is closed. &lt;br /&gt;
&lt;br /&gt;
At the end of your program, don’t forget to call {{Code|close}} to free any locks on the Phidget that the Phidget constructor call put in place!&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device.close&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;complete&#039;&#039; set of functions you have available for all Phidgets can be found in the [http://rubydoc.info/gems/phidgets-ffi/0.1.1/frames Ruby API]. You can also find more description on any device-specific function either in the [[Device List|Device API]] page for calls available only on your specific Phidget.&lt;br /&gt;
&lt;br /&gt;
=====Raw FFI=====&lt;br /&gt;
As an alternative to programming with the method as outlined in this document, you can also program making straight C calls through FFI. Please refer to the files in {{Code|Ruby Gems Directory/phidgets-ffi-x.x.x/lib/phidgets-ffi/ffi}} to see a list of available methods, and the [http://www.phidgets.com/documentation/Phidget21_C_Doc.zip C API] for usage. There are raw ffi examples in {{Code|Ruby Gems directory/phidgets-ffi-x.x.x/examples/raw-ffi}}.&lt;br /&gt;
&lt;br /&gt;
==Common Problems and Solutions/Workarounds==&lt;br /&gt;
&lt;br /&gt;
Here you can put various frequent problems and our recommended solutions.&lt;/div&gt;</summary>
		<author><name>Mphi</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs21/index.php?title=Language_-_Ruby&amp;diff=19116</id>
		<title>Language - Ruby</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs21/index.php?title=Language_-_Ruby&amp;diff=19116"/>
		<updated>2012-04-19T21:36:00Z</updated>

		<summary type="html">&lt;p&gt;Mphi: /* Raw FFI */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Language]]&lt;br /&gt;
[[File:Icon-Ruby.png|64x64px||link=|alt=]]Ruby is an interpreted and object oriented scripting language with simple syntax created by Yukihiro Matsumoto.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
{{LanguageSupport|Ruby|the complete Phidget API, including events|all Phidget devices.|Linux and OS X|}}&lt;br /&gt;
&lt;br /&gt;
{{QuickDownloads|Ruby|&lt;br /&gt;
{{APIQuickDownloads|http://www.phidgets.com/documentation/rubydoc RubyDoc}}|&lt;br /&gt;
{{ExampleQuickDownloads|http://rubygems.org/gems/phidgets-ffi| (@RubyGems)}}|&lt;br /&gt;
{{ExtraLibraryQuickDownloads|http://rubygems.org/gems/phidgets-ffi|Ruby| (@RubyGems)}}&lt;br /&gt;
{{MacQuickDownloads}}&lt;br /&gt;
{{LinuxQuickDownloads}}&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
==Getting started with Ruby==&lt;br /&gt;
&lt;br /&gt;
If you are new to writing code for Phidgets, we recommend starting by running, then modifying existing examples. This will allow you to:&lt;br /&gt;
{{ExampleCodeReasons}}&lt;br /&gt;
&lt;br /&gt;
Instructions for OS X and Linux are similar, so they will be combined into the same section.&lt;br /&gt;
&lt;br /&gt;
The first step in using Ruby on Mac or Linux is to install the Phidget libraries.  Compile and install them as explained on the [[Device List|getting started guide for your device]].&lt;br /&gt;
&lt;br /&gt;
The next step is to install the phidgets-ffi gem. This is the library that allows you to program Phidgets with Ruby. For more information, please see phidgets-ffi at [http://rubygems.org/gems/phidgets-ffi RubyGems] and [https://github.com/kreynolds/phidgets-ffi GitHub].&lt;br /&gt;
&lt;br /&gt;
==Description of Library Files==&lt;br /&gt;
Ruby programs on OS X and Linux depend on the following files, which the installers put into your system. &lt;br /&gt;
&lt;br /&gt;
If you are using OS X, you will need the:&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|Phidget21.framework}}&amp;lt;/b&amp;gt; contains the actual Phidgets library for OS X, which is used at run-time.  &lt;br /&gt;
&lt;br /&gt;
If you are using Linux, you will need the:&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|libphidget21.so}}&amp;lt;/b&amp;gt; contains the actual Phidgets library for Linux, which is used at run-time.&lt;br /&gt;
&lt;br /&gt;
For both OS X and Linux, you will need the phidgets-ffi gem: &lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|phidgets-ffi}}&amp;lt;/b&amp;gt; is the Phidgets gem for Ruby. This gem contains the library as well as example code.&lt;br /&gt;
&lt;br /&gt;
==Installing phidgets-ffi==&lt;br /&gt;
&lt;br /&gt;
The phidgets-ffi gem relies on the [http://rubygems.org/gems/ffi ffi gem]. So, if you do not already have it installed, please do so. A version of ffi 1.0.9 or greater is needed. Type the following to install ffi:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  gem install ffi&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Next, please install the phidgets-ffi gem by typing the following into command line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  gem install phidgets-ffi&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For OS X systems, the gem will be installed into {{Code|/Library/Ruby/Gems/1.x/phidgets-ffi-x.x.x}}.&lt;br /&gt;
&lt;br /&gt;
For typical Linux systems, the gem will be installed into {{Code|var/lib/gems/1.9/gems/phidgets-ffi-x.x.x}}.&lt;br /&gt;
&lt;br /&gt;
===Use Our Examples===&lt;br /&gt;
&lt;br /&gt;
Open a command line terminal and navigate to the phidgets-ffi gem directory. {{Code|cd}} into the {{Code|examples}} folder. Here, you will find all of the examples available for Ruby.  If you aren&#039;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]].&lt;br /&gt;
&lt;br /&gt;
The easiest way to confirm that your environment is set up properly will be to compile and run the {{Code|HelloWorld}} Ruby example.&lt;br /&gt;
&lt;br /&gt;
The only thing left to do is to run the examples! Type the following into command line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  ruby HelloWorld.rb&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This program will detect for devices that are attached/detached on the computer. Go ahead, and attach or detach your devices! Here is an example output:&lt;br /&gt;
&lt;br /&gt;
[[File:Ruby Linux HelloWorld Output.PNG|link=|alt=HelloWorld Output]]&lt;br /&gt;
&lt;br /&gt;
After confirming that the {{Code|HelloWorld}} example is working, you can proceed to run the example for your device. If you aren&#039;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 ensure that you have set your start up project to be the one that matches your device before compiling. &lt;br /&gt;
&lt;br /&gt;
Once you have the Ruby examples running, we have a [[#Follow the Examples|teaching]] section below to help you follow them.&lt;br /&gt;
&lt;br /&gt;
==Write Your Own Code==&lt;br /&gt;
&lt;br /&gt;
When you are building a project from scratch, or adding Phidget function calls to an existing project, you&#039;ll need to configure your environment to properly link the Phidget Ruby libraries.&lt;br /&gt;
&lt;br /&gt;
Simply, add the following two lines to the beginning of any {{Code|.rb}} script to make use of the phidgets-ffi gem&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  require &#039;rubygems&#039;&lt;br /&gt;
  require &#039;phidgets-ffi&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The project now has access to the Phidget21 function calls and you are ready to begin coding.&lt;br /&gt;
&lt;br /&gt;
The same [[#Follow the Examples|teaching ]] section which describes the examples also has further resources for programming your Phidget.&lt;br /&gt;
&lt;br /&gt;
==Follow the Examples==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
Next, comes our API information.  These resources outline the Ruby Phidget functions:&lt;br /&gt;
&lt;br /&gt;
{{UsingAPhidgetInCodeGeneral|both of which are available in Ruby|[http://www.phidgets.com/documentation/rubydoc Ruby API]}}&lt;br /&gt;
&lt;br /&gt;
===Example Flow===&lt;br /&gt;
&lt;br /&gt;
{{ExamplePseudocode|In Ruby, you can name these &#039;&#039;&#039;event&#039;&#039;&#039; 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. &amp;lt;br&amp;gt; &lt;br /&gt;
In the example code, the event functions common to all Phidgets are called things like &#039;&#039;&#039;AttachHandler()&#039;&#039;&#039; and &#039;&#039;&#039;DetachHandler()&#039;&#039;&#039;, etc.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
Other functions are given in the examples to show you more detail on using your Phidget.  For example, &#039;&#039;&#039;DeviceInitialize()&#039;&#039;&#039; will show what needs to be set up for your Phidget before using it.&lt;br /&gt;
|Creating a Phidget software object in Ruby is specific to the Phidget.  For a Phidget Spatial, for example, this would involve creating a {{Code|Spatial}} object.  The examples show how to do this and other API functions.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The object provides device specific methods and properties which are available from the API for your specific Phidget.|&lt;br /&gt;
[http://www.phidgets.com/documentation/rubydoc Ruby API]}}&lt;br /&gt;
&lt;br /&gt;
===Code Snippets===&lt;br /&gt;
&lt;br /&gt;
Specific calls in Ruby will differ in syntax from those on the [[General Phidget Programming]] page, but the concepts stay the same.  &lt;br /&gt;
&lt;br /&gt;
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 Ruby 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.&lt;br /&gt;
&lt;br /&gt;
For example, if we were using a [[Product - 1018 - PhidgetInterfaceKit 8/8/8 | Phidget Interface Kit]] as our device, the general calls would look like this:&lt;br /&gt;
&lt;br /&gt;
====Step One: Initialize and Open====&lt;br /&gt;
&lt;br /&gt;
The Phidget constructor method will need to be called to create the Phidget object. There are two methods or programming a Phidget in Ruby: with and without a block. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  # Without a block&lt;br /&gt;
    device = Phidgets::InterfaceKit.new&lt;br /&gt;
&lt;br /&gt;
  # With a block&lt;br /&gt;
    Phidgets::InterfaceKit.new do |device|&lt;br /&gt;
      ..&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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. In the above example, InterfaceKit represents the PhidgetInterfaceKit. For simplicity in explaining the programming concepts, the remainder of the code snippet section will use the InterfaceKit object.&lt;br /&gt;
&lt;br /&gt;
For the most part, the two methods behave the same, but there are some subtle differences, which will be explained in the next section.&lt;br /&gt;
&lt;br /&gt;
====Step Two: Wait for Attachment (plugging in) of the Phidget==== &lt;br /&gt;
&lt;br /&gt;
The program needs to try and connect to the Phidget. Options can be added to the InterfaceKit constructor to connect to the first Phidget it finds, based on its serial number, label, or even connect across the network. Rubydocs API Documentation lists all of the available modes that the constructor provides. &lt;br /&gt;
&lt;br /&gt;
For example, the following will try to connect to the first Phidget it finds:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device= Phidgets::InterfaceKit.new&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following will try to connect to a Phidget over the Phidget WebService with a serial number of {{Code|99999}}, and a server id of {{Code|myserver}}: &lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  options = {:serial_number =&amp;gt; 99999, :server_id =&amp;gt; ‘myserver’, :password =&amp;gt; nil}&lt;br /&gt;
  device= Phidgets::InterfaceKit.new(options)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One important thing to remember is that when working with Phidgets, a local connection will reserve the device until closed. This prevents any other instances from retrieving data from the Phidget, including other programs. The one connection per device limit does not apply when exclusively using the Phidget WebService.&lt;br /&gt;
&lt;br /&gt;
{{Code|Phidgets::InterfaceKit.new}} will tell the program to continuously try to connect to a Phidget, based on the options given(if any), even trying to reconnect if it gets disconnected. &lt;br /&gt;
&lt;br /&gt;
=====Non Block Programming=====&lt;br /&gt;
For the non block programming method, simply calling the constructor does not guarantee you can use the Phidget immediately. We can handle this by using event driven programming and  tracking the {{Code|on_attach}} and {{Code|on_detach}} events, or by calling: {{Code|wait_for_attachment}}. {{Code|wait_for_attachment}} will block indefinitely until a connection is made to the Phidget, or an optional timeout is exceeded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device= Phidgets::InterfaceKit.new&lt;br /&gt;
  device.wait_for_attachment 2000 #halt the program for up 2000 milliseconds or until the Phidget is connected&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Please also remember to call close at the end or the program to free any locks on the Phidget&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device.close&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=====Block Programming=====&lt;br /&gt;
If you are programming inside a block, {{Code|wait_for_attachment}} is automatically called. By default, it will halt the program and try to connect to the Phidget for up to 1000 milliseconds.  Afterwards, the block will yield. Finally, close is not needed as it is automatically called once the block has yield.&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  options = {:timeout =&amp;gt; 2000)&lt;br /&gt;
  Phidgets::InterfaceKit.new(options) do |device|&lt;br /&gt;
    ... &lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Sometimes, it makes more sense to handle the attachment via an event.  This would be in instances where the Phidget is being plugged and unplugged, and you want to handle these incidents.  Or, when you want to use event-driven programming because you have a GUI-driven program.  In these cases, an event-driven code snippet to handle the attachment might look something like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device.on_attach  do |device, obj|&lt;br /&gt;
    puts &amp;quot;Id: #{device.id}&amp;quot;&lt;br /&gt;
    puts &amp;quot;Serial number: #{device.serial_number}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Step Three: Do Things with the Phidget====&lt;br /&gt;
&lt;br /&gt;
You can read data and interact with your Phidget both by polling it for its current state (or to set a state), or by catching events that trigger when the data changes.&lt;br /&gt;
&lt;br /&gt;
For our [[Product - 1018 - PhidgetInterfaceKit 8/8/8 | Phidget Interface Kit]], the polling method of getting the current sensor state and setting an output state looks something like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
&lt;br /&gt;
# Get a data point from Analog Port 0&lt;br /&gt;
puts &amp;quot;Sensor value[0]: #{device.sensors[0].to_i}&amp;quot; &lt;br /&gt;
&lt;br /&gt;
# Set digital output port 0 to be on&lt;br /&gt;
device.outputs[0].state = true&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To catch data changes via events, you would use something like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device.on_sensor_change do |device, input, value, obj|&lt;br /&gt;
    puts &amp;quot;Sensor #{input.index}&#039;s value has changed to #{value}&amp;quot;&lt;br /&gt;
  end &lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=====Step Four: Close=====&lt;br /&gt;
 &lt;br /&gt;
If you are using the block programming method, you do not need to worry about closing the Phidget. It is already taken care of when the block ends. However, if you are using the non block programming method, then you will need take special consideration that the Phidget is closed. &lt;br /&gt;
&lt;br /&gt;
At the end of your program, don’t forget to call {{Code|close}} to free any locks on the Phidget that the Phidget constructor call put in place!&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device.close&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;complete&#039;&#039; set of functions you have available for all Phidgets can be found in the [http://rubydoc.info/gems/phidgets-ffi/0.1.1/frames Ruby API]. You can also find more description on any device-specific function either in the [[Device List|Device API]] page for calls available only on your specific Phidget.&lt;br /&gt;
&lt;br /&gt;
=====Raw FFI=====&lt;br /&gt;
As an alternative to programming with the method as outlined in this document, you can also program making straight C calls through FFI. Please refer to the files in {{Code|Ruby Gems Directory/phidgets-ffi-x.x.x/lib/phidgets-ffi/ffi}} to see a list of available methods, and the [http://www.phidgets.com/documentation/Phidget21_C_Doc.zip] for usage. There are raw ffi examples in {{Code|Ruby Gems directory/phidgets-ffi-x.x.x/examples/raw-ffi}}.&lt;br /&gt;
&lt;br /&gt;
==Common Problems and Solutions/Workarounds==&lt;br /&gt;
&lt;br /&gt;
Here you can put various frequent problems and our recommended solutions.&lt;/div&gt;</summary>
		<author><name>Mphi</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs21/index.php?title=Language_-_Ruby&amp;diff=19115</id>
		<title>Language - Ruby</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs21/index.php?title=Language_-_Ruby&amp;diff=19115"/>
		<updated>2012-04-19T21:35:03Z</updated>

		<summary type="html">&lt;p&gt;Mphi: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Language]]&lt;br /&gt;
[[File:Icon-Ruby.png|64x64px||link=|alt=]]Ruby is an interpreted and object oriented scripting language with simple syntax created by Yukihiro Matsumoto.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
{{LanguageSupport|Ruby|the complete Phidget API, including events|all Phidget devices.|Linux and OS X|}}&lt;br /&gt;
&lt;br /&gt;
{{QuickDownloads|Ruby|&lt;br /&gt;
{{APIQuickDownloads|http://www.phidgets.com/documentation/rubydoc RubyDoc}}|&lt;br /&gt;
{{ExampleQuickDownloads|http://rubygems.org/gems/phidgets-ffi| (@RubyGems)}}|&lt;br /&gt;
{{ExtraLibraryQuickDownloads|http://rubygems.org/gems/phidgets-ffi|Ruby| (@RubyGems)}}&lt;br /&gt;
{{MacQuickDownloads}}&lt;br /&gt;
{{LinuxQuickDownloads}}&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
==Getting started with Ruby==&lt;br /&gt;
&lt;br /&gt;
If you are new to writing code for Phidgets, we recommend starting by running, then modifying existing examples. This will allow you to:&lt;br /&gt;
{{ExampleCodeReasons}}&lt;br /&gt;
&lt;br /&gt;
Instructions for OS X and Linux are similar, so they will be combined into the same section.&lt;br /&gt;
&lt;br /&gt;
The first step in using Ruby on Mac or Linux is to install the Phidget libraries.  Compile and install them as explained on the [[Device List|getting started guide for your device]].&lt;br /&gt;
&lt;br /&gt;
The next step is to install the phidgets-ffi gem. This is the library that allows you to program Phidgets with Ruby. For more information, please see phidgets-ffi at [http://rubygems.org/gems/phidgets-ffi RubyGems] and [https://github.com/kreynolds/phidgets-ffi GitHub].&lt;br /&gt;
&lt;br /&gt;
==Description of Library Files==&lt;br /&gt;
Ruby programs on OS X and Linux depend on the following files, which the installers put into your system. &lt;br /&gt;
&lt;br /&gt;
If you are using OS X, you will need the:&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|Phidget21.framework}}&amp;lt;/b&amp;gt; contains the actual Phidgets library for OS X, which is used at run-time.  &lt;br /&gt;
&lt;br /&gt;
If you are using Linux, you will need the:&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|libphidget21.so}}&amp;lt;/b&amp;gt; contains the actual Phidgets library for Linux, which is used at run-time.&lt;br /&gt;
&lt;br /&gt;
For both OS X and Linux, you will need the phidgets-ffi gem: &lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|phidgets-ffi}}&amp;lt;/b&amp;gt; is the Phidgets gem for Ruby. This gem contains the library as well as example code.&lt;br /&gt;
&lt;br /&gt;
==Installing phidgets-ffi==&lt;br /&gt;
&lt;br /&gt;
The phidgets-ffi gem relies on the [http://rubygems.org/gems/ffi ffi gem]. So, if you do not already have it installed, please do so. A version of ffi 1.0.9 or greater is needed. Type the following to install ffi:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  gem install ffi&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Next, please install the phidgets-ffi gem by typing the following into command line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  gem install phidgets-ffi&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For OS X systems, the gem will be installed into {{Code|/Library/Ruby/Gems/1.x/phidgets-ffi-x.x.x}}.&lt;br /&gt;
&lt;br /&gt;
For typical Linux systems, the gem will be installed into {{Code|var/lib/gems/1.9/gems/phidgets-ffi-x.x.x}}.&lt;br /&gt;
&lt;br /&gt;
===Use Our Examples===&lt;br /&gt;
&lt;br /&gt;
Open a command line terminal and navigate to the phidgets-ffi gem directory. {{Code|cd}} into the {{Code|examples}} folder. Here, you will find all of the examples available for Ruby.  If you aren&#039;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]].&lt;br /&gt;
&lt;br /&gt;
The easiest way to confirm that your environment is set up properly will be to compile and run the {{Code|HelloWorld}} Ruby example.&lt;br /&gt;
&lt;br /&gt;
The only thing left to do is to run the examples! Type the following into command line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  ruby HelloWorld.rb&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This program will detect for devices that are attached/detached on the computer. Go ahead, and attach or detach your devices! Here is an example output:&lt;br /&gt;
&lt;br /&gt;
[[File:Ruby Linux HelloWorld Output.PNG|link=|alt=HelloWorld Output]]&lt;br /&gt;
&lt;br /&gt;
After confirming that the {{Code|HelloWorld}} example is working, you can proceed to run the example for your device. If you aren&#039;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 ensure that you have set your start up project to be the one that matches your device before compiling. &lt;br /&gt;
&lt;br /&gt;
Once you have the Ruby examples running, we have a [[#Follow the Examples|teaching]] section below to help you follow them.&lt;br /&gt;
&lt;br /&gt;
==Write Your Own Code==&lt;br /&gt;
&lt;br /&gt;
When you are building a project from scratch, or adding Phidget function calls to an existing project, you&#039;ll need to configure your environment to properly link the Phidget Ruby libraries.&lt;br /&gt;
&lt;br /&gt;
Simply, add the following two lines to the beginning of any {{Code|.rb}} script to make use of the phidgets-ffi gem&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  require &#039;rubygems&#039;&lt;br /&gt;
  require &#039;phidgets-ffi&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The project now has access to the Phidget21 function calls and you are ready to begin coding.&lt;br /&gt;
&lt;br /&gt;
The same [[#Follow the Examples|teaching ]] section which describes the examples also has further resources for programming your Phidget.&lt;br /&gt;
&lt;br /&gt;
==Follow the Examples==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
Next, comes our API information.  These resources outline the Ruby Phidget functions:&lt;br /&gt;
&lt;br /&gt;
{{UsingAPhidgetInCodeGeneral|both of which are available in Ruby|[http://www.phidgets.com/documentation/rubydoc Ruby API]}}&lt;br /&gt;
&lt;br /&gt;
===Example Flow===&lt;br /&gt;
&lt;br /&gt;
{{ExamplePseudocode|In Ruby, you can name these &#039;&#039;&#039;event&#039;&#039;&#039; 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. &amp;lt;br&amp;gt; &lt;br /&gt;
In the example code, the event functions common to all Phidgets are called things like &#039;&#039;&#039;AttachHandler()&#039;&#039;&#039; and &#039;&#039;&#039;DetachHandler()&#039;&#039;&#039;, etc.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
Other functions are given in the examples to show you more detail on using your Phidget.  For example, &#039;&#039;&#039;DeviceInitialize()&#039;&#039;&#039; will show what needs to be set up for your Phidget before using it.&lt;br /&gt;
|Creating a Phidget software object in Ruby is specific to the Phidget.  For a Phidget Spatial, for example, this would involve creating a {{Code|Spatial}} object.  The examples show how to do this and other API functions.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The object provides device specific methods and properties which are available from the API for your specific Phidget.|&lt;br /&gt;
[http://www.phidgets.com/documentation/rubydoc Ruby API]}}&lt;br /&gt;
&lt;br /&gt;
===Code Snippets===&lt;br /&gt;
&lt;br /&gt;
Specific calls in Ruby will differ in syntax from those on the [[General Phidget Programming]] page, but the concepts stay the same.  &lt;br /&gt;
&lt;br /&gt;
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 Ruby 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.&lt;br /&gt;
&lt;br /&gt;
For example, if we were using a [[Product - 1018 - PhidgetInterfaceKit 8/8/8 | Phidget Interface Kit]] as our device, the general calls would look like this:&lt;br /&gt;
&lt;br /&gt;
====Step One: Initialize and Open====&lt;br /&gt;
&lt;br /&gt;
The Phidget constructor method will need to be called to create the Phidget object. There are two methods or programming a Phidget in Ruby: with and without a block. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  # Without a block&lt;br /&gt;
    device = Phidgets::InterfaceKit.new&lt;br /&gt;
&lt;br /&gt;
  # With a block&lt;br /&gt;
    Phidgets::InterfaceKit.new do |device|&lt;br /&gt;
      ..&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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. In the above example, InterfaceKit represents the PhidgetInterfaceKit. For simplicity in explaining the programming concepts, the remainder of the code snippet section will use the InterfaceKit object.&lt;br /&gt;
&lt;br /&gt;
For the most part, the two methods behave the same, but there are some subtle differences, which will be explained in the next section.&lt;br /&gt;
&lt;br /&gt;
====Step Two: Wait for Attachment (plugging in) of the Phidget==== &lt;br /&gt;
&lt;br /&gt;
The program needs to try and connect to the Phidget. Options can be added to the InterfaceKit constructor to connect to the first Phidget it finds, based on its serial number, label, or even connect across the network. Rubydocs API Documentation lists all of the available modes that the constructor provides. &lt;br /&gt;
&lt;br /&gt;
For example, the following will try to connect to the first Phidget it finds:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device= Phidgets::InterfaceKit.new&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following will try to connect to a Phidget over the Phidget WebService with a serial number of {{Code|99999}}, and a server id of {{Code|myserver}}: &lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  options = {:serial_number =&amp;gt; 99999, :server_id =&amp;gt; ‘myserver’, :password =&amp;gt; nil}&lt;br /&gt;
  device= Phidgets::InterfaceKit.new(options)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One important thing to remember is that when working with Phidgets, a local connection will reserve the device until closed. This prevents any other instances from retrieving data from the Phidget, including other programs. The one connection per device limit does not apply when exclusively using the Phidget WebService.&lt;br /&gt;
&lt;br /&gt;
{{Code|Phidgets::InterfaceKit.new}} will tell the program to continuously try to connect to a Phidget, based on the options given(if any), even trying to reconnect if it gets disconnected. &lt;br /&gt;
&lt;br /&gt;
=====Non Block Programming=====&lt;br /&gt;
For the non block programming method, simply calling the constructor does not guarantee you can use the Phidget immediately. We can handle this by using event driven programming and  tracking the {{Code|on_attach}} and {{Code|on_detach}} events, or by calling: {{Code|wait_for_attachment}}. {{Code|wait_for_attachment}} will block indefinitely until a connection is made to the Phidget, or an optional timeout is exceeded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device= Phidgets::InterfaceKit.new&lt;br /&gt;
  device.wait_for_attachment 2000 #halt the program for up 2000 milliseconds or until the Phidget is connected&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Please also remember to call close at the end or the program to free any locks on the Phidget&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device.close&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=====Block Programming=====&lt;br /&gt;
If you are programming inside a block, {{Code|wait_for_attachment}} is automatically called. By default, it will halt the program and try to connect to the Phidget for up to 1000 milliseconds.  Afterwards, the block will yield. Finally, close is not needed as it is automatically called once the block has yield.&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  options = {:timeout =&amp;gt; 2000)&lt;br /&gt;
  Phidgets::InterfaceKit.new(options) do |device|&lt;br /&gt;
    ... &lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Sometimes, it makes more sense to handle the attachment via an event.  This would be in instances where the Phidget is being plugged and unplugged, and you want to handle these incidents.  Or, when you want to use event-driven programming because you have a GUI-driven program.  In these cases, an event-driven code snippet to handle the attachment might look something like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device.on_attach  do |device, obj|&lt;br /&gt;
    puts &amp;quot;Id: #{device.id}&amp;quot;&lt;br /&gt;
    puts &amp;quot;Serial number: #{device.serial_number}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Step Three: Do Things with the Phidget====&lt;br /&gt;
&lt;br /&gt;
You can read data and interact with your Phidget both by polling it for its current state (or to set a state), or by catching events that trigger when the data changes.&lt;br /&gt;
&lt;br /&gt;
For our [[Product - 1018 - PhidgetInterfaceKit 8/8/8 | Phidget Interface Kit]], the polling method of getting the current sensor state and setting an output state looks something like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
&lt;br /&gt;
# Get a data point from Analog Port 0&lt;br /&gt;
puts &amp;quot;Sensor value[0]: #{device.sensors[0].to_i}&amp;quot; &lt;br /&gt;
&lt;br /&gt;
# Set digital output port 0 to be on&lt;br /&gt;
device.outputs[0].state = true&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To catch data changes via events, you would use something like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device.on_sensor_change do |device, input, value, obj|&lt;br /&gt;
    puts &amp;quot;Sensor #{input.index}&#039;s value has changed to #{value}&amp;quot;&lt;br /&gt;
  end &lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=====Step Four: Close=====&lt;br /&gt;
 &lt;br /&gt;
If you are using the block programming method, you do not need to worry about closing the Phidget. It is already taken care of when the block ends. However, if you are using the non block programming method, then you will need take special consideration that the Phidget is closed. &lt;br /&gt;
&lt;br /&gt;
At the end of your program, don’t forget to call {{Code|close}} to free any locks on the Phidget that the Phidget constructor call put in place!&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device.close&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;complete&#039;&#039; set of functions you have available for all Phidgets can be found in the [http://rubydoc.info/gems/phidgets-ffi/0.1.1/frames Ruby API]. You can also find more description on any device-specific function either in the [[Device List|Device API]] page for calls available only on your specific Phidget.&lt;br /&gt;
&lt;br /&gt;
=====Raw FFI=====&lt;br /&gt;
As an alternative to programming with the method as outlined in this document, you can also program making straight C calls through FFI. Please refer to the files in {{Code|Ruby Gems Directory/phidgets-ffi-x.x.x/lib/phidgets-ffi/ffi}} to see a list of available methods, and the [[http://www.phidgets.com/documentation/Phidget21_C_Doc.zip]] for usage. There are raw ffi examples in {{Code|Ruby Gems directory/phidgets-ffi-x.x.x/examples/raw-ffi}}.&lt;br /&gt;
&lt;br /&gt;
==Common Problems and Solutions/Workarounds==&lt;br /&gt;
&lt;br /&gt;
Here you can put various frequent problems and our recommended solutions.&lt;/div&gt;</summary>
		<author><name>Mphi</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs21/index.php?title=Language_-_Ruby&amp;diff=19114</id>
		<title>Language - Ruby</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs21/index.php?title=Language_-_Ruby&amp;diff=19114"/>
		<updated>2012-04-19T21:31:22Z</updated>

		<summary type="html">&lt;p&gt;Mphi: /* Raw FFI */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Language]]&lt;br /&gt;
[[File:Icon-Ruby.png|64x64px||link=|alt=]]Ruby is an interpreted and object oriented scripting language with simple syntax created by Yukihiro Matsumoto.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
{{LanguageSupport|Ruby|the complete Phidget API, including events|all Phidget devices.|Linux and OS X|}}&lt;br /&gt;
&lt;br /&gt;
{{QuickDownloads|Ruby|&lt;br /&gt;
{{APIQuickDownloads|http://www.phidgets.com/documentation/rubydoc RubyDoc}}|&lt;br /&gt;
{{ExampleQuickDownloads|http://rubygems.org/gems/phidgets-ffi| (@RubyGems)}}|&lt;br /&gt;
{{ExtraLibraryQuickDownloads|http://rubygems.org/gems/phidgets-ffi|Ruby| (@RubyGems)}}&lt;br /&gt;
{{MacQuickDownloads}}&lt;br /&gt;
{{LinuxQuickDownloads}}&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
==Getting started with Ruby==&lt;br /&gt;
&lt;br /&gt;
If you are new to writing code for Phidgets, we recommend starting by running, then modifying existing examples. This will allow you to:&lt;br /&gt;
{{ExampleCodeReasons}}&lt;br /&gt;
&lt;br /&gt;
Instructions for OS X and Linux are similar, so they will be combined into the same section.&lt;br /&gt;
&lt;br /&gt;
The first step in using Ruby on Mac or Linux is to install the Phidget libraries.  Compile and install them as explained on the [[Device List|getting started guide for your device]].&lt;br /&gt;
&lt;br /&gt;
The next step is to install the phidgets-ffi gem. This is the library that allows you to program Phidgets with Ruby. For more information, please see phidgets-ffi at [http://rubygems.org/gems/phidgets-ffi RubyGems] and [https://github.com/kreynolds/phidgets-ffi GitHub].&lt;br /&gt;
&lt;br /&gt;
==Description of Library Files==&lt;br /&gt;
Ruby programs on OS X and Linux depend on the following files, which the installers put into your system. &lt;br /&gt;
&lt;br /&gt;
If you are using OS X, you will need the:&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|Phidget21.framework}}&amp;lt;/b&amp;gt; contains the actual Phidgets library for OS X, which is used at run-time.  &lt;br /&gt;
&lt;br /&gt;
If you are using Linux, you will need the:&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|libphidget21.so}}&amp;lt;/b&amp;gt; contains the actual Phidgets library for Linux, which is used at run-time.&lt;br /&gt;
&lt;br /&gt;
For both OS X and Linux, you will need the phidgets-ffi gem: &lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|phidgets-ffi}}&amp;lt;/b&amp;gt; is the Phidgets gem for Ruby. This gem contains the library as well as example code.&lt;br /&gt;
&lt;br /&gt;
==Installing phidgets-ffi==&lt;br /&gt;
&lt;br /&gt;
The phidgets-ffi gem relies on the [http://rubygems.org/gems/ffi ffi gem]. So, if you do not already have it installed, please do so. A version of ffi 1.0.9 or greater is needed. Type the following to install ffi:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  gem install ffi&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Next, please install the phidgets-ffi gem by typing the following into command line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  gem install phidgets-ffi&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For OS X systems, the gem will be installed into {{Code|/Library/Ruby/Gems/1.x/phidgets-ffi-x.x.x}}.&lt;br /&gt;
&lt;br /&gt;
For typical Linux systems, the gem will be installed into {{Code|var/lib/gems/1.9/gems/phidgets-ffi-x.x.x}}.&lt;br /&gt;
&lt;br /&gt;
===Use Our Examples===&lt;br /&gt;
&lt;br /&gt;
Open a command line terminal and navigate to the phidgets-ffi gem directory. {{Code|cd}} into the {{Code|examples}} folder. Here, you will find all of the examples available for Ruby.  If you aren&#039;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]].&lt;br /&gt;
&lt;br /&gt;
The easiest way to confirm that your environment is set up properly will be to compile and run the {{Code|HelloWorld}} Ruby example.&lt;br /&gt;
&lt;br /&gt;
The only thing left to do is to run the examples! Type the following into command line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  ruby HelloWorld.rb&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This program will detect for devices that are attached/detached on the computer. Go ahead, and attach or detach your devices! Here is an example output:&lt;br /&gt;
&lt;br /&gt;
[[File:Ruby Linux HelloWorld Output.PNG|link=|alt=HelloWorld Output]]&lt;br /&gt;
&lt;br /&gt;
After confirming that the {{Code|HelloWorld}} example is working, you can proceed to run the example for your device. If you aren&#039;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 ensure that you have set your start up project to be the one that matches your device before compiling. &lt;br /&gt;
&lt;br /&gt;
Once you have the Ruby examples running, we have a [[#Follow the Examples|teaching]] section below to help you follow them.&lt;br /&gt;
&lt;br /&gt;
==Write Your Own Code==&lt;br /&gt;
&lt;br /&gt;
When you are building a project from scratch, or adding Phidget function calls to an existing project, you&#039;ll need to configure your environment to properly link the Phidget Ruby libraries.&lt;br /&gt;
&lt;br /&gt;
Simply, add the following two lines to the beginning of any {{Code|.rb}} script to make use of the phidgets-ffi gem&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  require &#039;rubygems&#039;&lt;br /&gt;
  require &#039;phidgets-ffi&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The project now has access to the Phidget21 function calls and you are ready to begin coding.&lt;br /&gt;
&lt;br /&gt;
The same [[#Follow the Examples|teaching ]] section which describes the examples also has further resources for programming your Phidget.&lt;br /&gt;
&lt;br /&gt;
==Follow the Examples==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
Next, comes our API information.  These resources outline the Ruby Phidget functions:&lt;br /&gt;
&lt;br /&gt;
{{UsingAPhidgetInCodeGeneral|both of which are available in Ruby|[http://www.phidgets.com/documentation/rubydoc Ruby API]}}&lt;br /&gt;
&lt;br /&gt;
===Example Flow===&lt;br /&gt;
&lt;br /&gt;
{{ExamplePseudocode|In Ruby, you can name these &#039;&#039;&#039;event&#039;&#039;&#039; 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. &amp;lt;br&amp;gt; &lt;br /&gt;
In the example code, the event functions common to all Phidgets are called things like &#039;&#039;&#039;AttachHandler()&#039;&#039;&#039; and &#039;&#039;&#039;DetachHandler()&#039;&#039;&#039;, etc.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
Other functions are given in the examples to show you more detail on using your Phidget.  For example, &#039;&#039;&#039;DeviceInitialize()&#039;&#039;&#039; will show what needs to be set up for your Phidget before using it.&lt;br /&gt;
|Creating a Phidget software object in Ruby is specific to the Phidget.  For a Phidget Spatial, for example, this would involve creating a {{Code|Spatial}} object.  The examples show how to do this and other API functions.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The object provides device specific methods and properties which are available from the API for your specific Phidget.|&lt;br /&gt;
[http://www.phidgets.com/documentation/rubydoc Ruby API]}}&lt;br /&gt;
&lt;br /&gt;
===Code Snippets===&lt;br /&gt;
&lt;br /&gt;
Specific calls in Python will differ in syntax from those on the [[General Phidget Programming]] page, but the concepts stay the same.  &lt;br /&gt;
&lt;br /&gt;
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 Python 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.&lt;br /&gt;
&lt;br /&gt;
For example, if we were using a [[Product - 1018 - PhidgetInterfaceKit 8/8/8 | Phidget Interface Kit]] as our device, the general calls would look like this:&lt;br /&gt;
&lt;br /&gt;
====Step One: Initialize and Open====&lt;br /&gt;
&lt;br /&gt;
The Phidget constructor method will need to be called to create the Phidget object. There are two methods or programming a Phidget in Ruby: with and without a block. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  # Without a block&lt;br /&gt;
    device = Phidgets::InterfaceKit.new&lt;br /&gt;
&lt;br /&gt;
  # With a block&lt;br /&gt;
    Phidgets::InterfaceKit.new do |device|&lt;br /&gt;
      ..&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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. In the above example, InterfaceKit represents the PhidgetInterfaceKit. For simplicity in explaining the programming concepts, the remainder of the code snippet section will use the InterfaceKit object.&lt;br /&gt;
&lt;br /&gt;
For the most part, the two methods behave the same, but there are some subtle differences, which will be explained in the next section.&lt;br /&gt;
&lt;br /&gt;
====Step Two: Wait for Attachment (plugging in) of the Phidget==== &lt;br /&gt;
&lt;br /&gt;
The program needs to try and connect to the Phidget. Options can be added to the InterfaceKit constructor to connect to the first Phidget it finds, based on its serial number, label, or even connect across the network. Rubydocs API Documentation lists all of the available modes that the constructor provides. &lt;br /&gt;
&lt;br /&gt;
For example, the following will try to connect to the first Phidget it finds:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device= Phidgets::InterfaceKit.new&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following will try to connect to a Phidget over the Phidget WebService with a serial number of {{Code|99999}}, and a server id of {{Code|myserver}}: &lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  options = {:serial_number =&amp;gt; 99999, :server_id =&amp;gt; ‘myserver’, :password =&amp;gt; nil}&lt;br /&gt;
  device= Phidgets::InterfaceKit.new(options)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One important thing to remember is that when working with Phidgets, a local connection will reserve the device until closed. This prevents any other instances from retrieving data from the Phidget, including other programs. The one connection per device limit does not apply when exclusively using the Phidget WebService.&lt;br /&gt;
&lt;br /&gt;
{{Code|Phidgets::InterfaceKit.new}} will tell the program to continuously try to connect to a Phidget, based on the options given(if any), even trying to reconnect if it gets disconnected. &lt;br /&gt;
&lt;br /&gt;
=====Non Block Programming=====&lt;br /&gt;
For the non block programming method, simply calling the constructor does not guarantee you can use the Phidget immediately. We can handle this by using event driven programming and  tracking the {{Code|on_attach}} and {{Code|on_detach}} events, or by calling: {{Code|wait_for_attachment}}. {{Code|wait_for_attachment}} will block indefinitely until a connection is made to the Phidget, or an optional timeout is exceeded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device= Phidgets::InterfaceKit.new&lt;br /&gt;
  device.wait_for_attachment 2000 #halt the program for up 2000 milliseconds or until the Phidget is connected&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Please also remember to call close at the end or the program to free any locks on the Phidget&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device.close&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=====Block Programming=====&lt;br /&gt;
If you are programming inside a block, {{Code|wait_for_attachment}} is automatically called. By default, it will halt the program and try to connect to the Phidget for up to 1000 milliseconds.  Afterwards, the block will yield. Finally, close is not needed as it is automatically called once the block has yield.&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  options = {:timeout =&amp;gt; 2000)&lt;br /&gt;
  Phidgets::InterfaceKit.new(options) do |device|&lt;br /&gt;
    ... &lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Sometimes, it makes more sense to handle the attachment via an event.  This would be in instances where the Phidget is being plugged and unplugged, and you want to handle these incidents.  Or, when you want to use event-driven programming because you have a GUI-driven program.  In these cases, an event-driven code snippet to handle the attachment might look something like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device.on_attach  do |device, obj|&lt;br /&gt;
    puts &amp;quot;Id: #{device.id}&amp;quot;&lt;br /&gt;
    puts &amp;quot;Serial number: #{device.serial_number}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Step Three: Do Things with the Phidget====&lt;br /&gt;
&lt;br /&gt;
You can read data and interact with your Phidget both by polling it for its current state (or to set a state), or by catching events that trigger when the data changes.&lt;br /&gt;
&lt;br /&gt;
For our [[Product - 1018 - PhidgetInterfaceKit 8/8/8 | Phidget Interface Kit]], the polling method of getting the current sensor state and setting an output state looks something like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
&lt;br /&gt;
# Get a data point from Analog Port 0&lt;br /&gt;
puts &amp;quot;Sensor value[0]: #{device.sensors[0].to_i}&amp;quot; &lt;br /&gt;
&lt;br /&gt;
# Set digital output port 0 to be on&lt;br /&gt;
device.outputs[0].state = true&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To catch data changes via events, you would use something like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device.on_sensor_change do |device, input, value, obj|&lt;br /&gt;
    puts &amp;quot;Sensor #{input.index}&#039;s value has changed to #{value}&amp;quot;&lt;br /&gt;
  end &lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=====Step Four: Close=====&lt;br /&gt;
 &lt;br /&gt;
If you are using the block programming method, you do not need to worry about closing the Phidget. It is already taken care of when the block ends. However, if you are using the non block programming method, then you will need take special consideration that the Phidget is closed. &lt;br /&gt;
&lt;br /&gt;
At the end of your program, don’t forget to call {{Code|close}} to free any locks on the Phidget that the Phidget constructor call put in place!&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device.close&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;complete&#039;&#039; set of functions you have available for all Phidgets can be found in the [http://www.phidgets.com/documentation/PythonDoc.zip Python API]. You can also find more description on any device-specific function either in the [[Device List|Device API]] page for calls available only on your specific Phidget.&lt;br /&gt;
&lt;br /&gt;
=====Raw FFI=====&lt;br /&gt;
As an alternative to programming with the method as outlined in this document, you can also program making straight C calls through FFI. Please refer to the files in {{Code|Ruby Gems Directory/phidgets-ffi-x.x.x/lib/phidgets-ffi/ffi}} to see a list of available methods, and the [[http://www.phidgets.com/documentation/Phidget21_C_Doc.zip]] for usage. There are raw ffi examples in {{Code|Ruby Gems directory/phidgets-ffi-x.x.x/examples/raw-ffi}}.&lt;br /&gt;
&lt;br /&gt;
==Common Problems and Solutions/Workarounds==&lt;br /&gt;
&lt;br /&gt;
Here you can put various frequent problems and our recommended solutions.&lt;/div&gt;</summary>
		<author><name>Mphi</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs21/index.php?title=Language_-_Ruby&amp;diff=19113</id>
		<title>Language - Ruby</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs21/index.php?title=Language_-_Ruby&amp;diff=19113"/>
		<updated>2012-04-19T21:29:48Z</updated>

		<summary type="html">&lt;p&gt;Mphi: /* Raw FFI */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Language]]&lt;br /&gt;
[[File:Icon-Ruby.png|64x64px||link=|alt=]]Ruby is an interpreted and object oriented scripting language with simple syntax created by Yukihiro Matsumoto.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
{{LanguageSupport|Ruby|the complete Phidget API, including events|all Phidget devices.|Linux and OS X|}}&lt;br /&gt;
&lt;br /&gt;
{{QuickDownloads|Ruby|&lt;br /&gt;
{{APIQuickDownloads|http://www.phidgets.com/documentation/rubydoc RubyDoc}}|&lt;br /&gt;
{{ExampleQuickDownloads|http://rubygems.org/gems/phidgets-ffi| (@RubyGems)}}|&lt;br /&gt;
{{ExtraLibraryQuickDownloads|http://rubygems.org/gems/phidgets-ffi|Ruby| (@RubyGems)}}&lt;br /&gt;
{{MacQuickDownloads}}&lt;br /&gt;
{{LinuxQuickDownloads}}&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
==Getting started with Ruby==&lt;br /&gt;
&lt;br /&gt;
If you are new to writing code for Phidgets, we recommend starting by running, then modifying existing examples. This will allow you to:&lt;br /&gt;
{{ExampleCodeReasons}}&lt;br /&gt;
&lt;br /&gt;
Instructions for OS X and Linux are similar, so they will be combined into the same section.&lt;br /&gt;
&lt;br /&gt;
The first step in using Ruby on Mac or Linux is to install the Phidget libraries.  Compile and install them as explained on the [[Device List|getting started guide for your device]].&lt;br /&gt;
&lt;br /&gt;
The next step is to install the phidgets-ffi gem. This is the library that allows you to program Phidgets with Ruby. For more information, please see phidgets-ffi at [http://rubygems.org/gems/phidgets-ffi RubyGems] and [https://github.com/kreynolds/phidgets-ffi GitHub].&lt;br /&gt;
&lt;br /&gt;
==Description of Library Files==&lt;br /&gt;
Ruby programs on OS X and Linux depend on the following files, which the installers put into your system. &lt;br /&gt;
&lt;br /&gt;
If you are using OS X, you will need the:&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|Phidget21.framework}}&amp;lt;/b&amp;gt; contains the actual Phidgets library for OS X, which is used at run-time.  &lt;br /&gt;
&lt;br /&gt;
If you are using Linux, you will need the:&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|libphidget21.so}}&amp;lt;/b&amp;gt; contains the actual Phidgets library for Linux, which is used at run-time.&lt;br /&gt;
&lt;br /&gt;
For both OS X and Linux, you will need the phidgets-ffi gem: &lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|phidgets-ffi}}&amp;lt;/b&amp;gt; is the Phidgets gem for Ruby. This gem contains the library as well as example code.&lt;br /&gt;
&lt;br /&gt;
==Installing phidgets-ffi==&lt;br /&gt;
&lt;br /&gt;
The phidgets-ffi gem relies on the [http://rubygems.org/gems/ffi ffi gem]. So, if you do not already have it installed, please do so. A version of ffi 1.0.9 or greater is needed. Type the following to install ffi:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  gem install ffi&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Next, please install the phidgets-ffi gem by typing the following into command line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  gem install phidgets-ffi&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For OS X systems, the gem will be installed into {{Code|/Library/Ruby/Gems/1.x/phidgets-ffi-x.x.x}}.&lt;br /&gt;
&lt;br /&gt;
For typical Linux systems, the gem will be installed into {{Code|var/lib/gems/1.9/gems/phidgets-ffi-x.x.x}}.&lt;br /&gt;
&lt;br /&gt;
===Use Our Examples===&lt;br /&gt;
&lt;br /&gt;
Open a command line terminal and navigate to the phidgets-ffi gem directory. {{Code|cd}} into the {{Code|examples}} folder. Here, you will find all of the examples available for Ruby.  If you aren&#039;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]].&lt;br /&gt;
&lt;br /&gt;
The easiest way to confirm that your environment is set up properly will be to compile and run the {{Code|HelloWorld}} Ruby example.&lt;br /&gt;
&lt;br /&gt;
The only thing left to do is to run the examples! Type the following into command line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  ruby HelloWorld.rb&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This program will detect for devices that are attached/detached on the computer. Go ahead, and attach or detach your devices! Here is an example output:&lt;br /&gt;
&lt;br /&gt;
[[File:Ruby Linux HelloWorld Output.PNG|link=|alt=HelloWorld Output]]&lt;br /&gt;
&lt;br /&gt;
After confirming that the {{Code|HelloWorld}} example is working, you can proceed to run the example for your device. If you aren&#039;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 ensure that you have set your start up project to be the one that matches your device before compiling. &lt;br /&gt;
&lt;br /&gt;
Once you have the Ruby examples running, we have a [[#Follow the Examples|teaching]] section below to help you follow them.&lt;br /&gt;
&lt;br /&gt;
==Write Your Own Code==&lt;br /&gt;
&lt;br /&gt;
When you are building a project from scratch, or adding Phidget function calls to an existing project, you&#039;ll need to configure your environment to properly link the Phidget Ruby libraries.&lt;br /&gt;
&lt;br /&gt;
Simply, add the following two lines to the beginning of any {{Code|.rb}} script to make use of the phidgets-ffi gem&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  require &#039;rubygems&#039;&lt;br /&gt;
  require &#039;phidgets-ffi&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The project now has access to the Phidget21 function calls and you are ready to begin coding.&lt;br /&gt;
&lt;br /&gt;
The same [[#Follow the Examples|teaching ]] section which describes the examples also has further resources for programming your Phidget.&lt;br /&gt;
&lt;br /&gt;
==Follow the Examples==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
Next, comes our API information.  These resources outline the Ruby Phidget functions:&lt;br /&gt;
&lt;br /&gt;
{{UsingAPhidgetInCodeGeneral|both of which are available in Ruby|[http://www.phidgets.com/documentation/rubydoc Ruby API]}}&lt;br /&gt;
&lt;br /&gt;
===Example Flow===&lt;br /&gt;
&lt;br /&gt;
{{ExamplePseudocode|In Ruby, you can name these &#039;&#039;&#039;event&#039;&#039;&#039; 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. &amp;lt;br&amp;gt; &lt;br /&gt;
In the example code, the event functions common to all Phidgets are called things like &#039;&#039;&#039;AttachHandler()&#039;&#039;&#039; and &#039;&#039;&#039;DetachHandler()&#039;&#039;&#039;, etc.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
Other functions are given in the examples to show you more detail on using your Phidget.  For example, &#039;&#039;&#039;DeviceInitialize()&#039;&#039;&#039; will show what needs to be set up for your Phidget before using it.&lt;br /&gt;
|Creating a Phidget software object in Ruby is specific to the Phidget.  For a Phidget Spatial, for example, this would involve creating a {{Code|Spatial}} object.  The examples show how to do this and other API functions.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The object provides device specific methods and properties which are available from the API for your specific Phidget.|&lt;br /&gt;
[http://www.phidgets.com/documentation/rubydoc Ruby API]}}&lt;br /&gt;
&lt;br /&gt;
===Code Snippets===&lt;br /&gt;
&lt;br /&gt;
Specific calls in Python will differ in syntax from those on the [[General Phidget Programming]] page, but the concepts stay the same.  &lt;br /&gt;
&lt;br /&gt;
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 Python 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.&lt;br /&gt;
&lt;br /&gt;
For example, if we were using a [[Product - 1018 - PhidgetInterfaceKit 8/8/8 | Phidget Interface Kit]] as our device, the general calls would look like this:&lt;br /&gt;
&lt;br /&gt;
====Step One: Initialize and Open====&lt;br /&gt;
&lt;br /&gt;
The Phidget constructor method will need to be called to create the Phidget object. There are two methods or programming a Phidget in Ruby: with and without a block. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  # Without a block&lt;br /&gt;
    device = Phidgets::InterfaceKit.new&lt;br /&gt;
&lt;br /&gt;
  # With a block&lt;br /&gt;
    Phidgets::InterfaceKit.new do |device|&lt;br /&gt;
      ..&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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. In the above example, InterfaceKit represents the PhidgetInterfaceKit. For simplicity in explaining the programming concepts, the remainder of the code snippet section will use the InterfaceKit object.&lt;br /&gt;
&lt;br /&gt;
For the most part, the two methods behave the same, but there are some subtle differences, which will be explained in the next section.&lt;br /&gt;
&lt;br /&gt;
====Step Two: Wait for Attachment (plugging in) of the Phidget==== &lt;br /&gt;
&lt;br /&gt;
The program needs to try and connect to the Phidget. Options can be added to the InterfaceKit constructor to connect to the first Phidget it finds, based on its serial number, label, or even connect across the network. Rubydocs API Documentation lists all of the available modes that the constructor provides. &lt;br /&gt;
&lt;br /&gt;
For example, the following will try to connect to the first Phidget it finds:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device= Phidgets::InterfaceKit.new&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following will try to connect to a Phidget over the Phidget WebService with a serial number of {{Code|99999}}, and a server id of {{Code|myserver}}: &lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  options = {:serial_number =&amp;gt; 99999, :server_id =&amp;gt; ‘myserver’, :password =&amp;gt; nil}&lt;br /&gt;
  device= Phidgets::InterfaceKit.new(options)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One important thing to remember is that when working with Phidgets, a local connection will reserve the device until closed. This prevents any other instances from retrieving data from the Phidget, including other programs. The one connection per device limit does not apply when exclusively using the Phidget WebService.&lt;br /&gt;
&lt;br /&gt;
{{Code|Phidgets::InterfaceKit.new}} will tell the program to continuously try to connect to a Phidget, based on the options given(if any), even trying to reconnect if it gets disconnected. &lt;br /&gt;
&lt;br /&gt;
=====Non Block Programming=====&lt;br /&gt;
For the non block programming method, simply calling the constructor does not guarantee you can use the Phidget immediately. We can handle this by using event driven programming and  tracking the {{Code|on_attach}} and {{Code|on_detach}} events, or by calling: {{Code|wait_for_attachment}}. {{Code|wait_for_attachment}} will block indefinitely until a connection is made to the Phidget, or an optional timeout is exceeded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device= Phidgets::InterfaceKit.new&lt;br /&gt;
  device.wait_for_attachment 2000 #halt the program for up 2000 milliseconds or until the Phidget is connected&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Please also remember to call close at the end or the program to free any locks on the Phidget&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device.close&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=====Block Programming=====&lt;br /&gt;
If you are programming inside a block, {{Code|wait_for_attachment}} is automatically called. By default, it will halt the program and try to connect to the Phidget for up to 1000 milliseconds.  Afterwards, the block will yield. Finally, close is not needed as it is automatically called once the block has yield.&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  options = {:timeout =&amp;gt; 2000)&lt;br /&gt;
  Phidgets::InterfaceKit.new(options) do |device|&lt;br /&gt;
    ... &lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Sometimes, it makes more sense to handle the attachment via an event.  This would be in instances where the Phidget is being plugged and unplugged, and you want to handle these incidents.  Or, when you want to use event-driven programming because you have a GUI-driven program.  In these cases, an event-driven code snippet to handle the attachment might look something like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device.on_attach  do |device, obj|&lt;br /&gt;
    puts &amp;quot;Id: #{device.id}&amp;quot;&lt;br /&gt;
    puts &amp;quot;Serial number: #{device.serial_number}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Step Three: Do Things with the Phidget====&lt;br /&gt;
&lt;br /&gt;
You can read data and interact with your Phidget both by polling it for its current state (or to set a state), or by catching events that trigger when the data changes.&lt;br /&gt;
&lt;br /&gt;
For our [[Product - 1018 - PhidgetInterfaceKit 8/8/8 | Phidget Interface Kit]], the polling method of getting the current sensor state and setting an output state looks something like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
&lt;br /&gt;
# Get a data point from Analog Port 0&lt;br /&gt;
puts &amp;quot;Sensor value[0]: #{device.sensors[0].to_i}&amp;quot; &lt;br /&gt;
&lt;br /&gt;
# Set digital output port 0 to be on&lt;br /&gt;
device.outputs[0].state = true&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To catch data changes via events, you would use something like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device.on_sensor_change do |device, input, value, obj|&lt;br /&gt;
    puts &amp;quot;Sensor #{input.index}&#039;s value has changed to #{value}&amp;quot;&lt;br /&gt;
  end &lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=====Step Four: Close=====&lt;br /&gt;
 &lt;br /&gt;
If you are using the block programming method, you do not need to worry about closing the Phidget. It is already taken care of when the block ends. However, if you are using the non block programming method, then you will need take special consideration that the Phidget is closed. &lt;br /&gt;
&lt;br /&gt;
At the end of your program, don’t forget to call {{Code|close}} to free any locks on the Phidget that the Phidget constructor call put in place!&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device.close&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;complete&#039;&#039; set of functions you have available for all Phidgets can be found in the [http://www.phidgets.com/documentation/PythonDoc.zip Python API]. You can also find more description on any device-specific function either in the [[Device List|Device API]] page for calls available only on your specific Phidget.&lt;br /&gt;
&lt;br /&gt;
=====Raw FFI=====&lt;br /&gt;
As an alternative to programming with the method as outlined in this document, you can also program making straight C calls through FFI. Please refer to the files in {{Code|Ruby Gems Directory/phidgets-ffi-x.x.x/lib/phidgets-ffi/ffi}} to see a list of available methods, and the [[http://rubydoc.info/gems/phidgets-ffi/0.1.1/frames Ruby API]] for usage. There are raw ffi examples in {{Code|Ruby Gems directory/phidgets-ffi-x.x.x/examples/raw-ffi}}.&lt;br /&gt;
&lt;br /&gt;
==Common Problems and Solutions/Workarounds==&lt;br /&gt;
&lt;br /&gt;
Here you can put various frequent problems and our recommended solutions.&lt;/div&gt;</summary>
		<author><name>Mphi</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs21/index.php?title=Language_-_Ruby&amp;diff=19112</id>
		<title>Language - Ruby</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs21/index.php?title=Language_-_Ruby&amp;diff=19112"/>
		<updated>2012-04-19T21:26:54Z</updated>

		<summary type="html">&lt;p&gt;Mphi: /* =Common Problems and Solutions/Workarounds */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Language]]&lt;br /&gt;
[[File:Icon-Ruby.png|64x64px||link=|alt=]]Ruby is an interpreted and object oriented scripting language with simple syntax created by Yukihiro Matsumoto.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
{{LanguageSupport|Ruby|the complete Phidget API, including events|all Phidget devices.|Linux and OS X|}}&lt;br /&gt;
&lt;br /&gt;
{{QuickDownloads|Ruby|&lt;br /&gt;
{{APIQuickDownloads|http://www.phidgets.com/documentation/rubydoc RubyDoc}}|&lt;br /&gt;
{{ExampleQuickDownloads|http://rubygems.org/gems/phidgets-ffi| (@RubyGems)}}|&lt;br /&gt;
{{ExtraLibraryQuickDownloads|http://rubygems.org/gems/phidgets-ffi|Ruby| (@RubyGems)}}&lt;br /&gt;
{{MacQuickDownloads}}&lt;br /&gt;
{{LinuxQuickDownloads}}&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
==Getting started with Ruby==&lt;br /&gt;
&lt;br /&gt;
If you are new to writing code for Phidgets, we recommend starting by running, then modifying existing examples. This will allow you to:&lt;br /&gt;
{{ExampleCodeReasons}}&lt;br /&gt;
&lt;br /&gt;
Instructions for OS X and Linux are similar, so they will be combined into the same section.&lt;br /&gt;
&lt;br /&gt;
The first step in using Ruby on Mac or Linux is to install the Phidget libraries.  Compile and install them as explained on the [[Device List|getting started guide for your device]].&lt;br /&gt;
&lt;br /&gt;
The next step is to install the phidgets-ffi gem. This is the library that allows you to program Phidgets with Ruby. For more information, please see phidgets-ffi at [http://rubygems.org/gems/phidgets-ffi RubyGems] and [https://github.com/kreynolds/phidgets-ffi GitHub].&lt;br /&gt;
&lt;br /&gt;
==Description of Library Files==&lt;br /&gt;
Ruby programs on OS X and Linux depend on the following files, which the installers put into your system. &lt;br /&gt;
&lt;br /&gt;
If you are using OS X, you will need the:&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|Phidget21.framework}}&amp;lt;/b&amp;gt; contains the actual Phidgets library for OS X, which is used at run-time.  &lt;br /&gt;
&lt;br /&gt;
If you are using Linux, you will need the:&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|libphidget21.so}}&amp;lt;/b&amp;gt; contains the actual Phidgets library for Linux, which is used at run-time.&lt;br /&gt;
&lt;br /&gt;
For both OS X and Linux, you will need the phidgets-ffi gem: &lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|phidgets-ffi}}&amp;lt;/b&amp;gt; is the Phidgets gem for Ruby. This gem contains the library as well as example code.&lt;br /&gt;
&lt;br /&gt;
==Installing phidgets-ffi==&lt;br /&gt;
&lt;br /&gt;
The phidgets-ffi gem relies on the [http://rubygems.org/gems/ffi ffi gem]. So, if you do not already have it installed, please do so. A version of ffi 1.0.9 or greater is needed. Type the following to install ffi:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  gem install ffi&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Next, please install the phidgets-ffi gem by typing the following into command line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  gem install phidgets-ffi&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For OS X systems, the gem will be installed into {{Code|/Library/Ruby/Gems/1.x/phidgets-ffi-x.x.x}}.&lt;br /&gt;
&lt;br /&gt;
For typical Linux systems, the gem will be installed into {{Code|var/lib/gems/1.9/gems/phidgets-ffi-x.x.x}}.&lt;br /&gt;
&lt;br /&gt;
===Use Our Examples===&lt;br /&gt;
&lt;br /&gt;
Open a command line terminal and navigate to the phidgets-ffi gem directory. {{Code|cd}} into the {{Code|examples}} folder. Here, you will find all of the examples available for Ruby.  If you aren&#039;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]].&lt;br /&gt;
&lt;br /&gt;
The easiest way to confirm that your environment is set up properly will be to compile and run the {{Code|HelloWorld}} Ruby example.&lt;br /&gt;
&lt;br /&gt;
The only thing left to do is to run the examples! Type the following into command line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  ruby HelloWorld.rb&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This program will detect for devices that are attached/detached on the computer. Go ahead, and attach or detach your devices! Here is an example output:&lt;br /&gt;
&lt;br /&gt;
[[File:Ruby Linux HelloWorld Output.PNG|link=|alt=HelloWorld Output]]&lt;br /&gt;
&lt;br /&gt;
After confirming that the {{Code|HelloWorld}} example is working, you can proceed to run the example for your device. If you aren&#039;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 ensure that you have set your start up project to be the one that matches your device before compiling. &lt;br /&gt;
&lt;br /&gt;
Once you have the Ruby examples running, we have a [[#Follow the Examples|teaching]] section below to help you follow them.&lt;br /&gt;
&lt;br /&gt;
==Write Your Own Code==&lt;br /&gt;
&lt;br /&gt;
When you are building a project from scratch, or adding Phidget function calls to an existing project, you&#039;ll need to configure your environment to properly link the Phidget Ruby libraries.&lt;br /&gt;
&lt;br /&gt;
Simply, add the following two lines to the beginning of any {{Code|.rb}} script to make use of the phidgets-ffi gem&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  require &#039;rubygems&#039;&lt;br /&gt;
  require &#039;phidgets-ffi&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The project now has access to the Phidget21 function calls and you are ready to begin coding.&lt;br /&gt;
&lt;br /&gt;
The same [[#Follow the Examples|teaching ]] section which describes the examples also has further resources for programming your Phidget.&lt;br /&gt;
&lt;br /&gt;
==Follow the Examples==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
Next, comes our API information.  These resources outline the Ruby Phidget functions:&lt;br /&gt;
&lt;br /&gt;
{{UsingAPhidgetInCodeGeneral|both of which are available in Ruby|[http://www.phidgets.com/documentation/rubydoc Ruby API]}}&lt;br /&gt;
&lt;br /&gt;
===Example Flow===&lt;br /&gt;
&lt;br /&gt;
{{ExamplePseudocode|In Ruby, you can name these &#039;&#039;&#039;event&#039;&#039;&#039; 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. &amp;lt;br&amp;gt; &lt;br /&gt;
In the example code, the event functions common to all Phidgets are called things like &#039;&#039;&#039;AttachHandler()&#039;&#039;&#039; and &#039;&#039;&#039;DetachHandler()&#039;&#039;&#039;, etc.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
Other functions are given in the examples to show you more detail on using your Phidget.  For example, &#039;&#039;&#039;DeviceInitialize()&#039;&#039;&#039; will show what needs to be set up for your Phidget before using it.&lt;br /&gt;
|Creating a Phidget software object in Ruby is specific to the Phidget.  For a Phidget Spatial, for example, this would involve creating a {{Code|Spatial}} object.  The examples show how to do this and other API functions.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The object provides device specific methods and properties which are available from the API for your specific Phidget.|&lt;br /&gt;
[http://www.phidgets.com/documentation/rubydoc Ruby API]}}&lt;br /&gt;
&lt;br /&gt;
===Code Snippets===&lt;br /&gt;
&lt;br /&gt;
Specific calls in Python will differ in syntax from those on the [[General Phidget Programming]] page, but the concepts stay the same.  &lt;br /&gt;
&lt;br /&gt;
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 Python 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.&lt;br /&gt;
&lt;br /&gt;
For example, if we were using a [[Product - 1018 - PhidgetInterfaceKit 8/8/8 | Phidget Interface Kit]] as our device, the general calls would look like this:&lt;br /&gt;
&lt;br /&gt;
====Step One: Initialize and Open====&lt;br /&gt;
&lt;br /&gt;
The Phidget constructor method will need to be called to create the Phidget object. There are two methods or programming a Phidget in Ruby: with and without a block. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  # Without a block&lt;br /&gt;
    device = Phidgets::InterfaceKit.new&lt;br /&gt;
&lt;br /&gt;
  # With a block&lt;br /&gt;
    Phidgets::InterfaceKit.new do |device|&lt;br /&gt;
      ..&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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. In the above example, InterfaceKit represents the PhidgetInterfaceKit. For simplicity in explaining the programming concepts, the remainder of the code snippet section will use the InterfaceKit object.&lt;br /&gt;
&lt;br /&gt;
For the most part, the two methods behave the same, but there are some subtle differences, which will be explained in the next section.&lt;br /&gt;
&lt;br /&gt;
====Step Two: Wait for Attachment (plugging in) of the Phidget==== &lt;br /&gt;
&lt;br /&gt;
The program needs to try and connect to the Phidget. Options can be added to the InterfaceKit constructor to connect to the first Phidget it finds, based on its serial number, label, or even connect across the network. Rubydocs API Documentation lists all of the available modes that the constructor provides. &lt;br /&gt;
&lt;br /&gt;
For example, the following will try to connect to the first Phidget it finds:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device= Phidgets::InterfaceKit.new&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following will try to connect to a Phidget over the Phidget WebService with a serial number of {{Code|99999}}, and a server id of {{Code|myserver}}: &lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  options = {:serial_number =&amp;gt; 99999, :server_id =&amp;gt; ‘myserver’, :password =&amp;gt; nil}&lt;br /&gt;
  device= Phidgets::InterfaceKit.new(options)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One important thing to remember is that when working with Phidgets, a local connection will reserve the device until closed. This prevents any other instances from retrieving data from the Phidget, including other programs. The one connection per device limit does not apply when exclusively using the Phidget WebService.&lt;br /&gt;
&lt;br /&gt;
{{Code|Phidgets::InterfaceKit.new}} will tell the program to continuously try to connect to a Phidget, based on the options given(if any), even trying to reconnect if it gets disconnected. &lt;br /&gt;
&lt;br /&gt;
=====Non Block Programming=====&lt;br /&gt;
For the non block programming method, simply calling the constructor does not guarantee you can use the Phidget immediately. We can handle this by using event driven programming and  tracking the {{Code|on_attach}} and {{Code|on_detach}} events, or by calling: {{Code|wait_for_attachment}}. {{Code|wait_for_attachment}} will block indefinitely until a connection is made to the Phidget, or an optional timeout is exceeded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device= Phidgets::InterfaceKit.new&lt;br /&gt;
  device.wait_for_attachment 2000 #halt the program for up 2000 milliseconds or until the Phidget is connected&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Please also remember to call close at the end or the program to free any locks on the Phidget&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device.close&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=====Block Programming=====&lt;br /&gt;
If you are programming inside a block, {{Code|wait_for_attachment}} is automatically called. By default, it will halt the program and try to connect to the Phidget for up to 1000 milliseconds.  Afterwards, the block will yield. Finally, close is not needed as it is automatically called once the block has yield.&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  options = {:timeout =&amp;gt; 2000)&lt;br /&gt;
  Phidgets::InterfaceKit.new(options) do |device|&lt;br /&gt;
    ... &lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Sometimes, it makes more sense to handle the attachment via an event.  This would be in instances where the Phidget is being plugged and unplugged, and you want to handle these incidents.  Or, when you want to use event-driven programming because you have a GUI-driven program.  In these cases, an event-driven code snippet to handle the attachment might look something like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device.on_attach  do |device, obj|&lt;br /&gt;
    puts &amp;quot;Id: #{device.id}&amp;quot;&lt;br /&gt;
    puts &amp;quot;Serial number: #{device.serial_number}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Step Three: Do Things with the Phidget====&lt;br /&gt;
&lt;br /&gt;
You can read data and interact with your Phidget both by polling it for its current state (or to set a state), or by catching events that trigger when the data changes.&lt;br /&gt;
&lt;br /&gt;
For our [[Product - 1018 - PhidgetInterfaceKit 8/8/8 | Phidget Interface Kit]], the polling method of getting the current sensor state and setting an output state looks something like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
&lt;br /&gt;
# Get a data point from Analog Port 0&lt;br /&gt;
puts &amp;quot;Sensor value[0]: #{device.sensors[0].to_i}&amp;quot; &lt;br /&gt;
&lt;br /&gt;
# Set digital output port 0 to be on&lt;br /&gt;
device.outputs[0].state = true&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To catch data changes via events, you would use something like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device.on_sensor_change do |device, input, value, obj|&lt;br /&gt;
    puts &amp;quot;Sensor #{input.index}&#039;s value has changed to #{value}&amp;quot;&lt;br /&gt;
  end &lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=====Step Four: Close=====&lt;br /&gt;
 &lt;br /&gt;
If you are using the block programming method, you do not need to worry about closing the Phidget. It is already taken care of when the block ends. However, if you are using the non block programming method, then you will need take special consideration that the Phidget is closed. &lt;br /&gt;
&lt;br /&gt;
At the end of your program, don’t forget to call {{Code|close}} to free any locks on the Phidget that the Phidget constructor call put in place!&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device.close&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;complete&#039;&#039; set of functions you have available for all Phidgets can be found in the [http://www.phidgets.com/documentation/PythonDoc.zip Python API]. You can also find more description on any device-specific function either in the [[Device List|Device API]] page for calls available only on your specific Phidget.&lt;br /&gt;
&lt;br /&gt;
=====Raw FFI=====&lt;br /&gt;
As an alternative to programming with the method as outlined in this document, you can also program making straight C calls through FFI. Please refer to the files in {{Code|&amp;lt;Ruby Gems Directory&amp;gt;/phidgets-ffi-x.x.x/lib/phidgets-ffi/ffi}} to see a list of available methods, and the [http://rubydoc.info/gems/phidgets-ffi/0.1.1/frames Ruby API] for usage. There are raw ffi examples in {{Code|&amp;lt;Ruby Gems directory&amp;gt;/phidgets-ffi-x.x.x/examples/raw-ffi}}.&lt;br /&gt;
&lt;br /&gt;
==Common Problems and Solutions/Workarounds==&lt;br /&gt;
&lt;br /&gt;
Here you can put various frequent problems and our recommended solutions.&lt;/div&gt;</summary>
		<author><name>Mphi</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs21/index.php?title=Language_-_Ruby&amp;diff=19111</id>
		<title>Language - Ruby</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs21/index.php?title=Language_-_Ruby&amp;diff=19111"/>
		<updated>2012-04-19T21:26:31Z</updated>

		<summary type="html">&lt;p&gt;Mphi: /* Common Problems and Solutions/Workarounds */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Language]]&lt;br /&gt;
[[File:Icon-Ruby.png|64x64px||link=|alt=]]Ruby is an interpreted and object oriented scripting language with simple syntax created by Yukihiro Matsumoto.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
{{LanguageSupport|Ruby|the complete Phidget API, including events|all Phidget devices.|Linux and OS X|}}&lt;br /&gt;
&lt;br /&gt;
{{QuickDownloads|Ruby|&lt;br /&gt;
{{APIQuickDownloads|http://www.phidgets.com/documentation/rubydoc RubyDoc}}|&lt;br /&gt;
{{ExampleQuickDownloads|http://rubygems.org/gems/phidgets-ffi| (@RubyGems)}}|&lt;br /&gt;
{{ExtraLibraryQuickDownloads|http://rubygems.org/gems/phidgets-ffi|Ruby| (@RubyGems)}}&lt;br /&gt;
{{MacQuickDownloads}}&lt;br /&gt;
{{LinuxQuickDownloads}}&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
==Getting started with Ruby==&lt;br /&gt;
&lt;br /&gt;
If you are new to writing code for Phidgets, we recommend starting by running, then modifying existing examples. This will allow you to:&lt;br /&gt;
{{ExampleCodeReasons}}&lt;br /&gt;
&lt;br /&gt;
Instructions for OS X and Linux are similar, so they will be combined into the same section.&lt;br /&gt;
&lt;br /&gt;
The first step in using Ruby on Mac or Linux is to install the Phidget libraries.  Compile and install them as explained on the [[Device List|getting started guide for your device]].&lt;br /&gt;
&lt;br /&gt;
The next step is to install the phidgets-ffi gem. This is the library that allows you to program Phidgets with Ruby. For more information, please see phidgets-ffi at [http://rubygems.org/gems/phidgets-ffi RubyGems] and [https://github.com/kreynolds/phidgets-ffi GitHub].&lt;br /&gt;
&lt;br /&gt;
==Description of Library Files==&lt;br /&gt;
Ruby programs on OS X and Linux depend on the following files, which the installers put into your system. &lt;br /&gt;
&lt;br /&gt;
If you are using OS X, you will need the:&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|Phidget21.framework}}&amp;lt;/b&amp;gt; contains the actual Phidgets library for OS X, which is used at run-time.  &lt;br /&gt;
&lt;br /&gt;
If you are using Linux, you will need the:&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|libphidget21.so}}&amp;lt;/b&amp;gt; contains the actual Phidgets library for Linux, which is used at run-time.&lt;br /&gt;
&lt;br /&gt;
For both OS X and Linux, you will need the phidgets-ffi gem: &lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|phidgets-ffi}}&amp;lt;/b&amp;gt; is the Phidgets gem for Ruby. This gem contains the library as well as example code.&lt;br /&gt;
&lt;br /&gt;
==Installing phidgets-ffi==&lt;br /&gt;
&lt;br /&gt;
The phidgets-ffi gem relies on the [http://rubygems.org/gems/ffi ffi gem]. So, if you do not already have it installed, please do so. A version of ffi 1.0.9 or greater is needed. Type the following to install ffi:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  gem install ffi&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Next, please install the phidgets-ffi gem by typing the following into command line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  gem install phidgets-ffi&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For OS X systems, the gem will be installed into {{Code|/Library/Ruby/Gems/1.x/phidgets-ffi-x.x.x}}.&lt;br /&gt;
&lt;br /&gt;
For typical Linux systems, the gem will be installed into {{Code|var/lib/gems/1.9/gems/phidgets-ffi-x.x.x}}.&lt;br /&gt;
&lt;br /&gt;
===Use Our Examples===&lt;br /&gt;
&lt;br /&gt;
Open a command line terminal and navigate to the phidgets-ffi gem directory. {{Code|cd}} into the {{Code|examples}} folder. Here, you will find all of the examples available for Ruby.  If you aren&#039;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]].&lt;br /&gt;
&lt;br /&gt;
The easiest way to confirm that your environment is set up properly will be to compile and run the {{Code|HelloWorld}} Ruby example.&lt;br /&gt;
&lt;br /&gt;
The only thing left to do is to run the examples! Type the following into command line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  ruby HelloWorld.rb&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This program will detect for devices that are attached/detached on the computer. Go ahead, and attach or detach your devices! Here is an example output:&lt;br /&gt;
&lt;br /&gt;
[[File:Ruby Linux HelloWorld Output.PNG|link=|alt=HelloWorld Output]]&lt;br /&gt;
&lt;br /&gt;
After confirming that the {{Code|HelloWorld}} example is working, you can proceed to run the example for your device. If you aren&#039;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 ensure that you have set your start up project to be the one that matches your device before compiling. &lt;br /&gt;
&lt;br /&gt;
Once you have the Ruby examples running, we have a [[#Follow the Examples|teaching]] section below to help you follow them.&lt;br /&gt;
&lt;br /&gt;
==Write Your Own Code==&lt;br /&gt;
&lt;br /&gt;
When you are building a project from scratch, or adding Phidget function calls to an existing project, you&#039;ll need to configure your environment to properly link the Phidget Ruby libraries.&lt;br /&gt;
&lt;br /&gt;
Simply, add the following two lines to the beginning of any {{Code|.rb}} script to make use of the phidgets-ffi gem&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  require &#039;rubygems&#039;&lt;br /&gt;
  require &#039;phidgets-ffi&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The project now has access to the Phidget21 function calls and you are ready to begin coding.&lt;br /&gt;
&lt;br /&gt;
The same [[#Follow the Examples|teaching ]] section which describes the examples also has further resources for programming your Phidget.&lt;br /&gt;
&lt;br /&gt;
==Follow the Examples==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
Next, comes our API information.  These resources outline the Ruby Phidget functions:&lt;br /&gt;
&lt;br /&gt;
{{UsingAPhidgetInCodeGeneral|both of which are available in Ruby|[http://www.phidgets.com/documentation/rubydoc Ruby API]}}&lt;br /&gt;
&lt;br /&gt;
===Example Flow===&lt;br /&gt;
&lt;br /&gt;
{{ExamplePseudocode|In Ruby, you can name these &#039;&#039;&#039;event&#039;&#039;&#039; 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. &amp;lt;br&amp;gt; &lt;br /&gt;
In the example code, the event functions common to all Phidgets are called things like &#039;&#039;&#039;AttachHandler()&#039;&#039;&#039; and &#039;&#039;&#039;DetachHandler()&#039;&#039;&#039;, etc.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
Other functions are given in the examples to show you more detail on using your Phidget.  For example, &#039;&#039;&#039;DeviceInitialize()&#039;&#039;&#039; will show what needs to be set up for your Phidget before using it.&lt;br /&gt;
|Creating a Phidget software object in Ruby is specific to the Phidget.  For a Phidget Spatial, for example, this would involve creating a {{Code|Spatial}} object.  The examples show how to do this and other API functions.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The object provides device specific methods and properties which are available from the API for your specific Phidget.|&lt;br /&gt;
[http://www.phidgets.com/documentation/rubydoc Ruby API]}}&lt;br /&gt;
&lt;br /&gt;
===Code Snippets===&lt;br /&gt;
&lt;br /&gt;
Specific calls in Python will differ in syntax from those on the [[General Phidget Programming]] page, but the concepts stay the same.  &lt;br /&gt;
&lt;br /&gt;
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 Python 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.&lt;br /&gt;
&lt;br /&gt;
For example, if we were using a [[Product - 1018 - PhidgetInterfaceKit 8/8/8 | Phidget Interface Kit]] as our device, the general calls would look like this:&lt;br /&gt;
&lt;br /&gt;
====Step One: Initialize and Open====&lt;br /&gt;
&lt;br /&gt;
The Phidget constructor method will need to be called to create the Phidget object. There are two methods or programming a Phidget in Ruby: with and without a block. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  # Without a block&lt;br /&gt;
    device = Phidgets::InterfaceKit.new&lt;br /&gt;
&lt;br /&gt;
  # With a block&lt;br /&gt;
    Phidgets::InterfaceKit.new do |device|&lt;br /&gt;
      ..&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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. In the above example, InterfaceKit represents the PhidgetInterfaceKit. For simplicity in explaining the programming concepts, the remainder of the code snippet section will use the InterfaceKit object.&lt;br /&gt;
&lt;br /&gt;
For the most part, the two methods behave the same, but there are some subtle differences, which will be explained in the next section.&lt;br /&gt;
&lt;br /&gt;
====Step Two: Wait for Attachment (plugging in) of the Phidget==== &lt;br /&gt;
&lt;br /&gt;
The program needs to try and connect to the Phidget. Options can be added to the InterfaceKit constructor to connect to the first Phidget it finds, based on its serial number, label, or even connect across the network. Rubydocs API Documentation lists all of the available modes that the constructor provides. &lt;br /&gt;
&lt;br /&gt;
For example, the following will try to connect to the first Phidget it finds:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device= Phidgets::InterfaceKit.new&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following will try to connect to a Phidget over the Phidget WebService with a serial number of {{Code|99999}}, and a server id of {{Code|myserver}}: &lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  options = {:serial_number =&amp;gt; 99999, :server_id =&amp;gt; ‘myserver’, :password =&amp;gt; nil}&lt;br /&gt;
  device= Phidgets::InterfaceKit.new(options)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One important thing to remember is that when working with Phidgets, a local connection will reserve the device until closed. This prevents any other instances from retrieving data from the Phidget, including other programs. The one connection per device limit does not apply when exclusively using the Phidget WebService.&lt;br /&gt;
&lt;br /&gt;
{{Code|Phidgets::InterfaceKit.new}} will tell the program to continuously try to connect to a Phidget, based on the options given(if any), even trying to reconnect if it gets disconnected. &lt;br /&gt;
&lt;br /&gt;
=====Non Block Programming=====&lt;br /&gt;
For the non block programming method, simply calling the constructor does not guarantee you can use the Phidget immediately. We can handle this by using event driven programming and  tracking the {{Code|on_attach}} and {{Code|on_detach}} events, or by calling: {{Code|wait_for_attachment}}. {{Code|wait_for_attachment}} will block indefinitely until a connection is made to the Phidget, or an optional timeout is exceeded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device= Phidgets::InterfaceKit.new&lt;br /&gt;
  device.wait_for_attachment 2000 #halt the program for up 2000 milliseconds or until the Phidget is connected&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Please also remember to call close at the end or the program to free any locks on the Phidget&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device.close&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=====Block Programming=====&lt;br /&gt;
If you are programming inside a block, {{Code|wait_for_attachment}} is automatically called. By default, it will halt the program and try to connect to the Phidget for up to 1000 milliseconds.  Afterwards, the block will yield. Finally, close is not needed as it is automatically called once the block has yield.&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  options = {:timeout =&amp;gt; 2000)&lt;br /&gt;
  Phidgets::InterfaceKit.new(options) do |device|&lt;br /&gt;
    ... &lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Sometimes, it makes more sense to handle the attachment via an event.  This would be in instances where the Phidget is being plugged and unplugged, and you want to handle these incidents.  Or, when you want to use event-driven programming because you have a GUI-driven program.  In these cases, an event-driven code snippet to handle the attachment might look something like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device.on_attach  do |device, obj|&lt;br /&gt;
    puts &amp;quot;Id: #{device.id}&amp;quot;&lt;br /&gt;
    puts &amp;quot;Serial number: #{device.serial_number}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Step Three: Do Things with the Phidget====&lt;br /&gt;
&lt;br /&gt;
You can read data and interact with your Phidget both by polling it for its current state (or to set a state), or by catching events that trigger when the data changes.&lt;br /&gt;
&lt;br /&gt;
For our [[Product - 1018 - PhidgetInterfaceKit 8/8/8 | Phidget Interface Kit]], the polling method of getting the current sensor state and setting an output state looks something like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
&lt;br /&gt;
# Get a data point from Analog Port 0&lt;br /&gt;
puts &amp;quot;Sensor value[0]: #{device.sensors[0].to_i}&amp;quot; &lt;br /&gt;
&lt;br /&gt;
# Set digital output port 0 to be on&lt;br /&gt;
device.outputs[0].state = true&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To catch data changes via events, you would use something like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device.on_sensor_change do |device, input, value, obj|&lt;br /&gt;
    puts &amp;quot;Sensor #{input.index}&#039;s value has changed to #{value}&amp;quot;&lt;br /&gt;
  end &lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=====Step Four: Close=====&lt;br /&gt;
 &lt;br /&gt;
If you are using the block programming method, you do not need to worry about closing the Phidget. It is already taken care of when the block ends. However, if you are using the non block programming method, then you will need take special consideration that the Phidget is closed. &lt;br /&gt;
&lt;br /&gt;
At the end of your program, don’t forget to call {{Code|close}} to free any locks on the Phidget that the Phidget constructor call put in place!&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device.close&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;complete&#039;&#039; set of functions you have available for all Phidgets can be found in the [http://www.phidgets.com/documentation/PythonDoc.zip Python API]. You can also find more description on any device-specific function either in the [[Device List|Device API]] page for calls available only on your specific Phidget.&lt;br /&gt;
&lt;br /&gt;
=====Raw FFI=====&lt;br /&gt;
As an alternative to programming with the method as outlined in this document, you can also program making straight C calls through FFI. Please refer to the files in {{Code|&amp;lt;Ruby Gems Directory&amp;gt;/phidgets-ffi-x.x.x/lib/phidgets-ffi/ffi}} to see a list of available methods, and the [http://rubydoc.info/gems/phidgets-ffi/0.1.1/frames Ruby API] for usage. There are raw ffi examples in {{Code|&amp;lt;Ruby Gems directory&amp;gt;/phidgets-ffi-x.x.x/examples/raw-ffi}}.&lt;br /&gt;
&lt;br /&gt;
======Common Problems and Solutions/Workarounds=====&lt;br /&gt;
&lt;br /&gt;
Here you can put various frequent problems and our recommended solutions.&lt;/div&gt;</summary>
		<author><name>Mphi</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs21/index.php?title=Language_-_Ruby&amp;diff=19110</id>
		<title>Language - Ruby</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs21/index.php?title=Language_-_Ruby&amp;diff=19110"/>
		<updated>2012-04-19T21:22:21Z</updated>

		<summary type="html">&lt;p&gt;Mphi: /* Block Programming */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Language]]&lt;br /&gt;
[[File:Icon-Ruby.png|64x64px||link=|alt=]]Ruby is an interpreted and object oriented scripting language with simple syntax created by Yukihiro Matsumoto.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
{{LanguageSupport|Ruby|the complete Phidget API, including events|all Phidget devices.|Linux and OS X|}}&lt;br /&gt;
&lt;br /&gt;
{{QuickDownloads|Ruby|&lt;br /&gt;
{{APIQuickDownloads|http://www.phidgets.com/documentation/rubydoc RubyDoc}}|&lt;br /&gt;
{{ExampleQuickDownloads|http://rubygems.org/gems/phidgets-ffi| (@RubyGems)}}|&lt;br /&gt;
{{ExtraLibraryQuickDownloads|http://rubygems.org/gems/phidgets-ffi|Ruby| (@RubyGems)}}&lt;br /&gt;
{{MacQuickDownloads}}&lt;br /&gt;
{{LinuxQuickDownloads}}&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
==Getting started with Ruby==&lt;br /&gt;
&lt;br /&gt;
If you are new to writing code for Phidgets, we recommend starting by running, then modifying existing examples. This will allow you to:&lt;br /&gt;
{{ExampleCodeReasons}}&lt;br /&gt;
&lt;br /&gt;
Instructions for OS X and Linux are similar, so they will be combined into the same section.&lt;br /&gt;
&lt;br /&gt;
The first step in using Ruby on Mac or Linux is to install the Phidget libraries.  Compile and install them as explained on the [[Device List|getting started guide for your device]].&lt;br /&gt;
&lt;br /&gt;
The next step is to install the phidgets-ffi gem. This is the library that allows you to program Phidgets with Ruby. For more information, please see phidgets-ffi at [http://rubygems.org/gems/phidgets-ffi RubyGems] and [https://github.com/kreynolds/phidgets-ffi GitHub].&lt;br /&gt;
&lt;br /&gt;
==Description of Library Files==&lt;br /&gt;
Ruby programs on OS X and Linux depend on the following files, which the installers put into your system. &lt;br /&gt;
&lt;br /&gt;
If you are using OS X, you will need the:&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|Phidget21.framework}}&amp;lt;/b&amp;gt; contains the actual Phidgets library for OS X, which is used at run-time.  &lt;br /&gt;
&lt;br /&gt;
If you are using Linux, you will need the:&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|libphidget21.so}}&amp;lt;/b&amp;gt; contains the actual Phidgets library for Linux, which is used at run-time.&lt;br /&gt;
&lt;br /&gt;
For both OS X and Linux, you will need the phidgets-ffi gem: &lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|phidgets-ffi}}&amp;lt;/b&amp;gt; is the Phidgets gem for Ruby. This gem contains the library as well as example code.&lt;br /&gt;
&lt;br /&gt;
==Installing phidgets-ffi==&lt;br /&gt;
&lt;br /&gt;
The phidgets-ffi gem relies on the [http://rubygems.org/gems/ffi ffi gem]. So, if you do not already have it installed, please do so. A version of ffi 1.0.9 or greater is needed. Type the following to install ffi:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  gem install ffi&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Next, please install the phidgets-ffi gem by typing the following into command line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  gem install phidgets-ffi&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For OS X systems, the gem will be installed into {{Code|/Library/Ruby/Gems/1.x/phidgets-ffi-x.x.x}}.&lt;br /&gt;
&lt;br /&gt;
For typical Linux systems, the gem will be installed into {{Code|var/lib/gems/1.9/gems/phidgets-ffi-x.x.x}}.&lt;br /&gt;
&lt;br /&gt;
===Use Our Examples===&lt;br /&gt;
&lt;br /&gt;
Open a command line terminal and navigate to the phidgets-ffi gem directory. {{Code|cd}} into the {{Code|examples}} folder. Here, you will find all of the examples available for Ruby.  If you aren&#039;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]].&lt;br /&gt;
&lt;br /&gt;
The easiest way to confirm that your environment is set up properly will be to compile and run the {{Code|HelloWorld}} Ruby example.&lt;br /&gt;
&lt;br /&gt;
The only thing left to do is to run the examples! Type the following into command line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  ruby HelloWorld.rb&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This program will detect for devices that are attached/detached on the computer. Go ahead, and attach or detach your devices! Here is an example output:&lt;br /&gt;
&lt;br /&gt;
[[File:Ruby Linux HelloWorld Output.PNG|link=|alt=HelloWorld Output]]&lt;br /&gt;
&lt;br /&gt;
After confirming that the {{Code|HelloWorld}} example is working, you can proceed to run the example for your device. If you aren&#039;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 ensure that you have set your start up project to be the one that matches your device before compiling. &lt;br /&gt;
&lt;br /&gt;
Once you have the Ruby examples running, we have a [[#Follow the Examples|teaching]] section below to help you follow them.&lt;br /&gt;
&lt;br /&gt;
==Write Your Own Code==&lt;br /&gt;
&lt;br /&gt;
When you are building a project from scratch, or adding Phidget function calls to an existing project, you&#039;ll need to configure your environment to properly link the Phidget Ruby libraries.&lt;br /&gt;
&lt;br /&gt;
Simply, add the following two lines to the beginning of any {{Code|.rb}} script to make use of the phidgets-ffi gem&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  require &#039;rubygems&#039;&lt;br /&gt;
  require &#039;phidgets-ffi&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The project now has access to the Phidget21 function calls and you are ready to begin coding.&lt;br /&gt;
&lt;br /&gt;
The same [[#Follow the Examples|teaching ]] section which describes the examples also has further resources for programming your Phidget.&lt;br /&gt;
&lt;br /&gt;
==Follow the Examples==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
Next, comes our API information.  These resources outline the Ruby Phidget functions:&lt;br /&gt;
&lt;br /&gt;
{{UsingAPhidgetInCodeGeneral|both of which are available in Ruby|[http://www.phidgets.com/documentation/rubydoc Ruby API]}}&lt;br /&gt;
&lt;br /&gt;
===Example Flow===&lt;br /&gt;
&lt;br /&gt;
{{ExamplePseudocode|In Ruby, you can name these &#039;&#039;&#039;event&#039;&#039;&#039; 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. &amp;lt;br&amp;gt; &lt;br /&gt;
In the example code, the event functions common to all Phidgets are called things like &#039;&#039;&#039;AttachHandler()&#039;&#039;&#039; and &#039;&#039;&#039;DetachHandler()&#039;&#039;&#039;, etc.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
Other functions are given in the examples to show you more detail on using your Phidget.  For example, &#039;&#039;&#039;DeviceInitialize()&#039;&#039;&#039; will show what needs to be set up for your Phidget before using it.&lt;br /&gt;
|Creating a Phidget software object in Ruby is specific to the Phidget.  For a Phidget Spatial, for example, this would involve creating a {{Code|Spatial}} object.  The examples show how to do this and other API functions.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The object provides device specific methods and properties which are available from the API for your specific Phidget.|&lt;br /&gt;
[http://www.phidgets.com/documentation/rubydoc Ruby API]}}&lt;br /&gt;
&lt;br /&gt;
===Code Snippets===&lt;br /&gt;
&lt;br /&gt;
Specific calls in Python will differ in syntax from those on the [[General Phidget Programming]] page, but the concepts stay the same.  &lt;br /&gt;
&lt;br /&gt;
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 Python 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.&lt;br /&gt;
&lt;br /&gt;
For example, if we were using a [[Product - 1018 - PhidgetInterfaceKit 8/8/8 | Phidget Interface Kit]] as our device, the general calls would look like this:&lt;br /&gt;
&lt;br /&gt;
====Step One: Initialize and Open====&lt;br /&gt;
&lt;br /&gt;
The Phidget constructor method will need to be called to create the Phidget object. There are two methods or programming a Phidget in Ruby: with and without a block. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  # Without a block&lt;br /&gt;
    device = Phidgets::InterfaceKit.new&lt;br /&gt;
&lt;br /&gt;
  # With a block&lt;br /&gt;
    Phidgets::InterfaceKit.new do |device|&lt;br /&gt;
      ..&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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. In the above example, InterfaceKit represents the PhidgetInterfaceKit. For simplicity in explaining the programming concepts, the remainder of the code snippet section will use the InterfaceKit object.&lt;br /&gt;
&lt;br /&gt;
For the most part, the two methods behave the same, but there are some subtle differences, which will be explained in the next section.&lt;br /&gt;
&lt;br /&gt;
====Step Two: Wait for Attachment (plugging in) of the Phidget==== &lt;br /&gt;
&lt;br /&gt;
The program needs to try and connect to the Phidget. Options can be added to the InterfaceKit constructor to connect to the first Phidget it finds, based on its serial number, label, or even connect across the network. Rubydocs API Documentation lists all of the available modes that the constructor provides. &lt;br /&gt;
&lt;br /&gt;
For example, the following will try to connect to the first Phidget it finds:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device= Phidgets::InterfaceKit.new&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following will try to connect to a Phidget over the Phidget WebService with a serial number of {{Code|99999}}, and a server id of {{Code|myserver}}: &lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  options = {:serial_number =&amp;gt; 99999, :server_id =&amp;gt; ‘myserver’, :password =&amp;gt; nil}&lt;br /&gt;
  device= Phidgets::InterfaceKit.new(options)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One important thing to remember is that when working with Phidgets, a local connection will reserve the device until closed. This prevents any other instances from retrieving data from the Phidget, including other programs. The one connection per device limit does not apply when exclusively using the Phidget WebService.&lt;br /&gt;
&lt;br /&gt;
{{Code|Phidgets::InterfaceKit.new}} will tell the program to continuously try to connect to a Phidget, based on the options given(if any), even trying to reconnect if it gets disconnected. &lt;br /&gt;
&lt;br /&gt;
=====Non Block Programming=====&lt;br /&gt;
For the non block programming method, simply calling the constructor does not guarantee you can use the Phidget immediately. We can handle this by using event driven programming and  tracking the {{Code|on_attach}} and {{Code|on_detach}} events, or by calling: {{Code|wait_for_attachment}}. {{Code|wait_for_attachment}} will block indefinitely until a connection is made to the Phidget, or an optional timeout is exceeded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device= Phidgets::InterfaceKit.new&lt;br /&gt;
  device.wait_for_attachment 2000 #halt the program for up 2000 milliseconds or until the Phidget is connected&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Please also remember to call close at the end or the program to free any locks on the Phidget&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device.close&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=====Block Programming=====&lt;br /&gt;
If you are programming inside a block, {{Code|wait_for_attachment}} is automatically called. By default, it will halt the program and try to connect to the Phidget for up to 1000 milliseconds.  Afterwards, the block will yield. Finally, close is not needed as it is automatically called once the block has yield.&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  options = {:timeout =&amp;gt; 2000)&lt;br /&gt;
  Phidgets::InterfaceKit.new(options) do |device|&lt;br /&gt;
    ... &lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Sometimes, it makes more sense to handle the attachment via an event.  This would be in instances where the Phidget is being plugged and unplugged, and you want to handle these incidents.  Or, when you want to use event-driven programming because you have a GUI-driven program.  In these cases, an event-driven code snippet to handle the attachment might look something like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device.on_attach  do |device, obj|&lt;br /&gt;
    puts &amp;quot;Id: #{device.id}&amp;quot;&lt;br /&gt;
    puts &amp;quot;Serial number: #{device.serial_number}&amp;quot;&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Step Three: Do Things with the Phidget====&lt;br /&gt;
&lt;br /&gt;
You can read data and interact with your Phidget both by polling it for its current state (or to set a state), or by catching events that trigger when the data changes.&lt;br /&gt;
&lt;br /&gt;
For our [[Product - 1018 - PhidgetInterfaceKit 8/8/8 | Phidget Interface Kit]], the polling method of getting the current sensor state and setting an output state looks something like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
&lt;br /&gt;
# Get a data point from Analog Port 0&lt;br /&gt;
puts &amp;quot;Sensor value[0]: #{device.sensors[0].to_i}&amp;quot; &lt;br /&gt;
&lt;br /&gt;
# Set digital output port 0 to be on&lt;br /&gt;
device.outputs[0].state = true&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To catch data changes via events, you would use something like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device.on_sensor_change do |device, input, value, obj|&lt;br /&gt;
    puts &amp;quot;Sensor #{input.index}&#039;s value has changed to #{value}&amp;quot;&lt;br /&gt;
  end &lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=====Step Four: Close=====&lt;br /&gt;
 &lt;br /&gt;
If you are using the block programming method, you do not need to worry about closing the Phidget. It is already taken care of when the block ends. However, if you are using the non block programming method, then you will need take special consideration that the Phidget is closed. &lt;br /&gt;
&lt;br /&gt;
At the end of your program, don’t forget to call {{Code|close}} to free any locks on the Phidget that the Phidget constructor call put in place!&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device.close&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;complete&#039;&#039; set of functions you have available for all Phidgets can be found in the [http://www.phidgets.com/documentation/PythonDoc.zip Python API]. You can also find more description on any device-specific function either in the [[Device List|Device API]] page for calls available only on your specific Phidget.&lt;br /&gt;
&lt;br /&gt;
=====Raw FFI=====&lt;br /&gt;
As an alternative to programming with the method as outlined in this document, you can also program making straight C calls through FFI. Please refer to the files in {{Code|&amp;lt;Ruby Gems Directory&amp;gt;/phidgets-ffi-x.x.x/lib/phidgets-ffi/ffi}} to see a list of available methods, and the [http://rubydoc.info/gems/phidgets-ffi/0.1.1/frames Ruby API] for usage. There are raw ffi examples in {{Code|&amp;lt;Ruby Gems directory&amp;gt;/phidgets-ffi-x.x.x/examples/raw-ffi}}.&lt;br /&gt;
&lt;br /&gt;
==Common Problems and Solutions/Workarounds==&lt;br /&gt;
&lt;br /&gt;
Here you can put various frequent problems and our recommended solutions.&lt;/div&gt;</summary>
		<author><name>Mphi</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs21/index.php?title=Language_-_Ruby&amp;diff=19109</id>
		<title>Language - Ruby</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs21/index.php?title=Language_-_Ruby&amp;diff=19109"/>
		<updated>2012-04-19T21:21:44Z</updated>

		<summary type="html">&lt;p&gt;Mphi: /* Step Four: Close= */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Language]]&lt;br /&gt;
[[File:Icon-Ruby.png|64x64px||link=|alt=]]Ruby is an interpreted and object oriented scripting language with simple syntax created by Yukihiro Matsumoto.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
{{LanguageSupport|Ruby|the complete Phidget API, including events|all Phidget devices.|Linux and OS X|}}&lt;br /&gt;
&lt;br /&gt;
{{QuickDownloads|Ruby|&lt;br /&gt;
{{APIQuickDownloads|http://www.phidgets.com/documentation/rubydoc RubyDoc}}|&lt;br /&gt;
{{ExampleQuickDownloads|http://rubygems.org/gems/phidgets-ffi| (@RubyGems)}}|&lt;br /&gt;
{{ExtraLibraryQuickDownloads|http://rubygems.org/gems/phidgets-ffi|Ruby| (@RubyGems)}}&lt;br /&gt;
{{MacQuickDownloads}}&lt;br /&gt;
{{LinuxQuickDownloads}}&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
==Getting started with Ruby==&lt;br /&gt;
&lt;br /&gt;
If you are new to writing code for Phidgets, we recommend starting by running, then modifying existing examples. This will allow you to:&lt;br /&gt;
{{ExampleCodeReasons}}&lt;br /&gt;
&lt;br /&gt;
Instructions for OS X and Linux are similar, so they will be combined into the same section.&lt;br /&gt;
&lt;br /&gt;
The first step in using Ruby on Mac or Linux is to install the Phidget libraries.  Compile and install them as explained on the [[Device List|getting started guide for your device]].&lt;br /&gt;
&lt;br /&gt;
The next step is to install the phidgets-ffi gem. This is the library that allows you to program Phidgets with Ruby. For more information, please see phidgets-ffi at [http://rubygems.org/gems/phidgets-ffi RubyGems] and [https://github.com/kreynolds/phidgets-ffi GitHub].&lt;br /&gt;
&lt;br /&gt;
==Description of Library Files==&lt;br /&gt;
Ruby programs on OS X and Linux depend on the following files, which the installers put into your system. &lt;br /&gt;
&lt;br /&gt;
If you are using OS X, you will need the:&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|Phidget21.framework}}&amp;lt;/b&amp;gt; contains the actual Phidgets library for OS X, which is used at run-time.  &lt;br /&gt;
&lt;br /&gt;
If you are using Linux, you will need the:&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|libphidget21.so}}&amp;lt;/b&amp;gt; contains the actual Phidgets library for Linux, which is used at run-time.&lt;br /&gt;
&lt;br /&gt;
For both OS X and Linux, you will need the phidgets-ffi gem: &lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|phidgets-ffi}}&amp;lt;/b&amp;gt; is the Phidgets gem for Ruby. This gem contains the library as well as example code.&lt;br /&gt;
&lt;br /&gt;
==Installing phidgets-ffi==&lt;br /&gt;
&lt;br /&gt;
The phidgets-ffi gem relies on the [http://rubygems.org/gems/ffi ffi gem]. So, if you do not already have it installed, please do so. A version of ffi 1.0.9 or greater is needed. Type the following to install ffi:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  gem install ffi&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Next, please install the phidgets-ffi gem by typing the following into command line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  gem install phidgets-ffi&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For OS X systems, the gem will be installed into {{Code|/Library/Ruby/Gems/1.x/phidgets-ffi-x.x.x}}.&lt;br /&gt;
&lt;br /&gt;
For typical Linux systems, the gem will be installed into {{Code|var/lib/gems/1.9/gems/phidgets-ffi-x.x.x}}.&lt;br /&gt;
&lt;br /&gt;
===Use Our Examples===&lt;br /&gt;
&lt;br /&gt;
Open a command line terminal and navigate to the phidgets-ffi gem directory. {{Code|cd}} into the {{Code|examples}} folder. Here, you will find all of the examples available for Ruby.  If you aren&#039;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]].&lt;br /&gt;
&lt;br /&gt;
The easiest way to confirm that your environment is set up properly will be to compile and run the {{Code|HelloWorld}} Ruby example.&lt;br /&gt;
&lt;br /&gt;
The only thing left to do is to run the examples! Type the following into command line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  ruby HelloWorld.rb&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This program will detect for devices that are attached/detached on the computer. Go ahead, and attach or detach your devices! Here is an example output:&lt;br /&gt;
&lt;br /&gt;
[[File:Ruby Linux HelloWorld Output.PNG|link=|alt=HelloWorld Output]]&lt;br /&gt;
&lt;br /&gt;
After confirming that the {{Code|HelloWorld}} example is working, you can proceed to run the example for your device. If you aren&#039;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 ensure that you have set your start up project to be the one that matches your device before compiling. &lt;br /&gt;
&lt;br /&gt;
Once you have the Ruby examples running, we have a [[#Follow the Examples|teaching]] section below to help you follow them.&lt;br /&gt;
&lt;br /&gt;
==Write Your Own Code==&lt;br /&gt;
&lt;br /&gt;
When you are building a project from scratch, or adding Phidget function calls to an existing project, you&#039;ll need to configure your environment to properly link the Phidget Ruby libraries.&lt;br /&gt;
&lt;br /&gt;
Simply, add the following two lines to the beginning of any {{Code|.rb}} script to make use of the phidgets-ffi gem&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  require &#039;rubygems&#039;&lt;br /&gt;
  require &#039;phidgets-ffi&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The project now has access to the Phidget21 function calls and you are ready to begin coding.&lt;br /&gt;
&lt;br /&gt;
The same [[#Follow the Examples|teaching ]] section which describes the examples also has further resources for programming your Phidget.&lt;br /&gt;
&lt;br /&gt;
==Follow the Examples==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
Next, comes our API information.  These resources outline the Ruby Phidget functions:&lt;br /&gt;
&lt;br /&gt;
{{UsingAPhidgetInCodeGeneral|both of which are available in Ruby|[http://www.phidgets.com/documentation/rubydoc Ruby API]}}&lt;br /&gt;
&lt;br /&gt;
===Example Flow===&lt;br /&gt;
&lt;br /&gt;
{{ExamplePseudocode|In Ruby, you can name these &#039;&#039;&#039;event&#039;&#039;&#039; 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. &amp;lt;br&amp;gt; &lt;br /&gt;
In the example code, the event functions common to all Phidgets are called things like &#039;&#039;&#039;AttachHandler()&#039;&#039;&#039; and &#039;&#039;&#039;DetachHandler()&#039;&#039;&#039;, etc.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
Other functions are given in the examples to show you more detail on using your Phidget.  For example, &#039;&#039;&#039;DeviceInitialize()&#039;&#039;&#039; will show what needs to be set up for your Phidget before using it.&lt;br /&gt;
|Creating a Phidget software object in Ruby is specific to the Phidget.  For a Phidget Spatial, for example, this would involve creating a {{Code|Spatial}} object.  The examples show how to do this and other API functions.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The object provides device specific methods and properties which are available from the API for your specific Phidget.|&lt;br /&gt;
[http://www.phidgets.com/documentation/rubydoc Ruby API]}}&lt;br /&gt;
&lt;br /&gt;
===Code Snippets===&lt;br /&gt;
&lt;br /&gt;
Specific calls in Python will differ in syntax from those on the [[General Phidget Programming]] page, but the concepts stay the same.  &lt;br /&gt;
&lt;br /&gt;
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 Python 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.&lt;br /&gt;
&lt;br /&gt;
For example, if we were using a [[Product - 1018 - PhidgetInterfaceKit 8/8/8 | Phidget Interface Kit]] as our device, the general calls would look like this:&lt;br /&gt;
&lt;br /&gt;
====Step One: Initialize and Open====&lt;br /&gt;
&lt;br /&gt;
The Phidget constructor method will need to be called to create the Phidget object. There are two methods or programming a Phidget in Ruby: with and without a block. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  # Without a block&lt;br /&gt;
    device = Phidgets::InterfaceKit.new&lt;br /&gt;
&lt;br /&gt;
  # With a block&lt;br /&gt;
    Phidgets::InterfaceKit.new do |device|&lt;br /&gt;
      ..&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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. In the above example, InterfaceKit represents the PhidgetInterfaceKit. For simplicity in explaining the programming concepts, the remainder of the code snippet section will use the InterfaceKit object.&lt;br /&gt;
&lt;br /&gt;
For the most part, the two methods behave the same, but there are some subtle differences, which will be explained in the next section.&lt;br /&gt;
&lt;br /&gt;
====Step Two: Wait for Attachment (plugging in) of the Phidget==== &lt;br /&gt;
&lt;br /&gt;
The program needs to try and connect to the Phidget. Options can be added to the InterfaceKit constructor to connect to the first Phidget it finds, based on its serial number, label, or even connect across the network. Rubydocs API Documentation lists all of the available modes that the constructor provides. &lt;br /&gt;
&lt;br /&gt;
For example, the following will try to connect to the first Phidget it finds:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device= Phidgets::InterfaceKit.new&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following will try to connect to a Phidget over the Phidget WebService with a serial number of {{Code|99999}}, and a server id of {{Code|myserver}}: &lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  options = {:serial_number =&amp;gt; 99999, :server_id =&amp;gt; ‘myserver’, :password =&amp;gt; nil}&lt;br /&gt;
  device= Phidgets::InterfaceKit.new(options)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One important thing to remember is that when working with Phidgets, a local connection will reserve the device until closed. This prevents any other instances from retrieving data from the Phidget, including other programs. The one connection per device limit does not apply when exclusively using the Phidget WebService.&lt;br /&gt;
&lt;br /&gt;
{{Code|Phidgets::InterfaceKit.new}} will tell the program to continuously try to connect to a Phidget, based on the options given(if any), even trying to reconnect if it gets disconnected. &lt;br /&gt;
&lt;br /&gt;
=====Non Block Programming=====&lt;br /&gt;
For the non block programming method, simply calling the constructor does not guarantee you can use the Phidget immediately. We can handle this by using event driven programming and  tracking the {{Code|on_attach}} and {{Code|on_detach}} events, or by calling: {{Code|wait_for_attachment}}. {{Code|wait_for_attachment}} will block indefinitely until a connection is made to the Phidget, or an optional timeout is exceeded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device= Phidgets::InterfaceKit.new&lt;br /&gt;
  device.wait_for_attachment 2000 #halt the program for up 2000 milliseconds or until the Phidget is connected&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Please also remember to call close at the end or the program to free any locks on the Phidget&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device.close&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=====Block Programming=====&lt;br /&gt;
If you are programming inside a block, {{Code|wait_for_attachment}} is automatically called. By default, it will halt the program and try to connect to the Phidget for up to 1000 milliseconds.  Afterwards, the block will yield. Finally, close is not needed as it is automatically called once the block has yield.&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  options = {:timeout =&amp;gt; 2000)&lt;br /&gt;
  Phidgets::InterfaceKit.new(options) do |device|&lt;br /&gt;
    ... &lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Sometimes, it makes more sense to handle the attachment via an event.  This would be in instances where the Phidget is being plugged and unplugged, and you want to handle these incidents.  Or, when you want to use event-driven programming because you have a GUI-driven program.  In these cases, an event-driven code snippet to handle the attachment might look something like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device.on_attach  do |device, obj|&lt;br /&gt;
    puts &amp;quot;Id: #{device.id}&amp;quot;&lt;br /&gt;
    puts &amp;quot;Serial number: #{device.serial_number}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Step Three: Do Things with the Phidget====&lt;br /&gt;
&lt;br /&gt;
You can read data and interact with your Phidget both by polling it for its current state (or to set a state), or by catching events that trigger when the data changes.&lt;br /&gt;
&lt;br /&gt;
For our [[Product - 1018 - PhidgetInterfaceKit 8/8/8 | Phidget Interface Kit]], the polling method of getting the current sensor state and setting an output state looks something like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
&lt;br /&gt;
# Get a data point from Analog Port 0&lt;br /&gt;
puts &amp;quot;Sensor value[0]: #{device.sensors[0].to_i}&amp;quot; &lt;br /&gt;
&lt;br /&gt;
# Set digital output port 0 to be on&lt;br /&gt;
device.outputs[0].state = true&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To catch data changes via events, you would use something like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device.on_sensor_change do |device, input, value, obj|&lt;br /&gt;
    puts &amp;quot;Sensor #{input.index}&#039;s value has changed to #{value}&amp;quot;&lt;br /&gt;
  end &lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=====Step Four: Close=====&lt;br /&gt;
 &lt;br /&gt;
If you are using the block programming method, you do not need to worry about closing the Phidget. It is already taken care of when the block ends. However, if you are using the non block programming method, then you will need take special consideration that the Phidget is closed. &lt;br /&gt;
&lt;br /&gt;
At the end of your program, don’t forget to call {{Code|close}} to free any locks on the Phidget that the Phidget constructor call put in place!&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device.close&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;complete&#039;&#039; set of functions you have available for all Phidgets can be found in the [http://www.phidgets.com/documentation/PythonDoc.zip Python API]. You can also find more description on any device-specific function either in the [[Device List|Device API]] page for calls available only on your specific Phidget.&lt;br /&gt;
&lt;br /&gt;
=====Raw FFI=====&lt;br /&gt;
As an alternative to programming with the method as outlined in this document, you can also program making straight C calls through FFI. Please refer to the files in {{Code|&amp;lt;Ruby Gems Directory&amp;gt;/phidgets-ffi-x.x.x/lib/phidgets-ffi/ffi}} to see a list of available methods, and the [http://rubydoc.info/gems/phidgets-ffi/0.1.1/frames Ruby API] for usage. There are raw ffi examples in {{Code|&amp;lt;Ruby Gems directory&amp;gt;/phidgets-ffi-x.x.x/examples/raw-ffi}}.&lt;br /&gt;
&lt;br /&gt;
==Common Problems and Solutions/Workarounds==&lt;br /&gt;
&lt;br /&gt;
Here you can put various frequent problems and our recommended solutions.&lt;/div&gt;</summary>
		<author><name>Mphi</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs21/index.php?title=Language_-_Ruby&amp;diff=19108</id>
		<title>Language - Ruby</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs21/index.php?title=Language_-_Ruby&amp;diff=19108"/>
		<updated>2012-04-19T21:16:45Z</updated>

		<summary type="html">&lt;p&gt;Mphi: /* Code Snippets */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Language]]&lt;br /&gt;
[[File:Icon-Ruby.png|64x64px||link=|alt=]]Ruby is an interpreted and object oriented scripting language with simple syntax created by Yukihiro Matsumoto.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
{{LanguageSupport|Ruby|the complete Phidget API, including events|all Phidget devices.|Linux and OS X|}}&lt;br /&gt;
&lt;br /&gt;
{{QuickDownloads|Ruby|&lt;br /&gt;
{{APIQuickDownloads|http://www.phidgets.com/documentation/rubydoc RubyDoc}}|&lt;br /&gt;
{{ExampleQuickDownloads|http://rubygems.org/gems/phidgets-ffi| (@RubyGems)}}|&lt;br /&gt;
{{ExtraLibraryQuickDownloads|http://rubygems.org/gems/phidgets-ffi|Ruby| (@RubyGems)}}&lt;br /&gt;
{{MacQuickDownloads}}&lt;br /&gt;
{{LinuxQuickDownloads}}&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
==Getting started with Ruby==&lt;br /&gt;
&lt;br /&gt;
If you are new to writing code for Phidgets, we recommend starting by running, then modifying existing examples. This will allow you to:&lt;br /&gt;
{{ExampleCodeReasons}}&lt;br /&gt;
&lt;br /&gt;
Instructions for OS X and Linux are similar, so they will be combined into the same section.&lt;br /&gt;
&lt;br /&gt;
The first step in using Ruby on Mac or Linux is to install the Phidget libraries.  Compile and install them as explained on the [[Device List|getting started guide for your device]].&lt;br /&gt;
&lt;br /&gt;
The next step is to install the phidgets-ffi gem. This is the library that allows you to program Phidgets with Ruby. For more information, please see phidgets-ffi at [http://rubygems.org/gems/phidgets-ffi RubyGems] and [https://github.com/kreynolds/phidgets-ffi GitHub].&lt;br /&gt;
&lt;br /&gt;
==Description of Library Files==&lt;br /&gt;
Ruby programs on OS X and Linux depend on the following files, which the installers put into your system. &lt;br /&gt;
&lt;br /&gt;
If you are using OS X, you will need the:&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|Phidget21.framework}}&amp;lt;/b&amp;gt; contains the actual Phidgets library for OS X, which is used at run-time.  &lt;br /&gt;
&lt;br /&gt;
If you are using Linux, you will need the:&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|libphidget21.so}}&amp;lt;/b&amp;gt; contains the actual Phidgets library for Linux, which is used at run-time.&lt;br /&gt;
&lt;br /&gt;
For both OS X and Linux, you will need the phidgets-ffi gem: &lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|phidgets-ffi}}&amp;lt;/b&amp;gt; is the Phidgets gem for Ruby. This gem contains the library as well as example code.&lt;br /&gt;
&lt;br /&gt;
==Installing phidgets-ffi==&lt;br /&gt;
&lt;br /&gt;
The phidgets-ffi gem relies on the [http://rubygems.org/gems/ffi ffi gem]. So, if you do not already have it installed, please do so. A version of ffi 1.0.9 or greater is needed. Type the following to install ffi:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  gem install ffi&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Next, please install the phidgets-ffi gem by typing the following into command line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  gem install phidgets-ffi&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For OS X systems, the gem will be installed into {{Code|/Library/Ruby/Gems/1.x/phidgets-ffi-x.x.x}}.&lt;br /&gt;
&lt;br /&gt;
For typical Linux systems, the gem will be installed into {{Code|var/lib/gems/1.9/gems/phidgets-ffi-x.x.x}}.&lt;br /&gt;
&lt;br /&gt;
===Use Our Examples===&lt;br /&gt;
&lt;br /&gt;
Open a command line terminal and navigate to the phidgets-ffi gem directory. {{Code|cd}} into the {{Code|examples}} folder. Here, you will find all of the examples available for Ruby.  If you aren&#039;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]].&lt;br /&gt;
&lt;br /&gt;
The easiest way to confirm that your environment is set up properly will be to compile and run the {{Code|HelloWorld}} Ruby example.&lt;br /&gt;
&lt;br /&gt;
The only thing left to do is to run the examples! Type the following into command line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  ruby HelloWorld.rb&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This program will detect for devices that are attached/detached on the computer. Go ahead, and attach or detach your devices! Here is an example output:&lt;br /&gt;
&lt;br /&gt;
[[File:Ruby Linux HelloWorld Output.PNG|link=|alt=HelloWorld Output]]&lt;br /&gt;
&lt;br /&gt;
After confirming that the {{Code|HelloWorld}} example is working, you can proceed to run the example for your device. If you aren&#039;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 ensure that you have set your start up project to be the one that matches your device before compiling. &lt;br /&gt;
&lt;br /&gt;
Once you have the Ruby examples running, we have a [[#Follow the Examples|teaching]] section below to help you follow them.&lt;br /&gt;
&lt;br /&gt;
==Write Your Own Code==&lt;br /&gt;
&lt;br /&gt;
When you are building a project from scratch, or adding Phidget function calls to an existing project, you&#039;ll need to configure your environment to properly link the Phidget Ruby libraries.&lt;br /&gt;
&lt;br /&gt;
Simply, add the following two lines to the beginning of any {{Code|.rb}} script to make use of the phidgets-ffi gem&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  require &#039;rubygems&#039;&lt;br /&gt;
  require &#039;phidgets-ffi&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The project now has access to the Phidget21 function calls and you are ready to begin coding.&lt;br /&gt;
&lt;br /&gt;
The same [[#Follow the Examples|teaching ]] section which describes the examples also has further resources for programming your Phidget.&lt;br /&gt;
&lt;br /&gt;
==Follow the Examples==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
Next, comes our API information.  These resources outline the Ruby Phidget functions:&lt;br /&gt;
&lt;br /&gt;
{{UsingAPhidgetInCodeGeneral|both of which are available in Ruby|[http://www.phidgets.com/documentation/rubydoc Ruby API]}}&lt;br /&gt;
&lt;br /&gt;
===Example Flow===&lt;br /&gt;
&lt;br /&gt;
{{ExamplePseudocode|In Ruby, you can name these &#039;&#039;&#039;event&#039;&#039;&#039; 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. &amp;lt;br&amp;gt; &lt;br /&gt;
In the example code, the event functions common to all Phidgets are called things like &#039;&#039;&#039;AttachHandler()&#039;&#039;&#039; and &#039;&#039;&#039;DetachHandler()&#039;&#039;&#039;, etc.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
Other functions are given in the examples to show you more detail on using your Phidget.  For example, &#039;&#039;&#039;DeviceInitialize()&#039;&#039;&#039; will show what needs to be set up for your Phidget before using it.&lt;br /&gt;
|Creating a Phidget software object in Ruby is specific to the Phidget.  For a Phidget Spatial, for example, this would involve creating a {{Code|Spatial}} object.  The examples show how to do this and other API functions.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The object provides device specific methods and properties which are available from the API for your specific Phidget.|&lt;br /&gt;
[http://www.phidgets.com/documentation/rubydoc Ruby API]}}&lt;br /&gt;
&lt;br /&gt;
===Code Snippets===&lt;br /&gt;
&lt;br /&gt;
Specific calls in Python will differ in syntax from those on the [[General Phidget Programming]] page, but the concepts stay the same.  &lt;br /&gt;
&lt;br /&gt;
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 Python 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.&lt;br /&gt;
&lt;br /&gt;
For example, if we were using a [[Product - 1018 - PhidgetInterfaceKit 8/8/8 | Phidget Interface Kit]] as our device, the general calls would look like this:&lt;br /&gt;
&lt;br /&gt;
====Step One: Initialize and Open====&lt;br /&gt;
&lt;br /&gt;
The Phidget constructor method will need to be called to create the Phidget object. There are two methods or programming a Phidget in Ruby: with and without a block. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  # Without a block&lt;br /&gt;
    device = Phidgets::InterfaceKit.new&lt;br /&gt;
&lt;br /&gt;
  # With a block&lt;br /&gt;
    Phidgets::InterfaceKit.new do |device|&lt;br /&gt;
      ..&lt;br /&gt;
    end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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. In the above example, InterfaceKit represents the PhidgetInterfaceKit. For simplicity in explaining the programming concepts, the remainder of the code snippet section will use the InterfaceKit object.&lt;br /&gt;
&lt;br /&gt;
For the most part, the two methods behave the same, but there are some subtle differences, which will be explained in the next section.&lt;br /&gt;
&lt;br /&gt;
====Step Two: Wait for Attachment (plugging in) of the Phidget==== &lt;br /&gt;
&lt;br /&gt;
The program needs to try and connect to the Phidget. Options can be added to the InterfaceKit constructor to connect to the first Phidget it finds, based on its serial number, label, or even connect across the network. Rubydocs API Documentation lists all of the available modes that the constructor provides. &lt;br /&gt;
&lt;br /&gt;
For example, the following will try to connect to the first Phidget it finds:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device= Phidgets::InterfaceKit.new&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The following will try to connect to a Phidget over the Phidget WebService with a serial number of {{Code|99999}}, and a server id of {{Code|myserver}}: &lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  options = {:serial_number =&amp;gt; 99999, :server_id =&amp;gt; ‘myserver’, :password =&amp;gt; nil}&lt;br /&gt;
  device= Phidgets::InterfaceKit.new(options)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One important thing to remember is that when working with Phidgets, a local connection will reserve the device until closed. This prevents any other instances from retrieving data from the Phidget, including other programs. The one connection per device limit does not apply when exclusively using the Phidget WebService.&lt;br /&gt;
&lt;br /&gt;
{{Code|Phidgets::InterfaceKit.new}} will tell the program to continuously try to connect to a Phidget, based on the options given(if any), even trying to reconnect if it gets disconnected. &lt;br /&gt;
&lt;br /&gt;
=====Non Block Programming=====&lt;br /&gt;
For the non block programming method, simply calling the constructor does not guarantee you can use the Phidget immediately. We can handle this by using event driven programming and  tracking the {{Code|on_attach}} and {{Code|on_detach}} events, or by calling: {{Code|wait_for_attachment}}. {{Code|wait_for_attachment}} will block indefinitely until a connection is made to the Phidget, or an optional timeout is exceeded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device= Phidgets::InterfaceKit.new&lt;br /&gt;
  device.wait_for_attachment 2000 #halt the program for up 2000 milliseconds or until the Phidget is connected&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Please also remember to call close at the end or the program to free any locks on the Phidget&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device.close&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=====Block Programming=====&lt;br /&gt;
If you are programming inside a block, {{Code|wait_for_attachment}} is automatically called. By default, it will halt the program and try to connect to the Phidget for up to 1000 milliseconds.  Afterwards, the block will yield. Finally, close is not needed as it is automatically called once the block has yield.&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  options = {:timeout =&amp;gt; 2000)&lt;br /&gt;
  Phidgets::InterfaceKit.new(options) do |device|&lt;br /&gt;
    ... &lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Sometimes, it makes more sense to handle the attachment via an event.  This would be in instances where the Phidget is being plugged and unplugged, and you want to handle these incidents.  Or, when you want to use event-driven programming because you have a GUI-driven program.  In these cases, an event-driven code snippet to handle the attachment might look something like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device.on_attach  do |device, obj|&lt;br /&gt;
    puts &amp;quot;Id: #{device.id}&amp;quot;&lt;br /&gt;
    puts &amp;quot;Serial number: #{device.serial_number}&amp;quot;&lt;br /&gt;
&lt;br /&gt;
  end&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Step Three: Do Things with the Phidget====&lt;br /&gt;
&lt;br /&gt;
You can read data and interact with your Phidget both by polling it for its current state (or to set a state), or by catching events that trigger when the data changes.&lt;br /&gt;
&lt;br /&gt;
For our [[Product - 1018 - PhidgetInterfaceKit 8/8/8 | Phidget Interface Kit]], the polling method of getting the current sensor state and setting an output state looks something like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
&lt;br /&gt;
# Get a data point from Analog Port 0&lt;br /&gt;
puts &amp;quot;Sensor value[0]: #{device.sensors[0].to_i}&amp;quot; &lt;br /&gt;
&lt;br /&gt;
# Set digital output port 0 to be on&lt;br /&gt;
device.outputs[0].state = true&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To catch data changes via events, you would use something like this:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device.on_sensor_change do |device, input, value, obj|&lt;br /&gt;
    puts &amp;quot;Sensor #{input.index}&#039;s value has changed to #{value}&amp;quot;&lt;br /&gt;
  end &lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Step Four: Close=====&lt;br /&gt;
 &lt;br /&gt;
If you are using the block programming method, you do not need to worry about closing the Phidget. It is already taken care of when the block ends. However, if you are using the non block programming method, then you will need take special consideration that the Phidget is closed. &lt;br /&gt;
&lt;br /&gt;
At the end of your program, don’t forget to call {{Code|close}} to free any locks on the Phidget that the Phidget constructor call put in place!&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  device.close&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;complete&#039;&#039; set of functions you have available for all Phidgets can be found in the [http://www.phidgets.com/documentation/PythonDoc.zip Python API]. You can also find more description on any device-specific function either in the [[Device List|Device API]] page for calls available only on your specific Phidget.&lt;br /&gt;
&lt;br /&gt;
==Common Problems and Solutions/Workarounds==&lt;br /&gt;
&lt;br /&gt;
Here you can put various frequent problems and our recommended solutions.&lt;/div&gt;</summary>
		<author><name>Mphi</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs21/index.php?title=Language_-_Ruby&amp;diff=19107</id>
		<title>Language - Ruby</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs21/index.php?title=Language_-_Ruby&amp;diff=19107"/>
		<updated>2012-04-19T20:51:50Z</updated>

		<summary type="html">&lt;p&gt;Mphi: /* Description of Library Files */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Language]]&lt;br /&gt;
[[File:Icon-Ruby.png|64x64px||link=|alt=]]Ruby is an interpreted and object oriented scripting language with simple syntax created by Yukihiro Matsumoto.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
{{LanguageSupport|Ruby|the complete Phidget API, including events|all Phidget devices.|Linux and OS X|}}&lt;br /&gt;
&lt;br /&gt;
{{QuickDownloads|Ruby|&lt;br /&gt;
{{APIQuickDownloads|http://www.phidgets.com/documentation/rubydoc RubyDoc}}|&lt;br /&gt;
{{ExampleQuickDownloads|http://rubygems.org/gems/phidgets-ffi| (@RubyGems)}}|&lt;br /&gt;
{{ExtraLibraryQuickDownloads|http://rubygems.org/gems/phidgets-ffi|Ruby| (@RubyGems)}}&lt;br /&gt;
{{MacQuickDownloads}}&lt;br /&gt;
{{LinuxQuickDownloads}}&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
==Getting started with Ruby==&lt;br /&gt;
&lt;br /&gt;
If you are new to writing code for Phidgets, we recommend starting by running, then modifying existing examples. This will allow you to:&lt;br /&gt;
{{ExampleCodeReasons}}&lt;br /&gt;
&lt;br /&gt;
Instructions for OS X and Linux are similar, so they will be combined into the same section.&lt;br /&gt;
&lt;br /&gt;
The first step in using Ruby on Mac or Linux is to install the Phidget libraries.  Compile and install them as explained on the [[Device List|getting started guide for your device]].&lt;br /&gt;
&lt;br /&gt;
The next step is to install the phidgets-ffi gem. This is the library that allows you to program Phidgets with Ruby. For more information, please see phidgets-ffi at [http://rubygems.org/gems/phidgets-ffi RubyGems] and [https://github.com/kreynolds/phidgets-ffi GitHub].&lt;br /&gt;
&lt;br /&gt;
==Description of Library Files==&lt;br /&gt;
Ruby programs on OS X and Linux depend on the following files, which the installers put into your system. &lt;br /&gt;
&lt;br /&gt;
If you are using OS X, you will need the:&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|Phidget21.framework}}&amp;lt;/b&amp;gt; contains the actual Phidgets library for OS X, which is used at run-time.  &lt;br /&gt;
&lt;br /&gt;
If you are using Linux, you will need the:&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|libphidget21.so}}&amp;lt;/b&amp;gt; contains the actual Phidgets library for Linux, which is used at run-time.&lt;br /&gt;
&lt;br /&gt;
For both OS X and Linux, you will need the phidgets-ffi gem: &lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|phidgets-ffi}}&amp;lt;/b&amp;gt; is the Phidgets gem for Ruby. This gem contains the library as well as example code.&lt;br /&gt;
&lt;br /&gt;
==Installing phidgets-ffi==&lt;br /&gt;
&lt;br /&gt;
The phidgets-ffi gem relies on the [http://rubygems.org/gems/ffi ffi gem]. So, if you do not already have it installed, please do so. A version of ffi 1.0.9 or greater is needed. Type the following to install ffi:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  gem install ffi&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Next, please install the phidgets-ffi gem by typing the following into command line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  gem install phidgets-ffi&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For OS X systems, the gem will be installed into {{Code|/Library/Ruby/Gems/1.x/phidgets-ffi-x.x.x}}.&lt;br /&gt;
&lt;br /&gt;
For typical Linux systems, the gem will be installed into {{Code|var/lib/gems/1.9/gems/phidgets-ffi-x.x.x}}.&lt;br /&gt;
&lt;br /&gt;
===Use Our Examples===&lt;br /&gt;
&lt;br /&gt;
Open a command line terminal and navigate to the phidgets-ffi gem directory. {{Code|cd}} into the {{Code|examples}} folder. Here, you will find all of the examples available for Ruby.  If you aren&#039;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]].&lt;br /&gt;
&lt;br /&gt;
The easiest way to confirm that your environment is set up properly will be to compile and run the {{Code|HelloWorld}} Ruby example.&lt;br /&gt;
&lt;br /&gt;
The only thing left to do is to run the examples! Type the following into command line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  ruby HelloWorld.rb&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This program will detect for devices that are attached/detached on the computer. Go ahead, and attach or detach your devices! Here is an example output:&lt;br /&gt;
&lt;br /&gt;
[[File:Ruby Linux HelloWorld Output.PNG|link=|alt=HelloWorld Output]]&lt;br /&gt;
&lt;br /&gt;
After confirming that the {{Code|HelloWorld}} example is working, you can proceed to run the example for your device. If you aren&#039;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 ensure that you have set your start up project to be the one that matches your device before compiling. &lt;br /&gt;
&lt;br /&gt;
Once you have the Ruby examples running, we have a [[#Follow the Examples|teaching]] section below to help you follow them.&lt;br /&gt;
&lt;br /&gt;
==Write Your Own Code==&lt;br /&gt;
&lt;br /&gt;
When you are building a project from scratch, or adding Phidget function calls to an existing project, you&#039;ll need to configure your environment to properly link the Phidget Ruby libraries.&lt;br /&gt;
&lt;br /&gt;
Simply, add the following two lines to the beginning of any {{Code|.rb}} script to make use of the phidgets-ffi gem&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  require &#039;rubygems&#039;&lt;br /&gt;
  require &#039;phidgets-ffi&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The project now has access to the Phidget21 function calls and you are ready to begin coding.&lt;br /&gt;
&lt;br /&gt;
The same [[#Follow the Examples|teaching ]] section which describes the examples also has further resources for programming your Phidget.&lt;br /&gt;
&lt;br /&gt;
==Follow the Examples==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
Next, comes our API information.  These resources outline the Ruby Phidget functions:&lt;br /&gt;
&lt;br /&gt;
{{UsingAPhidgetInCodeGeneral|both of which are available in Ruby|[http://www.phidgets.com/documentation/rubydoc Ruby API]}}&lt;br /&gt;
&lt;br /&gt;
===Example Flow===&lt;br /&gt;
&lt;br /&gt;
{{ExamplePseudocode|In Ruby, you can name these &#039;&#039;&#039;event&#039;&#039;&#039; 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. &amp;lt;br&amp;gt; &lt;br /&gt;
In the example code, the event functions common to all Phidgets are called things like &#039;&#039;&#039;AttachHandler()&#039;&#039;&#039; and &#039;&#039;&#039;DetachHandler()&#039;&#039;&#039;, etc.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
Other functions are given in the examples to show you more detail on using your Phidget.  For example, &#039;&#039;&#039;DeviceInitialize()&#039;&#039;&#039; will show what needs to be set up for your Phidget before using it.&lt;br /&gt;
|Creating a Phidget software object in Ruby is specific to the Phidget.  For a Phidget Spatial, for example, this would involve creating a {{Code|Spatial}} object.  The examples show how to do this and other API functions.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The object provides device specific methods and properties which are available from the API for your specific Phidget.|&lt;br /&gt;
[http://www.phidgets.com/documentation/rubydoc Ruby API]}}&lt;br /&gt;
&lt;br /&gt;
==Code Snippets==&lt;br /&gt;
&lt;br /&gt;
==Common Problems and Solutions/Workarounds==&lt;br /&gt;
&lt;br /&gt;
Here you can put various frequent problems and our recommended solutions.&lt;/div&gt;</summary>
		<author><name>Mphi</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs21/index.php?title=Language_-_Ruby&amp;diff=19106</id>
		<title>Language - Ruby</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs21/index.php?title=Language_-_Ruby&amp;diff=19106"/>
		<updated>2012-04-19T20:51:06Z</updated>

		<summary type="html">&lt;p&gt;Mphi: /* Introduction */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Language]]&lt;br /&gt;
[[File:Icon-Ruby.png|64x64px||link=|alt=]]Ruby is an interpreted and object oriented scripting language with simple syntax created by Yukihiro Matsumoto.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
{{LanguageSupport|Ruby|the complete Phidget API, including events|all Phidget devices.|Linux and OS X|}}&lt;br /&gt;
&lt;br /&gt;
{{QuickDownloads|Ruby|&lt;br /&gt;
{{APIQuickDownloads|http://www.phidgets.com/documentation/rubydoc RubyDoc}}|&lt;br /&gt;
{{ExampleQuickDownloads|http://rubygems.org/gems/phidgets-ffi| (@RubyGems)}}|&lt;br /&gt;
{{ExtraLibraryQuickDownloads|http://rubygems.org/gems/phidgets-ffi|Ruby| (@RubyGems)}}&lt;br /&gt;
{{MacQuickDownloads}}&lt;br /&gt;
{{LinuxQuickDownloads}}&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
==Getting started with Ruby==&lt;br /&gt;
&lt;br /&gt;
If you are new to writing code for Phidgets, we recommend starting by running, then modifying existing examples. This will allow you to:&lt;br /&gt;
{{ExampleCodeReasons}}&lt;br /&gt;
&lt;br /&gt;
Instructions for OS X and Linux are similar, so they will be combined into the same section.&lt;br /&gt;
&lt;br /&gt;
The first step in using Ruby on Mac or Linux is to install the Phidget libraries.  Compile and install them as explained on the [[Device List|getting started guide for your device]].&lt;br /&gt;
&lt;br /&gt;
The next step is to install the phidgets-ffi gem. This is the library that allows you to program Phidgets with Ruby. For more information, please see phidgets-ffi at [http://rubygems.org/gems/phidgets-ffi RubyGems] and [https://github.com/kreynolds/phidgets-ffi GitHub].&lt;br /&gt;
&lt;br /&gt;
==Description of Library Files==&lt;br /&gt;
Ruby programs on OS X and Linux depend on the following files, which the installers put into your system. &lt;br /&gt;
&lt;br /&gt;
If you are using OS X, you will need the:&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|Phidget21.framework}}&amp;lt;/b&amp;gt; contains the actual Phidgets library for OS X, which is used at run-time.  &lt;br /&gt;
&lt;br /&gt;
If you are using Linux, you will need the:&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|libphidget21.so}}&amp;lt;/b&amp;gt; contains the actual Phidgets library for Linux, which is used at run-time.&lt;br /&gt;
&lt;br /&gt;
For both OS X and Linux, you will need the phidgets-ffi gem. &lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|phidgets-ffi}}&amp;lt;/b&amp;gt; is the Phidgets gem for Ruby. This gem contains the library as well as example code.&lt;br /&gt;
&lt;br /&gt;
==Installing phidgets-ffi==&lt;br /&gt;
&lt;br /&gt;
The phidgets-ffi gem relies on the [http://rubygems.org/gems/ffi ffi gem]. So, if you do not already have it installed, please do so. A version of ffi 1.0.9 or greater is needed. Type the following to install ffi:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  gem install ffi&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Next, please install the phidgets-ffi gem by typing the following into command line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  gem install phidgets-ffi&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For OS X systems, the gem will be installed into {{Code|/Library/Ruby/Gems/1.x/phidgets-ffi-x.x.x}}.&lt;br /&gt;
&lt;br /&gt;
For typical Linux systems, the gem will be installed into {{Code|var/lib/gems/1.9/gems/phidgets-ffi-x.x.x}}.&lt;br /&gt;
&lt;br /&gt;
===Use Our Examples===&lt;br /&gt;
&lt;br /&gt;
Open a command line terminal and navigate to the phidgets-ffi gem directory. {{Code|cd}} into the {{Code|examples}} folder. Here, you will find all of the examples available for Ruby.  If you aren&#039;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]].&lt;br /&gt;
&lt;br /&gt;
The easiest way to confirm that your environment is set up properly will be to compile and run the {{Code|HelloWorld}} Ruby example.&lt;br /&gt;
&lt;br /&gt;
The only thing left to do is to run the examples! Type the following into command line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  ruby HelloWorld.rb&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This program will detect for devices that are attached/detached on the computer. Go ahead, and attach or detach your devices! Here is an example output:&lt;br /&gt;
&lt;br /&gt;
[[File:Ruby Linux HelloWorld Output.PNG|link=|alt=HelloWorld Output]]&lt;br /&gt;
&lt;br /&gt;
After confirming that the {{Code|HelloWorld}} example is working, you can proceed to run the example for your device. If you aren&#039;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 ensure that you have set your start up project to be the one that matches your device before compiling. &lt;br /&gt;
&lt;br /&gt;
Once you have the Ruby examples running, we have a [[#Follow the Examples|teaching]] section below to help you follow them.&lt;br /&gt;
&lt;br /&gt;
==Write Your Own Code==&lt;br /&gt;
&lt;br /&gt;
When you are building a project from scratch, or adding Phidget function calls to an existing project, you&#039;ll need to configure your environment to properly link the Phidget Ruby libraries.&lt;br /&gt;
&lt;br /&gt;
Simply, add the following two lines to the beginning of any {{Code|.rb}} script to make use of the phidgets-ffi gem&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  require &#039;rubygems&#039;&lt;br /&gt;
  require &#039;phidgets-ffi&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The project now has access to the Phidget21 function calls and you are ready to begin coding.&lt;br /&gt;
&lt;br /&gt;
The same [[#Follow the Examples|teaching ]] section which describes the examples also has further resources for programming your Phidget.&lt;br /&gt;
&lt;br /&gt;
==Follow the Examples==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
Next, comes our API information.  These resources outline the Ruby Phidget functions:&lt;br /&gt;
&lt;br /&gt;
{{UsingAPhidgetInCodeGeneral|both of which are available in Ruby|[http://www.phidgets.com/documentation/rubydoc Ruby API]}}&lt;br /&gt;
&lt;br /&gt;
===Example Flow===&lt;br /&gt;
&lt;br /&gt;
{{ExamplePseudocode|In Ruby, you can name these &#039;&#039;&#039;event&#039;&#039;&#039; 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. &amp;lt;br&amp;gt; &lt;br /&gt;
In the example code, the event functions common to all Phidgets are called things like &#039;&#039;&#039;AttachHandler()&#039;&#039;&#039; and &#039;&#039;&#039;DetachHandler()&#039;&#039;&#039;, etc.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
Other functions are given in the examples to show you more detail on using your Phidget.  For example, &#039;&#039;&#039;DeviceInitialize()&#039;&#039;&#039; will show what needs to be set up for your Phidget before using it.&lt;br /&gt;
|Creating a Phidget software object in Ruby is specific to the Phidget.  For a Phidget Spatial, for example, this would involve creating a {{Code|Spatial}} object.  The examples show how to do this and other API functions.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The object provides device specific methods and properties which are available from the API for your specific Phidget.|&lt;br /&gt;
[http://www.phidgets.com/documentation/rubydoc Ruby API]}}&lt;br /&gt;
&lt;br /&gt;
==Code Snippets==&lt;br /&gt;
&lt;br /&gt;
==Common Problems and Solutions/Workarounds==&lt;br /&gt;
&lt;br /&gt;
Here you can put various frequent problems and our recommended solutions.&lt;/div&gt;</summary>
		<author><name>Mphi</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs21/index.php?title=Language_-_Ruby&amp;diff=19105</id>
		<title>Language - Ruby</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs21/index.php?title=Language_-_Ruby&amp;diff=19105"/>
		<updated>2012-04-19T20:49:31Z</updated>

		<summary type="html">&lt;p&gt;Mphi: /* Quick Downloads */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Language]]&lt;br /&gt;
[[File:Icon-Ruby.png|64x64px||link=|alt=]]Ruby is an interpreted and object oriented scripting language with simple syntax created by Yukihiro Matsumoto.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
{{LanguageSupport|Ruby|the complete Phidget API, including events|all Phidget devices.|Linux and OS X|}}&lt;br /&gt;
&lt;br /&gt;
{{QuickDownloads|Ruby|&lt;br /&gt;
{{APIQuickDownloads|http://www.phidgets.com/documentation/rubydoc RubyDoc}}|&lt;br /&gt;
{{ExampleQuickDownloads|http://www.phidgets.com/downloads/examples/rubylib_2.1.8.0.html| (@RubyGems)}}|&lt;br /&gt;
{{ExtraLibraryQuickDownloads|http://www.phidgets.com/downloads/examples/rubylib_2.1.8.0.html|Ruby| (@RubyGems)}}&lt;br /&gt;
{{MacQuickDownloads}}&lt;br /&gt;
{{LinuxQuickDownloads}}&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
==Getting started with Ruby==&lt;br /&gt;
&lt;br /&gt;
If you are new to writing code for Phidgets, we recommend starting by running, then modifying existing examples. This will allow you to:&lt;br /&gt;
{{ExampleCodeReasons}}&lt;br /&gt;
&lt;br /&gt;
Instructions for OS X and Linux are similar, so they will be combined into the same section.&lt;br /&gt;
&lt;br /&gt;
The first step in using Ruby on Mac or Linux is to install the Phidget libraries.  Compile and install them as explained on the [[Device List|getting started guide for your device]].&lt;br /&gt;
&lt;br /&gt;
The next step is to install the phidgets-ffi gem. This is the library that allows you to program Phidgets with Ruby. For more information, please see phidgets-ffi at [http://rubygems.org/gems/phidgets-ffi RubyGems] and [https://github.com/kreynolds/phidgets-ffi GitHub].&lt;br /&gt;
&lt;br /&gt;
==Description of Library Files==&lt;br /&gt;
Ruby programs on OS X and Linux depend on the following files, which the installers put into your system. &lt;br /&gt;
&lt;br /&gt;
If you are using OS X, you will need the:&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|Phidget21.framework}}&amp;lt;/b&amp;gt; contains the actual Phidgets library for OS X, which is used at run-time.  &lt;br /&gt;
&lt;br /&gt;
If you are using Linux, you will need the:&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|libphidget21.so}}&amp;lt;/b&amp;gt; contains the actual Phidgets library for Linux, which is used at run-time.&lt;br /&gt;
&lt;br /&gt;
For both OS X and Linux, you will need the phidgets-ffi gem. &lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|phidgets-ffi}}&amp;lt;/b&amp;gt; is the Phidgets gem for Ruby. This gem contains the library as well as example code.&lt;br /&gt;
&lt;br /&gt;
==Installing phidgets-ffi==&lt;br /&gt;
&lt;br /&gt;
The phidgets-ffi gem relies on the [http://rubygems.org/gems/ffi ffi gem]. So, if you do not already have it installed, please do so. A version of ffi 1.0.9 or greater is needed. Type the following to install ffi:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  gem install ffi&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Next, please install the phidgets-ffi gem by typing the following into command line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  gem install phidgets-ffi&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For OS X systems, the gem will be installed into {{Code|/Library/Ruby/Gems/1.x/phidgets-ffi-x.x.x}}.&lt;br /&gt;
&lt;br /&gt;
For typical Linux systems, the gem will be installed into {{Code|var/lib/gems/1.9/gems/phidgets-ffi-x.x.x}}.&lt;br /&gt;
&lt;br /&gt;
===Use Our Examples===&lt;br /&gt;
&lt;br /&gt;
Open a command line terminal and navigate to the phidgets-ffi gem directory. {{Code|cd}} into the {{Code|examples}} folder. Here, you will find all of the examples available for Ruby.  If you aren&#039;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]].&lt;br /&gt;
&lt;br /&gt;
The easiest way to confirm that your environment is set up properly will be to compile and run the {{Code|HelloWorld}} Ruby example.&lt;br /&gt;
&lt;br /&gt;
The only thing left to do is to run the examples! Type the following into command line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  ruby HelloWorld.rb&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This program will detect for devices that are attached/detached on the computer. Go ahead, and attach or detach your devices! Here is an example output:&lt;br /&gt;
&lt;br /&gt;
[[File:Ruby Linux HelloWorld Output.PNG|link=|alt=HelloWorld Output]]&lt;br /&gt;
&lt;br /&gt;
After confirming that the {{Code|HelloWorld}} example is working, you can proceed to run the example for your device. If you aren&#039;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 ensure that you have set your start up project to be the one that matches your device before compiling. &lt;br /&gt;
&lt;br /&gt;
Once you have the Ruby examples running, we have a [[#Follow the Examples|teaching]] section below to help you follow them.&lt;br /&gt;
&lt;br /&gt;
==Write Your Own Code==&lt;br /&gt;
&lt;br /&gt;
When you are building a project from scratch, or adding Phidget function calls to an existing project, you&#039;ll need to configure your environment to properly link the Phidget Ruby libraries.&lt;br /&gt;
&lt;br /&gt;
Simply, add the following two lines to the beginning of any {{Code|.rb}} script to make use of the phidgets-ffi gem&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  require &#039;rubygems&#039;&lt;br /&gt;
  require &#039;phidgets-ffi&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The project now has access to the Phidget21 function calls and you are ready to begin coding.&lt;br /&gt;
&lt;br /&gt;
The same [[#Follow the Examples|teaching ]] section which describes the examples also has further resources for programming your Phidget.&lt;br /&gt;
&lt;br /&gt;
==Follow the Examples==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
Next, comes our API information.  These resources outline the Ruby Phidget functions:&lt;br /&gt;
&lt;br /&gt;
{{UsingAPhidgetInCodeGeneral|both of which are available in Ruby|[http://www.phidgets.com/documentation/rubydoc Ruby API]}}&lt;br /&gt;
&lt;br /&gt;
===Example Flow===&lt;br /&gt;
&lt;br /&gt;
{{ExamplePseudocode|In Ruby, you can name these &#039;&#039;&#039;event&#039;&#039;&#039; 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. &amp;lt;br&amp;gt; &lt;br /&gt;
In the example code, the event functions common to all Phidgets are called things like &#039;&#039;&#039;AttachHandler()&#039;&#039;&#039; and &#039;&#039;&#039;DetachHandler()&#039;&#039;&#039;, etc.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
Other functions are given in the examples to show you more detail on using your Phidget.  For example, &#039;&#039;&#039;DeviceInitialize()&#039;&#039;&#039; will show what needs to be set up for your Phidget before using it.&lt;br /&gt;
|Creating a Phidget software object in Ruby is specific to the Phidget.  For a Phidget Spatial, for example, this would involve creating a {{Code|Spatial}} object.  The examples show how to do this and other API functions.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The object provides device specific methods and properties which are available from the API for your specific Phidget.|&lt;br /&gt;
[http://www.phidgets.com/documentation/rubydoc Ruby API]}}&lt;br /&gt;
&lt;br /&gt;
==Code Snippets==&lt;br /&gt;
&lt;br /&gt;
==Common Problems and Solutions/Workarounds==&lt;br /&gt;
&lt;br /&gt;
Here you can put various frequent problems and our recommended solutions.&lt;/div&gt;</summary>
		<author><name>Mphi</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs21/index.php?title=Language_-_Ruby&amp;diff=19104</id>
		<title>Language - Ruby</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs21/index.php?title=Language_-_Ruby&amp;diff=19104"/>
		<updated>2012-04-19T20:45:36Z</updated>

		<summary type="html">&lt;p&gt;Mphi: /* Linux */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Language]]&lt;br /&gt;
[[File:Icon-Ruby.png|64x64px||link=|alt=]]Ruby is an interpreted and object oriented scripting language with simple syntax created by Yukihiro Matsumoto.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
{{LanguageSupport|Ruby|the complete Phidget API, including events|all Phidget devices.|Linux and OS X|}}&lt;br /&gt;
&lt;br /&gt;
==Quick Downloads==&lt;br /&gt;
&lt;br /&gt;
Just need the Ruby documentation, drivers, libraries, and examples?  Here they are:&lt;br /&gt;
&lt;br /&gt;
===API Documentation===&lt;br /&gt;
*[http://www.phidgets.com/documentation/rubydoc API Manual]&lt;br /&gt;
&lt;br /&gt;
===Library and Example Code===&lt;br /&gt;
&lt;br /&gt;
*[http://www.phidgets.com/downloads/examples/rubylib_2.1.8.0.html phidgets-ffi @ RubyGems]&lt;br /&gt;
&lt;br /&gt;
===Drivers===&lt;br /&gt;
*[http://www.phidgets.com/downloads/libraries/libphidget_2.1.8.20111028.tar.gz Linux Source] &lt;br /&gt;
*[http://www.phidgets.com/downloads/libraries/Phidget_2.1.8.20111103.dmg OS X Drivers Installer]&lt;br /&gt;
&lt;br /&gt;
==Getting started with Ruby==&lt;br /&gt;
&lt;br /&gt;
If you are new to writing code for Phidgets, we recommend starting by running, then modifying existing examples. This will allow you to:&lt;br /&gt;
{{ExampleCodeReasons}}&lt;br /&gt;
&lt;br /&gt;
Instructions for OS X and Linux are similar, so they will be combined into the same section.&lt;br /&gt;
&lt;br /&gt;
The first step in using Ruby on Mac or Linux is to install the Phidget libraries.  Compile and install them as explained on the [[Device List|getting started guide for your device]].&lt;br /&gt;
&lt;br /&gt;
The next step is to install the phidgets-ffi gem. This is the library that allows you to program Phidgets with Ruby. For more information, please see phidgets-ffi at [http://rubygems.org/gems/phidgets-ffi RubyGems] and [https://github.com/kreynolds/phidgets-ffi GitHub].&lt;br /&gt;
&lt;br /&gt;
==Description of Library Files==&lt;br /&gt;
Ruby programs on OS X and Linux depend on the following files, which the installers put into your system. &lt;br /&gt;
&lt;br /&gt;
If you are using OS X, you will need the:&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|Phidget21.framework}}&amp;lt;/b&amp;gt; contains the actual Phidgets library for OS X, which is used at run-time.  &lt;br /&gt;
&lt;br /&gt;
If you are using Linux, you will need the:&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|libphidget21.so}}&amp;lt;/b&amp;gt; contains the actual Phidgets library for Linux, which is used at run-time.&lt;br /&gt;
&lt;br /&gt;
For both OS X and Linux, you will need the phidgets-ffi gem. &lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|phidgets-ffi}}&amp;lt;/b&amp;gt; is the Phidgets gem for Ruby. This gem contains the library as well as example code.&lt;br /&gt;
&lt;br /&gt;
==Installing phidgets-ffi==&lt;br /&gt;
&lt;br /&gt;
The phidgets-ffi gem relies on the [http://rubygems.org/gems/ffi ffi gem]. So, if you do not already have it installed, please do so. A version of ffi 1.0.9 or greater is needed. Type the following to install ffi:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  gem install ffi&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Next, please install the phidgets-ffi gem by typing the following into command line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  gem install phidgets-ffi&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For OS X systems, the gem will be installed into {{Code|/Library/Ruby/Gems/1.x/phidgets-ffi-x.x.x}}.&lt;br /&gt;
&lt;br /&gt;
For typical Linux systems, the gem will be installed into {{Code|var/lib/gems/1.9/gems/phidgets-ffi-x.x.x}}.&lt;br /&gt;
&lt;br /&gt;
===Use Our Examples===&lt;br /&gt;
&lt;br /&gt;
Open a command line terminal and navigate to the phidgets-ffi gem directory. {{Code|cd}} into the {{Code|examples}} folder. Here, you will find all of the examples available for Ruby.  If you aren&#039;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]].&lt;br /&gt;
&lt;br /&gt;
The easiest way to confirm that your environment is set up properly will be to compile and run the {{Code|HelloWorld}} Ruby example.&lt;br /&gt;
&lt;br /&gt;
The only thing left to do is to run the examples! Type the following into command line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  ruby HelloWorld.rb&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This program will detect for devices that are attached/detached on the computer. Go ahead, and attach or detach your devices! Here is an example output:&lt;br /&gt;
&lt;br /&gt;
[[File:Ruby Linux HelloWorld Output.PNG|link=|alt=HelloWorld Output]]&lt;br /&gt;
&lt;br /&gt;
After confirming that the {{Code|HelloWorld}} example is working, you can proceed to run the example for your device. If you aren&#039;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 ensure that you have set your start up project to be the one that matches your device before compiling. &lt;br /&gt;
&lt;br /&gt;
Once you have the Ruby examples running, we have a [[#Follow the Examples|teaching]] section below to help you follow them.&lt;br /&gt;
&lt;br /&gt;
==Write Your Own Code==&lt;br /&gt;
&lt;br /&gt;
When you are building a project from scratch, or adding Phidget function calls to an existing project, you&#039;ll need to configure your environment to properly link the Phidget Ruby libraries.&lt;br /&gt;
&lt;br /&gt;
Simply, add the following two lines to the beginning of any {{Code|.rb}} script to make use of the phidgets-ffi gem&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  require &#039;rubygems&#039;&lt;br /&gt;
  require &#039;phidgets-ffi&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The project now has access to the Phidget21 function calls and you are ready to begin coding.&lt;br /&gt;
&lt;br /&gt;
The same [[#Follow the Examples|teaching ]] section which describes the examples also has further resources for programming your Phidget.&lt;br /&gt;
&lt;br /&gt;
==Follow the Examples==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
Next, comes our API information.  These resources outline the Ruby Phidget functions:&lt;br /&gt;
&lt;br /&gt;
{{UsingAPhidgetInCodeGeneral|both of which are available in Ruby|[http://www.phidgets.com/documentation/rubydoc Ruby API]}}&lt;br /&gt;
&lt;br /&gt;
===Example Flow===&lt;br /&gt;
&lt;br /&gt;
{{ExamplePseudocode|In Ruby, you can name these &#039;&#039;&#039;event&#039;&#039;&#039; 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. &amp;lt;br&amp;gt; &lt;br /&gt;
In the example code, the event functions common to all Phidgets are called things like &#039;&#039;&#039;AttachHandler()&#039;&#039;&#039; and &#039;&#039;&#039;DetachHandler()&#039;&#039;&#039;, etc.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
Other functions are given in the examples to show you more detail on using your Phidget.  For example, &#039;&#039;&#039;DeviceInitialize()&#039;&#039;&#039; will show what needs to be set up for your Phidget before using it.&lt;br /&gt;
|Creating a Phidget software object in Ruby is specific to the Phidget.  For a Phidget Spatial, for example, this would involve creating a {{Code|Spatial}} object.  The examples show how to do this and other API functions.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The object provides device specific methods and properties which are available from the API for your specific Phidget.|&lt;br /&gt;
[http://www.phidgets.com/documentation/rubydoc Ruby API]}}&lt;br /&gt;
&lt;br /&gt;
==Code Snippets==&lt;br /&gt;
&lt;br /&gt;
==Common Problems and Solutions/Workarounds==&lt;br /&gt;
&lt;br /&gt;
Here you can put various frequent problems and our recommended solutions.&lt;/div&gt;</summary>
		<author><name>Mphi</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs21/index.php?title=Language_-_Ruby&amp;diff=19103</id>
		<title>Language - Ruby</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs21/index.php?title=Language_-_Ruby&amp;diff=19103"/>
		<updated>2012-04-19T20:45:27Z</updated>

		<summary type="html">&lt;p&gt;Mphi: /* Write Your Own Code */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Language]]&lt;br /&gt;
[[File:Icon-Ruby.png|64x64px||link=|alt=]]Ruby is an interpreted and object oriented scripting language with simple syntax created by Yukihiro Matsumoto.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
{{LanguageSupport|Ruby|the complete Phidget API, including events|all Phidget devices.|Linux and OS X|}}&lt;br /&gt;
&lt;br /&gt;
==Quick Downloads==&lt;br /&gt;
&lt;br /&gt;
Just need the Ruby documentation, drivers, libraries, and examples?  Here they are:&lt;br /&gt;
&lt;br /&gt;
===API Documentation===&lt;br /&gt;
*[http://www.phidgets.com/documentation/rubydoc API Manual]&lt;br /&gt;
&lt;br /&gt;
===Library and Example Code===&lt;br /&gt;
&lt;br /&gt;
*[http://www.phidgets.com/downloads/examples/rubylib_2.1.8.0.html phidgets-ffi @ RubyGems]&lt;br /&gt;
&lt;br /&gt;
===Drivers===&lt;br /&gt;
*[http://www.phidgets.com/downloads/libraries/libphidget_2.1.8.20111028.tar.gz Linux Source] &lt;br /&gt;
*[http://www.phidgets.com/downloads/libraries/Phidget_2.1.8.20111103.dmg OS X Drivers Installer]&lt;br /&gt;
&lt;br /&gt;
==Getting started with Ruby==&lt;br /&gt;
&lt;br /&gt;
If you are new to writing code for Phidgets, we recommend starting by running, then modifying existing examples. This will allow you to:&lt;br /&gt;
{{ExampleCodeReasons}}&lt;br /&gt;
&lt;br /&gt;
Instructions for OS X and Linux are similar, so they will be combined into the same section.&lt;br /&gt;
&lt;br /&gt;
The first step in using Ruby on Mac or Linux is to install the Phidget libraries.  Compile and install them as explained on the [[Device List|getting started guide for your device]].&lt;br /&gt;
&lt;br /&gt;
The next step is to install the phidgets-ffi gem. This is the library that allows you to program Phidgets with Ruby. For more information, please see phidgets-ffi at [http://rubygems.org/gems/phidgets-ffi RubyGems] and [https://github.com/kreynolds/phidgets-ffi GitHub].&lt;br /&gt;
&lt;br /&gt;
==Description of Library Files==&lt;br /&gt;
Ruby programs on OS X and Linux depend on the following files, which the installers put into your system. &lt;br /&gt;
&lt;br /&gt;
If you are using OS X, you will need the:&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|Phidget21.framework}}&amp;lt;/b&amp;gt; contains the actual Phidgets library for OS X, which is used at run-time.  &lt;br /&gt;
&lt;br /&gt;
If you are using Linux, you will need the:&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|libphidget21.so}}&amp;lt;/b&amp;gt; contains the actual Phidgets library for Linux, which is used at run-time.&lt;br /&gt;
&lt;br /&gt;
For both OS X and Linux, you will need the phidgets-ffi gem. &lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|phidgets-ffi}}&amp;lt;/b&amp;gt; is the Phidgets gem for Ruby. This gem contains the library as well as example code.&lt;br /&gt;
&lt;br /&gt;
==Installing phidgets-ffi==&lt;br /&gt;
&lt;br /&gt;
The phidgets-ffi gem relies on the [http://rubygems.org/gems/ffi ffi gem]. So, if you do not already have it installed, please do so. A version of ffi 1.0.9 or greater is needed. Type the following to install ffi:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  gem install ffi&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Next, please install the phidgets-ffi gem by typing the following into command line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  gem install phidgets-ffi&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For OS X systems, the gem will be installed into {{Code|/Library/Ruby/Gems/1.x/phidgets-ffi-x.x.x}}.&lt;br /&gt;
&lt;br /&gt;
For typical Linux systems, the gem will be installed into {{Code|var/lib/gems/1.9/gems/phidgets-ffi-x.x.x}}.&lt;br /&gt;
&lt;br /&gt;
===Use Our Examples===&lt;br /&gt;
&lt;br /&gt;
Open a command line terminal and navigate to the phidgets-ffi gem directory. {{Code|cd}} into the {{Code|examples}} folder. Here, you will find all of the examples available for Ruby.  If you aren&#039;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]].&lt;br /&gt;
&lt;br /&gt;
The easiest way to confirm that your environment is set up properly will be to compile and run the {{Code|HelloWorld}} Ruby example.&lt;br /&gt;
&lt;br /&gt;
The only thing left to do is to run the examples! Type the following into command line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  ruby HelloWorld.rb&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This program will detect for devices that are attached/detached on the computer. Go ahead, and attach or detach your devices! Here is an example output:&lt;br /&gt;
&lt;br /&gt;
[[File:Ruby Linux HelloWorld Output.PNG|link=|alt=HelloWorld Output]]&lt;br /&gt;
&lt;br /&gt;
After confirming that the {{Code|HelloWorld}} example is working, you can proceed to run the example for your device. If you aren&#039;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 ensure that you have set your start up project to be the one that matches your device before compiling. &lt;br /&gt;
&lt;br /&gt;
Once you have the Ruby examples running, we have a [[#Follow the Examples|teaching]] section below to help you follow them.&lt;br /&gt;
&lt;br /&gt;
==Write Your Own Code==&lt;br /&gt;
&lt;br /&gt;
When you are building a project from scratch, or adding Phidget function calls to an existing project, you&#039;ll need to configure your environment to properly link the Phidget Ruby libraries.&lt;br /&gt;
&lt;br /&gt;
Simply, add the following two lines to the beginning of any {{Code|.rb}} script to make use of the phidgets-ffi gem&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=ruby&amp;gt;&lt;br /&gt;
  require &#039;rubygems&#039;&lt;br /&gt;
  require &#039;phidgets-ffi&#039;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The project now has access to the Phidget21 function calls and you are ready to begin coding.&lt;br /&gt;
&lt;br /&gt;
The same [[#Follow the Examples|teaching ]] section which describes the examples also has further resources for programming your Phidget.&lt;br /&gt;
&lt;br /&gt;
==Linux==&lt;br /&gt;
&lt;br /&gt;
C# has support on Linux through the Mono framework.  &lt;br /&gt;
&lt;br /&gt;
The first step in using Ruby 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.&lt;br /&gt;
&lt;br /&gt;
==Follow the Examples==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
Next, comes our API information.  These resources outline the Ruby Phidget functions:&lt;br /&gt;
&lt;br /&gt;
{{UsingAPhidgetInCodeGeneral|both of which are available in Ruby|[http://www.phidgets.com/documentation/rubydoc Ruby API]}}&lt;br /&gt;
&lt;br /&gt;
===Example Flow===&lt;br /&gt;
&lt;br /&gt;
{{ExamplePseudocode|In Ruby, you can name these &#039;&#039;&#039;event&#039;&#039;&#039; 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. &amp;lt;br&amp;gt; &lt;br /&gt;
In the example code, the event functions common to all Phidgets are called things like &#039;&#039;&#039;AttachHandler()&#039;&#039;&#039; and &#039;&#039;&#039;DetachHandler()&#039;&#039;&#039;, etc.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
Other functions are given in the examples to show you more detail on using your Phidget.  For example, &#039;&#039;&#039;DeviceInitialize()&#039;&#039;&#039; will show what needs to be set up for your Phidget before using it.&lt;br /&gt;
|Creating a Phidget software object in Ruby is specific to the Phidget.  For a Phidget Spatial, for example, this would involve creating a {{Code|Spatial}} object.  The examples show how to do this and other API functions.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The object provides device specific methods and properties which are available from the API for your specific Phidget.|&lt;br /&gt;
[http://www.phidgets.com/documentation/rubydoc Ruby API]}}&lt;br /&gt;
&lt;br /&gt;
==Code Snippets==&lt;br /&gt;
&lt;br /&gt;
==Common Problems and Solutions/Workarounds==&lt;br /&gt;
&lt;br /&gt;
Here you can put various frequent problems and our recommended solutions.&lt;/div&gt;</summary>
		<author><name>Mphi</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs21/index.php?title=Language_-_Ruby&amp;diff=19102</id>
		<title>Language - Ruby</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs21/index.php?title=Language_-_Ruby&amp;diff=19102"/>
		<updated>2012-04-19T20:38:37Z</updated>

		<summary type="html">&lt;p&gt;Mphi: /* Getting started with Ruby */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Language]]&lt;br /&gt;
[[File:Icon-Ruby.png|64x64px||link=|alt=]]Ruby is an interpreted and object oriented scripting language with simple syntax created by Yukihiro Matsumoto.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
{{LanguageSupport|Ruby|the complete Phidget API, including events|all Phidget devices.|Linux and OS X|}}&lt;br /&gt;
&lt;br /&gt;
==Quick Downloads==&lt;br /&gt;
&lt;br /&gt;
Just need the Ruby documentation, drivers, libraries, and examples?  Here they are:&lt;br /&gt;
&lt;br /&gt;
===API Documentation===&lt;br /&gt;
*[http://www.phidgets.com/documentation/rubydoc API Manual]&lt;br /&gt;
&lt;br /&gt;
===Library and Example Code===&lt;br /&gt;
&lt;br /&gt;
*[http://www.phidgets.com/downloads/examples/rubylib_2.1.8.0.html phidgets-ffi @ RubyGems]&lt;br /&gt;
&lt;br /&gt;
===Drivers===&lt;br /&gt;
*[http://www.phidgets.com/downloads/libraries/libphidget_2.1.8.20111028.tar.gz Linux Source] &lt;br /&gt;
*[http://www.phidgets.com/downloads/libraries/Phidget_2.1.8.20111103.dmg OS X Drivers Installer]&lt;br /&gt;
&lt;br /&gt;
==Getting started with Ruby==&lt;br /&gt;
&lt;br /&gt;
If you are new to writing code for Phidgets, we recommend starting by running, then modifying existing examples. This will allow you to:&lt;br /&gt;
{{ExampleCodeReasons}}&lt;br /&gt;
&lt;br /&gt;
Instructions for OS X and Linux are similar, so they will be combined into the same section.&lt;br /&gt;
&lt;br /&gt;
The first step in using Ruby on Mac or Linux is to install the Phidget libraries.  Compile and install them as explained on the [[Device List|getting started guide for your device]].&lt;br /&gt;
&lt;br /&gt;
The next step is to install the phidgets-ffi gem. This is the library that allows you to program Phidgets with Ruby. For more information, please see phidgets-ffi at [http://rubygems.org/gems/phidgets-ffi RubyGems] and [https://github.com/kreynolds/phidgets-ffi GitHub].&lt;br /&gt;
&lt;br /&gt;
==Description of Library Files==&lt;br /&gt;
Ruby programs on OS X and Linux depend on the following files, which the installers put into your system. &lt;br /&gt;
&lt;br /&gt;
If you are using OS X, you will need the:&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|Phidget21.framework}}&amp;lt;/b&amp;gt; contains the actual Phidgets library for OS X, which is used at run-time.  &lt;br /&gt;
&lt;br /&gt;
If you are using Linux, you will need the:&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|libphidget21.so}}&amp;lt;/b&amp;gt; contains the actual Phidgets library for Linux, which is used at run-time.&lt;br /&gt;
&lt;br /&gt;
For both OS X and Linux, you will need the phidgets-ffi gem. &lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|phidgets-ffi}}&amp;lt;/b&amp;gt; is the Phidgets gem for Ruby. This gem contains the library as well as example code.&lt;br /&gt;
&lt;br /&gt;
==Installing phidgets-ffi==&lt;br /&gt;
&lt;br /&gt;
The phidgets-ffi gem relies on the [http://rubygems.org/gems/ffi ffi gem]. So, if you do not already have it installed, please do so. A version of ffi 1.0.9 or greater is needed. Type the following to install ffi:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  gem install ffi&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Next, please install the phidgets-ffi gem by typing the following into command line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  gem install phidgets-ffi&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For OS X systems, the gem will be installed into {{Code|/Library/Ruby/Gems/1.x/phidgets-ffi-x.x.x}}.&lt;br /&gt;
&lt;br /&gt;
For typical Linux systems, the gem will be installed into {{Code|var/lib/gems/1.9/gems/phidgets-ffi-x.x.x}}.&lt;br /&gt;
&lt;br /&gt;
===Use Our Examples===&lt;br /&gt;
&lt;br /&gt;
Open a command line terminal and navigate to the phidgets-ffi gem directory. {{Code|cd}} into the {{Code|examples}} folder. Here, you will find all of the examples available for Ruby.  If you aren&#039;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]].&lt;br /&gt;
&lt;br /&gt;
The easiest way to confirm that your environment is set up properly will be to compile and run the {{Code|HelloWorld}} Ruby example.&lt;br /&gt;
&lt;br /&gt;
The only thing left to do is to run the examples! Type the following into command line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  ruby HelloWorld.rb&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This program will detect for devices that are attached/detached on the computer. Go ahead, and attach or detach your devices! Here is an example output:&lt;br /&gt;
&lt;br /&gt;
[[File:Ruby Linux HelloWorld Output.PNG|link=|alt=HelloWorld Output]]&lt;br /&gt;
&lt;br /&gt;
After confirming that the {{Code|HelloWorld}} example is working, you can proceed to run the example for your device. If you aren&#039;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 ensure that you have set your start up project to be the one that matches your device before compiling. &lt;br /&gt;
&lt;br /&gt;
Once you have the Ruby examples running, we have a [[#Follow the Examples|teaching]] section below to help you follow them.&lt;br /&gt;
&lt;br /&gt;
==Write Your Own Code==&lt;br /&gt;
&lt;br /&gt;
When you are building a project from scratch, or adding Phidget function calls to an existing project, you&#039;ll need to configure your environment to properly link the Phidget Ruby libraries. To begin: &lt;br /&gt;
&lt;br /&gt;
1. Generate a new  Ruby Windows Applications project with a descriptive name such as PhidgetTest. &lt;br /&gt;
&lt;br /&gt;
[[File:CSharp VS2005 New Project.PNG|link=|alt=New Project]]&lt;br /&gt;
&lt;br /&gt;
2. Add a reference to the .NET Phidgets library.&lt;br /&gt;
&lt;br /&gt;
[[File:CSharp VS2005 Add Reference.PNG|link=|alt=Add Reference]]&lt;br /&gt;
&lt;br /&gt;
3. Under the .NET tab, select {{Code|Phidget21.NET.dll}}.&lt;br /&gt;
If you used our installer, these files are installed in {{Code|C:\Program Files\Phidgets}}, by default. If it does not appear in this list, then you can browse to the Phidget Framework installation directory and add the file.&lt;br /&gt;
&lt;br /&gt;
[[File:CSharp VS2005 Add Reference 2.PNG|link=|alt=Add Reference]]&lt;br /&gt;
&lt;br /&gt;
4. Then, in your code, you will need to include the Phidget library:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=csharp&amp;gt;&lt;br /&gt;
  using Phidgets;&lt;br /&gt;
  using Phidgets.Events;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The project now has access to the Phidget21 function calls and you are ready to begin coding.&lt;br /&gt;
&lt;br /&gt;
The same [[#Follow the Examples|teaching ]] section which describes the examples also has further resources for programming your Phidget.&lt;br /&gt;
&lt;br /&gt;
==Linux==&lt;br /&gt;
&lt;br /&gt;
C# has support on Linux through the Mono framework.  &lt;br /&gt;
&lt;br /&gt;
The first step in using Ruby 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.&lt;br /&gt;
&lt;br /&gt;
==Follow the Examples==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
Next, comes our API information.  These resources outline the Ruby Phidget functions:&lt;br /&gt;
&lt;br /&gt;
{{UsingAPhidgetInCodeGeneral|both of which are available in Ruby|[http://www.phidgets.com/documentation/rubydoc Ruby API]}}&lt;br /&gt;
&lt;br /&gt;
===Example Flow===&lt;br /&gt;
&lt;br /&gt;
{{ExamplePseudocode|In Ruby, you can name these &#039;&#039;&#039;event&#039;&#039;&#039; 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. &amp;lt;br&amp;gt; &lt;br /&gt;
In the example code, the event functions common to all Phidgets are called things like &#039;&#039;&#039;AttachHandler()&#039;&#039;&#039; and &#039;&#039;&#039;DetachHandler()&#039;&#039;&#039;, etc.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
Other functions are given in the examples to show you more detail on using your Phidget.  For example, &#039;&#039;&#039;DeviceInitialize()&#039;&#039;&#039; will show what needs to be set up for your Phidget before using it.&lt;br /&gt;
|Creating a Phidget software object in Ruby is specific to the Phidget.  For a Phidget Spatial, for example, this would involve creating a {{Code|Spatial}} object.  The examples show how to do this and other API functions.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The object provides device specific methods and properties which are available from the API for your specific Phidget.|&lt;br /&gt;
[http://www.phidgets.com/documentation/rubydoc Ruby API]}}&lt;br /&gt;
&lt;br /&gt;
==Code Snippets==&lt;br /&gt;
&lt;br /&gt;
==Common Problems and Solutions/Workarounds==&lt;br /&gt;
&lt;br /&gt;
Here you can put various frequent problems and our recommended solutions.&lt;/div&gt;</summary>
		<author><name>Mphi</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs21/index.php?title=Language_-_Ruby&amp;diff=19101</id>
		<title>Language - Ruby</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs21/index.php?title=Language_-_Ruby&amp;diff=19101"/>
		<updated>2012-04-19T20:36:14Z</updated>

		<summary type="html">&lt;p&gt;Mphi: /* Installing phidgets-ffi */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Language]]&lt;br /&gt;
[[File:Icon-Ruby.png|64x64px||link=|alt=]]Ruby is an interpreted and object oriented scripting language with simple syntax created by Yukihiro Matsumoto.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
{{LanguageSupport|Ruby|the complete Phidget API, including events|all Phidget devices.|Linux and OS X|}}&lt;br /&gt;
&lt;br /&gt;
==Quick Downloads==&lt;br /&gt;
&lt;br /&gt;
Just need the Ruby documentation, drivers, libraries, and examples?  Here they are:&lt;br /&gt;
&lt;br /&gt;
===API Documentation===&lt;br /&gt;
*[http://www.phidgets.com/documentation/rubydoc API Manual]&lt;br /&gt;
&lt;br /&gt;
===Library and Example Code===&lt;br /&gt;
&lt;br /&gt;
*[http://www.phidgets.com/downloads/examples/rubylib_2.1.8.0.html phidgets-ffi @ RubyGems]&lt;br /&gt;
&lt;br /&gt;
===Drivers===&lt;br /&gt;
*[http://www.phidgets.com/downloads/libraries/libphidget_2.1.8.20111028.tar.gz Linux Source] &lt;br /&gt;
*[http://www.phidgets.com/downloads/libraries/Phidget_2.1.8.20111103.dmg OS X Drivers Installer]&lt;br /&gt;
&lt;br /&gt;
==Getting started with Ruby==&lt;br /&gt;
&lt;br /&gt;
If you are new to writing code for Phidgets, we recommend starting by running, then modifying existing examples. This will allow you to:&lt;br /&gt;
{{ExampleCodeReasons}}&lt;br /&gt;
&lt;br /&gt;
Instructions for OS X and Linux are similar, so they will be combined into the same section.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The first step in using Ruby on Mac or Linux is to install the Phidget libraries.  Compile and install them as explained on the [[Device List|getting started guide for your device]].  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Description of Library Files==&lt;br /&gt;
Ruby programs on OS X and Linux depend on the following files, which the installers put into your system. &lt;br /&gt;
&lt;br /&gt;
If you are using OS X, you will need the:&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|Phidget21.framework}}&amp;lt;/b&amp;gt; contains the actual Phidgets library for OS X, which is used at run-time.  &lt;br /&gt;
&lt;br /&gt;
If you are using Linux, you will need the:&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|libphidget21.so}}&amp;lt;/b&amp;gt; contains the actual Phidgets library for Linux, which is used at run-time.&lt;br /&gt;
&lt;br /&gt;
For both OS X and Linux, you will need the phidgets-ffi gem. &lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|phidgets-ffi}}&amp;lt;/b&amp;gt; is the Phidgets gem for Ruby. This gem contains the library as well as example code.&lt;br /&gt;
&lt;br /&gt;
==Installing phidgets-ffi==&lt;br /&gt;
&lt;br /&gt;
The phidgets-ffi gem relies on the [http://rubygems.org/gems/ffi ffi gem]. So, if you do not already have it installed, please do so. A version of ffi 1.0.9 or greater is needed. Type the following to install ffi:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  gem install ffi&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Next, please install the phidgets-ffi gem by typing the following into command line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  gem install phidgets-ffi&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For OS X systems, the gem will be installed into {{Code|/Library/Ruby/Gems/1.x/phidgets-ffi-x.x.x}}.&lt;br /&gt;
&lt;br /&gt;
For typical Linux systems, the gem will be installed into {{Code|var/lib/gems/1.9/gems/phidgets-ffi-x.x.x}}.&lt;br /&gt;
&lt;br /&gt;
===Use Our Examples===&lt;br /&gt;
&lt;br /&gt;
Open a command line terminal and navigate to the phidgets-ffi gem directory. {{Code|cd}} into the {{Code|examples}} folder. Here, you will find all of the examples available for Ruby.  If you aren&#039;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]].&lt;br /&gt;
&lt;br /&gt;
The easiest way to confirm that your environment is set up properly will be to compile and run the {{Code|HelloWorld}} Ruby example.&lt;br /&gt;
&lt;br /&gt;
The only thing left to do is to run the examples! Type the following into command line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  ruby HelloWorld.rb&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This program will detect for devices that are attached/detached on the computer. Go ahead, and attach or detach your devices! Here is an example output:&lt;br /&gt;
&lt;br /&gt;
[[File:Ruby Linux HelloWorld Output.PNG|link=|alt=HelloWorld Output]]&lt;br /&gt;
&lt;br /&gt;
After confirming that the {{Code|HelloWorld}} example is working, you can proceed to run the example for your device. If you aren&#039;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 ensure that you have set your start up project to be the one that matches your device before compiling. &lt;br /&gt;
&lt;br /&gt;
Once you have the Ruby examples running, we have a [[#Follow the Examples|teaching]] section below to help you follow them.&lt;br /&gt;
&lt;br /&gt;
==Write Your Own Code==&lt;br /&gt;
&lt;br /&gt;
When you are building a project from scratch, or adding Phidget function calls to an existing project, you&#039;ll need to configure your environment to properly link the Phidget Ruby libraries. To begin: &lt;br /&gt;
&lt;br /&gt;
1. Generate a new  Ruby Windows Applications project with a descriptive name such as PhidgetTest. &lt;br /&gt;
&lt;br /&gt;
[[File:CSharp VS2005 New Project.PNG|link=|alt=New Project]]&lt;br /&gt;
&lt;br /&gt;
2. Add a reference to the .NET Phidgets library.&lt;br /&gt;
&lt;br /&gt;
[[File:CSharp VS2005 Add Reference.PNG|link=|alt=Add Reference]]&lt;br /&gt;
&lt;br /&gt;
3. Under the .NET tab, select {{Code|Phidget21.NET.dll}}.&lt;br /&gt;
If you used our installer, these files are installed in {{Code|C:\Program Files\Phidgets}}, by default. If it does not appear in this list, then you can browse to the Phidget Framework installation directory and add the file.&lt;br /&gt;
&lt;br /&gt;
[[File:CSharp VS2005 Add Reference 2.PNG|link=|alt=Add Reference]]&lt;br /&gt;
&lt;br /&gt;
4. Then, in your code, you will need to include the Phidget library:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=csharp&amp;gt;&lt;br /&gt;
  using Phidgets;&lt;br /&gt;
  using Phidgets.Events;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The project now has access to the Phidget21 function calls and you are ready to begin coding.&lt;br /&gt;
&lt;br /&gt;
The same [[#Follow the Examples|teaching ]] section which describes the examples also has further resources for programming your Phidget.&lt;br /&gt;
&lt;br /&gt;
==Linux==&lt;br /&gt;
&lt;br /&gt;
C# has support on Linux through the Mono framework.  &lt;br /&gt;
&lt;br /&gt;
The first step in using Ruby 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.&lt;br /&gt;
&lt;br /&gt;
==Follow the Examples==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
Next, comes our API information.  These resources outline the Ruby Phidget functions:&lt;br /&gt;
&lt;br /&gt;
{{UsingAPhidgetInCodeGeneral|both of which are available in Ruby|[http://www.phidgets.com/documentation/rubydoc Ruby API]}}&lt;br /&gt;
&lt;br /&gt;
===Example Flow===&lt;br /&gt;
&lt;br /&gt;
{{ExamplePseudocode|In Ruby, you can name these &#039;&#039;&#039;event&#039;&#039;&#039; 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. &amp;lt;br&amp;gt; &lt;br /&gt;
In the example code, the event functions common to all Phidgets are called things like &#039;&#039;&#039;AttachHandler()&#039;&#039;&#039; and &#039;&#039;&#039;DetachHandler()&#039;&#039;&#039;, etc.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
Other functions are given in the examples to show you more detail on using your Phidget.  For example, &#039;&#039;&#039;DeviceInitialize()&#039;&#039;&#039; will show what needs to be set up for your Phidget before using it.&lt;br /&gt;
|Creating a Phidget software object in Ruby is specific to the Phidget.  For a Phidget Spatial, for example, this would involve creating a {{Code|Spatial}} object.  The examples show how to do this and other API functions.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The object provides device specific methods and properties which are available from the API for your specific Phidget.|&lt;br /&gt;
[http://www.phidgets.com/documentation/rubydoc Ruby API]}}&lt;br /&gt;
&lt;br /&gt;
==Code Snippets==&lt;br /&gt;
&lt;br /&gt;
==Common Problems and Solutions/Workarounds==&lt;br /&gt;
&lt;br /&gt;
Here you can put various frequent problems and our recommended solutions.&lt;/div&gt;</summary>
		<author><name>Mphi</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs21/index.php?title=Language_-_Ruby&amp;diff=19100</id>
		<title>Language - Ruby</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs21/index.php?title=Language_-_Ruby&amp;diff=19100"/>
		<updated>2012-04-19T20:35:35Z</updated>

		<summary type="html">&lt;p&gt;Mphi: /* Installing phidgets-ffi */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Language]]&lt;br /&gt;
[[File:Icon-Ruby.png|64x64px||link=|alt=]]Ruby is an interpreted and object oriented scripting language with simple syntax created by Yukihiro Matsumoto.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
{{LanguageSupport|Ruby|the complete Phidget API, including events|all Phidget devices.|Linux and OS X|}}&lt;br /&gt;
&lt;br /&gt;
==Quick Downloads==&lt;br /&gt;
&lt;br /&gt;
Just need the Ruby documentation, drivers, libraries, and examples?  Here they are:&lt;br /&gt;
&lt;br /&gt;
===API Documentation===&lt;br /&gt;
*[http://www.phidgets.com/documentation/rubydoc API Manual]&lt;br /&gt;
&lt;br /&gt;
===Library and Example Code===&lt;br /&gt;
&lt;br /&gt;
*[http://www.phidgets.com/downloads/examples/rubylib_2.1.8.0.html phidgets-ffi @ RubyGems]&lt;br /&gt;
&lt;br /&gt;
===Drivers===&lt;br /&gt;
*[http://www.phidgets.com/downloads/libraries/libphidget_2.1.8.20111028.tar.gz Linux Source] &lt;br /&gt;
*[http://www.phidgets.com/downloads/libraries/Phidget_2.1.8.20111103.dmg OS X Drivers Installer]&lt;br /&gt;
&lt;br /&gt;
==Getting started with Ruby==&lt;br /&gt;
&lt;br /&gt;
If you are new to writing code for Phidgets, we recommend starting by running, then modifying existing examples. This will allow you to:&lt;br /&gt;
{{ExampleCodeReasons}}&lt;br /&gt;
&lt;br /&gt;
Instructions for OS X and Linux are similar, so they will be combined into the same section.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The first step in using Ruby on Mac or Linux is to install the Phidget libraries.  Compile and install them as explained on the [[Device List|getting started guide for your device]].  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Description of Library Files==&lt;br /&gt;
Ruby programs on OS X and Linux depend on the following files, which the installers put into your system. &lt;br /&gt;
&lt;br /&gt;
If you are using OS X, you will need the:&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|Phidget21.framework}}&amp;lt;/b&amp;gt; contains the actual Phidgets library for OS X, which is used at run-time.  &lt;br /&gt;
&lt;br /&gt;
If you are using Linux, you will need the:&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|libphidget21.so}}&amp;lt;/b&amp;gt; contains the actual Phidgets library for Linux, which is used at run-time.&lt;br /&gt;
&lt;br /&gt;
For both OS X and Linux, you will need the phidgets-ffi gem. &lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|phidgets-ffi}}&amp;lt;/b&amp;gt; is the Phidgets gem for Ruby. This gem contains the library as well as example code.&lt;br /&gt;
&lt;br /&gt;
==Installing phidgets-ffi==&lt;br /&gt;
&lt;br /&gt;
The phidgets-ffi gem relies on the ffi gem. So, if you do not already have it installed, please do so. A version of ffi 1.0.9 or greater is needed. Type the following to install ffi:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  gem install ffi&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Next, please install the phidgets-ffi gem by typing the following into command line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  gem install phidgets-ffi&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For OS X systems, the gem will be installed into {{Code|/Library/Ruby/Gems/1.x/phidgets-ffi-x.x.x}}.&lt;br /&gt;
&lt;br /&gt;
For typical Linux systems, the gem will be installed into {{Code|var/lib/gems/1.9/gems/phidgets-ffi-x.x.x}}.&lt;br /&gt;
&lt;br /&gt;
===Use Our Examples===&lt;br /&gt;
&lt;br /&gt;
Open a command line terminal and navigate to the phidgets-ffi gem directory. {{Code|cd}} into the {{Code|examples}} folder. Here, you will find all of the examples available for Ruby.  If you aren&#039;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]].&lt;br /&gt;
&lt;br /&gt;
The easiest way to confirm that your environment is set up properly will be to compile and run the {{Code|HelloWorld}} Ruby example.&lt;br /&gt;
&lt;br /&gt;
The only thing left to do is to run the examples! Type the following into command line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  ruby HelloWorld.rb&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This program will detect for devices that are attached/detached on the computer. Go ahead, and attach or detach your devices! Here is an example output:&lt;br /&gt;
&lt;br /&gt;
[[File:Ruby Linux HelloWorld Output.PNG|link=|alt=HelloWorld Output]]&lt;br /&gt;
&lt;br /&gt;
After confirming that the {{Code|HelloWorld}} example is working, you can proceed to run the example for your device. If you aren&#039;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 ensure that you have set your start up project to be the one that matches your device before compiling. &lt;br /&gt;
&lt;br /&gt;
Once you have the Ruby examples running, we have a [[#Follow the Examples|teaching]] section below to help you follow them.&lt;br /&gt;
&lt;br /&gt;
==Write Your Own Code==&lt;br /&gt;
&lt;br /&gt;
When you are building a project from scratch, or adding Phidget function calls to an existing project, you&#039;ll need to configure your environment to properly link the Phidget Ruby libraries. To begin: &lt;br /&gt;
&lt;br /&gt;
1. Generate a new  Ruby Windows Applications project with a descriptive name such as PhidgetTest. &lt;br /&gt;
&lt;br /&gt;
[[File:CSharp VS2005 New Project.PNG|link=|alt=New Project]]&lt;br /&gt;
&lt;br /&gt;
2. Add a reference to the .NET Phidgets library.&lt;br /&gt;
&lt;br /&gt;
[[File:CSharp VS2005 Add Reference.PNG|link=|alt=Add Reference]]&lt;br /&gt;
&lt;br /&gt;
3. Under the .NET tab, select {{Code|Phidget21.NET.dll}}.&lt;br /&gt;
If you used our installer, these files are installed in {{Code|C:\Program Files\Phidgets}}, by default. If it does not appear in this list, then you can browse to the Phidget Framework installation directory and add the file.&lt;br /&gt;
&lt;br /&gt;
[[File:CSharp VS2005 Add Reference 2.PNG|link=|alt=Add Reference]]&lt;br /&gt;
&lt;br /&gt;
4. Then, in your code, you will need to include the Phidget library:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=csharp&amp;gt;&lt;br /&gt;
  using Phidgets;&lt;br /&gt;
  using Phidgets.Events;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The project now has access to the Phidget21 function calls and you are ready to begin coding.&lt;br /&gt;
&lt;br /&gt;
The same [[#Follow the Examples|teaching ]] section which describes the examples also has further resources for programming your Phidget.&lt;br /&gt;
&lt;br /&gt;
==Linux==&lt;br /&gt;
&lt;br /&gt;
C# has support on Linux through the Mono framework.  &lt;br /&gt;
&lt;br /&gt;
The first step in using Ruby 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.&lt;br /&gt;
&lt;br /&gt;
==Follow the Examples==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
Next, comes our API information.  These resources outline the Ruby Phidget functions:&lt;br /&gt;
&lt;br /&gt;
{{UsingAPhidgetInCodeGeneral|both of which are available in Ruby|[http://www.phidgets.com/documentation/rubydoc Ruby API]}}&lt;br /&gt;
&lt;br /&gt;
===Example Flow===&lt;br /&gt;
&lt;br /&gt;
{{ExamplePseudocode|In Ruby, you can name these &#039;&#039;&#039;event&#039;&#039;&#039; 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. &amp;lt;br&amp;gt; &lt;br /&gt;
In the example code, the event functions common to all Phidgets are called things like &#039;&#039;&#039;AttachHandler()&#039;&#039;&#039; and &#039;&#039;&#039;DetachHandler()&#039;&#039;&#039;, etc.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
Other functions are given in the examples to show you more detail on using your Phidget.  For example, &#039;&#039;&#039;DeviceInitialize()&#039;&#039;&#039; will show what needs to be set up for your Phidget before using it.&lt;br /&gt;
|Creating a Phidget software object in Ruby is specific to the Phidget.  For a Phidget Spatial, for example, this would involve creating a {{Code|Spatial}} object.  The examples show how to do this and other API functions.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The object provides device specific methods and properties which are available from the API for your specific Phidget.|&lt;br /&gt;
[http://www.phidgets.com/documentation/rubydoc Ruby API]}}&lt;br /&gt;
&lt;br /&gt;
==Code Snippets==&lt;br /&gt;
&lt;br /&gt;
==Common Problems and Solutions/Workarounds==&lt;br /&gt;
&lt;br /&gt;
Here you can put various frequent problems and our recommended solutions.&lt;/div&gt;</summary>
		<author><name>Mphi</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs21/index.php?title=Language_-_Ruby&amp;diff=19099</id>
		<title>Language - Ruby</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs21/index.php?title=Language_-_Ruby&amp;diff=19099"/>
		<updated>2012-04-19T20:35:16Z</updated>

		<summary type="html">&lt;p&gt;Mphi: /* Description of Library Files */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Language]]&lt;br /&gt;
[[File:Icon-Ruby.png|64x64px||link=|alt=]]Ruby is an interpreted and object oriented scripting language with simple syntax created by Yukihiro Matsumoto.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
{{LanguageSupport|Ruby|the complete Phidget API, including events|all Phidget devices.|Linux and OS X|}}&lt;br /&gt;
&lt;br /&gt;
==Quick Downloads==&lt;br /&gt;
&lt;br /&gt;
Just need the Ruby documentation, drivers, libraries, and examples?  Here they are:&lt;br /&gt;
&lt;br /&gt;
===API Documentation===&lt;br /&gt;
*[http://www.phidgets.com/documentation/rubydoc API Manual]&lt;br /&gt;
&lt;br /&gt;
===Library and Example Code===&lt;br /&gt;
&lt;br /&gt;
*[http://www.phidgets.com/downloads/examples/rubylib_2.1.8.0.html phidgets-ffi @ RubyGems]&lt;br /&gt;
&lt;br /&gt;
===Drivers===&lt;br /&gt;
*[http://www.phidgets.com/downloads/libraries/libphidget_2.1.8.20111028.tar.gz Linux Source] &lt;br /&gt;
*[http://www.phidgets.com/downloads/libraries/Phidget_2.1.8.20111103.dmg OS X Drivers Installer]&lt;br /&gt;
&lt;br /&gt;
==Getting started with Ruby==&lt;br /&gt;
&lt;br /&gt;
If you are new to writing code for Phidgets, we recommend starting by running, then modifying existing examples. This will allow you to:&lt;br /&gt;
{{ExampleCodeReasons}}&lt;br /&gt;
&lt;br /&gt;
Instructions for OS X and Linux are similar, so they will be combined into the same section.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The first step in using Ruby on Mac or Linux is to install the Phidget libraries.  Compile and install them as explained on the [[Device List|getting started guide for your device]].  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Description of Library Files==&lt;br /&gt;
Ruby programs on OS X and Linux depend on the following files, which the installers put into your system. &lt;br /&gt;
&lt;br /&gt;
If you are using OS X, you will need the:&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|Phidget21.framework}}&amp;lt;/b&amp;gt; contains the actual Phidgets library for OS X, which is used at run-time.  &lt;br /&gt;
&lt;br /&gt;
If you are using Linux, you will need the:&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|libphidget21.so}}&amp;lt;/b&amp;gt; contains the actual Phidgets library for Linux, which is used at run-time.&lt;br /&gt;
&lt;br /&gt;
For both OS X and Linux, you will need the phidgets-ffi gem. &lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|phidgets-ffi}}&amp;lt;/b&amp;gt; is the Phidgets gem for Ruby. This gem contains the library as well as example code.&lt;br /&gt;
&lt;br /&gt;
==Installing phidgets-ffi==&lt;br /&gt;
&lt;br /&gt;
The phidgets-ffi gem will need to be installed. There phidgets-ffi gem relies on the ffi gem. So, if you do not already have it installed, please do so. A version of ffi 1.0.9 or greater is needed. Type the following to install ffi:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  gem install ffi&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Next, please install the phidgets-ffi gem by typing the following into command line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  gem install phidgets-ffi&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For OS X systems, the gem will be installed into {{Code|/Library/Ruby/Gems/1.x/phidgets-ffi-x.x.x}}.&lt;br /&gt;
&lt;br /&gt;
For typical Linux systems, the gem will be installed into {{Code|var/lib/gems/1.9/gems/phidgets-ffi-x.x.x}}.&lt;br /&gt;
&lt;br /&gt;
===Use Our Examples===&lt;br /&gt;
&lt;br /&gt;
Open a command line terminal and navigate to the phidgets-ffi gem directory. {{Code|cd}} into the {{Code|examples}} folder. Here, you will find all of the examples available for Ruby.  If you aren&#039;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]].&lt;br /&gt;
&lt;br /&gt;
The easiest way to confirm that your environment is set up properly will be to compile and run the {{Code|HelloWorld}} Ruby example.&lt;br /&gt;
&lt;br /&gt;
The only thing left to do is to run the examples! Type the following into command line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  ruby HelloWorld.rb&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This program will detect for devices that are attached/detached on the computer. Go ahead, and attach or detach your devices! Here is an example output:&lt;br /&gt;
&lt;br /&gt;
[[File:Ruby Linux HelloWorld Output.PNG|link=|alt=HelloWorld Output]]&lt;br /&gt;
&lt;br /&gt;
After confirming that the {{Code|HelloWorld}} example is working, you can proceed to run the example for your device. If you aren&#039;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 ensure that you have set your start up project to be the one that matches your device before compiling. &lt;br /&gt;
&lt;br /&gt;
Once you have the Ruby examples running, we have a [[#Follow the Examples|teaching]] section below to help you follow them.&lt;br /&gt;
&lt;br /&gt;
==Write Your Own Code==&lt;br /&gt;
&lt;br /&gt;
When you are building a project from scratch, or adding Phidget function calls to an existing project, you&#039;ll need to configure your environment to properly link the Phidget Ruby libraries. To begin: &lt;br /&gt;
&lt;br /&gt;
1. Generate a new  Ruby Windows Applications project with a descriptive name such as PhidgetTest. &lt;br /&gt;
&lt;br /&gt;
[[File:CSharp VS2005 New Project.PNG|link=|alt=New Project]]&lt;br /&gt;
&lt;br /&gt;
2. Add a reference to the .NET Phidgets library.&lt;br /&gt;
&lt;br /&gt;
[[File:CSharp VS2005 Add Reference.PNG|link=|alt=Add Reference]]&lt;br /&gt;
&lt;br /&gt;
3. Under the .NET tab, select {{Code|Phidget21.NET.dll}}.&lt;br /&gt;
If you used our installer, these files are installed in {{Code|C:\Program Files\Phidgets}}, by default. If it does not appear in this list, then you can browse to the Phidget Framework installation directory and add the file.&lt;br /&gt;
&lt;br /&gt;
[[File:CSharp VS2005 Add Reference 2.PNG|link=|alt=Add Reference]]&lt;br /&gt;
&lt;br /&gt;
4. Then, in your code, you will need to include the Phidget library:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=csharp&amp;gt;&lt;br /&gt;
  using Phidgets;&lt;br /&gt;
  using Phidgets.Events;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The project now has access to the Phidget21 function calls and you are ready to begin coding.&lt;br /&gt;
&lt;br /&gt;
The same [[#Follow the Examples|teaching ]] section which describes the examples also has further resources for programming your Phidget.&lt;br /&gt;
&lt;br /&gt;
==Linux==&lt;br /&gt;
&lt;br /&gt;
C# has support on Linux through the Mono framework.  &lt;br /&gt;
&lt;br /&gt;
The first step in using Ruby 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.&lt;br /&gt;
&lt;br /&gt;
==Follow the Examples==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
Next, comes our API information.  These resources outline the Ruby Phidget functions:&lt;br /&gt;
&lt;br /&gt;
{{UsingAPhidgetInCodeGeneral|both of which are available in Ruby|[http://www.phidgets.com/documentation/rubydoc Ruby API]}}&lt;br /&gt;
&lt;br /&gt;
===Example Flow===&lt;br /&gt;
&lt;br /&gt;
{{ExamplePseudocode|In Ruby, you can name these &#039;&#039;&#039;event&#039;&#039;&#039; 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. &amp;lt;br&amp;gt; &lt;br /&gt;
In the example code, the event functions common to all Phidgets are called things like &#039;&#039;&#039;AttachHandler()&#039;&#039;&#039; and &#039;&#039;&#039;DetachHandler()&#039;&#039;&#039;, etc.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
Other functions are given in the examples to show you more detail on using your Phidget.  For example, &#039;&#039;&#039;DeviceInitialize()&#039;&#039;&#039; will show what needs to be set up for your Phidget before using it.&lt;br /&gt;
|Creating a Phidget software object in Ruby is specific to the Phidget.  For a Phidget Spatial, for example, this would involve creating a {{Code|Spatial}} object.  The examples show how to do this and other API functions.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The object provides device specific methods and properties which are available from the API for your specific Phidget.|&lt;br /&gt;
[http://www.phidgets.com/documentation/rubydoc Ruby API]}}&lt;br /&gt;
&lt;br /&gt;
==Code Snippets==&lt;br /&gt;
&lt;br /&gt;
==Common Problems and Solutions/Workarounds==&lt;br /&gt;
&lt;br /&gt;
Here you can put various frequent problems and our recommended solutions.&lt;/div&gt;</summary>
		<author><name>Mphi</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs21/index.php?title=Language_-_Ruby&amp;diff=19096</id>
		<title>Language - Ruby</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs21/index.php?title=Language_-_Ruby&amp;diff=19096"/>
		<updated>2012-04-19T20:26:36Z</updated>

		<summary type="html">&lt;p&gt;Mphi: /* Description of Library Files */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Language]]&lt;br /&gt;
[[File:Icon-Ruby.png|64x64px||link=|alt=]]Ruby is an interpreted and object oriented scripting language with simple syntax created by Yukihiro Matsumoto.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
{{LanguageSupport|Ruby|the complete Phidget API, including events|all Phidget devices.|Linux and OS X|}}&lt;br /&gt;
&lt;br /&gt;
==Quick Downloads==&lt;br /&gt;
&lt;br /&gt;
Just need the Ruby documentation, drivers, libraries, and examples?  Here they are:&lt;br /&gt;
&lt;br /&gt;
===API Documentation===&lt;br /&gt;
*[http://www.phidgets.com/documentation/rubydoc API Manual]&lt;br /&gt;
&lt;br /&gt;
===Library and Example Code===&lt;br /&gt;
&lt;br /&gt;
*[http://www.phidgets.com/downloads/examples/rubylib_2.1.8.0.html phidgets-ffi @ RubyGems]&lt;br /&gt;
&lt;br /&gt;
===Drivers===&lt;br /&gt;
*[http://www.phidgets.com/downloads/libraries/libphidget_2.1.8.20111028.tar.gz Linux Source] &lt;br /&gt;
*[http://www.phidgets.com/downloads/libraries/Phidget_2.1.8.20111103.dmg OS X Drivers Installer]&lt;br /&gt;
&lt;br /&gt;
==Getting started with Ruby==&lt;br /&gt;
&lt;br /&gt;
If you are new to writing code for Phidgets, we recommend starting by running, then modifying existing examples. This will allow you to:&lt;br /&gt;
{{ExampleCodeReasons}}&lt;br /&gt;
&lt;br /&gt;
Instructions for OS X and Linux are similar, so they will be combined into the same section.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The first step in using Ruby on Mac or Linux is to install the Phidget libraries.  Compile and install them as explained on the [[Device List|getting started guide for your device]].  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Description of Library Files==&lt;br /&gt;
Ruby programs on Windows depend on the following files, which the installers above put onto your system:&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|phidget21.dll}}&amp;lt;/b&amp;gt; contains the actual Phidgets library, which is used at run-time.  By default, it is placed in {{Code|C:\Windows\System32}}.&lt;br /&gt;
You will also need one of the following two files, depending on the .NET framework version you are targeting:&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|phidgets-ffi}}&amp;lt;/b&amp;gt; is the Phidgets gem for Ruby. This gem contains the library as well as example code.&lt;br /&gt;
&lt;br /&gt;
==Installing phidgets-ffi==&lt;br /&gt;
&lt;br /&gt;
The phidgets-ffi gem will need to be installed. There phidgets-ffi gem relies on the ffi gem. So, if you do not already have it installed, please do so. A version of ffi 1.0.9 or greater is needed. Type the following to install ffi:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  gem install ffi&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Next, please install the phidgets-ffi gem by typing the following into command line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  gem install phidgets-ffi&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For OS X systems, the gem will be installed into {{Code|/Library/Ruby/Gems/1.x/phidgets-ffi-x.x.x}}.&lt;br /&gt;
&lt;br /&gt;
For typical Linux systems, the gem will be installed into {{Code|var/lib/gems/1.9/gems/phidgets-ffi-x.x.x}}.&lt;br /&gt;
&lt;br /&gt;
===Use Our Examples===&lt;br /&gt;
&lt;br /&gt;
Open a command line terminal and navigate to the phidgets-ffi gem directory. {{Code|cd}} into the {{Code|examples}} folder. Here, you will find all of the examples available for Ruby.  If you aren&#039;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]].&lt;br /&gt;
&lt;br /&gt;
The easiest way to confirm that your environment is set up properly will be to compile and run the {{Code|HelloWorld}} Ruby example.&lt;br /&gt;
&lt;br /&gt;
The only thing left to do is to run the examples! Type the following into command line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  ruby HelloWorld.rb&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This program will detect for devices that are attached/detached on the computer. Go ahead, and attach or detach your devices! Here is an example output:&lt;br /&gt;
&lt;br /&gt;
[[File:Ruby Linux HelloWorld Output.PNG|link=|alt=HelloWorld Output]]&lt;br /&gt;
&lt;br /&gt;
After confirming that the {{Code|HelloWorld}} example is working, you can proceed to run the example for your device. If you aren&#039;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 ensure that you have set your start up project to be the one that matches your device before compiling. &lt;br /&gt;
&lt;br /&gt;
Once you have the Ruby examples running, we have a [[#Follow the Examples|teaching]] section below to help you follow them.&lt;br /&gt;
&lt;br /&gt;
==Write Your Own Code==&lt;br /&gt;
&lt;br /&gt;
When you are building a project from scratch, or adding Phidget function calls to an existing project, you&#039;ll need to configure your environment to properly link the Phidget Ruby libraries. To begin: &lt;br /&gt;
&lt;br /&gt;
1. Generate a new  Ruby Windows Applications project with a descriptive name such as PhidgetTest. &lt;br /&gt;
&lt;br /&gt;
[[File:CSharp VS2005 New Project.PNG|link=|alt=New Project]]&lt;br /&gt;
&lt;br /&gt;
2. Add a reference to the .NET Phidgets library.&lt;br /&gt;
&lt;br /&gt;
[[File:CSharp VS2005 Add Reference.PNG|link=|alt=Add Reference]]&lt;br /&gt;
&lt;br /&gt;
3. Under the .NET tab, select {{Code|Phidget21.NET.dll}}.&lt;br /&gt;
If you used our installer, these files are installed in {{Code|C:\Program Files\Phidgets}}, by default. If it does not appear in this list, then you can browse to the Phidget Framework installation directory and add the file.&lt;br /&gt;
&lt;br /&gt;
[[File:CSharp VS2005 Add Reference 2.PNG|link=|alt=Add Reference]]&lt;br /&gt;
&lt;br /&gt;
4. Then, in your code, you will need to include the Phidget library:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=csharp&amp;gt;&lt;br /&gt;
  using Phidgets;&lt;br /&gt;
  using Phidgets.Events;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The project now has access to the Phidget21 function calls and you are ready to begin coding.&lt;br /&gt;
&lt;br /&gt;
The same [[#Follow the Examples|teaching ]] section which describes the examples also has further resources for programming your Phidget.&lt;br /&gt;
&lt;br /&gt;
==Linux==&lt;br /&gt;
&lt;br /&gt;
C# has support on Linux through the Mono framework.  &lt;br /&gt;
&lt;br /&gt;
The first step in using Ruby 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.&lt;br /&gt;
&lt;br /&gt;
==Follow the Examples==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
Next, comes our API information.  These resources outline the Ruby Phidget functions:&lt;br /&gt;
&lt;br /&gt;
{{UsingAPhidgetInCodeGeneral|both of which are available in Ruby|[http://www.phidgets.com/documentation/rubydoc Ruby API]}}&lt;br /&gt;
&lt;br /&gt;
===Example Flow===&lt;br /&gt;
&lt;br /&gt;
{{ExamplePseudocode|In Ruby, you can name these &#039;&#039;&#039;event&#039;&#039;&#039; 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. &amp;lt;br&amp;gt; &lt;br /&gt;
In the example code, the event functions common to all Phidgets are called things like &#039;&#039;&#039;AttachHandler()&#039;&#039;&#039; and &#039;&#039;&#039;DetachHandler()&#039;&#039;&#039;, etc.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
Other functions are given in the examples to show you more detail on using your Phidget.  For example, &#039;&#039;&#039;DeviceInitialize()&#039;&#039;&#039; will show what needs to be set up for your Phidget before using it.&lt;br /&gt;
|Creating a Phidget software object in Ruby is specific to the Phidget.  For a Phidget Spatial, for example, this would involve creating a {{Code|Spatial}} object.  The examples show how to do this and other API functions.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The object provides device specific methods and properties which are available from the API for your specific Phidget.|&lt;br /&gt;
[http://www.phidgets.com/documentation/rubydoc Ruby API]}}&lt;br /&gt;
&lt;br /&gt;
==Code Snippets==&lt;br /&gt;
&lt;br /&gt;
==Common Problems and Solutions/Workarounds==&lt;br /&gt;
&lt;br /&gt;
Here you can put various frequent problems and our recommended solutions.&lt;/div&gt;</summary>
		<author><name>Mphi</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs21/index.php?title=Language_-_Ruby&amp;diff=19093</id>
		<title>Language - Ruby</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs21/index.php?title=Language_-_Ruby&amp;diff=19093"/>
		<updated>2012-04-19T20:11:02Z</updated>

		<summary type="html">&lt;p&gt;Mphi: /* OS X */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Language]]&lt;br /&gt;
[[File:Icon-Ruby.png|64x64px||link=|alt=]]Ruby is an interpreted and object oriented scripting language with simple syntax created by Yukihiro Matsumoto.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
{{LanguageSupport|Ruby|the complete Phidget API, including events|all Phidget devices.|Linux and OS X|}}&lt;br /&gt;
&lt;br /&gt;
==Quick Downloads==&lt;br /&gt;
&lt;br /&gt;
Just need the Ruby documentation, drivers, libraries, and examples?  Here they are:&lt;br /&gt;
&lt;br /&gt;
===API Documentation===&lt;br /&gt;
*[http://www.phidgets.com/documentation/rubydoc API Manual]&lt;br /&gt;
&lt;br /&gt;
===Library and Example Code===&lt;br /&gt;
&lt;br /&gt;
*[http://www.phidgets.com/downloads/examples/rubylib_2.1.8.0.html phidgets-ffi @ RubyGems]&lt;br /&gt;
&lt;br /&gt;
===Drivers===&lt;br /&gt;
*[http://www.phidgets.com/downloads/libraries/libphidget_2.1.8.20111028.tar.gz Linux Source] &lt;br /&gt;
*[http://www.phidgets.com/downloads/libraries/Phidget_2.1.8.20111103.dmg OS X Drivers Installer]&lt;br /&gt;
&lt;br /&gt;
==Getting started with Ruby==&lt;br /&gt;
&lt;br /&gt;
If you are new to writing code for Phidgets, we recommend starting by running, then modifying existing examples. This will allow you to:&lt;br /&gt;
{{ExampleCodeReasons}}&lt;br /&gt;
&lt;br /&gt;
Instructions for OS X and Linux are similar, so they will be combined into the same section.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The first step in using Ruby on Mac or Linux is to install the Phidget libraries.  Compile and install them as explained on the [[Device List|getting started guide for your device]].  &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==Description of Library Files==&lt;br /&gt;
Ruby programs on Windows depend on the following files, which the installers above put onto your system:&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|phidget21.dll}}&amp;lt;/b&amp;gt; contains the actual Phidgets library, which is used at run-time.  By default, it is placed in {{Code|C:\Windows\System32}}.&lt;br /&gt;
You will also need one of the following two files, depending on the .NET framework version you are targeting:&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|/lib}}&amp;lt;/b&amp;gt; is the Phidgets library for .NET framework &amp;lt;i&amp;gt;&amp;lt;b&amp;gt;2.0&amp;lt;/b&amp;gt;&amp;lt;/i&amp;gt; or higher. Your compiler has to know where this file is. By default, it is placed into {{Code|C:\Program Files\Phidgets}}. You can either point your compiler to that location, or copy and link to it in a directory for your project workspace. &lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|examples}}&amp;lt;/b&amp;gt; is the Phidgets library for .NET framework &amp;lt;i&amp;gt;&amp;lt;b&amp;gt;1.1&amp;lt;/b&amp;gt;&amp;lt;/i&amp;gt;. Your compiler has to know where this file is. By default, is is placed into {{Code|C:\Program Files\Phidgets}}. You can either point your compiler to that location, or copy and link to it in a directory for your project workspace. &lt;br /&gt;
&lt;br /&gt;
If you do not want to use our installer, you can download the five [http://www.phidgets.com/downloads/libraries/Phidget21-windevel_2.1.8.20111220.zip file files].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Running the examples and writing your own code can be fairly compiler-specific, so we include instructions for each compiler below.&lt;br /&gt;
&lt;br /&gt;
==Installing phidgets-ffi==&lt;br /&gt;
&lt;br /&gt;
The phidgets-ffi gem will need to be installed. There phidgets-ffi gem relies on the ffi gem. So, if you do not already have it installed, please do so. A version of ffi 1.0.9 or greater is needed. Type the following to install ffi:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  gem install ffi&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Next, please install the phidgets-ffi gem by typing the following into command line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  gem install phidgets-ffi&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For OS X systems, the gem will be installed into {{Code|/Library/Ruby/Gems/1.x/phidgets-ffi-x.x.x}}.&lt;br /&gt;
&lt;br /&gt;
For typical Linux systems, the gem will be installed into {{Code|var/lib/gems/1.9/gems/phidgets-ffi-x.x.x}}.&lt;br /&gt;
&lt;br /&gt;
===Use Our Examples===&lt;br /&gt;
&lt;br /&gt;
Open a command line terminal and navigate to the phidgets-ffi gem directory. {{Code|cd}} into the {{Code|examples}} folder. Here, you will find all of the examples available for Ruby.  If you aren&#039;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]].&lt;br /&gt;
&lt;br /&gt;
The easiest way to confirm that your environment is set up properly will be to compile and run the {{Code|HelloWorld}} Ruby example.&lt;br /&gt;
&lt;br /&gt;
The only thing left to do is to run the examples! Type the following into command line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  ruby HelloWorld.rb&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This program will detect for devices that are attached/detached on the computer. Go ahead, and attach or detach your devices! Here is an example output:&lt;br /&gt;
&lt;br /&gt;
[[File:Ruby Linux HelloWorld Output.PNG|link=|alt=HelloWorld Output]]&lt;br /&gt;
&lt;br /&gt;
After confirming that the {{Code|HelloWorld}} example is working, you can proceed to run the example for your device. If you aren&#039;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 ensure that you have set your start up project to be the one that matches your device before compiling. &lt;br /&gt;
&lt;br /&gt;
Once you have the Ruby examples running, we have a [[#Follow the Examples|teaching]] section below to help you follow them.&lt;br /&gt;
&lt;br /&gt;
==Write Your Own Code==&lt;br /&gt;
&lt;br /&gt;
When you are building a project from scratch, or adding Phidget function calls to an existing project, you&#039;ll need to configure your environment to properly link the Phidget Ruby libraries. To begin: &lt;br /&gt;
&lt;br /&gt;
1. Generate a new  Ruby Windows Applications project with a descriptive name such as PhidgetTest. &lt;br /&gt;
&lt;br /&gt;
[[File:CSharp VS2005 New Project.PNG|link=|alt=New Project]]&lt;br /&gt;
&lt;br /&gt;
2. Add a reference to the .NET Phidgets library.&lt;br /&gt;
&lt;br /&gt;
[[File:CSharp VS2005 Add Reference.PNG|link=|alt=Add Reference]]&lt;br /&gt;
&lt;br /&gt;
3. Under the .NET tab, select {{Code|Phidget21.NET.dll}}.&lt;br /&gt;
If you used our installer, these files are installed in {{Code|C:\Program Files\Phidgets}}, by default. If it does not appear in this list, then you can browse to the Phidget Framework installation directory and add the file.&lt;br /&gt;
&lt;br /&gt;
[[File:CSharp VS2005 Add Reference 2.PNG|link=|alt=Add Reference]]&lt;br /&gt;
&lt;br /&gt;
4. Then, in your code, you will need to include the Phidget library:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=csharp&amp;gt;&lt;br /&gt;
  using Phidgets;&lt;br /&gt;
  using Phidgets.Events;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The project now has access to the Phidget21 function calls and you are ready to begin coding.&lt;br /&gt;
&lt;br /&gt;
The same [[#Follow the Examples|teaching ]] section which describes the examples also has further resources for programming your Phidget.&lt;br /&gt;
&lt;br /&gt;
==Linux==&lt;br /&gt;
&lt;br /&gt;
C# has support on Linux through the Mono framework.  &lt;br /&gt;
&lt;br /&gt;
The first step in using Ruby 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.&lt;br /&gt;
&lt;br /&gt;
==Follow the Examples==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
Next, comes our API information.  These resources outline the Ruby Phidget functions:&lt;br /&gt;
&lt;br /&gt;
{{UsingAPhidgetInCodeGeneral|both of which are available in Ruby|[http://www.phidgets.com/documentation/rubydoc Ruby API]}}&lt;br /&gt;
&lt;br /&gt;
===Example Flow===&lt;br /&gt;
&lt;br /&gt;
{{ExamplePseudocode|In Ruby, you can name these &#039;&#039;&#039;event&#039;&#039;&#039; 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. &amp;lt;br&amp;gt; &lt;br /&gt;
In the example code, the event functions common to all Phidgets are called things like &#039;&#039;&#039;AttachHandler()&#039;&#039;&#039; and &#039;&#039;&#039;DetachHandler()&#039;&#039;&#039;, etc.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
Other functions are given in the examples to show you more detail on using your Phidget.  For example, &#039;&#039;&#039;DeviceInitialize()&#039;&#039;&#039; will show what needs to be set up for your Phidget before using it.&lt;br /&gt;
|Creating a Phidget software object in Ruby is specific to the Phidget.  For a Phidget Spatial, for example, this would involve creating a {{Code|Spatial}} object.  The examples show how to do this and other API functions.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The object provides device specific methods and properties which are available from the API for your specific Phidget.|&lt;br /&gt;
[http://www.phidgets.com/documentation/rubydoc Ruby API]}}&lt;br /&gt;
&lt;br /&gt;
==Code Snippets==&lt;br /&gt;
&lt;br /&gt;
==Common Problems and Solutions/Workarounds==&lt;br /&gt;
&lt;br /&gt;
Here you can put various frequent problems and our recommended solutions.&lt;/div&gt;</summary>
		<author><name>Mphi</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs21/index.php?title=Language_-_Ruby&amp;diff=19092</id>
		<title>Language - Ruby</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs21/index.php?title=Language_-_Ruby&amp;diff=19092"/>
		<updated>2012-04-19T20:09:41Z</updated>

		<summary type="html">&lt;p&gt;Mphi: /* Getting started with Ruby */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Language]]&lt;br /&gt;
[[File:Icon-Ruby.png|64x64px||link=|alt=]]Ruby is an interpreted and object oriented scripting language with simple syntax created by Yukihiro Matsumoto.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
{{LanguageSupport|Ruby|the complete Phidget API, including events|all Phidget devices.|Linux and OS X|}}&lt;br /&gt;
&lt;br /&gt;
==Quick Downloads==&lt;br /&gt;
&lt;br /&gt;
Just need the Ruby documentation, drivers, libraries, and examples?  Here they are:&lt;br /&gt;
&lt;br /&gt;
===API Documentation===&lt;br /&gt;
*[http://www.phidgets.com/documentation/rubydoc API Manual]&lt;br /&gt;
&lt;br /&gt;
===Library and Example Code===&lt;br /&gt;
&lt;br /&gt;
*[http://www.phidgets.com/downloads/examples/rubylib_2.1.8.0.html phidgets-ffi @ RubyGems]&lt;br /&gt;
&lt;br /&gt;
===Drivers===&lt;br /&gt;
*[http://www.phidgets.com/downloads/libraries/libphidget_2.1.8.20111028.tar.gz Linux Source] &lt;br /&gt;
*[http://www.phidgets.com/downloads/libraries/Phidget_2.1.8.20111103.dmg OS X Drivers Installer]&lt;br /&gt;
&lt;br /&gt;
==Getting started with Ruby==&lt;br /&gt;
&lt;br /&gt;
If you are new to writing code for Phidgets, we recommend starting by running, then modifying existing examples. This will allow you to:&lt;br /&gt;
{{ExampleCodeReasons}}&lt;br /&gt;
&lt;br /&gt;
Instructions for OS X and Linux are similar, so they will be combined into the same section.&lt;br /&gt;
&lt;br /&gt;
==OS X==&lt;br /&gt;
&lt;br /&gt;
The first step in using Ruby on Mac is to install the Phidget libraries.  Compile and install them as explained on the [[Device List|getting started guide for your device]].  Then, the [[OS - OS X]] page also describes the different Phidget files, their installed locations, and their roles....&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Description of Library Files===&lt;br /&gt;
Ruby programs on Windows depend on the following files, which the installers above put onto your system:&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|phidget21.dll}}&amp;lt;/b&amp;gt; contains the actual Phidgets library, which is used at run-time.  By default, it is placed in {{Code|C:\Windows\System32}}.&lt;br /&gt;
You will also need one of the following two files, depending on the .NET framework version you are targeting:&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|/lib}}&amp;lt;/b&amp;gt; is the Phidgets library for .NET framework &amp;lt;i&amp;gt;&amp;lt;b&amp;gt;2.0&amp;lt;/b&amp;gt;&amp;lt;/i&amp;gt; or higher. Your compiler has to know where this file is. By default, it is placed into {{Code|C:\Program Files\Phidgets}}. You can either point your compiler to that location, or copy and link to it in a directory for your project workspace. &lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|examples}}&amp;lt;/b&amp;gt; is the Phidgets library for .NET framework &amp;lt;i&amp;gt;&amp;lt;b&amp;gt;1.1&amp;lt;/b&amp;gt;&amp;lt;/i&amp;gt;. Your compiler has to know where this file is. By default, is is placed into {{Code|C:\Program Files\Phidgets}}. You can either point your compiler to that location, or copy and link to it in a directory for your project workspace. &lt;br /&gt;
&lt;br /&gt;
If you do not want to use our installer, you can download the five [http://www.phidgets.com/downloads/libraries/Phidget21-windevel_2.1.8.20111220.zip file files].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Running the examples and writing your own code can be fairly compiler-specific, so we include instructions for each compiler below.&lt;br /&gt;
&lt;br /&gt;
===Installing phidgets-ffi===&lt;br /&gt;
&lt;br /&gt;
The phidgets-ffi gem will need to be installed. There phidgets-ffi gem relies on the ffi gem. So, if you do not already have it installed, please do so. A version of ffi 1.0.9 or greater is needed. Type the following to install ffi:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  gem install ffi&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Next, please install the phidgets-ffi gem by typing the following into command line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  gem install phidgets-ffi&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For OS X systems, the gem will be installed into {{Code|/Library/Ruby/Gems/1.x/phidgets-ffi-x.x.x}}.&lt;br /&gt;
&lt;br /&gt;
For typical Linux systems, the gem will be installed into {{Code|var/lib/gems/1.9/gems/phidgets-ffi-x.x.x}}.&lt;br /&gt;
&lt;br /&gt;
===Use Our Examples===&lt;br /&gt;
&lt;br /&gt;
Open a command line terminal and navigate to the phidgets-ffi gem directory. {{Code|cd}} into the {{Code|examples}} folder. Here, you will find all of the examples available for Ruby.  If you aren&#039;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]].&lt;br /&gt;
&lt;br /&gt;
The easiest way to confirm that your environment is set up properly will be to compile and run the {{Code|HelloWorld}} Ruby example.&lt;br /&gt;
&lt;br /&gt;
The only thing left to do is to run the examples! Type the following into command line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  ruby HelloWorld.rb&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This program will detect for devices that are attached/detached on the computer. Go ahead, and attach or detach your devices! Here is an example output:&lt;br /&gt;
&lt;br /&gt;
[[File:Ruby Linux HelloWorld Output.PNG|link=|alt=HelloWorld Output]]&lt;br /&gt;
&lt;br /&gt;
After confirming that the {{Code|HelloWorld}} example is working, you can proceed to run the example for your device. If you aren&#039;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 ensure that you have set your start up project to be the one that matches your device before compiling. &lt;br /&gt;
&lt;br /&gt;
Once you have the Ruby examples running, we have a [[#Follow the Examples|teaching]] section below to help you follow them.&lt;br /&gt;
&lt;br /&gt;
=====Write Your Own Code=====&lt;br /&gt;
&lt;br /&gt;
When you are building a project from scratch, or adding Phidget function calls to an existing project, you&#039;ll need to configure your environment to properly link the Phidget Ruby libraries. To begin: &lt;br /&gt;
&lt;br /&gt;
1. Generate a new  Ruby Windows Applications project with a descriptive name such as PhidgetTest. &lt;br /&gt;
&lt;br /&gt;
[[File:CSharp VS2005 New Project.PNG|link=|alt=New Project]]&lt;br /&gt;
&lt;br /&gt;
2. Add a reference to the .NET Phidgets library.&lt;br /&gt;
&lt;br /&gt;
[[File:CSharp VS2005 Add Reference.PNG|link=|alt=Add Reference]]&lt;br /&gt;
&lt;br /&gt;
3. Under the .NET tab, select {{Code|Phidget21.NET.dll}}.&lt;br /&gt;
If you used our installer, these files are installed in {{Code|C:\Program Files\Phidgets}}, by default. If it does not appear in this list, then you can browse to the Phidget Framework installation directory and add the file.&lt;br /&gt;
&lt;br /&gt;
[[File:CSharp VS2005 Add Reference 2.PNG|link=|alt=Add Reference]]&lt;br /&gt;
&lt;br /&gt;
4. Then, in your code, you will need to include the Phidget library:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=csharp&amp;gt;&lt;br /&gt;
  using Phidgets;&lt;br /&gt;
  using Phidgets.Events;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The project now has access to the Phidget21 function calls and you are ready to begin coding.&lt;br /&gt;
&lt;br /&gt;
The same [[#Follow the Examples|teaching ]] section which describes the examples also has further resources for programming your Phidget.&lt;br /&gt;
&lt;br /&gt;
==Linux==&lt;br /&gt;
&lt;br /&gt;
C# has support on Linux through the Mono framework.  &lt;br /&gt;
&lt;br /&gt;
The first step in using Ruby 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.&lt;br /&gt;
&lt;br /&gt;
==Follow the Examples==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
Next, comes our API information.  These resources outline the Ruby Phidget functions:&lt;br /&gt;
&lt;br /&gt;
{{UsingAPhidgetInCodeGeneral|both of which are available in Ruby|[http://www.phidgets.com/documentation/rubydoc Ruby API]}}&lt;br /&gt;
&lt;br /&gt;
===Example Flow===&lt;br /&gt;
&lt;br /&gt;
{{ExamplePseudocode|In Ruby, you can name these &#039;&#039;&#039;event&#039;&#039;&#039; 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. &amp;lt;br&amp;gt; &lt;br /&gt;
In the example code, the event functions common to all Phidgets are called things like &#039;&#039;&#039;AttachHandler()&#039;&#039;&#039; and &#039;&#039;&#039;DetachHandler()&#039;&#039;&#039;, etc.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
Other functions are given in the examples to show you more detail on using your Phidget.  For example, &#039;&#039;&#039;DeviceInitialize()&#039;&#039;&#039; will show what needs to be set up for your Phidget before using it.&lt;br /&gt;
|Creating a Phidget software object in Ruby is specific to the Phidget.  For a Phidget Spatial, for example, this would involve creating a {{Code|Spatial}} object.  The examples show how to do this and other API functions.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The object provides device specific methods and properties which are available from the API for your specific Phidget.|&lt;br /&gt;
[http://www.phidgets.com/documentation/rubydoc Ruby API]}}&lt;br /&gt;
&lt;br /&gt;
==Code Snippets==&lt;br /&gt;
&lt;br /&gt;
==Common Problems and Solutions/Workarounds==&lt;br /&gt;
&lt;br /&gt;
Here you can put various frequent problems and our recommended solutions.&lt;/div&gt;</summary>
		<author><name>Mphi</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs21/index.php?title=Language_-_Ruby&amp;diff=19091</id>
		<title>Language - Ruby</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs21/index.php?title=Language_-_Ruby&amp;diff=19091"/>
		<updated>2012-04-19T20:09:01Z</updated>

		<summary type="html">&lt;p&gt;Mphi: /* OS X */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Language]]&lt;br /&gt;
[[File:Icon-Ruby.png|64x64px||link=|alt=]]Ruby is an interpreted and object oriented scripting language with simple syntax created by Yukihiro Matsumoto.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
{{LanguageSupport|Ruby|the complete Phidget API, including events|all Phidget devices.|Linux and OS X|}}&lt;br /&gt;
&lt;br /&gt;
==Quick Downloads==&lt;br /&gt;
&lt;br /&gt;
Just need the Ruby documentation, drivers, libraries, and examples?  Here they are:&lt;br /&gt;
&lt;br /&gt;
===API Documentation===&lt;br /&gt;
*[http://www.phidgets.com/documentation/rubydoc API Manual]&lt;br /&gt;
&lt;br /&gt;
===Library and Example Code===&lt;br /&gt;
&lt;br /&gt;
*[http://www.phidgets.com/downloads/examples/rubylib_2.1.8.0.html phidgets-ffi @ RubyGems]&lt;br /&gt;
&lt;br /&gt;
===Drivers===&lt;br /&gt;
*[http://www.phidgets.com/downloads/libraries/libphidget_2.1.8.20111028.tar.gz Linux Source] &lt;br /&gt;
*[http://www.phidgets.com/downloads/libraries/Phidget_2.1.8.20111103.dmg OS X Drivers Installer]&lt;br /&gt;
&lt;br /&gt;
==Getting started with Ruby==&lt;br /&gt;
&lt;br /&gt;
If you are new to writing code for Phidgets, we recommend starting by running, then modifying existing examples. This will allow you to:&lt;br /&gt;
{{ExampleCodeReasons}}&lt;br /&gt;
&lt;br /&gt;
Instructions are divided up by operating system. Choose:&lt;br /&gt;
*[[#OS X |OS X]]&lt;br /&gt;
*[[#Linux | Linux]] (including PhidgetSBC)&lt;br /&gt;
&lt;br /&gt;
==OS X==&lt;br /&gt;
&lt;br /&gt;
The first step in using Ruby on Mac is to install the Phidget libraries.  Compile and install them as explained on the [[Device List|getting started guide for your device]].  Then, the [[OS - OS X]] page also describes the different Phidget files, their installed locations, and their roles....&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Description of Library Files===&lt;br /&gt;
Ruby programs on Windows depend on the following files, which the installers above put onto your system:&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|phidget21.dll}}&amp;lt;/b&amp;gt; contains the actual Phidgets library, which is used at run-time.  By default, it is placed in {{Code|C:\Windows\System32}}.&lt;br /&gt;
You will also need one of the following two files, depending on the .NET framework version you are targeting:&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|/lib}}&amp;lt;/b&amp;gt; is the Phidgets library for .NET framework &amp;lt;i&amp;gt;&amp;lt;b&amp;gt;2.0&amp;lt;/b&amp;gt;&amp;lt;/i&amp;gt; or higher. Your compiler has to know where this file is. By default, it is placed into {{Code|C:\Program Files\Phidgets}}. You can either point your compiler to that location, or copy and link to it in a directory for your project workspace. &lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|examples}}&amp;lt;/b&amp;gt; is the Phidgets library for .NET framework &amp;lt;i&amp;gt;&amp;lt;b&amp;gt;1.1&amp;lt;/b&amp;gt;&amp;lt;/i&amp;gt;. Your compiler has to know where this file is. By default, is is placed into {{Code|C:\Program Files\Phidgets}}. You can either point your compiler to that location, or copy and link to it in a directory for your project workspace. &lt;br /&gt;
&lt;br /&gt;
If you do not want to use our installer, you can download the five [http://www.phidgets.com/downloads/libraries/Phidget21-windevel_2.1.8.20111220.zip file files].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Running the examples and writing your own code can be fairly compiler-specific, so we include instructions for each compiler below.&lt;br /&gt;
&lt;br /&gt;
===Installing phidgets-ffi===&lt;br /&gt;
&lt;br /&gt;
The phidgets-ffi gem will need to be installed. There phidgets-ffi gem relies on the ffi gem. So, if you do not already have it installed, please do so. A version of ffi 1.0.9 or greater is needed. Type the following to install ffi:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  gem install ffi&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Next, please install the phidgets-ffi gem by typing the following into command line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  gem install phidgets-ffi&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For OS X systems, the gem will be installed into {{Code|/Library/Ruby/Gems/1.x/phidgets-ffi-x.x.x}}.&lt;br /&gt;
&lt;br /&gt;
For typical Linux systems, the gem will be installed into {{Code|var/lib/gems/1.9/gems/phidgets-ffi-x.x.x}}.&lt;br /&gt;
&lt;br /&gt;
===Use Our Examples===&lt;br /&gt;
&lt;br /&gt;
Open a command line terminal and navigate to the phidgets-ffi gem directory. {{Code|cd}} into the {{Code|examples}} folder. Here, you will find all of the examples available for Ruby.  If you aren&#039;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]].&lt;br /&gt;
&lt;br /&gt;
The easiest way to confirm that your environment is set up properly will be to compile and run the {{Code|HelloWorld}} Ruby example.&lt;br /&gt;
&lt;br /&gt;
The only thing left to do is to run the examples! Type the following into command line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  ruby HelloWorld.rb&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This program will detect for devices that are attached/detached on the computer. Go ahead, and attach or detach your devices! Here is an example output:&lt;br /&gt;
&lt;br /&gt;
[[File:Ruby Linux HelloWorld Output.PNG|link=|alt=HelloWorld Output]]&lt;br /&gt;
&lt;br /&gt;
After confirming that the {{Code|HelloWorld}} example is working, you can proceed to run the example for your device. If you aren&#039;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 ensure that you have set your start up project to be the one that matches your device before compiling. &lt;br /&gt;
&lt;br /&gt;
Once you have the Ruby examples running, we have a [[#Follow the Examples|teaching]] section below to help you follow them.&lt;br /&gt;
&lt;br /&gt;
=====Write Your Own Code=====&lt;br /&gt;
&lt;br /&gt;
When you are building a project from scratch, or adding Phidget function calls to an existing project, you&#039;ll need to configure your environment to properly link the Phidget Ruby libraries. To begin: &lt;br /&gt;
&lt;br /&gt;
1. Generate a new  Ruby Windows Applications project with a descriptive name such as PhidgetTest. &lt;br /&gt;
&lt;br /&gt;
[[File:CSharp VS2005 New Project.PNG|link=|alt=New Project]]&lt;br /&gt;
&lt;br /&gt;
2. Add a reference to the .NET Phidgets library.&lt;br /&gt;
&lt;br /&gt;
[[File:CSharp VS2005 Add Reference.PNG|link=|alt=Add Reference]]&lt;br /&gt;
&lt;br /&gt;
3. Under the .NET tab, select {{Code|Phidget21.NET.dll}}.&lt;br /&gt;
If you used our installer, these files are installed in {{Code|C:\Program Files\Phidgets}}, by default. If it does not appear in this list, then you can browse to the Phidget Framework installation directory and add the file.&lt;br /&gt;
&lt;br /&gt;
[[File:CSharp VS2005 Add Reference 2.PNG|link=|alt=Add Reference]]&lt;br /&gt;
&lt;br /&gt;
4. Then, in your code, you will need to include the Phidget library:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=csharp&amp;gt;&lt;br /&gt;
  using Phidgets;&lt;br /&gt;
  using Phidgets.Events;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The project now has access to the Phidget21 function calls and you are ready to begin coding.&lt;br /&gt;
&lt;br /&gt;
The same [[#Follow the Examples|teaching ]] section which describes the examples also has further resources for programming your Phidget.&lt;br /&gt;
&lt;br /&gt;
==Linux==&lt;br /&gt;
&lt;br /&gt;
C# has support on Linux through the Mono framework.  &lt;br /&gt;
&lt;br /&gt;
The first step in using Ruby 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.&lt;br /&gt;
&lt;br /&gt;
==Follow the Examples==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
Next, comes our API information.  These resources outline the Ruby Phidget functions:&lt;br /&gt;
&lt;br /&gt;
{{UsingAPhidgetInCodeGeneral|both of which are available in Ruby|[http://www.phidgets.com/documentation/rubydoc Ruby API]}}&lt;br /&gt;
&lt;br /&gt;
===Example Flow===&lt;br /&gt;
&lt;br /&gt;
{{ExamplePseudocode|In Ruby, you can name these &#039;&#039;&#039;event&#039;&#039;&#039; 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. &amp;lt;br&amp;gt; &lt;br /&gt;
In the example code, the event functions common to all Phidgets are called things like &#039;&#039;&#039;AttachHandler()&#039;&#039;&#039; and &#039;&#039;&#039;DetachHandler()&#039;&#039;&#039;, etc.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
Other functions are given in the examples to show you more detail on using your Phidget.  For example, &#039;&#039;&#039;DeviceInitialize()&#039;&#039;&#039; will show what needs to be set up for your Phidget before using it.&lt;br /&gt;
|Creating a Phidget software object in Ruby is specific to the Phidget.  For a Phidget Spatial, for example, this would involve creating a {{Code|Spatial}} object.  The examples show how to do this and other API functions.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The object provides device specific methods and properties which are available from the API for your specific Phidget.|&lt;br /&gt;
[http://www.phidgets.com/documentation/rubydoc Ruby API]}}&lt;br /&gt;
&lt;br /&gt;
==Code Snippets==&lt;br /&gt;
&lt;br /&gt;
==Common Problems and Solutions/Workarounds==&lt;br /&gt;
&lt;br /&gt;
Here you can put various frequent problems and our recommended solutions.&lt;/div&gt;</summary>
		<author><name>Mphi</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs21/index.php?title=Language_-_Ruby&amp;diff=19090</id>
		<title>Language - Ruby</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs21/index.php?title=Language_-_Ruby&amp;diff=19090"/>
		<updated>2012-04-19T19:53:41Z</updated>

		<summary type="html">&lt;p&gt;Mphi: /* Description of Library Files */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Language]]&lt;br /&gt;
[[File:Icon-Ruby.png|64x64px||link=|alt=]]Ruby is an interpreted and object oriented scripting language with simple syntax created by Yukihiro Matsumoto.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
{{LanguageSupport|Ruby|the complete Phidget API, including events|all Phidget devices.|Linux and OS X|}}&lt;br /&gt;
&lt;br /&gt;
==Quick Downloads==&lt;br /&gt;
&lt;br /&gt;
Just need the Ruby documentation, drivers, libraries, and examples?  Here they are:&lt;br /&gt;
&lt;br /&gt;
===API Documentation===&lt;br /&gt;
*[http://www.phidgets.com/documentation/rubydoc API Manual]&lt;br /&gt;
&lt;br /&gt;
===Library and Example Code===&lt;br /&gt;
&lt;br /&gt;
*[http://www.phidgets.com/downloads/examples/rubylib_2.1.8.0.html phidgets-ffi @ RubyGems]&lt;br /&gt;
&lt;br /&gt;
===Drivers===&lt;br /&gt;
*[http://www.phidgets.com/downloads/libraries/libphidget_2.1.8.20111028.tar.gz Linux Source] &lt;br /&gt;
*[http://www.phidgets.com/downloads/libraries/Phidget_2.1.8.20111103.dmg OS X Drivers Installer]&lt;br /&gt;
&lt;br /&gt;
==Getting started with Ruby==&lt;br /&gt;
&lt;br /&gt;
If you are new to writing code for Phidgets, we recommend starting by running, then modifying existing examples. This will allow you to:&lt;br /&gt;
{{ExampleCodeReasons}}&lt;br /&gt;
&lt;br /&gt;
Instructions are divided up by operating system. Choose:&lt;br /&gt;
*[[#OS X |OS X]]&lt;br /&gt;
*[[#Linux | Linux]] (including PhidgetSBC)&lt;br /&gt;
&lt;br /&gt;
==OS X==&lt;br /&gt;
&lt;br /&gt;
Ruby has excellent support on OS X through the Mono framework. &lt;br /&gt;
&lt;br /&gt;
The first step in using Ruby on Mac is to install the Phidget libraries.  Compile and install them as explained on the [[Device List|getting started guide for your device]].  Then, the [[OS - OS X]] page also describes the different Phidget files, their installed locations, and their roles....&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Description of Library Files===&lt;br /&gt;
Ruby programs on Windows depend on the following files, which the installers above put onto your system:&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|phidget21.dll}}&amp;lt;/b&amp;gt; contains the actual Phidgets library, which is used at run-time.  By default, it is placed in {{Code|C:\Windows\System32}}.&lt;br /&gt;
You will also need one of the following two files, depending on the .NET framework version you are targeting:&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|/lib}}&amp;lt;/b&amp;gt; is the Phidgets library for .NET framework &amp;lt;i&amp;gt;&amp;lt;b&amp;gt;2.0&amp;lt;/b&amp;gt;&amp;lt;/i&amp;gt; or higher. Your compiler has to know where this file is. By default, it is placed into {{Code|C:\Program Files\Phidgets}}. You can either point your compiler to that location, or copy and link to it in a directory for your project workspace. &lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|examples}}&amp;lt;/b&amp;gt; is the Phidgets library for .NET framework &amp;lt;i&amp;gt;&amp;lt;b&amp;gt;1.1&amp;lt;/b&amp;gt;&amp;lt;/i&amp;gt;. Your compiler has to know where this file is. By default, is is placed into {{Code|C:\Program Files\Phidgets}}. You can either point your compiler to that location, or copy and link to it in a directory for your project workspace. &lt;br /&gt;
&lt;br /&gt;
If you do not want to use our installer, you can download the five [http://www.phidgets.com/downloads/libraries/Phidget21-windevel_2.1.8.20111220.zip file files].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Running the examples and writing your own code can be fairly compiler-specific, so we include instructions for each compiler below.&lt;br /&gt;
&lt;br /&gt;
===Installing phidgets-ffi===&lt;br /&gt;
&lt;br /&gt;
The phidgets-ffi gem will need to be installed. There phidgets-ffi gem relies on the ffi gem. So, if you do not already have it installed, please do so. A version of ffi 1.0.9 or greater is needed. Type the following to install ffi:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  gem install ffi&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Next, please install the phidgets-ffi gem by typing the following into command line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  gem install phidgets-ffi&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For OS X systems, the gem will be installed into {{Code|/Library/Ruby/Gems/1.x/phidgets-ffi-x.x.x}}.&lt;br /&gt;
&lt;br /&gt;
For typical Linux systems, the gem will be installed into {{Code|var/lib/gems/1.9/gems/phidgets-ffi-x.x.x}}.&lt;br /&gt;
&lt;br /&gt;
===Use Our Examples===&lt;br /&gt;
&lt;br /&gt;
Open a command line terminal and navigate to the phidgets-ffi gem directory. {{Code|cd}} into the {{Code|examples}} folder. Here, you will find all of the examples available for Ruby.  If you aren&#039;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]].&lt;br /&gt;
&lt;br /&gt;
The easiest way to confirm that your environment is set up properly will be to compile and run the {{Code|HelloWorld}} Ruby example.&lt;br /&gt;
&lt;br /&gt;
The only thing left to do is to run the examples! Type the following into command line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  ruby HelloWorld.rb&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This program will detect for devices that are attached/detached on the computer. Go ahead, and attach or detach your devices! Here is an example output:&lt;br /&gt;
&lt;br /&gt;
[[File:Ruby Linux HelloWorld Output.PNG|link=|alt=HelloWorld Output]]&lt;br /&gt;
&lt;br /&gt;
After confirming that the {{Code|HelloWorld}} example is working, you can proceed to run the example for your device. If you aren&#039;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 ensure that you have set your start up project to be the one that matches your device before compiling. &lt;br /&gt;
&lt;br /&gt;
Once you have the Ruby examples running, we have a [[#Follow the Examples|teaching]] section below to help you follow them.&lt;br /&gt;
&lt;br /&gt;
=====Write Your Own Code=====&lt;br /&gt;
&lt;br /&gt;
When you are building a project from scratch, or adding Phidget function calls to an existing project, you&#039;ll need to configure your environment to properly link the Phidget Ruby libraries. To begin: &lt;br /&gt;
&lt;br /&gt;
1. Generate a new  Ruby Windows Applications project with a descriptive name such as PhidgetTest. &lt;br /&gt;
&lt;br /&gt;
[[File:CSharp VS2005 New Project.PNG|link=|alt=New Project]]&lt;br /&gt;
&lt;br /&gt;
2. Add a reference to the .NET Phidgets library.&lt;br /&gt;
&lt;br /&gt;
[[File:CSharp VS2005 Add Reference.PNG|link=|alt=Add Reference]]&lt;br /&gt;
&lt;br /&gt;
3. Under the .NET tab, select {{Code|Phidget21.NET.dll}}.&lt;br /&gt;
If you used our installer, these files are installed in {{Code|C:\Program Files\Phidgets}}, by default. If it does not appear in this list, then you can browse to the Phidget Framework installation directory and add the file.&lt;br /&gt;
&lt;br /&gt;
[[File:CSharp VS2005 Add Reference 2.PNG|link=|alt=Add Reference]]&lt;br /&gt;
&lt;br /&gt;
4. Then, in your code, you will need to include the Phidget library:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=csharp&amp;gt;&lt;br /&gt;
  using Phidgets;&lt;br /&gt;
  using Phidgets.Events;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The project now has access to the Phidget21 function calls and you are ready to begin coding.&lt;br /&gt;
&lt;br /&gt;
The same [[#Follow the Examples|teaching ]] section which describes the examples also has further resources for programming your Phidget.&lt;br /&gt;
&lt;br /&gt;
==Linux==&lt;br /&gt;
&lt;br /&gt;
C# has support on Linux through the Mono framework.  &lt;br /&gt;
&lt;br /&gt;
The first step in using Ruby 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.&lt;br /&gt;
&lt;br /&gt;
==Follow the Examples==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
Next, comes our API information.  These resources outline the Ruby Phidget functions:&lt;br /&gt;
&lt;br /&gt;
{{UsingAPhidgetInCodeGeneral|both of which are available in Ruby|[http://www.phidgets.com/documentation/rubydoc Ruby API]}}&lt;br /&gt;
&lt;br /&gt;
===Example Flow===&lt;br /&gt;
&lt;br /&gt;
{{ExamplePseudocode|In Ruby, you can name these &#039;&#039;&#039;event&#039;&#039;&#039; 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. &amp;lt;br&amp;gt; &lt;br /&gt;
In the example code, the event functions common to all Phidgets are called things like &#039;&#039;&#039;AttachHandler()&#039;&#039;&#039; and &#039;&#039;&#039;DetachHandler()&#039;&#039;&#039;, etc.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
Other functions are given in the examples to show you more detail on using your Phidget.  For example, &#039;&#039;&#039;DeviceInitialize()&#039;&#039;&#039; will show what needs to be set up for your Phidget before using it.&lt;br /&gt;
|Creating a Phidget software object in Ruby is specific to the Phidget.  For a Phidget Spatial, for example, this would involve creating a {{Code|Spatial}} object.  The examples show how to do this and other API functions.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The object provides device specific methods and properties which are available from the API for your specific Phidget.|&lt;br /&gt;
[http://www.phidgets.com/documentation/rubydoc Ruby API]}}&lt;br /&gt;
&lt;br /&gt;
==Code Snippets==&lt;br /&gt;
&lt;br /&gt;
==Common Problems and Solutions/Workarounds==&lt;br /&gt;
&lt;br /&gt;
Here you can put various frequent problems and our recommended solutions.&lt;/div&gt;</summary>
		<author><name>Mphi</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs21/index.php?title=Language_-_Ruby&amp;diff=19087</id>
		<title>Language - Ruby</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs21/index.php?title=Language_-_Ruby&amp;diff=19087"/>
		<updated>2012-04-19T19:19:25Z</updated>

		<summary type="html">&lt;p&gt;Mphi: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Language]]&lt;br /&gt;
[[File:Icon-Ruby.png|64x64px||link=|alt=]]Ruby is an interpreted and object oriented scripting language with simple syntax created by Yukihiro Matsumoto.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
{{LanguageSupport|Ruby|the complete Phidget API, including events|all Phidget devices.|Linux and OS X|}}&lt;br /&gt;
&lt;br /&gt;
==Quick Downloads==&lt;br /&gt;
&lt;br /&gt;
Just need the Ruby documentation, drivers, libraries, and examples?  Here they are:&lt;br /&gt;
&lt;br /&gt;
===API Documentation===&lt;br /&gt;
*[http://www.phidgets.com/documentation/rubydoc API Manual]&lt;br /&gt;
&lt;br /&gt;
===Library and Example Code===&lt;br /&gt;
&lt;br /&gt;
*[http://www.phidgets.com/downloads/examples/rubylib_2.1.8.0.html phidgets-ffi @ RubyGems]&lt;br /&gt;
&lt;br /&gt;
===Drivers===&lt;br /&gt;
*[http://www.phidgets.com/downloads/libraries/libphidget_2.1.8.20111028.tar.gz Linux Source] &lt;br /&gt;
*[http://www.phidgets.com/downloads/libraries/Phidget_2.1.8.20111103.dmg OS X Drivers Installer]&lt;br /&gt;
&lt;br /&gt;
==Getting started with Ruby==&lt;br /&gt;
&lt;br /&gt;
If you are new to writing code for Phidgets, we recommend starting by running, then modifying existing examples. This will allow you to:&lt;br /&gt;
{{ExampleCodeReasons}}&lt;br /&gt;
&lt;br /&gt;
Instructions are divided up by operating system. Choose:&lt;br /&gt;
*[[#OS X |OS X]]&lt;br /&gt;
*[[#Linux | Linux]] (including PhidgetSBC)&lt;br /&gt;
&lt;br /&gt;
==OS X==&lt;br /&gt;
&lt;br /&gt;
Ruby has excellent support on OS X through the Mono framework. &lt;br /&gt;
&lt;br /&gt;
The first step in using Ruby on Mac is to install the Phidget libraries.  Compile and install them as explained on the [[Device List|getting started guide for your device]].  Then, the [[OS - OS X]] page also describes the different Phidget files, their installed locations, and their roles....&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Description of Library Files===&lt;br /&gt;
Ruby programs on Windows depend on the following files, which the installers above put onto your system:&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|phidget21.dll}}&amp;lt;/b&amp;gt; contains the actual Phidgets library, which is used at run-time.  By default, it is placed in {{Code|C:\Windows\System32}}.&lt;br /&gt;
You will also need one of the following two files, depending on the .NET framework version you are targeting:&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|/lib}}&amp;lt;/b&amp;gt; is the Phidgets library for .NET framework &amp;lt;i&amp;gt;&amp;lt;b&amp;gt;2.0&amp;lt;/b&amp;gt;&amp;lt;/i&amp;gt; or higher. Your compiler has to know where this file is. By default, it is placed into {{Code|C:\Program Files\Phidgets}}. You can either point your compiler to that location, or copy and link to it in a directory for your project workspace. &lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|examples}}&amp;lt;/b&amp;gt; is the Phidgets library for .NET framework &amp;lt;i&amp;gt;&amp;lt;b&amp;gt;1.1&amp;lt;/b&amp;gt;&amp;lt;/i&amp;gt;. Your compiler has to know where this file is. By default, is is placed into {{Code|C:\Program Files\Phidgets}}. You can either point your compiler to that location, or copy and link to it in a directory for your project workspace. &lt;br /&gt;
&lt;br /&gt;
If you do not want to use our installer, you can download the five [http://www.phidgets.com/downloads/libraries/Phidget21-windevel_2.1.8.20111220.zip file files].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Running the examples and writing your own code can be fairly compiler-specific, so we include instructions for each compiler below.&lt;br /&gt;
&lt;br /&gt;
=====Use Our Examples=====&lt;br /&gt;
&lt;br /&gt;
The phidgets-ffi gem will need to be installed. There phidgets-ffi gem relies on the ffi gem. So, if you do not already have it installed, please do so. A version of ffi 1.0.9 or greater is needed. Type the following to install ffi:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  gem install ffi&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Next, please install the phidgets-ffi gem by typing the following into command line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  gem install phidgets-ffi&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
For OS X systems, the gem will be installed into {{Code|/Library/Ruby/Gems/1.x/phidgets-ffi-x.x.x}}.&lt;br /&gt;
&lt;br /&gt;
For typical Linux systems, the gem will be installed into {{Code|var/lib/gems/1.9/gems/phidgets-ffi-x.x.x}}.&lt;br /&gt;
&lt;br /&gt;
Open a command line terminal and navigate to this folder. {{Code|cd}} into the {{Code|examples}} folder. Here, you will find all of the examples available for Ruby.  If you aren&#039;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]].&lt;br /&gt;
&lt;br /&gt;
The easiest way to confirm that your environment is set up properly will be to compile and run the {{Code|HelloWorld}} Ruby example.&lt;br /&gt;
&lt;br /&gt;
The only thing left to do is to run the examples! Type the following into command line:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  ruby HelloWorld.rb&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This program will detect for devices that are attached/detached on the computer. Go ahead, and attach or detach your devices! Here is an example output:&lt;br /&gt;
&lt;br /&gt;
[[File:Ruby Linux HelloWorld Output.PNG|link=|alt=HelloWorld Output]]&lt;br /&gt;
&lt;br /&gt;
After confirming that the {{Code|HelloWorld}} example is working, you can proceed to run the example for your device. If you aren&#039;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 ensure that you have set your start up project to be the one that matches your device before compiling. &lt;br /&gt;
&lt;br /&gt;
Once you have the Ruby examples running, we have a [[#Follow the Examples|teaching]] section below to help you follow them.&lt;br /&gt;
&lt;br /&gt;
=====Write Your Own Code=====&lt;br /&gt;
&lt;br /&gt;
When you are building a project from scratch, or adding Phidget function calls to an existing project, you&#039;ll need to configure your environment to properly link the Phidget Ruby libraries. To begin: &lt;br /&gt;
&lt;br /&gt;
1. Generate a new  Ruby Windows Applications project with a descriptive name such as PhidgetTest. &lt;br /&gt;
&lt;br /&gt;
[[File:CSharp VS2005 New Project.PNG|link=|alt=New Project]]&lt;br /&gt;
&lt;br /&gt;
2. Add a reference to the .NET Phidgets library.&lt;br /&gt;
&lt;br /&gt;
[[File:CSharp VS2005 Add Reference.PNG|link=|alt=Add Reference]]&lt;br /&gt;
&lt;br /&gt;
3. Under the .NET tab, select {{Code|Phidget21.NET.dll}}.&lt;br /&gt;
If you used our installer, these files are installed in {{Code|C:\Program Files\Phidgets}}, by default. If it does not appear in this list, then you can browse to the Phidget Framework installation directory and add the file.&lt;br /&gt;
&lt;br /&gt;
[[File:CSharp VS2005 Add Reference 2.PNG|link=|alt=Add Reference]]&lt;br /&gt;
&lt;br /&gt;
4. Then, in your code, you will need to include the Phidget library:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=csharp&amp;gt;&lt;br /&gt;
  using Phidgets;&lt;br /&gt;
  using Phidgets.Events;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The project now has access to the Phidget21 function calls and you are ready to begin coding.&lt;br /&gt;
&lt;br /&gt;
The same [[#Follow the Examples|teaching ]] section which describes the examples also has further resources for programming your Phidget.&lt;br /&gt;
&lt;br /&gt;
==Linux==&lt;br /&gt;
&lt;br /&gt;
C# has support on Linux through the Mono framework.  &lt;br /&gt;
&lt;br /&gt;
The first step in using Ruby 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.&lt;br /&gt;
&lt;br /&gt;
==Follow the Examples==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
Next, comes our API information.  These resources outline the Ruby Phidget functions:&lt;br /&gt;
&lt;br /&gt;
{{UsingAPhidgetInCodeGeneral|both of which are available in Ruby|[http://www.phidgets.com/documentation/rubydoc Ruby API]}}&lt;br /&gt;
&lt;br /&gt;
===Example Flow===&lt;br /&gt;
&lt;br /&gt;
{{ExamplePseudocode|In Ruby, you can name these &#039;&#039;&#039;event&#039;&#039;&#039; 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. &amp;lt;br&amp;gt; &lt;br /&gt;
In the example code, the event functions common to all Phidgets are called things like &#039;&#039;&#039;AttachHandler()&#039;&#039;&#039; and &#039;&#039;&#039;DetachHandler()&#039;&#039;&#039;, etc.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
Other functions are given in the examples to show you more detail on using your Phidget.  For example, &#039;&#039;&#039;DeviceInitialize()&#039;&#039;&#039; will show what needs to be set up for your Phidget before using it.&lt;br /&gt;
|Creating a Phidget software object in Ruby is specific to the Phidget.  For a Phidget Spatial, for example, this would involve creating a {{Code|Spatial}} object.  The examples show how to do this and other API functions.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The object provides device specific methods and properties which are available from the API for your specific Phidget.|&lt;br /&gt;
[http://www.phidgets.com/documentation/rubydoc Ruby API]}}&lt;br /&gt;
&lt;br /&gt;
==Code Snippets==&lt;br /&gt;
&lt;br /&gt;
==Common Problems and Solutions/Workarounds==&lt;br /&gt;
&lt;br /&gt;
Here you can put various frequent problems and our recommended solutions.&lt;/div&gt;</summary>
		<author><name>Mphi</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs21/index.php?title=Language_-_Ruby&amp;diff=19086</id>
		<title>Language - Ruby</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs21/index.php?title=Language_-_Ruby&amp;diff=19086"/>
		<updated>2012-04-19T19:16:34Z</updated>

		<summary type="html">&lt;p&gt;Mphi: /* Use Our Examples */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Language]]&lt;br /&gt;
[[File:Icon-Ruby.png|64x64px||link=|alt=]]Ruby is an interpreted and object oriented scripting language with simple syntax created by Yukihiro Matsumoto.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
{{LanguageSupport|Ruby|the complete Phidget API, including events|all Phidget devices.|Linux and OS X|}}&lt;br /&gt;
&lt;br /&gt;
==Quick Downloads==&lt;br /&gt;
&lt;br /&gt;
Just need the Ruby documentation, drivers, libraries, and examples?  Here they are:&lt;br /&gt;
&lt;br /&gt;
===API Documentation===&lt;br /&gt;
*[http://www.phidgets.com/documentation/rubydoc API Manual]&lt;br /&gt;
&lt;br /&gt;
===Library and Example Code===&lt;br /&gt;
&lt;br /&gt;
*[http://www.phidgets.com/downloads/examples/rubylib_2.1.8.0.html phidgets-ffi @ RubyGems]&lt;br /&gt;
&lt;br /&gt;
===Drivers===&lt;br /&gt;
*[http://www.phidgets.com/downloads/libraries/libphidget_2.1.8.20111028.tar.gz Linux Source] &lt;br /&gt;
*[http://www.phidgets.com/downloads/libraries/Phidget_2.1.8.20111103.dmg OS X Drivers Installer]&lt;br /&gt;
&lt;br /&gt;
==Getting started with Ruby==&lt;br /&gt;
&lt;br /&gt;
If you are new to writing code for Phidgets, we recommend starting by running, then modifying existing examples. This will allow you to:&lt;br /&gt;
{{ExampleCodeReasons}}&lt;br /&gt;
&lt;br /&gt;
Instructions are divided up by operating system. Choose:&lt;br /&gt;
*[[#OS X |OS X]]&lt;br /&gt;
*[[#Linux | Linux]] (including PhidgetSBC)&lt;br /&gt;
&lt;br /&gt;
==OS X==&lt;br /&gt;
&lt;br /&gt;
Ruby has excellent support on OS X through the Mono framework. &lt;br /&gt;
&lt;br /&gt;
The first step in using Ruby on Mac is to install the Phidget libraries.  Compile and install them as explained on the [[Device List|getting started guide for your device]].  Then, the [[OS - OS X]] page also describes the different Phidget files, their installed locations, and their roles....&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Description of Library Files===&lt;br /&gt;
Ruby programs on Windows depend on the following files, which the installers above put onto your system:&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|phidget21.dll}}&amp;lt;/b&amp;gt; contains the actual Phidgets library, which is used at run-time.  By default, it is placed in {{Code|C:\Windows\System32}}.&lt;br /&gt;
You will also need one of the following two files, depending on the .NET framework version you are targeting:&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|/lib}}&amp;lt;/b&amp;gt; is the Phidgets library for .NET framework &amp;lt;i&amp;gt;&amp;lt;b&amp;gt;2.0&amp;lt;/b&amp;gt;&amp;lt;/i&amp;gt; or higher. Your compiler has to know where this file is. By default, it is placed into {{Code|C:\Program Files\Phidgets}}. You can either point your compiler to that location, or copy and link to it in a directory for your project workspace. &lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|examples}}&amp;lt;/b&amp;gt; is the Phidgets library for .NET framework &amp;lt;i&amp;gt;&amp;lt;b&amp;gt;1.1&amp;lt;/b&amp;gt;&amp;lt;/i&amp;gt;. Your compiler has to know where this file is. By default, is is placed into {{Code|C:\Program Files\Phidgets}}. You can either point your compiler to that location, or copy and link to it in a directory for your project workspace. &lt;br /&gt;
&lt;br /&gt;
If you do not want to use our installer, you can download the five [http://www.phidgets.com/downloads/libraries/Phidget21-windevel_2.1.8.20111220.zip file files].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Running the examples and writing your own code can be fairly compiler-specific, so we include instructions for each compiler below.&lt;br /&gt;
&lt;br /&gt;
=====Use Our Examples=====&lt;br /&gt;
&lt;br /&gt;
The phidgets-ffi gem will need to be installed. There phidgets-ffi gem relies on the ffi gem. So, if you do not already have it installed, please do so. A version of ffi 1.0.9 or greater is needed. Type the following to install ffi:&lt;br /&gt;
&lt;br /&gt;
{{Code|gem install ffi}}&lt;br /&gt;
&lt;br /&gt;
Next, please install the phidgets-ffi gem by typing the following into command line:&lt;br /&gt;
&lt;br /&gt;
{{Code|gem install phidgets-ffi}}&lt;br /&gt;
&lt;br /&gt;
For OS X systems, the gem will be installed into {{Code|/Library/Ruby/Gems/1.x/phidgets-ffi-x.x.x}}.&lt;br /&gt;
&lt;br /&gt;
For typical Linux systems, the gem will be installed into {{Code|var/lib/gems/1.9/gems/phidgets-ffi-x.x.x}}.&lt;br /&gt;
&lt;br /&gt;
Open a command line terminal and navigate to this folder. {{Code|cd}} into the {{Code|examples}} folder. Here, you will find all of the examples available for Ruby.  If you aren&#039;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]].&lt;br /&gt;
&lt;br /&gt;
The easiest way to confirm that your environment is set up properly will be to compile and run the {{Code|HelloWorld}} Ruby example.&lt;br /&gt;
&lt;br /&gt;
The only thing left to do is to run the examples! Type the following into command line:&lt;br /&gt;
&lt;br /&gt;
{{Code|ruby HelloWorld.rb}} &lt;br /&gt;
&lt;br /&gt;
This program will detect for devices that are attached/detached on the computer. Go ahead, and attach or detach your devices! Here is an example output:&lt;br /&gt;
&lt;br /&gt;
[[File:Ruby Linux HelloWorld Output.PNG|link=|alt=HelloWorld Output]]&lt;br /&gt;
&lt;br /&gt;
After confirming that the {{Code|HelloWorld}} example is working, you can proceed to run the example for your device. If you aren&#039;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 ensure that you have set your start up project to be the one that matches your device before compiling. &lt;br /&gt;
&lt;br /&gt;
Once you have the Ruby examples running, we have a [[#Follow the Examples|teaching]] section below to help you follow them.&lt;br /&gt;
&lt;br /&gt;
=====Write Your Own Code=====&lt;br /&gt;
&lt;br /&gt;
When you are building a project from scratch, or adding Phidget function calls to an existing project, you&#039;ll need to configure your environment to properly link the Phidget Ruby libraries. To begin: &lt;br /&gt;
&lt;br /&gt;
1. Generate a new  Ruby Windows Applications project with a descriptive name such as PhidgetTest. &lt;br /&gt;
&lt;br /&gt;
[[File:CSharp VS2005 New Project.PNG|link=|alt=New Project]]&lt;br /&gt;
&lt;br /&gt;
2. Add a reference to the .NET Phidgets library.&lt;br /&gt;
&lt;br /&gt;
[[File:CSharp VS2005 Add Reference.PNG|link=|alt=Add Reference]]&lt;br /&gt;
&lt;br /&gt;
3. Under the .NET tab, select {{Code|Phidget21.NET.dll}}.&lt;br /&gt;
If you used our installer, these files are installed in {{Code|C:\Program Files\Phidgets}}, by default. If it does not appear in this list, then you can browse to the Phidget Framework installation directory and add the file.&lt;br /&gt;
&lt;br /&gt;
[[File:CSharp VS2005 Add Reference 2.PNG|link=|alt=Add Reference]]&lt;br /&gt;
&lt;br /&gt;
4. Then, in your code, you will need to include the Phidget library:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=csharp&amp;gt;&lt;br /&gt;
  using Phidgets;&lt;br /&gt;
  using Phidgets.Events;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The project now has access to the Phidget21 function calls and you are ready to begin coding.&lt;br /&gt;
&lt;br /&gt;
The same [[#Follow the Examples|teaching ]] section which describes the examples also has further resources for programming your Phidget.&lt;br /&gt;
&lt;br /&gt;
==Linux==&lt;br /&gt;
&lt;br /&gt;
C# has support on Linux through the Mono framework.  &lt;br /&gt;
&lt;br /&gt;
The first step in using Ruby 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.&lt;br /&gt;
&lt;br /&gt;
==Follow the Examples==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
Next, comes our API information.  These resources outline the Ruby Phidget functions:&lt;br /&gt;
&lt;br /&gt;
{{UsingAPhidgetInCodeGeneral|both of which are available in Ruby|[http://www.phidgets.com/documentation/rubydoc Ruby API]}}&lt;br /&gt;
&lt;br /&gt;
===Example Flow===&lt;br /&gt;
&lt;br /&gt;
{{ExamplePseudocode|In Ruby, you can name these &#039;&#039;&#039;event&#039;&#039;&#039; 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. &amp;lt;br&amp;gt; &lt;br /&gt;
In the example code, the event functions common to all Phidgets are called things like &#039;&#039;&#039;AttachHandler()&#039;&#039;&#039; and &#039;&#039;&#039;DetachHandler()&#039;&#039;&#039;, etc.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
Other functions are given in the examples to show you more detail on using your Phidget.  For example, &#039;&#039;&#039;DeviceInitialize()&#039;&#039;&#039; will show what needs to be set up for your Phidget before using it.&lt;br /&gt;
|Creating a Phidget software object in Ruby is specific to the Phidget.  For a Phidget Spatial, for example, this would involve creating a {{Code|Spatial}} object.  The examples show how to do this and other API functions.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The object provides device specific methods and properties which are available from the API for your specific Phidget.|&lt;br /&gt;
[http://www.phidgets.com/documentation/rubydoc Ruby API]}}&lt;br /&gt;
&lt;br /&gt;
==Code Snippets==&lt;br /&gt;
&lt;br /&gt;
==Common Problems and Solutions/Workarounds==&lt;br /&gt;
&lt;br /&gt;
Here you can put various frequent problems and our recommended solutions.&lt;/div&gt;</summary>
		<author><name>Mphi</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs21/index.php?title=Language_-_Ruby&amp;diff=19085</id>
		<title>Language - Ruby</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs21/index.php?title=Language_-_Ruby&amp;diff=19085"/>
		<updated>2012-04-19T19:11:48Z</updated>

		<summary type="html">&lt;p&gt;Mphi: /* Use Our Examples */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Language]]&lt;br /&gt;
[[File:Icon-Ruby.png|64x64px||link=|alt=]]Ruby is an interpreted and object oriented scripting language with simple syntax created by Yukihiro Matsumoto.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
{{LanguageSupport|Ruby|the complete Phidget API, including events|all Phidget devices.|Linux and OS X|}}&lt;br /&gt;
&lt;br /&gt;
==Quick Downloads==&lt;br /&gt;
&lt;br /&gt;
Just need the Ruby documentation, drivers, libraries, and examples?  Here they are:&lt;br /&gt;
&lt;br /&gt;
===API Documentation===&lt;br /&gt;
*[http://www.phidgets.com/documentation/rubydoc API Manual]&lt;br /&gt;
&lt;br /&gt;
===Library and Example Code===&lt;br /&gt;
&lt;br /&gt;
*[http://www.phidgets.com/downloads/examples/rubylib_2.1.8.0.html phidgets-ffi @ RubyGems]&lt;br /&gt;
&lt;br /&gt;
===Drivers===&lt;br /&gt;
*[http://www.phidgets.com/downloads/libraries/libphidget_2.1.8.20111028.tar.gz Linux Source] &lt;br /&gt;
*[http://www.phidgets.com/downloads/libraries/Phidget_2.1.8.20111103.dmg OS X Drivers Installer]&lt;br /&gt;
&lt;br /&gt;
==Getting started with Ruby==&lt;br /&gt;
&lt;br /&gt;
If you are new to writing code for Phidgets, we recommend starting by running, then modifying existing examples. This will allow you to:&lt;br /&gt;
{{ExampleCodeReasons}}&lt;br /&gt;
&lt;br /&gt;
Instructions are divided up by operating system. Choose:&lt;br /&gt;
*[[#OS X |OS X]]&lt;br /&gt;
*[[#Linux | Linux]] (including PhidgetSBC)&lt;br /&gt;
&lt;br /&gt;
==OS X==&lt;br /&gt;
&lt;br /&gt;
Ruby has excellent support on OS X through the Mono framework. &lt;br /&gt;
&lt;br /&gt;
The first step in using Ruby on Mac is to install the Phidget libraries.  Compile and install them as explained on the [[Device List|getting started guide for your device]].  Then, the [[OS - OS X]] page also describes the different Phidget files, their installed locations, and their roles....&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Description of Library Files===&lt;br /&gt;
Ruby programs on Windows depend on the following files, which the installers above put onto your system:&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|phidget21.dll}}&amp;lt;/b&amp;gt; contains the actual Phidgets library, which is used at run-time.  By default, it is placed in {{Code|C:\Windows\System32}}.&lt;br /&gt;
You will also need one of the following two files, depending on the .NET framework version you are targeting:&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|/lib}}&amp;lt;/b&amp;gt; is the Phidgets library for .NET framework &amp;lt;i&amp;gt;&amp;lt;b&amp;gt;2.0&amp;lt;/b&amp;gt;&amp;lt;/i&amp;gt; or higher. Your compiler has to know where this file is. By default, it is placed into {{Code|C:\Program Files\Phidgets}}. You can either point your compiler to that location, or copy and link to it in a directory for your project workspace. &lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|examples}}&amp;lt;/b&amp;gt; is the Phidgets library for .NET framework &amp;lt;i&amp;gt;&amp;lt;b&amp;gt;1.1&amp;lt;/b&amp;gt;&amp;lt;/i&amp;gt;. Your compiler has to know where this file is. By default, is is placed into {{Code|C:\Program Files\Phidgets}}. You can either point your compiler to that location, or copy and link to it in a directory for your project workspace. &lt;br /&gt;
&lt;br /&gt;
If you do not want to use our installer, you can download the five [http://www.phidgets.com/downloads/libraries/Phidget21-windevel_2.1.8.20111220.zip file files].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Running the examples and writing your own code can be fairly compiler-specific, so we include instructions for each compiler below.&lt;br /&gt;
&lt;br /&gt;
=====Use Our Examples=====&lt;br /&gt;
&lt;br /&gt;
To start, please install the phidgets-ffi gem by typing the following into command line:&lt;br /&gt;
&lt;br /&gt;
{{Code|gem install phidgets-ffi}}&lt;br /&gt;
&lt;br /&gt;
For OS X systems, the gem will be installed into {{Code|/Library/Ruby/Gems/1.x/phidgets-ffi-x.x.x}}.&lt;br /&gt;
&lt;br /&gt;
For typical Linux systems, the gem will be installed into {{Code|var/lib/gems/1.9/gems/phidgets-ffi-x.x.x}}.&lt;br /&gt;
&lt;br /&gt;
Open a command line terminal and navigate to this folder. {{Code|cd}} into the {{Code|examples}} folder. Here, you will find all of the examples available for Ruby.  If you aren&#039;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]].&lt;br /&gt;
&lt;br /&gt;
The easiest way to confirm that your environment is set up properly will be to compile and run the {{Code|HelloWorld}} Ruby example.&lt;br /&gt;
&lt;br /&gt;
The only thing left to do is to run the examples! Type the following into command line:&lt;br /&gt;
&lt;br /&gt;
{{Code|ruby HelloWorld.rb}} &lt;br /&gt;
&lt;br /&gt;
This program will detect for devices that are attached/detached on the computer. Go ahead, and attach or detach your devices! Here is an example output:&lt;br /&gt;
&lt;br /&gt;
[[File:Ruby Linux HelloWorld Output.PNG|link=|alt=HelloWorld Output]]&lt;br /&gt;
&lt;br /&gt;
After confirming that the {{Code|HelloWorld}} example is working, you can proceed to run the example for your device. If you aren&#039;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 ensure that you have set your start up project to be the one that matches your device before compiling. &lt;br /&gt;
&lt;br /&gt;
Once you have the Ruby examples running, we have a [[#Follow the Examples|teaching]] section below to help you follow them.&lt;br /&gt;
&lt;br /&gt;
=====Write Your Own Code=====&lt;br /&gt;
&lt;br /&gt;
When you are building a project from scratch, or adding Phidget function calls to an existing project, you&#039;ll need to configure your environment to properly link the Phidget Ruby libraries. To begin: &lt;br /&gt;
&lt;br /&gt;
1. Generate a new  Ruby Windows Applications project with a descriptive name such as PhidgetTest. &lt;br /&gt;
&lt;br /&gt;
[[File:CSharp VS2005 New Project.PNG|link=|alt=New Project]]&lt;br /&gt;
&lt;br /&gt;
2. Add a reference to the .NET Phidgets library.&lt;br /&gt;
&lt;br /&gt;
[[File:CSharp VS2005 Add Reference.PNG|link=|alt=Add Reference]]&lt;br /&gt;
&lt;br /&gt;
3. Under the .NET tab, select {{Code|Phidget21.NET.dll}}.&lt;br /&gt;
If you used our installer, these files are installed in {{Code|C:\Program Files\Phidgets}}, by default. If it does not appear in this list, then you can browse to the Phidget Framework installation directory and add the file.&lt;br /&gt;
&lt;br /&gt;
[[File:CSharp VS2005 Add Reference 2.PNG|link=|alt=Add Reference]]&lt;br /&gt;
&lt;br /&gt;
4. Then, in your code, you will need to include the Phidget library:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=csharp&amp;gt;&lt;br /&gt;
  using Phidgets;&lt;br /&gt;
  using Phidgets.Events;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The project now has access to the Phidget21 function calls and you are ready to begin coding.&lt;br /&gt;
&lt;br /&gt;
The same [[#Follow the Examples|teaching ]] section which describes the examples also has further resources for programming your Phidget.&lt;br /&gt;
&lt;br /&gt;
==Linux==&lt;br /&gt;
&lt;br /&gt;
C# has support on Linux through the Mono framework.  &lt;br /&gt;
&lt;br /&gt;
The first step in using Ruby 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.&lt;br /&gt;
&lt;br /&gt;
==Follow the Examples==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
Next, comes our API information.  These resources outline the Ruby Phidget functions:&lt;br /&gt;
&lt;br /&gt;
{{UsingAPhidgetInCodeGeneral|both of which are available in Ruby|[http://www.phidgets.com/documentation/rubydoc Ruby API]}}&lt;br /&gt;
&lt;br /&gt;
===Example Flow===&lt;br /&gt;
&lt;br /&gt;
{{ExamplePseudocode|In Ruby, you can name these &#039;&#039;&#039;event&#039;&#039;&#039; 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. &amp;lt;br&amp;gt; &lt;br /&gt;
In the example code, the event functions common to all Phidgets are called things like &#039;&#039;&#039;AttachHandler()&#039;&#039;&#039; and &#039;&#039;&#039;DetachHandler()&#039;&#039;&#039;, etc.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
Other functions are given in the examples to show you more detail on using your Phidget.  For example, &#039;&#039;&#039;DeviceInitialize()&#039;&#039;&#039; will show what needs to be set up for your Phidget before using it.&lt;br /&gt;
|Creating a Phidget software object in Ruby is specific to the Phidget.  For a Phidget Spatial, for example, this would involve creating a {{Code|Spatial}} object.  The examples show how to do this and other API functions.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The object provides device specific methods and properties which are available from the API for your specific Phidget.|&lt;br /&gt;
[http://www.phidgets.com/documentation/rubydoc Ruby API]}}&lt;br /&gt;
&lt;br /&gt;
==Code Snippets==&lt;br /&gt;
&lt;br /&gt;
==Common Problems and Solutions/Workarounds==&lt;br /&gt;
&lt;br /&gt;
Here you can put various frequent problems and our recommended solutions.&lt;/div&gt;</summary>
		<author><name>Mphi</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs21/index.php?title=Language_-_Ruby&amp;diff=19084</id>
		<title>Language - Ruby</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs21/index.php?title=Language_-_Ruby&amp;diff=19084"/>
		<updated>2012-04-19T19:09:17Z</updated>

		<summary type="html">&lt;p&gt;Mphi: /* Use Our Examples */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Language]]&lt;br /&gt;
[[File:Icon-Ruby.png|64x64px||link=|alt=]]Ruby is an interpreted and object oriented scripting language with simple syntax created by Yukihiro Matsumoto.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
{{LanguageSupport|Ruby|the complete Phidget API, including events|all Phidget devices.|Linux and OS X|}}&lt;br /&gt;
&lt;br /&gt;
==Quick Downloads==&lt;br /&gt;
&lt;br /&gt;
Just need the Ruby documentation, drivers, libraries, and examples?  Here they are:&lt;br /&gt;
&lt;br /&gt;
===API Documentation===&lt;br /&gt;
*[http://www.phidgets.com/documentation/rubydoc API Manual]&lt;br /&gt;
&lt;br /&gt;
===Library and Example Code===&lt;br /&gt;
&lt;br /&gt;
*[http://www.phidgets.com/downloads/examples/rubylib_2.1.8.0.html phidgets-ffi @ RubyGems]&lt;br /&gt;
&lt;br /&gt;
===Drivers===&lt;br /&gt;
*[http://www.phidgets.com/downloads/libraries/libphidget_2.1.8.20111028.tar.gz Linux Source] &lt;br /&gt;
*[http://www.phidgets.com/downloads/libraries/Phidget_2.1.8.20111103.dmg OS X Drivers Installer]&lt;br /&gt;
&lt;br /&gt;
==Getting started with Ruby==&lt;br /&gt;
&lt;br /&gt;
If you are new to writing code for Phidgets, we recommend starting by running, then modifying existing examples. This will allow you to:&lt;br /&gt;
{{ExampleCodeReasons}}&lt;br /&gt;
&lt;br /&gt;
Instructions are divided up by operating system. Choose:&lt;br /&gt;
*[[#OS X |OS X]]&lt;br /&gt;
*[[#Linux | Linux]] (including PhidgetSBC)&lt;br /&gt;
&lt;br /&gt;
==OS X==&lt;br /&gt;
&lt;br /&gt;
Ruby has excellent support on OS X through the Mono framework. &lt;br /&gt;
&lt;br /&gt;
The first step in using Ruby on Mac is to install the Phidget libraries.  Compile and install them as explained on the [[Device List|getting started guide for your device]].  Then, the [[OS - OS X]] page also describes the different Phidget files, their installed locations, and their roles....&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Description of Library Files===&lt;br /&gt;
Ruby programs on Windows depend on the following files, which the installers above put onto your system:&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|phidget21.dll}}&amp;lt;/b&amp;gt; contains the actual Phidgets library, which is used at run-time.  By default, it is placed in {{Code|C:\Windows\System32}}.&lt;br /&gt;
You will also need one of the following two files, depending on the .NET framework version you are targeting:&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|/lib}}&amp;lt;/b&amp;gt; is the Phidgets library for .NET framework &amp;lt;i&amp;gt;&amp;lt;b&amp;gt;2.0&amp;lt;/b&amp;gt;&amp;lt;/i&amp;gt; or higher. Your compiler has to know where this file is. By default, it is placed into {{Code|C:\Program Files\Phidgets}}. You can either point your compiler to that location, or copy and link to it in a directory for your project workspace. &lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|examples}}&amp;lt;/b&amp;gt; is the Phidgets library for .NET framework &amp;lt;i&amp;gt;&amp;lt;b&amp;gt;1.1&amp;lt;/b&amp;gt;&amp;lt;/i&amp;gt;. Your compiler has to know where this file is. By default, is is placed into {{Code|C:\Program Files\Phidgets}}. You can either point your compiler to that location, or copy and link to it in a directory for your project workspace. &lt;br /&gt;
&lt;br /&gt;
If you do not want to use our installer, you can download the five [http://www.phidgets.com/downloads/libraries/Phidget21-windevel_2.1.8.20111220.zip file files].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Running the examples and writing your own code can be fairly compiler-specific, so we include instructions for each compiler below.&lt;br /&gt;
&lt;br /&gt;
=====Use Our Examples=====&lt;br /&gt;
&lt;br /&gt;
To start, please install the phidgets-ffi gem by typing the following into command line:&lt;br /&gt;
&lt;br /&gt;
{{Code|gem install phidgets-ffi}}&lt;br /&gt;
&lt;br /&gt;
For OS X systems, the gem will be installed into {{Code|/Library/Ruby/Gems/1.x/phidgets-ffi-x.x.x}}&lt;br /&gt;
&lt;br /&gt;
For typical Linux systems, the gem will be installed into {{Code|var/lib/gems/1.9/gems/phidgets-ffi-x.x.x}}&lt;br /&gt;
&lt;br /&gt;
Open a command line terminal and navigate to this folder. {{Code|cd}} into the {{Code|examples}} folder. Here, you will find all of the examples available for Ruby.  If you aren&#039;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]].&lt;br /&gt;
&lt;br /&gt;
The easiest way to confirm that your environment is set up properly will be to compile and run the {{Code|HelloWorld}} Ruby example.&lt;br /&gt;
&lt;br /&gt;
The only thing left to do is to run the examples! Type the following into command line:&lt;br /&gt;
&lt;br /&gt;
{{Code|ruby HelloWorld.rb}} &lt;br /&gt;
&lt;br /&gt;
This program will detect for devices that are attached/detached on the computer. Go ahead, and attach or detach your devices! Here is an example output:&lt;br /&gt;
&lt;br /&gt;
[[File:Ruby Linux HelloWorld Output.PNG|link=|alt=HelloWorld Output]]&lt;br /&gt;
&lt;br /&gt;
After confirming that the {{Code|HelloWorld}} example is working, you can proceed to run the example for your device. If you aren&#039;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 ensure that you have set your start up project to be the one that matches your device before compiling. &lt;br /&gt;
&lt;br /&gt;
Once you have the Ruby examples running, we have a [[#Follow the Examples|teaching]] section below to help you follow them.&lt;br /&gt;
&lt;br /&gt;
=====Write Your Own Code=====&lt;br /&gt;
&lt;br /&gt;
When you are building a project from scratch, or adding Phidget function calls to an existing project, you&#039;ll need to configure your environment to properly link the Phidget Ruby libraries. To begin: &lt;br /&gt;
&lt;br /&gt;
1. Generate a new  Ruby Windows Applications project with a descriptive name such as PhidgetTest. &lt;br /&gt;
&lt;br /&gt;
[[File:CSharp VS2005 New Project.PNG|link=|alt=New Project]]&lt;br /&gt;
&lt;br /&gt;
2. Add a reference to the .NET Phidgets library.&lt;br /&gt;
&lt;br /&gt;
[[File:CSharp VS2005 Add Reference.PNG|link=|alt=Add Reference]]&lt;br /&gt;
&lt;br /&gt;
3. Under the .NET tab, select {{Code|Phidget21.NET.dll}}.&lt;br /&gt;
If you used our installer, these files are installed in {{Code|C:\Program Files\Phidgets}}, by default. If it does not appear in this list, then you can browse to the Phidget Framework installation directory and add the file.&lt;br /&gt;
&lt;br /&gt;
[[File:CSharp VS2005 Add Reference 2.PNG|link=|alt=Add Reference]]&lt;br /&gt;
&lt;br /&gt;
4. Then, in your code, you will need to include the Phidget library:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=csharp&amp;gt;&lt;br /&gt;
  using Phidgets;&lt;br /&gt;
  using Phidgets.Events;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The project now has access to the Phidget21 function calls and you are ready to begin coding.&lt;br /&gt;
&lt;br /&gt;
The same [[#Follow the Examples|teaching ]] section which describes the examples also has further resources for programming your Phidget.&lt;br /&gt;
&lt;br /&gt;
==Linux==&lt;br /&gt;
&lt;br /&gt;
C# has support on Linux through the Mono framework.  &lt;br /&gt;
&lt;br /&gt;
The first step in using Ruby 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.&lt;br /&gt;
&lt;br /&gt;
==Follow the Examples==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
Next, comes our API information.  These resources outline the Ruby Phidget functions:&lt;br /&gt;
&lt;br /&gt;
{{UsingAPhidgetInCodeGeneral|both of which are available in Ruby|[http://www.phidgets.com/documentation/rubydoc Ruby API]}}&lt;br /&gt;
&lt;br /&gt;
===Example Flow===&lt;br /&gt;
&lt;br /&gt;
{{ExamplePseudocode|In Ruby, you can name these &#039;&#039;&#039;event&#039;&#039;&#039; 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. &amp;lt;br&amp;gt; &lt;br /&gt;
In the example code, the event functions common to all Phidgets are called things like &#039;&#039;&#039;AttachHandler()&#039;&#039;&#039; and &#039;&#039;&#039;DetachHandler()&#039;&#039;&#039;, etc.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
Other functions are given in the examples to show you more detail on using your Phidget.  For example, &#039;&#039;&#039;DeviceInitialize()&#039;&#039;&#039; will show what needs to be set up for your Phidget before using it.&lt;br /&gt;
|Creating a Phidget software object in Ruby is specific to the Phidget.  For a Phidget Spatial, for example, this would involve creating a {{Code|Spatial}} object.  The examples show how to do this and other API functions.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The object provides device specific methods and properties which are available from the API for your specific Phidget.|&lt;br /&gt;
[http://www.phidgets.com/documentation/rubydoc Ruby API]}}&lt;br /&gt;
&lt;br /&gt;
==Code Snippets==&lt;br /&gt;
&lt;br /&gt;
==Common Problems and Solutions/Workarounds==&lt;br /&gt;
&lt;br /&gt;
Here you can put various frequent problems and our recommended solutions.&lt;/div&gt;</summary>
		<author><name>Mphi</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs21/index.php?title=Language_-_Ruby&amp;diff=19083</id>
		<title>Language - Ruby</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs21/index.php?title=Language_-_Ruby&amp;diff=19083"/>
		<updated>2012-04-19T19:08:54Z</updated>

		<summary type="html">&lt;p&gt;Mphi: /* Use Our Examples */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Language]]&lt;br /&gt;
[[File:Icon-Ruby.png|64x64px||link=|alt=]]Ruby is an interpreted and object oriented scripting language with simple syntax created by Yukihiro Matsumoto.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
{{LanguageSupport|Ruby|the complete Phidget API, including events|all Phidget devices.|Linux and OS X|}}&lt;br /&gt;
&lt;br /&gt;
==Quick Downloads==&lt;br /&gt;
&lt;br /&gt;
Just need the Ruby documentation, drivers, libraries, and examples?  Here they are:&lt;br /&gt;
&lt;br /&gt;
===API Documentation===&lt;br /&gt;
*[http://www.phidgets.com/documentation/rubydoc API Manual]&lt;br /&gt;
&lt;br /&gt;
===Library and Example Code===&lt;br /&gt;
&lt;br /&gt;
*[http://www.phidgets.com/downloads/examples/rubylib_2.1.8.0.html phidgets-ffi @ RubyGems]&lt;br /&gt;
&lt;br /&gt;
===Drivers===&lt;br /&gt;
*[http://www.phidgets.com/downloads/libraries/libphidget_2.1.8.20111028.tar.gz Linux Source] &lt;br /&gt;
*[http://www.phidgets.com/downloads/libraries/Phidget_2.1.8.20111103.dmg OS X Drivers Installer]&lt;br /&gt;
&lt;br /&gt;
==Getting started with Ruby==&lt;br /&gt;
&lt;br /&gt;
If you are new to writing code for Phidgets, we recommend starting by running, then modifying existing examples. This will allow you to:&lt;br /&gt;
{{ExampleCodeReasons}}&lt;br /&gt;
&lt;br /&gt;
Instructions are divided up by operating system. Choose:&lt;br /&gt;
*[[#OS X |OS X]]&lt;br /&gt;
*[[#Linux | Linux]] (including PhidgetSBC)&lt;br /&gt;
&lt;br /&gt;
==OS X==&lt;br /&gt;
&lt;br /&gt;
Ruby has excellent support on OS X through the Mono framework. &lt;br /&gt;
&lt;br /&gt;
The first step in using Ruby on Mac is to install the Phidget libraries.  Compile and install them as explained on the [[Device List|getting started guide for your device]].  Then, the [[OS - OS X]] page also describes the different Phidget files, their installed locations, and their roles....&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Description of Library Files===&lt;br /&gt;
Ruby programs on Windows depend on the following files, which the installers above put onto your system:&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|phidget21.dll}}&amp;lt;/b&amp;gt; contains the actual Phidgets library, which is used at run-time.  By default, it is placed in {{Code|C:\Windows\System32}}.&lt;br /&gt;
You will also need one of the following two files, depending on the .NET framework version you are targeting:&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|/lib}}&amp;lt;/b&amp;gt; is the Phidgets library for .NET framework &amp;lt;i&amp;gt;&amp;lt;b&amp;gt;2.0&amp;lt;/b&amp;gt;&amp;lt;/i&amp;gt; or higher. Your compiler has to know where this file is. By default, it is placed into {{Code|C:\Program Files\Phidgets}}. You can either point your compiler to that location, or copy and link to it in a directory for your project workspace. &lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|examples}}&amp;lt;/b&amp;gt; is the Phidgets library for .NET framework &amp;lt;i&amp;gt;&amp;lt;b&amp;gt;1.1&amp;lt;/b&amp;gt;&amp;lt;/i&amp;gt;. Your compiler has to know where this file is. By default, is is placed into {{Code|C:\Program Files\Phidgets}}. You can either point your compiler to that location, or copy and link to it in a directory for your project workspace. &lt;br /&gt;
&lt;br /&gt;
If you do not want to use our installer, you can download the five [http://www.phidgets.com/downloads/libraries/Phidget21-windevel_2.1.8.20111220.zip file files].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Running the examples and writing your own code can be fairly compiler-specific, so we include instructions for each compiler below.&lt;br /&gt;
&lt;br /&gt;
=====Use Our Examples=====&lt;br /&gt;
&lt;br /&gt;
To start, please install the phidgets-ffi gem by typing the following into command line:&lt;br /&gt;
&lt;br /&gt;
{{Code|gem install phidgets-ffi}}&lt;br /&gt;
&lt;br /&gt;
For OS X systems, the gem will be installed into {{Code|/Library/Ruby/Gems/1.x/phidgets-ffi-x.x.x}}&lt;br /&gt;
For typical Linux systems, the gem will be installed into {{Code|var/lib/gems/1.9/gems/phidgets-ffi-x.x.x}}&lt;br /&gt;
&lt;br /&gt;
Open a command line terminal and navigate to this folder. {{Code|cd}} into the {{Code|examples}} folder. Here, you will find all of the examples available for Ruby.  If you aren&#039;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]].&lt;br /&gt;
&lt;br /&gt;
The easiest way to confirm that your environment is set up properly will be to compile and run the {{Code|HelloWorld}} Ruby example.&lt;br /&gt;
&lt;br /&gt;
The only thing left to do is to run the examples! Type the following into command line:&lt;br /&gt;
&lt;br /&gt;
{{Code|ruby HelloWorld.rb}} &lt;br /&gt;
&lt;br /&gt;
This program will detect for devices that are attached/detached on the computer. Go ahead, and attach or detach your devices! Here is an example output:&lt;br /&gt;
&lt;br /&gt;
[[File:Ruby Linux HelloWorld Output.PNG|link=|alt=HelloWorld Output]]&lt;br /&gt;
&lt;br /&gt;
After confirming that the {{Code|HelloWorld}} example is working, you can proceed to run the example for your device. If you aren&#039;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 ensure that you have set your start up project to be the one that matches your device before compiling. &lt;br /&gt;
&lt;br /&gt;
Once you have the Ruby examples running, we have a [[#Follow the Examples|teaching]] section below to help you follow them.&lt;br /&gt;
&lt;br /&gt;
=====Write Your Own Code=====&lt;br /&gt;
&lt;br /&gt;
When you are building a project from scratch, or adding Phidget function calls to an existing project, you&#039;ll need to configure your environment to properly link the Phidget Ruby libraries. To begin: &lt;br /&gt;
&lt;br /&gt;
1. Generate a new  Ruby Windows Applications project with a descriptive name such as PhidgetTest. &lt;br /&gt;
&lt;br /&gt;
[[File:CSharp VS2005 New Project.PNG|link=|alt=New Project]]&lt;br /&gt;
&lt;br /&gt;
2. Add a reference to the .NET Phidgets library.&lt;br /&gt;
&lt;br /&gt;
[[File:CSharp VS2005 Add Reference.PNG|link=|alt=Add Reference]]&lt;br /&gt;
&lt;br /&gt;
3. Under the .NET tab, select {{Code|Phidget21.NET.dll}}.&lt;br /&gt;
If you used our installer, these files are installed in {{Code|C:\Program Files\Phidgets}}, by default. If it does not appear in this list, then you can browse to the Phidget Framework installation directory and add the file.&lt;br /&gt;
&lt;br /&gt;
[[File:CSharp VS2005 Add Reference 2.PNG|link=|alt=Add Reference]]&lt;br /&gt;
&lt;br /&gt;
4. Then, in your code, you will need to include the Phidget library:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=csharp&amp;gt;&lt;br /&gt;
  using Phidgets;&lt;br /&gt;
  using Phidgets.Events;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The project now has access to the Phidget21 function calls and you are ready to begin coding.&lt;br /&gt;
&lt;br /&gt;
The same [[#Follow the Examples|teaching ]] section which describes the examples also has further resources for programming your Phidget.&lt;br /&gt;
&lt;br /&gt;
==Linux==&lt;br /&gt;
&lt;br /&gt;
C# has support on Linux through the Mono framework.  &lt;br /&gt;
&lt;br /&gt;
The first step in using Ruby 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.&lt;br /&gt;
&lt;br /&gt;
==Follow the Examples==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
Next, comes our API information.  These resources outline the Ruby Phidget functions:&lt;br /&gt;
&lt;br /&gt;
{{UsingAPhidgetInCodeGeneral|both of which are available in Ruby|[http://www.phidgets.com/documentation/rubydoc Ruby API]}}&lt;br /&gt;
&lt;br /&gt;
===Example Flow===&lt;br /&gt;
&lt;br /&gt;
{{ExamplePseudocode|In Ruby, you can name these &#039;&#039;&#039;event&#039;&#039;&#039; 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. &amp;lt;br&amp;gt; &lt;br /&gt;
In the example code, the event functions common to all Phidgets are called things like &#039;&#039;&#039;AttachHandler()&#039;&#039;&#039; and &#039;&#039;&#039;DetachHandler()&#039;&#039;&#039;, etc.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
Other functions are given in the examples to show you more detail on using your Phidget.  For example, &#039;&#039;&#039;DeviceInitialize()&#039;&#039;&#039; will show what needs to be set up for your Phidget before using it.&lt;br /&gt;
|Creating a Phidget software object in Ruby is specific to the Phidget.  For a Phidget Spatial, for example, this would involve creating a {{Code|Spatial}} object.  The examples show how to do this and other API functions.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The object provides device specific methods and properties which are available from the API for your specific Phidget.|&lt;br /&gt;
[http://www.phidgets.com/documentation/rubydoc Ruby API]}}&lt;br /&gt;
&lt;br /&gt;
==Code Snippets==&lt;br /&gt;
&lt;br /&gt;
==Common Problems and Solutions/Workarounds==&lt;br /&gt;
&lt;br /&gt;
Here you can put various frequent problems and our recommended solutions.&lt;/div&gt;</summary>
		<author><name>Mphi</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs21/index.php?title=File:Ruby_Linux_HelloWorld_Output.PNG&amp;diff=19082</id>
		<title>File:Ruby Linux HelloWorld Output.PNG</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs21/index.php?title=File:Ruby_Linux_HelloWorld_Output.PNG&amp;diff=19082"/>
		<updated>2012-04-19T19:08:03Z</updated>

		<summary type="html">&lt;p&gt;Mphi: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Mphi</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs21/index.php?title=Language_-_Ruby&amp;diff=19081</id>
		<title>Language - Ruby</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs21/index.php?title=Language_-_Ruby&amp;diff=19081"/>
		<updated>2012-04-19T19:05:50Z</updated>

		<summary type="html">&lt;p&gt;Mphi: /* Use Our Examples */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Language]]&lt;br /&gt;
[[File:Icon-Ruby.png|64x64px||link=|alt=]]Ruby is an interpreted and object oriented scripting language with simple syntax created by Yukihiro Matsumoto.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
{{LanguageSupport|Ruby|the complete Phidget API, including events|all Phidget devices.|Linux and OS X|}}&lt;br /&gt;
&lt;br /&gt;
==Quick Downloads==&lt;br /&gt;
&lt;br /&gt;
Just need the Ruby documentation, drivers, libraries, and examples?  Here they are:&lt;br /&gt;
&lt;br /&gt;
===API Documentation===&lt;br /&gt;
*[http://www.phidgets.com/documentation/rubydoc API Manual]&lt;br /&gt;
&lt;br /&gt;
===Library and Example Code===&lt;br /&gt;
&lt;br /&gt;
*[http://www.phidgets.com/downloads/examples/rubylib_2.1.8.0.html phidgets-ffi @ RubyGems]&lt;br /&gt;
&lt;br /&gt;
===Drivers===&lt;br /&gt;
*[http://www.phidgets.com/downloads/libraries/libphidget_2.1.8.20111028.tar.gz Linux Source] &lt;br /&gt;
*[http://www.phidgets.com/downloads/libraries/Phidget_2.1.8.20111103.dmg OS X Drivers Installer]&lt;br /&gt;
&lt;br /&gt;
==Getting started with Ruby==&lt;br /&gt;
&lt;br /&gt;
If you are new to writing code for Phidgets, we recommend starting by running, then modifying existing examples. This will allow you to:&lt;br /&gt;
{{ExampleCodeReasons}}&lt;br /&gt;
&lt;br /&gt;
Instructions are divided up by operating system. Choose:&lt;br /&gt;
*[[#OS X |OS X]]&lt;br /&gt;
*[[#Linux | Linux]] (including PhidgetSBC)&lt;br /&gt;
&lt;br /&gt;
==OS X==&lt;br /&gt;
&lt;br /&gt;
Ruby has excellent support on OS X through the Mono framework. &lt;br /&gt;
&lt;br /&gt;
The first step in using Ruby on Mac is to install the Phidget libraries.  Compile and install them as explained on the [[Device List|getting started guide for your device]].  Then, the [[OS - OS X]] page also describes the different Phidget files, their installed locations, and their roles....&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Description of Library Files===&lt;br /&gt;
Ruby programs on Windows depend on the following files, which the installers above put onto your system:&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|phidget21.dll}}&amp;lt;/b&amp;gt; contains the actual Phidgets library, which is used at run-time.  By default, it is placed in {{Code|C:\Windows\System32}}.&lt;br /&gt;
You will also need one of the following two files, depending on the .NET framework version you are targeting:&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|/lib}}&amp;lt;/b&amp;gt; is the Phidgets library for .NET framework &amp;lt;i&amp;gt;&amp;lt;b&amp;gt;2.0&amp;lt;/b&amp;gt;&amp;lt;/i&amp;gt; or higher. Your compiler has to know where this file is. By default, it is placed into {{Code|C:\Program Files\Phidgets}}. You can either point your compiler to that location, or copy and link to it in a directory for your project workspace. &lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|examples}}&amp;lt;/b&amp;gt; is the Phidgets library for .NET framework &amp;lt;i&amp;gt;&amp;lt;b&amp;gt;1.1&amp;lt;/b&amp;gt;&amp;lt;/i&amp;gt;. Your compiler has to know where this file is. By default, is is placed into {{Code|C:\Program Files\Phidgets}}. You can either point your compiler to that location, or copy and link to it in a directory for your project workspace. &lt;br /&gt;
&lt;br /&gt;
If you do not want to use our installer, you can download the five [http://www.phidgets.com/downloads/libraries/Phidget21-windevel_2.1.8.20111220.zip file files].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Running the examples and writing your own code can be fairly compiler-specific, so we include instructions for each compiler below.&lt;br /&gt;
&lt;br /&gt;
=====Use Our Examples=====&lt;br /&gt;
&lt;br /&gt;
To start, please install the phidgets-ffi gem by typing the following into command line:&lt;br /&gt;
&lt;br /&gt;
{{Code|gem install phidgets-ffi}}&lt;br /&gt;
&lt;br /&gt;
For OS X systems, the gem will be installed into {{Code|/Library/Ruby/Gems/1.x/phidgets-ffi-x.x.x}}&lt;br /&gt;
For typical Linux systems, the gem will be installed into {{Code|var/lib/gems/1.9/gems/phidgets-ffi-x.x.x}}&lt;br /&gt;
&lt;br /&gt;
Open a command line terminal and navigate to this folder. {{Code|cd}} into the {{Code|examples}} folder. Here, you will find all of the examples available for Ruby.  If you aren&#039;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]].&lt;br /&gt;
&lt;br /&gt;
The easiest way to confirm that your environment is set up properly will be to compile and run the {{Code|HelloWorld}} Ruby example.&lt;br /&gt;
&lt;br /&gt;
The only thing left to do is to run the examples! Type the following into command line:&lt;br /&gt;
&lt;br /&gt;
{{Code|ruby HelloWorld.rb}} &lt;br /&gt;
&lt;br /&gt;
[[File:CSharp VS2005 Run.PNG|link=|alt=Run]]&lt;br /&gt;
&lt;br /&gt;
This program will detect for devices that are attached/detached on the computer. Go ahead, and attach or detach your devices! Here is an example output:&lt;br /&gt;
&lt;br /&gt;
[[File:CSharp VS2005 HelloWorld Output.PNG|link=|alt=HelloWorld Output]]&lt;br /&gt;
&lt;br /&gt;
After confirming that the {{Code|HelloWorld}} example is working, you can proceed to run the example for your device. If you aren&#039;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 ensure that you have set your start up project to be the one that matches your device before compiling. &lt;br /&gt;
&lt;br /&gt;
Once you have the Ruby examples running, we have a [[#Follow the Examples|teaching]] section below to help you follow them.&lt;br /&gt;
&lt;br /&gt;
=====Write Your Own Code=====&lt;br /&gt;
&lt;br /&gt;
When you are building a project from scratch, or adding Phidget function calls to an existing project, you&#039;ll need to configure your environment to properly link the Phidget Ruby libraries. To begin: &lt;br /&gt;
&lt;br /&gt;
1. Generate a new  Ruby Windows Applications project with a descriptive name such as PhidgetTest. &lt;br /&gt;
&lt;br /&gt;
[[File:CSharp VS2005 New Project.PNG|link=|alt=New Project]]&lt;br /&gt;
&lt;br /&gt;
2. Add a reference to the .NET Phidgets library.&lt;br /&gt;
&lt;br /&gt;
[[File:CSharp VS2005 Add Reference.PNG|link=|alt=Add Reference]]&lt;br /&gt;
&lt;br /&gt;
3. Under the .NET tab, select {{Code|Phidget21.NET.dll}}.&lt;br /&gt;
If you used our installer, these files are installed in {{Code|C:\Program Files\Phidgets}}, by default. If it does not appear in this list, then you can browse to the Phidget Framework installation directory and add the file.&lt;br /&gt;
&lt;br /&gt;
[[File:CSharp VS2005 Add Reference 2.PNG|link=|alt=Add Reference]]&lt;br /&gt;
&lt;br /&gt;
4. Then, in your code, you will need to include the Phidget library:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=csharp&amp;gt;&lt;br /&gt;
  using Phidgets;&lt;br /&gt;
  using Phidgets.Events;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The project now has access to the Phidget21 function calls and you are ready to begin coding.&lt;br /&gt;
&lt;br /&gt;
The same [[#Follow the Examples|teaching ]] section which describes the examples also has further resources for programming your Phidget.&lt;br /&gt;
&lt;br /&gt;
==Linux==&lt;br /&gt;
&lt;br /&gt;
C# has support on Linux through the Mono framework.  &lt;br /&gt;
&lt;br /&gt;
The first step in using Ruby 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.&lt;br /&gt;
&lt;br /&gt;
==Follow the Examples==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
Next, comes our API information.  These resources outline the Ruby Phidget functions:&lt;br /&gt;
&lt;br /&gt;
{{UsingAPhidgetInCodeGeneral|both of which are available in Ruby|[http://www.phidgets.com/documentation/rubydoc Ruby API]}}&lt;br /&gt;
&lt;br /&gt;
===Example Flow===&lt;br /&gt;
&lt;br /&gt;
{{ExamplePseudocode|In Ruby, you can name these &#039;&#039;&#039;event&#039;&#039;&#039; 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. &amp;lt;br&amp;gt; &lt;br /&gt;
In the example code, the event functions common to all Phidgets are called things like &#039;&#039;&#039;AttachHandler()&#039;&#039;&#039; and &#039;&#039;&#039;DetachHandler()&#039;&#039;&#039;, etc.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
Other functions are given in the examples to show you more detail on using your Phidget.  For example, &#039;&#039;&#039;DeviceInitialize()&#039;&#039;&#039; will show what needs to be set up for your Phidget before using it.&lt;br /&gt;
|Creating a Phidget software object in Ruby is specific to the Phidget.  For a Phidget Spatial, for example, this would involve creating a {{Code|Spatial}} object.  The examples show how to do this and other API functions.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The object provides device specific methods and properties which are available from the API for your specific Phidget.|&lt;br /&gt;
[http://www.phidgets.com/documentation/rubydoc Ruby API]}}&lt;br /&gt;
&lt;br /&gt;
==Code Snippets==&lt;br /&gt;
&lt;br /&gt;
==Common Problems and Solutions/Workarounds==&lt;br /&gt;
&lt;br /&gt;
Here you can put various frequent problems and our recommended solutions.&lt;/div&gt;</summary>
		<author><name>Mphi</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs21/index.php?title=Language_-_Ruby&amp;diff=19080</id>
		<title>Language - Ruby</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs21/index.php?title=Language_-_Ruby&amp;diff=19080"/>
		<updated>2012-04-19T18:46:36Z</updated>

		<summary type="html">&lt;p&gt;Mphi: /* Visual Studio 2005/2008/2010 */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Language]]&lt;br /&gt;
[[File:Icon-Ruby.png|64x64px||link=|alt=]]Ruby is an interpreted and object oriented scripting language with simple syntax created by Yukihiro Matsumoto.&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
{{LanguageSupport|Ruby|the complete Phidget API, including events|all Phidget devices.|Linux and OS X|}}&lt;br /&gt;
&lt;br /&gt;
==Quick Downloads==&lt;br /&gt;
&lt;br /&gt;
Just need the Ruby documentation, drivers, libraries, and examples?  Here they are:&lt;br /&gt;
&lt;br /&gt;
===API Documentation===&lt;br /&gt;
*[http://www.phidgets.com/documentation/rubydoc API Manual]&lt;br /&gt;
&lt;br /&gt;
===Library and Example Code===&lt;br /&gt;
&lt;br /&gt;
*[http://www.phidgets.com/downloads/examples/rubylib_2.1.8.0.html phidgets-ffi @ RubyGems]&lt;br /&gt;
&lt;br /&gt;
===Drivers===&lt;br /&gt;
*[http://www.phidgets.com/downloads/libraries/libphidget_2.1.8.20111028.tar.gz Linux Source] &lt;br /&gt;
*[http://www.phidgets.com/downloads/libraries/Phidget_2.1.8.20111103.dmg OS X Drivers Installer]&lt;br /&gt;
&lt;br /&gt;
==Getting started with Ruby==&lt;br /&gt;
&lt;br /&gt;
If you are new to writing code for Phidgets, we recommend starting by running, then modifying existing examples. This will allow you to:&lt;br /&gt;
{{ExampleCodeReasons}}&lt;br /&gt;
&lt;br /&gt;
Instructions are divided up by operating system. Choose:&lt;br /&gt;
*[[#OS X |OS X]]&lt;br /&gt;
*[[#Linux | Linux]] (including PhidgetSBC)&lt;br /&gt;
&lt;br /&gt;
==OS X==&lt;br /&gt;
&lt;br /&gt;
Ruby has excellent support on OS X through the Mono framework. &lt;br /&gt;
&lt;br /&gt;
The first step in using Ruby on Mac is to install the Phidget libraries.  Compile and install them as explained on the [[Device List|getting started guide for your device]].  Then, the [[OS - OS X]] page also describes the different Phidget files, their installed locations, and their roles....&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
===Description of Library Files===&lt;br /&gt;
Ruby programs on Windows depend on the following files, which the installers above put onto your system:&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|phidget21.dll}}&amp;lt;/b&amp;gt; contains the actual Phidgets library, which is used at run-time.  By default, it is placed in {{Code|C:\Windows\System32}}.&lt;br /&gt;
You will also need one of the following two files, depending on the .NET framework version you are targeting:&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|/lib}}&amp;lt;/b&amp;gt; is the Phidgets library for .NET framework &amp;lt;i&amp;gt;&amp;lt;b&amp;gt;2.0&amp;lt;/b&amp;gt;&amp;lt;/i&amp;gt; or higher. Your compiler has to know where this file is. By default, it is placed into {{Code|C:\Program Files\Phidgets}}. You can either point your compiler to that location, or copy and link to it in a directory for your project workspace. &lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|examples}}&amp;lt;/b&amp;gt; is the Phidgets library for .NET framework &amp;lt;i&amp;gt;&amp;lt;b&amp;gt;1.1&amp;lt;/b&amp;gt;&amp;lt;/i&amp;gt;. Your compiler has to know where this file is. By default, is is placed into {{Code|C:\Program Files\Phidgets}}. You can either point your compiler to that location, or copy and link to it in a directory for your project workspace. &lt;br /&gt;
&lt;br /&gt;
If you do not want to use our installer, you can download the five [http://www.phidgets.com/downloads/libraries/Phidget21-windevel_2.1.8.20111220.zip file files].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Running the examples and writing your own code can be fairly compiler-specific, so we include instructions for each compiler below.&lt;br /&gt;
&lt;br /&gt;
=====Use Our Examples=====&lt;br /&gt;
&lt;br /&gt;
To start, please install the phidgets-ffi gem by typing the following into command line:&lt;br /&gt;
&lt;br /&gt;
{{Code|gem install phidgets-ffi}}&lt;br /&gt;
&lt;br /&gt;
The gem will be installed into {{Code|\Library\Ruby\Gems\1.x\phidgets-ffi-x.x.x}}&lt;br /&gt;
&lt;br /&gt;
Open a command line terminal and navigate to this folder. {{Code|cd}} into the {{Code|examples}} folder. Here, you will find all of the examples available for Ruby.  If you aren&#039;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]].&lt;br /&gt;
&lt;br /&gt;
The easiest way to confirm that your environment is set up properly will be to compile and run the {{Code|HelloWorld}} Ruby example.&lt;br /&gt;
&lt;br /&gt;
The only thing left to do is to run the examples! Type the following into command line:&lt;br /&gt;
&lt;br /&gt;
{{Code|ruby HelloWorld.rb}} &lt;br /&gt;
&lt;br /&gt;
[[File:CSharp VS2005 Run.PNG|link=|alt=Run]]&lt;br /&gt;
&lt;br /&gt;
This program will detect for devices that are attached/detached on the computer. Go ahead, and attach or detach your devices! Here is an example output:&lt;br /&gt;
&lt;br /&gt;
[[File:CSharp VS2005 HelloWorld Output.PNG|link=|alt=HelloWorld Output]]&lt;br /&gt;
&lt;br /&gt;
After confirming that the {{Code|HelloWorld}} example is working, you can proceed to run the example for your device. If you aren&#039;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 ensure that you have set your start up project to be the one that matches your device before compiling. &lt;br /&gt;
&lt;br /&gt;
Once you have the Ruby examples running, we have a [[#Follow the Examples|teaching]] section below to help you follow them.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
=====Write Your Own Code=====&lt;br /&gt;
&lt;br /&gt;
When you are building a project from scratch, or adding Phidget function calls to an existing project, you&#039;ll need to configure your environment to properly link the Phidget Ruby libraries. To begin: &lt;br /&gt;
&lt;br /&gt;
1. Generate a new  Ruby Windows Applications project with a descriptive name such as PhidgetTest. &lt;br /&gt;
&lt;br /&gt;
[[File:CSharp VS2005 New Project.PNG|link=|alt=New Project]]&lt;br /&gt;
&lt;br /&gt;
2. Add a reference to the .NET Phidgets library.&lt;br /&gt;
&lt;br /&gt;
[[File:CSharp VS2005 Add Reference.PNG|link=|alt=Add Reference]]&lt;br /&gt;
&lt;br /&gt;
3. Under the .NET tab, select {{Code|Phidget21.NET.dll}}.&lt;br /&gt;
If you used our installer, these files are installed in {{Code|C:\Program Files\Phidgets}}, by default. If it does not appear in this list, then you can browse to the Phidget Framework installation directory and add the file.&lt;br /&gt;
&lt;br /&gt;
[[File:CSharp VS2005 Add Reference 2.PNG|link=|alt=Add Reference]]&lt;br /&gt;
&lt;br /&gt;
4. Then, in your code, you will need to include the Phidget library:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=csharp&amp;gt;&lt;br /&gt;
  using Phidgets;&lt;br /&gt;
  using Phidgets.Events;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The project now has access to the Phidget21 function calls and you are ready to begin coding.&lt;br /&gt;
&lt;br /&gt;
The same [[#Follow the Examples|teaching ]] section which describes the examples also has further resources for programming your Phidget.&lt;br /&gt;
&lt;br /&gt;
==Linux==&lt;br /&gt;
&lt;br /&gt;
C# has support on Linux through the Mono framework.  &lt;br /&gt;
&lt;br /&gt;
The first step in using Ruby 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.&lt;br /&gt;
&lt;br /&gt;
==Follow the Examples==&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
Next, comes our API information.  These resources outline the Ruby Phidget functions:&lt;br /&gt;
&lt;br /&gt;
{{UsingAPhidgetInCodeGeneral|both of which are available in Ruby|[http://www.phidgets.com/documentation/rubydoc Ruby API]}}&lt;br /&gt;
&lt;br /&gt;
===Example Flow===&lt;br /&gt;
&lt;br /&gt;
{{ExamplePseudocode|In Ruby, you can name these &#039;&#039;&#039;event&#039;&#039;&#039; 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. &amp;lt;br&amp;gt; &lt;br /&gt;
In the example code, the event functions common to all Phidgets are called things like &#039;&#039;&#039;AttachHandler()&#039;&#039;&#039; and &#039;&#039;&#039;DetachHandler()&#039;&#039;&#039;, etc.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
Other functions are given in the examples to show you more detail on using your Phidget.  For example, &#039;&#039;&#039;DeviceInitialize()&#039;&#039;&#039; will show what needs to be set up for your Phidget before using it.&lt;br /&gt;
|Creating a Phidget software object in Ruby is specific to the Phidget.  For a Phidget Spatial, for example, this would involve creating a {{Code|Spatial}} object.  The examples show how to do this and other API functions.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
The object provides device specific methods and properties which are available from the API for your specific Phidget.|&lt;br /&gt;
[http://www.phidgets.com/documentation/rubydoc Ruby API]}}&lt;br /&gt;
&lt;br /&gt;
==Code Snippets==&lt;br /&gt;
&lt;br /&gt;
==Common Problems and Solutions/Workarounds==&lt;br /&gt;
&lt;br /&gt;
Here you can put various frequent problems and our recommended solutions.&lt;/div&gt;</summary>
		<author><name>Mphi</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs21/index.php?title=Language_-_Flex_AS3&amp;diff=19077</id>
		<title>Language - Flex AS3</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs21/index.php?title=Language_-_Flex_AS3&amp;diff=19077"/>
		<updated>2012-04-19T18:11:34Z</updated>

		<summary type="html">&lt;p&gt;Mphi: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Language]]&lt;br /&gt;
Flex AS3 is no longer maintained by Phidgets. Preamble about the language and its general strengths and weaknesses.&lt;br /&gt;
&lt;br /&gt;
==Assessment for use with Phidgets==&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
==Support==&lt;br /&gt;
Flex AS3 has a complete API and code samples for all Phidgets devices.&lt;br /&gt;
&lt;br /&gt;
For a complete list of our supported languages and their support status, [[Levels of Support|click here]].&lt;br /&gt;
&lt;br /&gt;
==Restrictions==&lt;br /&gt;
In this section, list any restrictions or limitations that this particular language may impose. For example, incompatibility with certain operating systems.&lt;br /&gt;
&lt;br /&gt;
==Development Environments and Compilers==&lt;br /&gt;
&lt;br /&gt;
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. &lt;br /&gt;
&lt;br /&gt;
==Quick Downloads==&lt;br /&gt;
Before you can run your program, you need to set up the proper environment and get the necessary files off the Phidgets website. &lt;br /&gt;
Visit the drivers section at www.phidgets.com and get the latest:&lt;br /&gt;
* [http://www.phidgets.com/drivers.php Phidget Framework]&lt;br /&gt;
You will need the Phidget Framework to use and to program with Phidgets. We also recommend that you download the following reference materials:&lt;br /&gt;
* [http://www.phidgets.com/documentation/Phidget21Flash_doc.zip API Manual]&lt;br /&gt;
* [http://www.phidgets.com/documentation/web/flashdoc/index.html API Reference]&lt;br /&gt;
* [http://www.phidgets.com/downloads/examples/Flex_2.1.8.20110615.zip Flex AS3 Sample Code]&lt;br /&gt;
* You can find a high level discussion about programming with Phidgets in general on the [[General Phidget Programming]] page. &lt;br /&gt;
* The [[Device Functionality]] page explains the general operational information for your device. &lt;br /&gt;
&lt;br /&gt;
You may want to have these pages open while working through these instructions.&lt;br /&gt;
&lt;br /&gt;
==Getting Started==&lt;br /&gt;
&lt;br /&gt;
The Phidget examples were written using Actionscript 3.0 under Adobe Flex Builder 3 and this tutorial assumes its use. &lt;br /&gt;
Other development environments should work provided they support Actionscript 3.0, and each would be set up in a similar manner. &lt;br /&gt;
First launch Flex and generate a new project with a descriptive name such as PhidgetTest. &lt;br /&gt;
You will then need to add the Phidget Library to your project library path. &lt;br /&gt;
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. &lt;br /&gt;
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. &lt;br /&gt;
&lt;br /&gt;
===Coding For Your Phidget===&lt;br /&gt;
&lt;br /&gt;
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:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&amp;lt;syntaxhighlight lang=actionscript&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;lt;?xml version=&amp;quot;1.0&amp;quot; encoding=&amp;quot;utf-8&amp;quot;?&amp;gt;&lt;br /&gt;
  &amp;lt;mx:Application xmlns:mx=&amp;quot;http://www.adobe.com/2006/mxml&amp;quot; layout=&amp;quot;absolute&amp;quot;&amp;gt;&lt;br /&gt;
  &amp;lt;mx:Script&amp;gt;&lt;br /&gt;
     &amp;lt;![CDATA[&lt;br /&gt;
        import com.phidgets.PhidgetInterfaceKit;&lt;br /&gt;
        import com.phidgets.events.*;&lt;br /&gt;
        //More code goes here&lt;br /&gt;
     ]]&amp;gt;&lt;br /&gt;
  &amp;lt;/mx:Script&amp;gt;&lt;br /&gt;
  &amp;lt;/mx:Application&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Afterwards, the Phidget object will need to be declared and then initialized. &lt;br /&gt;
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: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&amp;lt;syntaxhighlight lang=actionscript&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  public var phid:com.phidgets.PhidgetInterfaceKit;&lt;br /&gt;
  public function start():void{ &lt;br /&gt;
     phid = new PhidgetInterfaceKit();&lt;br /&gt;
     //More code goes here&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
===Connecting to the Phidget===&lt;br /&gt;
&lt;br /&gt;
Next, the program needs to try and connect to the Phidget through an open call. &lt;br /&gt;
Open will continuously try to connect to a Phidget, based on the parameters given, even trying to reconnect if it gets disconnected. &lt;br /&gt;
This means that simply calling open does not guarantee you can use the Phidget immediately. &lt;br /&gt;
We can handle this by using event driven programming and tracking the AttachEvents and DetachEvents, or checking the isAttached property.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&amp;lt;syntaxhighlight lang=actionscript&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  phid.open(&amp;quot;localhost&amp;quot;, 5001);&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The API manual lists all of the available modes that open provides. &lt;br /&gt;
In Flash, the parameters can be used to open the first Phidget of a type it can find or based on its serial number. &lt;br /&gt;
One important thing to remember is that when working with Phidgets, a local connection will reserve the device until closed. &lt;br /&gt;
This means other programs that open the Phidget may end up preventing other instances from retrieving data. &lt;br /&gt;
The one connection per device limit does not apply when exclusively using the Phidget WebService, and the Actionscript libraries by extension.&lt;br /&gt;
&lt;br /&gt;
===Event Driven Programming===&lt;br /&gt;
&lt;br /&gt;
We recommend the use of event driven programming when working with Phidgets. &lt;br /&gt;
In Actionscript 3.0, we hook an event handler with the following code: &lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&amp;lt;syntaxhighlight lang=actionscript&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
  phid.addEventListener(PhidgetDataEvent.SENSOR_CHANGE, onSensorChange);&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The onSensorChange method will get executed every time the PhidgetInterfaceKit reports a change on one of its analog inputs. &lt;br /&gt;
The values from the report can be accessed from the PhidgetDataEvent object.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&amp;lt;syntaxhighlight lang=actionscript&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  private function onSensorChange(evt:PhidgetDataEvent):void{&lt;br /&gt;
     //Insert your code here&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Some events such as Attach and Detach belong to the base Phidget object and thus are common to all types of Phidgets. &lt;br /&gt;
Please refer to the API manual for a full list of events and their usage. &lt;br /&gt;
&lt;br /&gt;
===Working directly with the Phidget===&lt;br /&gt;
&lt;br /&gt;
Some values can be directly read and set on the Phidget and used as an alternative to event driven programming. &lt;br /&gt;
Simply use the instance properties or call member functions such as getSensorValue(index: int) or setOutputState(index: int, val: Boolean) for PhidgetInterfaceKits.&lt;br /&gt;
&lt;br /&gt;
===Working with multiple Phidgets===&lt;br /&gt;
&lt;br /&gt;
Multiple Phidgets of the same type can easily be run inside the same program. &lt;br /&gt;
In our case, it requires another PhidgetInterfaceKit instance to be defined and initialized. &lt;br /&gt;
The new instance can then be set up, opened and used in the same process as the previous one. &lt;br /&gt;
If the application needs to distinguish between the devices, open can be called with the serial number of a specific Phidget.&lt;br /&gt;
&lt;br /&gt;
===Flash Security Settings===&lt;br /&gt;
&lt;br /&gt;
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. &lt;br /&gt;
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...”.&lt;br /&gt;
&lt;br /&gt;
===Other Phidgets===&lt;br /&gt;
&lt;br /&gt;
The design given in this document can also be followed for almost all Phidgets. &lt;br /&gt;
For example, if you were using a PhidgetRFID instead of an PhidgetInterfacekit, you would declare a RFID object instead of a InterfaceKit object. &lt;br /&gt;
The methods and events available would change but they can be accessed in a similar manner.&lt;br /&gt;
&lt;br /&gt;
==Building your Project==&lt;br /&gt;
Describe the different ways a project could be built using this language.&lt;br /&gt;
&lt;br /&gt;
==Common Problems and Solutions/Workarounds==&lt;br /&gt;
Here you can put various frequent problems and our recommended solutions.&lt;/div&gt;</summary>
		<author><name>Mphi</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs21/index.php?title=Language_-_REALBasic&amp;diff=19076</id>
		<title>Language - REALBasic</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs21/index.php?title=Language_-_REALBasic&amp;diff=19076"/>
		<updated>2012-04-19T18:11:07Z</updated>

		<summary type="html">&lt;p&gt;Mphi: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Language]]&lt;br /&gt;
[[File:icon-REALBasic.png|64x64px]] REALBasic is no longer maintained by Phidgets. Preamble about the language&#039;s origin and its main characteristics.&lt;br /&gt;
&lt;br /&gt;
==Support==&lt;br /&gt;
REALBasic has a complete API for all Phidgets devices, but no sample code at this time. &lt;br /&gt;
&lt;br /&gt;
For a complete list of our supported languages and their support status, [[Levels of Support|click here]].&lt;br /&gt;
&lt;br /&gt;
* 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.&lt;br /&gt;
* In this section, list any restrictions or limitations that this particular language may impose. For example, incompatibility with certain operating systems.&lt;br /&gt;
&lt;br /&gt;
==Development Environments and Compilers==&lt;br /&gt;
&lt;br /&gt;
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. &lt;br /&gt;
&lt;br /&gt;
==Quick Downloads==&lt;br /&gt;
Before you can run your program, you need to set up the proper environment and get the necessary files off the Phidgets website. &lt;br /&gt;
Visit the drivers section at www.phidgets.com and get the latest:&lt;br /&gt;
* [http://www.phidgets.com/drivers.php Phidget Framework]&lt;br /&gt;
You will need the Phidget Framework to use and to program with Phidgets. We also recommend that you download the following reference materials:&lt;br /&gt;
* [http://www.phidgets.com/documentation/Phidget21_C_Doc.zip API Manual]&lt;br /&gt;
* [http://www.phidgets.com/documentation/web/cdoc/index.html API Reference]&lt;br /&gt;
* We currently do not have any sample code for REALBasic.&lt;br /&gt;
* You can find a high level discussion about programming with Phidgets in general on the [[General API]] page. &lt;br /&gt;
* The [[Device Functionality]] page explains the general operational information for your device. &lt;br /&gt;
&lt;br /&gt;
You may want to have these pages open while working through these instructions.&lt;br /&gt;
&lt;br /&gt;
==Getting Started==&lt;br /&gt;
&lt;br /&gt;
Applications using Phidget can be developed in REALbasic through the C API. &lt;br /&gt;
This tutorial assumes the use of REALbasic 2009, other recent versions should work as well and would be set up in a similar manner. &lt;br /&gt;
Phidgets will not run under REALbasic 2006. Although it’s possible to import Phidgets as ActiveX controls, this is not recommended due to the way REALbasic handles them. &lt;br /&gt;
To begin, launch REALbasic and create a new “Desktop Application”. &lt;br /&gt;
Then, place an EditField “EditField1” in the form designer for the purpose of capturing some simple output.  &lt;br /&gt;
&lt;br /&gt;
===Coding For Your Phidget===&lt;br /&gt;
&lt;br /&gt;
Before you can use the Phidget, you must declare each of the functions from the C library that the program will use. &lt;br /&gt;
For example, you can declare at initialization (inside Windows1.Open) some C functions for a PhidgetInterfaceKit with the following syntax:&lt;br /&gt;
  &lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&amp;lt;syntaxhighlight lang=vb&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  Declare Function CPhidget_open lib &amp;quot;phidget21&amp;quot; (phid as Ptr, serialNumber as Integer) as integer&lt;br /&gt;
  Declare Function CPhidget_waitForAttachment lib &amp;quot;phidget21&amp;quot; (phid as Ptr, milliseconds asInteger) as Integer&lt;br /&gt;
  Declare Function CPhidgetInterfaceKit_create lib &amp;quot;phidget21&amp;quot; (CPhidgetInterfaceKitHandle as Ptr) as Integer&lt;br /&gt;
  Declare Function CPhidgetInterfaceKit_set_OnSensorChange_Handler lib &amp;quot;phidget21&amp;quot;(CPhidgetInterfaceKitHandle as Ptr, fptr as Ptr, UserPtr as Ptr) as Integer&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The declaration closely follows the C API with a few minor changes. &lt;br /&gt;
Generally, when any type of PhidgetHandle is used, it’s safe to change the type to Ptr. &lt;br /&gt;
You will also need to copy the phidget21.dll file from the Phidget Framework directory into your project output directory for these declarations to work.&lt;br /&gt;
&lt;br /&gt;
Afterwards, the Phidget object itself will need to be declared and then initialized. &lt;br /&gt;
This can be done by declaring a property on Windows1 named “ifKitHandle” as a Ptr, calling create on an empty block of memory, and then setting the handle to the result.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&amp;lt;syntaxhighlight lang=vb&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  Dim tmp As MemoryBlock&lt;br /&gt;
  Dim result As Integer&lt;br /&gt;
  tmp = NewMemoryBlock(1000)&lt;br /&gt;
  Call CPhidgetInterfaceKit_create(tmp)&lt;br /&gt;
  ifKitHandle = tmp.Ptr(0)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The object name for any type of Phidget is listed in the API manual. &lt;br /&gt;
Every type of Phidget also inherits functionality from the Phidget base class.&lt;br /&gt;
&lt;br /&gt;
===Connecting to the Phidget===&lt;br /&gt;
&lt;br /&gt;
The program can try to connect to the Phidget through an open call. &lt;br /&gt;
Open will continuously try to connect to a Phidget, based on the parameters given, even trying to reconnect if it gets disconnected. &lt;br /&gt;
This means that simply calling open does not guarantee you can use the Phidget immediately. &lt;br /&gt;
We can account for a connection by using event driven programming and tracking the AttachEvents and DetachEvents, or by calling WaitForAttachment. &lt;br /&gt;
WaitForAttachment will block indefinitely until a connection is made to the Phidget, or an optional timeout is exceeded.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&amp;lt;syntaxhighlight lang=vb&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  result = CPhidget_open(ifKitHandle, -1)&lt;br /&gt;
  if CPhidget_waitForAttachment(ifKitHandle, 3000)&amp;lt;&amp;gt;0 Then&lt;br /&gt;
     MsgBox(&amp;quot;Can’t find a PhidgetInterfaceKit&amp;quot;)&lt;br /&gt;
  else&lt;br /&gt;
      &#039;More code goes here&lt;br /&gt;
  end if&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The different parameters and open calls can be used to open the first Phidget of a type it can find, open based on a serial number, or even open across the network. &lt;br /&gt;
The API manual lists all of the available modes that open provides. &lt;br /&gt;
One important thing to remember is that when working with Phidgets, a local connection will reserve the device until closed. &lt;br /&gt;
This prevents any other instances from retrieving data from the Phidget, including other programs. &lt;br /&gt;
The one connection per device limit does not apply when exclusively using the Phidget Webservice.&lt;br /&gt;
&lt;br /&gt;
===Event Driven Programming===&lt;br /&gt;
&lt;br /&gt;
We recommend the use of event driven programming when working with Phidgets. &lt;br /&gt;
In Real Basic, a few steps will need to be followed to properly hook C events. &lt;br /&gt;
Event handler methods with matching definitions must be created inside their own module (Project | Add | Module). &lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&amp;lt;syntaxhighlight lang=vb&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  Function ifKit_OnSensorChange(ByVal phid As Ptr, ByVal userPtr As Ptr, ByVal index &lt;br /&gt;
  As Integer, ByVal sensorValue As Integer) As Integer&lt;br /&gt;
     #pragma X86CallingConvention StdCall&lt;br /&gt;
  Window1.EditField1.text = Str(index) + &amp;quot;:&amp;quot; + Str(sensorValue)&lt;br /&gt;
  End Function&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this example, a new module “Module1” was created and given the ifKit_OnSensorChange method. &lt;br /&gt;
Note that the handler must have “#pragma X86CallingConvention StdCall” as the first line. &lt;br /&gt;
Inside Window1.Open we can then link to the method we wrote with a call to CPhidgetInterfaceKit_set_OnSensorChange_Handler.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&amp;lt;syntaxhighlight lang=vb&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  Call CPhidgetInterfaceKit_set_OnSensorChange_Handler(ifKitHandle, AddressOf Module1.ifKit_OnsensorChange, nil)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
With this, the code inside ifKit_onSensorChange will get executed every time the PhidgetInterfaceKit reports a change on one of its analog inputs. &lt;br /&gt;
Some events such as Attach and Detach belong to the base Phidget object and thus are common to all types of Phidgets. &lt;br /&gt;
Please refer to the API manual for a full list of events and their usage. &lt;br /&gt;
&lt;br /&gt;
===Working directly with the Phidget===&lt;br /&gt;
&lt;br /&gt;
Some values can be directly read and set on the Phidget, and inside polling loops used as an alternative to event driven programming. &lt;br /&gt;
Simply declare the C function before using calls such as CPhidgetInterfaceKit_getSensorValue for PhidgetInterfaceKits.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&amp;lt;syntaxhighlight lang=vb&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  Declare Function CPhidgetInterfaceKit_getSensorValue lib &amp;quot;phidget21&amp;quot; (phid as Ptr, index as Integer, sensorValue as Ptr) as Integerresult = CPhidgetInterfaceKit_getSensorValue(ifKitHandle, 0, tmp)&lt;br /&gt;
  Dim sensorValue As Integer = tmp.Long(0)&lt;br /&gt;
  Window1.EditField1.Text = Str(sensorValue)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Working with multiple Phidgets===&lt;br /&gt;
&lt;br /&gt;
Multiple Phidgets of the same type can easily be run inside the same program. &lt;br /&gt;
In our case, it requires another instance of a PhidgetInterfaceKit to be defined and initialized.&lt;br /&gt;
The new instance can be set up, opened and used in the same process as the previous one. &lt;br /&gt;
If the application needs to distinguish between the devices, open can be called with the serial number of a specific Phidget.&lt;br /&gt;
&lt;br /&gt;
===Other Phidgets===&lt;br /&gt;
&lt;br /&gt;
The design given in this document can also be followed for almost all Phidgets. &lt;br /&gt;
For example, if you were using a PhidgetRFID instead of an PhidgetInterfaceKit, you would call CPhidgetRFID_create instead of CPhidgetInterfaceKit_create. &lt;br /&gt;
The methods and events available would change but they can be used in a similar manner.&lt;br /&gt;
&lt;br /&gt;
==Building your Project==&lt;br /&gt;
Describe the different ways a project could be built using this language.&lt;br /&gt;
&lt;br /&gt;
==Common Problems and Solutions/Workarounds==&lt;br /&gt;
Here you can put various frequent problems and our recommended solutions.&lt;/div&gt;</summary>
		<author><name>Mphi</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs21/index.php?title=Language_-_C&amp;diff=19067</id>
		<title>Language - C</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs21/index.php?title=Language_-_C&amp;diff=19067"/>
		<updated>2012-04-19T17:52:54Z</updated>

		<summary type="html">&lt;p&gt;Mphi: /* Description of Library Files */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Language]]&lt;br /&gt;
[[File:icon-C++.png|link=|alt=C/C++|64x64px]] C++ is a general purpose, cross-platform programming language with a vast user base. &lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
{{LanguageSupport|C/C++|the complete Phidget API, including events|all Phidget devices.|Windows 2000/XP/Vista/7(environments include [[#Visual Studio | Visual Studio]], [[#Borland | Borland]], [[#Cygwin/MinGW | Cygwin, and MinGW]]), [[#Windows CE | Windows CE]], [[#OS X | OS X]], and [[#Linux | Linux]]|}}&lt;br /&gt;
&lt;br /&gt;
==Quick Downloads==&lt;br /&gt;
{{QuickDownloads|C/C++|&lt;br /&gt;
{{APIQuickDownloads|http://www.phidgets.com/documentation/Phidget21_C_Doc.zip}}|&lt;br /&gt;
{{ExampleQuickDownloads|http://www.phidgets.com/downloads/examples/VCpp_2.1.8.20110615.zip Visual Studio 2005/2008/2010|}}&lt;br /&gt;
{{ExampleQuickDownloads|http://www.phidgets.com/downloads/examples/phidget21-c-examples_2.1.8.20110615.tar.gz Generic|}}|&lt;br /&gt;
{{ExtraLibraryQuickDownloads|http://www.phidgets.com/downloads/libraries/phidget21bcc_2.1.8.20110615.zip|Borland(Windows)|}}&lt;br /&gt;
{{WindowsQuickDownloads}}&lt;br /&gt;
{{MacQuickDownloads}}&lt;br /&gt;
{{LinuxQuickDownloads}}&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
==Getting started with C/C++==&lt;br /&gt;
&lt;br /&gt;
If you are new to writing code for Phidgets, we recommend starting by running, then modifying existing examples. This will allow you to:&lt;br /&gt;
{{ExampleCodeReasons}}&lt;br /&gt;
&lt;br /&gt;
Instructions are divided up by operating system. Choose:&lt;br /&gt;
*[[#Windows(2000/XP/Vista/7)|Windows 2000 / XP / Vista / 7]]&lt;br /&gt;
*[[#OS X |OS X]]&lt;br /&gt;
*[[#Linux | Linux]] (including PhidgetSBC)&lt;br /&gt;
&lt;br /&gt;
==Windows (2000/XP/Vista/7)==&lt;br /&gt;
&lt;br /&gt;
===Description of Library Files===&lt;br /&gt;
C/C++ programs on Windows depend on three files, which the installers in [[#Libraries and Drivers|Quick Downloads]] put onto your system:&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|phidget21.dll}}&amp;lt;/b&amp;gt; contains the actual Phidget library, which is used at run-time.  By default, it is placed in {{Code|C:\Windows\System32}}.&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|phidget21.lib}}&amp;lt;/b&amp;gt; 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}}. &lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|phidget21.h}}&amp;lt;/b&amp;gt; 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. &lt;br /&gt;
&lt;br /&gt;
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 [[OS_-_Windows#Manual_File_Installation | Manual Installation Instructions]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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++]].&lt;br /&gt;
&lt;br /&gt;
===Visual Studio===&lt;br /&gt;
&lt;br /&gt;
C++/CLI (which used to be called Managed C++) is very different from mainstream C/C++.  If you must use C++/CLI, consider calling the Phidget .NET library, instead of the C API normally used from C/C++.  We have no documentation for using C++/CLI.&lt;br /&gt;
&lt;br /&gt;
Microsoft makes free versions of Visual Studio available known as Express Editions.  The Express editions are suitable for most applications, but are limited in features for more complex applications. Please see [http://www.microsoft.com/visualstudio Microsoft Visual Studio] for more information.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====Visual Studio 2005/2008/2010====&lt;br /&gt;
&lt;br /&gt;
=====Use Our Examples=====&lt;br /&gt;
&lt;br /&gt;
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 &amp;amp;rarr; Open &amp;amp;rarr; Project &amp;amp;rarr; Solution, and open {{Code|Visual Studio Phidgets Examples.sln}} in the {{Code|VCpp}} folder of the examples.&lt;br /&gt;
&lt;br /&gt;
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. &lt;br /&gt;
&lt;br /&gt;
[[File:VS2005 Conversion Wizard.PNG|link=|alt=Conversion Wizard]]&lt;br /&gt;
&lt;br /&gt;
This will load all of the examples available for C/C++. The easiest way to confirm that your environment is set up properly will be to compile and run the {{Code|HelloWorld}} C/C++ example.&lt;br /&gt;
&lt;br /&gt;
Start by setting the {{Code|HelloWorld}} project as your start up project.&lt;br /&gt;
&lt;br /&gt;
[[File:VS2005 StartUp Project.PNG|link=|alt=Start Up Project]]&lt;br /&gt;
&lt;br /&gt;
To run the example, click on Debug &amp;amp;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&#039;s location accordingly. Please see the [[#Write Your Own Code | Write Your Own Code]] section for details. &lt;br /&gt;
&lt;br /&gt;
[[File:VS2005 Run.PNG|link=|alt=Run]]&lt;br /&gt;
&lt;br /&gt;
This program will detect for devices that are attached/detached on the computer. Go ahead, and attach or detach your devices! Here is an example output:&lt;br /&gt;
&lt;br /&gt;
[[File:VS2005 HelloWorld Output.PNG|link=|alt=HelloWorld Output]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
After confirming that the {{Code|HelloWorld}} example is working, you can proceed to run the example for your device. If you aren&#039;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 ensure that you have set your start up project to be the one that matches your device before compiling. &lt;br /&gt;
&lt;br /&gt;
Once you have the C/C++ examples running, we have a [[#Follow the Examples|teaching]] section below to help you follow them.&lt;br /&gt;
&lt;br /&gt;
=====Write Your Own Code=====&lt;br /&gt;
&lt;br /&gt;
When you are building a project from scratch, or adding Phidget function calls to an existing project, you&#039;ll need to configure your development environment to properly link the Phidget C/C++ library. To begin: &lt;br /&gt;
&lt;br /&gt;
1. Generate a new Visual C++: Win32 Console Application project with a descriptive name such as PhidgetTest.&lt;br /&gt;
&lt;br /&gt;
[[File:VS2005 New Project.PNG|link=|alt=New Project]]&lt;br /&gt;
&lt;br /&gt;
2. Next, select Console Application.&lt;br /&gt;
&lt;br /&gt;
[[File:VS2005 New Project 2.PNG|link=|alt=New Project]]&lt;br /&gt;
&lt;br /&gt;
3. Open the project properties window.&lt;br /&gt;
&lt;br /&gt;
4. Navigate to Configuration Properties &amp;amp;rarr; C/C++.&lt;br /&gt;
&lt;br /&gt;
5. Add {{Code|&amp;quot;C:\Program Files\Phidgets&amp;quot;}} 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&#039;s location accordingly.&lt;br /&gt;
&lt;br /&gt;
[[File:VS2005 Header.PNG|link=|alt=Header File]]&lt;br /&gt;
&lt;br /&gt;
6. Navigate to Configuration Properties &amp;amp;rarr; Linker &amp;amp;rarr; Input.&lt;br /&gt;
&lt;br /&gt;
7. Edit the additional dependencies and add {{Code|&amp;quot;C:\Program  Files\Phidgets\phidget21.lib&amp;quot;}}. 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&#039;s location accordingly.&lt;br /&gt;
&lt;br /&gt;
[[File:VS2005 Library.PNG|link=|alt=Library File]]&lt;br /&gt;
&lt;br /&gt;
8. The project now has access to the Phidget function calls and you are ready to begin coding.&lt;br /&gt;
&lt;br /&gt;
Then, in your code, you will need to include the Phidget C/C++ library:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=cpp&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  #include &amp;lt;phidget21.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The same [[#Follow the Examples|teaching]] section which describes the examples also has further resources for programming your Phidget.&lt;br /&gt;
&lt;br /&gt;
====Visual Studio 2003====&lt;br /&gt;
&lt;br /&gt;
=====Use Our Examples=====&lt;br /&gt;
&lt;br /&gt;
1. Start by downloading the [http://www.phidgets.com/downloads/examples/phidget21-c-examples_2.1.8.20111028.tar.gz examples]. You can import these examples into a Visual Studio 2003 C++ project. Afterwards, unpack them into a folder. Here, you can find example programs for all the devices. You will need this example source code to be copied into your C++ project later on. The easiest way to confirm that your environment is set up properly will be to compile and run the {{Code|HelloWorld}} C/C++ example. &lt;br /&gt;
&lt;br /&gt;
2. A new project will need to be created. Generate a new Visual C++ empty project(.NET) with a descriptive name such as HelloWorld.&lt;br /&gt;
&lt;br /&gt;
[[File:VS2003 New Project.PNG|link=|alt=New Project]]&lt;br /&gt;
&lt;br /&gt;
3. Create a new C++ file by adding a new item to the source files folder. &lt;br /&gt;
&lt;br /&gt;
[[File:VS2003 New File.PNG|link=|alt=New File]]&lt;br /&gt;
&lt;br /&gt;
[[File:VS2003 New File 2.PNG|link=|alt=New File]]&lt;br /&gt;
&lt;br /&gt;
4. An empty C++ file will pop up. Please copy and paste the contents of the {{Code|HelloWorld.c}} program into here. &lt;br /&gt;
&lt;br /&gt;
[[File:VS2003 Source.PNG|link=|alt=Source Code]]&lt;br /&gt;
&lt;br /&gt;
5. Next, the project setting needs to be set up. Open the project properties window.&lt;br /&gt;
&lt;br /&gt;
6. Navigate to Configuration Properties &amp;amp;rarr; C/C++.&lt;br /&gt;
&lt;br /&gt;
7. Add {{Code|&amp;quot;C:\Program Files\Phidgets&amp;quot;}} to the additional include 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&#039;s location accordingly.&lt;br /&gt;
&lt;br /&gt;
[[File:VS2003 Header.PNG|link=|alt=Header File]]&lt;br /&gt;
&lt;br /&gt;
8. Navigate to Configuration Properties &amp;amp;rarr; Linker &amp;amp;rarr; Input.&lt;br /&gt;
&lt;br /&gt;
9. Add {{Code|&amp;quot;C:\Program  Files\Phidgets\phidget21.lib&amp;quot;}} to the additional dependencies field. 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&#039;s location accordingly. &lt;br /&gt;
&lt;br /&gt;
[[File:VS2003 Library.PNG|link=|alt=Library File]]&lt;br /&gt;
&lt;br /&gt;
10. Now, you can run the example. Click on Debug &amp;amp;rarr; Start Without Debugging.&lt;br /&gt;
&lt;br /&gt;
[[File:VS2003 Run.PNG|link=|alt=Run]]&lt;br /&gt;
&lt;br /&gt;
11. This program will detect for devices that are attached/detached on the computer. Go ahead, and attach or detach your devices! Here is an example output:&lt;br /&gt;
&lt;br /&gt;
[[File:VS2003 HelloWorld Output.PNG|link=|alt=HelloWorld Output]]&lt;br /&gt;
&lt;br /&gt;
After confirming that the {{Code|HelloWorld}} example is working, you can proceed to run the example for your device. If you aren&#039;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]].&lt;br /&gt;
&lt;br /&gt;
Once you have the C/C++ examples running, we have a [[#Follow the Examples|teaching]] section below to help you follow them.&lt;br /&gt;
&lt;br /&gt;
=====Write Your Own Code=====&lt;br /&gt;
&lt;br /&gt;
When you are building a project from scratch, or adding Phidget function calls to an existing project, you&#039;ll need to configure your  development environment to properly link the Phidget C/C++ library. Please see the [[#Use Our Examples 2 | Use Our Examples]] section for instructions.&lt;br /&gt;
&lt;br /&gt;
Then, in your code, you will need to include the Phidget C/C++ library:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=cpp&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  #include &amp;lt;phidget21.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Visual Studio C++ 6.0====&lt;br /&gt;
&lt;br /&gt;
=====Use Our Examples=====&lt;br /&gt;
&lt;br /&gt;
1. Download the [http://www.phidgets.com/downloads/examples/phidget21-c-examples_2.1.8.20110615.tar.gz examples] and unpack them into a folder. Here, you can find example programs for all the devices. You will need this example source code to be copied into your C++ project later on. The easiest way to confirm that your environment is set up properly will be to compile and run the {{Code|HelloWorld}} C/C++ example.&lt;br /&gt;
&lt;br /&gt;
2. Next, a new project will need to be created. Generate a new Win32 Console Application project with a descriptive name such as HelloWorld.&lt;br /&gt;
&lt;br /&gt;
[[File:VS6 New Project.PNG|link=|alt=New Project]]&lt;br /&gt;
&lt;br /&gt;
3. Create an empty project.&lt;br /&gt;
&lt;br /&gt;
[[File:VS6 New Project 2.PNG|link=|alt=New Project]]&lt;br /&gt;
&lt;br /&gt;
4. Next, the project settings needs to be set up. Navigate to Project &amp;amp;rarr; Settings &amp;amp;rarr; C/C++ &amp;amp;rarr; Preprocessor.&lt;br /&gt;
&lt;br /&gt;
5. Add {{Code|C:\Program Files\Phidgets}} to the additional include 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&#039;s location accordingly.&lt;br /&gt;
&lt;br /&gt;
[[File:VS6 Header.PNG|link=|alt=Header File]]&lt;br /&gt;
&lt;br /&gt;
6. Navigate to Project &amp;amp;rarr; Settings &amp;amp;rarr; Link &amp;amp;rarr; Input &amp;amp;rarr; Additional library Path.&lt;br /&gt;
&lt;br /&gt;
7. Add {{Code|phidget21.lib}} to the object/library modules field.&lt;br /&gt;
&lt;br /&gt;
8. Add {{Code|C:\Program  Files\Phidgets}} to the additional library path. 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&#039;s location accordingly. &lt;br /&gt;
&lt;br /&gt;
[[File:VS6 Library.PNG|link=|alt=Library File]]&lt;br /&gt;
&lt;br /&gt;
The project now has access to the Phidget function calls and you are ready to begin coding.&lt;br /&gt;
&lt;br /&gt;
To import the example program into your project, please:&lt;br /&gt;
&lt;br /&gt;
9. Create a new C++ file by navigating to File &amp;amp;rarr; New &amp;amp;rarr; Files &amp;amp;rarr; C++ Source File and enter a descriptive name such as HelloWorld.&lt;br /&gt;
&lt;br /&gt;
[[File:VS6 New File.PNG|link=|alt=New File]]&lt;br /&gt;
&lt;br /&gt;
10. An empty C++ file will pop up. Please copy and paste the contents of the {{Code|HelloWorld.c}} program here.&lt;br /&gt;
&lt;br /&gt;
[[File:VS6 Source.PNG|link=|alt=Source Code]]&lt;br /&gt;
&lt;br /&gt;
11. Now, you can run the example. Click on Build &amp;amp;rarr; Execute.&lt;br /&gt;
&lt;br /&gt;
[[File:VS6 Run.PNG|link=|alt=Run]]&lt;br /&gt;
&lt;br /&gt;
12. This program will detect for devices that are attached/detached on the computer. Go ahead, and attach or detach your devices! Here is an example output:&lt;br /&gt;
&lt;br /&gt;
[[File:VS6 HelloWorld Output.PNG|link=|alt=HelloWorld Output]]&lt;br /&gt;
&lt;br /&gt;
After confirming that the {{Code|HelloWorld}} example is working, you can proceed to run the example for your device. If you aren&#039;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]].&lt;br /&gt;
&lt;br /&gt;
Once you have the C/C++ examples running, we have a [[#Follow the Examples|teaching]] section below to help you follow them.&lt;br /&gt;
&lt;br /&gt;
=====Write Your Own Code=====&lt;br /&gt;
&lt;br /&gt;
When you are building a project from scratch, or adding Phidget function calls to an existing project, you&#039;ll need to configure your development environment to properly link the Phidget C/C++ library. Please see the [[#Use Our Examples 3 | Use Our Examples]] section for instructions.&lt;br /&gt;
&lt;br /&gt;
In your &#039;&#039;&#039;{{Code|.c}}&#039;&#039;&#039; source code file, you must include a reference to the library header:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=cpp&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  #include &amp;lt;phidget21.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then, you would compile your completed C/C++ code the same way as shown in the [[#Use Our Examples 3 | Use Our Examples]] section. &lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
===Borland===&lt;br /&gt;
&lt;br /&gt;
====Use Our Examples====&lt;br /&gt;
&lt;br /&gt;
In addition to running one of the two [[#Libraries and Drivers:| Windows Installers]] above (which you probably already have if you worked through the &#039;&#039;Getting Started&#039;&#039; page [[Device List | for your device]]), you will need the [http://www.phidgets.com/downloads/libraries/phidget21bcc_2.1.8.20110615.zip Borland C++ Libraries]. {{Code|phidget21bcc.lib}} is typically placed in {{Code|C:\Program Files\Phidgets}}, but you are free to place it in any directory you wish.&lt;br /&gt;
&lt;br /&gt;
After installing the Phidget C/C++ library, you&#039;re ready to download the [http://www.phidgets.com/downloads/examples/phidget21-c-examples_2.1.8.20111028.tar.gz examples] and run the examples.&lt;br /&gt;
&lt;br /&gt;
Afterwards, unpack the examples. The easiest way to confirm that your environment is set up properly will be to compile and run the {{Code|HelloWorld}} C/C++ example. Locate the {{Code|HelloWorld.c}} file and type the following to compile the file and link the Phidget C/C++ library:&lt;br /&gt;
&lt;br /&gt;
To compile, link the Phidget C/C++ library and build a binary executable, enter the following in a command line prompt in the directory with {{Code|HelloWorld.c}}:&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  bcc32 -eHelloWorld -I&amp;quot;C:\Program Files\Phidgets&amp;quot; -L&amp;quot;C:\Program Files\Phidgets&amp;quot; phidget21bcc.lib HelloWorld.c&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It is assumed that {{Code|phidget21bcc.lib}} and {{Code|phidget21.h}} are placed in {{Code|C:\Program Files\Phidgets}}. If the files are placed in another location, please adjust the paths to both of the file&#039;s location accordingly.&lt;br /&gt;
&lt;br /&gt;
In this case, {{Code|HelloWorld.c}} would be the &#039;&#039;&#039;.c&#039;&#039;&#039; file specific to your device.  After using {{Code|bcc32}}, you will have an executable named {{Code|HelloWorld}} that you can run.  &lt;br /&gt;
&lt;br /&gt;
This program will detect for devices that are attached/detached on the computer. Go ahead, and attach or detach your devices! Here is an example output:&lt;br /&gt;
&lt;br /&gt;
[[File:Borland HelloWorld Output.PNG|link=|alt=HelloWorld Output]]&lt;br /&gt;
&lt;br /&gt;
After confirming that the {{Code|HelloWorld}} example is working, you can proceed to run the example for your device. If you aren&#039;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]].&lt;br /&gt;
&lt;br /&gt;
====Write Your Own Code====&lt;br /&gt;
&lt;br /&gt;
When writing your code from scratch, you start it as you would any C/C++ code with Borland. In your &#039;&#039;&#039;{{Code|.c}}&#039;&#039;&#039; source code file, you must include a reference to the library header:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=cpp&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  #include &amp;lt;phidget21.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then, you would compile your completed C/C++ code the same way as the examples [[#Use Our Examples 4 |above]]. &lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
===GCC on Windows===&lt;br /&gt;
&lt;br /&gt;
====Cygwin/MinGW====&lt;br /&gt;
&lt;br /&gt;
=====Use Our Examples=====&lt;br /&gt;
&lt;br /&gt;
Download the [http://www.phidgets.com/downloads/examples/phidget21-c-examples_2.1.8.20111028.tar.gz examples] and unpack them into a folder. Afterwards, unpack the examples. The easiest way to confirm that your environment is set up properly will be to compile and run the {{Code|HelloWorld}} C/C++ example. Locate the {{Code|HelloWorld.c}} file and type the following to compile the file and link the Phidget C/C++ library in a command line prompt:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Cygwin&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  gcc example.c -o HelloWorld -I&amp;quot;/cygdrive/c/Program Files/Phidgets&amp;quot; -L&amp;quot;/cygdrive/c/Program Files/Phidgets&amp;quot; -lphidget21&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;MinGW&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  gcc example.c -o HelloWorld -I&amp;quot;C:\Program Files\Phidgets&amp;quot; -L&amp;quot;C:\Program Files\Phidgets&amp;quot; -lphidget21&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After using gcc, you will have an executable named {{Code|HelloWorld}} that you can run.  &lt;br /&gt;
It is assumed that {{Code|phidget21.h}} and {{Code|phidget21.lib}} are placed in {{Code|C:\Program Files\Phidgets}}. If the files are placed in another location, please adjust the paths to the file&#039;s location accordingly.&lt;br /&gt;
&lt;br /&gt;
After using {{Code|gcc}}, you will have an executable named {{Code|HelloWorld}} that you can run.  &lt;br /&gt;
&lt;br /&gt;
This program will detect for devices that are attached/detached on the computer. Go ahead, and attach or detach your devices! Here is an example output:&lt;br /&gt;
&lt;br /&gt;
[[File:C MinGW HelloWorld Output.PNG|link=|alt=HelloWorld Output]]&lt;br /&gt;
&lt;br /&gt;
After confirming that the {{Code|HelloWorld}} example is working, you can proceed to run the example for your device. If you aren&#039;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]].&lt;br /&gt;
&lt;br /&gt;
=====Write Your Own Code=====&lt;br /&gt;
&lt;br /&gt;
When writing your code from scratch, you start it as you would any C/C++ code with Cygwin/MinGW in your favourite text editor. In your &#039;&#039;&#039;{{Code|.c}}&#039;&#039;&#039; source code file, you must include a reference to the library header:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=cpp&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  #include &amp;lt;phidget21.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then, you would compile your completed C/C++ code the same way as shown in the [[#Use Our Examples 5| Use Our Examples]] section above. &lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
===Dev C++===&lt;br /&gt;
&lt;br /&gt;
=====Use Our Examples=====&lt;br /&gt;
&lt;br /&gt;
1. Download the [http://www.phidgets.com/downloads/examples/phidget21-c-examples_2.1.8.20111028.tar.gz examples] and unpack them into a folder. Here, you can find example programs for all the devices. If you aren&#039;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]]. You will need this example source code to be copied into your Dev C++ project later on. The easiest way to confirm that your environment is set up properly will be to compile and run the {{Code|HelloWorld}} C/C++ example.&lt;br /&gt;
&lt;br /&gt;
2. In order to control Phidgets with Dev C++, we will use the {{Code|reimp}} tool to convert the {{Code|phidget21.lib}} to a format that Dev C++ accepts. Download the [http://www.phidgets.com reimp tool].&lt;br /&gt;
&lt;br /&gt;
3. Open up command line and traverse to the directory containing the reimp tool. Type the following command to create {{Code|libphidget21.a}}.&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  reimp.exe &amp;quot;C:\Program Files\Phidgets\phidget21.lib&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
The command above assumes that the {{Code|phidget21.lib}} is in the corresponding directory. If the file is placed in another location, please adjust the path to the file&#039;s location accordingly. Please note that the 64 bit version of {{Code|phidget21.lib}} is not supported on Dev C/C++. Please use the 32 bit version of {{Code|phidget21.lib}}.&lt;br /&gt;
&lt;br /&gt;
4. Place {{Code|libphidget21.a}} in {{Code|&amp;lt;Dev-Cpp Install Directory&amp;gt;/lib}}.&lt;br /&gt;
&lt;br /&gt;
5. Next, a new project will need to be created. Generate a new console application with a descriptive name such as PhidgetTest. Please select C as the project type.&lt;br /&gt;
&lt;br /&gt;
[[File:DevC New Project.PNG|link=|alt=New Project]]&lt;br /&gt;
&lt;br /&gt;
6. Next, the project settings needs to be set up. Navigate to Project Options &amp;amp;rarr; Directories &amp;amp;rarr; Include Directories.&lt;br /&gt;
&lt;br /&gt;
7. Add a new path to {{Code|C:\Program Files\Phidgets}}. 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&#039;s location accordingly.&lt;br /&gt;
&lt;br /&gt;
[[File:DevC Header.PNG|link=|alt=Header File]]&lt;br /&gt;
&lt;br /&gt;
8. Navigate to Project Options &amp;amp;rarr; Parameters &amp;amp;rarr; Linker.&lt;br /&gt;
&lt;br /&gt;
9. Add {{Code|-lphidget21}} to the field. This step will find the {{Code|libphidget21.a}} file in {{Code|&amp;lt;Dev-Cpp Install Directory&amp;gt;/lib}}. &lt;br /&gt;
&lt;br /&gt;
[[File:DevC Library.PNG|link=|alt=Library File]]&lt;br /&gt;
&lt;br /&gt;
10. To import the {{Code|HelloWorld}} program into your project, please open up {{Code|main.c}} in the editor.&lt;br /&gt;
&lt;br /&gt;
11. An empty C file will pop up. Please copy and paste the contents of the example program. &lt;br /&gt;
&lt;br /&gt;
[[File:DevC Source.PNG|link=|alt=Source Code]]&lt;br /&gt;
&lt;br /&gt;
12. Now, you can run the example. Click on Execute &amp;amp;rarr; Compile &amp;amp; Run.&lt;br /&gt;
&lt;br /&gt;
[[File:DevC Run.PNG|link=|alt=Run]]&lt;br /&gt;
&lt;br /&gt;
13. This program will detect for devices that are attached/detached on the computer. Go ahead, and attach or detach your devices! Here is an example output:&lt;br /&gt;
&lt;br /&gt;
[[File:DevC HelloWorld Output.PNG|link=|alt=HelloWorld Output]]&lt;br /&gt;
&lt;br /&gt;
After confirming that the {{Code|HelloWorld}} example is working, you can proceed to run the example for your device. If you aren&#039;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]].&lt;br /&gt;
&lt;br /&gt;
Once you have the C/C++ examples running, we have a [[#Follow the Examples|teaching]] section below to help you follow them.&lt;br /&gt;
&lt;br /&gt;
=====Write Your Own Code=====&lt;br /&gt;
&lt;br /&gt;
When you are building a project from scratch, or adding Phidget function calls to an existing project, you&#039;ll need to configure your development environment to properly link the Phidget C/C++ library. Please see the [[#Use Our Examples 6 | Use Our Examples]] section for instructions.&lt;br /&gt;
&lt;br /&gt;
In your &#039;&#039;&#039;{{Code|.c}}&#039;&#039;&#039; source code file, you must include a reference to the library header:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=cpp&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  #include &amp;lt;phidget21.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then, you would compile your completed C/C++ code the same way as the [[#Use Our Examples 6 | examples]] above. &lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
==OS X==&lt;br /&gt;
&lt;br /&gt;
C/C++ has excellent support on OS X through the gcc compiler.  &lt;br /&gt;
&lt;br /&gt;
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 - OS X]] page also describes the different Phidget files, their installed locations, and their roles. &lt;br /&gt;
&lt;br /&gt;
{{ContentNeeded|The information we say that we say on the OS - OS X page (i.e. the different files, locations, and roles) should actually be added there}}&lt;br /&gt;
&lt;br /&gt;
===Use Our Examples===&lt;br /&gt;
&lt;br /&gt;
After installing the Phidget C/C++ library for OS X as above, you&#039;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&#039;ll need to find the source code &#039;&#039;for your specific device&#039;&#039;.  Then, compile the code under your platform and run it.&lt;br /&gt;
&lt;br /&gt;
The examples assume that the compiled libraries have been set up properly.  To set them up on OS X, follow the &#039;&#039;Getting Started&#039;&#039; page for [[Device List | your specific device]]&lt;br /&gt;
&lt;br /&gt;
To compile, link the Phidget C/c++ library, and build an executable binary on OS X, do (for example, depending on the Headers location):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  gcc example.c -o example -framework Phidget21 -I/Library/Frameworks/Phidget21.framework/Headers&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
After using gcc, you will have an executable named {{Code|example}} that you can run.&lt;br /&gt;
&lt;br /&gt;
===Write Your Own Code===&lt;br /&gt;
&lt;br /&gt;
When writing your code from scratch, you must include a reference to the library header:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=cpp&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  #include &amp;lt;phidget21.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
==Linux==&lt;br /&gt;
&lt;br /&gt;
C/C++ has support on Linux through the gcc compiler.  &lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
===Use Our Examples===&lt;br /&gt;
&lt;br /&gt;
After installing the Phidget libraries for Linux as above, you&#039;re ready to download and run the examples:&lt;br /&gt;
*[http://www.phidgets.com/downloads/examples/phidget21-c-examples_2.1.8.20111028.tar.gz Generic C/C++ Examples]&lt;br /&gt;
&lt;br /&gt;
To run the example code, you&#039;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 &#039;&#039;software object&#039;&#039; 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.&lt;br /&gt;
&lt;br /&gt;
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}}:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  gcc example.c -o example -lphidget21&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this case, {{Code|example.c}} would be the &#039;&#039;&#039;.c&#039;&#039;&#039; file specific to your device.  After using gcc, you will have an executable named {{Code|example}} that you can run.  &lt;br /&gt;
&lt;br /&gt;
On Linux, if you have not set up [[OS_-_Linux#Setting_udev_Rules | your udev rules for USB access]], you will need to run the program &#039;&#039;&#039;as root&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  sudo ./example&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Write Your Own Code===&lt;br /&gt;
&lt;br /&gt;
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 &#039;&#039;&#039;{{Code|.c}}&#039;&#039;&#039; source code file, you must include a reference to the library header:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=cpp&amp;gt;&lt;br /&gt;
  #include &amp;lt;phidget21.h&amp;gt;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then, you would compile your completed C/C++ code the same way as the examples above. &lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
==Windows CE==&lt;br /&gt;
&lt;br /&gt;
===Description of Library Files===&lt;br /&gt;
C/C++ programs on Windows CE depend on the following files, which the Windows CE installer puts onto your system:&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|phidget21.dll}}&amp;lt;/b&amp;gt; contains the actual Phidget library, which is used at run-time.  It is placed in {{Code|\Windows}}.&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|PhidgetWebService21.exe}}&amp;lt;/b&amp;gt; is used to control Phidgets remotely across a network using the [[#WebService | PhidgetWebService]]. It can be placed anywhere on the system.&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|phidget21.lib}}&amp;lt;/b&amp;gt; is used by your compiler to link to the dll.  Your compiler has to know where this file is.&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|phidget21.h}}&amp;lt;/b&amp;gt; lists all the Phidget API function calls available to your code.  Your compiler also has to know where this file is. &lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|phidget.dll}}&amp;lt;/b&amp;gt; is the Phidgets kernel driver. It is placed in {{Code|\Windows}}.&lt;br /&gt;
&lt;br /&gt;
===Visual Studio 2005/2008/2010===&lt;br /&gt;
&lt;br /&gt;
=====Use Our Examples=====&lt;br /&gt;
&lt;br /&gt;
Currently, we have no example code for C/C++ on Windows CE. However, set up is very much the same as what it would be with [[#Visual Studio 2005/2008/2010 |Visual Studio 2005/2008/2010]] in Windows. The {{Code|phidget21.h}} and {{Code|phidget21.lib}} can be downloaded [http://www.phidgets.com/downloads/libraries/Phidget21-wincedevel_2.1.8.20120216.zip here].&lt;br /&gt;
&lt;br /&gt;
=====Write Your Own Code=====&lt;br /&gt;
&lt;br /&gt;
When you are building a project from scratch, or adding Phidget function calls to an existing project, you&#039;ll need to configure your development environment to properly link the Phidget C/C++ library. To begin: &lt;br /&gt;
&lt;br /&gt;
1. Generate a new Visual C++: Win32 Smart Device project with a descriptive name such as PhidgetTest.&lt;br /&gt;
&lt;br /&gt;
[[File:WinCE VS C NewProject 1.PNG|link=|alt=New Project]]&lt;br /&gt;
&lt;br /&gt;
2. Select {{Code|Next}}.&lt;br /&gt;
&lt;br /&gt;
[[File:WinCE VS C NewProject 2.PNG|link=|alt=New Project]]&lt;br /&gt;
&lt;br /&gt;
3. Select the SDK(s) that you want to code against and elect {{Code|Next}}.&lt;br /&gt;
&lt;br /&gt;
[[File:WinCE VS C NewProject 3.PNG|link=|alt=SDKs]]&lt;br /&gt;
&lt;br /&gt;
4. Create a console application and select {{Code|Next}}.&lt;br /&gt;
&lt;br /&gt;
[[File:WinCE VS C NewProject 4.PNG|link=|alt=Create Console Application]]&lt;br /&gt;
&lt;br /&gt;
3. Open the project properties window.&lt;br /&gt;
&lt;br /&gt;
4. Navigate to Configuration Properties &amp;amp;rarr; C/C++.&lt;br /&gt;
&lt;br /&gt;
5. Add {{Code|&amp;quot;C:\Program Files\Phidgets&amp;quot;}} 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&#039;s location accordingly.&lt;br /&gt;
&lt;br /&gt;
[[File:VS2005 Header.PNG|link=|alt=Header File]]&lt;br /&gt;
&lt;br /&gt;
6. Navigate to Configuration Properties &amp;amp;rarr; Linker &amp;amp;rarr; Input.&lt;br /&gt;
&lt;br /&gt;
7. Edit the additional dependencies and add {{Code|&amp;quot;C:\Program  Files\Phidgets\phidget21.lib&amp;quot;}}. 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&#039;s location accordingly.&lt;br /&gt;
&lt;br /&gt;
[[File:VS2005 Library.PNG|link=|alt=Library File]]&lt;br /&gt;
&lt;br /&gt;
8. The project now has access to the Phidget function calls and you are ready to begin coding.&lt;br /&gt;
&lt;br /&gt;
Then, in your code, you will need to include the Phidget C/C++ library:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=cpp&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  #include &amp;lt;phidget21.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The same [[#Follow the Examples|teaching]] section which describes the examples also has further resources for programming your Phidget.&lt;br /&gt;
&lt;br /&gt;
==Follow the Examples==&lt;br /&gt;
&lt;br /&gt;
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.  &lt;br /&gt;
&lt;br /&gt;
Your main reference for writing C code will be our C/C++ API information, with syntax for all of our functions:&lt;br /&gt;
&lt;br /&gt;
{{UsingAPhidgetInCodeGeneral|both of which are available in C/C++|[http://www.phidgets.com/documentation/Phidget21_C_Doc.zip C/C++ API]}}&lt;br /&gt;
&lt;br /&gt;
===Example Flow===&lt;br /&gt;
&lt;br /&gt;
{{ExamplePseudocode|In C/C++, you can name these &#039;&#039;&#039;event&#039;&#039;&#039; 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. &amp;lt;br&amp;gt; &lt;br /&gt;
In the example code, the event functions common to all Phidgets are called things like &#039;&#039;&#039;AttachHandler()&#039;&#039;&#039; and &#039;&#039;&#039;DetachHandler()&#039;&#039;&#039;, etc.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
Other functions are given in the examples to show you more detail on using your Phidget.  For example, &#039;&#039;&#039;DeviceInitialize()&#039;&#039;&#039; will show what needs to be set up for your Phidget before using it.&lt;br /&gt;
|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.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
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.|&lt;br /&gt;
[http://www.phidgets.com/documentation/Phidget21_C_Doc.zip C/C++ API]}}&lt;br /&gt;
&lt;br /&gt;
==Common Problems and Solutions/Workarounds==&lt;br /&gt;
&lt;br /&gt;
===Issue: I am using a non US-English version of Windows, and the Visual C/C++ examples run into a linker error===&lt;br /&gt;
Affected Operating Systems: &#039;&#039;&#039;Windows&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The example projects, by default finds the {{Code|phidget21.h}} and {{Code|phidget21.lib}} in ${SystemDrive}\Program Files\Phidgets. If you are using a non US-English version of Windows, the Phidget drivers may be installed into a different location. To resolve, you will have to modify the paths to these two files. For instructions, please see your environment/compiler section.&lt;/div&gt;</summary>
		<author><name>Mphi</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs21/index.php?title=OS_-_Windows&amp;diff=19066</id>
		<title>OS - Windows</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs21/index.php?title=OS_-_Windows&amp;diff=19066"/>
		<updated>2012-04-19T17:46:25Z</updated>

		<summary type="html">&lt;p&gt;Mphi: /* Hardware */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:OS]]&lt;br /&gt;
[[File:icon-Windows.png|64x64px|link=OS - Windows]]On Windows, Phidgets can be either plugged directly into a USB Port or run over a network using the [[#WebService | WebService]].&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
Phidgets are designed to run on &#039;&#039;&#039;Windows 2000 or newer&#039;&#039;&#039;, on both 32 and 64-bit systems.&lt;br /&gt;
&lt;br /&gt;
==Getting Started (Libraries and Drivers)==&lt;br /&gt;
&lt;br /&gt;
If this is your first Phidget, we highly recommend working through the Getting Started guide [[Device List|for your specific Phidget device]]. If you already have the [[File:Ph.jpg]] icon in your task bar and know how to use it, then you&#039;ve already followed the guide and are ready to learn more about the control panel, the Phidget WebService, and more - all specific to Windows.&lt;br /&gt;
&lt;br /&gt;
If you are already a pro, and just want the drivers:&lt;br /&gt;
&lt;br /&gt;
*[http://www.phidgets.com/downloads/libraries/Phidget-x86_2.1.8.20111028.exe 32 Bit]&lt;br /&gt;
*[http://www.phidgets.com/downloads/libraries/Phidget-x64_2.1.8.20111028.exe 64 Bit]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
For special cases where you want to install the Phidget libraries without the installer, please see the [[#Advanced Uses | Advanced Uses]] section.&lt;br /&gt;
*[http://www.phidgets.com/downloads/libraries/Phidget21-windevel_2.1.8.20110615.zip Phidget21 Libraries] (32-Bit and 64-Bit development files without an installer)&lt;br /&gt;
*[http://www.phidgets.com/Drivers_Info.html#windows Software License]&lt;br /&gt;
&lt;br /&gt;
===Installing===&lt;br /&gt;
&lt;br /&gt;
The Phidget installer requires that your system has .NET framework 2.0 or higher. The .NET framework can be downloaded from [http://www.microsoft.com/net Microsoft]. If you do not have the .NET framework 2.0 or later installed, you can still use Phidgets. However, you won&#039;t be able to use the installer, and will have to manually install the Phidget libraries. Please see the [[#Advanced Uses | Advanced Uses]] section.&lt;br /&gt;
&lt;br /&gt;
To install the libraries, follow these steps:&lt;br /&gt;
&lt;br /&gt;
1. Download one of the Phidget installer for your system, depending on whether your system is 32 or 64-bit. &lt;br /&gt;
*[http://www.phidgets.com/downloads/libraries/Phidget-x86_2.1.8.20120216.exe 32-bit]&lt;br /&gt;
*[http://www.phidgets.com/downloads/libraries/Phidget-x64_2.1.8.20120216.exe 64-bit]&lt;br /&gt;
&lt;br /&gt;
2. Open up the installer, and proceed through the wizard. &lt;br /&gt;
&lt;br /&gt;
[[File:Windows Install.PNG|link=|alt=Windows Install]]&lt;br /&gt;
&lt;br /&gt;
Please note that by default, the installer places the [[#Description of Installer files | Phidget libraries]] in {{Code|C:\Program Files\Phidgets}}. &lt;br /&gt;
&lt;br /&gt;
3. Once the installation is complete, you are ready to program with Phidgets. To find out what files got installed, please see [[#Description of Installer files | Description of Installer files]] in the Appendix section.&lt;br /&gt;
&lt;br /&gt;
Next, the Phidget Control Panel will be discussed.&lt;br /&gt;
&lt;br /&gt;
====Phidget Control Panel====&lt;br /&gt;
&lt;br /&gt;
The Phidget Control Panel is a tool to quickly determine whether your system is able to communicate Phidgets, and also act as a debugging tool. &lt;br /&gt;
&lt;br /&gt;
Once the Phidget libraries are installed using the installer, you should see the [[File:Ph.jpg]] icon in the taskbar. Double click on it to bring up the Phidget Control Panel. If the icon does not appear, just find and open the Phidget Control Panel from the start menu.&lt;br /&gt;
&lt;br /&gt;
[[File:Windows ControlPanel General.PNG|link=|alt=Windows Control Panel General]]&lt;br /&gt;
&lt;br /&gt;
The general tab shows the list of Phidgets currently physically attached to the computer. You can also view the currently installed Phidget library version, as well as having the checkbox option to choose whether the Phidget Control Panel is to be started up automatically once Windows boots up. You can double click on a Phidget device in the Phidget Control Panel to open up an example program for the device.&lt;br /&gt;
&lt;br /&gt;
[[File:Windows ControlPanel Example.PNG|link=|alt=Windows Control Panel Example]]&lt;br /&gt;
&lt;br /&gt;
In the above screenshot, the RFID example was opened. These examples are intended for demonstration and debugging purposes. If you have not yet already, please see the &#039;&#039;&#039;Getting Started&#039;&#039;&#039; guide [[Device List|for your device]]. It is important to keep in mind that when an example Phidget application is opened from the Phidget Control Panel or opened from any of your Phidget applications that you develop, it holds a lock on the Phidget. This prevents any other program from accessing the Phidget. Please ensure that this example application is closed(the Phidget Control Panel can still be running) when you are running your own applications. &lt;br /&gt;
&lt;br /&gt;
The next tab is the {{Code|WebService}} tab, which allows you to control Phidgets over a network. There are four sub-tabs. The first sub-tab is the {{Code|Setup}} tab.&lt;br /&gt;
&lt;br /&gt;
[[File:Windows ControlPanel WebService Setup Running.PNG|link=|alt=Windows Control Panel WebService Setup]]&lt;br /&gt;
&lt;br /&gt;
Here, you can start and stop the WebService. Details are provided in the [[#WebService | WebService]] section. You can also determine whether the Phidget WebService is currently running. There is also a check box that you can select to turn on verbose output, which will display useful troubleshooting information for the WebService.&lt;br /&gt;
&lt;br /&gt;
The next sub-tab is the {{Code|Output}} tab, which provides useful debugging information while you are using the WebService. This tab will only show up if the verbose output option is selected in the previous tab. &lt;br /&gt;
&lt;br /&gt;
[[File:Windows ControlPanel WebService Output.PNG|link=|alt=Windows Control Panel WebService Output]]&lt;br /&gt;
&lt;br /&gt;
The {{Code|Dictionary}} sub-tab comes next; it lists all the key-value pairs that gets created when the WebService runs. More information is provided in the [[General Phidget Programming#Using the Dictionary|Dictionary]] section of the [[General Phidget Programming|General Phidget Programming]] page. &lt;br /&gt;
&lt;br /&gt;
[[File:Windows ControlPanel WebService Dictionary.PNG|link=|alt=Windows Control WebService Dictionary]]&lt;br /&gt;
&lt;br /&gt;
The {{Code|Bonjour}} sub-tab gives a list of all currently attached Phidgets that are connected to the WebService. This tab will only appear if you have [http://www.apple.com/support/downloads/bonjourforwindows.html Bonjour] installed onto your system. You can also double click on the Phidget to connect to it over the network using one or more computers, but still use the Phidget on the computer it is directly connected to.&lt;br /&gt;
&lt;br /&gt;
[[File:Windows ControlPanel WebService Bonjour.PNG|link=|alt=Windows Control Panel WebService Bonjour]]&lt;br /&gt;
&lt;br /&gt;
The last tab is the {{Code|PhidgetSBC}} tab, which displays the complete list of PhidgetSBCs connected to the network. This tab will be enabled if you have [http://www.apple.com/support/downloads/bonjourforwindows.html Bonjour] installed onto your system.&lt;br /&gt;
&lt;br /&gt;
[[File:Windows ControlPanel WebService PhidgetSBC.PNG|link=|alt=Windows Control Panel WebService PhidgetSBC]]&lt;br /&gt;
&lt;br /&gt;
You can double click on the PhidgetSBC to bring up the PhidgetSBC Administration Console log-in page in your default browser.&lt;br /&gt;
&lt;br /&gt;
[[File:PhidgetSBCAdminConsole.PNG|link=|alt=PhidgetSBC Admin Console]]&lt;br /&gt;
&lt;br /&gt;
The PhidgetSBC Administration Console is where you can go to configure the PhidgetSBC. For more details, please see the [[1072 0 - Getting Started | PhidgetSBC]] section.&lt;br /&gt;
&lt;br /&gt;
===Checking===&lt;br /&gt;
&lt;br /&gt;
To confirm the libraries were installed and work correctly, you can check both the hardware and software components of the interface. It is worth checking the software side first, because if it works then you know the hardware side is also okay.&lt;br /&gt;
&lt;br /&gt;
====Software====&lt;br /&gt;
&lt;br /&gt;
If you have the Phidgets library installed on your system, you can verify that the software side component is working by seeing if a Phidget device is listed in the {{Code|General}} tab of the Phidget Control Panel.&lt;br /&gt;
&lt;br /&gt;
[[File:Windows_ControlPanel_General.PNG|link=|alt=Windows Control Panel General]]&lt;br /&gt;
&lt;br /&gt;
The above screenshot shows that a PhidgetRFID and a PhidgetInterfaceKit are attached to the computer. If you see your Phidget in the list, you can continue to the [[#Programming Languages | programming languages]] section to learn more. If you are not able to see that the Phidget is in the list, there may be a hardware issue. Please see the [[#Hardware| hardware]] section for more details.&lt;br /&gt;
&lt;br /&gt;
====Hardware====&lt;br /&gt;
&lt;br /&gt;
You can verify that your computer detects that the Phidget is plugged in through a USB connection by going to the Windows Device Manager. &lt;br /&gt;
On Windows XP, you can access the Device Manager by accessing the start menu, right clicking on {{Code|Computer}} and selecting {{Code|Properties}}. Next, select {{Code|Advanced System Settings}} to open up a new Window. Here, open up the {{Code|Hardware}} tab and select {{Code|Device Manager}}. The Device Manager window will open.  &lt;br /&gt;
&lt;br /&gt;
[[File:Windows7 DeviceManager.PNG|link=|alt=Windows Device Manager]]&lt;br /&gt;
&lt;br /&gt;
Under the {{Code|Human Interface Devices}} heading, you can view whether your computer detects that the Phidget is connected through the USB if it is in the list. There should be a {{Code|HID-compliant device}} and a {{Code|USB Input Device}} entry for every Phidget that is attached to the computer. Please note that there is currently no way of directly determining which entry belongs to which Phidget. A simple way of verifying which entry belongs to which Phidget is to simply connect or disconnect the Phidget from the USB port of the computer. The list will automatically refresh to show the updated list of all connected USB devices.&lt;br /&gt;
&lt;br /&gt;
If you don&#039;t see the Phidget in the list, then take a look at the [[#Troubleshooting|troubleshooting]] section below, as well as the &#039;&#039;&#039;Communications&#039;&#039;&#039; section of our [[General Troubleshooting#Communications Troubleshooting|general troubleshooting page]].&lt;br /&gt;
&lt;br /&gt;
====Troubleshooting====&lt;br /&gt;
&lt;br /&gt;
If the examples &#039;&#039;&#039;do not&#039;&#039;&#039; work but USB &#039;&#039;&#039;does&#039;&#039;&#039; work (i.e. your computer can consistently see the device in the [[#Hardware|hardware]]), take a moment to check the basics:&lt;br /&gt;
* You are using Windows 2000 or newer. &lt;br /&gt;
* The Phidget Control Panel requires that you have .NET framework 2.0 or newer.&lt;br /&gt;
* No other programs, drivers, or processes are using that USB port in software. Some drivers or software will sometimes mistakenly claim Phidget devices when waiting on some hardware to be connected. Please see the section: [[Communication Protocols#Issue: Some third party software prevents communications with Phidgets|third party software prevents communications with Phidgets]] for more information.&lt;br /&gt;
* The Phidget libraries are the latest version (visit the [[#Getting Started (Libraries and Drivers)| getting started section]] to download them)&lt;br /&gt;
* Check the [[#Common Problems and Solutions|common problems]] section below, some specific combinations can cause problems&lt;br /&gt;
&lt;br /&gt;
If your problem doesn&#039;t seem to be fixed by these steps, make sure that the Phidget is seen &#039;&#039;&#039;consistently&#039;&#039;&#039; by USB (if it is erratic, try our [[General Troubleshooting|general troubleshooting guide]]).  If you are still having problems after using the troubleshooting guide, please [[Contact Information|ask us]]!&lt;br /&gt;
&lt;br /&gt;
==Programming Languages==&lt;br /&gt;
&lt;br /&gt;
Phidgets’ philosophy is that you do not have to be an electrical engineer in order to do projects that use devices like sensors, motors, motor controllers, and interface boards. All you need to know is how to program.&lt;br /&gt;
&lt;br /&gt;
After you have installed the drivers above, you should pick a programming language, install libraries, and run the examples for that specific language. You can learn more about what is needed to program in a particular language by choosing the language of your preference below. If you need help choosing a language, please look at the [[Software Overview#Language Support |language comparison table]]. &lt;br /&gt;
&lt;br /&gt;
On Windows, we recommend the following languages:&lt;br /&gt;
&lt;br /&gt;
*[[Language - C Sharp|C#]]&lt;br /&gt;
*[[Language - C/C++|C/C++]]&lt;br /&gt;
*[[Language - Flash AS3|Flash AS3]]&lt;br /&gt;
*[[Language - Java|Java]]&lt;br /&gt;
*[[Language - LabVIEW|LabView]]&lt;br /&gt;
*[[Language - Max/MSP|Max/MSP]]&lt;br /&gt;
*[[Language - Python|Python]]&lt;br /&gt;
*[[Language - Visual Basic .NET|Visual Basic .NET]]&lt;br /&gt;
*[[Language - Visual Basic 6.0|Visual Basic 6.0]]&lt;br /&gt;
&lt;br /&gt;
You can also use these languages, but they do not support [[General Phidget Programming#Event Driven Code | event driven code]], and must use [[General Phidget Programming#Logic Code | logic code]] only:&lt;br /&gt;
&lt;br /&gt;
*[[Language - MATLAB|MATLAB]]&lt;br /&gt;
*[[Language - Simulink|Simulink]]&lt;br /&gt;
&lt;br /&gt;
The following languages are also supported, but due to a lack of demand, the full API is not implemented. Please refer to the specific language for more information on what features are unsupported.&lt;br /&gt;
*[[Language - Adobe Director|Adobe Director]]&lt;br /&gt;
*[[Language - AutoIt|AutoIt]]&lt;br /&gt;
*[[Language - Delphi|Delphi]]&lt;br /&gt;
*[[Language - LiveCode|LiveCode]]&lt;br /&gt;
*[[Language - Visual Basic for Applications|Visual Basic for Applications]]&lt;br /&gt;
*[[Language - Visual Basic Script|Visual Basic Script]]&lt;br /&gt;
&lt;br /&gt;
==WebService==&lt;br /&gt;
&lt;br /&gt;
The Phidget WebService allows you to remotely control a Phidget over a network. For more information, please see the [[Phidget WebService|Phidget WebService]]  page.&lt;br /&gt;
&lt;br /&gt;
Drivers for the Phidget WebService on Windows are already included in the [[#Getting Started (Libraries and Drivers) | Drivers]] above. If you have a [[File:Ph.jpg]] icon in your taskbar, you already have the WebService drivers installed.&lt;br /&gt;
&lt;br /&gt;
There are two ways that you can connect to a Phidget hosted on another computer. The first method is by using the IP address/host name and port of the host computer. The second method makes the use of [http://en.wikipedia.org/wiki/Multicast_DNS mDNS], which allows Phidgets to be found and opened on the network by a server id instead of an IP address/host name. When using a server id, both the client and server will need to be running an implementation of zero configuration networking. The Phidget WebService takes advantage of the [http://www.apple.com/support/downloads/bonjourforwindows.html Bonjour] software. It is a tool, developed by Apple to locate devices such as Phidgets, and printers. You will have to install Bonjour onto your system to use the second method.&lt;br /&gt;
&lt;br /&gt;
This section helps you install, check, and use the WebService on Windows, but we also have an overview of the [[Phidget WebService]] in general.&lt;br /&gt;
&lt;br /&gt;
===Turning the WebService On and Off===&lt;br /&gt;
&lt;br /&gt;
There are two methods that can be used to turn the WebService on and off. The first method is through the Phidget Control Panel. In the {{Code|WebService}} tab, you can start, restart or stop the WebService. You can also choose to have the WebService start up automatically upon Windows boot up by selecting {{Code|Automatic}} as the {{Code|Startup Type}}. By leaving the {{Code|Startup Type}} as {{Code|Manual}}, you will have to manually turn the WebService on everytime you wish to use it. &lt;br /&gt;
&lt;br /&gt;
[[File:Windows ControlPanel WebService Setup Stopped.PNG|link=|alt=Windows Control Panel WebService Setup]]&lt;br /&gt;
&lt;br /&gt;
The second method of turning the WebService on and off is through command line. If you used our installer, the WebService utility is automatically installed in {{Code|C:\Program Files\Phidgets\PhidgetWebservice21.exe}}. Otherwise, if you wish to manually install the {{Code|PhidgetWebservice21.exe}}, you can place it anywhere on your system, and navigate to it in command line.&lt;br /&gt;
&lt;br /&gt;
You can get command line help with {{Code|PhidgetWebservice21.exe}} using the {{Code|-h}} option:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
 PhidgetWebservice21 -h&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=text&amp;gt;&lt;br /&gt;
&#039;phidgetwebservice21&#039; is a Phidget and Dictionary server from Phidgets Inc. See www.phidgets.com for more information.&lt;br /&gt;
Usage: phidgetwebservice21 [OPTION]&lt;br /&gt;
All parameters are optional. The default parameters are: port=5001, ServerName=(Computer Name) and no password&lt;br /&gt;
&lt;br /&gt;
Options:&lt;br /&gt;
  -p      Port&lt;br /&gt;
  -n      Server Name&lt;br /&gt;
  -P      Password&lt;br /&gt;
  -v      Debug mode&lt;br /&gt;
  -h      Display this help&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Mapping out which command line options to which Phidget Control Panel option is as follows:&lt;br /&gt;
&lt;br /&gt;
-p: {{Code|Port}} field&lt;br /&gt;
&lt;br /&gt;
-n: {{Code|ServerID}} field&lt;br /&gt;
&lt;br /&gt;
-P: {{Code|Password}} field&lt;br /&gt;
&lt;br /&gt;
-v: {{Code|Enable verbose output}} checkbox&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
To find the host name and IP address of  your computer, open up the command line prompt.&lt;br /&gt;
*For the default server name, type {{Code|hostname}}.&lt;br /&gt;
*For your IP address, type {{Code|ipconfig -all}}.&lt;br /&gt;
**A line in the return text, will say something like {{Code|192.168.2.198}}, which is your IP.&lt;br /&gt;
&lt;br /&gt;
Here are some usage examples.  The Windows command line is used. Traverse to the Phidget installation directory(by default, it is located in {{Code|C:\Program Files\Phidgets}}).&lt;br /&gt;
&lt;br /&gt;
To start the WebService with default parameters:&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
 PhidgetWebservice21.exe&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To start the WebService with a server name of {{Code|myServer}}:&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
 PhidgetWebservice21.exe -n myServer&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To stop the WebService, simply close the command line window or press {{Code|Ctrl}} and {{Code|c}} at the same time in the command line window.&lt;br /&gt;
&lt;br /&gt;
===Using the WebService===&lt;br /&gt;
&lt;br /&gt;
To use a Phidget over the WebService, you&#039;ll want to:&lt;br /&gt;
* Have two different computers connected to the same network. We will call the computer that has the Phidget directly connected to the USB port the host. The client will be the computer that runs a Phidget application to connect to the Phidget attached to the host. Please note that if you only have a single computer, you can also connect to the Phidget over the WebService. The computer will simply act as both a host and client. This will allow you to bypass the [[General Phidget Programming # Details for Open() | one application per Phidget limitation]]. &lt;br /&gt;
&lt;br /&gt;
* Start the WebService on the computer that directly connects to the Phidget&lt;br /&gt;
* Run your program on the remote computer that will control the Phidget over the network&lt;br /&gt;
&lt;br /&gt;
The easiest way to test these steps on Windows is [http://www.apple.com/support/downloads/bonjourforwindows.html download] and install Bonjour onto both the host and client. Next, we will set up the WebService and run the Phidget program on the client. Please follow these steps:&lt;br /&gt;
&lt;br /&gt;
1. On the host, open up the Phidget Control Panel and traverse to the {{Code|Setup}} tab of the {{Code|WebService}} section.&lt;br /&gt;
&lt;br /&gt;
[[File:Windows ControlPanel WebService Setup Stopped.PNG|link=|alt=Windows Control Panel WebService Setup Stopped]]&lt;br /&gt;
&lt;br /&gt;
2. Leave all fields the way they are, and click on {{Code|Start}} to run the WebService.&lt;br /&gt;
&lt;br /&gt;
3. You can determine that the WebService is running by looking at the WebService status at the bottom of the window.&lt;br /&gt;
&lt;br /&gt;
[[File:Windows ControlPanel WebService Setup Running.PNG|link=|alt=Windows Control Panel WebService Setup Running]]&lt;br /&gt;
&lt;br /&gt;
4. Ensure that the Phidget is plugged in to the host. &lt;br /&gt;
&lt;br /&gt;
5. On the client&#039;s Phidget Control Panel, open up the {{Code|Bonjour}} tab in the {{Code|WebService}} section. You will see the Phidget that is plugged into the host as one of the entries listed. Double click it to open the example application.&lt;br /&gt;
&lt;br /&gt;
[[File:Windows ControlPanel WebService Bonjour Running.PNG|link=|alt=Windows Control Panel WebService Bonjour]]&lt;br /&gt;
&lt;br /&gt;
6. The example application will open up, and you will be able to communicate with the Phidget over the WebService.&lt;br /&gt;
&lt;br /&gt;
[[File:Windows ControlPanel Example.PNG|link=|alt=Windows Control Panel Example]]&lt;br /&gt;
&lt;br /&gt;
7. You can confirm that the WebService was indeed behind this exchange by terminating the WebService process while still allowing the remote program to run. On the host&#039;s Phidget Control Panel, traverse to the {{Code|Setup}} tab of the {{Code|WebService}} section. Hit {{Code|Stop}} to terminate the WebService. &lt;br /&gt;
&lt;br /&gt;
[[File:Windows ControlPanel WebService Setup Running.PNG|link=|alt=Windows Control Panel WebService Setup Running]]&lt;br /&gt;
&lt;br /&gt;
8. Take a look at the example application on the client. Since the application can no longer connect to the WebService, the attached state of the Phidget is false. &lt;br /&gt;
&lt;br /&gt;
[[File:Windows ControlPanel WebService Example Disconnected.PNG|link=|alt=Windows Control Panel WebService Example while WebService Stopped]]&lt;br /&gt;
&lt;br /&gt;
===Debugging the WebService===&lt;br /&gt;
&lt;br /&gt;
In addition to enabling [[General Phidget Programming#Logging|logging]] in your Phidget code, you can get additional debugging information from the WebService itself.  This additional debugging can be enabled from the {{Code|Enable verbose output}} checkbox in the Phidget Control Panel.&lt;br /&gt;
&lt;br /&gt;
[[File:Windows ControlPanel WebService Setup Running.PNG|link=|alt=Windows Control Panel WebService Setup]]&lt;br /&gt;
&lt;br /&gt;
The debugging information is shown in the {{Code|Output}} tab.&lt;br /&gt;
&lt;br /&gt;
[[File:Windows ControlPanel WebService Output Running.PNG|link=|alt=Windows Control Panel WebService Output Running]]&lt;br /&gt;
&lt;br /&gt;
If you are using the command line approach to start the WebService, debug information is enabled by specifying the {{Code|-v}} option:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
 PhidgetWebservice21.exe -v -n &amp;quot;myServer&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The debugging information is shown command line output. This will prove useful when debugging WebService problems.&lt;br /&gt;
&lt;br /&gt;
==Advanced Uses==&lt;br /&gt;
&lt;br /&gt;
===Manual File Installation===&lt;br /&gt;
&lt;br /&gt;
The Phidget installer installs the most commonly used files onto your system. However, there may be special cases where you want to install the Phidget libraries without the installers. This section will describe the purpose of the most important files file and cover how to manually install and distribute the libraries with your code. You can get the files [http://www.phidgets.com/downloads/libraries/Phidget21-windevel_2.1.8.20110615.zip here].&lt;br /&gt;
&lt;br /&gt;
====Description of Library files====&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|phidget21.dll}}&amp;lt;/b&amp;gt; contains the actual Phidget library, which is used at run-time.  It is placed in {{Code|C:\Windows\System32}}.&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|PhidgetWebService21.exe}}&amp;lt;/b&amp;gt; is used to control Phidgets remotely across a network using the [[#WebService | PhidgetWebservce]].&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|phidget21.lib}}&amp;lt;/b&amp;gt; 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}}. &lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|phidget21.h}}&amp;lt;/b&amp;gt; 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. &lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|Phidget21.NET.dll}}&amp;lt;/b&amp;gt; is the Phidgets library for .NET framework 2.0 or greater. Any .NET language can be used, including [[Language - C Sharp | C#]]  , and [[Language - Visual Basic .NET | Visual Basic .NET]].&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|Phidget21.NET1.1.dll}}&amp;lt;/b&amp;gt; is the Phidgets library for .NET framework 1.1. Any .NET language can be used, including [[Language - C Sharp | C#]]  , and [[Language - Visual Basic .NET | Visual Basic .NET]].&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|Phidget21COM.dll}}&amp;lt;/b&amp;gt; is the Component Object Model(COM) library and provides your project access to the Phidget ActiveX objects. This libraries is used by the [[Language - Adobe Director|AdobeDirector]], [[Language - AutoIt|AutoIT]], [[Language - Delphi|Delphi]], [[Language -  Visual Basic 6.0|Visual Basic 6.0]], [[Language - Visual Basic for Applications|Visual Basic for Applications]], [[Language - Visual Basic Script|Visual Basic Script]].&lt;br /&gt;
&lt;br /&gt;
=====Special Cases of Library Install=====&lt;br /&gt;
&lt;br /&gt;
Regardless of what language you will be using to program Phidgets, you will need the {{Code|phidget21.dll}} placed in the {{Code|C:\WINDOWS\system32}} directory. Additional files are needed for the language that you choose. Please refer to the documentation provided by your [[Device List|language]] to determine what files are needed and the steps needed to install them onto your system.&lt;br /&gt;
&lt;br /&gt;
You can find the {{Code|phidget21.dll}} in the link below:&lt;br /&gt;
&lt;br /&gt;
*[http://www.phidgets.com/downloads/libraries/Phidget21-windevel_2.1.8.20110615.zip Phidget21 Libraries] (32-Bit and 64-Bit development files without an installer)&lt;br /&gt;
&lt;br /&gt;
{{Code|PhidgetWebService21.exe}} is also provided in the link above.&lt;br /&gt;
&lt;br /&gt;
===Windows in a Virtual Machine===&lt;br /&gt;
&lt;br /&gt;
Phidgets can also be used inside a virtual machine. Instructions for VMWare and VirtualBox are provided below. Virtual PC is not supported as USB Phidgets requires a virtual platform that supports HID USB Devices and since Virtual PC does not support HID USB devices, Phidgets may not be used.&lt;br /&gt;
&lt;br /&gt;
As always, please ensure that you have the latest Phidget [[#Getting Started (Libraries and Drivers) | drivers]] installed on the virtual machine and that you are using the latest version of your virtual software. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;VMWare:&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To enable USB Phidgets, select Virtual Machine -&amp;gt; Removable Devices -&amp;gt; and select the Phidget Input Device -&amp;gt; Connect. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;VirtualBox&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To enable USB Phidgets, VirtualBox Guest Additions(Devices -&amp;gt; Install Guest Additions) may need to be installed. Afterwards, click on Devices -&amp;gt; USB Devices and select the Phidget device to enable. The state should go from Busy to Captured. VirtualBox may bring up a new hardware wizard in the host operating system, which has to be installed. Please note that Phidgets with USB hubs(i.e, [[Product - 1019 1 - PhidgetInterfaceKit 8/8/8 w/6 Port Hub|1019]]), are undetectable; Fortunately, Phidgets that are attached to such Phidgets are detectable.&lt;br /&gt;
&lt;br /&gt;
==Common Problems and Solutions==&lt;br /&gt;
&lt;br /&gt;
===Issue: A corrupt installation fails on uninstall or repair===&lt;br /&gt;
Affected Operating Systems: &#039;&#039;&#039;Windows&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Solution: If the normal uninstall fails, or for whatever reason, you can choose to remove the Phidget drivers manually.&lt;br /&gt;
Please perform the following:&lt;br /&gt;
# Shut down any programs using the Phidget libraries, including the WebService and the Phidget Control Panel.&lt;br /&gt;
# Delete {{Code|C:\Program Files\Phidgets\}}&lt;br /&gt;
# Remove the Phidgets key from the Registry {{Code|[-HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services PhidgetWebService21]}}.&lt;br /&gt;
&lt;br /&gt;
In most cases this is enough to get the installer working again. If you need to remove all traces of the Phidgets libraries manually, perform the following additional steps:&lt;br /&gt;
&lt;br /&gt;
# Unregister the COM library: {{Code|regsvr32 /u &amp;quot;C:\Program Files\Phidgets\Phidget21COM.dll&amp;quot;}}.&lt;br /&gt;
# Remove {{Code|Phidget21.NET}} and {{Code|Policy.2.1.Phidget21.NET}} from {{Code|C:\Windows\Assembly\}}.&lt;br /&gt;
# Delete {{Code|C:\Documents and Settings\All Users\Application Data\Phidgets}} if you are  using WindowsXP or {{Code|C:\Users\All Users\Phidgets}} if you are using Windows Vista.&lt;br /&gt;
# Delete {{Code|C:\Windows\system32\phidget21.dll}}.&lt;br /&gt;
# Delete Phidgets from the start menu.&lt;br /&gt;
# Search for and remove keys mentioning Phidgets from the registry in the following locations:&lt;br /&gt;
:* {{Code|[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\]}}&lt;br /&gt;
:* {{Code|[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Installer\Products\]}}&lt;br /&gt;
:* {{Code|[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework\AssemblyFolders\Phidgets Inc]}}&lt;br /&gt;
:* {{Code|[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run\Phidget21Manager]}} &lt;br /&gt;
:* {{Code|[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Installer\Assemblies\Global\]}}&lt;br /&gt;
&lt;br /&gt;
:7. Reboot&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;NOTE:&#039;&#039;&#039; You can go through the registry and purge any other keys mentioning Phidgets if you still have problems, but at this point you should be able to reinstall under most cases. There will also be keys relating to the installer, the .NET library and the COM library, but they should not interfere with anything.&lt;br /&gt;
&lt;br /&gt;
===Issue: Event data is sporadic/slow/clumped over the WebService===&lt;br /&gt;
Affected Operating Systems: &#039;&#039;&#039;Windows&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Windows implements 200ms delayed ACKs for network traffic. When traffic is one-way only - as it is with event data, the data will all arrive in clumps every 200ms because of delayed ACKs. &lt;br /&gt;
&lt;br /&gt;
This can be a great drawback for applications which rely on low latency event data over the network. (source: http://support.microsoft.com/kb/214397)&lt;br /&gt;
&lt;br /&gt;
This delayed ACK behavior can be disabled in windows to decrease event latency as documented here: http://support.microsoft.com/kb/328890&lt;br /&gt;
&lt;br /&gt;
In the future, the Phidgets library may implement this differently, but so far we have been unable to match the performance achieved by disabling delayed ACK.&lt;br /&gt;
&lt;br /&gt;
==Appendix==&lt;br /&gt;
&lt;br /&gt;
===Description of Installer files===&lt;br /&gt;
&lt;br /&gt;
Here is the list of files and their description for each file the installer puts onto your system. &lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|phidget21.dll}}&amp;lt;/b&amp;gt; contains the actual Phidget library, which is used at run-time.  It is also placed in {{Code|C:\Windows\System32}}.&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|PhidgetWebService21.exe}}&amp;lt;/b&amp;gt; is used to control Phidgets remotely across a network using the [[#WebService | PhidgetWebservce]].&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|PhidgetWindowsService21.exe}}&amp;lt;/b&amp;gt; is a Windows service that controls {{Code|PhidgetWebService21.exe}}. &lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|phidget21.lib}}&amp;lt;/b&amp;gt; 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}}. &lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|phidget21.h}}&amp;lt;/b&amp;gt; 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. &lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|phidget21.jar}}&amp;lt;/b&amp;gt; is an archive containing the Phidgets library, used by the [[Language -  Java | Java]] programming language.&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|Phidget21.NET.dll}}&amp;lt;/b&amp;gt; is the Phidgets library for .NET framework 2.0 or greater. Any .NET language can be used, including [[Language - C Sharp | C#]]  , and [[Language - Visual Basic .NET | Visual Basic .NET]].&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|Phidget21.NET1.1.dll}}&amp;lt;/b&amp;gt; is the Phidgets library for .NET framework 1.1. Any .NET language can be used, including [[Language - C Sharp | C#]]  , and [[Language - Visual Basic .NET | Visual Basic .NET]].&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|Phidget21.NET.XML}}&amp;lt;/b&amp;gt; provides the IntelliSense in-line documentation for the .NET library in Visual Studio.&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|Phidget21COM.dll}}&amp;lt;/b&amp;gt; is the Component Object Model(COM) library and provides your project access to the Phidget ActiveX objects. This libraries is used by the [[Language - Adobe Director|AdobeDirector]], [[Language - AutoIt|AutoIT]], [[Language - Delphi|Delphi]], [[Language -  Visual Basic 6.0|Visual Basic 6.0]], [[Language - Visual Basic for Applications|Visual Basic for Applications]], [[Language - Visual Basic Script|Visual Basic Script]].&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|Phidget21Manager.exe}}&amp;lt;/b&amp;gt; is a tool to quickly determine whether your system is able to control Phidgets, and also act as a debugging tool. &lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|Examples}} folder&amp;lt;/b&amp;gt; contain example applications that allows you to quickly see if your Phidget is properly configured.&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|x86 folder}} folder&amp;lt;/b&amp;gt; contain the 32 bit versions of {{Code|phidget21.dll}}, {{Code|phidget21.lib}}, {{Code|Phidget21COM.dll}}. These folder will only appear on 64 bit installations and is useful if you want to code against the 32 bit libraries.&lt;/div&gt;</summary>
		<author><name>Mphi</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs21/index.php?title=OS_-_Windows&amp;diff=19065</id>
		<title>OS - Windows</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs21/index.php?title=OS_-_Windows&amp;diff=19065"/>
		<updated>2012-04-19T17:45:29Z</updated>

		<summary type="html">&lt;p&gt;Mphi: /* Hardware */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:OS]]&lt;br /&gt;
[[File:icon-Windows.png|64x64px|link=OS - Windows]]On Windows, Phidgets can be either plugged directly into a USB Port or run over a network using the [[#WebService | WebService]].&lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
Phidgets are designed to run on &#039;&#039;&#039;Windows 2000 or newer&#039;&#039;&#039;, on both 32 and 64-bit systems.&lt;br /&gt;
&lt;br /&gt;
==Getting Started (Libraries and Drivers)==&lt;br /&gt;
&lt;br /&gt;
If this is your first Phidget, we highly recommend working through the Getting Started guide [[Device List|for your specific Phidget device]]. If you already have the [[File:Ph.jpg]] icon in your task bar and know how to use it, then you&#039;ve already followed the guide and are ready to learn more about the control panel, the Phidget WebService, and more - all specific to Windows.&lt;br /&gt;
&lt;br /&gt;
If you are already a pro, and just want the drivers:&lt;br /&gt;
&lt;br /&gt;
*[http://www.phidgets.com/downloads/libraries/Phidget-x86_2.1.8.20111028.exe 32 Bit]&lt;br /&gt;
*[http://www.phidgets.com/downloads/libraries/Phidget-x64_2.1.8.20111028.exe 64 Bit]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
For special cases where you want to install the Phidget libraries without the installer, please see the [[#Advanced Uses | Advanced Uses]] section.&lt;br /&gt;
*[http://www.phidgets.com/downloads/libraries/Phidget21-windevel_2.1.8.20110615.zip Phidget21 Libraries] (32-Bit and 64-Bit development files without an installer)&lt;br /&gt;
*[http://www.phidgets.com/Drivers_Info.html#windows Software License]&lt;br /&gt;
&lt;br /&gt;
===Installing===&lt;br /&gt;
&lt;br /&gt;
The Phidget installer requires that your system has .NET framework 2.0 or higher. The .NET framework can be downloaded from [http://www.microsoft.com/net Microsoft]. If you do not have the .NET framework 2.0 or later installed, you can still use Phidgets. However, you won&#039;t be able to use the installer, and will have to manually install the Phidget libraries. Please see the [[#Advanced Uses | Advanced Uses]] section.&lt;br /&gt;
&lt;br /&gt;
To install the libraries, follow these steps:&lt;br /&gt;
&lt;br /&gt;
1. Download one of the Phidget installer for your system, depending on whether your system is 32 or 64-bit. &lt;br /&gt;
*[http://www.phidgets.com/downloads/libraries/Phidget-x86_2.1.8.20120216.exe 32-bit]&lt;br /&gt;
*[http://www.phidgets.com/downloads/libraries/Phidget-x64_2.1.8.20120216.exe 64-bit]&lt;br /&gt;
&lt;br /&gt;
2. Open up the installer, and proceed through the wizard. &lt;br /&gt;
&lt;br /&gt;
[[File:Windows Install.PNG|link=|alt=Windows Install]]&lt;br /&gt;
&lt;br /&gt;
Please note that by default, the installer places the [[#Description of Installer files | Phidget libraries]] in {{Code|C:\Program Files\Phidgets}}. &lt;br /&gt;
&lt;br /&gt;
3. Once the installation is complete, you are ready to program with Phidgets. To find out what files got installed, please see [[#Description of Installer files | Description of Installer files]] in the Appendix section.&lt;br /&gt;
&lt;br /&gt;
Next, the Phidget Control Panel will be discussed.&lt;br /&gt;
&lt;br /&gt;
====Phidget Control Panel====&lt;br /&gt;
&lt;br /&gt;
The Phidget Control Panel is a tool to quickly determine whether your system is able to communicate Phidgets, and also act as a debugging tool. &lt;br /&gt;
&lt;br /&gt;
Once the Phidget libraries are installed using the installer, you should see the [[File:Ph.jpg]] icon in the taskbar. Double click on it to bring up the Phidget Control Panel. If the icon does not appear, just find and open the Phidget Control Panel from the start menu.&lt;br /&gt;
&lt;br /&gt;
[[File:Windows ControlPanel General.PNG|link=|alt=Windows Control Panel General]]&lt;br /&gt;
&lt;br /&gt;
The general tab shows the list of Phidgets currently physically attached to the computer. You can also view the currently installed Phidget library version, as well as having the checkbox option to choose whether the Phidget Control Panel is to be started up automatically once Windows boots up. You can double click on a Phidget device in the Phidget Control Panel to open up an example program for the device.&lt;br /&gt;
&lt;br /&gt;
[[File:Windows ControlPanel Example.PNG|link=|alt=Windows Control Panel Example]]&lt;br /&gt;
&lt;br /&gt;
In the above screenshot, the RFID example was opened. These examples are intended for demonstration and debugging purposes. If you have not yet already, please see the &#039;&#039;&#039;Getting Started&#039;&#039;&#039; guide [[Device List|for your device]]. It is important to keep in mind that when an example Phidget application is opened from the Phidget Control Panel or opened from any of your Phidget applications that you develop, it holds a lock on the Phidget. This prevents any other program from accessing the Phidget. Please ensure that this example application is closed(the Phidget Control Panel can still be running) when you are running your own applications. &lt;br /&gt;
&lt;br /&gt;
The next tab is the {{Code|WebService}} tab, which allows you to control Phidgets over a network. There are four sub-tabs. The first sub-tab is the {{Code|Setup}} tab.&lt;br /&gt;
&lt;br /&gt;
[[File:Windows ControlPanel WebService Setup Running.PNG|link=|alt=Windows Control Panel WebService Setup]]&lt;br /&gt;
&lt;br /&gt;
Here, you can start and stop the WebService. Details are provided in the [[#WebService | WebService]] section. You can also determine whether the Phidget WebService is currently running. There is also a check box that you can select to turn on verbose output, which will display useful troubleshooting information for the WebService.&lt;br /&gt;
&lt;br /&gt;
The next sub-tab is the {{Code|Output}} tab, which provides useful debugging information while you are using the WebService. This tab will only show up if the verbose output option is selected in the previous tab. &lt;br /&gt;
&lt;br /&gt;
[[File:Windows ControlPanel WebService Output.PNG|link=|alt=Windows Control Panel WebService Output]]&lt;br /&gt;
&lt;br /&gt;
The {{Code|Dictionary}} sub-tab comes next; it lists all the key-value pairs that gets created when the WebService runs. More information is provided in the [[General Phidget Programming#Using the Dictionary|Dictionary]] section of the [[General Phidget Programming|General Phidget Programming]] page. &lt;br /&gt;
&lt;br /&gt;
[[File:Windows ControlPanel WebService Dictionary.PNG|link=|alt=Windows Control WebService Dictionary]]&lt;br /&gt;
&lt;br /&gt;
The {{Code|Bonjour}} sub-tab gives a list of all currently attached Phidgets that are connected to the WebService. This tab will only appear if you have [http://www.apple.com/support/downloads/bonjourforwindows.html Bonjour] installed onto your system. You can also double click on the Phidget to connect to it over the network using one or more computers, but still use the Phidget on the computer it is directly connected to.&lt;br /&gt;
&lt;br /&gt;
[[File:Windows ControlPanel WebService Bonjour.PNG|link=|alt=Windows Control Panel WebService Bonjour]]&lt;br /&gt;
&lt;br /&gt;
The last tab is the {{Code|PhidgetSBC}} tab, which displays the complete list of PhidgetSBCs connected to the network. This tab will be enabled if you have [http://www.apple.com/support/downloads/bonjourforwindows.html Bonjour] installed onto your system.&lt;br /&gt;
&lt;br /&gt;
[[File:Windows ControlPanel WebService PhidgetSBC.PNG|link=|alt=Windows Control Panel WebService PhidgetSBC]]&lt;br /&gt;
&lt;br /&gt;
You can double click on the PhidgetSBC to bring up the PhidgetSBC Administration Console log-in page in your default browser.&lt;br /&gt;
&lt;br /&gt;
[[File:PhidgetSBCAdminConsole.PNG|link=|alt=PhidgetSBC Admin Console]]&lt;br /&gt;
&lt;br /&gt;
The PhidgetSBC Administration Console is where you can go to configure the PhidgetSBC. For more details, please see the [[1072 0 - Getting Started | PhidgetSBC]] section.&lt;br /&gt;
&lt;br /&gt;
===Checking===&lt;br /&gt;
&lt;br /&gt;
To confirm the libraries were installed and work correctly, you can check both the hardware and software components of the interface. It is worth checking the software side first, because if it works then you know the hardware side is also okay.&lt;br /&gt;
&lt;br /&gt;
====Software====&lt;br /&gt;
&lt;br /&gt;
If you have the Phidgets library installed on your system, you can verify that the software side component is working by seeing if a Phidget device is listed in the {{Code|General}} tab of the Phidget Control Panel.&lt;br /&gt;
&lt;br /&gt;
[[File:Windows_ControlPanel_General.PNG|link=|alt=Windows Control Panel General]]&lt;br /&gt;
&lt;br /&gt;
The above screenshot shows that a PhidgetRFID and a PhidgetInterfaceKit are attached to the computer. If you see your Phidget in the list, you can continue to the [[#Programming Languages | programming languages]] section to learn more. If you are not able to see that the Phidget is in the list, there may be a hardware issue. Please see the [[#Hardware| hardware]] section for more details.&lt;br /&gt;
&lt;br /&gt;
====Hardware====&lt;br /&gt;
&lt;br /&gt;
You can verify that your computer detects that the Phidget is plugged in through a USB connection by going to the Windows Device Manager. &lt;br /&gt;
On Windows XP, you can access the Device Manager by accessing the start menu, right clicking on {{Code|Computer}} and selecting {{Code|Properties}}. Next, select {{Code|Advanced System Settings}} to open up a new Window. Here, open up the {{Code|Hardware}} tab and select {{Code|Device Manager}}. The Device Manager window will open.  &lt;br /&gt;
&lt;br /&gt;
[[File:Windows7 DeviceManager.PNG|link=|alt=Windows Device Manager]]&lt;br /&gt;
&lt;br /&gt;
Under the {{Code|Human Interface Devices}} heading, you can view whether your computer detects that the Phidget is connected through the USB if it is in the list. There should be a {{Code|HID-compliant device}} and a {{Code|USB Human Interface Device}} entry for every Phidget that is attached to the computer. Please note that there is currently no way of directly determining which entry belongs to which Phidget. A simple way of verifying which entry belongs to which Phidget is to simply connect or disconnect the Phidget from the USB port of the computer. The list will automatically refresh to show the updated list of all connected USB devices.&lt;br /&gt;
&lt;br /&gt;
If you don&#039;t see the Phidget in the list, then take a look at the [[#Troubleshooting|troubleshooting]] section below, as well as the &#039;&#039;&#039;Communications&#039;&#039;&#039; section of our [[General Troubleshooting#Communications Troubleshooting|general troubleshooting page]].&lt;br /&gt;
&lt;br /&gt;
====Troubleshooting====&lt;br /&gt;
&lt;br /&gt;
If the examples &#039;&#039;&#039;do not&#039;&#039;&#039; work but USB &#039;&#039;&#039;does&#039;&#039;&#039; work (i.e. your computer can consistently see the device in the [[#Hardware|hardware]]), take a moment to check the basics:&lt;br /&gt;
* You are using Windows 2000 or newer. &lt;br /&gt;
* The Phidget Control Panel requires that you have .NET framework 2.0 or newer.&lt;br /&gt;
* No other programs, drivers, or processes are using that USB port in software. Some drivers or software will sometimes mistakenly claim Phidget devices when waiting on some hardware to be connected. Please see the section: [[Communication Protocols#Issue: Some third party software prevents communications with Phidgets|third party software prevents communications with Phidgets]] for more information.&lt;br /&gt;
* The Phidget libraries are the latest version (visit the [[#Getting Started (Libraries and Drivers)| getting started section]] to download them)&lt;br /&gt;
* Check the [[#Common Problems and Solutions|common problems]] section below, some specific combinations can cause problems&lt;br /&gt;
&lt;br /&gt;
If your problem doesn&#039;t seem to be fixed by these steps, make sure that the Phidget is seen &#039;&#039;&#039;consistently&#039;&#039;&#039; by USB (if it is erratic, try our [[General Troubleshooting|general troubleshooting guide]]).  If you are still having problems after using the troubleshooting guide, please [[Contact Information|ask us]]!&lt;br /&gt;
&lt;br /&gt;
==Programming Languages==&lt;br /&gt;
&lt;br /&gt;
Phidgets’ philosophy is that you do not have to be an electrical engineer in order to do projects that use devices like sensors, motors, motor controllers, and interface boards. All you need to know is how to program.&lt;br /&gt;
&lt;br /&gt;
After you have installed the drivers above, you should pick a programming language, install libraries, and run the examples for that specific language. You can learn more about what is needed to program in a particular language by choosing the language of your preference below. If you need help choosing a language, please look at the [[Software Overview#Language Support |language comparison table]]. &lt;br /&gt;
&lt;br /&gt;
On Windows, we recommend the following languages:&lt;br /&gt;
&lt;br /&gt;
*[[Language - C Sharp|C#]]&lt;br /&gt;
*[[Language - C/C++|C/C++]]&lt;br /&gt;
*[[Language - Flash AS3|Flash AS3]]&lt;br /&gt;
*[[Language - Java|Java]]&lt;br /&gt;
*[[Language - LabVIEW|LabView]]&lt;br /&gt;
*[[Language - Max/MSP|Max/MSP]]&lt;br /&gt;
*[[Language - Python|Python]]&lt;br /&gt;
*[[Language - Visual Basic .NET|Visual Basic .NET]]&lt;br /&gt;
*[[Language - Visual Basic 6.0|Visual Basic 6.0]]&lt;br /&gt;
&lt;br /&gt;
You can also use these languages, but they do not support [[General Phidget Programming#Event Driven Code | event driven code]], and must use [[General Phidget Programming#Logic Code | logic code]] only:&lt;br /&gt;
&lt;br /&gt;
*[[Language - MATLAB|MATLAB]]&lt;br /&gt;
*[[Language - Simulink|Simulink]]&lt;br /&gt;
&lt;br /&gt;
The following languages are also supported, but due to a lack of demand, the full API is not implemented. Please refer to the specific language for more information on what features are unsupported.&lt;br /&gt;
*[[Language - Adobe Director|Adobe Director]]&lt;br /&gt;
*[[Language - AutoIt|AutoIt]]&lt;br /&gt;
*[[Language - Delphi|Delphi]]&lt;br /&gt;
*[[Language - LiveCode|LiveCode]]&lt;br /&gt;
*[[Language - Visual Basic for Applications|Visual Basic for Applications]]&lt;br /&gt;
*[[Language - Visual Basic Script|Visual Basic Script]]&lt;br /&gt;
&lt;br /&gt;
==WebService==&lt;br /&gt;
&lt;br /&gt;
The Phidget WebService allows you to remotely control a Phidget over a network. For more information, please see the [[Phidget WebService|Phidget WebService]]  page.&lt;br /&gt;
&lt;br /&gt;
Drivers for the Phidget WebService on Windows are already included in the [[#Getting Started (Libraries and Drivers) | Drivers]] above. If you have a [[File:Ph.jpg]] icon in your taskbar, you already have the WebService drivers installed.&lt;br /&gt;
&lt;br /&gt;
There are two ways that you can connect to a Phidget hosted on another computer. The first method is by using the IP address/host name and port of the host computer. The second method makes the use of [http://en.wikipedia.org/wiki/Multicast_DNS mDNS], which allows Phidgets to be found and opened on the network by a server id instead of an IP address/host name. When using a server id, both the client and server will need to be running an implementation of zero configuration networking. The Phidget WebService takes advantage of the [http://www.apple.com/support/downloads/bonjourforwindows.html Bonjour] software. It is a tool, developed by Apple to locate devices such as Phidgets, and printers. You will have to install Bonjour onto your system to use the second method.&lt;br /&gt;
&lt;br /&gt;
This section helps you install, check, and use the WebService on Windows, but we also have an overview of the [[Phidget WebService]] in general.&lt;br /&gt;
&lt;br /&gt;
===Turning the WebService On and Off===&lt;br /&gt;
&lt;br /&gt;
There are two methods that can be used to turn the WebService on and off. The first method is through the Phidget Control Panel. In the {{Code|WebService}} tab, you can start, restart or stop the WebService. You can also choose to have the WebService start up automatically upon Windows boot up by selecting {{Code|Automatic}} as the {{Code|Startup Type}}. By leaving the {{Code|Startup Type}} as {{Code|Manual}}, you will have to manually turn the WebService on everytime you wish to use it. &lt;br /&gt;
&lt;br /&gt;
[[File:Windows ControlPanel WebService Setup Stopped.PNG|link=|alt=Windows Control Panel WebService Setup]]&lt;br /&gt;
&lt;br /&gt;
The second method of turning the WebService on and off is through command line. If you used our installer, the WebService utility is automatically installed in {{Code|C:\Program Files\Phidgets\PhidgetWebservice21.exe}}. Otherwise, if you wish to manually install the {{Code|PhidgetWebservice21.exe}}, you can place it anywhere on your system, and navigate to it in command line.&lt;br /&gt;
&lt;br /&gt;
You can get command line help with {{Code|PhidgetWebservice21.exe}} using the {{Code|-h}} option:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
 PhidgetWebservice21 -h&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=text&amp;gt;&lt;br /&gt;
&#039;phidgetwebservice21&#039; is a Phidget and Dictionary server from Phidgets Inc. See www.phidgets.com for more information.&lt;br /&gt;
Usage: phidgetwebservice21 [OPTION]&lt;br /&gt;
All parameters are optional. The default parameters are: port=5001, ServerName=(Computer Name) and no password&lt;br /&gt;
&lt;br /&gt;
Options:&lt;br /&gt;
  -p      Port&lt;br /&gt;
  -n      Server Name&lt;br /&gt;
  -P      Password&lt;br /&gt;
  -v      Debug mode&lt;br /&gt;
  -h      Display this help&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Mapping out which command line options to which Phidget Control Panel option is as follows:&lt;br /&gt;
&lt;br /&gt;
-p: {{Code|Port}} field&lt;br /&gt;
&lt;br /&gt;
-n: {{Code|ServerID}} field&lt;br /&gt;
&lt;br /&gt;
-P: {{Code|Password}} field&lt;br /&gt;
&lt;br /&gt;
-v: {{Code|Enable verbose output}} checkbox&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
To find the host name and IP address of  your computer, open up the command line prompt.&lt;br /&gt;
*For the default server name, type {{Code|hostname}}.&lt;br /&gt;
*For your IP address, type {{Code|ipconfig -all}}.&lt;br /&gt;
**A line in the return text, will say something like {{Code|192.168.2.198}}, which is your IP.&lt;br /&gt;
&lt;br /&gt;
Here are some usage examples.  The Windows command line is used. Traverse to the Phidget installation directory(by default, it is located in {{Code|C:\Program Files\Phidgets}}).&lt;br /&gt;
&lt;br /&gt;
To start the WebService with default parameters:&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
 PhidgetWebservice21.exe&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To start the WebService with a server name of {{Code|myServer}}:&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
 PhidgetWebservice21.exe -n myServer&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To stop the WebService, simply close the command line window or press {{Code|Ctrl}} and {{Code|c}} at the same time in the command line window.&lt;br /&gt;
&lt;br /&gt;
===Using the WebService===&lt;br /&gt;
&lt;br /&gt;
To use a Phidget over the WebService, you&#039;ll want to:&lt;br /&gt;
* Have two different computers connected to the same network. We will call the computer that has the Phidget directly connected to the USB port the host. The client will be the computer that runs a Phidget application to connect to the Phidget attached to the host. Please note that if you only have a single computer, you can also connect to the Phidget over the WebService. The computer will simply act as both a host and client. This will allow you to bypass the [[General Phidget Programming # Details for Open() | one application per Phidget limitation]]. &lt;br /&gt;
&lt;br /&gt;
* Start the WebService on the computer that directly connects to the Phidget&lt;br /&gt;
* Run your program on the remote computer that will control the Phidget over the network&lt;br /&gt;
&lt;br /&gt;
The easiest way to test these steps on Windows is [http://www.apple.com/support/downloads/bonjourforwindows.html download] and install Bonjour onto both the host and client. Next, we will set up the WebService and run the Phidget program on the client. Please follow these steps:&lt;br /&gt;
&lt;br /&gt;
1. On the host, open up the Phidget Control Panel and traverse to the {{Code|Setup}} tab of the {{Code|WebService}} section.&lt;br /&gt;
&lt;br /&gt;
[[File:Windows ControlPanel WebService Setup Stopped.PNG|link=|alt=Windows Control Panel WebService Setup Stopped]]&lt;br /&gt;
&lt;br /&gt;
2. Leave all fields the way they are, and click on {{Code|Start}} to run the WebService.&lt;br /&gt;
&lt;br /&gt;
3. You can determine that the WebService is running by looking at the WebService status at the bottom of the window.&lt;br /&gt;
&lt;br /&gt;
[[File:Windows ControlPanel WebService Setup Running.PNG|link=|alt=Windows Control Panel WebService Setup Running]]&lt;br /&gt;
&lt;br /&gt;
4. Ensure that the Phidget is plugged in to the host. &lt;br /&gt;
&lt;br /&gt;
5. On the client&#039;s Phidget Control Panel, open up the {{Code|Bonjour}} tab in the {{Code|WebService}} section. You will see the Phidget that is plugged into the host as one of the entries listed. Double click it to open the example application.&lt;br /&gt;
&lt;br /&gt;
[[File:Windows ControlPanel WebService Bonjour Running.PNG|link=|alt=Windows Control Panel WebService Bonjour]]&lt;br /&gt;
&lt;br /&gt;
6. The example application will open up, and you will be able to communicate with the Phidget over the WebService.&lt;br /&gt;
&lt;br /&gt;
[[File:Windows ControlPanel Example.PNG|link=|alt=Windows Control Panel Example]]&lt;br /&gt;
&lt;br /&gt;
7. You can confirm that the WebService was indeed behind this exchange by terminating the WebService process while still allowing the remote program to run. On the host&#039;s Phidget Control Panel, traverse to the {{Code|Setup}} tab of the {{Code|WebService}} section. Hit {{Code|Stop}} to terminate the WebService. &lt;br /&gt;
&lt;br /&gt;
[[File:Windows ControlPanel WebService Setup Running.PNG|link=|alt=Windows Control Panel WebService Setup Running]]&lt;br /&gt;
&lt;br /&gt;
8. Take a look at the example application on the client. Since the application can no longer connect to the WebService, the attached state of the Phidget is false. &lt;br /&gt;
&lt;br /&gt;
[[File:Windows ControlPanel WebService Example Disconnected.PNG|link=|alt=Windows Control Panel WebService Example while WebService Stopped]]&lt;br /&gt;
&lt;br /&gt;
===Debugging the WebService===&lt;br /&gt;
&lt;br /&gt;
In addition to enabling [[General Phidget Programming#Logging|logging]] in your Phidget code, you can get additional debugging information from the WebService itself.  This additional debugging can be enabled from the {{Code|Enable verbose output}} checkbox in the Phidget Control Panel.&lt;br /&gt;
&lt;br /&gt;
[[File:Windows ControlPanel WebService Setup Running.PNG|link=|alt=Windows Control Panel WebService Setup]]&lt;br /&gt;
&lt;br /&gt;
The debugging information is shown in the {{Code|Output}} tab.&lt;br /&gt;
&lt;br /&gt;
[[File:Windows ControlPanel WebService Output Running.PNG|link=|alt=Windows Control Panel WebService Output Running]]&lt;br /&gt;
&lt;br /&gt;
If you are using the command line approach to start the WebService, debug information is enabled by specifying the {{Code|-v}} option:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
 PhidgetWebservice21.exe -v -n &amp;quot;myServer&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The debugging information is shown command line output. This will prove useful when debugging WebService problems.&lt;br /&gt;
&lt;br /&gt;
==Advanced Uses==&lt;br /&gt;
&lt;br /&gt;
===Manual File Installation===&lt;br /&gt;
&lt;br /&gt;
The Phidget installer installs the most commonly used files onto your system. However, there may be special cases where you want to install the Phidget libraries without the installers. This section will describe the purpose of the most important files file and cover how to manually install and distribute the libraries with your code. You can get the files [http://www.phidgets.com/downloads/libraries/Phidget21-windevel_2.1.8.20110615.zip here].&lt;br /&gt;
&lt;br /&gt;
====Description of Library files====&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|phidget21.dll}}&amp;lt;/b&amp;gt; contains the actual Phidget library, which is used at run-time.  It is placed in {{Code|C:\Windows\System32}}.&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|PhidgetWebService21.exe}}&amp;lt;/b&amp;gt; is used to control Phidgets remotely across a network using the [[#WebService | PhidgetWebservce]].&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|phidget21.lib}}&amp;lt;/b&amp;gt; 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}}. &lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|phidget21.h}}&amp;lt;/b&amp;gt; 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. &lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|Phidget21.NET.dll}}&amp;lt;/b&amp;gt; is the Phidgets library for .NET framework 2.0 or greater. Any .NET language can be used, including [[Language - C Sharp | C#]]  , and [[Language - Visual Basic .NET | Visual Basic .NET]].&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|Phidget21.NET1.1.dll}}&amp;lt;/b&amp;gt; is the Phidgets library for .NET framework 1.1. Any .NET language can be used, including [[Language - C Sharp | C#]]  , and [[Language - Visual Basic .NET | Visual Basic .NET]].&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|Phidget21COM.dll}}&amp;lt;/b&amp;gt; is the Component Object Model(COM) library and provides your project access to the Phidget ActiveX objects. This libraries is used by the [[Language - Adobe Director|AdobeDirector]], [[Language - AutoIt|AutoIT]], [[Language - Delphi|Delphi]], [[Language -  Visual Basic 6.0|Visual Basic 6.0]], [[Language - Visual Basic for Applications|Visual Basic for Applications]], [[Language - Visual Basic Script|Visual Basic Script]].&lt;br /&gt;
&lt;br /&gt;
=====Special Cases of Library Install=====&lt;br /&gt;
&lt;br /&gt;
Regardless of what language you will be using to program Phidgets, you will need the {{Code|phidget21.dll}} placed in the {{Code|C:\WINDOWS\system32}} directory. Additional files are needed for the language that you choose. Please refer to the documentation provided by your [[Device List|language]] to determine what files are needed and the steps needed to install them onto your system.&lt;br /&gt;
&lt;br /&gt;
You can find the {{Code|phidget21.dll}} in the link below:&lt;br /&gt;
&lt;br /&gt;
*[http://www.phidgets.com/downloads/libraries/Phidget21-windevel_2.1.8.20110615.zip Phidget21 Libraries] (32-Bit and 64-Bit development files without an installer)&lt;br /&gt;
&lt;br /&gt;
{{Code|PhidgetWebService21.exe}} is also provided in the link above.&lt;br /&gt;
&lt;br /&gt;
===Windows in a Virtual Machine===&lt;br /&gt;
&lt;br /&gt;
Phidgets can also be used inside a virtual machine. Instructions for VMWare and VirtualBox are provided below. Virtual PC is not supported as USB Phidgets requires a virtual platform that supports HID USB Devices and since Virtual PC does not support HID USB devices, Phidgets may not be used.&lt;br /&gt;
&lt;br /&gt;
As always, please ensure that you have the latest Phidget [[#Getting Started (Libraries and Drivers) | drivers]] installed on the virtual machine and that you are using the latest version of your virtual software. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;VMWare:&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To enable USB Phidgets, select Virtual Machine -&amp;gt; Removable Devices -&amp;gt; and select the Phidget Input Device -&amp;gt; Connect. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;VirtualBox&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
To enable USB Phidgets, VirtualBox Guest Additions(Devices -&amp;gt; Install Guest Additions) may need to be installed. Afterwards, click on Devices -&amp;gt; USB Devices and select the Phidget device to enable. The state should go from Busy to Captured. VirtualBox may bring up a new hardware wizard in the host operating system, which has to be installed. Please note that Phidgets with USB hubs(i.e, [[Product - 1019 1 - PhidgetInterfaceKit 8/8/8 w/6 Port Hub|1019]]), are undetectable; Fortunately, Phidgets that are attached to such Phidgets are detectable.&lt;br /&gt;
&lt;br /&gt;
==Common Problems and Solutions==&lt;br /&gt;
&lt;br /&gt;
===Issue: A corrupt installation fails on uninstall or repair===&lt;br /&gt;
Affected Operating Systems: &#039;&#039;&#039;Windows&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Solution: If the normal uninstall fails, or for whatever reason, you can choose to remove the Phidget drivers manually.&lt;br /&gt;
Please perform the following:&lt;br /&gt;
# Shut down any programs using the Phidget libraries, including the WebService and the Phidget Control Panel.&lt;br /&gt;
# Delete {{Code|C:\Program Files\Phidgets\}}&lt;br /&gt;
# Remove the Phidgets key from the Registry {{Code|[-HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services PhidgetWebService21]}}.&lt;br /&gt;
&lt;br /&gt;
In most cases this is enough to get the installer working again. If you need to remove all traces of the Phidgets libraries manually, perform the following additional steps:&lt;br /&gt;
&lt;br /&gt;
# Unregister the COM library: {{Code|regsvr32 /u &amp;quot;C:\Program Files\Phidgets\Phidget21COM.dll&amp;quot;}}.&lt;br /&gt;
# Remove {{Code|Phidget21.NET}} and {{Code|Policy.2.1.Phidget21.NET}} from {{Code|C:\Windows\Assembly\}}.&lt;br /&gt;
# Delete {{Code|C:\Documents and Settings\All Users\Application Data\Phidgets}} if you are  using WindowsXP or {{Code|C:\Users\All Users\Phidgets}} if you are using Windows Vista.&lt;br /&gt;
# Delete {{Code|C:\Windows\system32\phidget21.dll}}.&lt;br /&gt;
# Delete Phidgets from the start menu.&lt;br /&gt;
# Search for and remove keys mentioning Phidgets from the registry in the following locations:&lt;br /&gt;
:* {{Code|[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\]}}&lt;br /&gt;
:* {{Code|[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Installer\Products\]}}&lt;br /&gt;
:* {{Code|[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework\AssemblyFolders\Phidgets Inc]}}&lt;br /&gt;
:* {{Code|[HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run\Phidget21Manager]}} &lt;br /&gt;
:* {{Code|[HKEY_LOCAL_MACHINE\SOFTWARE\Classes\Installer\Assemblies\Global\]}}&lt;br /&gt;
&lt;br /&gt;
:7. Reboot&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;NOTE:&#039;&#039;&#039; You can go through the registry and purge any other keys mentioning Phidgets if you still have problems, but at this point you should be able to reinstall under most cases. There will also be keys relating to the installer, the .NET library and the COM library, but they should not interfere with anything.&lt;br /&gt;
&lt;br /&gt;
===Issue: Event data is sporadic/slow/clumped over the WebService===&lt;br /&gt;
Affected Operating Systems: &#039;&#039;&#039;Windows&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
Windows implements 200ms delayed ACKs for network traffic. When traffic is one-way only - as it is with event data, the data will all arrive in clumps every 200ms because of delayed ACKs. &lt;br /&gt;
&lt;br /&gt;
This can be a great drawback for applications which rely on low latency event data over the network. (source: http://support.microsoft.com/kb/214397)&lt;br /&gt;
&lt;br /&gt;
This delayed ACK behavior can be disabled in windows to decrease event latency as documented here: http://support.microsoft.com/kb/328890&lt;br /&gt;
&lt;br /&gt;
In the future, the Phidgets library may implement this differently, but so far we have been unable to match the performance achieved by disabling delayed ACK.&lt;br /&gt;
&lt;br /&gt;
==Appendix==&lt;br /&gt;
&lt;br /&gt;
===Description of Installer files===&lt;br /&gt;
&lt;br /&gt;
Here is the list of files and their description for each file the installer puts onto your system. &lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|phidget21.dll}}&amp;lt;/b&amp;gt; contains the actual Phidget library, which is used at run-time.  It is also placed in {{Code|C:\Windows\System32}}.&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|PhidgetWebService21.exe}}&amp;lt;/b&amp;gt; is used to control Phidgets remotely across a network using the [[#WebService | PhidgetWebservce]].&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|PhidgetWindowsService21.exe}}&amp;lt;/b&amp;gt; is a Windows service that controls {{Code|PhidgetWebService21.exe}}. &lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|phidget21.lib}}&amp;lt;/b&amp;gt; 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}}. &lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|phidget21.h}}&amp;lt;/b&amp;gt; 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. &lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|phidget21.jar}}&amp;lt;/b&amp;gt; is an archive containing the Phidgets library, used by the [[Language -  Java | Java]] programming language.&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|Phidget21.NET.dll}}&amp;lt;/b&amp;gt; is the Phidgets library for .NET framework 2.0 or greater. Any .NET language can be used, including [[Language - C Sharp | C#]]  , and [[Language - Visual Basic .NET | Visual Basic .NET]].&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|Phidget21.NET1.1.dll}}&amp;lt;/b&amp;gt; is the Phidgets library for .NET framework 1.1. Any .NET language can be used, including [[Language - C Sharp | C#]]  , and [[Language - Visual Basic .NET | Visual Basic .NET]].&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|Phidget21.NET.XML}}&amp;lt;/b&amp;gt; provides the IntelliSense in-line documentation for the .NET library in Visual Studio.&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|Phidget21COM.dll}}&amp;lt;/b&amp;gt; is the Component Object Model(COM) library and provides your project access to the Phidget ActiveX objects. This libraries is used by the [[Language - Adobe Director|AdobeDirector]], [[Language - AutoIt|AutoIT]], [[Language - Delphi|Delphi]], [[Language -  Visual Basic 6.0|Visual Basic 6.0]], [[Language - Visual Basic for Applications|Visual Basic for Applications]], [[Language - Visual Basic Script|Visual Basic Script]].&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|Phidget21Manager.exe}}&amp;lt;/b&amp;gt; is a tool to quickly determine whether your system is able to control Phidgets, and also act as a debugging tool. &lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|Examples}} folder&amp;lt;/b&amp;gt; contain example applications that allows you to quickly see if your Phidget is properly configured.&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|x86 folder}} folder&amp;lt;/b&amp;gt; contain the 32 bit versions of {{Code|phidget21.dll}}, {{Code|phidget21.lib}}, {{Code|Phidget21COM.dll}}. These folder will only appear on 64 bit installations and is useful if you want to code against the 32 bit libraries.&lt;/div&gt;</summary>
		<author><name>Mphi</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs21/index.php?title=File:Windows7_DeviceManager.PNG&amp;diff=19064</id>
		<title>File:Windows7 DeviceManager.PNG</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs21/index.php?title=File:Windows7_DeviceManager.PNG&amp;diff=19064"/>
		<updated>2012-04-19T17:45:14Z</updated>

		<summary type="html">&lt;p&gt;Mphi: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Mphi</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs21/index.php?title=Language_-_C&amp;diff=19063</id>
		<title>Language - C</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs21/index.php?title=Language_-_C&amp;diff=19063"/>
		<updated>2012-04-19T17:35:28Z</updated>

		<summary type="html">&lt;p&gt;Mphi: /* Write Your Own Code */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[Category:Language]]&lt;br /&gt;
[[File:icon-C++.png|link=|alt=C/C++|64x64px]] C++ is a general purpose, cross-platform programming language with a vast user base. &lt;br /&gt;
&lt;br /&gt;
__TOC__&lt;br /&gt;
&lt;br /&gt;
==Introduction==&lt;br /&gt;
&lt;br /&gt;
{{LanguageSupport|C/C++|the complete Phidget API, including events|all Phidget devices.|Windows 2000/XP/Vista/7(environments include [[#Visual Studio | Visual Studio]], [[#Borland | Borland]], [[#Cygwin/MinGW | Cygwin, and MinGW]]), [[#Windows CE | Windows CE]], [[#OS X | OS X]], and [[#Linux | Linux]]|}}&lt;br /&gt;
&lt;br /&gt;
==Quick Downloads==&lt;br /&gt;
{{QuickDownloads|C/C++|&lt;br /&gt;
{{APIQuickDownloads|http://www.phidgets.com/documentation/Phidget21_C_Doc.zip}}|&lt;br /&gt;
{{ExampleQuickDownloads|http://www.phidgets.com/downloads/examples/VCpp_2.1.8.20110615.zip Visual Studio 2005/2008/2010|}}&lt;br /&gt;
{{ExampleQuickDownloads|http://www.phidgets.com/downloads/examples/phidget21-c-examples_2.1.8.20110615.tar.gz Generic|}}|&lt;br /&gt;
{{ExtraLibraryQuickDownloads|http://www.phidgets.com/downloads/libraries/phidget21bcc_2.1.8.20110615.zip|Borland(Windows)|}}&lt;br /&gt;
{{WindowsQuickDownloads}}&lt;br /&gt;
{{MacQuickDownloads}}&lt;br /&gt;
{{LinuxQuickDownloads}}&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
==Getting started with C/C++==&lt;br /&gt;
&lt;br /&gt;
If you are new to writing code for Phidgets, we recommend starting by running, then modifying existing examples. This will allow you to:&lt;br /&gt;
{{ExampleCodeReasons}}&lt;br /&gt;
&lt;br /&gt;
Instructions are divided up by operating system. Choose:&lt;br /&gt;
*[[#Windows(2000/XP/Vista/7)|Windows 2000 / XP / Vista / 7]]&lt;br /&gt;
*[[#OS X |OS X]]&lt;br /&gt;
*[[#Linux | Linux]] (including PhidgetSBC)&lt;br /&gt;
&lt;br /&gt;
==Windows (2000/XP/Vista/7)==&lt;br /&gt;
&lt;br /&gt;
===Description of Library Files===&lt;br /&gt;
C/C++ programs on Windows depend on three files, which the installers in [[#Libraries and Drivers|Quick Downloads]] put onto your system:&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|phidget21.dll}}&amp;lt;/b&amp;gt; contains the actual Phidget library, which is used at run-time.  By default, it is placed in {{Code|C:\Windows\System32}}.&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|phidget21.lib}}&amp;lt;/b&amp;gt; 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}}. &lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|phidget21.h}}&amp;lt;/b&amp;gt; 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. &lt;br /&gt;
&lt;br /&gt;
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 [[OS_-_Windows#Manual_File_Installation | Manual Installation Instructions]].&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
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++]].&lt;br /&gt;
&lt;br /&gt;
===Visual Studio===&lt;br /&gt;
&lt;br /&gt;
C++/CLI (which used to be called Managed C++) is very different from mainstream C/C++.  If you must use C++/CLI, consider calling the Phidget .NET library, instead of the C API normally used from C/C++.  We have no documentation for using C++/CLI.&lt;br /&gt;
&lt;br /&gt;
Microsoft makes free versions of Visual Studio available known as Express Editions.  The Express editions are suitable for most applications, but are limited in features for more complex applications. Please see [http://www.microsoft.com/visualstudio Microsoft Visual Studio] for more information.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
====Visual Studio 2005/2008/2010====&lt;br /&gt;
&lt;br /&gt;
=====Use Our Examples=====&lt;br /&gt;
&lt;br /&gt;
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 &amp;amp;rarr; Open &amp;amp;rarr; Project &amp;amp;rarr; Solution, and open {{Code|Visual Studio Phidgets Examples.sln}} in the {{Code|VCpp}} folder of the examples.&lt;br /&gt;
&lt;br /&gt;
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. &lt;br /&gt;
&lt;br /&gt;
[[File:VS2005 Conversion Wizard.PNG|link=|alt=Conversion Wizard]]&lt;br /&gt;
&lt;br /&gt;
This will load all of the examples available for C/C++. The easiest way to confirm that your environment is set up properly will be to compile and run the {{Code|HelloWorld}} C/C++ example.&lt;br /&gt;
&lt;br /&gt;
Start by setting the {{Code|HelloWorld}} project as your start up project.&lt;br /&gt;
&lt;br /&gt;
[[File:VS2005 StartUp Project.PNG|link=|alt=Start Up Project]]&lt;br /&gt;
&lt;br /&gt;
To run the example, click on Debug &amp;amp;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&#039;s location accordingly. Please see the [[#Write Your Own Code | Write Your Own Code]] section for details. &lt;br /&gt;
&lt;br /&gt;
[[File:VS2005 Run.PNG|link=|alt=Run]]&lt;br /&gt;
&lt;br /&gt;
This program will detect for devices that are attached/detached on the computer. Go ahead, and attach or detach your devices! Here is an example output:&lt;br /&gt;
&lt;br /&gt;
[[File:VS2005 HelloWorld Output.PNG|link=|alt=HelloWorld Output]]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
After confirming that the {{Code|HelloWorld}} example is working, you can proceed to run the example for your device. If you aren&#039;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 ensure that you have set your start up project to be the one that matches your device before compiling. &lt;br /&gt;
&lt;br /&gt;
Once you have the C/C++ examples running, we have a [[#Follow the Examples|teaching]] section below to help you follow them.&lt;br /&gt;
&lt;br /&gt;
=====Write Your Own Code=====&lt;br /&gt;
&lt;br /&gt;
When you are building a project from scratch, or adding Phidget function calls to an existing project, you&#039;ll need to configure your development environment to properly link the Phidget C/C++ library. To begin: &lt;br /&gt;
&lt;br /&gt;
1. Generate a new Visual C++: Win32 Console Application project with a descriptive name such as PhidgetTest.&lt;br /&gt;
&lt;br /&gt;
[[File:VS2005 New Project.PNG|link=|alt=New Project]]&lt;br /&gt;
&lt;br /&gt;
2. Next, select Console Application.&lt;br /&gt;
&lt;br /&gt;
[[File:VS2005 New Project 2.PNG|link=|alt=New Project]]&lt;br /&gt;
&lt;br /&gt;
3. Open the project properties window.&lt;br /&gt;
&lt;br /&gt;
4. Navigate to Configuration Properties &amp;amp;rarr; C/C++.&lt;br /&gt;
&lt;br /&gt;
5. Add {{Code|&amp;quot;C:\Program Files\Phidgets&amp;quot;}} 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&#039;s location accordingly.&lt;br /&gt;
&lt;br /&gt;
[[File:VS2005 Header.PNG|link=|alt=Header File]]&lt;br /&gt;
&lt;br /&gt;
6. Navigate to Configuration Properties &amp;amp;rarr; Linker &amp;amp;rarr; Input.&lt;br /&gt;
&lt;br /&gt;
7. Edit the additional dependencies and add {{Code|&amp;quot;C:\Program  Files\Phidgets\phidget21.lib&amp;quot;}}. 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&#039;s location accordingly.&lt;br /&gt;
&lt;br /&gt;
[[File:VS2005 Library.PNG|link=|alt=Library File]]&lt;br /&gt;
&lt;br /&gt;
8. The project now has access to the Phidget function calls and you are ready to begin coding.&lt;br /&gt;
&lt;br /&gt;
Then, in your code, you will need to include the Phidget C/C++ library:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=cpp&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  #include &amp;lt;phidget21.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The same [[#Follow the Examples|teaching]] section which describes the examples also has further resources for programming your Phidget.&lt;br /&gt;
&lt;br /&gt;
====Visual Studio 2003====&lt;br /&gt;
&lt;br /&gt;
=====Use Our Examples=====&lt;br /&gt;
&lt;br /&gt;
1. Start by downloading the [http://www.phidgets.com/downloads/examples/phidget21-c-examples_2.1.8.20111028.tar.gz examples]. You can import these examples into a Visual Studio 2003 C++ project. Afterwards, unpack them into a folder. Here, you can find example programs for all the devices. You will need this example source code to be copied into your C++ project later on. The easiest way to confirm that your environment is set up properly will be to compile and run the {{Code|HelloWorld}} C/C++ example. &lt;br /&gt;
&lt;br /&gt;
2. A new project will need to be created. Generate a new Visual C++ empty project(.NET) with a descriptive name such as HelloWorld.&lt;br /&gt;
&lt;br /&gt;
[[File:VS2003 New Project.PNG|link=|alt=New Project]]&lt;br /&gt;
&lt;br /&gt;
3. Create a new C++ file by adding a new item to the source files folder. &lt;br /&gt;
&lt;br /&gt;
[[File:VS2003 New File.PNG|link=|alt=New File]]&lt;br /&gt;
&lt;br /&gt;
[[File:VS2003 New File 2.PNG|link=|alt=New File]]&lt;br /&gt;
&lt;br /&gt;
4. An empty C++ file will pop up. Please copy and paste the contents of the {{Code|HelloWorld.c}} program into here. &lt;br /&gt;
&lt;br /&gt;
[[File:VS2003 Source.PNG|link=|alt=Source Code]]&lt;br /&gt;
&lt;br /&gt;
5. Next, the project setting needs to be set up. Open the project properties window.&lt;br /&gt;
&lt;br /&gt;
6. Navigate to Configuration Properties &amp;amp;rarr; C/C++.&lt;br /&gt;
&lt;br /&gt;
7. Add {{Code|&amp;quot;C:\Program Files\Phidgets&amp;quot;}} to the additional include 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&#039;s location accordingly.&lt;br /&gt;
&lt;br /&gt;
[[File:VS2003 Header.PNG|link=|alt=Header File]]&lt;br /&gt;
&lt;br /&gt;
8. Navigate to Configuration Properties &amp;amp;rarr; Linker &amp;amp;rarr; Input.&lt;br /&gt;
&lt;br /&gt;
9. Add {{Code|&amp;quot;C:\Program  Files\Phidgets\phidget21.lib&amp;quot;}} to the additional dependencies field. 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&#039;s location accordingly. &lt;br /&gt;
&lt;br /&gt;
[[File:VS2003 Library.PNG|link=|alt=Library File]]&lt;br /&gt;
&lt;br /&gt;
10. Now, you can run the example. Click on Debug &amp;amp;rarr; Start Without Debugging.&lt;br /&gt;
&lt;br /&gt;
[[File:VS2003 Run.PNG|link=|alt=Run]]&lt;br /&gt;
&lt;br /&gt;
11. This program will detect for devices that are attached/detached on the computer. Go ahead, and attach or detach your devices! Here is an example output:&lt;br /&gt;
&lt;br /&gt;
[[File:VS2003 HelloWorld Output.PNG|link=|alt=HelloWorld Output]]&lt;br /&gt;
&lt;br /&gt;
After confirming that the {{Code|HelloWorld}} example is working, you can proceed to run the example for your device. If you aren&#039;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]].&lt;br /&gt;
&lt;br /&gt;
Once you have the C/C++ examples running, we have a [[#Follow the Examples|teaching]] section below to help you follow them.&lt;br /&gt;
&lt;br /&gt;
=====Write Your Own Code=====&lt;br /&gt;
&lt;br /&gt;
When you are building a project from scratch, or adding Phidget function calls to an existing project, you&#039;ll need to configure your  development environment to properly link the Phidget C/C++ library. Please see the [[#Use Our Examples 2 | Use Our Examples]] section for instructions.&lt;br /&gt;
&lt;br /&gt;
Then, in your code, you will need to include the Phidget C/C++ library:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=cpp&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  #include &amp;lt;phidget21.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
====Visual Studio C++ 6.0====&lt;br /&gt;
&lt;br /&gt;
=====Use Our Examples=====&lt;br /&gt;
&lt;br /&gt;
1. Download the [http://www.phidgets.com/downloads/examples/phidget21-c-examples_2.1.8.20110615.tar.gz examples] and unpack them into a folder. Here, you can find example programs for all the devices. You will need this example source code to be copied into your C++ project later on. The easiest way to confirm that your environment is set up properly will be to compile and run the {{Code|HelloWorld}} C/C++ example.&lt;br /&gt;
&lt;br /&gt;
2. Next, a new project will need to be created. Generate a new Win32 Console Application project with a descriptive name such as HelloWorld.&lt;br /&gt;
&lt;br /&gt;
[[File:VS6 New Project.PNG|link=|alt=New Project]]&lt;br /&gt;
&lt;br /&gt;
3. Create an empty project.&lt;br /&gt;
&lt;br /&gt;
[[File:VS6 New Project 2.PNG|link=|alt=New Project]]&lt;br /&gt;
&lt;br /&gt;
4. Next, the project settings needs to be set up. Navigate to Project &amp;amp;rarr; Settings &amp;amp;rarr; C/C++ &amp;amp;rarr; Preprocessor.&lt;br /&gt;
&lt;br /&gt;
5. Add {{Code|C:\Program Files\Phidgets}} to the additional include 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&#039;s location accordingly.&lt;br /&gt;
&lt;br /&gt;
[[File:VS6 Header.PNG|link=|alt=Header File]]&lt;br /&gt;
&lt;br /&gt;
6. Navigate to Project &amp;amp;rarr; Settings &amp;amp;rarr; Link &amp;amp;rarr; Input &amp;amp;rarr; Additional library Path.&lt;br /&gt;
&lt;br /&gt;
7. Add {{Code|phidget21.lib}} to the object/library modules field.&lt;br /&gt;
&lt;br /&gt;
8. Add {{Code|C:\Program  Files\Phidgets}} to the additional library path. 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&#039;s location accordingly. &lt;br /&gt;
&lt;br /&gt;
[[File:VS6 Library.PNG|link=|alt=Library File]]&lt;br /&gt;
&lt;br /&gt;
The project now has access to the Phidget function calls and you are ready to begin coding.&lt;br /&gt;
&lt;br /&gt;
To import the example program into your project, please:&lt;br /&gt;
&lt;br /&gt;
9. Create a new C++ file by navigating to File &amp;amp;rarr; New &amp;amp;rarr; Files &amp;amp;rarr; C++ Source File and enter a descriptive name such as HelloWorld.&lt;br /&gt;
&lt;br /&gt;
[[File:VS6 New File.PNG|link=|alt=New File]]&lt;br /&gt;
&lt;br /&gt;
10. An empty C++ file will pop up. Please copy and paste the contents of the {{Code|HelloWorld.c}} program here.&lt;br /&gt;
&lt;br /&gt;
[[File:VS6 Source.PNG|link=|alt=Source Code]]&lt;br /&gt;
&lt;br /&gt;
11. Now, you can run the example. Click on Build &amp;amp;rarr; Execute.&lt;br /&gt;
&lt;br /&gt;
[[File:VS6 Run.PNG|link=|alt=Run]]&lt;br /&gt;
&lt;br /&gt;
12. This program will detect for devices that are attached/detached on the computer. Go ahead, and attach or detach your devices! Here is an example output:&lt;br /&gt;
&lt;br /&gt;
[[File:VS6 HelloWorld Output.PNG|link=|alt=HelloWorld Output]]&lt;br /&gt;
&lt;br /&gt;
After confirming that the {{Code|HelloWorld}} example is working, you can proceed to run the example for your device. If you aren&#039;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]].&lt;br /&gt;
&lt;br /&gt;
Once you have the C/C++ examples running, we have a [[#Follow the Examples|teaching]] section below to help you follow them.&lt;br /&gt;
&lt;br /&gt;
=====Write Your Own Code=====&lt;br /&gt;
&lt;br /&gt;
When you are building a project from scratch, or adding Phidget function calls to an existing project, you&#039;ll need to configure your development environment to properly link the Phidget C/C++ library. Please see the [[#Use Our Examples 3 | Use Our Examples]] section for instructions.&lt;br /&gt;
&lt;br /&gt;
In your &#039;&#039;&#039;{{Code|.c}}&#039;&#039;&#039; source code file, you must include a reference to the library header:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=cpp&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  #include &amp;lt;phidget21.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then, you would compile your completed C/C++ code the same way as shown in the [[#Use Our Examples 3 | Use Our Examples]] section. &lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
===Borland===&lt;br /&gt;
&lt;br /&gt;
====Use Our Examples====&lt;br /&gt;
&lt;br /&gt;
In addition to running one of the two [[#Libraries and Drivers:| Windows Installers]] above (which you probably already have if you worked through the &#039;&#039;Getting Started&#039;&#039; page [[Device List | for your device]]), you will need the [http://www.phidgets.com/downloads/libraries/phidget21bcc_2.1.8.20110615.zip Borland C++ Libraries]. {{Code|phidget21bcc.lib}} is typically placed in {{Code|C:\Program Files\Phidgets}}, but you are free to place it in any directory you wish.&lt;br /&gt;
&lt;br /&gt;
After installing the Phidget C/C++ library, you&#039;re ready to download the [http://www.phidgets.com/downloads/examples/phidget21-c-examples_2.1.8.20111028.tar.gz examples] and run the examples.&lt;br /&gt;
&lt;br /&gt;
Afterwards, unpack the examples. The easiest way to confirm that your environment is set up properly will be to compile and run the {{Code|HelloWorld}} C/C++ example. Locate the {{Code|HelloWorld.c}} file and type the following to compile the file and link the Phidget C/C++ library:&lt;br /&gt;
&lt;br /&gt;
To compile, link the Phidget C/C++ library and build a binary executable, enter the following in a command line prompt in the directory with {{Code|HelloWorld.c}}:&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  bcc32 -eHelloWorld -I&amp;quot;C:\Program Files\Phidgets&amp;quot; -L&amp;quot;C:\Program Files\Phidgets&amp;quot; phidget21bcc.lib HelloWorld.c&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
It is assumed that {{Code|phidget21bcc.lib}} and {{Code|phidget21.h}} are placed in {{Code|C:\Program Files\Phidgets}}. If the files are placed in another location, please adjust the paths to both of the file&#039;s location accordingly.&lt;br /&gt;
&lt;br /&gt;
In this case, {{Code|HelloWorld.c}} would be the &#039;&#039;&#039;.c&#039;&#039;&#039; file specific to your device.  After using {{Code|bcc32}}, you will have an executable named {{Code|HelloWorld}} that you can run.  &lt;br /&gt;
&lt;br /&gt;
This program will detect for devices that are attached/detached on the computer. Go ahead, and attach or detach your devices! Here is an example output:&lt;br /&gt;
&lt;br /&gt;
[[File:Borland HelloWorld Output.PNG|link=|alt=HelloWorld Output]]&lt;br /&gt;
&lt;br /&gt;
After confirming that the {{Code|HelloWorld}} example is working, you can proceed to run the example for your device. If you aren&#039;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]].&lt;br /&gt;
&lt;br /&gt;
====Write Your Own Code====&lt;br /&gt;
&lt;br /&gt;
When writing your code from scratch, you start it as you would any C/C++ code with Borland. In your &#039;&#039;&#039;{{Code|.c}}&#039;&#039;&#039; source code file, you must include a reference to the library header:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=cpp&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  #include &amp;lt;phidget21.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then, you would compile your completed C/C++ code the same way as the examples [[#Use Our Examples 4 |above]]. &lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
===GCC on Windows===&lt;br /&gt;
&lt;br /&gt;
====Cygwin/MinGW====&lt;br /&gt;
&lt;br /&gt;
=====Use Our Examples=====&lt;br /&gt;
&lt;br /&gt;
Download the [http://www.phidgets.com/downloads/examples/phidget21-c-examples_2.1.8.20111028.tar.gz examples] and unpack them into a folder. Afterwards, unpack the examples. The easiest way to confirm that your environment is set up properly will be to compile and run the {{Code|HelloWorld}} C/C++ example. Locate the {{Code|HelloWorld.c}} file and type the following to compile the file and link the Phidget C/C++ library in a command line prompt:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;Cygwin&amp;lt;/b&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  gcc example.c -o HelloWorld -I&amp;quot;/cygdrive/c/Program Files/Phidgets&amp;quot; -L&amp;quot;/cygdrive/c/Program Files/Phidgets&amp;quot; -lphidget21&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;b&amp;gt;MinGW&amp;lt;/b&amp;gt;&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  gcc example.c -o HelloWorld -I&amp;quot;C:\Program Files\Phidgets&amp;quot; -L&amp;quot;C:\Program Files\Phidgets&amp;quot; -lphidget21&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
After using gcc, you will have an executable named {{Code|HelloWorld}} that you can run.  &lt;br /&gt;
It is assumed that {{Code|phidget21.h}} and {{Code|phidget21.lib}} are placed in {{Code|C:\Program Files\Phidgets}}. If the files are placed in another location, please adjust the paths to the file&#039;s location accordingly.&lt;br /&gt;
&lt;br /&gt;
After using {{Code|gcc}}, you will have an executable named {{Code|HelloWorld}} that you can run.  &lt;br /&gt;
&lt;br /&gt;
This program will detect for devices that are attached/detached on the computer. Go ahead, and attach or detach your devices! Here is an example output:&lt;br /&gt;
&lt;br /&gt;
[[File:C MinGW HelloWorld Output.PNG|link=|alt=HelloWorld Output]]&lt;br /&gt;
&lt;br /&gt;
After confirming that the {{Code|HelloWorld}} example is working, you can proceed to run the example for your device. If you aren&#039;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]].&lt;br /&gt;
&lt;br /&gt;
=====Write Your Own Code=====&lt;br /&gt;
&lt;br /&gt;
When writing your code from scratch, you start it as you would any C/C++ code with Cygwin/MinGW in your favourite text editor. In your &#039;&#039;&#039;{{Code|.c}}&#039;&#039;&#039; source code file, you must include a reference to the library header:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=cpp&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  #include &amp;lt;phidget21.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then, you would compile your completed C/C++ code the same way as shown in the [[#Use Our Examples 5| Use Our Examples]] section above. &lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
===Dev C++===&lt;br /&gt;
&lt;br /&gt;
=====Use Our Examples=====&lt;br /&gt;
&lt;br /&gt;
1. Download the [http://www.phidgets.com/downloads/examples/phidget21-c-examples_2.1.8.20111028.tar.gz examples] and unpack them into a folder. Here, you can find example programs for all the devices. If you aren&#039;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]]. You will need this example source code to be copied into your Dev C++ project later on. The easiest way to confirm that your environment is set up properly will be to compile and run the {{Code|HelloWorld}} C/C++ example.&lt;br /&gt;
&lt;br /&gt;
2. In order to control Phidgets with Dev C++, we will use the {{Code|reimp}} tool to convert the {{Code|phidget21.lib}} to a format that Dev C++ accepts. Download the [http://www.phidgets.com reimp tool].&lt;br /&gt;
&lt;br /&gt;
3. Open up command line and traverse to the directory containing the reimp tool. Type the following command to create {{Code|libphidget21.a}}.&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  reimp.exe &amp;quot;C:\Program Files\Phidgets\phidget21.lib&amp;quot;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
The command above assumes that the {{Code|phidget21.lib}} is in the corresponding directory. If the file is placed in another location, please adjust the path to the file&#039;s location accordingly. Please note that the 64 bit version of {{Code|phidget21.lib}} is not supported on Dev C/C++. Please use the 32 bit version of {{Code|phidget21.lib}}.&lt;br /&gt;
&lt;br /&gt;
4. Place {{Code|libphidget21.a}} in {{Code|&amp;lt;Dev-Cpp Install Directory&amp;gt;/lib}}.&lt;br /&gt;
&lt;br /&gt;
5. Next, a new project will need to be created. Generate a new console application with a descriptive name such as PhidgetTest. Please select C as the project type.&lt;br /&gt;
&lt;br /&gt;
[[File:DevC New Project.PNG|link=|alt=New Project]]&lt;br /&gt;
&lt;br /&gt;
6. Next, the project settings needs to be set up. Navigate to Project Options &amp;amp;rarr; Directories &amp;amp;rarr; Include Directories.&lt;br /&gt;
&lt;br /&gt;
7. Add a new path to {{Code|C:\Program Files\Phidgets}}. 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&#039;s location accordingly.&lt;br /&gt;
&lt;br /&gt;
[[File:DevC Header.PNG|link=|alt=Header File]]&lt;br /&gt;
&lt;br /&gt;
8. Navigate to Project Options &amp;amp;rarr; Parameters &amp;amp;rarr; Linker.&lt;br /&gt;
&lt;br /&gt;
9. Add {{Code|-lphidget21}} to the field. This step will find the {{Code|libphidget21.a}} file in {{Code|&amp;lt;Dev-Cpp Install Directory&amp;gt;/lib}}. &lt;br /&gt;
&lt;br /&gt;
[[File:DevC Library.PNG|link=|alt=Library File]]&lt;br /&gt;
&lt;br /&gt;
10. To import the {{Code|HelloWorld}} program into your project, please open up {{Code|main.c}} in the editor.&lt;br /&gt;
&lt;br /&gt;
11. An empty C file will pop up. Please copy and paste the contents of the example program. &lt;br /&gt;
&lt;br /&gt;
[[File:DevC Source.PNG|link=|alt=Source Code]]&lt;br /&gt;
&lt;br /&gt;
12. Now, you can run the example. Click on Execute &amp;amp;rarr; Compile &amp;amp; Run.&lt;br /&gt;
&lt;br /&gt;
[[File:DevC Run.PNG|link=|alt=Run]]&lt;br /&gt;
&lt;br /&gt;
13. This program will detect for devices that are attached/detached on the computer. Go ahead, and attach or detach your devices! Here is an example output:&lt;br /&gt;
&lt;br /&gt;
[[File:DevC HelloWorld Output.PNG|link=|alt=HelloWorld Output]]&lt;br /&gt;
&lt;br /&gt;
After confirming that the {{Code|HelloWorld}} example is working, you can proceed to run the example for your device. If you aren&#039;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]].&lt;br /&gt;
&lt;br /&gt;
Once you have the C/C++ examples running, we have a [[#Follow the Examples|teaching]] section below to help you follow them.&lt;br /&gt;
&lt;br /&gt;
=====Write Your Own Code=====&lt;br /&gt;
&lt;br /&gt;
When you are building a project from scratch, or adding Phidget function calls to an existing project, you&#039;ll need to configure your development environment to properly link the Phidget C/C++ library. Please see the [[#Use Our Examples 6 | Use Our Examples]] section for instructions.&lt;br /&gt;
&lt;br /&gt;
In your &#039;&#039;&#039;{{Code|.c}}&#039;&#039;&#039; source code file, you must include a reference to the library header:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=cpp&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  #include &amp;lt;phidget21.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then, you would compile your completed C/C++ code the same way as the [[#Use Our Examples 6 | examples]] above. &lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
==OS X==&lt;br /&gt;
&lt;br /&gt;
C/C++ has excellent support on OS X through the gcc compiler.  &lt;br /&gt;
&lt;br /&gt;
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 - OS X]] page also describes the different Phidget files, their installed locations, and their roles. &lt;br /&gt;
&lt;br /&gt;
{{ContentNeeded|The information we say that we say on the OS - OS X page (i.e. the different files, locations, and roles) should actually be added there}}&lt;br /&gt;
&lt;br /&gt;
===Use Our Examples===&lt;br /&gt;
&lt;br /&gt;
After installing the Phidget C/C++ library for OS X as above, you&#039;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&#039;ll need to find the source code &#039;&#039;for your specific device&#039;&#039;.  Then, compile the code under your platform and run it.&lt;br /&gt;
&lt;br /&gt;
The examples assume that the compiled libraries have been set up properly.  To set them up on OS X, follow the &#039;&#039;Getting Started&#039;&#039; page for [[Device List | your specific device]]&lt;br /&gt;
&lt;br /&gt;
To compile, link the Phidget C/c++ library, and build an executable binary on OS X, do (for example, depending on the Headers location):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  gcc example.c -o example -framework Phidget21 -I/Library/Frameworks/Phidget21.framework/Headers&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
After using gcc, you will have an executable named {{Code|example}} that you can run.&lt;br /&gt;
&lt;br /&gt;
===Write Your Own Code===&lt;br /&gt;
&lt;br /&gt;
When writing your code from scratch, you must include a reference to the library header:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=cpp&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  #include &amp;lt;phidget21.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
==Linux==&lt;br /&gt;
&lt;br /&gt;
C/C++ has support on Linux through the gcc compiler.  &lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
===Use Our Examples===&lt;br /&gt;
&lt;br /&gt;
After installing the Phidget libraries for Linux as above, you&#039;re ready to download and run the examples:&lt;br /&gt;
*[http://www.phidgets.com/downloads/examples/phidget21-c-examples_2.1.8.20111028.tar.gz Generic C/C++ Examples]&lt;br /&gt;
&lt;br /&gt;
To run the example code, you&#039;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 &#039;&#039;software object&#039;&#039; 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.&lt;br /&gt;
&lt;br /&gt;
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}}:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  gcc example.c -o example -lphidget21&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
In this case, {{Code|example.c}} would be the &#039;&#039;&#039;.c&#039;&#039;&#039; file specific to your device.  After using gcc, you will have an executable named {{Code|example}} that you can run.  &lt;br /&gt;
&lt;br /&gt;
On Linux, if you have not set up [[OS_-_Linux#Setting_udev_Rules | your udev rules for USB access]], you will need to run the program &#039;&#039;&#039;as root&#039;&#039;&#039;:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=bash&amp;gt;&lt;br /&gt;
  sudo ./example&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
===Write Your Own Code===&lt;br /&gt;
&lt;br /&gt;
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 &#039;&#039;&#039;{{Code|.c}}&#039;&#039;&#039; source code file, you must include a reference to the library header:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=cpp&amp;gt;&lt;br /&gt;
  #include &amp;lt;phidget21.h&amp;gt;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Then, you would compile your completed C/C++ code the same way as the examples above. &lt;br /&gt;
&lt;br /&gt;
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.&lt;br /&gt;
&lt;br /&gt;
==Windows CE==&lt;br /&gt;
&lt;br /&gt;
===Description of Library Files===&lt;br /&gt;
C/C++ programs on Windows CE depend on the following files, which the Windows CE installer puts onto your system:&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|phidget21.dll}}&amp;lt;/b&amp;gt; contains the actual Phidget library, which is used at run-time.  It is placed in {{Code|\Windows}}.&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|PhidgetWebService21.exe}}&amp;lt;/b&amp;gt; is used to control Phidgets remotely across a network using the [[#WebService | PhidgetWebService]].&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|phidget21.lib}}&amp;lt;/b&amp;gt; is used by your compiler to link to the dll.  Your compiler has to know where this file is.&lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|phidget21.h}}&amp;lt;/b&amp;gt; lists all the Phidget API function calls available to your code.  Your compiler also has to know where this file is. &lt;br /&gt;
* &amp;lt;b&amp;gt;{{Code|phidget.dll}}&amp;lt;/b&amp;gt; is the Phidgets kernel driver. It is placed in {{Code|\Windows}}.&lt;br /&gt;
&lt;br /&gt;
===Visual Studio 2005/2008/2010===&lt;br /&gt;
&lt;br /&gt;
=====Use Our Examples=====&lt;br /&gt;
&lt;br /&gt;
Currently, we have no example code for C/C++ on Windows CE. However, set up is very much the same as what it would be with [[#Visual Studio 2005/2008/2010 |Visual Studio 2005/2008/2010]] in Windows. The {{Code|phidget21.h}} and {{Code|phidget21.lib}} can be downloaded [http://www.phidgets.com/downloads/libraries/Phidget21-wincedevel_2.1.8.20120216.zip here].&lt;br /&gt;
&lt;br /&gt;
=====Write Your Own Code=====&lt;br /&gt;
&lt;br /&gt;
When you are building a project from scratch, or adding Phidget function calls to an existing project, you&#039;ll need to configure your development environment to properly link the Phidget C/C++ library. To begin: &lt;br /&gt;
&lt;br /&gt;
1. Generate a new Visual C++: Win32 Smart Device project with a descriptive name such as PhidgetTest.&lt;br /&gt;
&lt;br /&gt;
[[File:WinCE VS C NewProject 1.PNG|link=|alt=New Project]]&lt;br /&gt;
&lt;br /&gt;
2. Select {{Code|Next}}.&lt;br /&gt;
&lt;br /&gt;
[[File:WinCE VS C NewProject 2.PNG|link=|alt=New Project]]&lt;br /&gt;
&lt;br /&gt;
3. Select the SDK(s) that you want to code against and elect {{Code|Next}}.&lt;br /&gt;
&lt;br /&gt;
[[File:WinCE VS C NewProject 3.PNG|link=|alt=SDKs]]&lt;br /&gt;
&lt;br /&gt;
4. Create a console application and select {{Code|Next}}.&lt;br /&gt;
&lt;br /&gt;
[[File:WinCE VS C NewProject 4.PNG|link=|alt=Create Console Application]]&lt;br /&gt;
&lt;br /&gt;
3. Open the project properties window.&lt;br /&gt;
&lt;br /&gt;
4. Navigate to Configuration Properties &amp;amp;rarr; C/C++.&lt;br /&gt;
&lt;br /&gt;
5. Add {{Code|&amp;quot;C:\Program Files\Phidgets&amp;quot;}} 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&#039;s location accordingly.&lt;br /&gt;
&lt;br /&gt;
[[File:VS2005 Header.PNG|link=|alt=Header File]]&lt;br /&gt;
&lt;br /&gt;
6. Navigate to Configuration Properties &amp;amp;rarr; Linker &amp;amp;rarr; Input.&lt;br /&gt;
&lt;br /&gt;
7. Edit the additional dependencies and add {{Code|&amp;quot;C:\Program  Files\Phidgets\phidget21.lib&amp;quot;}}. 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&#039;s location accordingly.&lt;br /&gt;
&lt;br /&gt;
[[File:VS2005 Library.PNG|link=|alt=Library File]]&lt;br /&gt;
&lt;br /&gt;
8. The project now has access to the Phidget function calls and you are ready to begin coding.&lt;br /&gt;
&lt;br /&gt;
Then, in your code, you will need to include the Phidget C/C++ library:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;div class=&amp;quot;source&amp;quot;&amp;gt;&lt;br /&gt;
&amp;lt;syntaxhighlight lang=cpp&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  #include &amp;lt;phidget21.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;/div&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The same [[#Follow the Examples|teaching]] section which describes the examples also has further resources for programming your Phidget.&lt;br /&gt;
&lt;br /&gt;
==Follow the Examples==&lt;br /&gt;
&lt;br /&gt;
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.  &lt;br /&gt;
&lt;br /&gt;
Your main reference for writing C code will be our C/C++ API information, with syntax for all of our functions:&lt;br /&gt;
&lt;br /&gt;
{{UsingAPhidgetInCodeGeneral|both of which are available in C/C++|[http://www.phidgets.com/documentation/Phidget21_C_Doc.zip C/C++ API]}}&lt;br /&gt;
&lt;br /&gt;
===Example Flow===&lt;br /&gt;
&lt;br /&gt;
{{ExamplePseudocode|In C/C++, you can name these &#039;&#039;&#039;event&#039;&#039;&#039; 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. &amp;lt;br&amp;gt; &lt;br /&gt;
In the example code, the event functions common to all Phidgets are called things like &#039;&#039;&#039;AttachHandler()&#039;&#039;&#039; and &#039;&#039;&#039;DetachHandler()&#039;&#039;&#039;, etc.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
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.&lt;br /&gt;
Other functions are given in the examples to show you more detail on using your Phidget.  For example, &#039;&#039;&#039;DeviceInitialize()&#039;&#039;&#039; will show what needs to be set up for your Phidget before using it.&lt;br /&gt;
|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.&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
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.|&lt;br /&gt;
[http://www.phidgets.com/documentation/Phidget21_C_Doc.zip C/C++ API]}}&lt;br /&gt;
&lt;br /&gt;
==Common Problems and Solutions/Workarounds==&lt;br /&gt;
&lt;br /&gt;
===Issue: I am using a non US-English version of Windows, and the Visual C/C++ examples run into a linker error===&lt;br /&gt;
Affected Operating Systems: &#039;&#039;&#039;Windows&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The example projects, by default finds the {{Code|phidget21.h}} and {{Code|phidget21.lib}} in ${SystemDrive}\Program Files\Phidgets. If you are using a non US-English version of Windows, the Phidget drivers may be installed into a different location. To resolve, you will have to modify the paths to these two files. For instructions, please see your environment/compiler section.&lt;/div&gt;</summary>
		<author><name>Mphi</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs21/index.php?title=File:WinCE_VS_C_NewProject_4.PNG&amp;diff=19062</id>
		<title>File:WinCE VS C NewProject 4.PNG</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs21/index.php?title=File:WinCE_VS_C_NewProject_4.PNG&amp;diff=19062"/>
		<updated>2012-04-19T17:29:26Z</updated>

		<summary type="html">&lt;p&gt;Mphi: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Mphi</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs21/index.php?title=File:WinCE_VS_C_NewProject_3.PNG&amp;diff=19061</id>
		<title>File:WinCE VS C NewProject 3.PNG</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs21/index.php?title=File:WinCE_VS_C_NewProject_3.PNG&amp;diff=19061"/>
		<updated>2012-04-19T17:29:20Z</updated>

		<summary type="html">&lt;p&gt;Mphi: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Mphi</name></author>
	</entry>
	<entry>
		<id>https://www.phidgets.com/docs21/index.php?title=File:WinCE_VS_C_NewProject_1.PNG&amp;diff=19060</id>
		<title>File:WinCE VS C NewProject 1.PNG</title>
		<link rel="alternate" type="text/html" href="https://www.phidgets.com/docs21/index.php?title=File:WinCE_VS_C_NewProject_1.PNG&amp;diff=19060"/>
		<updated>2012-04-19T17:29:12Z</updated>

		<summary type="html">&lt;p&gt;Mphi: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;/div&gt;</summary>
		<author><name>Mphi</name></author>
	</entry>
</feed>