Skip to content

Commit 85ceabe

Browse files
committed
Release 1.2.0 - Update Wiegand-NG Library
* Update jpliew Wiegand-NG Library to commit 67d53aef12c10ef20c883bf49faf2cb902bfc306
1 parent c43494c commit 85ceabe

File tree

4 files changed

+129
-107
lines changed

4 files changed

+129
-107
lines changed

Source Code/esprfidtool/WiegandNG.cpp

Lines changed: 92 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -2,126 +2,141 @@
22

33
// pcintbranch
44

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

1010

1111
void shift_left(volatile unsigned char *ar, int size, int shift)
1212
{
13-
while (shift--) { // for each bit to shift ...
14-
int carry = 0; // clear the initial carry bit.
15-
int lastElement = size-1;
16-
for (int i = 0; i < size; i++) { // for each element of the array, from low byte to high byte
17-
if (i!=lastElement) {
18-
// condition ? valueIfTrue : valueIfFalse
19-
carry = (ar[i+1] & 0x80) ? 1 : 0;
20-
ar[i] = carry | (ar[i]<<1);
21-
}
22-
else {
23-
ar[i] <<=1;
24-
}
25-
}
26-
}
13+
while (shift--) { // for each bit to shift ...
14+
int carry = 0; // clear the initial carry bit.
15+
int lastElement = size-1;
16+
for (int i = 0; i < size; i++) { // for each element of the array, from low byte to high byte
17+
if (i!=lastElement) {
18+
// condition ? valueIfTrue : valueIfFalse
19+
carry = (ar[i+1] & 0x80) ? 1 : 0;
20+
ar[i] = carry | (ar[i]<<1);
21+
}
22+
else {
23+
ar[i] <<=1;
24+
}
25+
}
26+
}
2727
}
2828

29-
void WiegandNG::clear() { // reset variables to start new capture
30-
_bitCounted=0;
31-
_lastPulseTime = millis();
32-
memset((unsigned char *)_buffer,0,_bufferSize);
33-
interrupts(); // allow interrupt
29+
void WiegandNG::clear() { // reset variables to start new capture
30+
_bitCounted=0;
31+
_lastPulseTime = millis();
32+
memset((unsigned char *)_buffer,0,_bufferSize);
33+
interrupts(); // allow interrupt
3434
}
3535

3636
void WiegandNG::pause() {
37-
noInterrupts(); // disable interrupt so that user can process data
37+
noInterrupts(); // disable interrupt so that user can process data
3838
}
3939

4040
volatile unsigned char * WiegandNG::getRawData() {
41-
return _buffer; // return pointer of the buffer
41+
return _buffer; // return pointer of the buffer
4242
}
4343

4444
unsigned int WiegandNG::getPacketGap() {
45-
return _packetGap;
45+
return _packetGap;
4646
}
4747

4848
unsigned int WiegandNG::getBitAllocated() {
49-
return _bitAllocated;
49+
return _bitAllocated;
5050
}
5151

5252
unsigned int WiegandNG::getBitCounted() {
53-
return _bitCounted;
53+
return _bitCounted;
5454
}
5555

5656
unsigned int WiegandNG::getBufferSize() {
57-
return _bufferSize;
57+
return _bufferSize;
5858
}
5959

6060
bool WiegandNG::available() {
61-
bool ret=false;
62-
unsigned long sysTick = millis();
63-
if ((sysTick - _lastPulseTime) > _packetGap) { // _packetGap (ms) laps
64-
if(_bitCounted>0) { // bits found, must have data, return true
65-
ret=true;
66-
}
67-
else
68-
{
69-
_lastPulseTime = millis();
70-
}
71-
}
72-
return ret;
61+
bool ret=false;
62+
noInterrupts();
63+
unsigned long tempLastPulseTime = _lastPulseTime;
64+
interrupts();
65+
66+
unsigned long sysTick = millis();
67+
// if ((sysTick - _lastPulseTime) > _packetGap) { // _packetGap (ms) laps
68+
if ((sysTick - tempLastPulseTime) > _packetGap) { // _packetGap (ms) laps
69+
if(_bitCounted>0) { // bits found, must have data, return true
70+
/*if(_bitCounted<8) {
71+
Serial.print(_bitCounted);
72+
Serial.print(", ");
73+
Serial.print(sysTick);
74+
Serial.print(", ");
75+
Serial.print(_lastPulseTime);
76+
Serial.print(",");
77+
Serial.println(tempLastPulseTime);
78+
}*/
79+
ret=true;
80+
}
81+
else
82+
{
83+
_lastPulseTime = millis();
84+
}
85+
}
86+
return ret;
7387
}
7488

7589
void WiegandNG::ReadD0 () {
76-
_bitCounted++; // increment bit count for Interrupt connected to D0
77-
shift_left(_buffer,_bufferSize,1); // shift 0 into buffer
78-
_lastPulseTime = millis(); // keep track of time last wiegand bit received
90+
_bitCounted++; // increment bit count for Interrupt connected to D0
91+
shift_left(_buffer,_bufferSize,1); // shift 0 into buffer
92+
_lastPulseTime = millis(); // keep track of time last wiegand bit received
7993
}
8094

8195
void WiegandNG::ReadD1() {
82-
_bitCounted++; // increment bit count for Interrupt connected to D1
83-
if (_bitCounted > (_bufferSize * 8)) {
84-
_bitCounted=0; // overflowed,
85-
} else {
86-
shift_left(_buffer,_bufferSize,1); // shift 1 into buffer
87-
_buffer[_bufferSize-1] |=1; // set last bit 1
88-
_lastPulseTime = millis(); // keep track of time last wiegand bit received
89-
}
96+
_bitCounted++; // increment bit count for Interrupt connected to D1
97+
if (_bitCounted > (_bufferSize * 8)) {
98+
_bitCounted=0; // overflowed,
99+
} else {
100+
shift_left(_buffer,_bufferSize,1); // shift 1 into buffer
101+
_buffer[_bufferSize-1] |=1; // set last bit 1
102+
_lastPulseTime = millis(); // keep track of time last wiegand bit received
103+
}
90104
}
91105

92106
bool WiegandNG::begin(unsigned int allocateBits, unsigned int packetGap) {
93-
begin(DATA0,digitalPinToInterrupt(DATA0),DATA1,digitalPinToInterrupt(DATA1), allocateBits, packetGap);
107+
bool ret;
108+
// newer versions of Arduino provide pin to interrupt mapping
109+
ret=begin(2, 3, allocateBits, packetGap);
110+
return ret;
94111
}
95112

96-
bool WiegandNG::begin(uint8_t pinD0, uint8_t pinIntD0, uint8_t pinD1, uint8_t pinIntD1, unsigned int allocateBits, unsigned int packetGap) {
97-
if (_buffer != NULL) {
98-
delete [] _buffer;
99-
}
100-
_packetGap = packetGap;
101-
_bitAllocated = allocateBits;
102-
103-
_bufferSize=(_bitAllocated/8); // calculate the number of bytes required to store wiegand bits
104-
if((_bitAllocated % 8) >0) _bufferSize++; // add 1 extra byte to cater for bits that are not divisible by 8
105-
_buffer = new unsigned char [_bufferSize]; // allocate memory for buffer
106-
if(_buffer == NULL) return false; // not enough memory, return false
107-
108-
clear();
109-
110-
pinMode(pinD0, INPUT); // set D0 pin as input
111-
pinMode(pinD1, INPUT); // set D1 pin as input
112-
attachInterrupt(pinIntD0, ReadD0, FALLING); // hardware interrupt - high to low pulse
113-
attachInterrupt(pinIntD1, ReadD1, FALLING); // hardware interrupt - high to low pulse
114-
return true;
113+
bool WiegandNG::begin(uint8_t pinD0, uint8_t pinD1, unsigned int allocateBits, unsigned int packetGap) {
114+
if (_buffer != NULL) {
115+
delete [] _buffer;
116+
}
117+
_packetGap = packetGap;
118+
_bitAllocated = allocateBits;
119+
120+
_bufferSize=(_bitAllocated/8); // calculate the number of bytes required to store wiegand bits
121+
if((_bitAllocated % 8) >0) _bufferSize++; // add 1 extra byte to cater for bits that are not divisible by 8
122+
_buffer = new unsigned char [_bufferSize]; // allocate memory for buffer
123+
if(_buffer == NULL) return false; // not enough memory, return false
124+
125+
clear();
126+
127+
pinMode(pinD0, INPUT); // set D0 pin as input
128+
pinMode(pinD1, INPUT); // set D1 pin as input
129+
attachInterrupt(digitalPinToInterrupt(pinD0), ReadD0, FALLING); // hardware interrupt - high to low pulse
130+
attachInterrupt(digitalPinToInterrupt(pinD1), ReadD1, FALLING); // hardware interrupt - high to low pulse
131+
return true;
115132
}
116133

117134
WiegandNG::WiegandNG() {
118135

119136
}
120137

121138
WiegandNG::~WiegandNG() {
122-
if (_buffer != NULL) {
123-
delete [] _buffer;
124-
}
139+
if (_buffer != NULL) {
140+
delete [] _buffer;
141+
}
125142
}
126-
127-

Source Code/esprfidtool/WiegandNG.h

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
#define DATA0 14
2-
#define DATA1 12
3-
41
#ifndef _WIEGAND_NG_H
52
#define _WIEGAND_NG_H
63

@@ -13,28 +10,29 @@
1310
class WiegandNG {
1411

1512
private:
16-
static void ReadD0();
17-
static void ReadD1();
18-
static volatile unsigned long _lastPulseTime; // time last bits received
19-
static volatile unsigned int _bitCounted; // number of bits arrived at Interrupt pins
20-
static unsigned int _bufferSize; // memory (bytes) allocated for buffer
21-
unsigned int _bitAllocated; // wiegand bits required
22-
unsigned int _packetGap; // gap between wiegand packet in millisecond
23-
static volatile unsigned char * _buffer; // buffer for data retention
24-
13+
static void ReadD0();
14+
static void ReadD1();
15+
static volatile unsigned long _lastPulseTime; // time last bits received
16+
static volatile unsigned int _bitCounted; // number of bits arrived at Interrupt pins
17+
static unsigned int _bufferSize; // memory (bytes) allocated for buffer
18+
unsigned int _bitAllocated; // wiegand bits required
19+
unsigned int _packetGap; // gap between wiegand packet in millisecond
20+
static volatile unsigned char * _buffer; // buffer for data retention
21+
2522
public:
26-
bool begin(unsigned int bits, unsigned int packetGap=25); // default packetGap is 25ms
27-
bool begin(uint8_t pinD0, uint8_t pinIntD0, uint8_t pinD1, uint8_t pinIntD1, unsigned int bits, unsigned int packetGap);
28-
bool available();
29-
void clear();
30-
void pause();
31-
unsigned int getBitCounted();
32-
unsigned int getBitAllocated();
33-
unsigned int getBufferSize();
34-
unsigned int getPacketGap();
35-
volatile unsigned char *getRawData();
36-
WiegandNG();
37-
~WiegandNG();
23+
bool begin(unsigned int bits, unsigned int packetGap=25); // default packetGap is 25ms
24+
bool begin(uint8_t pinD0, uint8_t pinD1, unsigned int bits, unsigned int packetGap);
25+
bool available();
26+
void clear();
27+
void pause();
28+
unsigned int getBitCounted();
29+
unsigned int getBitAllocated();
30+
unsigned int getBufferSize();
31+
unsigned int getPacketGap();
32+
volatile unsigned char *getRawData();
33+
WiegandNG();
34+
~WiegandNG();
3835
};
3936

4037
#endif
38+

Source Code/esprfidtool/esprfidtool.ino

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@
4141
#include <DNSServer.h>
4242
#include <ESP8266mDNS.h>
4343

44+
#define DATA0 14
45+
#define DATA1 12
46+
4447
#define LED_BUILTIN 2
4548
#define RESTORE_DEFAULTS_PIN 4 //GPIO 4
4649
int jumperState = 0; //For restoring default settings
@@ -74,7 +77,8 @@ char ftp_password[64];
7477
int ftpenabled;
7578
int ledenabled;
7679
char logname[31];
77-
int bufferlength;
80+
unsigned int bufferlength;
81+
unsigned int rxpacketgap;
7882
int txdelayus;
7983
int txdelayms;
8084
int safemode;
@@ -96,7 +100,7 @@ void LogWiegand(WiegandNG &tempwg) {
96100

97101
unsigned int countedBytes = (countedBits/8);
98102
if ((countedBits % 8)>0) countedBytes++;
99-
unsigned int bitsUsed = countedBytes * 8;
103+
//unsigned int bitsUsed = countedBytes * 8;
100104

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

112-
for (int i=bufferSize-countedBytes; i< bufferSize;i++) {
116+
for (unsigned int i=bufferSize-countedBytes; i< bufferSize;i++) {
113117
unsigned char bufByte=buffer[i];
114118
for(int x=0; x<8;x++) {
115119
if ( (((bufferSize-i) *8)-x) <= countedBits) {
@@ -713,7 +717,8 @@ void settingsPage()
713717
"<small>Default Buffer Length is 256 bits with an allowed range of 52-4096 bits."
714718
"<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."
715719
"<br>Changing these settings may result in unstable performance.</small><br>"
716-
"Wiegand Buffer Length: <input type=\"number\" name=\"bufferlength\" value=\"")+bufferlength+F("\" maxlength=\"30\" size=\"31\" min=\"52\" max=\"4096\"> bit(s)<br>"
720+
"Wiegand RX Buffer Length: <input type=\"number\" name=\"bufferlength\" value=\"")+bufferlength+F("\" maxlength=\"30\" size=\"31\" min=\"52\" max=\"4096\"> bit(s)<br>"
721+
"Wiegand RX Packet Length: <input type=\"number\" name=\"rxpacketgap\" value=\"")+rxpacketgap+F("\" maxlength=\"30\" size=\"31\" min=\"1\" max=\"4096\"> millisecond(s)<br>"
717722
"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>"
718723
"Experimental TX Wiegand Data Interval: <input type=\"number\" name=\"txdelayms\" value=\"")+txdelayms+F("\" maxlength=\"30\" size=\"31\" min=\"0\" max=\"1000\"> millisecond(s)<br>"
719724
"<hr>"
@@ -773,6 +778,7 @@ void handleSubmitSettings()
773778
ledenabled = server.arg("ledenabled").toInt();
774779
server.arg("logname").toCharArray(logname, 31);
775780
bufferlength = server.arg("bufferlength").toInt();
781+
rxpacketgap = server.arg("rxpacketgap").toInt();
776782
txdelayus = server.arg("txdelayus").toInt();
777783
txdelayms = server.arg("txdelayms").toInt();
778784
safemode = server.arg("safemode").toInt();
@@ -811,6 +817,7 @@ bool loadDefaults() {
811817
json["ledenabled"] = "1";
812818
json["logname"] = "log.txt";
813819
json["bufferlength"] = "256";
820+
json["rxpacketgap"] = "15";
814821
json["txdelayus"] = "40";
815822
json["txdelayms"] = "2";
816823
json["safemode"] = "0";
@@ -866,6 +873,7 @@ bool loadConfig() {
866873
ledenabled = json["ledenabled"];
867874
strcpy(logname, (const char*)json["logname"]);
868875
bufferlength = json["bufferlength"];
876+
rxpacketgap = json["rxpacketgap"];
869877
txdelayus = json["txdelayus"];
870878
txdelayms = json["txdelayms"];
871879
safemode = json["safemode"];
@@ -948,6 +956,7 @@ bool saveConfig() {
948956
json["ledenabled"] = ledenabled;
949957
json["logname"] = logname;
950958
json["bufferlength"] = bufferlength;
959+
json["rxpacketgap"] = rxpacketgap;
951960
json["txdelayus"] = txdelayus;
952961
json["txdelayms"] = txdelayms;
953962
json["safemode"] = safemode;
@@ -1033,7 +1042,7 @@ void setup() {
10331042

10341043
loadConfig();
10351044

1036-
if(!wg.begin(bufferlength)) {
1045+
if(!wg.begin(DATA0,DATA1,bufferlength,rxpacketgap)) {
10371046
Serial.println(F("Could not begin Wiegand logging,"));
10381047
Serial.println(F("Out of memory!"));
10391048
}

Source Code/esprfidtool/version.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
String version = "1.1.9";
1+
String version = "1.2.0";
22
String APIversion = "1.0.1";

0 commit comments

Comments
 (0)