Language - Max/MSP: Difference between revisions

From Phidgets Support
No edit summary
(43 intermediate revisions by 5 users not shown)
Line 1: Line 1:
<metadesc>Communicate over USB with sensors, controllers and relays with Phidgets! Our Max/MSP library is supported under Windows or MacOS.</metadesc>
[[Category:Language]]
[[Category:Language]]
{{OSLang|[[File:icon-MaxMSP.png|link=|alt=MaxMSP|64x64px]]|Max/MSP, developed by [http://cycling74.com Cycling74] is a visual programming language for creating music and media applications.}}
__TOC__
__TOC__


==Introduction==


{{LanguageSupport|Max/MSP|most of the Phidget API, including events. All the supported functions for Max/MSP will be documented in the {{Code|.help}} files|all Phidget devices.| Windows and OS X|
== Quick Downloads ==


Only Max/MSP 4.5 or higher is supported.}}
=== Documentation ===


==Quick Downloads==
*{{Phidget22API}} (select Max/MSP from the drop-down menu)
{{QuickDownloads|Max/MSP|
 
* [{{SERVER}}/downloads/examples/WinMaxMSP.zip {{Code|.help}} file (Windows - same file as Max/MSP Examples and Libraries)]
=== Example Code ===
* [{{SERVER}}/downloads/examples/WinMaxMSP.zip {{Code|.help}} file (OS X - same file as Max/MSP Examples and Libraries)]|
* [{{SERVER}}/downloads/phidget22/libraries/windows/Phidget22MaxMSP.zip Max/MSP Examples]
{{ExampleQuickDownloads|{{SERVER}}/downloads/examples/WinMaxMSP.zip|(Windows - same file as Max/MSP Documentation and Libraries)}}
 
{{ExampleQuickDownloads|{{SERVER}}/downloads/examples/WinMaxMSP.zip|(OS X - same file as Max/MSP Documentation and Libraries)}}|{{ExtraLibraryQuickDownloads|{{SERVER}}/downloads/examples/WinMaxMSP.zip|Max/MSP|(Windows - same file as Max/MSP Documentation and Examples)}}
=== Libraries ===
{{ExtraLibraryQuickDownloads|{{SERVER}}/downloads/examples/WinMaxMSP.zip|Max/MSP|(OS X - same file as Max/MSP Documentation and Examples)}}
*[{{SERVER}}/downloads/phidget22/libraries/windows/Phidget22MaxMSP.zip Phidget Max/MSP library - Windows]
*[{{SERVER}}/downloads/phidget22/libraries/macos/Phidget22MaxMSP.zip Phidget Max/MSP library - macOS]
{{WindowsQuickDownloads}}
{{WindowsQuickDownloads}}
{{MacQuickDownloads}}
{{MacQuickDownloads}}
}}


==Getting started with Max/MSP==
== Getting Started with Max/MSP ==
Welcome to using Phidgets with Max/MSP! By using Max/MSP, you will have access to the complete {{Phidget22API}}, including events. We also provide example code in Max/MSP for all Phidget devices.
 
Phidget22 supports Max/MSP versions 6 and up.
 
If you are developing for Windows, keep reading. Otherwise, jump ahead to [[#macOS | macOS]].
 
== Windows ==
{{Windows_Languages}}
===Use Our Examples===
One of the best ways to start programming with Phidgets is to use our example code as a guide. In order to run the examples, you will need to download and install Max/MSP from [https://cycling74.com/downloads Cycling '74].
 
 
Next, download and unpack the Phidgets Max/MSP library:
*[{{SERVER}}/downloads/phidget22/libraries/windows/Phidget22MaxMSP.zip Phidget Max/MSP library]
 
 
After unpacking the download, navigate to the ''externals'' folder. Copy the following folder to your clipboard:
*32-bit Max/MSP -> copy the ''x86'' folder
*64-bit Max/MSP -> copy the ''x64'' folder
 


If you are new to writing code for Phidgets, we recommend starting by running, then modifying existing examples. This will allow you to:
[[Image:Max_externals.png|link=|center]]
{{ExampleCodeReasons}}


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


==Windows (2000/XP/Vista/7)==
The folder you copied needs to be placed in a specific location for Max/MSP to reference it. We will find this location in in the next step.
 
===Description of Library Files===
Max/MSP programs on Windows depend on the following files. The installers in the [[#Libraries and Drivers | Quick Downloads]] section put only the {{Code|phidget21.dll}} into your system. You will need to manually put the {{Code|PhidgetXXX.mxe}} onto your system.
* <b><code>phidget21.dll</code></b> contains the actual Phidget library, which is used at run-time. By default, the installer places it in <code>C:\Windows\System32</code>.
* <b><code>PhidgetXXX.mxe</code></b> is the Phidget library for your specific device. XXX denotes the name of your device, Please make sure the <code>.mxe</code> file corresponds with the device you are using. For example, if you are using the PhidgetInterfaceKit, you will need the {{Code|PhidgetInterfaceKit.mxe}}. It is to be placed in the same directory as your Max/MSP application or anywhere in the Max/MSP search path.


If you do not want to use our installer, you can download the {{Code|[{{SERVER}}/downloads/libraries/phidget21-x86.zip phidget21.dll]}} and manually install it where you want; refer to our [[OS_-_Windows#Manual_File_Installation|Manual Installation Instructions]].


===Use Our Examples===
Open Max/MSP and navigate to ''Options'' -> ''File Preferences''.
[[Image:Max_filepreferences.png|link=|center]]


Please start by downloading the [{{SERVER}}/downloads/examples/WinMaxMSP.zip  Max/MSP Examples and Library] and unpack them into a folder.  These examples were written in Max/MSP 4.6, but any version above 4.5 are also supported.


Here, you will find example programs, in {{code | .help}} format for all the devices. {{FindYourDevice}}
You will see something similar to this:
[[Image:Max_filepreferences_locations.PNG|link=|center]]


The only thing left to do is to run the examples! Open the {{code| .help}} file in the Max environment.


Once you have the Max/MSP examples running, we have a [[#Follow the Examples|teaching]] section below to help you follow them.
The folder that you copied earlier needs to be placed in any of the locations listed in ''File Preferences''. Navigate to one of the locations and paste the folder:
[[Image:Max_folder.PNG|link=|center]]


===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 the Max/MSP environment to properly link the Phidget Max/MSP libraries. To begin:
The Phidget Max/MSP library is now being referenced. Next, navigate to the ''examples'' folder located within the Phidget22MaxMSP folder you previously unpacked:
[[Image:Max_examples.PNG|link=|center]]


Place the {{Code|.mxe}} in the same directory as the patcher, or anywhere in the Max/MSP search path. You can verify the search path locations by navigating to Options &rarr; File Preferences.


[[File:MaxMSP Path.PNG|link=|alt=Search Path]]
From here, select an example that will work with your Phidget and open it in Max/MSP. You can run the example by simply pressing the ''start'' button:
[[Image:Max_run.png|link=|center]]


The best way to start writing your patch is to modify an example, and saving it as a {{Code|.pat}} file.
You should now have the example up and running for your device. Play around with the device and experiment with some of the functionality. When you are ready, the next step is configuring your project and writing your own code!


If you wish to start a new patch. All you need to do is create an object named PhidgetXXX where XXX is the name of your device. For example, the [{{SERVER}}/products.php?product_id=1023 PhidgetRFID device] will have the {{Code|PhidgetRFID}} object name.
===Configure Your Project===
The project now has access to the Phidget function calls and you are ready to begin coding.
When you are building a project from scratch, or adding Phidget function calls to an existing project, you'll need to configure the Max/MSP environment to properly link the Phidget Max/MSP library.


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


==OS X==
Ensure the Phidget library files are moved to a location in the Max/MSP search path. This procedure is covered in detail above in the [[#Use our examples | use our examples]] section.


Max/MSP has excellent support on OS X.


The first step in using Max/MSP on Mac is to install the Phidget library. Compile and install as described on the [[OS - OS X]] page, which also describes the different Phidget files, their installed locations, and their roles.
After moving the library files into the correct location your project will have access to Phidgets. Next, view the [[#Write Code | write your own code]] section below.


==macOS==
{{macOS_Languages}}
===Use Our Examples===
===Use Our Examples===
One of the best ways to start programming with Phidgets is to use our example code as a guide. In order to run the examples, you will need to download and install Max/MSP from [https://cycling74.com/downloads Cycling '74].
Next, download and unpack the Phidgets Max/MSP library:
*[{{SERVER}}/downloads/phidget22/libraries/macos/Phidget22.dmg Phidget Max/MSP library]


Please start by downloading the [{{SERVER}}/downloads/examples/MaxMSP.zip  Max/MSP Examples and Library] and unpack them into a folder.  These examples were written in Max/MSP 4.6, but any version above 4.5 are also supported.
After unpacking the download, navigate to the ''externals'' folder and copy it to your clipboard:
[[Image:Max_mac_externals.png|link=|center]]


Here, you will find example programs, in {{code | .help}} format for all the devices. {{FindYourDevice}}


The only thing left to do is to run the examples! Open the {{code| .help}} file in the Max environment.
The folder you copied needs to be placed in a specific location for Max/MSP to reference it. We will find this location in in the next step.


Once you have the Max/MSP examples running, we have a [[#Follow the Examples|teaching]] section below to help you follow them.


===Write Your Own Code===
Open Max/MSP and navigate to ''Options'' -> ''File Preferences''.
[[Image:Max_mac_filepreferences.png|link=|center]]


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


Place the {{Code|.mxe}} in the same directory as the patcher, or anywhere in the Max/MSP search path. You can verify the search path locations by navigating to Options &rarr; File Preferences.  
You will see something similar to this:
[[Image:Max_mac_filepreferences_locations.png|link=|center]]


[[File:MaxMSP MacPath.PNG|link=|alt=Search Path]]


The best way to start writing your patch is to modify an example, and saving it as a {{Code|.pat}} file.
The folder that you copied earlier needs to be placed in any of the locations listed in ''File Preferences''. Navigate to one of the locations and paste the folder:
[[Image:Max_mac_folder.png|link=|center]]


If you wish to start a new patch. All you need to do is create an object named PhidgetXXX where XXX is the name of your device. For example, the [{{SERVER}}/products.php?product_id=1023 PhidgetRFID device] will have the {{Code|PhidgetRFID}} object name.
The project now has access to the Phidget function calls and you are ready to begin coding.


The same [[#Follow the Examples|teaching]] section which describes the examples also has further resources for programming your Phidget.
The Phidget Max/MSP library is now being referenced. Next, navigate to the ''examples'' folder located within the Phidget22MaxMSP folder you previously unpacked:
[[Image:Max_mac_examples.png|link=|center]]


==Follow the Examples==


By following the instructions for your operating system, 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.
From here, select an example that will work with your Phidget and open it in Max/MSP. You can run the example by simply pressing the ''start'' button:
[[Image:Max_mac_run.png|link=|center]]


First, let's explain how to operate the example.  
You should now have the example up and running for your device. Play around with the device and experiment with some of the functionality. When you are ready, the next step is configuring your project and writing your own code!


[[File:MaxMSP Example.PNG|link=|alt=Example]]
===Configure Your Project===
When you are building a project from scratch, or adding Phidget function calls to an existing project, you'll need to configure the Max/MSP environment to properly link the Phidget Max/MSP library.


The above screenshot is what shows up when you open the example for the PhidgetInterfaceKit. If you are using a different device, your example patch will be different, but the idea is the same.


For this particular example, the Max object is called {{Code|PhidgetInterfaceKit}}, which is located in the center of the screen. Objects/message boxes are connected to the inputs and outputs of the PhidgetInterfaceKit object. The input objects will either cause a property of the device to change or request for a property to be retrieved. The output objects return the retrieved information. All the supported features of the device are shown in the {{Code|.help}} file.  
Ensure the Phidget library files are moved to a location in the Max/MSP search path. This procedure is covered in detail above in the [[#Use our examples 2 | use our examples]] section.  


Try it for yourself! Click on the {{Code | getSerial}} message box to request the Phidget to retrieve the serial number of the device. You should see the last output object of {{Code|route}} changed to the serial number of your device. All devices support the {{Code|getSerial}} object, and is the easiest way to determine if the Phidget libraries are correctly set up, and whether the Max/MSP application is connected to your device.


[[File:MaxMSP getSerial.PNG|link=|alt=getSerial]]
After moving the library files into the correct location your project will have access to Phidgets. Next, view the [[#Write Code | write your own code]] section below.


If your example contains the {{Code|read}} message box, click on it. This will return device specific values to the screen.
== Write Code ==
{{WriteCode_Intro|Max/MSP|MaxMSP}}


[[File:MaxMSP read.PNG|link=|alt=Read Data]]
=== Example Flow ===
First, let's explain how to operate the examples. We will take a look at the PhidgetVoltageInput example:
[[Image: MSP Example.png|link=|center]]


If your example contains the {{Code|start}} message box, you can continuously poll for events. Set the sample rate input for the {{Code|setSampleRate}} object. Then, press the {{Code|start}} message box to start sampling. When a noteworthy event occurs on a Phidget(i.e., when an analog sensor senses a change in the environment), the value will be displayed onto the screen. Press the {{Code|stop}} message box to stop sampling.


For the PhidgetInterfaceKit example, there also a {{Code|setoutput}} message box. Clicking on the check boxes will cause the digital outputs of the PhidgetInterfaceKit device to change. Your example may contain device specific message boxes/objects to click on. See for yourself what they do!
For this particular example, the Max object is called PhidgetVoltageInput, which is located near the bottom left of the screen. Objects/message boxes are connected to the inputs and outputs of the PhidgetVoltageInput object. The input objects will either cause a property of the device to change or request for a property to be retrieved. The output objects return the retrieved information. All the supported functions for Max/MSP will be documented in the {{Phidget22API}} for your device.


For information regarding calls specific to your device, please see the API for your specific device, which can be found on its product page on our [{{SERVER}} main website]. Please note that some device functionality are not supported in Max/MSP; the {{Code|.help}} example shows the complete list of functionality supported under Max/MSP.


===Code Snippets===
Try it for yourself! Click on the getDeviceSerialNumber message box to request the Phidget to retrieve the serial number of the device. You should see the a message in the Max window denoting the serial number of your device. All devices support the getDeviceSerialNumber message, making it an easy way to determine if the Phidget libraries are correctly set up, and whether the Max/MSP application is connected to your device.
[[Image: MaxMSP_GetSerial.png|link=|center]]


Your best resource to program code {{Code|.help}} files in MaxMSP will be our examples.  The examples are both our complete API (they include all of the calls for each Phidget you can use, and none you can't) and examples of how to use our API. 


If you aren't familiar with concepts in Phidget programming, you may find our [[General Phidget Programming]] page helpful.  It provides a very generic overview of what traditional languages follow when using Phidgets. For setup 'syntax', your main resource will of course be this MaxMSP page and the examples. But for conceptual details about particular actions - opening a Phidget, for example - the General Phidget Programming page is a more in-depth resource.
If your example contains a get[DataType] message box, click on it to cause the associated data to be output with a relevant prefix. [DataType] can denote any data the object may offer, such as Voltage for a voltage input. A list of available messages and their associated outputs is outlined in the {{Phidget22API}} for your device.
[[Image: MaxMSP_GetVoltage.png|link=|center]]


Keep in mind when reading these general resources that the Max/MSP libraries may not implement the full Phidget API - some function calls and Phidget classes may not be supported. The {{Code|.help}} files included with the Phidget externals show all the supported function calls for their type of Phidget.


In general, Phidgets can be placed inside the patcher using Max objects, and functions can be called on them using appropriately connected messages. We go over a basic setup for this below.
If your example contains the start message box, you can continuously poll for events. Just press the start message box to start sampling. When an event occurs on a Phidget (i.e. when a sensor detects a change in the measured data), associated data will be output with related prefixes. Press the stop message box to stop sampling.


====Step One: Initialize and Open====


This tutorial uses a Phidget Interface Kit and a new instance will be created.  This initializing and opening can be done by placing a new "object" object.  Other objects can handle different Phidgets - a Spatial, a Temperature Sensor, a Motor Controller, etc. Only the name of the object changes. You can find the name in the example {{Code|.help}} file for your device. {{FindYourDevice}}
For the PhidgetDigitalOutput example, there are setDutyCycle and setState message boxes. Changing the numbers will cause the digital output to change. Your example may contain device specific message boxes to click on. Click them to see what they do!


In the case of an Interface Kit, we name it PhidgetInterfaceKit:
=== Code Snippets ===
Your best resource to program code in MaxMSP will be our examples.


[[Image:Max1_.png|link=|alt=]]
If you aren't familiar with concepts in Phidget programming, you may find our General Phidget Programming page helpful. It provides a very generic overview of what traditional languages follow when using Phidgets. For setup 'syntax', your main resource will of course be this MaxMSP page, the {{Phidget22API}}, and the examples. But for conceptual details about particular actions - opening a Phidget, for example - the General Phidget Programming page is a more in-depth resource.


'''Important:''' a local connection will reserve the device until closed. This prevents any other instances from retrieving data from the Phidget, including other programs.  Every Phidget object in Max will
Keep in mind when reading these general resources that the Max/MSP libraries may not implement the full {{Phidget22API}} - some function calls and Phidget classes may not be supported.
automatically try to connect to and reserve the Phidget for itself. As long as a MaxMSP Phidget program is running, it will continuously try to connect to a Phidget, even trying to reconnect if it gets disconnected.


When the instance is created as with the Interface Kit above, normally it will make a connection to the first device of its type it can find. The Phidget object can also be declared with a serial number to open a specific Phidget Interface Kit instead:
In general, Phidget objects can be placed inside the patcher, and functions can be called on them using appropriately connected messages. We go over a basic setup for this below.


[[Image:Max2_.png|link=|alt=]]
==== Step One: Initialize and Open ====
This tutorial uses a Phidget Voltage Input, and thus the object we use will be called PhidgetVoltageInput. Initializing and opening the device is be done by placing a new object. Other objects handle different Phidgets - a Spatial, a Temperature Sensor, a Motor Controller, etc. Only the name of the object changes. You can find the name for the object in the .maxhelp file for your device. The help file will be named the same as the software object for your device. If you are not sure what the software object for your device is, find your Phidget on our [http://www.phidgets.com webpage], and then check the API documentation for it.


=====Using a Phidget over a Network=====
In the case of a Voltage Input, we name it PhidgetVoltageInput:
[[Image: MaxMSP_VoltageInput_generic.png|link=|center]]


The one connection per device limit does not apply when using the [[Phidget WebService]]. In MaxMSP, we can use this to our advantage in order to have multiple programs access one Phidget, or to simply use a Phidget remotely.
'''Important:''' a local connection will reserve the device until closed. This prevents any other instances from retrieving data from the Phidget, including other programs. Every Phidget object in Max will automatically try to connect to and reserve a Phidget for itself. As long as a MaxMSP Phidget object is running, it will continuously try to connect to a Phidget, even trying to reconnect if it gets disconnected.


To use the WebService, first the Phidget needs to be plugged in to a computer that has the WebService turned on within your local network.  (For information on how to do this, see the [[Phidget WebService]] page in the section on how to use the WebService for your operating system).  Then, in MaxMSP, we can change the open "object" text for various types of opening over a network.  Many examples are given below.
=====Specifying a Phidget=====
When the instance is created as with the Voltage Input above, normally it will make a connection to the first device of its type it can find. The Phidget object can also be declared with a number of specifiers to open a specific Phidget instead.


Open a remote Interface Kit using a Server ID:
Specifiers can be added to the object in the format:
'''PhidgetExternal {Specifiers}'''


<div class="source"><syntaxhighlight lang=text>
These will be written in the form:
PhidgetInterfaceKit remote “Some remote serverID”
'''specifier=value'''
</syntaxhighlight></div>


Open a remote Interface Kit using IP address and port:
The full list of specifiers that can be used to identify a Phidget in Max/MSP are as follows:
{|
|-
|'''serialnumber'''
|The serial number of the device
|-
|'''channel'''
|The channel of the device to open
|-
|'''hubport'''
|The hub port the device is plugged into (where applicable)
|-
|'''ishubport'''
|Specifies whether this channel should be opened on a hub port directly, or on a VINT device attached to a hub port.
|-
|'''remote'''
|Forces the object to connect to a remote device over a network, ignoring devices on the local machine
|-
|'''local'''
|Forces the object to connect to a device plugged into the local machine, ignoring network devices
|}


<div class="source"><syntaxhighlight lang=text>
For instance, to open a VoltageInput with serial number 349428, you would use:
PhidgetInterfaceKit remoteip 192.168.2.5 5001
[[Image: MaxMSP VoltageInput specific.png|link=|center]]
</syntaxhighlight></div>


Open a remote Interface Kit with serial number 35569 on serverID “My PC”, with password “pass”:
Some other examples would be:


<div class="source"><syntaxhighlight lang=text>
''Open a Digital Input:''
PhidgetInterfaceKit 35569 remote “My PC” pass
    PhidgetDigitalInput
</syntaxhighlight></div>


Open a remote Interface Kit with serial number 35569, on any remote server
''Open channel 1 of a Digital Input on port 2 of a hub with serial number 35569''
    PhidgetDigitalInput serialnumber=35569 channel=1 hubport=2


<div class="source"><syntaxhighlight lang=text>
''Open open hub port 2 as a DigitalInput for a hub with serial number 35569''
PhidgetInterfaceKit 35569 remote
    PhidgetDigitalInput serialnumber=35569 hubport=2 ishubport=1
</syntaxhighlight></div>


Open the Interface Kit remotely on a default-named PhidgetSBC:
''Open a Digital Input with serial number 35569 on a remote server''
    PhidgetDigitalInput serialnumber=35569 remote=1


<div class="source"><syntaxhighlight lang=text>
To access Phidgets over a network, use the PhidgetNet object in your patcher
PhidgetInterfaceKit remote phidgetsbc
</syntaxhighlight></div>


====Step Two: Wait for Attachment (plugging in) of the Phidget====
=====Using a Phidget Over a Network=====


Although this is a required step in many of our [[Software Overview|other languages]] (and therefore you may be expecting this if coming from another Phidget language), in MaxMSP you do not have to add a specific waitForAttachment block.
To use the Network Server, first the Phidget needs to be plugged in to a computer that has the Network  Server turned on within your local network. (For information on how to do this, see the Phidget Network Server page in the section on how to use the Network Server for your operating system).


Keep in mind, however, that if your Phidget is not responding within your MaxMSP program, it may simply not be plugged in!
Next, in your patch you need a PhidgetNet object. To automatically find local networks, send it a message saying enableServerDiscovery.
[[Image: MaxMSP Enable Server Discovery.png|link=|center]]


====Step Three: Do Things with the Phidget====


There are two main approaches for retrieving data when working with Phidgets.  Data is accessed either by one-time polling, or at a fixed rate via internal timers.  
Then, to connect a Phidget over the network, change the object text to specify it is to connect to a Phidget on a remote server, as per the following example.
[[Image: MaxMSP Remote.png|link=|center]]


Getting or setting values ''directly'' via polling on the Phidget is done through messages linked to the inlet. The object’s inlet can be wired to send commands to the device, and the outlet used to retrieve the results. Setting values on the Phidget is achieved by using the set messages, and some properties can be read with get messages:
==== Step Two: Wait for Attachment (plugging in) of the Phidget ====


[[Image:Max3_.png|link=|alt=]]
Although this is a required step in many of our other languages (and therefore you may be expecting this if coming from another Phidget language), in MaxMSP you do not have to add a specific waitForAttachment block.


For data, polling occurs through the "read" message.  To sample at a ''fixed rate'' you use setSampleRate and use the start and stop message.  Read will read the data off the Phidget once, while using start will trigger data to be sent or received in periodic intervals. If the sample rate is set to -1, then data output is only triggered on a change:
Keep in mind, however, that if your Phidget is not responding within your MaxMSP program, it may simply not be plugged in! Send a getAttached message to a Phidget object at any time to see if it's attached.


[[Image:Max4_.png|link=|alt=]]
==== Step Three: Do Things with the Phidget ====


Phidget-'''specific''' data is always given a prefix in Max to allow for their routing. For instance, the digital input states are given the prefix “di” and the analog inputs similarly use “ai”. The specific prefixes used for each Phidget is listed in their respective help file.  
There are two main approaches for retrieving data when working with Phidgets. Data is accessed either by one-time polling or at a fixed rate via on-board timers for some devices.


Data '''common''' to all Phidgets, such as the serial number, is not prefixed.  
Getting or setting values directly via polling on the Phidget is done through messages linked to the inlet. The object’s inlet can be wired to send commands to the device, and the outlet used to retrieve the results. Setting values on the Phidget is achieved by using the set messages, and some properties can be read with get messages:
[[Image: MaxMSP_get_and_set.png|link=|center]]


With the prefixes and common data, the following picture would be an example of how to route and split some of the data for the PhidgetInterfaceKit:


[[Image:Max5_.png|link=|alt=]]
To sample at a fixed rate, use the start and stop messages to start and stop the data flow, respectively.
[[Image: MaxMSP_Start_Stop.png|link=|center]]


====Step Four: Close and Delete====
Data from the outlet of a Phidget object is always given a prefix to allow for routing. For instance, the digital input state state change event data is given the prefix “stateChange”, and the voltage input voltage change event similarly use “voltageChange”. The specific prefixes used for each set of outlet data is listed under the API page for their class in the {{Phidget22API}}.
[[Image: MaxMSP_Routing.png|link=|center]]


Although this is a required step in many of our [[Software Overview|other languages]] (and therefore you may be expecting this if coming from another Phidget language), in MaxMSP you do not have to add a specific close and delete block.
 
The rightmost outlet on the Phidget object outputs error event data. This will send information on error events such as saturation events. To see which error events may apply to your device, check its API page in the {{Phidget22API}}.
[[Image: MaxMSP_Error_Event.png|link=|center]]
 
 
==== Step Four: Close and Delete ====
 
Although this is a required step in many of our other languages (and therefore you may be expecting this if coming from another Phidget language), in MaxMSP you do not have to add a specific close and delete block.


====Special Case: Multiple Phidgets====
====Special Case: Multiple Phidgets====


Multiple Phidgets of the same type can easily be used inside a single program, it only requires another Phidget object placed. If two of the same type of Phidget object are placed, the serial number argument should always be specified to ensure that the correct Phidget gets associated with the correct object.
Multiple Phidgets of the same type can easily be used inside a single program, it only requires another Phidget object placed. If two of the same type of Phidget object are placed, the serial number and channel arguments should always be specified (as well as hub port, if applicable) to ensure that the correct Phidget gets associated with the correct object.
 
== Further Reading ==
 
[[Phidget Programming Basics]] - Here you can find the basic concepts to help you get started with making your own programs that use Phidgets.
 
[[Data Interval/Change Trigger]] - Learn about these two properties that control how much data comes in from your sensors.


==Common Problems and Solutions/Workarounds==
[[Using Multiple Phidgets]] - It can be difficult to figure out how to use more than one Phidget in your program. This page will guide you through the steps.


{{ProblemSolution|Crash|When a patch file is closed in the Max/MSP environment, the program crashes}}
[[Polling vs. Events]] - Your program can gather data in either a polling-driven or event-driven manner. Learn the difference to determine which is best for your application.


If in your Max/MSP environment, you have more patches(of the same Phidget object) than you have of the actual hardware device, the Max/MSP environment may crash. This is due to the fact that a single Phidget Max/MSP object <i>only</i> corresponds to a single Phidget device hardware. For example, your Max/MSP environment may experience unexpected behavior while you have one PhidgetInterfaceKit device connected to the computer, but you have a two seperate patches opened with a single PhidgetInterfaceKit Max object in each one.
[[Logging, Exceptions, and Errors]] - Learn about all the tools you can use to debug your program.


Likely Fix: Please ensure that you do not use more patches(of the same Phidget object) than you have of the actual Phidget device.
[[Phidget Network Server]] - Phidgets can be controlled and communicated with over your network- either wirelessly or over ethernet.

Revision as of 21:32, 26 June 2018


Quick Downloads

Documentation

Example Code

Libraries

Getting Started with Max/MSP

Welcome to using Phidgets with Max/MSP! By using Max/MSP, you will have access to the complete Phidget22 API, including events. We also provide example code in Max/MSP for all Phidget devices.

Phidget22 supports Max/MSP versions 6 and up.

If you are developing for Windows, keep reading. Otherwise, jump ahead to macOS.

Windows

If you haven't already, please visit the Windows page before you continue reading. There you will be instructed on how to properly set up your Windows machine so you can follow the guides below!

Use Our Examples

One of the best ways to start programming with Phidgets is to use our example code as a guide. In order to run the examples, you will need to download and install Max/MSP from Cycling '74.


Next, download and unpack the Phidgets Max/MSP library:


After unpacking the download, navigate to the externals folder. Copy the following folder to your clipboard:

  • 32-bit Max/MSP -> copy the x86 folder
  • 64-bit Max/MSP -> copy the x64 folder


Max externals.png


The folder you copied needs to be placed in a specific location for Max/MSP to reference it. We will find this location in in the next step.


Open Max/MSP and navigate to Options -> File Preferences.

Max filepreferences.png


You will see something similar to this:

Max filepreferences locations.PNG


The folder that you copied earlier needs to be placed in any of the locations listed in File Preferences. Navigate to one of the locations and paste the folder:

Max folder.PNG


The Phidget Max/MSP library is now being referenced. Next, navigate to the examples folder located within the Phidget22MaxMSP folder you previously unpacked:

Max examples.PNG


From here, select an example that will work with your Phidget and open it in Max/MSP. You can run the example by simply pressing the start button:

Max run.png


You should now have the example up and running for your device. Play around with the device and experiment with some of the functionality. When you are ready, the next step is configuring your project and writing your own code!

Configure Your Project

When you are building a project from scratch, or adding Phidget function calls to an existing project, you'll need to configure the Max/MSP environment to properly link the Phidget Max/MSP library.


Ensure the Phidget library files are moved to a location in the Max/MSP search path. This procedure is covered in detail above in the use our examples section.


After moving the library files into the correct location your project will have access to Phidgets. Next, view the write your own code section below.

macOS

If you haven't already, please visit the macOS page before you continue reading. There you will be instructed on how to properly set up your macOS machine so you can follow the guides below!

Use Our Examples

One of the best ways to start programming with Phidgets is to use our example code as a guide. In order to run the examples, you will need to download and install Max/MSP from Cycling '74.


Next, download and unpack the Phidgets Max/MSP library:


After unpacking the download, navigate to the externals folder and copy it to your clipboard:

Max mac externals.png


The folder you copied needs to be placed in a specific location for Max/MSP to reference it. We will find this location in in the next step.


Open Max/MSP and navigate to Options -> File Preferences.

Max mac filepreferences.png


You will see something similar to this:

Max mac filepreferences locations.png


The folder that you copied earlier needs to be placed in any of the locations listed in File Preferences. Navigate to one of the locations and paste the folder:

Max mac folder.png


The Phidget Max/MSP library is now being referenced. Next, navigate to the examples folder located within the Phidget22MaxMSP folder you previously unpacked:

Max mac examples.png


From here, select an example that will work with your Phidget and open it in Max/MSP. You can run the example by simply pressing the start button:

Max mac run.png


You should now have the example up and running for your device. Play around with the device and experiment with some of the functionality. When you are ready, the next step is configuring your project and writing your own code!

Configure Your Project

When you are building a project from scratch, or adding Phidget function calls to an existing project, you'll need to configure the Max/MSP environment to properly link the Phidget Max/MSP library.


Ensure the Phidget library files are moved to a location in the Max/MSP search path. This procedure is covered in detail above in the use our examples section.


After moving the library files into the correct location your project will have access to Phidgets. Next, view the write your own code section below.

Write Code

By following the instructions for your operating system and compiler above, you now have working examples and a project that is configured. This teaching section will help you understand how the examples were written so you can start writing your own code.


Remember: your main reference for writing Max/MSP code will be the Phidget22 API Manual and the example code.

Example Flow

First, let's explain how to operate the examples. We will take a look at the PhidgetVoltageInput example:

MSP Example.png


For this particular example, the Max object is called PhidgetVoltageInput, which is located near the bottom left of the screen. Objects/message boxes are connected to the inputs and outputs of the PhidgetVoltageInput object. The input objects will either cause a property of the device to change or request for a property to be retrieved. The output objects return the retrieved information. All the supported functions for Max/MSP will be documented in the Phidget22 API for your device.


Try it for yourself! Click on the getDeviceSerialNumber message box to request the Phidget to retrieve the serial number of the device. You should see the a message in the Max window denoting the serial number of your device. All devices support the getDeviceSerialNumber message, making it an easy way to determine if the Phidget libraries are correctly set up, and whether the Max/MSP application is connected to your device.

MaxMSP GetSerial.png


If your example contains a get[DataType] message box, click on it to cause the associated data to be output with a relevant prefix. [DataType] can denote any data the object may offer, such as Voltage for a voltage input. A list of available messages and their associated outputs is outlined in the Phidget22 API for your device.

MaxMSP GetVoltage.png


If your example contains the start message box, you can continuously poll for events. Just press the start message box to start sampling. When an event occurs on a Phidget (i.e. when a sensor detects a change in the measured data), associated data will be output with related prefixes. Press the stop message box to stop sampling.


For the PhidgetDigitalOutput example, there are setDutyCycle and setState message boxes. Changing the numbers will cause the digital output to change. Your example may contain device specific message boxes to click on. Click them to see what they do!

Code Snippets

Your best resource to program code in MaxMSP will be our examples.

If you aren't familiar with concepts in Phidget programming, you may find our General Phidget Programming page helpful. It provides a very generic overview of what traditional languages follow when using Phidgets. For setup 'syntax', your main resource will of course be this MaxMSP page, the Phidget22 API, and the examples. But for conceptual details about particular actions - opening a Phidget, for example - the General Phidget Programming page is a more in-depth resource.

Keep in mind when reading these general resources that the Max/MSP libraries may not implement the full Phidget22 API - some function calls and Phidget classes may not be supported.

In general, Phidget objects can be placed inside the patcher, and functions can be called on them using appropriately connected messages. We go over a basic setup for this below.

Step One: Initialize and Open

This tutorial uses a Phidget Voltage Input, and thus the object we use will be called PhidgetVoltageInput. Initializing and opening the device is be done by placing a new object. Other objects handle different Phidgets - a Spatial, a Temperature Sensor, a Motor Controller, etc. Only the name of the object changes. You can find the name for the object in the .maxhelp file for your device. The help file will be named the same as the software object for your device. If you are not sure what the software object for your device is, find your Phidget on our webpage, and then check the API documentation for it.

In the case of a Voltage Input, we name it PhidgetVoltageInput:

MaxMSP VoltageInput generic.png

Important: a local connection will reserve the device until closed. This prevents any other instances from retrieving data from the Phidget, including other programs. Every Phidget object in Max will automatically try to connect to and reserve a Phidget for itself. As long as a MaxMSP Phidget object is running, it will continuously try to connect to a Phidget, even trying to reconnect if it gets disconnected.

Specifying a Phidget

When the instance is created as with the Voltage Input above, normally it will make a connection to the first device of its type it can find. The Phidget object can also be declared with a number of specifiers to open a specific Phidget instead.

Specifiers can be added to the object in the format: PhidgetExternal {Specifiers}

These will be written in the form: specifier=value

The full list of specifiers that can be used to identify a Phidget in Max/MSP are as follows:

serialnumber The serial number of the device
channel The channel of the device to open
hubport The hub port the device is plugged into (where applicable)
ishubport Specifies whether this channel should be opened on a hub port directly, or on a VINT device attached to a hub port.
remote Forces the object to connect to a remote device over a network, ignoring devices on the local machine
local Forces the object to connect to a device plugged into the local machine, ignoring network devices

For instance, to open a VoltageInput with serial number 349428, you would use:

MaxMSP VoltageInput specific.png

Some other examples would be:

Open a Digital Input:

   PhidgetDigitalInput

Open channel 1 of a Digital Input on port 2 of a hub with serial number 35569

   PhidgetDigitalInput serialnumber=35569 channel=1 hubport=2

Open open hub port 2 as a DigitalInput for a hub with serial number 35569

   PhidgetDigitalInput serialnumber=35569 hubport=2 ishubport=1

Open a Digital Input with serial number 35569 on a remote server

   PhidgetDigitalInput serialnumber=35569 remote=1

To access Phidgets over a network, use the PhidgetNet object in your patcher

Using a Phidget Over a Network

To use the Network Server, first the Phidget needs to be plugged in to a computer that has the Network Server turned on within your local network. (For information on how to do this, see the Phidget Network Server page in the section on how to use the Network Server for your operating system).

Next, in your patch you need a PhidgetNet object. To automatically find local networks, send it a message saying enableServerDiscovery.

MaxMSP Enable Server Discovery.png


Then, to connect a Phidget over the network, change the object text to specify it is to connect to a Phidget on a remote server, as per the following example.

MaxMSP Remote.png

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

Although this is a required step in many of our other languages (and therefore you may be expecting this if coming from another Phidget language), in MaxMSP you do not have to add a specific waitForAttachment block.

Keep in mind, however, that if your Phidget is not responding within your MaxMSP program, it may simply not be plugged in! Send a getAttached message to a Phidget object at any time to see if it's attached.

Step Three: Do Things with the Phidget

There are two main approaches for retrieving data when working with Phidgets. Data is accessed either by one-time polling or at a fixed rate via on-board timers for some devices.

Getting or setting values directly via polling on the Phidget is done through messages linked to the inlet. The object’s inlet can be wired to send commands to the device, and the outlet used to retrieve the results. Setting values on the Phidget is achieved by using the set messages, and some properties can be read with get messages:

MaxMSP get and set.png


To sample at a fixed rate, use the start and stop messages to start and stop the data flow, respectively.

MaxMSP Start Stop.png

Data from the outlet of a Phidget object is always given a prefix to allow for routing. For instance, the digital input state state change event data is given the prefix “stateChange”, and the voltage input voltage change event similarly use “voltageChange”. The specific prefixes used for each set of outlet data is listed under the API page for their class in the Phidget22 API.

MaxMSP Routing.png


The rightmost outlet on the Phidget object outputs error event data. This will send information on error events such as saturation events. To see which error events may apply to your device, check its API page in the Phidget22 API.

MaxMSP Error Event.png


Step Four: Close and Delete

Although this is a required step in many of our other languages (and therefore you may be expecting this if coming from another Phidget language), in MaxMSP you do not have to add a specific close and delete block.

Special Case: Multiple Phidgets

Multiple Phidgets of the same type can easily be used inside a single program, it only requires another Phidget object placed. If two of the same type of Phidget object are placed, the serial number and channel arguments should always be specified (as well as hub port, if applicable) to ensure that the correct Phidget gets associated with the correct object.

Further Reading

Phidget Programming Basics - Here you can find the basic concepts to help you get started with making your own programs that use Phidgets.

Data Interval/Change Trigger - Learn about these two properties that control how much data comes in from your sensors.

Using Multiple Phidgets - It can be difficult to figure out how to use more than one Phidget in your program. This page will guide you through the steps.

Polling vs. Events - Your program can gather data in either a polling-driven or event-driven manner. Learn the difference to determine which is best for your application.

Logging, Exceptions, and Errors - Learn about all the tools you can use to debug your program.

Phidget Network Server - Phidgets can be controlled and communicated with over your network- either wirelessly or over ethernet.