Henrik Gill
Posts: 112
Joined: Sat Feb 24, 2024 3:23 pm

Re: How do I fix this Python code?

Wed Feb 28, 2024 8:26 pm

The commands are being reported. I think the "Stop" command just isn't being sent when the code is stopped. What should I try?

Henrik Gill
Posts: 112
Joined: Sat Feb 24, 2024 3:23 pm

Re: How do I fix this Python code?

Wed Feb 28, 2024 8:29 pm

memjr wrote:
Wed Feb 28, 2024 1:52 pm
Henrik Gill wrote:
Wed Feb 28, 2024 1:19 pm
The run is in the python editor, Mu. I pressed Run at the top of the app to run the code and stop to stop running it. The buttons aren't external they are built in the app.
That's because you're not sending the "stop" command before your program exits.

The stop button interrupts your program. It does not send the stop command to stop the recording.
Then what can I do to send the command once the code is stopped?

ame
Posts: 8944
Joined: Sat Aug 18, 2012 1:21 am
Location: New Zealand

Re: How do I fix this Python code?

Wed Feb 28, 2024 8:35 pm

Henrik Gill wrote:
Wed Feb 28, 2024 8:29 pm
memjr wrote:
Wed Feb 28, 2024 1:52 pm
Henrik Gill wrote:
Wed Feb 28, 2024 1:19 pm
The run is in the python editor, Mu. I pressed Run at the top of the app to run the code and stop to stop running it. The buttons aren't external they are built in the app.
That's because you're not sending the "stop" command before your program exits.

The stop button interrupts your program. It does not send the stop command to stop the recording.
Then what can I do to send the command once the code is stopped?
Well, you could run your code natively, instead of from the Mu editor. Then you'd have to hit Ctrl-C to stop it, which would interrupt the waiting loop, drop out, send the stop command, then exit.
Oh no, not again.

Henrik Gill
Posts: 112
Joined: Sat Feb 24, 2024 3:23 pm

Re: How do I fix this Python code?

Thu Feb 29, 2024 1:03 pm

No, the point is when you stop the code it stops the recording. Is there a command or something like sys.exit() I accomplish this?

memjr
Posts: 3823
Joined: Fri Aug 21, 2020 5:59 pm

Re: How do I fix this Python code?

Thu Feb 29, 2024 4:10 pm

Henrik Gill wrote:
Thu Feb 29, 2024 1:03 pm
No, the point is when you stop the code it stops the recording. Is there a command or something like sys.exit() I accomplish this?
Your editor is interrupting the code. If the code is interrupted, brute force style, your code will never be able to do anything after it is killed.

You need to tell you code to send the stop message ONLY THEN click the button in the editor to stop your code.

If you run your code from the terminal, you can add logic to it so that if hit something like CTRL+C is hit it will do things (like clean up and send the stop message) first and only then exit.

The alternative is to start leaning about threading and multiprocess communications and the like so that if one piece of your code stops communicating with another then it knows something is wrong and it should stop too.

Your code as it is and running it the way you are now, is not going to work the way you want it to.

User avatar
scruss
Posts: 5825
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON

Re: How do I fix this Python code?

Thu Feb 29, 2024 4:53 pm

Henrik Gill wrote:
Thu Feb 29, 2024 1:03 pm
No, the point is when you stop the code it stops the recording. Is there a command or something like sys.exit() I accomplish this?
There's atexit. You might want to try putting something like this near the top of your code, but after where you define send_command():

Code: Select all

import atexit

def stop_recording():
    send_command("Stop")
    
atexit.register(stop_recording)
# ... the rest of your code follows
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.
Pronouns: he/him

memjr
Posts: 3823
Joined: Fri Aug 21, 2020 5:59 pm

Re: How do I fix this Python code?

Thu Feb 29, 2024 6:33 pm

scruss wrote:
Thu Feb 29, 2024 4:53 pm
Henrik Gill wrote:
Thu Feb 29, 2024 1:03 pm
No, the point is when you stop the code it stops the recording. Is there a command or something like sys.exit() I accomplish this?
There's atexit. You might want to try putting something like this near the top of your code, but after where you define send_command():

Code: Select all

import atexit

def stop_recording():
    send_command("Stop")
    
atexit.register(stop_recording)
# ... the rest of your code follows
Interesting.

I'll have to read up the docs on this.

Henrik Gill
Posts: 112
Joined: Sat Feb 24, 2024 3:23 pm

Re: How do I fix this Python code?

Fri Mar 01, 2024 1:10 pm

Any way to automate this? I ran it and I needed to press enter for it to stop everything. Is there a way to run it when code stop button is pressed?

memjr
Posts: 3823
Joined: Fri Aug 21, 2020 5:59 pm

Re: How do I fix this Python code?

Fri Mar 01, 2024 3:02 pm

Henrik Gill wrote:
Fri Mar 01, 2024 1:10 pm
Any way to automate this? I ran it and I needed to press enter for it to stop everything. Is there a way to run it when code stop button is pressed?
Have you read the docs for that module yet to see if it has that option to not prompt like that? I have not. If that's an option it will be documented in there.

Though I am thinking this calls sys.exit() and that is causing Mu to ask if you really want to exit the interpreter. If that's the case, you're back at square one.

ame
Posts: 8944
Joined: Sat Aug 18, 2012 1:21 am
Location: New Zealand

Re: How do I fix this Python code?

Fri Mar 01, 2024 3:46 pm

Henrik Gill wrote:
Fri Mar 01, 2024 1:10 pm
Any way to automate this? I ran it and I needed to press enter for it to stop everything. Is there a way to run it when code stop button is pressed?
Well, you could run your code natively, instead of from the Mu editor. Then you'd have to hit Ctrl-C to stop it, which would interrupt the waiting loop, drop out, send the stop command, then exit.
Oh no, not again.

memjr
Posts: 3823
Joined: Fri Aug 21, 2020 5:59 pm

Re: How do I fix this Python code?

Fri Mar 01, 2024 3:58 pm

ame wrote:
Fri Mar 01, 2024 3:46 pm
Henrik Gill wrote:
Fri Mar 01, 2024 1:10 pm
Any way to automate this? I ran it and I needed to press enter for it to stop everything. Is there a way to run it when code stop button is pressed?
Well, you could run your code natively, instead of from the Mu editor. Then you'd have to hit Ctrl-C to stop it, which would interrupt the waiting loop, drop out, send the stop command, then exit.
Already suggested and ignored. Also, OP's "what now?" followed by another "what now?" approach has turned me off, feels like he's not doing any homework and he expects we'll end up coding the entire thing for him. I've decided to just sit back and watch from now and see where this goes.

Henrik Gill
Posts: 112
Joined: Sat Feb 24, 2024 3:23 pm

Re: How do I fix this Python code?

Fri Mar 01, 2024 8:29 pm

If this helps, the reason I am doing this is when I add a button the code will be ready to add. I want the Pi to record when the button is held down and stop recording when the code is let go. Maybe I could change the code for this task?

hippy
Posts: 16111
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: How do I fix this Python code?

Fri Mar 01, 2024 9:31 pm

Henrik Gill wrote:
Fri Mar 01, 2024 8:29 pm
I want the Pi to record when the button is held down and stop recording when the code is let go. Maybe I could change the code for this task?
Adding code to present an on-screen button, handling pressed and released activations, sending your commands when those are seen, would be the proper and best way to do it because the Mu Editor itself won't allow you to prototype your code how you would like to.

Henrik Gill
Posts: 112
Joined: Sat Feb 24, 2024 3:23 pm

Re: How do I fix this Python code?

Sat Mar 02, 2024 2:38 pm

The "button" is a physical button not a button on screen.

memjr
Posts: 3823
Joined: Fri Aug 21, 2020 5:59 pm

Re: How do I fix this Python code?

Sun Mar 03, 2024 5:32 am

Henrik Gill wrote:
Sat Mar 02, 2024 2:38 pm
The "button" is a physical button not a button on screen.
Same thing as far as code goes.

Henrik Gill
Posts: 112
Joined: Sat Feb 24, 2024 3:23 pm

Re: How do I fix this Python code?

Sun Mar 03, 2024 1:53 pm

Oh, okay but would I change the code at all to make it work for a button?

ame
Posts: 8944
Joined: Sat Aug 18, 2012 1:21 am
Location: New Zealand

Re: How do I fix this Python code?

Sun Mar 03, 2024 2:31 pm

Henrik Gill wrote:
Sun Mar 03, 2024 1:53 pm
Oh, okay but would I change the code at all to make it work for a button?
Probably. I see you have created another thread to ask about that.
Oh no, not again.

Henrik Gill
Posts: 112
Joined: Sat Feb 24, 2024 3:23 pm

Re: How do I fix this Python code?

Mon Mar 04, 2024 11:25 pm

I watched this tutorial by Sam Westby Tech that was VERY helpful. (I recommend you watch it. Title: Controlling Your Raspberry Pi With a BUTTON) I found out what you had been saying I should do and what I had been doing wrong: using Mu instead of GNU nano 5.4. Mu is for overall python coding whereas GNU nano is more for command-line coding, which is what I need. I will try the methods you posted and let you know if any of them worked. So far, as you said, when you press Ctrl+C it stops the code. I will update you on the results.

ame
Posts: 8944
Joined: Sat Aug 18, 2012 1:21 am
Location: New Zealand

Re: How do I fix this Python code?

Mon Mar 04, 2024 11:43 pm

Henrik Gill wrote:
Mon Mar 04, 2024 11:25 pm
I watched this tutorial by Sam Westby Tech that was VERY helpful. (I recommend you watch it. Title: Controlling Your Raspberry Pi With a BUTTON) I found out what you had been saying I should do and what I had been doing wrong: using Mu instead of GNU nano 5.4. Mu is for overall python coding whereas GNU nano is more for command-line coding, which is what I need. I will try the methods you posted and let you know if any of them worked. So far, as you said, when you press Ctrl+C it stops the code. I will update you on the results.
Do you mean that you are running your code natively, instead of from the Mu editor? What a great idea!
Oh no, not again.

Henrik Gill
Posts: 112
Joined: Sat Feb 24, 2024 3:23 pm

Re: How do I fix this Python code?

Wed Mar 06, 2024 5:34 pm

Yeah I am running code natively instead of running from Mu.

Return to “Python”