Wiegand card reader driver library

Import libraryCardReader

Wiegand card reader driver library

This is a Wiegand 2 signal (Data0, Data1) card reader interface library.

Information

Todo - parity checking.

Wiegand technology References

Hobbyist project

Library use example

/*
 *  Wiegand card reader driver library
 *  Copyright (c) 2012 Neal Horman - http://www.wanlink.com
 *  
 *  License: MIT open source (http://opensource.org/licenses/MIT)
 *      Summary;
 *      Use / modify / distribute / publish it how you want and 
 *      if you use it, or don't, you can't hold me liable for how
 *      it does or doesn't work.
 *      If it doesn't work how you want, don't use it, or change
 *      it so that it does work.
 */
 
#include "mbed.h"
#include "ReaderWiegand.h"

Serial gSerial(USBTX, USBRX);
ReaderWiegand gReader(p10,p11);

int main()
{
    gSerial.printf("Ready\r\n");
    
    while(1)
    {
        if(gReader.isNew())
        {   uint8_t bq = gReader.bitCount();
            uint64_t bits = gReader.bits();
        
            for(uint8_t i=0; i<bq; i++)
                gSerial.printf("%c",'0' + ( ( bits & ( (uint64_t)1 << i ) ) != 0));
            gSerial.printf(" bq: %u h: 0x%llX ",bq,bits);
            if(gReader.isValid())
                gSerial.printf("f: %lu, c: %lu\r\n",gReader.facility(),gReader.card());
            else
                gSerial.printf("Unknown format\r\n");

            gReader.old();
        }
    }
}

Files at this revision

API Documentation at this revision

Comitter:
nkhorman
Date:
Wed Jul 18 01:54:58 2012 +0000
Commit message:
Add copyright and licensing.; Move to library.

Changed in this revision

Reader.h Show annotated file Show diff for this revision Revisions of this file
ReaderWiegand.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Reader.h	Wed Jul 18 01:54:58 2012 +0000
@@ -0,0 +1,126 @@
+/*
+ *  Card / Proximity reader driver library
+ *  Copyright (c) 2012 Neal Horman - http://www.wanlink.com
+ *  
+ *  License: MIT open source (http://opensource.org/licenses/MIT)
+ *      Summary;
+ *      Use / modify / distribute / publish it how you want and 
+ *      if you use it, or don't, you can't hold me liable for how
+ *      it does or doesn't work.
+ *      If it doesn't work how you want, don't use it, or change
+ *      it so that it does work.
+ */
+
+#ifndef _READER_H_
+#define _READER_H_
+
+class Reader
+{
+public:
+    Reader()
+        : mCard(0)
+        , mFacility(0)
+        , mSite(0)
+        , mBits(0)
+        , mBitsCount(0)
+        , mbIsValid(false)
+        , mbIsNew(false)
+        , mbInhibited(false)
+        {};
+    
+    uint32_t card() { return mCard; };
+    uint32_t facility() { return mFacility; };
+    uint16_t site() { return mSite; };
+    bool isValid() { return mbIsValid; };
+    bool isNew() { return mbIsNew; };
+    void old() { mbIsNew = false; };
+    
+    bool isInhibited() { return mbInhibited; };
+    void inhibit(bool bInhibit = true) { mbInhibited = bInhibit; };
+    
+    uint64_t bits() { return mLastBits; };
+    uint8_t bitCount() { return mLastBitsCount; };
+
+protected:
+    volatile uint32_t mCard;
+    volatile uint32_t mFacility;
+    volatile uint16_t mSite;
+    volatile uint64_t mLastBits;
+    volatile uint8_t mLastBitsCount;
+    
+    volatile uint64_t mBits;
+    volatile uint8_t mBitsCount;
+    
+    volatile bool mbIsValid;
+    volatile bool mbIsNew;
+    
+    bool mbInhibited;
+
+    void shiftIn(uint8_t bit)
+    {
+        if(bit)
+            mBits |= ((uint64_t)1 << mBitsCount);
+        mBitsCount ++;
+    }
+    
+    uint32_t bits32(uint8_t first, uint8_t qty)
+    {   uint32_t val = 0;
+        uint8_t last = first + qty;
+    
+        for(uint8_t i=first; i<last; i++)
+        {
+            if(val)
+                val <<= 1;
+            if(mBits & (1<<i))
+                val |= 1;
+        }
+        
+        return val;
+    }
+    
+    virtual bool decode()
+    {   bool bIsValid = false;
+        
+        mSite = 0;
+        if(mBitsCount == 26)
+        {
+            mFacility = bits32(1,8);
+            mCard = bits32(9,16);
+            bIsValid = true;
+        }
+        else if(mBitsCount == 34)
+        {
+            mFacility = bits32(1,16);
+            mCard = bits32(17,16);
+            bIsValid = true;
+        }
+        else if(mBitsCount == 35)
+        {
+            mFacility = bits32(2,12);
+            mCard = bits32(14,20);
+            bIsValid = true;
+        }
+        else
+        {
+            mFacility = 0;
+            mCard = 0;
+        }
+        
+        mLastBits = mBits;
+        mLastBitsCount = mBitsCount;
+        
+        return bIsValid;
+    }
+    
+    void swiped()
+    {
+        // decode mBits into mCard, mFacility, and mSite
+        mbIsValid = !mbInhibited && decode();
+        // reset mBits and mBitsCount for next operation
+        mBits = 0;
+        mBitsCount = 0;
+        mbIsNew = !mbInhibited;
+    };
+};
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ReaderWiegand.h	Wed Jul 18 01:54:58 2012 +0000
@@ -0,0 +1,46 @@
+/*
+ *  Wiegand card reader driver library
+ *  Copyright (c) 2012 Neal Horman - http://www.wanlink.com
+ *  
+ *  License: MIT open source (http://opensource.org/licenses/MIT)
+ *      Summary;
+ *      Use / modify / distribute / publish it how you want and 
+ *      if you use it, or don't, you can't hold me liable for how
+ *      it does or doesn't work.
+ *      If it doesn't work how you want, don't use it, or change
+ *      it so that it does work.
+ */
+ 
+ 
+#ifndef _READERWIEGAND_H_
+#define _READERWIEGAND_H_
+
+#include "Reader.h"
+
+// Use interupt driven inputs to gather the card data
+class ReaderWiegand : public Reader
+{
+public:
+    ReaderWiegand(PinName data0, PinName data1) : Reader(), mData0Irq(data0), mData1Irq(data1)
+    {
+        mData0Irq.mode(PullUp);
+        mData1Irq.mode(PullUp);
+        mData0Irq.fall(this,&ReaderWiegand::data0Fall);
+        mData1Irq.fall(this,&ReaderWiegand::data1Fall);
+        mData0Irq.rise(this,&ReaderWiegand::data01Rise);
+        mData1Irq.rise(this,&ReaderWiegand::data01Rise);
+    };
+protected:
+    InterruptIn mData0Irq;
+    InterruptIn mData1Irq;
+    Timeout mTimer;
+    void swiped() { Reader::swiped(); };
+    
+    void timerRestart() { mTimer.detach(); mTimer.attach_us(this,&ReaderWiegand::swiped,50*1000); };
+    
+    void data0Fall() { shiftIn(0); timerRestart(); };
+    void data1Fall() { shiftIn(1); timerRestart(); };
+    void data01Rise() { timerRestart(); };
+};
+
+#endif
\ No newline at end of file