forked from cyrusbuilt/HidProxWeigand
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HidProxWeigand.h
189 lines (166 loc) · 5.34 KB
/
HidProxWeigand.h
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/**
* HidProxWeigand.h
* Version 1.0b
* Author
* Cyrus Brunner
* Joseph Selby
*
* This library provides a means of reading RFID cards (fobs) in both HID 35bit
* Corporate 1000 format, 32bit Wiegand and 26bit Wiegand format.
*/
#ifndef HidProxWeigand_h
#define HidProxWeigand_h
#include <Arduino.h>
#define MAX_READ_BITS 100 // Max number of read bits.
#define WEIGAND_WAIT_TIME 3000 // Time to wait for another Wiegand pulse.
#define CARD_FORMAT_CORPORATE_1000 35 // HID Corporate 100 card format (35bit).
#define CARD_FORMAT_WEIGAND_26 26 // Standard 26bit Wiegand format.
#define CARD_FORMAT_WEIGAND_32 32 // Standard 32bit Wiegand format.
#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
// Mega 1280 and 2560 has 6 interrupts, thus can support 3 readers.
#define SUPPORTED_READERS 3
#elif defined(__AVR_ATmega168__) ||defined(__AVR_ATmega168P__) ||defined(__AVR_ATmega328P__)
// These (primarily Uno) only have 2 interrupts, thus can support 1 reader.
#define SUPPORTED_READERS 1
#elif defined(__AVR_ATmega32U4__)
// Teensy, Micro, Leonardo, etc, has 5 interrupts, thus can support 2 readers.
#define SUPPORTED_READERS 2
#else
// Here we assume we are running on some other board where any input pin
// can be used as an interrupt (i.e. Due, Zero, MKR1000, 101, etc) and
// therefore can attach as many readers as possible.
#define SUPPORTED_READERS 0
#endif
/**
* Helper method for attaching interrupt handlers for the two data lines the
* reader uses to transmit data pulses.
* @param int0 Interrupt for DATA 0.
* @param int1 Interrupt for DATA 1.
* @param int0Handler Callback method for handling interrupt for DATA 0.
* @param int1Handler Callback method for handling interrupt for DATA 1.
*/
void HidProxWeigand_AttachReaderInterrupts(uint8_t int0, uint8_t int1, void (*int0Handler)(), void (*int1Handler)());
/**
* Proximity RFID info structure. This carries the necessary info needed for
* each reader attached to the system and callback methods for updating the
* necessary values.
*/
struct ProxReaderInfo {
/**
* The pin for data line 0.
*/
short pinData0;
/**
* The pin for data line 1.
*/
short pinData1;
/**
* Flag to indicate the read is done.
*/
bool flagDone;
/**
* Flag to indicate that the card read is of an unsupported format.
*/
bool cardUnsupported;
/**
* The facility code. Will be zero unless a valid code is read.
*/
unsigned long facilityCode;
/**
* The card code. Will be zero unless a valid code is read.
*/
unsigned long cardCode;
/**
* The number of bits read.
*/
uint8_t bitCount;
/**
* Countdown until it is assumed there are no more bits to read.
*/
uint16_t weigandCounter;
/**
* Buffer for storing the bits read.
*/
unsigned char databits[MAX_READ_BITS];
/**
* Event callback method that fires when a card is read.
*/
void (*onCardRead)(ProxReaderInfo* reader);
/**
* This should be called by an interrupt handler when DATA0 goes low (0 bit)
*/
void ISR_Data0() {
bitCount++;
flagDone = false;
weigandCounter = WEIGAND_WAIT_TIME;
}
/**
* This should be called by an interrupt handler when DATA1 goes low (1 bit)
*/
void ISR_Data1() {
databits[bitCount] = 1;
bitCount++;
flagDone = false;
weigandCounter = WEIGAND_WAIT_TIME;
}
/**
* Default constructor. Sets default values.
*/
ProxReaderInfo() {
onCardRead = NULL;
flagDone = false;
cardUnsupported = false;
facilityCode = 0;
cardCode = 0;
bitCount = 0;
weigandCounter = 0;
}
};
/**
* HID card reader manager class. Provides facilities for adding card readers
* and processing card reads.
*/
class HidProxWeigandClass {
public:
/**
* Default class constructor.
*/
HidProxWeigandClass();
/**
* Creates a reader instance and adds it to the system.
* @param pinData0 The DATA0 pin from the reader.
* @param pinData1 The DATA1 pin from the reader.
* @param onCardRead Callback method that fires when a card is read.
* @return A reference to a ProxReaderInfo representing the reader
* that was added. Returns NULL if the reader could not be added due to
* platform limitation.
*/
ProxReaderInfo* addReader(short pinData0, short pinData1, void (*onCardRead)(ProxReaderInfo* reader));
/**
* Processing loop. Checks the buffers for each attached reader and checks
* to see if any cards have been read. Should be called in your sketch's
* main program loop() method.
*/
void loop();
/**
* Gets a reference to the currently active reader.
* @return A reference to the currently active reader.
*/
ProxReaderInfo* getCurrentReader();
private:
short _readerCount;
short _mallocSize;
short _index;
uint8_t _initialCapacity;
ProxReaderInfo* _readers;
ProxReaderInfo* _currentReader;
/**
* Sets the position in the reader array. This essentially selects the
* reader to process.
* @param position The position to set.
*/
void setPosition(short position);
};
// Global instance.
extern HidProxWeigandClass HidProxWeigand;
#endif