This library reads Wiegand, saving it into a buffer, calling a callback function after it is full or a timeout is reached.

Dependents:   mbed-os-wiegand-example

This library reads Wiegand, saving it into a buffer, calling a callback function after it is full or a timeout is reached. Data can be extracted as :

  • Raw
  • Base 10 Integer
  • Base 10 String
  • Base 16 String

Always call reset after saving Wiegand data.

It automatically calibrates the timeout during the first reading unless specified.

Timeout can be set using setTimout, units are in milliseconds (first call stopCalibrating()).

EventQueue is required for deferring from ISR, it is possible to dispatch the EventQueue from the mainQueue or any other thread.

Revision:
2:2c72a6b13593
Parent:
0:4929608f96d0
Child:
3:ea5e0ab156b5
--- a/wiegand.cpp	Sat Apr 27 23:53:14 2019 +0000
+++ b/wiegand.cpp	Wed May 08 18:08:18 2019 +0000
@@ -1,7 +1,7 @@
 #include "wiegand.h"
 
-Wiegand::Wiegand(PinName d0, PinName d1, EventQueue* eventQueue, Callback<void(void)> wiegandCallback, unsigned char bits) :  
-_d0(d0), _d1(d1), _eventQueue(eventQueue), _callback(wiegandCallback), _bits(bits){   
+Wiegand::Wiegand(PinName d0, PinName d1, EventQueue* eventQueue, unsigned char bits, unsigned char id) :  
+_d0(d0), _d1(d1), _eventQueue(eventQueue), _bits(bits), _id(id){   
     _bufferSize = (_bits/8);
     if((_bits % 8) >0) 
         _bufferSize++;
@@ -27,6 +27,10 @@
     delete[] _interPulseGapBuffer;
 }
 
+void Wiegand::attach(Callback<void(Wiegand *WiegandObj)> wiegandCallback){
+    _callback = wiegandCallback;
+}
+
 void Wiegand::calibrateTimeout(void){
     unsigned int interPulseGapAvg = 0;
     for( int i = 1; i < _bits; i++)
@@ -37,7 +41,7 @@
 
 void Wiegand::_data0ISR(void){
     _timeout.detach();
-    if ( _autoCalibration){
+    if (_autoCalibration){
         _interPulseGapTimer.stop();
         _interPulseGapBuffer[_bitsRead] = _interPulseGapTimer.read_us();
         _interPulseGapTimer.reset();
@@ -46,7 +50,7 @@
     _shift_left(_buffer,_bufferSize,1);    // shift 0 into buffer
     if (_bitsRead == _bits)
         _onWiegandISR();
-    else 
+    else
         _timeout.attach_us(callback(this, &Wiegand::_onWiegandISR), _transmissionTimeout);
 }
 
@@ -56,15 +60,14 @@
 
 void Wiegand::_data1ISR(void){
     _timeout.detach();
-    if ( _autoCalibration){           
+    if (_autoCalibration){           
         _interPulseGapTimer.stop();
         _interPulseGapBuffer[_bitsRead] = _interPulseGapTimer.read_us();
         _interPulseGapTimer.reset();
     }
-    _bitsRead++;
     _shift_left(_buffer,_bufferSize,1);
     _buffer[_bufferSize-1] |=1; 
-    _timeout.attach_us(callback(this, &Wiegand::_onWiegandISR), _transmissionTimeout);
+    _bitsRead++;
     if (_bitsRead == _bits)
         _onWiegandISR();
     else 
@@ -125,6 +128,10 @@
     delete[] hexadecimalNumber;
 }
 
+unsigned char Wiegand::getId(){
+    return _id;
+}
+
 uint8_t * Wiegand::getRaw(void){
     return _buffer;
 } 
@@ -160,7 +167,7 @@
         stopCalibrating();
         calibrateTimeout();
     }
-    _callback();
+    _callback(this);
 }
 
 void Wiegand::reset(void){