Jun-18-2022, 09:46 PM
What I'm attempting to do is have a python script running in the background at all times that only takes input from one of the usb devices plugged into the Raspberry Pi, in this case, it's a barcode scanner. What I've gotten so far works really well, but here's what's missing. I don't want that input to "show up" anywhere. I want the script to handle it and then make it go away.
Here's what I've got, in case anyone's interested:
Any help is appreciated in advance!
Here's what I've got, in case anyone's interested:
Any help is appreciated in advance!
import evdev from evdev import ecodes import RPi.GPIO as GPIO import time from datetime import datetime SCANCODES = { # Scancode: ASCIICode 0: None, 1: u'ESC', 2: u'1', 3: u'2', 4: u'3', 5: u'4', 6: u'5', 7: u'6', 8: u'7', 9: u'8', 10: u'9', 11: u'0', 12: u'-', 13: u'=', 14: u'BKSP', 15: u'TAB', 16: u'q', 17: u'w', 18: u'e', 19: u'r', 20: u't', 21: u'y', 22: u'u', 23: u'i', 24: u'o', 25: u'p', 26: u'[', 27: u']', 28: u'CRLF', 29: u'LCTRL', 30: u'a', 31: u's', 32: u'd', 33: u'f', 34: u'g', 35: u'h', 36: u'j', 37: u'k', 38: u'l', 39: u';', 40: u'"', 41: u'`', 42: u'LSHFT', 43: u'\\', 44: u'z', 45: u'x', 46: u'c', 47: u'v', 48: u'b', 49: u'n', 50: u'm', 51: u',', 52: u'.', 53: u'/', 54: u'RSHFT', 56: u'LALT', 57: u' ', 100: u'RALT' } in1 = 16 in2 = 18 GPIO.setmode(GPIO.BOARD) GPIO.setwarnings(False) GPIO.setup(in1, GPIO.OUT) GPIO.setup(in2, GPIO.OUT) GPIO.output(in1, True) GPIO.output(in2, True) mem_num = "" valid_card = False mem_file = open("/home/verhovay/Programs/members.txt", "r") mem_list = mem_file.readlines() mem_file.close() def read_keys(device): obj = evdev.InputDevice(device) for x in obj.read_loop(): if x.code in SCANCODES and x.type == ecodes.EV_KEY and x.value == 1: yield SCANCODES[x.code] def inputesque(what): s = [] for x in what: if x == 'CRLF': break s.append(x) return "".join(s) def LogEntry( mNum, Valid ): today = datetime.today() now = datetime.now() dateCap = today.strftime("%Y-%m-%d") timeCap = now.strftime("%H:%M:%S") targetFolder = "/home/verhovay/Programs/Logs/" + dateCap + ".txt" target = open(targetFolder, 'a') if Valid: target.write(timeCap + " => " + mNum + " => Valid \n") else: target.write(timeCap + " => " + mNum + " => Invalid \n") target.close() return while True: try: mem_num = inputesque(read_keys("/dev/input/by-id/usb-SM_SM-2D_PRODUCT_HID_KBW_APP-000000000-event-kbd")) if mem_num == "1": break valid_card = mem_num.startswith("LSHFTvLSHFThLSHFTa100") or mem_num.startswith("LSHFTvLSHFThLSHFTa200") mem_num = str(mem_num.replace("LSHFTvLSHFThLSHFTa100", "")) mem_num = str(mem_num.replace("LSHFTvLSHFThLSHFTa200", "")) if (mem_num + "\n") in mem_list and valid_card: LogEntry (mem_num, True); GPIO.output(in1, False) GPIO.output(in2, False) time.sleep(.5) GPIO.output(in1, True) GPIO.output(in2, True) else: LogEntry (mem_num, False); print("Not Valid") except Exception as err: now = datetime.now() dt_string = now.strftime("%d/%m/%Y %H:%M:%S") print (err, "***", dt_string)