A library to read HID RFID cards through the wiegand protocol. This was tested with the HID ProxPoint Plus 6005BG00.

Dependents:   HID_ProxCard_Wiegand_Example

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Wiegand.cpp Source File

Wiegand.cpp

00001 #include "Wiegand.h"
00002 #include "mbed.h"
00003 
00004 Wiegand::Wiegand(PinName pdata0, PinName pdata1, PinName pHold, void (*onCardRead)()) :  _data0ISR(pdata0), _data1ISR(pdata1), _hold(pHold) {
00005     _onCardRead = onCardRead;
00006     /*
00007         stop reader from sending us any data
00008         and ensure after this block that no data through 
00009         wiegand gets sent
00010     */
00011     _hold = 1;
00012     wait_ms(250); 
00013     _sizeOfBuffer = sizeof(_buffer)/sizeof(*_buffer);
00014     _resetBuffer();
00015     
00016     /* Attach our interrupts, and start the ISRs */
00017     _attachInterrupts();
00018     _hold = 0; //start sending data
00019 }
00020 
00021 /*
00022     Check if we've read all card data
00023 */
00024 void Wiegand::doEvents(void) {
00025     if(_bitsRead > 0 && _lastISR.read_ms() > 5) {
00026         _onCardRead();
00027         _resetBuffer();
00028         _hold = 0;   
00029     }
00030 }
00031     
00032 /*
00033     return number of bits currently read
00034 */
00035 uint8_t Wiegand::bitsRead(void) {
00036     return _bitsRead;
00037 }
00038     
00039 /*
00040     Returns the bits read from in [start,end] (Inclusive).
00041     if [start,end] not in range of the bits read, 0 is returned
00042 */
00043 uint64_t Wiegand::getBits(uint8_t start, uint8_t end) {
00044     //make sure the start and end indexes don't over index our read bit buffer
00045     if(!(start < _bitsRead) || !(end < _bitsRead)) {
00046         return 0;
00047     }
00048     uint64_t output = 0;
00049     for(int i = start; i <= end; ++i) {
00050         output <<=1;
00051         output |= 0x1 & _buffer[i]; 
00052     }
00053     return output;
00054 }
00055 
00056 /*
00057     Resets the buffer to its intial state (0), and _bitsRead = 0
00058 */
00059 void Wiegand::_resetBuffer(void) {
00060      for(int i = 0, l = _sizeOfBuffer; i <l; ++i) {  _buffer[i] = 0; }
00061     _bitsRead = 0;
00062 }
00063 
00064 /*
00065     Attach ISRs to the rising edge
00066 */
00067 void Wiegand::_attachInterrupts(void) {
00068     _data0ISR.rise(this, &Wiegand::_data0ISRAction);
00069     _data1ISR.rise(this, &Wiegand::_data1ISRAction);
00070 }
00071 
00072 /*
00073     Data0ISR Routine, called by rising edge of data0
00074 */
00075 void Wiegand::_data0ISRAction(void) {
00076     _lastISR.reset();
00077     _lastISR.start();
00078     _hold = 1;
00079     if(_bitsRead < _sizeOfBuffer) {
00080         _buffer[_bitsRead++] = 0;
00081     }
00082 }
00083 
00084 /*
00085     Data1ISR Routine, called by rising edge of data1
00086 */
00087 void Wiegand::_data1ISRAction(void) {
00088     _lastISR.reset();
00089     _lastISR.start();
00090     _hold = 1;
00091     if(_bitsRead < _sizeOfBuffer) {
00092         _buffer[_bitsRead++] = 1;
00093     }
00094 }