2

I am using pimylifeup library to read RFId card. But after id, text = reader.read(), I don't get reading from 4×4 keypad. I tried with both BOARD and BCM numbering to read keypad. I have to exit the program after a key press. The program is available on my GitHub repository.

  • Is read() blocking? – user96931 Apr 30 at 20:23
  • 1
    RFID reads perfectly. But at that time keypad never works. I tried with importing Timer from threading. Then keypad reads sometimes. – Arun K S Apr 30 at 20:27
  • Try spawning a new thread to read keypad while RFID is being scanned, then read its data and kill it the moment the RFID is scanned. – user96931 Apr 30 at 20:28
  • Could you please write this one as answer. – Arun K S Apr 30 at 20:35
1

Spawn a new thread to read keypad while RFID is being scanned, then read its data and kill it the moment RFID is done being scanned. In the code below, I'll demonstrate the use of processes.

At the top of your script (new_main.py) add

from multiprocessing import Process, Pipe, Value

then, before line 79 (which shall be line 80 once you insert the above), add a call to a procedure, built to read keypad input while the RFID is being scanned:

parent_conn, child_conn = Pipe() #Data which shall contain keys pressed while the RFID is being read.
v = Value('i', 0) #The amount of keys pressed while RFID is being scanned
p = Process(target=readKeypadWhileRFIDIsScanned, args=(child_conn, v))
p.start()
id, text = reader.read()
keysPressed = []
for i in range(0, v.value):
  keysPressed.append(parent_conn.recv())
p.join() #We don't need it anymore

"""Now, add a loop to process each key stored in the list keysPressed"""

The function readKeypadWhileRFIDIsScanned() would be something like:

def readKeypadWhileRFIDIsScanned(q, v):
  while True:
    key = get_key()

    if key:
      v += 1
      q.send(key)

And I reccomend you indent the code starting from line 7 and encapsulate it in to a main() method, finishing by adding:

if __name__ == '__main__':

    main()

to the end of the script; it's both better format and more readable.

  • I will try this soon. Thank you. – Arun K S May 8 at 18:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.