M4TTHI
Posts: 6
Joined: Wed Jul 15, 2020 1:40 pm

Can you create a macro

Wed Jul 29, 2020 9:42 am

Hello guys,
I've made a program that collects data from differents sensors and saves it on a USB stick.
The only way the program can be closed/stopped is by a keyboardInterrupt.

Now to my question:
Is there a way to create a macro that if you press a button (that is connected thorugh the GPIO's or if it's simplier connected with USB) a keyboardInterrupt occurs and the program stop?

Thanks in advance

Kind regards
Matthias

deepo
Posts: 584
Joined: Sun Dec 30, 2018 8:36 pm
Location: Denmark

Re: Can you create a macro

Wed Jul 29, 2020 9:50 am

You could use the kill command to send the keyboard interrupt.

Code: Select all

kill -SIGINT PID
where PID is the process ID of your script, use ps -ef or ps -aux to find the PID.

/Mogens

pcmanbob
Posts: 9610
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Can you create a macro

Wed Jul 29, 2020 9:51 am

You have not said which programming language you are using , but you should be able to add the switch ( button ) to the gpio and read its state as part of you existing program,

when the program sees the state change its a simple matter of running the same commands to stop your program running as you would if you did Ctrl ,C.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

M4TTHI
Posts: 6
Joined: Wed Jul 15, 2020 1:40 pm

Re: Can you create a macro

Wed Jul 29, 2020 11:47 am

Oh sry im running the programm in the python language.

So that means instead of
try:
...
except KeyboardInterrupt:
...

I could simplay use this to stop the programm:

try:
...
except GPIO == True:
...

Or did i just missunderstand what you said?

pcmanbob
Posts: 9610
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: Can you create a macro

Wed Jul 29, 2020 2:43 pm

So I would do it like this, add a gpio event detect and then when it detects a button press it runs the call back, in the call back you can put what ever commands you want , for example gpio.cleanup() then the last line in the callback is sys.exit() which exits python / your program.

so code might look some thins like this

Code: Select all

def program_exit(channel):
    print("program exit called")
    # add any other commands you might want like gpio.cleanup()
    sys.exit()

GPIO.add_event_detect(21, GPIO.RISING, callback=program_exit)  # add rising edge detection on a channel
...the rest of your program...

put the function and the gpio line neat the sart of your program after setting up any gpio.

this give more detail about using event detect and call backs
https://sourceforge.net/p/raspberry-gpi ... ki/Inputs/
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

M4TTHI
Posts: 6
Joined: Wed Jul 15, 2020 1:40 pm

Re: Can you create a macro

Thu Jul 30, 2020 5:45 am

Ok thank you for your help.
I will try to do it like u showed me.

Have a nice day

Return to “General discussion”