-
Notifications
You must be signed in to change notification settings - Fork 0
/
RfidReader.cpp
56 lines (43 loc) · 1.03 KB
/
RfidReader.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
* "Drinks" RFID Terminal
* Buy sodas with your company badge!
*
* Benoit Blanchon 2014 - MIT License
* https://github.com/bblanchon/DrinksRfidTerminal
*/
#include <Arduino.h>
#include "RfidReader.h"
byte RfidReader::parseHexNibble(char c)
{
if (c >= 'A') return c - 'A' + 0xA;
if (c >= 'a') return c - 'a' + 0xA;
return c - '0';
}
byte RfidReader::parseHexByte(char* str)
{
return (parseHexNibble(str[0]) << 4) | parseHexNibble(str[1]);
}
byte RfidReader::computeCheckSum(char badge[])
{
char checkSum = 0;
for (int i = 0; i < 5; i++)
{
checkSum ^= parseHexByte(badge + 2 * i);
}
return checkSum;
}
char* RfidReader::tryRead()
{
int i;
// Look for new cards
if ( ! mfrc522.PICC_IsNewCardPresent())
return NULL;
// Select one of the cards
if ( ! mfrc522.PICC_ReadCardSerial())
return NULL;
// convert UID to string
for(int i=0; i<mfrc522.uid.size; i++) {
sprintf(&buffer[i*2], "%02X", (int)mfrc522.uid.uidByte[i]);
}
return buffer;
}