Clean up and simplify code, write output as keyboard
This commit is contained in:
parent
2251196114
commit
df2210bf4d
188
hidReader.ino
188
hidReader.ino
@ -1,153 +1,105 @@
|
|||||||
/*
|
/* Based on:
|
||||||
* HID RFID Reader Wiegand Interface for Arduino Uno
|
* HID RFID Reader Wiegand Interface for Arduino Uno
|
||||||
* Originally by Daniel Smith, 2012.01.30 -- http://www.pagemac.com/projects/rfid/arduino_wiegand
|
* Originally by Daniel Smith, 2012.01.30 -- http://www.pagemac.com/projects/rfid/arduino_wiegand
|
||||||
*
|
*
|
||||||
* Updated 2016-11-23 by Jon "ShakataGaNai" Davis.
|
* Updated 2016-11-23 by Jon "ShakataGaNai" Davis.
|
||||||
* See https://obviate.io/?p=7470 for more details & instructions
|
* See https://obviate.io/?p=7470 for more details & instructions
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
#define MAX_BITS 100 // max number of bits
|
|
||||||
#define WEIGAND_WAIT_TIME 3000 // time to wait for another weigand pulse.
|
|
||||||
|
|
||||||
unsigned char databits[MAX_BITS]; // stores all of the data bits
|
|
||||||
unsigned char bitCount; // number of bits currently captured
|
|
||||||
unsigned char flagDone; // goes low when data is currently being captured
|
|
||||||
unsigned int weigand_counter; // countdown until we assume there are no more bits
|
|
||||||
|
|
||||||
unsigned long facilityCode=0; // decoded facility code
|
|
||||||
unsigned long cardCode=0; // decoded card code
|
|
||||||
|
|
||||||
int LED_GREEN = 11;
|
#include <limits.h>
|
||||||
int LED_RED = 12;
|
#include <Keyboard.h>
|
||||||
int BEEP_BEEP = 10;
|
|
||||||
|
// LED pins
|
||||||
|
#define LED_GREEN 11;
|
||||||
|
#define LED_RED 12;
|
||||||
|
#define BEEP_BEEP 10;
|
||||||
|
|
||||||
|
#define WEIGAND_WAIT_TIME 3000 // time to wait for another weigand pulse.
|
||||||
|
|
||||||
|
#define DATA_SIZE 32
|
||||||
|
volatile uint32_t dataBits = 0; // stores all of the data bits
|
||||||
|
volatile size_t bitCount = 0; // number of bits recieved
|
||||||
|
|
||||||
|
volatile unsigned char flagDone; // goes low when data is currently being captured
|
||||||
|
volatile unsigned int weigand_counter; // countdown until we assume there are no more bits
|
||||||
|
|
||||||
|
inline void gotBit() {
|
||||||
|
bitCount++;
|
||||||
|
flagDone = 0;
|
||||||
|
weigand_counter = WEIGAND_WAIT_TIME;
|
||||||
|
}
|
||||||
|
|
||||||
// interrupt that happens when INTO goes low (0 bit)
|
// interrupt that happens when INTO goes low (0 bit)
|
||||||
void ISR_INT0() {
|
void ISR_INT0() {
|
||||||
//Serial.print("0"); // uncomment this line to display raw binary
|
//Serial.print("0");
|
||||||
bitCount++;
|
dataBits &= ~(1UL << (DATA_SIZE - bitCount));
|
||||||
flagDone = 0;
|
gotBit();
|
||||||
weigand_counter = WEIGAND_WAIT_TIME;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// interrupt that happens when INT1 goes low (1 bit)
|
// interrupt that happens when INT1 goes low (1 bit)
|
||||||
void ISR_INT1() {
|
void ISR_INT1() {
|
||||||
//Serial.print("1"); // uncomment this line to display raw binary
|
//Serial.print("1");
|
||||||
databits[bitCount] = 1;
|
dataBits |= 1UL << (DATA_SIZE - bitCount);
|
||||||
bitCount++;
|
gotBit();
|
||||||
flagDone = 0;
|
|
||||||
weigand_counter = WEIGAND_WAIT_TIME;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
pinMode(LED_RED, OUTPUT);
|
pinMode(2, INPUT); // DATA0 (INT0)
|
||||||
pinMode(LED_GREEN, OUTPUT);
|
pinMode(3, INPUT); // DATA1 (INT1)
|
||||||
pinMode(BEEP_BEEP, OUTPUT);
|
|
||||||
digitalWrite(LED_RED, HIGH); // High = Off
|
|
||||||
digitalWrite(BEEP_BEEP, HIGH); // High = off
|
|
||||||
digitalWrite(LED_GREEN, LOW); // Low = On
|
|
||||||
pinMode(2, INPUT); // DATA0 (INT0)
|
|
||||||
pinMode(3, INPUT); // DATA1 (INT1)
|
|
||||||
|
|
||||||
Serial.begin(9600);
|
Serial.begin(9600);
|
||||||
Serial.println("RFID Readers");
|
|
||||||
|
attachInterrupt(0, ISR_INT0, FALLING);
|
||||||
// binds the ISR functions to the falling edge of INTO and INT1
|
|
||||||
attachInterrupt(0, ISR_INT0, FALLING);
|
|
||||||
attachInterrupt(1, ISR_INT1, FALLING);
|
attachInterrupt(1, ISR_INT1, FALLING);
|
||||||
|
|
||||||
|
|
||||||
weigand_counter = WEIGAND_WAIT_TIME;
|
weigand_counter = WEIGAND_WAIT_TIME;
|
||||||
|
Keyboard.begin();
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop()
|
void loop()
|
||||||
{
|
{
|
||||||
// This waits to make sure that there have been no more data pulses before processing data
|
// This waits to make sure that there have been no more data pulses before processing data
|
||||||
if (!flagDone) {
|
if (!flagDone) {
|
||||||
if (--weigand_counter == 0)
|
if (--weigand_counter == 0)
|
||||||
flagDone = 1;
|
flagDone = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
// if we have bits and we the weigand counter went out
|
// if we have bits and we the weigand counter went out
|
||||||
if (bitCount > 0 && flagDone) {
|
if (bitCount > 0 && flagDone) {
|
||||||
unsigned char i;
|
unsigned char i;
|
||||||
|
uint32_t data = dataBits >> (DATA_SIZE - bitCount + 1);
|
||||||
|
|
||||||
Serial.print("Read ");
|
Serial.print("Read ");
|
||||||
Serial.print(bitCount);
|
Serial.print(bitCount);
|
||||||
Serial.print(" bits. ");
|
Serial.print(" bits: ");
|
||||||
|
Serial.println(data, BIN);
|
||||||
if (bitCount == 35) {
|
|
||||||
// 35 bit HID Corporate 1000 format
|
// standard 26 bit format:
|
||||||
// facility code = bits 2 to 14
|
// P: parity bit, F: facility code, C: card code
|
||||||
for (i=2; i<14; i++) {
|
// PFFFFFFFFCCCCCCCCCCCCCCCCP
|
||||||
facilityCode <<=1;
|
if (bitCount == 26) {
|
||||||
facilityCode |= databits[i];
|
uint8_t facilityCode = (data >> 17);
|
||||||
}
|
uint16_t cardCode = (data >> 1);
|
||||||
|
// TODO: check parity bits
|
||||||
// card code = bits 15 to 34
|
|
||||||
for (i=14; i<34; i++) {
|
Keyboard.print(facilityCode);
|
||||||
cardCode <<=1;
|
Keyboard.print('\t');
|
||||||
cardCode |= databits[i];
|
Keyboard.print(cardCode);
|
||||||
}
|
Keyboard.print('\n');
|
||||||
|
printBits(facilityCode, cardCode);
|
||||||
printBits();
|
|
||||||
}
|
|
||||||
else if (bitCount == 26) {
|
|
||||||
// standard 26 bit format
|
|
||||||
// facility code = bits 2 to 9
|
|
||||||
for (i=1; i<9; i++) {
|
|
||||||
facilityCode <<=1;
|
|
||||||
facilityCode |= databits[i];
|
|
||||||
}
|
|
||||||
|
|
||||||
// card code = bits 10 to 23
|
|
||||||
for (i=9; i<25; i++) {
|
|
||||||
cardCode <<=1;
|
|
||||||
cardCode |= databits[i];
|
|
||||||
}
|
|
||||||
|
|
||||||
printBits();
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// you can add other formats if you want!
|
// TODO: handle failures and other formats
|
||||||
// Serial.println("Unable to decode.");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// cleanup and get ready for the next card
|
// reset for the next card
|
||||||
bitCount = 0;
|
bitCount = 0;
|
||||||
facilityCode = 0;
|
|
||||||
cardCode = 0;
|
|
||||||
for (i=0; i<MAX_BITS; i++)
|
|
||||||
{
|
|
||||||
databits[i] = 0;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void printBits() {
|
|
||||||
Serial.print("FC = ");
|
|
||||||
Serial.print(facilityCode);
|
|
||||||
Serial.print(", CC = ");
|
|
||||||
Serial.println(cardCode);
|
|
||||||
|
|
||||||
// Now lets play with some LED's for fun:
|
void printBits(uint8_t facilityCode, uint16_t cardCode) {
|
||||||
digitalWrite(LED_RED, LOW); // Red
|
Serial.print("FC = ");
|
||||||
if(cardCode == 12345){
|
Serial.print(facilityCode);
|
||||||
// If this one "bad" card, turn off green
|
Serial.print(", CC = ");
|
||||||
// so it's just red. Otherwise you get orange-ish
|
Serial.println(cardCode);
|
||||||
digitalWrite(LED_GREEN, HIGH);
|
|
||||||
}
|
|
||||||
delay(500);
|
|
||||||
digitalWrite(LED_RED, HIGH); // Red Off
|
|
||||||
digitalWrite(LED_GREEN, LOW); // Green back on
|
|
||||||
|
|
||||||
// Lets be annoying and beep more
|
|
||||||
digitalWrite(BEEP_BEEP, LOW);
|
|
||||||
delay(500);
|
|
||||||
digitalWrite(BEEP_BEEP, HIGH);
|
|
||||||
delay(500);
|
|
||||||
digitalWrite(BEEP_BEEP, LOW);
|
|
||||||
delay(500);
|
|
||||||
digitalWrite(BEEP_BEEP, HIGH);
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user