Reputation: 25
Using a Raspberry Pi (raspbian) I've modified this slideshow script for python that reads out a PIR motion sensor.
In the original, the PIR switches the monitor (HDMI) on after it switches off when a certain amount of time with no motion has passed. I wanted the motion sensor to initiate the display of a new image.
So, whenever the PIR is triggered I call this function:
def new_pic():
sys.stderr.write("\x1b[2J\x1b[H")
myList = []
file = open("dir.txt","rt")
for line in file:
myList.append(line)
file.close()
count = len(myList)
rnd = randint(0,count-1)
current_image = myList[rnd]
current_image = current_image[:-1]
proc = subprocess.Popen(["pgrep", "fbi"], stdout=subprocess.PIPE)
for pid in proc.stdout:
os.kill(int(pid), signal.SIGTERM)
subprocess.call(["fbi", "-noverbose", "-a", "-T", "2", "/home/pi/photoframe/photos/"+current_image])
I use an rsync in the crontab to keep the pool of images updated. After each rsync I write the list of files into 'dir.txt'.
It works, the only problem I have is that each time a new image is loaded the terminal text shows up for a second. I tried making the prompt black by modifying -bashrc and running a 'clear' before launching my python script.
Here's what I'm stumbling over: when fbi is called, it displays
using "DejaVu Sans Mono-16", pixelsize=16.67 file=/usr/share/fonts/truetype/ttf-dejavu/DejaVuSansMono.ttf
I tried adding
> /dev/null
to the end of the fbi command, but it ignores it.
In my previous attempts, I just kept calling new instances of fbi - but they would pile up in the TOP and at some point the Pi would run out of resources. An alternative could be to track the fbi instances and kill them after the next instance has been called...
I've been running around in circles for a few weeks now - any help would be greatly appreciated.
A follow-up:
The first answer did remove the output from fbi (subprocess.call).
Now I'm seeing something... it goes by really fast - flickering white text.
I think it has to do with the fact that this if statement:
if io.input(PIR_PIN):
if time.time() > (last_flip + SWITCH_TIMER):
new_pic()
gets called 8 to 10 times each time the PIR triggered and the sufficient time has passed...
I thought the time-if would only allow the new_pic() to be called once. last_flip is set to the current time after the new pic is displayed by fbi...
How can I ensure that new_pic() only gets called once?
Thanks! Andrew
Upvotes: 0
Views: 606
Reputation: 27423
From the docs for subprocess, subprocess starts another program and has stdin, stdout, and stderr parameters so that the program's I/O can be redirected to other files.
The default is None
, which makes the child inherit the parent's I/O files, which is why it leaks text into your nice interface.
You can open a file, including /dev/null
for a black hole, and send the output there by calling like this:
shutup = open("/dev/null","w")
subprocess.call(["fbi", "-noverbose", "-a", "-T", "2", "/home/pi/photoframe/photos/"+current_image],
stdout=shutup,
stderr=shutup)
On the other hand, you might want to save the output and parse it in python and make sure fbi did what it is expected to do.
Upvotes: 2