Reputation: 133
I have the following Python code that has an if
condition that checks to see if current_ident
is the same as last ident
and if so then not print the current_ident
value.
What I would like to do is add a time-out to this check so only check to see if the current and last is the same for 7 seconds after that let it print if it's the same. I'm thinking possible by adding a global variable last_read
which saves the time and then maybe use something like last_read + [timeout] > [current_time]
?
from nfc import ContactlessFrontend
from time import sleep
ident = ''
def connected(tag):
global ident
current_ident = ''.join('{:02x}'.format(ord(c)) for c in tag.identifier)
if current_ident != ident:
print(current_ident)
ident = current_ident
return False
clf = ContactlessFrontend('usb')
while True:
clf.connect(rdwr={'on-connect': connected})
sleep(1)
Upvotes: 0
Views: 211
Reputation: 39364
You should be able to keep a track of time as well as the last ident:
from nfc import ContactlessFrontend
from time import sleep
import time
ident = ''
next_time = None
def connected(tag):
global ident
global next_time
current_ident = ''.join('{:02x}'.format(ord(c)) for c in tag.identifier)
if current_ident != ident:
next_time = time.time() + 7
print(current_ident) # Brand new ident
ident = current_ident
elif time.time() > next_time:
next_time = time.time() + 7
print(current_ident) # Repeated ident allowed
# else: # disallow repeated ident
return False
clf = ContactlessFrontend('usb')
while True:
clf.connect(rdwr={'on-connect': connected})
sleep(1)
Upvotes: 1