Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
203 views
in Technique[技术] by (71.8m points)

python - How do I get my program to enter into shell in background?

I have a program that opens a door through a RFID scanner. The cards that are acceptable are loaded in a txt file.

My issue is when the script runs, I scan a card and nothing happens. I alt-tab into the code and click inside of the shell while the program is running. It is here when I scan acceptable and non acceptable cards, the video runs and door unlocks as intended with the acceptable cards. Non-acceptable cards do nothing as intended.

How do I fix this so that the image runs unless an acceptable card is scanned to trigger the video and unlock? Here is my code:

#!/usr/bin/env python3
from evdev import InputDevice
from select import select
import RPi.GPIO as GPIO
import time
from time import sleep
import subprocess
from subprocess import run
import datetime
import os
import sys
import re
import pygame
from pygame.locals import *

def main():    

    def check_if_string_in_file(file_name, string_to_search):
        """Check if any line in the file contains given string"""
        with open(file_name,'r') as read_obj:
            for line in read_obj:
                if string_to_search in line:
                    return True
        return False
    
    
    keys = "X^1234567890asdfghjklXXXXXycbnmXXXXXXXXXXXXXXXXXXXXXXX"
    dev = InputDevice('/dev/input/event0')
    
    pygame.init()
    windowSurface = pygame.display.set_mode ((1024, 600), pygame.NOFRAME)
    img = pygame.image.load("/home/pi/Access/New Phone Wallpaper copy.jpg")
    pygame.mouse.set_cursor((8,8),(0,0), (0,0,0,0,0,0,0,0), (0,0,0,0,0,0,0,0))
    windowSurface.blit(img, (0,0))
    pygame.display.flip()
    pygame.event.clear()
    
    while True:
            r,w,x = select([dev], [],[])
            for event in dev.read():
                    if event.type==1 and event.value==1:
                        if event.code==28:
                            rfid_presented = input()
                            print(rfid_presented)
                            e = rfid_presented
                            if check_if_string_in_file("/home/pi/Access/Loaded_Cards.txt", e):
                                videoPath = "/home/pi/Access/18 Test_1.mp4"
                                omx = run(["omxplayer", '--win', '1,1,1024,600', videoPath])
                                GPIO.setmode(GPIO.BCM)
                                GPIO.setwarnings(False)
                                GPIO.setup(26, GPIO.OUT, initial=GPIO.LOW)
                                GPIO.output(26, GPIO.HIGH)
                                time.sleep(0.5)
                                GPIO.output(26, GPIO.LOW)
                                
                            else:              
                                    rfid_presented = ""
                                      
    else:
                    rfid_presented += keys[event.code]
    del e
main()       

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.9k users

...