55 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;
Serial.print(this->rfid);
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;
}