Re: [Dorset] Python Function Appears to be Entered Twice

2017-03-09 Thread Ralph Corderoy
Hi Terry,

> On Thursday, 9 March 2017 17:46:45 GMT Ralph Corderoy wrote:
> > > def change_rings_2(channel):
> > > global msg_subdir, chr_subdir, c_player
> > 
> > You can delete that global, just to make the test simpler still.
> 
> I will still need the msg_subdir; it's used in the code.

What
https://docs.python.org/2.7/reference/simple_stmts.html#the-global-statement
is saying is that you only need to point out global variables with
`global' when you want to write to them, not just read them.  And using
`global' is an indication to the reader that you intend to write, so it
isn't used just to point out a variable is global as that misleads.
(The reader can probably recognise a global that's only being read due
to its longer name.)

> > > If I uncomment the time.sleep() line, then it prints two.
> > 
> > Two `Message Played'?  Do they appear roughly together, both after a
> > two-second gap?  If you're not sure, increase the 2.
> 
> Increasing the delay increases the time between the messages being
> played, when leave the time.sleep() commented, there is no delay.

I echo Patrick's question;  that seems inconsistent with there only
being one line printed in the other email.

> > Are the callbacks defined to be edge triggered, so on the switch's
> > transition from off to on?

You didn't mention whether the Python library says what triggers the
callback.  Have you tried moving the switch from off to on and holding
it there against the spring and observe what happens?  Does the callback
run once with one line printed?  Once it's all finished, let the switch
spring back to off;  does it call the callback again?

Cheers, Ralph.

-- 
Next meeting:  Bournemouth, Tuesday, 2017-04-04 20:00
Meets, Mailing list, IRC, LinkedIn, ...  http://dorset.lug.org.uk/
New thread:  mailto:dorset@mailman.lug.org.uk / CHECK IF YOU'RE REPLYING
Reporting bugs well:  http://goo.gl/4Xue / TO THE LIST OR THE AUTHOR


Re: [Dorset] Python Function Appears to be Entered Twice

2017-03-09 Thread Patrick Wigmore
On Thursday, 9 March 2017, at 18:01:43 GMT, Terry Coles wrote:
> > > If I uncomment the time.sleep() line, then it prints two.
> > 
> > Two `Message Played'?  Do they appear roughly together, both
> > after a two-second gap?  If you're not sure, increase the 2.
> 
> Increasing the delay increases the time between the messages
> being played, when leave the time.sleep() commented, there is
> no delay.

In your first email, you said that there was only one 'Message 
Played' text output when time.sleep() is commented.

So, does this mean that sometimes you get one 'Message Played', 
but sometimes you get two with no delay between them? Does it 
depend on whether the line to invoke the player is commented out?


Just trying to think of possiblities: Is there any kind of 
feedback loop occurring through you (the human operator), whereby 
the delay influences the way in which you operate the switch?

Does it make a difference how long you hold down the switch 
before releasing it?


Patrick.

-- 
Next meeting:  Bournemouth, Tuesday, 2017-04-04 20:00
Meets, Mailing list, IRC, LinkedIn, ...  http://dorset.lug.org.uk/
New thread:  mailto:dorset@mailman.lug.org.uk / CHECK IF YOU'RE REPLYING
Reporting bugs well:  http://goo.gl/4Xue / TO THE LIST OR THE AUTHOR


Re: [Dorset] Python Function Appears to be Entered Twice

2017-03-09 Thread Ralph Corderoy
Hi Terry,

> def change_rings_2(channel):
> global msg_subdir, chr_subdir, c_player

You can delete that global, just to make the test simpler still.

> msg_file = 'Change_Rings_On.wav'
> msg_path = os.path.join(msg_subdir, msg_file)
> 
> #subprocess.call(['aplay', '--device=plughw:1,0', msg_path])
> #time.sleep(2)
> 
> print 'Message Played'
...
> If I uncomment the time.sleep() line, then it prints two.

Two `Message Played'?  Do they appear roughly together, both after a
two-second gap?  If you're not sure, increase the 2.

> If I uncomment the line that invokes the player, I get two lots of the
> 'Change_Rings_On.wav' message to the speaker.

In addition to the sleep, or instead of, or doesn't it matter?

There was some debounce option when registering the callback?  Are you
still using that and to a big enough time?  Are the callbacks defined to
be edge triggered, so on the switch's transition from off to on?

Cheers, Ralph.

-- 
Next meeting:  Bournemouth, Tuesday, 2017-04-04 20:00
Meets, Mailing list, IRC, LinkedIn, ...  http://dorset.lug.org.uk/
New thread:  mailto:dorset@mailman.lug.org.uk / CHECK IF YOU'RE REPLYING
Reporting bugs well:  http://goo.gl/4Xue / TO THE LIST OR THE AUTHOR


[Dorset] Python Function Appears to be Entered Twice

2017-03-09 Thread Terry Coles
Hi,

It's me again.  My program is all but fully working and those who were at the 
Meeting on Tuesday may recall me foolishly saying how I didn't have much left 
to do ;-(

One of the few things left is playing the announcements before the Change 
Rings, MP3 Player, etc start.  The functions all work fine until I add this 
code at the beginning.  When I do, the triggered function seems to be executed 
twice.

Here is a cut down version of the change_rings_start() function, which I've 
been using to analyse what is going on:

def change_rings_2(channel):
global msg_subdir, chr_subdir, c_player

msg_file = 'Change_Rings_On.wav'
msg_path = os.path.join(msg_subdir, msg_file)

#subprocess.call(['aplay', '--device=plughw:1,0', msg_path])
#time.sleep(2)

print 'Message Played'

(In the full function, the mpg321 player is invoked using subprocess.Popen() 
to play the bells/music straight after this code.  I have changed the callback 
so that this cut-down function is called instead when I press the switch.)

As written, this code executes as expected and prints a single line 'Message 
Played' to the Python console.

If I uncomment the time.sleep() line, then it prints two.  If I uncomment the 
line that invokes the player, I get two lots of the 'Change_Rings_On.wav' 
message to the speaker.

In the original change_rings_start() function, I get two announcements and the 
change rings start playing, twice.  (The first playback is terminated by a call 
to change_rings_stop() just after the announcement.)

(Please ignore the fact that I am now using aplay instead of mpg321.  I 
couldn't get the message to play at all for some reason and when I did, by 
using aplay, two came along.)

Has anyone any ideas what is going on here?

-- 



Terry Coles

-- 
Next meeting:  Bournemouth, Tuesday, 2017-04-04 20:00
Meets, Mailing list, IRC, LinkedIn, ...  http://dorset.lug.org.uk/
New thread:  mailto:dorset@mailman.lug.org.uk / CHECK IF YOU'RE REPLYING
Reporting bugs well:  http://goo.gl/4Xue / TO THE LIST OR THE AUTHOR