Exemplo n.º 1
0
class Card(object):


    def __init__(self, loop_callback, complete_callback=None):
        self.stopped = True
        self.loop_callback = loop_callback
        self.complete_callback = complete_callback
        self.rfid = RFID("/dev/ttyUSB0")


    def _start(self, *args, **kwargs):
        self.stopped = False
        cardnumber = None
        while not cardnumber:
            #read until card is found
            cardnumber = self.rfid.read_once()
            if self.stopped:
                thread.exit()
            sleep(0.1)
            gobject.idle_add(self._loop, "No Card found!")
        if self.complete_callback is not None:
            gobject.idle_add(self.complete_callback, cardnumber)
        thread.exit()

            
    def _write(self, cardnumber, dialog = None):
        self.stopped = False
        card_write_result = None
        while(card_write_result != "DONE!"):
            card_write_result = self.rfid.write(cardnumber)
            if self.stopped:
                thread.exit()
            sleep(0.1)

        dialog.destroy()
        thread.exit()


    def _loop(self, ret):
        if ret is None:
            ret = ()
        if not isinstance(ret, tuple):
            ret = (ret,)
        self.loop_callback(*ret)


    #TODO Change name to read
    def start(self, *args, **kwargs):
        threading.Thread(target=self._start, args=args, kwargs=kwargs).start()


    def write(self, cardnumber, dialog = None):
        threading.Thread(target=self._write, args=(cardnumber, dialog)).start()


    def stop(self):
        self.stopped = True
Exemplo n.º 2
0
#!/usr/bin/python
# This Python file uses the following encoding: utf-8
import sys
from lib.rfid import RFID

# number = int(sys.argv[1])
import time

number = 12
rfid = RFID("/dev/ttyUSB1")
current_number = 0

while True:

    time.sleep(0.1)

    try:
        current_number = int(rfid.read_once())
    except:
        pass

    if current_number + 1 != number:
        result = rfid.write(str(number))
        if result == "DONE!":
            print "Successfully writen " + str(number) + " to card"
            number += 1