Reputation: 1784
I have this panic button, and a script that responds to the button press.
if I send the process to the background, using the command $sudo python3 ./Panicbutton.py &
I get the command line back. when I press the panic button, the script responds, and prints the message corresponding to the number of times the button was pressed. What I want it to do is go back to the background, whilst it waits for more button presses. pressing ctrl+c returns me to the command line, but I don’t want to need to press ctrl+c each time after the button is pressed. is there a way to return the script to the background while it awaits further key presses? How can I send the ctrl+c command to the terminal from the script? I don't want to terminate the process, I just want the command line prompt to return after the message is printed.
I am using Linux, and python3.4. Please assist.
heres the script:
# USB Panic Button interface code
# Copyright 2010 Ken Shirriff
# http://arcfn.com
import usb.core
x = 0
comment0 = """ PanicButton - interface to USB Panic Button This code requires PyUSB."""
class PanicButton:
def __init__(self):
# Device is: ID 1130:0202 Tenx Technology, Inc.
self.dev = usb.core.find(idVendor=0x1130, idProduct=0x0202)
if not self.dev:
raise ValueError("Panic Button not found")
try:
self.dev.detach_kernel_driver(0) # Get rid of hidraw
except Exception as e:
pass # already unregistered
def read(self):
comment1 = """ Read the USB port. Return 1 if pressed and released, 0 otherwise."""
#Magic numbers are from http://search.cpan.org/~bkendi/Device-USB-PanicButton-0.04/lib/Device/USB/PanicButton.pm
return self.dev.ctrl_transfer(bmRequestType=0xA1, bRequest=1, wValue=0x300, data_or_wLength=8, timeout=500)[0]
if __name__ == "__main__":
import time
button = PanicButton()
while 1:
if button.read():
global x
x = x + 1
if x < 5:
print(x)
elif x == 5:
print("Baby just screem if you want some more!")
elif x == 6:
print("Like AH!")
elif x == 7:
print("push it, push it.")
elif x == 8:
print("watch me work it.")
elif x == 9:
print("I'm PERFECT")
else:
x = 0
print("")
time.sleep(.5)
Upvotes: 0
Views: 1075
Reputation: 123460
The script is always in the background, and does not go to the foreground at any point. It just looks that way because the script's output has the purely cosmetic effect of appearing to mess up the prompt.
Instead of pressing Ctrl-C, you can just write ls
and press enter. You'll see that the shell works just fine and that your script is not in the foreground.
Bash can't really redraw the prompt because it doesn't control which other programs write to the screen. Consider finding a less invasive way of reporting button presses such as:
bel
character to make the terminal play a soundtmux
or screen
to have a window or status bar messageUpvotes: 1