/
sketch_rfid_hid.ino
409 lines (337 loc) · 11.2 KB
/
sketch_rfid_hid.ino
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
#include "pins_arduino.h"
#include <SPI.h>
#include <Servo.h>
////////////////////////////////////////////////////////////////////////////////
// pin change interrupt handlers
// All I/O pins on the Atmega168 are covered by pin change interrupts. The PCINT
// corresponding to the pin must be enabled and masked and an ISR routine
// provided. Since PCINTs are per port, not per pin, the ISR must use some logic
// to actually implement a per-pin interrupt service.
// Pin to interrupt map:
// D0-D7 = PCINT 16-23 = PCIR2 = PD = PCIE2 = pcmsk2
// D8-D13 = PCINT 0-5 = PCIR0 = PB = PCIE0 = pcmsk0
// A0-A5 (D14-D19) = PCINT 8-13 = PCIR1 = PC = PCIE1 = pcmsk1
volatile uint8_t *port_to_pcmask[] = {
&PCMSK0,
&PCMSK1,
&PCMSK2,
};
static int pin_interrupt_mode[24];
typedef void (*interrupt_fn_t)(void);
volatile static interrupt_fn_t pin_interrupt_functions[24] = {
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL};
volatile static uint8_t pin_register_last_state[3];
void enable_pin_interrupt(uint8_t pin, void (*cb)(void), int mode) {
uint8_t bit = digitalPinToBitMask(pin);
uint8_t port = digitalPinToPort(pin);
uint8_t slot;
volatile uint8_t *pcmask;
// map pin to PCIR register
if (port == NOT_A_PORT) {
return;
} else {
port -= 2;
pcmask = port_to_pcmask[port];
}
if (port == 1) {
slot = port * 8 + (pin - 14);
} else {
slot = port * 8 + (pin % 8);
}
pin_interrupt_mode[slot] = mode;
pin_interrupt_functions[slot] = cb;
// set the mask
*pcmask |= bit;
// enable the interrupt for this port
PCICR |= 0x01 << port;
}
void disable_pin_interrupt(uint8_t pin) {
uint8_t bit = digitalPinToBitMask(pin);
uint8_t port = digitalPinToPort(pin);
volatile uint8_t *pcmask;
// map pin to PCIR register
if (port == NOT_A_PORT) {
return;
}
else {
port -= 2;
pcmask = port_to_pcmask[port];
}
*pcmask &= ~bit;
// disable the port interrupt if there are no pin interrupts remaining
if (*pcmask == 0) {
PCICR &= ~(0x01 << port);
}
}
static void port_interrupt_handler(uint8_t port) {
// get the pin states for the indicated port.
uint8_t curr = *portInputRegister(port + 2);
uint8_t mask = curr ^ pin_register_last_state[port];
pin_register_last_state[port] = curr;
// mask is the pins that have changed; filter out those that don't have
// registered handlers
mask &= *port_to_pcmask[port];
if (mask == 0) {
return;
}
for (uint8_t x = 0; x < 8; x++) {
uint8_t bit = 0x01 << x;
if (!(bit & mask)) {
continue;
}
uint8_t pin = port * 8 + x;
if (pin_interrupt_functions[pin] == NULL) {
continue;
}
if ((pin_interrupt_mode[pin] == CHANGE) ||
((pin_interrupt_mode[pin] == RISING) && (curr & bit)) ||
((pin_interrupt_mode[pin] == FALLING) && !(curr & bit))) {
pin_interrupt_functions[pin]();
}
}
}
SIGNAL(PCINT0_vect) {
port_interrupt_handler(0);
}
SIGNAL(PCINT1_vect) {
port_interrupt_handler(1);
}
SIGNAL(PCINT2_vect) {
port_interrupt_handler(2);
}
////////////////////////////////////////////////////////////////////////////////
// wiegand protocol decoder
volatile uint8_t wiegand_count;
volatile uint8_t wiegand_data[16];
volatile uint32_t wiegand_last_recv_time;
void on_wiegand_bit(uint8_t bit) {
if (bit) {
wiegand_data[wiegand_count >> 3] |= (1 << (7 - (wiegand_count & 7)));
}
wiegand_count++;
wiegand_last_recv_time = millis();
}
void on_wiegand_zero() {
on_wiegand_bit(0);
}
void on_wiegand_one() {
on_wiegand_bit(1);
}
void clear_wiegand_value() {
wiegand_count = 0;
for (uint8_t x = 0; x < 16; x++) {
wiegand_data[x] = 0;
}
wiegand_last_recv_time = millis();
}
void enable_wiegand_reader(int data0_pin, int data1_pin) {
clear_wiegand_value();
pinMode(data0_pin, INPUT);
pinMode(data1_pin, INPUT);
enable_pin_interrupt(data0_pin, on_wiegand_zero, FALLING);
enable_pin_interrupt(data1_pin, on_wiegand_one, FALLING);
}
////////////////////////////////////////////////////////////////////////////////
uint8_t read_capacitive_pin(int pin_num) {
volatile uint8_t* port = portOutputRegister(digitalPinToPort(pin_num));
volatile uint8_t* ddr = portModeRegister(digitalPinToPort(pin_num));
byte bitmask = digitalPinToBitMask(pin_num);
volatile uint8_t* pin = portInputRegister(digitalPinToPort(pin_num));
// Discharge the pin first by setting it low and output
*port &= ~(bitmask);
*ddr |= bitmask;
delay(1);
// Prevent the timer IRQ from disturbing our measurement
uint8_t SREG_old = SREG; //back up the AVR Status Register
noInterrupts();
// Make the pin an input with the internal pull-up on
*ddr &= ~(bitmask);
*port |= bitmask;
// Now see how long the pin to get pulled up. This manual unrolling of the loop
// decreases the number of hardware cycles between each read of the pin,
// thus increasing sensitivity.
uint8_t cycles = 17;
if (*pin & bitmask) { cycles = 0;}
else if (*pin & bitmask) { cycles = 1;}
else if (*pin & bitmask) { cycles = 2;}
else if (*pin & bitmask) { cycles = 3;}
else if (*pin & bitmask) { cycles = 4;}
else if (*pin & bitmask) { cycles = 5;}
else if (*pin & bitmask) { cycles = 6;}
else if (*pin & bitmask) { cycles = 7;}
else if (*pin & bitmask) { cycles = 8;}
else if (*pin & bitmask) { cycles = 9;}
else if (*pin & bitmask) { cycles = 10;}
else if (*pin & bitmask) { cycles = 11;}
else if (*pin & bitmask) { cycles = 12;}
else if (*pin & bitmask) { cycles = 13;}
else if (*pin & bitmask) { cycles = 14;}
else if (*pin & bitmask) { cycles = 15;}
else if (*pin & bitmask) { cycles = 16;}
// End of timing-critical section; turn interrupts back on if they were on before, or leave them off if they were off before
SREG = SREG_old;
// Discharge the pin again by setting it low and output
// It's important to leave the pins low if you want to
// be able to touch more than 1 sensor at a time - if
// the sensor is left pulled high, when you touch
// two sensors, your body will transfer the charge between
// sensors.
*port &= ~(bitmask);
*ddr |= bitmask;
return cycles;
}
////////////////////////////////////////////////////////////////////////////////
// main program
// pin assignments
int reader_data0_pin = 3; // green from R40
int reader_data1_pin = 2; // white from R40
int servo_control_pin = A5; // white wire from servo
int relay_control_pin = A1;
int reader_hold_pin = 4; // blue from R40
int reader_beeper_pin = 5; // yellow from R40
int reader_green_led_pin = 6; // orange from R40
int reader_red_led_pin = 7; // brown from R40
int exit_sense_pin = A0; // exit switch
int ir_phototransistor_sense_pin = A4;
Servo servo;
void setup() {
enable_wiegand_reader(reader_data0_pin, reader_data1_pin);
pinMode(relay_control_pin, OUTPUT);
pinMode(reader_hold_pin, OUTPUT);
pinMode(reader_beeper_pin, OUTPUT);
pinMode(reader_green_led_pin, OUTPUT);
pinMode(reader_red_led_pin, OUTPUT);
pinMode(ir_phototransistor_sense_pin, INPUT);
delay(100);
Serial.begin(115200);
Serial.println("inited");
digitalWrite(reader_hold_pin, HIGH); // disable HOLD
digitalWrite(reader_beeper_pin, HIGH); // disable BEEPER
digitalWrite(reader_green_led_pin, HIGH); // disable GREEN LED
digitalWrite(reader_red_led_pin, HIGH); // disable RED LED
servo.attach(servo_control_pin);
actuate_servo_temporary(0);
}
void actuate_servo_temporary(int position) {
servo.write(position);
digitalWrite(relay_control_pin, HIGH); // activate relay (activating servo)
delay(1500); // wait for servo to get to position
digitalWrite(relay_control_pin, LOW); // deactivate relay + servo
}
bool is_open() {
return digitalRead(ir_phototransistor_sense_pin) == LOW;
}
void unlock() {
digitalWrite(reader_hold_pin, LOW); // enable HOLD
digitalWrite(reader_green_led_pin, LOW); // set reader LED to green
actuate_servo_temporary(180);
long open_time = millis();
delay(3000);
long is_open_time = millis();
while ((millis() - open_time < 10000) && (millis() - is_open_time < 1000)) {
if (is_open()) {
is_open_time = millis();
}
delay(100);
}
actuate_servo_temporary(0);
digitalWrite(reader_green_led_pin, HIGH); // set reader LED to red
digitalWrite(reader_hold_pin, HIGH); // disable HOLD
}
bool memcmp_bits(const uint8_t* a, const uint8_t* b, size_t bits) {
while (bits >= 8) {
if (*a != *b) {
return true;
}
a++;
b++;
bits -= 8;
}
uint8_t mask_for_bit_count[8] = {
0x00, 0x80, 0xC0, 0xE0, 0xF0, 0xF8, 0xFC, 0xFE,
};
if (bits != 0) {
uint8_t mask = mask_for_bit_count[bits];
if ((*a & mask) != (*b & mask)) {
return true;
}
}
return false;
}
void on_card_read(const uint8_t* data, uint16_t bit_count) {
Serial.print("on_card_read: bits=");
Serial.print(bit_count);
Serial.print(" data=0x");
uint8_t bytes_to_print = bit_count >> 3;
if ((bytes_to_print << 3) != bit_count) {
bytes_to_print++;
}
for (uint16_t offset = 0; offset < bytes_to_print; offset++) {
// Serial.print(..., HEX) doesn't print leading zeroes
if ((data[offset] & 0xF0) == 0x00) {
Serial.print("0");
}
Serial.print(data[offset], HEX);
}
Serial.println();
// TODO: Add your cards here! Set num_accepted appropriately, then remove the
// examples and put your own card's ID. To find your card's ID, wire up the
// reader and power the Arduino from your computer's USB port. The Arduino
// will print the card's ID to the serial monitor each time the reader reads
// a card. But watch out: after power-on, sometimes the first card read by
// the reader is sent incorrectly. To make sure you have the right ID, read
// it twice.
const uint8_t num_accepted = 2;
const uint8_t expected_data[num_accepted][16] = {
// 35-bit example
{0xA5, 0x39, 0x58, 0x03, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
// 42-bit example
{0x38, 0x77, 0x2A, 0x94, 0xF6, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
};
const uint8_t expected_bits[num_accepted] = {
35, 42,
};
bool found = false;
for (uint8_t index = 0; index < num_accepted; index++) {
if (bit_count != expected_bits[index]) {
continue;
}
if (memcmp_bits(data, expected_data[index], bit_count)) {
continue;
}
found = true;
}
if (found) {
// if the door is already open, warn (by beeping twice)
if (is_open()) {
digitalWrite(reader_beeper_pin, LOW);
delay(100);
digitalWrite(reader_beeper_pin, HIGH);
delay(100);
digitalWrite(reader_beeper_pin, LOW);
delay(100);
digitalWrite(reader_beeper_pin, HIGH);
}
unlock();
}
}
void loop() {
wiegand_last_recv_time = millis();
long last_open_sense_time = wiegand_last_recv_time;
// wait until there's data, and we haven't received any new data for 100ms
while ((wiegand_count == 0) || (wiegand_last_recv_time > millis() - 100)) {
if (last_open_sense_time < millis() - 500) {
last_open_sense_time = millis();
is_open();
}
if (digitalRead(exit_sense_pin) == HIGH) {
unlock();
}
}
if (wiegand_count > 0) {
on_card_read(wiegand_data, wiegand_count);
}
clear_wiegand_value();
}