Using Multiple Phidgets: Difference between revisions
No edit summary |
|||
(7 intermediate revisions by 2 users not shown) | |||
Line 4: | Line 4: | ||
This video explains the process of using multiple Phidgets in your program: | This video explains the process of using multiple Phidgets in your program: | ||
<center>{{#ev:youtube| | <center>{{#ev:youtube|17VQNQlrxxU|||||rel=0}}</center> | ||
==The Basics== | ==The Basics== | ||
Line 112: | Line 112: | ||
PhidgetTemperatureSensor_delete(&ch); | PhidgetTemperatureSensor_delete(&ch); | ||
PhidgetTemperatureSensor_delete(&ch1); | PhidgetTemperatureSensor_delete(&ch1); | ||
</syntaxhighlight> | |||
|-| | |||
JavaScript=<syntaxhighlight lang=javascript> | |||
// Set up the first channel as normal | |||
const ch = new phidget22.TemperatureSensor() | |||
ch.deviceSerialNumber = 12345 | |||
ch.hubPort = 4 | |||
ch.channel = 0 | |||
await ch.open(5000) | |||
// For a second channel, simply repeat the process with different addressing information | |||
const ch1 = new phidget22.TemperatureSensor() | |||
ch1.deviceSerialNumber = 12345 | |||
ch1.hubPort = 3 | |||
ch1.channel = 0 | |||
await ch1.open(5000) | |||
// Do stuff with your Phidgets here... | |||
// Remember to close the channels when done | |||
await ch.close() | |||
await ch1.close() | |||
</syntaxhighlight> | </syntaxhighlight> | ||
</tabber> | </tabber> | ||
Line 126: | Line 148: | ||
for i in range (0, 8): | for i in range (0, 8): | ||
#Address, then open the channels | #Address, then open the channels | ||
ch[i].setChannel(i) | |||
ch[i].openWaitForAttachment(5000) | ch[i].openWaitForAttachment(5000) | ||
Line 146: | Line 169: | ||
ch[i] = new DigitalOutput(); | ch[i] = new DigitalOutput(); | ||
//Address, then open the channels | //Address, then open the channels | ||
ch[i].setChannel(i); | |||
ch[i].open(5000); | ch[i].open(5000); | ||
} | } | ||
Line 173: | Line 197: | ||
ch[i] = new DigitalOutput(); | ch[i] = new DigitalOutput(); | ||
//Address, then open the channels | //Address, then open the channels | ||
ch[i].Channel = i; | |||
ch[i].Open(5000); | ch[i].Open(5000); | ||
} | } | ||
Line 197: | Line 222: | ||
PhidgetDigitalOutput_create(&ch[i]); | PhidgetDigitalOutput_create(&ch[i]); | ||
//Address, then open the channels | //Address, then open the channels | ||
Phidget_setChannel((PhidgetHandle)ch[i], i); | |||
Phidget_openWaitForAttachment((PhidgetHandle)ch[i], 5000); | Phidget_openWaitForAttachment((PhidgetHandle)ch[i], 5000); | ||
} | } | ||
Line 208: | Line 234: | ||
for (int i = 0; i < 8; i++) { | for (int i = 0; i < 8; i++) { | ||
Phidget_close((PhidgetHandle)ch[i]); | Phidget_close((PhidgetHandle)ch[i]); | ||
} | |||
</syntaxhighlight> | |||
|-| | |||
JavaScript=<syntaxhighlight lang=javascript> | |||
// Create the array of Phidget channels | |||
var ch = [] | |||
for(i = 0; i < 8; i++) { | |||
// Create, address, then open the channels | |||
var tmp = new phidget22.DigitalInput() | |||
tmp.channel = i | |||
await tmp.open(5000) | |||
ch.push(tmp) | |||
} | |||
// Now you can access each channel by its position in the array | |||
ch[0].state = True | |||
ch[1].state = False | |||
ch[2].state = False | |||
ch[3].state = True | |||
for(i = 0; i < 8; i++) { | |||
await ch[i].close() | |||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 239: | Line 289: | ||
<syntaxhighlight lang=java> | <syntaxhighlight lang=java> | ||
//Declare the event listener | //Declare the event listener | ||
public static | public static AttachListener onAttach = new AttachListener() { | ||
@Override | @Override | ||
public void onAttach(AttachEvent e) { | public void onAttach(AttachEvent e) { | ||
Line 292: | Line 342: | ||
//Assign the handler that will be called when the event occurs | //Assign the handler that will be called when the event occurs | ||
Phidget_setOnAttachHandler((PhidgetHandle)ch, onAttachHandler, NULL); | Phidget_setOnAttachHandler((PhidgetHandle)ch, onAttachHandler, NULL); | ||
</syntaxhighlight> | |||
|-| | |||
JavaScript= | |||
In JavaScript, you can access the Phidget that fired the event using the 'this' property. | |||
<syntaxhighlight lang=c> | |||
// Declare your object. Replace "DigitalInput" with the object for your Phidget | |||
const ch = new phidget22.DigitalInput() | |||
// Assign the handler that will be called when the event occurs | |||
ch.onAttach = function { | |||
// You can access the Phidget that fired the event using the "this" parameter | |||
let deviceSerial = this.deviceSerialNumber | |||
} | |||
</syntaxhighlight> | </syntaxhighlight> | ||
</tabber> | </tabber> | ||
Line 319: | Line 382: | ||
button.setOnStateChangeHandler(onStateChangeHandler) | button.setOnStateChangeHandler(onStateChangeHandler) | ||
#Be sure to open any channels you are using within events before the channels | |||
#that cause the events. | |||
#This gives them a chance to be attached before the event tries to use them. | |||
output.openWaitForAttachment(5000) | output.openWaitForAttachment(5000) | ||
button.openWaitForAttachment(5000) | button.openWaitForAttachment(5000) | ||
Line 349: | Line 415: | ||
try { | try { | ||
button = new DigitalInput(); | button = new DigitalInput(); | ||
output = new DigitalOutput(); | |||
//Set Any Addressing Parameters Here | //Set Any Addressing Parameters Here | ||
Line 355: | Line 421: | ||
button.addStateChangeListener(onStateChange); | button.addStateChangeListener(onStateChange); | ||
//Be sure to open any channels you are using within events before the channels | |||
//that cause the events. | |||
//This gives them a chance to be attached before the event tries to use them. | |||
output.open(5000); | output.open(5000); | ||
button.open(5000); | button.open(5000); | ||
Line 396: | Line 465: | ||
button.StateChange += onStateChange; | button.StateChange += onStateChange; | ||
//Be sure to open any channels you are using within events before the channels | |||
//that cause the events. | |||
//This gives them a chance to be attached before the event tries to use them. | |||
output.Open(5000); | output.Open(5000); | ||
button.Open(5000); | button.Open(5000); | ||
Line 435: | Line 507: | ||
PhidgetDigitalInput_setOnStateChangeHandler(ch, onStateChangeHandler, output); | PhidgetDigitalInput_setOnStateChangeHandler(ch, onStateChangeHandler, output); | ||
//Be sure to open any channels you are using within events before the channels | |||
//that cause the events. | |||
//This gives them a chance to be attached before the event tries to use them. | |||
Phidget_openWaitForAttachment((PhidgetHandle)output, 5000); | Phidget_openWaitForAttachment((PhidgetHandle)output, 5000); | ||
Phidget_openWaitForAttachment((PhidgetHandle)button, 5000); | Phidget_openWaitForAttachment((PhidgetHandle)button, 5000); | ||
Line 448: | Line 523: | ||
For example, if we wanted to make a Digital Output channel follow the state of a button: | For example, if we wanted to make a Digital Output channel follow the state of a button: | ||
<syntaxhighlight lang=javaScript> | <syntaxhighlight lang=javaScript> | ||
const button = new phidget22.DigitalInput(); | |||
const output = new phidget22.DigitalOutput(); | |||
... | ... | ||
//Here we create an attribute of input called "linkedOutput", and assign it the handle for output | //Here we create an attribute of input called "linkedOutput", and assign it the handle for output | ||
button.linkedOutput = output; | button.linkedOutput = output; | ||
button.onStateChange = | button.onStateChange = function(state){ | ||
//Be sure the other Phidget you are trying to access is attached before using it | |||
if(this.linkedOutput.attached) | |||
this.linkedOutput.state = state; | |||
} | |||
output.open(); | await output.open(); | ||
button.open(); | await button.open(); | ||
//The rest of your code here... | //The rest of your code here... |
Latest revision as of 21:05, 19 April 2022

12 . Using Multiple Phidgets
Chances are your project with Phidgets is going to involve more than one Phidget channel. Luckily, making a program that deals with multiple Phidgets is done in much the same way as making a program that only deals with one.
This video explains the process of using multiple Phidgets in your program:
The Basics
To use more than one Phidget channel in you program, simply repeat the Create, Address, and, Open process for each channel, and remember to Close them all when done.
Addressing Channels
When you are using more than one Phidget channel in your program, you are going to have to specify some addressing parameters to ensure each software channel connects to the right Phidget.
Full descriptions of all the addressing parameters can be found on the Addressing Phidgets page.
Example
For example, to open two Phidgets, the code might be:
#Set up the first channel as normal
ch = TemperatureSensor()
ch.setDeviceSerialNumber(12345)
ch.setHubPort(4)
ch.setChannel(0)
ch.openWaitForAttachment(5000)
#For a second channel, simply repeat the process with different addressing information
ch1 = TemperatureSensor()
ch1.setDeviceSerialNumber(12345)
ch1.setHubPort(3)
ch1.setChannel(0)
ch1.openWaitForAttachment(5000)
#Do stuff with your Phidgets here...
#Remember to close the channels when done
ch.close()
ch1.close()
Similar Phidgets
If you have a large number of the same Phidget channel and want an easier way to keep track of them all, consider using an array to keep them all together.
#Create the array of Phidget channels
ch = [DigitalOutput() for i in range (0, 8)]
for i in range (0, 8):
#Address, then open the channels
ch[i].setChannel(i)
ch[i].openWaitForAttachment(5000)
#Now you can access each channel by its position in the array
ch[0].setState(True)
ch[1].setState(False)
ch[2].setState(False)
ch[3].setState(True)
for i in range (0, 8):
ch[i].close()
Distinguishing Events
When using events, you can either create separate events for each device, or handle multiple devices with the same event (or some combination of both). If multiple devices use the same event handler, you can use the addressing properties of the channel to determine which Phidget channel caused the event.
For example, for an Attach Event handler:
In Python, the channel that fired the event can be accessed from the event handler using the self
parameter (the first parameter in the list).
#Declare the event handler
def onAttachHandler(self):
#You can access the Phidget that fired the event using the "self" parameter
ph = self
deviceSerialNumber = ph.getDeviceSerialNumber()
...
#Declare your object. Replace "DigitalInput" with the object for your Phidget
ch = DigitalInput()
...
#Assign the handler that will be called when the event occurs
ch.setOnAttachHandler(onAttachHandler)
Referencing Other Phidgets from Events
When using multiple Phidgets in the same program, you may want to access one Phidget from the within an event caused by another. There are simple ways of doing this for all languages, though the specifics depend on the programming language you are using:
Python is dynamically interpreted, and objects follow a less rigid structure than in other languages. To access another Phidget from an event handler, you can add the second Phidget's handle as an attribute of the Phidget object that will be triggering the event. Then, you can access the second Phidget using the corresponding attribute from the self
parameter of the event.
For example, if we wanted to make a Digital Output channel follow the state of a button:
def onStateChangeHandler(self, state):
#Be sure the other Phidget you are trying to access is attached before using it
if(self.linkedOutput.getAttached()):
self.linkedOutput.setState(state)
button = DigitalInput()
output = DigitalOutput()
#Addressing info here
#Here we create an attribute of input called "linkedOutput", and assign it the handle for output
button.linkedOutput = output
button.setOnStateChangeHandler(onStateChangeHandler)
#Be sure to open any channels you are using within events before the channels
#that cause the events.
#This gives them a chance to be attached before the event tries to use them.
output.openWaitForAttachment(5000)
button.openWaitForAttachment(5000)
# The rest of your code here....
What's Next?
Now that you know how to use multiple Phidgets in your program, we should discuss how to find the features available to you by using the Phidget22 API.