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.

Files at this revision

API Documentation at this revision

Comitter:
goymame
Date:
Thu May 16 22:12:19 2019 +0000
Parent:
2:2c72a6b13593
Commit message:
New update bits function

Changed in this revision

wiegand.cpp Show annotated file Show diff for this revision Revisions of this file
wiegand.h Show annotated file Show diff for this revision Revisions of this file
--- a/wiegand.cpp	Wed May 08 18:08:18 2019 +0000
+++ b/wiegand.cpp	Thu May 16 22:12:19 2019 +0000
@@ -1,25 +1,11 @@
 #include "wiegand.h"
 
-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++;
-    _hexDigits = (_bits/4);
-    if((_bits % 4) >0) 
-        _hexDigits++;
-    unsigned long long int maxNumber = 0;
-    for(int i = 0; i < _bits; i++)
-        maxNumber +=  pow((long double)2,i);
-    _decDigits = 0;
-    do { maxNumber /= 10; _decDigits++; } while(maxNumber != 0);
+Wiegand::Wiegand(PinName d0, PinName d1, EventQueue* eventQueue, unsigned char id) :  
+_d0(d0), _d1(d1), _eventQueue(eventQueue), _id(id){   
     _transmissionTimeout = 100000;
-    
-    _buffer = new uint8_t [_bufferSize];
     _d0.fall(callback(this, &Wiegand::_data0ISR));
     _d1.fall(callback(this, &Wiegand::_data1ISR));
     startCalibrating();
-    reset();
 }
 
 Wiegand::~Wiegand(){
@@ -47,7 +33,7 @@
         _interPulseGapTimer.reset();
     }
     _bitsRead++;
-    _shift_left(_buffer,_bufferSize,1);    // shift 0 into buffer
+    _shift_left(_buffer,_bufferSize,1);    
     if (_bitsRead == _bits)
         _onWiegandISR();
     else
@@ -116,7 +102,6 @@
         hexadecimalNumber[--i] = temp;
         quotient = quotient / 16;
     }
-    //shift array left if string is smaller than array
     if (counter < _hexDigits){
         unsigned char difference = _hexDigits - counter;
         for(int j = 0 ; j < counter; j++){
@@ -175,15 +160,32 @@
     _bitsRead = 0;
 }
 
+void Wiegand::setBits(unsigned char bits){
+    _bits = bits;
+    _bufferSize = (_bits/8);
+    if((_bits % 8) >0) 
+        _bufferSize++;
+    _hexDigits = (_bits/4);
+    if((_bits % 4) >0) 
+        _hexDigits++;
+    unsigned long long int maxNumber = 0;
+    for(int i = 0; i < _bits; i++)
+        maxNumber +=  pow((long double)2,i);
+    _decDigits = 0;
+    do { maxNumber /= 10; _decDigits++; } while(maxNumber != 0);
+    _buffer = new uint8_t [_bufferSize];
+    reset();
+}
+
 void Wiegand::setTimeout(unsigned int time){
     _transmissionTimeout = time * 1000;
 }
 
 void Wiegand::_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.
+    while (shift--) {                           
+        int carry = 0;                              
         int lastElement = size-1;
-        for (int i = 0; i < size; i++) {            // for each element of the array, from low byte to high byte
+        for (int i = 0; i < size; i++) {            
             if (i!=lastElement) {
                 carry = (ar[i+1] & 0x80) ? 1 : 0;
                 ar[i] = carry | (ar[i]<<1);
--- a/wiegand.h	Wed May 08 18:08:18 2019 +0000
+++ b/wiegand.h	Thu May 16 22:12:19 2019 +0000
@@ -69,7 +69,7 @@
     void _shift_left(volatile unsigned char *ar, int size, int shift);
 
 public:
-    Wiegand(PinName d0, PinName d1, EventQueue* eventQueue, unsigned char bits, unsigned char i=0);
+    Wiegand(PinName d0, PinName d1, EventQueue* eventQueue, unsigned char i=0);
     ~Wiegand();
 
     void            attach(Callback<void(Wiegand *WiegandObj)> wiegandCallback);
@@ -84,6 +84,7 @@
     unsigned int    getTimeout(void);
     void            reset(void);
     void            setTimeout(unsigned int time);
+    void            setBits(unsigned char bits);
     void            startCalibrating(void);
     void            stopCalibrating(void);
 };