try to implement code for rfid
This commit is contained in:
47
project/rfid/rfid_reader.cpp
Normal file
47
project/rfid/rfid_reader.cpp
Normal file
@@ -0,0 +1,47 @@
|
|||||||
|
#include "rfid_reader.hpp"
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <iostream>
|
||||||
|
#include <chrono>
|
||||||
|
#include <thread>
|
||||||
|
#include <ctime>
|
||||||
|
|
||||||
|
rfid_reader::rfid_reader()
|
||||||
|
{
|
||||||
|
mfrc.PCD_Init();
|
||||||
|
loop();
|
||||||
|
std::this_thread::sleep_for(1s);
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t rfid_reader::last_id() const
|
||||||
|
{
|
||||||
|
return uid;
|
||||||
|
}
|
||||||
|
|
||||||
|
void rfid_reader::loop()
|
||||||
|
{
|
||||||
|
while(true)
|
||||||
|
{
|
||||||
|
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
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
24
project/rfid/rfid_reader.hpp
Normal file
24
project/rfid/rfid_reader.hpp
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
#ifndef RFIDREADER_HPP_
|
||||||
|
#define RFIDREADER_HPP_
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
|
||||||
|
#include "MFRC522.h"
|
||||||
|
|
||||||
|
class rfid_reader
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
uint32_t last_id() const;
|
||||||
|
rfid_reader();
|
||||||
|
rfid_reader(const rfid_reader &) = delete;
|
||||||
|
rfid_reader(const rfid_reader &&) = delete;
|
||||||
|
~rfid_reader() = default;
|
||||||
|
void loop();
|
||||||
|
private:
|
||||||
|
MFRC522 mfrc;
|
||||||
|
uint32_t uid;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
||||||
Reference in New Issue
Block a user