from Phidget22.Devices.RFID import *
from Phidget22.Devices.DigitalOutput import *
import time

# Dictionary for protocol
protocols = {
    1 : "EM4100",
    2 : "ISO11785 FDX B",
    3 : "PhidgetTag"
}

# Tag present Handler
def onTag(self, tag, protocol):
    print("Tag: {} Using protocal {} detected!".format(str(tag), str(protocols.get(protocol))))
    # Enable on board LED
    led.setState(True)

# Tag Lost Handler
def onTagLost(self, tag, protocol):
    print("Tag: {} Using protocal {} out of range!".format(str(tag), str(protocols.get(protocol))))
    # Disable on board LED
    led.setState(False)


# Init Object
rfid = RFID()
led = DigitalOutput()

# Add tag handler
rfid.setOnTagHandler(onTag)
# Add tag removed handler
rfid.setOnTagLostHandler(onTagLost)

# Set RFID Channel and open Phidget RFID with 1000ms timeout
rfid.setChannel(0)
rfid.openWaitForAttachment(1000)

# Set LED Channel and open
led.setChannel(2)
led.openWaitForAttachment(1000)

# Enable Antenna
rfid.setAntennaEnabled(True)

# Loop forever
while True:
    time.sleep(1)


