Friday, October 9, 2015

FBI Exit Problem Fixed

I finished the first prototype for the MMS Photo-inator Xsi a few months back.  It will take a picture when someone presses the button.  Then, it displays the picture on the screen for a few seconds.  Next, it flashes the lights on the green and red buttons and waits for the person to press one.  If the person presses the red button, the program deletes the picture and resets.  If the person presses the green button, the program keeps the picture and resets.

One thing that I need the Photo-inator to do is stop displaying the picture if the person presses either the red or green button.  I have not been able to figure that out, until now.  

I tried sending a keystroke to the session but that did not work.  I also tried killing the process but that did not work.  I tried other software and solutions but could not find one.  I spent the last few months trying to figure this out.  It has been extremely frustrating.  Here is how I finally resolved the problem.

I heard that the uinput module for python will allow me to send keystrokes to a session.  I downloaded the source code from here:

Before I could install the source code, I had to install a supporting program by typing
sudo apt-get install

Next, I installed the source code by running
sudo python buildsudo python install
I could not get fbi to work in the python program.  Tried it from the terminal and it will not work either.  I keep getting an error message that says, "ioctl VT_GETSTATE: Inappropriate ioctl for device (not a linux console?)"

Used Ctrl-Alt-F1 to switch to tty1 .  Executing fbi on tty1 works perfectly.

If I have the python program execute this: "fbi -a -T 1 -t 1 BBB.jpeg", fbi will display the image.  The -T 1 tells fbi to display the program on tty1.  I need to specify this on my PC but it apparently did that by default when I ran the program on the Raspberry Pi. 

Next, I wrote a small python program to display a picture with fbi, wait a few seconds, then send a q to the uinput subsystem on Linux to simulate a user pressing the letter q.  This worked perfectly.

The test code looked like this:

import subprocessimport psutilimport timeimport uinput"fbi -a -T 1 -1 /home/ken/Pictures/BBB.jpeg", shell=True)time.sleep(3)
#Uinput testsevents = (    uinput.KEY_Q,    uinput.KEY_H,    uinput.KEY_L,    uinput.KEY_O,    )

device = uinput.Device(events)time.sleep(1)
Finally, I modified the photo-inator program to use the uinput code.  I need to fire up the whole system to test it out.  I'll do that some other day.