forked from ugge75/Wiegand-V3-Library-for-all-Arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Wiegand.cpp
485 lines (424 loc) · 12.6 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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
#include "Wiegand.h"
unsigned long WIEGAND::_sysTick=0;
unsigned long WIEGAND::_lastWiegand=0;
int WIEGAND::_GateActive=0; // 1 = Active A -- 2 = Active B ---- 3 = Active C
unsigned long WIEGAND::_cardTempHighA=0;
unsigned long WIEGAND::_cardTempA=0;
unsigned long WIEGAND::_codeA=0;
int WIEGAND::_bitCountA=0;
int WIEGAND::_wiegandTypeA=0;
unsigned long WIEGAND::_cardTempHighB=0;
unsigned long WIEGAND::_cardTempB=0;
unsigned long WIEGAND::_codeB=0;
int WIEGAND::_bitCountB=0;
int WIEGAND::_wiegandTypeB=0;
unsigned long WIEGAND::_cardTempHighC=0;
unsigned long WIEGAND::_cardTempC=0;
unsigned long WIEGAND::_codeC=0;
int WIEGAND::_bitCountC=0;
int WIEGAND::_wiegandTypeC=0;
WIEGAND::WIEGAND()
{
}
unsigned long WIEGAND::getCode()
{
switch (_GateActive) {
case 1:
return _codeA;
break;
case 2:
return _codeB;
break;
case 3:
return _codeC;
break;
default:
return 0;
}
}
int WIEGAND::getWiegandType()
{
switch (_GateActive) {
case 1:
return _wiegandTypeA;
break;
case 2:
return _wiegandTypeB;
break;
case 3:
return _wiegandTypeC;
break;
default:
return 0;
}
}
int WIEGAND::getGateActive()
{
return _GateActive;
}
bool WIEGAND::available()
{
return DoWiegandConversion();
}
void WIEGAND::begin(bool GateA, bool GateB, bool GateC)
{
_sysTick=millis();
_lastWiegand = 0;
if (GateA == 1 )
{
_cardTempHighA = 0;
_cardTempA = 0;
_codeA = 0;
_wiegandTypeA = 0;
_bitCountA = 0;
pinMode(D0PinA, INPUT); // Set D0 pin as input
pinMode(D1PinA, INPUT); // Set D1 pin as input
attachInterrupt(digitalPinToInterrupt(D0PinA), ReadD0A, FALLING); // Hardware interrupt - high to low pulse
attachInterrupt(digitalPinToInterrupt(D1PinA), ReadD1A, FALLING); // Hardware interrupt - high to low pulse
}
if (GateB == 1 )
{
_cardTempHighB = 0;
_cardTempB = 0;
_codeB = 0;
_wiegandTypeB = 0;
_bitCountB = 0;
pinMode(D0PinB, INPUT); // Set D0 pin as input
pinMode(D1PinB, INPUT); // Set D1 pin as input
attachInterrupt(digitalPinToInterrupt(D0PinB), ReadD0B, FALLING); // Hardware interrupt - high to low pulse
attachInterrupt(digitalPinToInterrupt(D1PinB), ReadD1B, FALLING); // Hardware interrupt - high to low pulse
}
if (GateC == 1 )
{
_cardTempHighC = 0;
_cardTempC = 0;
_codeC = 0;
_wiegandTypeC = 0;
_bitCountC = 0;
pinMode(D0PinC, INPUT); // Set D0 pin as input
pinMode(D1PinC, INPUT); // Set D1 pin as input
attachInterrupt(digitalPinToInterrupt(D0PinC), ReadD0C, FALLING); // Hardware interrupt - high to low pulse
attachInterrupt(digitalPinToInterrupt(D1PinC), ReadD1C, FALLING); // Hardware interrupt - high to low pulse
}
Serial.print("GateA Enabled = ");
Serial.println(GateA);
Serial.print("GateB Enabled = ");
Serial.println(GateB);
Serial.print("GateC Enabled = ");
Serial.println(GateC);
}
void WIEGAND::ReadD0A ()
{
_bitCountA++; // Increament bit count for Interrupt connected to D0
if (_bitCountA>31) // If bit count more than 31, process high bits
{
_cardTempHighA |= ((0x80000000 & _cardTempA)>>31); // shift value to high bits
_cardTempHighA <<= 1;
_cardTempA <<=1;
}
else
{
_cardTempA <<= 1; // D0 represent binary 0, so just left shift card data
}
_lastWiegand = _sysTick; // Keep track of last wiegand bit received
}
void WIEGAND::ReadD1A()
{
_bitCountA ++; // Increment bit count for Interrupt connected to D1
if (_bitCountA>31) // If bit count more than 31, process high bits
{
_cardTempHighA |= ((0x80000000 & _cardTempA)>>31); // shift value to high bits
_cardTempHighA <<= 1;
_cardTempA |= 1;
_cardTempA <<=1;
}
else
{
_cardTempA |= 1; // D1 represent binary 1, so OR card data with 1 then
_cardTempA <<= 1; // left shift card data
}
_lastWiegand = _sysTick; // Keep track of last wiegand bit received
}
void WIEGAND::ReadD0B ()
{
_bitCountB++; // Increament bit count for Interrupt connected to D0
if (_bitCountB>31) // If bit count more than 31, process high bits
{
_cardTempHighB |= ((0x80000000 & _cardTempB)>>31); // shift value to high bits
_cardTempHighB <<= 1;
_cardTempB <<=1;
}
else
{
_cardTempB <<= 1; // D0 represent binary 0, so just left shift card data
}
_lastWiegand = _sysTick; // Keep track of last wiegand bit received
}
void WIEGAND::ReadD1B()
{
_bitCountB ++; // Increment bit count for Interrupt connected to D1
if (_bitCountB>31) // If bit count more than 31, process high bits
{
_cardTempHighB |= ((0x80000000 & _cardTempB)>>31); // shift value to high bits
_cardTempHighB <<= 1;
_cardTempB |= 1;
_cardTempB <<=1;
}
else
{
_cardTempB |= 1; // D1 represent binary 1, so OR card data with 1 then
_cardTempB <<= 1; // left shift card data
}
_lastWiegand = _sysTick; // Keep track of last wiegand bit received
}
void WIEGAND::ReadD0C ()
{
_bitCountC++; // Increament bit count for Interrupt connected to D0
if (_bitCountC>31) // If bit count more than 31, process high bits
{
_cardTempHighC |= ((0x80000000 & _cardTempC)>>31); // shift value to high bits
_cardTempHighC <<= 1;
_cardTempC <<=1;
}
else
{
_cardTempC <<= 1; // D0 represent binary 0, so just left shift card data
}
_lastWiegand = _sysTick; // Keep track of last wiegand bit received
}
void WIEGAND::ReadD1C()
{
_bitCountC ++; // Increment bit count for Interrupt connected to D1
if (_bitCountC>31) // If bit count more than 31, process high bits
{
_cardTempHighC |= ((0x80000000 & _cardTempC)>>31); // shift value to high bits
_cardTempHighC <<= 1;
_cardTempC |= 1;
_cardTempC <<=1;
}
else
{
_cardTempC |= 1; // D1 represent binary 1, so OR card data with 1 then
_cardTempC <<= 1; // left shift card data
}
_lastWiegand = _sysTick; // Keep track of last wiegand bit received
}
unsigned long WIEGAND::GetCardId (unsigned long *codehigh, unsigned long *codelow, char bitlength)
{
unsigned long cardID=0;
if (bitlength==26) // EM tag
cardID = (*codelow & 0x1FFFFFE) >>1;
if (bitlength==34) // Mifare
{
*codehigh = *codehigh & 0x03; // only need the 2 LSB of the codehigh
*codehigh <<= 30; // shift 2 LSB to MSB
*codelow >>=1;
cardID = *codehigh | *codelow;
}
return cardID;
}
bool WIEGAND::DoWiegandConversion ()
{
unsigned long cardIDA;
unsigned long cardIDB;
unsigned long cardIDC;
_sysTick=millis();
if ((_sysTick - _lastWiegand) > 25) // if no more signal coming through after 25ms
{
if ((_bitCountA==26) || (_bitCountA==34) || (_bitCountA==8) || (_bitCountB==26) || (_bitCountB==34) || (_bitCountB==8) || (_bitCountC==26) || (_bitCountC==34) || (_bitCountC==8) ) // bitCount for keypress=8, Wiegand 26=26, Wiegand 34=34
{
if ((_bitCountA==26) || (_bitCountA==34) || (_bitCountA==8)) // bitCount for keypress=8, Wiegand 26=26, Wiegand 34=34
{
_cardTempA >>= 1; // shift right 1 bit to get back the real value - interrupt done 1 left shift in advance
if (_bitCountA>32) // bit count more than 32 bits, shift high bits right to make adjustment
_cardTempHighA >>= 1;
if((_bitCountA==26) || (_bitCountA==34)) // wiegand 26 or wiegand 34
{
cardIDA = GetCardId (&_cardTempHighA, &_cardTempA, _bitCountA);
_wiegandTypeA=_bitCountA;
_bitCountA=0;
_cardTempA=0;
_cardTempHighA=0;
_GateActive=1;
_codeA=cardIDA;
return true;
}
else if (_bitCountA==8) // keypress wiegand
{
// 8-bit Wiegand keyboard data, high nibble is the "NOT" of low nibble
// eg if key 1 pressed, data=E1 in binary 11100001 , high nibble=1110 , low nibble = 0001
char highNibble = (_cardTempA & 0xf0) >>4;
char lowNibble = (_cardTempA & 0x0f);
_wiegandTypeA=_bitCountA;
_bitCountA=0;
_cardTempA=0;
_cardTempHighA=0;
_GateActive=1;
if (lowNibble == (~highNibble & 0x0f)) // check if low nibble matches the "NOT" of high nibble.
{
if (lowNibble==0x0b) // ENT pressed
{
_codeA=0x0d;
}
else if (lowNibble==0x0a) // ESC pressed
{
_codeA=0x1b;
}
else
{
_codeA=(int)lowNibble; // 0 - 9 keys
}
return true;
}
}
}
else
{
// well time over 25 ms and bitCount !=8 , !=26, !=34 , must be noise or nothing then.
_lastWiegand=_sysTick;
_bitCountA=0;
_cardTempA=0;
_cardTempHighA=0;
_GateActive=0;
}
// fine controllo accesso A
// inizio controllo accesso B
if ((_bitCountB==26) || (_bitCountB==34) || (_bitCountB==8)) // bitCount for keypress=8, Wiegand 26=26, Wiegand 34=34
{
_cardTempB >>= 1; // shift right 1 bit to get back the real value - interrupt done 1 left shift in advance
if (_bitCountB>32) // bit count more than 32 bits, shift high bits right to make adjustment
_cardTempHighB >>= 1;
if((_bitCountB==26) || (_bitCountB==34)) // wiegand 26 or wiegand 34
{
cardIDB = GetCardId (&_cardTempHighB, &_cardTempB, _bitCountB);
_wiegandTypeB=_bitCountB;
_bitCountB=0;
_cardTempB=0;
_cardTempHighB=0;
_GateActive=2;
_codeB=cardIDB;
return true;
}
else if (_bitCountB==8) // keypress wiegand
{
// 8-bit Wiegand keyboard data, high nibble is the "NOT" of low nibble
// eg if key 1 pressed, data=E1 in binary 11100001 , high nibble=1110 , low nibble = 0001
char highNibble = (_cardTempB & 0xf0) >>4;
char lowNibble = (_cardTempB & 0x0f);
_wiegandTypeB=_bitCountB;
_bitCountB=0;
_cardTempB=0;
_cardTempHighB=0;
_GateActive=2;
if (lowNibble == (~highNibble & 0x0f)) // check if low nibble matches the "NOT" of high nibble.
{
if (lowNibble==0x0b) // ENT pressed
{
_codeB=0x0d;
}
else if (lowNibble==0x0a) // ESC pressed
{
_codeB=0x1b;
}
else
{
_codeB=(int)lowNibble; // 0 - 9 keys
}
return true;
}
}
}
else
{
// well time over 25 ms and bitCount !=8 , !=26, !=34 , must be noise or nothing then.
_lastWiegand=_sysTick;
_bitCountB=0;
_cardTempB=0;
_cardTempHighB=0;
_GateActive=0;
}
// fine controllo accesso B
// inizio controllo accesso C
if ((_bitCountC==26) || (_bitCountC==34) || (_bitCountC==8)) // bitCount for keypress=8, Wiegand 26=26, Wiegand 34=34
{
_cardTempC >>= 1; // shift right 1 bit to get back the real value - interrupt done 1 left shift in advance
if (_bitCountC>32) // bit count more than 32 bits, shift high bits right to make adjustment
_cardTempHighC >>= 1;
if((_bitCountC==26) || (_bitCountC==34)) // wiegand 26 or wiegand 34
{
cardIDC = GetCardId (&_cardTempHighC, &_cardTempC, _bitCountC);
_wiegandTypeC=_bitCountC;
_bitCountC=0;
_cardTempC=0;
_cardTempHighC=0;
_GateActive=3;
_codeC=cardIDC;
return true;
}
else if (_bitCountC==8) // keypress wiegand
{
// 8-bit Wiegand keyboard data, high nibble is the "NOT" of low nibble
// eg if key 1 pressed, data=E1 in binary 11100001 , high nibble=1110 , low nibble = 0001
char highNibble = (_cardTempC & 0xf0) >>4;
char lowNibble = (_cardTempC & 0x0f);
_wiegandTypeC=_bitCountC;
_bitCountC=0;
_cardTempC=0;
_cardTempHighC=0;
_GateActive=3;
if (lowNibble == (~highNibble & 0x0f)) // check if low nibble matches the "NOT" of high nibble.
{
if (lowNibble==0x0b) // ENT pressed
{
_codeB=0x0d;
}
else if (lowNibble==0x0a) // ESC pressed
{
_codeB=0x1b;
}
else
{
_codeB=(int)lowNibble; // 0 - 9 keys
}
return true;
}
}
}
else
{
// well time over 25 ms and bitCount !=8 , !=26, !=34 , must be noise or nothing then.
_lastWiegand=_sysTick;
_bitCountC=0;
_cardTempC=0;
_cardTempHighC=0;
_GateActive=0;
}
// fine controllo accesso C
return false;
}
else {
// for reasons, noisy bits could be introduced externally.
// if bitCount !=8 , !=26, !=34, we shall clear all counters to avoid
// existing offset made.
if (_bitCountA) {
_bitCountA=0;
_cardTempA=0;
_cardTempHighA=0;
}
if (_bitCountB) {
_bitCountB=0;
_cardTempB=0;
_cardTempHighB=0;
}
if (_bitCountC) {
_bitCountC=0;
_cardTempC=0;
_cardTempHighC=0;
}
_GateActive=0;
return false;
}
}
else
return false;
}