Last active
November 8, 2022 22:21
-
-
Save beikeland/145b20b1fc585049ea8951eddfc5732e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <WiegandNG.h> //https://github.com/jpliew/Wiegand-NG-Multi-Bit-Wiegand-Library-for-Arduino | |
//needs https://github.com/jpliew/Wiegand-NG-Multi-Bit-Wiegand-Library-for-Arduino/pull/6 | |
WiegandNG wg; | |
void PrintAR6182(WiegandNG &tempwg) { | |
volatile unsigned char *rawData=tempwg.getRawData(); | |
uint8_t rawSize = tempwg.getBufferSize(); | |
uint8_t countedBits = tempwg.getBitCounted(); | |
uint8_t pinDigits = 0; | |
uint8_t tagDigits = 0; | |
char pin[9] = ""; | |
char tag[14] = ""; | |
if (countedBits % 4 == 0) | |
{ | |
//PIN alone is 32 bits or less | |
if (countedBits <= 32) | |
pinDigits = countedBits/4; | |
//tag alone is exactly 52 bits | |
else if (countedBits == 52) | |
tagDigits = countedBits/4; | |
//tag + PIN is 52 + 4 bits or more. 0xB separates tag and PIN in string. | |
else if (countedBits >= 52+4) { | |
pinDigits = (countedBits-52-4)/4; | |
tagDigits = countedBits/4 - pinDigits-1; | |
} | |
} | |
uint8_t p1 = rawSize*2-pinDigits; //which digit pin start at | |
uint8_t p2 = p1+pinDigits; //which digit pin ends at | |
uint8_t t1 = p1-tagDigits-(pinDigits ? 1 : 0); //which digit card start at | |
uint8_t t2 = p1-(pinDigits ? 1 : 0); //which digit card start at | |
/* | |
//print out raw data indexes | |
for (uint8_t i=0; i<rawSize; i++) | |
printf(" %02d ", i); | |
printf("\n\n"); | |
//print out raw data as readable hex | |
for (uint8_t i=0; i<rawSize; i++) | |
printf("0x%02x ", rawData[i]); | |
printf("\n\n"); | |
//print statistics | |
printf("%d bits = %d digits = raw[%d]\n", countedBits, countedBits/4, (unsigned char) rawSize); | |
printf("tag is from %d to %d, pin is from %d to %d\n", t1, t2-1, p1, p2-1); | |
*/ | |
//AR6182 sends data BCD coded on a Wiegand bus. | |
//loop over each BCD value in each byte in buffer and convert to ascii | |
//extract tag digits, if any | |
for (uint8_t i=t1; i< t2; i++) | |
{ | |
tag[i-t1] = 0x30 + (i%2 ? rawData[i/2] & 0x0F : rawData[i/2] >> 4); | |
//printf("tag[%2d]: digit %2d = raw[%2d]0x%02x (%c) \n", i-t1, i, i/2, (i%2 ? 0x0f : 0xf0), tag[i-t1]); | |
} | |
//extract pin digits, if any | |
for (uint8_t i=p1; i<p2; i++) | |
{ | |
pin[i-p1] = 0x30 + (i%2 ? rawData[i/2] & 0x0F : rawData[i/2] >> 4); | |
//printf("pin[%2d]: digit %2d = raw[%2d]0x%02x (%c) \n", i-p1, i, i/2, (i%2 ? 0x0f : 0xf0), pin[i-p1]); | |
} | |
printf("tag: '%s'(%d) PIN: '%s'(%d)\n", tag, tagDigits, pin, pinDigits); | |
} | |
void setup() { | |
Serial.begin(115200); | |
unsigned int pinD0 = D2; | |
unsigned int pinD1 = D1; | |
unsigned int wiegandbits = 88; | |
unsigned int packetGap = 15; // 25 ms between packet | |
if(!wg.begin(pinD0, pinD1, wiegandbits, packetGap)) { | |
Serial.println("Out of memory!"); | |
} | |
Serial.println("Ready..."); | |
} | |
void loop() { | |
if(wg.available()) { | |
wg.pause(); // pause Wiegand pin interrupts | |
PrintAR6182(wg); | |
wg.clear(); // compulsory to call clear() to enable interrupts for subsequent data | |
} | |
} | |
/* | |
tag: ''(0) PIN: '1'(1) | |
tag: ''(0) PIN: '12'(2) | |
tag: ''(0) PIN: '123'(3) | |
tag: ''(0) PIN: '1234'(4) | |
tag: ''(0) PIN: '12345'(5) | |
tag: ''(0) PIN: '123456'(6) | |
tag: ''(0) PIN: '1234567'(7) | |
tag: ''(0) PIN: '34567890'(8) | |
tag: ''(0) PIN: '1111'(4) | |
tag: '004211925490='(13) PIN: '1111'(4) | |
tag: ''(0) PIN: '11112222'(8) | |
tag: '004211925490='(13) PIN: '11112222'(8) | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment