57 lines
1.2 KiB
C++

#include "Rfid.h"
#define SS_PIN D8
#define RST_PIN D1
#define RFID_TIMEOUT 3000
Rfid::Rfid(/* args */) : _mfrc522(SS_PIN)
{
}
Rfid::~Rfid()
{
}
void Rfid::begin()
{
SPI.begin();
SPI.setClockDivider(SPI_CLOCK_DIV8);
this->_mfrc522.PCD_Init();
this->_mfrc522.PCD_SetAntennaGain(_mfrc522.RxGain_max);
}
void Rfid::scan()
{
if (this->_mfrc522.PICC_IsNewCardPresent() && this->_mfrc522.PICC_ReadCardSerial())
{
this->_rfid = "";
for (byte i = 0; i < this->_mfrc522.uid.size; i++)
{
this->_rfid += this->_mfrc522.uid.uidByte[i] < 0x10 ? "0" : "";
this->_rfid += String(this->_mfrc522.uid.uidByte[i], HEX);
}
this->_rfid.trim();
this->_rfid.toUpperCase();
if (this->_rfid != this->_lastRfid || millis() > this->_lastRfidScan + RFID_TIMEOUT)
{
_status = 1;
#ifdef DEBUG
Serial.print(this->_rfid);
#endif
this->_lastRfid = this->_rfid;
this->_lastRfidScan = millis();
}
}
}
bool Rfid::available()
{
if (this->_status==1)
{
this->_status = 0;
return true;
}
else
return false;
}
String Rfid::getID()
{
return this->_rfid;
}