Wiegand card reader driver library
Fork of CardReader by
Revision 1:47807713b37d, committed 2017-09-25
- Comitter:
- acesrobertm
- Date:
- Mon Sep 25 18:58:39 2017 +0000
- Parent:
- 0:b468573740b5
- Commit message:
- Modified for my own purposes, fixed bit order bugs.
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 |
diff -r b468573740b5 -r 47807713b37d Reader.h --- a/Reader.h Wed Jul 18 01:54:58 2012 +0000 +++ b/Reader.h Mon Sep 25 18:58:39 2017 +0000 @@ -10,10 +10,13 @@ * 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_ +#define KEYPAD_DIGIT_LIMIT 5 +#define KEYPAD_TIMEOUT_MS 1500 + class Reader { public: @@ -21,6 +24,7 @@ : mCard(0) , mFacility(0) , mSite(0) + , mDigits(0) , mBits(0) , mBitsCount(0) , mbIsValid(false) @@ -30,7 +34,6 @@ 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; }; @@ -42,9 +45,10 @@ uint8_t bitCount() { return mLastBitsCount; }; protected: - volatile uint32_t mCard; + volatile uint16_t mCard; volatile uint32_t mFacility; volatile uint16_t mSite; + volatile uint8_t mDigits; volatile uint64_t mLastBits; volatile uint8_t mLastBitsCount; @@ -55,55 +59,86 @@ volatile bool mbIsNew; bool mbInhibited; + Timeout mKeypadTimer; + + void keypadTimerRestart() + { + mKeypadTimer.detach(); + mKeypadTimer.attach_us(callback(this, &Reader::keypadTimeout), KEYPAD_TIMEOUT_MS * 1000); + } + + void keypadTimeout() + { + mDigits = 0; + mbIsValid = true; + codeSubmit(); + } void shiftIn(uint8_t bit) { - if(bit) - mBits |= ((uint64_t)1 << mBitsCount); - mBitsCount ++; + mBits <<= 1; + if (bit) + { + mBits |= (uint64_t)1; + } + mBitsCount++; } + // param first: The index of the starting bit, counting from the most significant bit. (1 based) uint32_t bits32(uint8_t first, uint8_t qty) { uint32_t val = 0; - uint8_t last = first + qty; + uint8_t start = mBitsCount - first; + uint8_t end = start - qty; - for(uint8_t i=first; i<last; i++) + for(uint8_t i = start; i > end; i--) { - if(val) - val <<= 1; - if(mBits & (1<<i)) + val <<= 1; + + if (mBits & (1 << i)) + { val |= 1; + } } return val; } - virtual bool decode() - { bool bIsValid = false; + bool decode() + { + bool bIsValid = false; - mSite = 0; - if(mBitsCount == 26) + if (mBitsCount == 4) // Key press { - mFacility = bits32(1,8); - mCard = bits32(9,16); - bIsValid = true; + keypadTimerRestart(); + + if (mDigits == 0) // Reset the card code for the first keypress + { + mCard = 0; + } + + if (mBits < 10) // Number key + { + mCard *= 10; + mCard += mBits; + mDigits += 1; + } + else if (mBits == 11 || mDigits == KEYPAD_DIGIT_LIMIT) // # Key (Enter) + { + bIsValid = true; + mKeypadTimer.detach(); + mDigits = 0; + } } - else if(mBitsCount == 34) + else if (mBitsCount >= 26) { - mFacility = bits32(1,16); - mCard = bits32(17,16); - bIsValid = true; - } - else if(mBitsCount == 35) - { - mFacility = bits32(2,12); - mCard = bits32(14,20); + mCard = bits32(10, 16); bIsValid = true; } else { mFacility = 0; mCard = 0; + mDigits = 0; } mLastBits = mBits; @@ -116,11 +151,17 @@ { // decode mBits into mCard, mFacility, and mSite mbIsValid = !mbInhibited && decode(); + + codeSubmit(); + } + + void codeSubmit() + { // reset mBits and mBitsCount for next operation mBits = 0; mBitsCount = 0; - mbIsNew = !mbInhibited; - }; + mbIsNew = mbIsValid; + } }; #endif \ No newline at end of file
diff -r b468573740b5 -r 47807713b37d ReaderWiegand.h --- a/ReaderWiegand.h Wed Jul 18 01:54:58 2012 +0000 +++ b/ReaderWiegand.h Mon Sep 25 18:58:39 2017 +0000 @@ -25,10 +25,10 @@ { 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); + mData0Irq.fall(callback(this, &ReaderWiegand::data0Fall)); + mData1Irq.fall(callback(this, &ReaderWiegand::data1Fall)); + mData0Irq.rise(callback(this, &ReaderWiegand::data01Rise)); + mData1Irq.rise(callback(this, &ReaderWiegand::data01Rise)); }; protected: InterruptIn mData0Irq; @@ -36,7 +36,7 @@ Timeout mTimer; void swiped() { Reader::swiped(); }; - void timerRestart() { mTimer.detach(); mTimer.attach_us(this,&ReaderWiegand::swiped,50*1000); }; + void timerRestart() { mTimer.detach(); mTimer.attach_us(callback(this, &ReaderWiegand::swiped), 50 * 1000); }; void data0Fall() { shiftIn(0); timerRestart(); }; void data1Fall() { shiftIn(1); timerRestart(); };