Alert.png

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

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

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

We recommend that new projects be developed against the Phidget22 Library.


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

Alert.png

Language - LiveCode: Difference between revisions

From Phidgets Legacy Support
No edit summary
No edit summary
Line 1: Line 1:
[[File:icon-LiveCode.png|64x64px]] LiveCode is a high level programming language, developed by [[http://www.runrev.com RunRev]] that draws on English-like syntax for rapid application development.
[[File:icon-LiveCode.png|64x64px]] LiveCode is a high level programming language, developed by [[http://www.runrev.com RunRev]] that draws on English-like syntax for rapid application development.


==Support==
__TOC__
LiveCode has an incomplete API and code samples for PhidgetInterfaceKit devices only.


For a complete list of our supported languages and their support status, [[Levels of Support|click here]].
==Introduction==


* 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.
{{LanguageSupport|LiveCode|the complete Phidget API, including events|all Phidget devices.|Windows and Mac OS X.}}
* In this section, list any restrictions or limitations that this particular language may impose. For example, incompatibility with certain operating systems.
 
==Development Environments and Compilers==
 
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.  


==Quick Downloads==
==Quick Downloads==
Before you can run your program, you need to set up the proper environment and get the necessary files off the Phidgets website.
Visit the drivers section at www.phidgets.com and get the latest:
* [http://www.phidgets.com/drivers.php Phidget Framework]
* LiveCode Required Files:
** [http://www.phidgets.com/downloads/libraries/LiveCodeRequiredWin_2.1.8.20110615.zip Windows]
** [http://www.phidgets.com/downloads/libraries/LiveCodeRequiredMac_2.1.8.20110615.zip MacOS]
You will need the Phidget Framework to use and to program with Phidgets. We also recommend that you download the following reference materials:
* [http://www.phidgets.com/documentation/LiveCode_API_Manual.pdf API Manual]
* [http://www.phidgets.com/downloads/examples/LiveCode_2.1.8.20110615.zip LiveCode Sample Code]
* You can find a high level discussion about programming with Phidgets in general on the [[General API]] page.
* The [[Device Functionality]] page explains the general operational information for your device.
You may want to have these pages open while working through these instructions.
==Getting Started==
Phidgets supports development in LiveCode 4.5 for InterfaceKit 8/8/8 devices on local connections in Windows XP SP3 and Mac OS X 10.6.4 environments.
First, we need to set up the proper environment and get the necessary files from the Drivers, Libraries and Resources section above.


The Phidget examples were written using LiveCode. This tutorial assumes the use of the LiveCode 4.5 IDE in a Windows XP SP3 or a Mac OS 10.6.4 environment, other recent versions should work as well and would be set up in a similar manner.
Just need the LiveCode drivers, libraries, and examples?  Here they are:
To begin,  
* The first step is to integrate the phidgets_livecode.dll(Windows) or phidgets_livecode.bundle(Mac OS X) and the Externals.txt into the LiveCode IDE. Locate the “Externals” directory under the
LiveCode User Extensions folder. On a Windows machine, you can verify the location of the Users Extensions folder by checking the User Extensions field (from Edit | Preferences | Files & Memory | User Extensions in the LiveCode 4.5 IDE).
In Mac OS X, the path is: LiveCode | Preferences | Files & Memory | User Extensions.
If the folders do not exist, please create it.
Place the phidgets_livecode.dll(or phidgets_livecode.bundle) and the Externals.txt inside the “Externals” folder.
For more information on the User Exensions Folder, please consult http://www.runrev.com.
* Launch the LiveCode IDE, and create a “New MainStack”.
* Insert Text Entry Fields on the card for the purpose of capturing output.
The project now has access to Phidgets and we are ready to begin coding.


===Coding For Your Phidget===
C# API Documentation:
*[http://www.phidgets.com/documentation/LiveCode_API_Manual.pdf LiveCode# API Manual Download]
*[[General API]] (more help on functions common to all Phidgets)
*[[Device List | Device Specific APIs]] (more help on functions specific to your Phidget)


The LiveCode libraries does not implement the full Phidget API - some function calls and Phidget classes are not be supported.
LiveCode Example Code:
Please refer to the LiveCode API manual for a list of supported classes and functions.
The Phidget object will need to be declared and initialized.
For example, we can declare and initialize an instance of a PhidgetInterfaceKit inside our script with:


<div style="background-color: #f3f3f3; border-color: #1c9edb; border-width:1px; border-style: dashed;">
*[http://www.phidgets.com/downloads/examples/LiveCode_2.1.8.20111220.zip LiveCode Example Code]
<font size="3">
<source lang=csharp>


Libraries and Drivers:
*[http://www.phidgets.com/downloads/libraries/LiveCodeRequiredMac_2.1.8.20111220.zip Phidgets LiveCode Library for Mac OS X]
*[http://www.phidgets.com/downloads/libraries/LiveCodeRequiredWin_2.1.8.20111220.zip Phidgets LiveCode Library for Windows]
*[http://www.phidgets.com/downloads/libraries/Phidget-x86_2.1.8.20111220.exe 32 bit Windows (drivers, with libraries)]
*[http://www.phidgets.com/downloads/libraries/Phidget-x64_2.1.8.20111220.exe 64 bit Windows (drivers, with libraries)]
*[http://www.phidgets.com/downloads/libraries/Phidget_2.1.8.20111103.dmg General Mac OSX Libraries] ([[OS - Mac OS X | Mac OS X Library Setup Instructions]])


  //Declare a variable to store the InterfaceKit object
==Getting started with LiveCode==
  global ifKit
  //Create the InterfaceKit object and store it in the variable, “ifKit”
  get phidgetsInterfaceKit_create(“ifKit”)
 
</source>
</font>
</div>
 
The PhidgetInterfaceKit also inherits functionality from the Phidget base class.


===Connecting to the Phidget===
If you are new to writing code for Phidgets, we recommend starting by running, then modifying existing examples. This will allow you to:
{{ExampleCodeReasons}}


The program needs to try and connect to the Phidget through an open call.  
Instructions are divided up by operating system. Choose:
The open will tell the program to continuously try to connect to a Phidget, based on the parameters given, even trying to reconnect if it gets disconnected.
*[[#Windows(2000/XP/Vista/7)|Windows 2000 / XP / Vista / 7]]
This means that simply calling open does not guarantee you can use the Phidget immediately.
*[[#Mac OS X |Mac OS X]]
We can handle this by using event driven programming and tracking the AttachEvents and DetachEvents, or by calling waitForAttachment.
WaitForAttachment will block indefinitely until a connection is made to the Phidget, or an optional timeout is exceeded.


<div style="background-color: #f3f3f3; border-color: #1c9edb; border-width:1px; border-style: dashed;">
==Windows(2000/XP/Vista/7)==
<font size="3">
<source lang=csharp>


  get phidgets_open(ifKit, -1) //-1 to open any Phidget
===Libraries===
  get phidgets_waitForAttachment(ifKit, 1000)
*[http://www.phidgets.com/downloads/libraries/Phidget21-windevel_2.1.8.20111220.zip .NET Framework Files]
 
</source>
</font>
</div>
The open call can specify a serial number to open a specific Phidget, or specify -1 to open any Phidget.  
The API manual lists all of the available modes that open provides.  
At the end of your program, don’t forget to call close and delete to free any locks on the Phidget.


<div style="background-color: #f3f3f3; border-color: #1c9edb; border-width:1px; border-style: dashed;">
===Description===
<font size="3">
C# programs on Windows depend on the following files, which the installers above put onto your system:
<source lang=csharp>
* <b><code>phidget21.dll</code></b> contains the actual Phidgets library, which is used at run-time.  If you used our installer, it's already correctly placed in <code>C:\Windows\System32</code>.  It can be manually installed - check our [[Manual Installation instructions]].
You will also need one of the following two files, depending on the .NET framework version you are targeting:
* <b><code>Phidget21.NET.dll</code></b> is the Phidgets library for .NET framework <i><b>2.0</b></i> or higher. Your compiler has to know where this file is. By default, our installer puts this file into <code>C:\Program Files\Phidgets</code>. So, you can either point your compiler to that location, or copy and link to it in a directory for your project workspace. For more information, please see the section for your specific compiler/environment. If you do not want to use our installer, you can get the file [[#Libraries | here]].
* <b><code>Phidget21.NET1.1.dll</code></b> is the Phidgets library for .NET framework <i><b>1.1</b></i>. Your compiler has to know where this file is. By default, our installer puts this file into <code>C:\Program Files\Phidgets</code>. So, you can either point your compiler to that location, or copy and link to it in a directory for your project workspace. For more information, please see the section for your specific compiler/environment. If you do not want to use our installer, you can get the file [[#Libraries | here]].
You can optionally install the following files:
* <b><code>Phidget21.NET.XML</code></b> provides the IntelliSense in-line documentation for the .NET library in Visual Studio/MonoDevelop. This documentation is also visible in the Object Browser. By default, our installer puts this file into <code>C:\Program Files\Phidgets</code>. If you do not want to use our installer, you can get the file [[#Libraries | here]] and place it in the same directory as <code>Phidget21.NET.dll</code>.
* <b><code>Policy.2.1.Phidget21.NET.dll</code></b> is the policy assembly for <code>Phidget21.NET.dll</code>. Our installer places this file in the Global Assembly Cache(GAC) directory. It directs any programs compiled against version 2.1.0 or higher of <code>Phidget21.NET.dll</code> to use the most recent installed version. If you do not want to use our installer, you can get the file [[#Libraries | here]].


  get phidgets_close(ifKit)
  get phidgets_delete(ifKit)


</source>
</font>
</div>


===Event Driven Programming===
===Use Our Examples===


We recommend the use of event driven programming when working with Phidgets.  
Please start by downloading the LiveCode Examples.  These examples were written in LiveCode 4.5, but will also work with other versions.
We can setup an event handler with the following code:
*[http://www.phidgets.com/downloads/examples/LiveCode_2.1.8.20111220.zip LiveCode Examples]


<div style="background-color: #f3f3f3; border-color: #1c9edb; border-width:1px; border-style: dashed;">
To run the examples, you first download them [[#Use Our Examples|from above]] and unpack them into a folder.  To load all projects in Visual Studio, go to File | Open | Project, and open <code>AllExamples/AllExamples.sln</code> or <code>AllExamples/AllExamples_vs2008.sln</code> for Visual Studio 2005 and 2008, respectively.
<font size="3">
<source lang=csharp>


  get phidgetsInterfaceKit_set_OnSensorChange_Handler(ifKit, “SensorChangeHandler”)
If you are opening the Phidget examples in Visual Studio 2010, you will need to go through the Visual Studio Conversion Wizard to convert the 2005 or 2008 project.
<br/><br/>[[File:VS2005 Conversion Wizard.PNG]]<br/><br/>


</source>
This will load all of the examples available for C#, and then you can set your main project to be the one that matches your device.  If you aren't sure what the software example for your device is called, check the software object listed in the [[Device List | Getting Started guide for your device]].
</font>
</div>


The SensorChangeHandler command is defined as follows:
The only thing left to do is to run the examples! Click on Debug -> Start Debugging. Please note that the projects, by default try to find the <code>Phidget21.NET.dll</code> in the <code>C:\Program Files\Phidgets</code>. If you have it installed in another location, please change the path accordingly. If you are receiving an error message regarding that the namespace Phidgets cannot be found, please re-add the reference to <code>Phidget21.NET.dll</code>. Please see the [[#Write Your Own Code | next section]] for details.
<br/><br/>
[[File:CSharp VS2005 Run.PNG ‎]]
<br/><br/>
Once you have the C# examples running, we have a [[#Follow The Examples|teaching section]] below to help you follow them.


<div style="background-color: #f3f3f3; border-color: #1c9edb; border-width:1px; border-style: dashed;">
=====Write Your Own Code=====
<font size="3">
<source lang=csharp>


  command SensorChangeHandler ifKit pIndex pValue
When you are building a project from scratch, or adding Phidget function calls to an existing project, you'll need to configure your compiler / development environment to properly link the Phidget C# libraries. To begin:  
      put “Sensor#: “ & pIndex & “ , Value: “ & pValue & return after field 
        “currSensorValues”
  end SensorChangeHandler


</source>
* Generate a new Visual C# Windows Applications project with a descriptive name such as PhidgetTest.
</font>
<br/>[[File:CSharp VS2005 New Project.PNG ]]
</div>


In this function, the code inside SensorChangeHandler will get executed every time the PhidgetInterfaceKit reports a change on one of its analog inputs.
<br/>
Please refer to the LiveCode API manual for a full list of events and their usage.


===Working directly with the Phidget===
* Add a reference to the .NET Phidgets library.
<br/>[[File:CSharp VS2005 Add Reference.PNG ]]<br/>
* Under the .NET tab, select <code>Phidget21.NET.dll</code>.
If you used our installer, these files are installed in <code>C:\Program Files\Phidgets</code>, by default. If it does not appear in this list, then you can browse to the Phidget Framework installation directory and add the file.
<br/>[[File:CSharp VS2005 Add Reference 2.PNG ]]<br/><br/>


Most values can be read directly as an alternative to event driven programming.
Then, in your code, you will need to include the Phidget library:
Simply use the LiveCode API functions, such as phidgetsInterfaceKit_getSensorValue for PhidgetInterfaceKits.


<div style="background-color: #f3f3f3; border-color: #1c9edb; border-width:1px; border-style: dashed;">
<div style="background-color: #f3f3f3; border-color: #1c9edb; border-width:1px; border-style: dashed;">
<font size="3">
<font size="3">
<source lang=csharp>
<source lang=cpp>
 
   using Phidgets;
   repeat with currSensor=0 to 7
   using Phidgets.Events;
      local sensorValue
   </source>
    get phidgetsInterfaceKit_getSensorValue(ifKit, currSensor, “sensorValue”)
    put “Sensor: “ & currSensor & “, Value: “ & sensorValue into         
    sensorValueList[currSensor]
   end repeat
  combine sensorValueList using return
   put sensorValueList into field “sensorValueField”
 
</source>
</font>
</font>
</div>
</div>


Above, phidgetsInterfaceKit_getSensorValue copies the sensor value into the variable “sensorValue”,  and places the value in the field “sensorValueField”.  
The project now has access to the Phidget21 function calls and you are ready to begin coding.


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


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


===Other Phidgets===
==Mac OS X==


At this time, only the PhidgetInterfaceKit 8/8/8 is supported. 
LiveCode has excellent support on Mac OS X.
For more information on how you can leverage LiveCode with other Phidget devices, please refer to the “Extending LiveCode to other Phidgets” section.


===Creating Standalone Applications===
The first step in using LiveCode 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 - Mac OS X]] page also describes the different Phidget files, their installed locations, and their roles....


Packaging standalone applications require the phidgets_livecode.dll(Windows) or phidgets_livecode.bundle(Mac OS X) to be attached with the executable.
==Follow The Examples==
Please ensure that the phidgets_livecode.dll(or phidgets_livecode.bundle) and Externals.txt are in the “Runtime” directory under the LiveCode User Extensions folder.
If the directories do not exist, please create it.
In addition, include the phidgets_livecode external in the Standalone Application Settings of the LiveCode IDE.
For more information on the exact procedure for packaging externals with standalones, please consult http://www.runrev.com.


===Extending LiveCode to other Phidgets===
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.


A C++ external wrapper was created which allows the InterfaceKit 8/8/8 to be programmable in LiveCode.
Next, comes our API information. These resources outline the LiveCode Phidget methods:
It is possible to extend this external to other Phidgets as well.
The goal of this section is to equip the reader with knowledge so that they can understand the relationship between LiveCode and the Phidgets framework.
It is intended to provide guidance for readers who wish to extend LiveCode to other Phidgets.
This guide uses C++, Microsoft Visual Studio 2005(for Windows XP SP3), XCode 3.2.4(for Mac OS X 10.6.4),  
LiveCode 4.5 IDE and it is assumed that the reader has a solid understanding of these technologies.  
For your reference, there is also Visual Studio and XCode project examples in the LiveCode sample code in the [[#Drivers, Libraries and Resources|resource section]] of this page.


The first step is to download LiveCode Externals Creator SDK.
{{UsingAPhidgetInCodeGeneral|both of which are available in LiveCode|[http://www.phidgets.com/documentation/LiveCode_API_Manual.pdf LiveCode API]}}
The SDK can be found in the Externals tutorial section at http://www.runrev.com.  
Run the SDK, select C++ as the language, enter the project name, select the platform and specify the LiveCode installation path.
Hit generate to create a skeleton project for the external.
The next step is to setup the environment to allow for access to the Phidget21 function calls.


In Visual Studio, you will need the latest Phidget21 Windows library and header - these are installed into “Program Files/Phidgets” by the Phidgets installer. Place the phidget21.lib and phidget21.h in the Visual Studio project directory.
===Example Flow===
Be sure to open the properties window, navigate to Configuration Properties | Linker | Input and add “phidget21.lib” to the additional dependencies section.
Also, include a reference to the library header in the source code.


In XCode, open the newly created .xcodeproj project into XCode.  
{{ExamplePseudocode|In LiveCode, you can name these '''event''' functions whatever you like.  You will then pass them as function pointers to the Phidget library below in the Main Code section. This hooks them into the actual events when they occur. <br>
Add the Phidget21 framework to the project and include the a reference to the library header in the source code.  
In the example code, the event functions common to all Phidgets are called things like '''AttachHandler()''' and '''DetachHandler()''', etc.<br><br>
Now, we are ready to create external functions. LiveCode will have to acknowledge the existence of any external function.  
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.
Function declarations are placed inside the USER DECLARATIONS section of the the code.  
Other functions are given in the examples to show you more detail on using your Phidget. For example, '''DeviceInitialize()''' will show what needs to be set up for your Phidget before using it.
Here, a call to the function phidgetsInterfaceKit_create in LiveCode will be mapped to a call to the C++ function phidgetsInterfaceKit_create.
|Creating a Phidget software object in C# is specific to the Phidget. For a Phidget Spatial, for example, this would involve creating a <code>Spatial</code> object.  The examples show how to do this and other API functions.<br><br>
EXTERNAL_DECLARE_FUNCTION(“phidgetsInterfaceKit_create”, phidgetsInterfaceKit_create)
The object provides device specific methods and properties which are available from the API for your specific Phidget.|
For example, LiveCode will use the following lines of code to call the external function phidgetsInterfaceKit_create,
[http://www.phidgets.com/documentation/Phidget21.NET.zip C# API]}}


<div style="background-color: #f3f3f3; border-color: #1c9edb; border-width:1px; border-style: dashed;">
==Common Problems and Solutions/Workarounds==
<font size="3">
<source lang=csharp>
 
  local ifKit
  get phidgetsInterfaceKit_create(“ifKit”)  //”ifKit” is the name of the variable phid
 
</source>
</font>
</div>


The result of get phidgetsInterfaceKit_create(“ifKit”) will be 0 or the error code depending on whether the external function succeeded or failed, respectively.
Now, we implement the phidgetsInterfaceKit_create C++ function.
<div style="background-color: #f3f3f3; border-color: #1c9edb; border-width:1px; border-style: dashed;">
<font size="3">
<source lang=csharp>
  void phidgetsInterfaceKit_create(char *p_arguments[], int p_argument_count, char **r_result, Bool *r_pass, Bool *r_err)
  {
    char outcome_str[8];
    CPhidgetInterfaceKitHandle ifKit = (CPhidgetInterfaceKitHandle) strtoul(p_arguments[0], NULL, 16); 
    int outcome=CPhidgetInterfaceKit_create(&ifKit);
    if(outcome==0){
      char str[90];
      int t_success;
      /* Converts to Hexadecimal format */
      sprintf(str,”%08x”,ifKit);
      /* Passes back to LiveCode a reference of the InterfaceKit handle */
      SetVariable(p_arguments[0], str, &t_success);
    }
    sprintf(outcome_str, “%d”, outcome);
    *r_result = strdup(outcome_str);
    *r_err = False;
    *r_pass = False;
    return;
  }
</source>
</font>
</div>
As you can see, this function follows the same format as a typical external function.
p_arguments[] stores the arguments that are passed from the LiveCode function.
First, we allocate memory to store a CPhidgetInterfaceKitHandle and store its address in ifKit.
Next, we call the C function CPhidgetInterfaceKit_create, create the PhidgetInterfaceKit handle and return the error code to outcome.
If outcome is 0, the address of ifKit is converted into hexadecimal form.
The hexadecimal form is then stored in the LiveCode variable “ifKit”, so that LiveCode has a reference to the newly created InterfaceKit handle.
Lastly, the result of this external function is stored in *r_result, which is passed back as the return value for the LiveCode function.
It is also possible to implement Phidgets events with LiveCode.
We will implement the phidgetsInterfaceKit_set_OnSensorChange_Handler function.
Again, we declare the existence of the function:
<div style="background-color: #f3f3f3; border-color: #1c9edb; border-width:1px; border-style: dashed;">
<font size="3">
<source lang=csharp>
  EXTERNAL_DECLARE_FUNCTION(“phidgetsInterfaceKit_set_OnSensorChange_Handler”, phidgetsInterfaceKit_set_OnSensorChange_Handler)
</source>
</font>
</div>
In LiveCode, we use the following code to call the external function:
get phidgetsInterfaceKit_set_OnSensorChange_Handler(ifKit, “sensorChange”)
and “sensorChange” is the name of the LiveCode command to be called whenever a digital input
changes. This command must be in the script of the card that called phidgetsInterfaceKit_set_
OnSensorChange_Handler or its stack. The form of the command is as follow:
<div style="background-color: #f3f3f3; border-color: #1c9edb; border-width:1px; border-style: dashed;">
<font size="3">
<source lang=csharp>
  command sensorChange phid pIndex pValue
  ...
  end sensorChange
</source>
</font>
</div>
Now, we implement the phidgetsInterfaceKit_set_OnSensorChange_Handler C++ function.
<div style="background-color: #f3f3f3; border-color: #1c9edb; border-width:1px; border-style: dashed;">
<font size="3">
<source lang=csharp>
  void phidgetsInterfaceKit_set_OnSensorChange_Handler(char *p_arguments[], int p_argument_count, char **r_result, Bool *r_pass, Bool *r_err)
  { 
    CPhidgetInterfaceKitHandle ifKit = (CPhidgetInterfaceKitHandle)strtoul(p_
    arguments[0], NULL, 16); 
    char* str=(char *)malloc(90);
    str=strdup(p_arguments[1]);
    int outcome=CPhidgetInterfaceKit_set_OnSensorChange_Handler((CPhidgetInterfaceKitHandle)ifKit, &SensorChangeHandler, str);
    returnOutcome(outcome, r_result, r_pass, r_err);
  }
</source>
</font>
</div>
and the callback function
<div style="background-color: #f3f3f3; border-color: #1c9edb; border-width:1px; border-style: dashed;">
<font size="3">
<source lang=csharp>
  int __stdcall SensorChangeHandler(CPhidgetInterfaceKitHandle ifKit, void *usrptr, int Index, int Value)
  {
    int t_success;
    char * scmMsg;
    scmMsg=NULL;
    scmMsg = (char *)malloc(512);
    sprintf(scmMsg, “send \”%s %08x, %d, %d\” to current card”, (char *)usrptr, ifKit, Index, Value);
    SendCardMessage(scmMsg, &t_success);
    if (scmMsg != NULL)
      free(scmMsg);
    return 0;
  }
</source>
</font>
</div>
Note: For Mac OS X, __stdcall is not needed.
Here, we use SendCardMessage to run the LiveCode command “sensorChange”.
The message will be passed to the card that called phidgetsInterfaceKit_set_OnSensorChange_Handler before passing through the stack in the message hierarchy.
Other Phidgets and functions can be extended in a similar manner.
The C API manual contains calls and events for every type of Phidget.
==Building your Project==
Describe the different ways a project could be built using this language.
==Common Problems and Solutions/Workarounds==
Here you can put various frequent problems and our recommended solutions.
Here you can put various frequent problems and our recommended solutions.

Revision as of 18:19, 20 January 2012

Icon-LiveCode.png LiveCode is a high level programming language, developed by [RunRev] that draws on English-like syntax for rapid application development.

Introduction

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

LiveCode is capable of using the complete Phidget API, including events. We also provide example code in LiveCode for all Phidget devices.

LiveCode can be developed with Windows and Mac OS X..{{{5}}}

You can compare LiveCode with our other supported languages.

Quick Downloads

Just need the LiveCode drivers, libraries, and examples? Here they are:

C# API Documentation:

LiveCode Example Code:

Libraries and Drivers:

Getting started with LiveCode

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

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

Instructions are divided up by operating system. Choose:

Windows(2000/XP/Vista/7)

Libraries

Description

C# programs on Windows depend on the following files, which the installers above put onto your system:

  • phidget21.dll contains the actual Phidgets library, which is used at run-time. If you used our installer, it's already correctly placed in C:\Windows\System32. It can be manually installed - check our Manual Installation instructions.

You will also need one of the following two files, depending on the .NET framework version you are targeting:

  • Phidget21.NET.dll is the Phidgets library for .NET framework 2.0 or higher. Your compiler has to know where this file is. By default, our installer puts this file into 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. For more information, please see the section for your specific compiler/environment. If you do not want to use our installer, you can get the file here.
  • Phidget21.NET1.1.dll is the Phidgets library for .NET framework 1.1. Your compiler has to know where this file is. By default, our installer puts this file into 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. For more information, please see the section for your specific compiler/environment. If you do not want to use our installer, you can get the file here.

You can optionally install the following files:

  • Phidget21.NET.XML provides the IntelliSense in-line documentation for the .NET library in Visual Studio/MonoDevelop. This documentation is also visible in the Object Browser. By default, our installer puts this file into C:\Program Files\Phidgets. If you do not want to use our installer, you can get the file here and place it in the same directory as Phidget21.NET.dll.
  • Policy.2.1.Phidget21.NET.dll is the policy assembly for Phidget21.NET.dll. Our installer places this file in the Global Assembly Cache(GAC) directory. It directs any programs compiled against version 2.1.0 or higher of Phidget21.NET.dll to use the most recent installed version. If you do not want to use our installer, you can get the file here.


Use Our Examples

Please start by downloading the LiveCode Examples. These examples were written in LiveCode 4.5, but will also work with other versions.

To run the examples, you first download them from above and unpack them into a folder. To load all projects in Visual Studio, go to File | Open | Project, and open AllExamples/AllExamples.sln or AllExamples/AllExamples_vs2008.sln for Visual Studio 2005 and 2008, respectively.

If you are opening the Phidget examples in Visual Studio 2010, you will need to go through the Visual Studio Conversion Wizard to convert the 2005 or 2008 project.

VS2005 Conversion Wizard.PNG

This will load all of the examples available for C#, and then you can set your main project to be the one that matches your device. If you aren't sure what the software example for your device is called, check the software object listed in the Getting Started guide for your device.

The only thing left to do is to run the examples! Click on Debug -> Start Debugging. Please note that the projects, by default try to find the Phidget21.NET.dll in the C:\Program Files\Phidgets. If you have it installed in another location, please change the path accordingly. If you are receiving an error message regarding that the namespace Phidgets cannot be found, please re-add the reference to Phidget21.NET.dll. Please see the next section for details.

CSharp VS2005 Run.PNG

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

Write Your Own Code

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

  • Generate a new Visual C# Windows Applications project with a descriptive name such as PhidgetTest.


CSharp VS2005 New Project.PNG


  • Add a reference to the .NET Phidgets library.


CSharp VS2005 Add Reference.PNG

  • Under the .NET tab, select Phidget21.NET.dll.

If you used our installer, these files are installed in 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.
CSharp VS2005 Add Reference 2.PNG

Then, in your code, you will need to include the Phidget library:

  using Phidgets;
  using Phidgets.Events;

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

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


Mac OS X

LiveCode has excellent support on Mac OS X.

The first step in using LiveCode on Mac is to install the Phidget libraries. Compile and install them as explained on the getting started guide for your device. Then, the OS - Mac OS X page also describes the different Phidget files, their installed locations, and their roles....

Follow The Examples

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

Next, comes our API information. These resources outline the LiveCode Phidget methods:

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

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

Example Flow

The Hello World example has this general structure so you can follow along. We also have an in-depth general introduction to writing Phidget code (like open, read data, etc), as well as the C# API for specific syntax:

// ----- Event and Other Functions -----

Create any Language-Specific Functions (exception handling)

Create General Attach, Detach, and Error Handling Functions:

On attach: Print Hello Message
On detach: Print Goodbye Message

 

In LiveCode, you can name these event functions whatever you like. You will then pass them as function pointers to the Phidget library below in the Main Code section. This hooks them into the actual events when they occur.
In the example code, the event functions common to all Phidgets are called things like AttachHandler() and DetachHandler(), etc.

Some event functions will be specific to each device, like when a tag is read on an RFID board, or when a sensor value changes on an Interface Kit. Other functions are given in the examples to show you more detail on using your Phidget. For example, DeviceInitialize() will show what needs to be set up for your Phidget before using it.

// ----- Main Code -----

Create Manager Software Object
Hook Event Functions created above to Device
Open Device

Wait for 'Enter' key character input
Handle on-going attach and detach events
Print Hello and Goodbye messages
Exit upon input

Close Device

Delete Device

 

Creating a Phidget software object in C# is specific to the Phidget. For a Phidget Spatial, for example, this would involve creating a Spatial object. The examples show how to do this and other API functions.

The object provides device specific methods and properties which are available from the API for your specific Phidget.

Common Problems and Solutions/Workarounds

Here you can put various frequent problems and our recommended solutions.