forked from birkir/arduino-wiegand
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Wiegand.cpp
145 lines (124 loc) · 2.24 KB
/
Wiegand.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
Wiegand.h - Library for reading RFID Wiegand protocol.
Created by Birkir R Gudjonsson, July 16, 2012.
Released into the public domain.
*/
#include "Arduino.h"
#include "Wiegand.h"
// long Reader
volatile long Reader = 0;
// int ReaderCount
volatile int ReaderCount = 0;
// valid site codes
int _ValidSiteCodes[];
// valid serial numbers
int _ValidSerialNumbers[];
/**
* Wiegand init
*
* @return void
*/
Wiegand::Wiegand(int Data0, int Data1, int ValidSiteCodes[], int ValidSerialNumbers[])
{
// Data pins
_Data0 = Data0;
_Data1 = Data1;
// Set valid numbers
_ValidSiteCodes[] = ValidSiteCodes;
_ValidSerialNumbers[] = ValidSerialNumbers;
}
/**
* Setup
*
* @return void
*/
void Wiegand::Setup()
{
// delay 10ms
delay(10);
// send some stuff to Data0
pinMode(Data0, OUTPUT);
digitalWrite(Data0, HIGH);
digitalWrite(Data0, LOW);
pinMode(Data0, INPUT);
pinMode(Data0, HIGH);
// send some stuff to Data1
pinMode(Data1, OUTPUT);
digitalWrite(Data1, HIGH);
digitalWrite(Data1, LOW);
pinMode(Data1, INPUT);
pinMode(Data1, HIGH);
// delay 10ms
delay(10);
// Reset reader and reader count
Reader = 0;
ReaderCount = 0;
}
/**
* Read from protocol
*
* @return bool
*/
bool Wiegand::Read()
{
if (ReaderCount >=26)
{
int SerialNumber = (Reader >> 1) & 0x3fff;
int SiteCode = (Reader >> 17) & 0x3ff;
Reader = 0;
ReaderCount = 0;
if (Wiegand::IsTagValid(SiteCode, SerialNumber))
{
return true;
}
return false;
}
}
/**
* Check if tag is valid
*
* @param int
* @param int
* @return bool
*/
bool Wiegand::IsTagValid(int SiteCode, int SerialNumber)
{
// set valid flag
bool Valid = false;
// count valids from array
int ValidTagCount = sizeof(_ValidSiteCodes) / sizeof(int);
// loop through array
for (int t = 0; t < ValidTagCount; t++)
{
// check if sitecode and serialnumber is in array
if (_ValidSiteCodes[t] == SiteCode && _ValidSerialNumbers[t] == SerialNumber)
{
// set flag true
Valid = true;
break;
}
}
// return flag
return Valid;
}
/**
* Read One bit
*
* @return void
*/
void Wiegand::ReaderOne(void)
{
ReaderCount++;
Reader = Reader << 1;
Reader |= 1;
}
/**
* Read Zero bit
*
* @return void
*/
void Wiegand::ReaderZero(void)
{
ReaderCount++;
Reader = Reader << 1;
}