Skip to content

Commit

Permalink
Release 1.2.0 - Update Wiegand-NG Library
Browse files Browse the repository at this point in the history
* Update jpliew Wiegand-NG Library to commit 67d53aef12c10ef20c883bf49faf2cb902bfc306
  • Loading branch information
exploitagency committed Mar 18, 2018
1 parent c43494c commit 85ceabe
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 107 deletions.
169 changes: 92 additions & 77 deletions Source Code/esprfidtool/WiegandNG.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,126 +2,141 @@

// pcintbranch

volatile unsigned long WiegandNG::_lastPulseTime; // time last bit pulse received
volatile unsigned int WiegandNG::_bitCounted; // number of bits arrived at Interrupt pins
volatile unsigned char *WiegandNG::_buffer; // buffer for data retention
unsigned int WiegandNG::_bufferSize; // memory (bytes) allocated for buffer
volatile unsigned long WiegandNG::_lastPulseTime; // time last bit pulse received
volatile unsigned int WiegandNG::_bitCounted; // number of bits arrived at Interrupt pins
volatile unsigned char *WiegandNG::_buffer; // buffer for data retention
unsigned int WiegandNG::_bufferSize; // memory (bytes) allocated for buffer


void shift_left(volatile unsigned char *ar, int size, int shift)
{
while (shift--) { // for each bit to shift ...
int carry = 0; // clear the initial carry bit.
int lastElement = size-1;
for (int i = 0; i < size; i++) { // for each element of the array, from low byte to high byte
if (i!=lastElement) {
// condition ? valueIfTrue : valueIfFalse
carry = (ar[i+1] & 0x80) ? 1 : 0;
ar[i] = carry | (ar[i]<<1);
}
else {
ar[i] <<=1;
}
}
}
while (shift--) { // for each bit to shift ...
int carry = 0; // clear the initial carry bit.
int lastElement = size-1;
for (int i = 0; i < size; i++) { // for each element of the array, from low byte to high byte
if (i!=lastElement) {
// condition ? valueIfTrue : valueIfFalse
carry = (ar[i+1] & 0x80) ? 1 : 0;
ar[i] = carry | (ar[i]<<1);
}
else {
ar[i] <<=1;
}
}
}
}

void WiegandNG::clear() { // reset variables to start new capture
_bitCounted=0;
_lastPulseTime = millis();
memset((unsigned char *)_buffer,0,_bufferSize);
interrupts(); // allow interrupt
void WiegandNG::clear() { // reset variables to start new capture
_bitCounted=0;
_lastPulseTime = millis();
memset((unsigned char *)_buffer,0,_bufferSize);
interrupts(); // allow interrupt
}

void WiegandNG::pause() {
noInterrupts(); // disable interrupt so that user can process data
noInterrupts(); // disable interrupt so that user can process data
}

volatile unsigned char * WiegandNG::getRawData() {
return _buffer; // return pointer of the buffer
return _buffer; // return pointer of the buffer
}

unsigned int WiegandNG::getPacketGap() {
return _packetGap;
return _packetGap;
}

unsigned int WiegandNG::getBitAllocated() {
return _bitAllocated;
return _bitAllocated;
}

unsigned int WiegandNG::getBitCounted() {
return _bitCounted;
return _bitCounted;
}

unsigned int WiegandNG::getBufferSize() {
return _bufferSize;
return _bufferSize;
}

bool WiegandNG::available() {
bool ret=false;
unsigned long sysTick = millis();
if ((sysTick - _lastPulseTime) > _packetGap) { // _packetGap (ms) laps
if(_bitCounted>0) { // bits found, must have data, return true
ret=true;
}
else
{
_lastPulseTime = millis();
}
}
return ret;
bool ret=false;
noInterrupts();
unsigned long tempLastPulseTime = _lastPulseTime;
interrupts();

unsigned long sysTick = millis();
// if ((sysTick - _lastPulseTime) > _packetGap) { // _packetGap (ms) laps
if ((sysTick - tempLastPulseTime) > _packetGap) { // _packetGap (ms) laps
if(_bitCounted>0) { // bits found, must have data, return true
/*if(_bitCounted<8) {
Serial.print(_bitCounted);
Serial.print(", ");
Serial.print(sysTick);
Serial.print(", ");
Serial.print(_lastPulseTime);
Serial.print(",");
Serial.println(tempLastPulseTime);
}*/
ret=true;
}
else
{
_lastPulseTime = millis();
}
}
return ret;
}

void WiegandNG::ReadD0 () {
_bitCounted++; // increment bit count for Interrupt connected to D0
shift_left(_buffer,_bufferSize,1); // shift 0 into buffer
_lastPulseTime = millis(); // keep track of time last wiegand bit received
_bitCounted++; // increment bit count for Interrupt connected to D0
shift_left(_buffer,_bufferSize,1); // shift 0 into buffer
_lastPulseTime = millis(); // keep track of time last wiegand bit received
}

void WiegandNG::ReadD1() {
_bitCounted++; // increment bit count for Interrupt connected to D1
if (_bitCounted > (_bufferSize * 8)) {
_bitCounted=0; // overflowed,
} else {
shift_left(_buffer,_bufferSize,1); // shift 1 into buffer
_buffer[_bufferSize-1] |=1; // set last bit 1
_lastPulseTime = millis(); // keep track of time last wiegand bit received
}
_bitCounted++; // increment bit count for Interrupt connected to D1
if (_bitCounted > (_bufferSize * 8)) {
_bitCounted=0; // overflowed,
} else {
shift_left(_buffer,_bufferSize,1); // shift 1 into buffer
_buffer[_bufferSize-1] |=1; // set last bit 1
_lastPulseTime = millis(); // keep track of time last wiegand bit received
}
}

bool WiegandNG::begin(unsigned int allocateBits, unsigned int packetGap) {
begin(DATA0,digitalPinToInterrupt(DATA0),DATA1,digitalPinToInterrupt(DATA1), allocateBits, packetGap);
bool ret;
// newer versions of Arduino provide pin to interrupt mapping
ret=begin(2, 3, allocateBits, packetGap);
return ret;
}

bool WiegandNG::begin(uint8_t pinD0, uint8_t pinIntD0, uint8_t pinD1, uint8_t pinIntD1, unsigned int allocateBits, unsigned int packetGap) {
if (_buffer != NULL) {
delete [] _buffer;
}
_packetGap = packetGap;
_bitAllocated = allocateBits;
_bufferSize=(_bitAllocated/8); // calculate the number of bytes required to store wiegand bits
if((_bitAllocated % 8) >0) _bufferSize++; // add 1 extra byte to cater for bits that are not divisible by 8
_buffer = new unsigned char [_bufferSize]; // allocate memory for buffer
if(_buffer == NULL) return false; // not enough memory, return false

clear();
pinMode(pinD0, INPUT); // set D0 pin as input
pinMode(pinD1, INPUT); // set D1 pin as input
attachInterrupt(pinIntD0, ReadD0, FALLING); // hardware interrupt - high to low pulse
attachInterrupt(pinIntD1, ReadD1, FALLING); // hardware interrupt - high to low pulse
return true;
bool WiegandNG::begin(uint8_t pinD0, uint8_t pinD1, unsigned int allocateBits, unsigned int packetGap) {
if (_buffer != NULL) {
delete [] _buffer;
}
_packetGap = packetGap;
_bitAllocated = allocateBits;
_bufferSize=(_bitAllocated/8); // calculate the number of bytes required to store wiegand bits
if((_bitAllocated % 8) >0) _bufferSize++; // add 1 extra byte to cater for bits that are not divisible by 8
_buffer = new unsigned char [_bufferSize]; // allocate memory for buffer
if(_buffer == NULL) return false; // not enough memory, return false

clear();
pinMode(pinD0, INPUT); // set D0 pin as input
pinMode(pinD1, INPUT); // set D1 pin as input
attachInterrupt(digitalPinToInterrupt(pinD0), ReadD0, FALLING); // hardware interrupt - high to low pulse
attachInterrupt(digitalPinToInterrupt(pinD1), ReadD1, FALLING); // hardware interrupt - high to low pulse
return true;
}

WiegandNG::WiegandNG() {

}

WiegandNG::~WiegandNG() {
if (_buffer != NULL) {
delete [] _buffer;
}
if (_buffer != NULL) {
delete [] _buffer;
}
}


46 changes: 22 additions & 24 deletions Source Code/esprfidtool/WiegandNG.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
#define DATA0 14
#define DATA1 12

#ifndef _WIEGAND_NG_H
#define _WIEGAND_NG_H

Expand All @@ -13,28 +10,29 @@
class WiegandNG {

private:
static void ReadD0();
static void ReadD1();
static volatile unsigned long _lastPulseTime; // time last bits received
static volatile unsigned int _bitCounted; // number of bits arrived at Interrupt pins
static unsigned int _bufferSize; // memory (bytes) allocated for buffer
unsigned int _bitAllocated; // wiegand bits required
unsigned int _packetGap; // gap between wiegand packet in millisecond
static volatile unsigned char * _buffer; // buffer for data retention
static void ReadD0();
static void ReadD1();
static volatile unsigned long _lastPulseTime; // time last bits received
static volatile unsigned int _bitCounted; // number of bits arrived at Interrupt pins
static unsigned int _bufferSize; // memory (bytes) allocated for buffer
unsigned int _bitAllocated; // wiegand bits required
unsigned int _packetGap; // gap between wiegand packet in millisecond
static volatile unsigned char * _buffer; // buffer for data retention
public:
bool begin(unsigned int bits, unsigned int packetGap=25); // default packetGap is 25ms
bool begin(uint8_t pinD0, uint8_t pinIntD0, uint8_t pinD1, uint8_t pinIntD1, unsigned int bits, unsigned int packetGap);
bool available();
void clear();
void pause();
unsigned int getBitCounted();
unsigned int getBitAllocated();
unsigned int getBufferSize();
unsigned int getPacketGap();
volatile unsigned char *getRawData();
WiegandNG();
~WiegandNG();
bool begin(unsigned int bits, unsigned int packetGap=25); // default packetGap is 25ms
bool begin(uint8_t pinD0, uint8_t pinD1, unsigned int bits, unsigned int packetGap);
bool available();
void clear();
void pause();
unsigned int getBitCounted();
unsigned int getBitAllocated();
unsigned int getBufferSize();
unsigned int getPacketGap();
volatile unsigned char *getRawData();
WiegandNG();
~WiegandNG();
};

#endif

19 changes: 14 additions & 5 deletions Source Code/esprfidtool/esprfidtool.ino
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@
#include <DNSServer.h>
#include <ESP8266mDNS.h>

#define DATA0 14
#define DATA1 12

#define LED_BUILTIN 2
#define RESTORE_DEFAULTS_PIN 4 //GPIO 4
int jumperState = 0; //For restoring default settings
Expand Down Expand Up @@ -74,7 +77,8 @@ char ftp_password[64];
int ftpenabled;
int ledenabled;
char logname[31];
int bufferlength;
unsigned int bufferlength;
unsigned int rxpacketgap;
int txdelayus;
int txdelayms;
int safemode;
Expand All @@ -96,7 +100,7 @@ void LogWiegand(WiegandNG &tempwg) {

unsigned int countedBytes = (countedBits/8);
if ((countedBits % 8)>0) countedBytes++;
unsigned int bitsUsed = countedBytes * 8;
//unsigned int bitsUsed = countedBytes * 8;

bool binChunk2exists=false;
volatile unsigned long cardChunk1 = 0;
Expand All @@ -109,7 +113,7 @@ void LogWiegand(WiegandNG &tempwg) {
int binChunk2len=0;
int j=0;

for (int i=bufferSize-countedBytes; i< bufferSize;i++) {
for (unsigned int i=bufferSize-countedBytes; i< bufferSize;i++) {
unsigned char bufByte=buffer[i];
for(int x=0; x<8;x++) {
if ( (((bufferSize-i) *8)-x) <= countedBits) {
Expand Down Expand Up @@ -713,7 +717,8 @@ void settingsPage()
"<small>Default Buffer Length is 256 bits with an allowed range of 52-4096 bits."
"<br>Default Experimental TX mode timing is 40us Wiegand Data Pulse Width and a 2ms Wiegand Data Interval with an allowed range of 0-1000."
"<br>Changing these settings may result in unstable performance.</small><br>"
"Wiegand Buffer Length: <input type=\"number\" name=\"bufferlength\" value=\"")+bufferlength+F("\" maxlength=\"30\" size=\"31\" min=\"52\" max=\"4096\"> bit(s)<br>"
"Wiegand RX Buffer Length: <input type=\"number\" name=\"bufferlength\" value=\"")+bufferlength+F("\" maxlength=\"30\" size=\"31\" min=\"52\" max=\"4096\"> bit(s)<br>"
"Wiegand RX Packet Length: <input type=\"number\" name=\"rxpacketgap\" value=\"")+rxpacketgap+F("\" maxlength=\"30\" size=\"31\" min=\"1\" max=\"4096\"> millisecond(s)<br>"
"Experimental TX Wiegand Data Pulse Width: <input type=\"number\" name=\"txdelayus\" value=\"")+txdelayus+F("\" maxlength=\"30\" size=\"31\" min=\"0\" max=\"1000\"> microsecond(s)<br>"
"Experimental TX Wiegand Data Interval: <input type=\"number\" name=\"txdelayms\" value=\"")+txdelayms+F("\" maxlength=\"30\" size=\"31\" min=\"0\" max=\"1000\"> millisecond(s)<br>"
"<hr>"
Expand Down Expand Up @@ -773,6 +778,7 @@ void handleSubmitSettings()
ledenabled = server.arg("ledenabled").toInt();
server.arg("logname").toCharArray(logname, 31);
bufferlength = server.arg("bufferlength").toInt();
rxpacketgap = server.arg("rxpacketgap").toInt();
txdelayus = server.arg("txdelayus").toInt();
txdelayms = server.arg("txdelayms").toInt();
safemode = server.arg("safemode").toInt();
Expand Down Expand Up @@ -811,6 +817,7 @@ bool loadDefaults() {
json["ledenabled"] = "1";
json["logname"] = "log.txt";
json["bufferlength"] = "256";
json["rxpacketgap"] = "15";
json["txdelayus"] = "40";
json["txdelayms"] = "2";
json["safemode"] = "0";
Expand Down Expand Up @@ -866,6 +873,7 @@ bool loadConfig() {
ledenabled = json["ledenabled"];
strcpy(logname, (const char*)json["logname"]);
bufferlength = json["bufferlength"];
rxpacketgap = json["rxpacketgap"];
txdelayus = json["txdelayus"];
txdelayms = json["txdelayms"];
safemode = json["safemode"];
Expand Down Expand Up @@ -948,6 +956,7 @@ bool saveConfig() {
json["ledenabled"] = ledenabled;
json["logname"] = logname;
json["bufferlength"] = bufferlength;
json["rxpacketgap"] = rxpacketgap;
json["txdelayus"] = txdelayus;
json["txdelayms"] = txdelayms;
json["safemode"] = safemode;
Expand Down Expand Up @@ -1033,7 +1042,7 @@ void setup() {

loadConfig();

if(!wg.begin(bufferlength)) {
if(!wg.begin(DATA0,DATA1,bufferlength,rxpacketgap)) {
Serial.println(F("Could not begin Wiegand logging,"));
Serial.println(F("Out of memory!"));
}
Expand Down
2 changes: 1 addition & 1 deletion Source Code/esprfidtool/version.h
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
String version = "1.1.9";
String version = "1.2.0";
String APIversion = "1.0.1";

0 comments on commit 85ceabe

Please sign in to comment.