54 lines
940 B
C++
54 lines
940 B
C++
#include "rfid_reader.hpp"
|
|
#include <unistd.h>
|
|
#include <iostream>
|
|
#include <chrono>
|
|
#include <ctime>
|
|
|
|
rfid_reader::rfid_reader()
|
|
{
|
|
mfrc.PCD_Init();
|
|
thread = std::thread(&rfid_reader::loop, this);
|
|
}
|
|
|
|
rfid_reader::~rfid_reader()
|
|
{
|
|
stop_thread = true;
|
|
if(thread.joinable()) thread.join();
|
|
}
|
|
|
|
uint32_t rfid_reader::last_id() const
|
|
{
|
|
return uid;
|
|
}
|
|
|
|
void rfid_reader::loop()
|
|
{
|
|
while(!stop_thread)
|
|
{
|
|
if(!mfrc.PICC_IsNewCardPresent())
|
|
{
|
|
continue;
|
|
}
|
|
if(!mfrc.PICC_ReadCardSerial())
|
|
{
|
|
continue;
|
|
}
|
|
|
|
|
|
uid = int((unsigned char)(mfrc.uid.uidByte[0]) << 24 |
|
|
(unsigned char)(mfrc.uid.uidByte[1]) << 16 |
|
|
(unsigned char)(mfrc.uid.uidByte[2]) << 8 |
|
|
(unsigned char)(mfrc.uid.uidByte[3]));
|
|
|
|
#ifndef NDEBUG
|
|
printf("\n");
|
|
std::time_t result = std::time(nullptr);
|
|
std::cout << std::asctime(std::localtime(&result));
|
|
printf("%X\n", last_id());
|
|
#endif
|
|
|
|
std::this_thread::sleep_for(1s);
|
|
|
|
}
|
|
}
|