Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Revision 0:b468573740b5, committed 2012-07-18
- 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