-
Notifications
You must be signed in to change notification settings - Fork 17
/
reader.c
executable file
·146 lines (120 loc) · 3.84 KB
/
reader.c
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
/*
* linked with -lpthread -lwiringPi -lrt
*/
#include <stdio.h>
#include <stdlib.h>
#include <wiringPi.h>
#include <time.h>
#include <unistd.h>
#include <memory.h>
#define PIN_0 0 // GPIO Pin 17 | Green cable | Data0
#define PIN_1 1 // GPIO Pin 18 | White cable | Data1
#define PIN_SOUND 25 // GPIO Pin 26 | Yellow cable | Sound
#define MAXWIEGANDBITS 32
#define READERTIMEOUT 3000000
#define LEN 256
static unsigned char __wiegandData[MAXWIEGANDBITS];
static unsigned long __wiegandBitCount;
static struct timespec __wiegandBitTime;
void getData0(void) {
if (__wiegandBitCount / 8 < MAXWIEGANDBITS) {
__wiegandData[__wiegandBitCount / 8] <<= 1;
__wiegandBitCount++;
}
clock_gettime(CLOCK_MONOTONIC, &__wiegandBitTime);
}
void getData1(void) {
if (__wiegandBitCount / 8 < MAXWIEGANDBITS) {
__wiegandData[__wiegandBitCount / 8] <<= 1;
__wiegandData[__wiegandBitCount / 8] |= 1;
__wiegandBitCount++;
}
clock_gettime(CLOCK_MONOTONIC, &__wiegandBitTime);
}
int wiegandInit(int d0pin, int d1pin) {
// Setup wiringPi
wiringPiSetup() ;
pinMode(d0pin, INPUT);
pinMode(d1pin, INPUT);
pinMode(PIN_SOUND, OUTPUT);
wiringPiISR(d0pin, INT_EDGE_FALLING, getData0);
wiringPiISR(d1pin, INT_EDGE_FALLING, getData1);
}
void wiegandReset() {
memset((void *)__wiegandData, 0, MAXWIEGANDBITS);
__wiegandBitCount = 0;
}
int wiegandGetPendingBitCount() {
struct timespec now, delta;
clock_gettime(CLOCK_MONOTONIC, &now);
delta.tv_sec = now.tv_sec - __wiegandBitTime.tv_sec;
delta.tv_nsec = now.tv_nsec - __wiegandBitTime.tv_nsec;
if ((delta.tv_sec > 1) || (delta.tv_nsec > READERTIMEOUT))
return __wiegandBitCount;
return 0;
}
int wiegandReadData(void* data, int dataMaxLen) {
if (wiegandGetPendingBitCount() > 0) {
int bitCount = __wiegandBitCount;
int byteCount = (__wiegandBitCount / 8) + 1;
memcpy(data, (void *)__wiegandData, ((byteCount > dataMaxLen) ? dataMaxLen : byteCount));
wiegandReset();
return bitCount;
}
return 0;
}
void printCharAsBinary(unsigned char ch) {
int i;
FILE * fp;
fp = fopen("output","a");
for (i = 0; i < 8; i++) {
printf("%d", (ch & 0x80) ? 1 : 0);
fprintf(fp, "%d", (ch & 0x80) ? 1 : 0);
ch <<= 1;
}
fclose(fp);
}
void makeBeep(int millisecs, int times){
int i;
for (i = 0; i < times; i++) {
digitalWrite (PIN_SOUND, LOW);
delay(millisecs);
digitalWrite (PIN_SOUND, HIGH);
delay(millisecs/2);
}
}
void main(void) {
int i;
wiegandInit(PIN_0, PIN_1);
while(1) {
int bitLen = wiegandGetPendingBitCount();
if (bitLen == 0) {
usleep(5000);
} else {
char data[100];
char string1[100];
bitLen = wiegandReadData((void *)data, 100);
int bytes = bitLen / 8 + 1;
FILE *fp;
fp = fopen("output","a");
printf("%lu ", (unsigned long)time(NULL));
fprintf(fp, "%lu ", (unsigned long)time(NULL));
printf("Read %d bits (%d bytes): ", bitLen, bytes);
fprintf(fp, "Read %d bits (%d bytes): ", bitLen, bytes);
for (i = 0; i < bytes; i++)
printf("%02X", (int)data[i]);
for (i = 0; i < bytes; i++)
fprintf(fp, "%02X", (int)data[i]);
printf(" : ");
fprintf(fp, " : ");
fclose(fp);
for (i = 0; i < bytes; i++)
printCharAsBinary(data[i]);
fp = fopen("output","a");
printf("\n");
fprintf(fp, "\n");
fclose(fp);
makeBeep(200, 1);
}
}
}