|
2 | 2 |
|
3 | 3 | // pcintbranch
|
4 | 4 |
|
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 |
9 | 9 |
|
10 | 10 |
|
11 | 11 | void shift_left(volatile unsigned char *ar, int size, int shift)
|
12 | 12 | {
|
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 | + } |
27 | 27 | }
|
28 | 28 |
|
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 |
34 | 34 | }
|
35 | 35 |
|
36 | 36 | void WiegandNG::pause() {
|
37 |
| - noInterrupts(); // disable interrupt so that user can process data |
| 37 | + noInterrupts(); // disable interrupt so that user can process data |
38 | 38 | }
|
39 | 39 |
|
40 | 40 | volatile unsigned char * WiegandNG::getRawData() {
|
41 |
| - return _buffer; // return pointer of the buffer |
| 41 | + return _buffer; // return pointer of the buffer |
42 | 42 | }
|
43 | 43 |
|
44 | 44 | unsigned int WiegandNG::getPacketGap() {
|
45 |
| - return _packetGap; |
| 45 | + return _packetGap; |
46 | 46 | }
|
47 | 47 |
|
48 | 48 | unsigned int WiegandNG::getBitAllocated() {
|
49 |
| - return _bitAllocated; |
| 49 | + return _bitAllocated; |
50 | 50 | }
|
51 | 51 |
|
52 | 52 | unsigned int WiegandNG::getBitCounted() {
|
53 |
| - return _bitCounted; |
| 53 | + return _bitCounted; |
54 | 54 | }
|
55 | 55 |
|
56 | 56 | unsigned int WiegandNG::getBufferSize() {
|
57 |
| - return _bufferSize; |
| 57 | + return _bufferSize; |
58 | 58 | }
|
59 | 59 |
|
60 | 60 | 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; |
73 | 87 | }
|
74 | 88 |
|
75 | 89 | 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 |
79 | 93 | }
|
80 | 94 |
|
81 | 95 | 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 | + } |
90 | 104 | }
|
91 | 105 |
|
92 | 106 | 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; |
94 | 111 | }
|
95 | 112 |
|
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; |
115 | 132 | }
|
116 | 133 |
|
117 | 134 | WiegandNG::WiegandNG() {
|
118 | 135 |
|
119 | 136 | }
|
120 | 137 |
|
121 | 138 | WiegandNG::~WiegandNG() {
|
122 |
| - if (_buffer != NULL) { |
123 |
| - delete [] _buffer; |
124 |
| - } |
| 139 | + if (_buffer != NULL) { |
| 140 | + delete [] _buffer; |
| 141 | + } |
125 | 142 | }
|
126 |
| - |
127 |
| - |
|
0 commit comments