1

I am starting with python, and I am trying to understand the sample code that Phidget website give to me (I bought a Phidget Bridge 4 input where I have plug on 4 gauge cell). For my project I need to use python but not much use to it.

To read only one channel, sol only one gauge cell with my Phidget bridge, the website give me this code.

from Phidget22.Phidget import *
from Phidget22.Devices.VoltageRatioInput import *
import time

def onVoltageRatioChange(self, voltageRatio):
    print("VoltageRatio: " + str(voltageRatio))

def main():
    voltageRatioInput0 = VoltageRatioInput()

    voltageRatioInput0.setOnVoltageRatioChangeHandler(onVoltageRatioChange)

    voltageRatioInput0.openWaitForAttachment(5000)

    time.sleep(5)

    voltageRatioInput0.close()

main()

There is the function def onVoltageRatioChange(self, voltageRatio): which takes 2 arguments, self and voltageRatio, and this function is used inside setOnVoltageRatioChangeHandler(onVoltageRatioChange) with no argument. What I mean, it's that I do not get why we give as an argument onVoltageChange (which is normally a function) to the function setOnVoltageRatioChangeHandler...

It someone could explain it to me, it could help me to build my own code... at least try.. thank you

2
  • 1
    Don't call us. We'll call you. (As soon as the voltage ratio changes, we'll call the function you gave us.) Commented Jan 14, 2020 at 15:30
  • 1
    setOnVoltageRatioChangeHandler give args to onVoltageRatioChange inside ... Commented Jan 14, 2020 at 15:34

1 Answer 1

2

Phidget22 knows how to detect a voltage ratio change, but it doesn't know what you want to do with it: maybe you want to sound an alarm. maybe you want to send an email with the new ratio. maybe you just want to print it to the screen.

So it uses setOnVoltageRatioChangeHandler to ask you what you would like to do once it detects this voltage change, saying "hey, if you'll give me a function that takes a voltage ratio, I'll call it when I detect a change" then you can do:

setOnVoltageRatioChangeHandler(soundTheAlarm)

or

setOnVoltageRatioChangeHandler(sendAnEmail)

or:

setOnVoltageRatioChangeHandler(printToScreen)

The supplied onVoltageRatioChange will just print the new value to the screen.

Generally, passing a function as an argument is a great way to give flexibility in the way events are handled. It's basically saying "I know how to detect a certain situation or get a certain value. You let me know what you want me to do once I do"

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for this answer !
But nVoltageRatioChange normally need some arguments, we do not give it directly ? maybe setOnVoltageRatioChangeHandler is giving the 2 arguments we need (self and voltageRatio) ? @shayelk
setOnVoltageRatioChangeHandler is a setter. It will store the function you gave it for later use. Then, when a voltage ratio change will be detected, your function (onVoltageRatioChange) will be called with the new ratio as an argument (note that onVoltageRatioChange actually takes one argument. the self argument is the object it belongs to, but that's a story for a different question ;) )

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.