arduino_rfid_controller / wiegand_rfid.h

#ifndef WIEGAND_RFID_H
#define WIEGAND_RFID_H
/*
37 bit format
EXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXO
E = even parity bit
O = odd parity bit
X = data
*/
#include "WProgram.h"
#include "bitarray.h"

/* This is the max number of bits we accept from a reader */
#define MAX_BIT_LENGTH 96



#define WIEGAND_RFID(o,pinDataZero,pinDataOne) WiegandRFID o(pinDataZero,pinDataOne); \
                            void wiegandRfidDateZeroInterrupt_##o(void) { o.DataZeroInterrupt();} \
                            void wiegandRfidDateOneInterrupt_##o(void) { o.DataOneInterrupt();} \

#define WIEGAND_RFID_INIT(o) PCattachInterrupt( o._pinDataZero, wiegandRfidDateZeroInterrupt_##o, CHANGE); \
                             PCattachInterrupt( o._pinDataOne, wiegandRfidDateOneInterrupt_##o, CHANGE); \
                             o.reset();

typedef unsigned char byte;
typedef unsigned int uint;

#define BYTES_FOR(nbits) ((nbits / 8)+1)

template <uint NBITS>
class BitArray {
	byte data[BYTES_FOR(NBITS)];
        long highest;
private:
        bool _get(uint i) {
                uint byte = i / 8;
		uint bit = i % 8;
		
		return (data[byte] & (1 << bit)) > 0;
	}

public:
	BitArray() {
		for (uint i = 0; i < BYTES_FOR(NBITS); i++) {
			data[i] = 0;
		}
                highest = 0;
	}
	void clear() {
  		for (uint i = 0; i < BYTES_FOR(NBITS); i++) {
			data[i] = 0;
		}
                highest = 0;
        }
	bool get(uint i) {
                return _get(i);
	}
	void set(uint i, bool v) {
                uint byte = i / 8;
		uint bit = i % 8;
		
		data[byte] &= ~(1 << bit);
		data[byte] |= (v << bit);
                if(i > highest) highest = i;
	}
	long length()  { return highest+1; }
	void to_string(char* buffer) {
                for (int i = 0; i < NBITS; i++) { buffer[i] = '0'; }
		for (int i = 0; i < length(); i++) {
			buffer[i+NBITS-highest] = get(i)? '1' : '0';
		}
	}
};


class WiegandRFID
{
  public:
    WiegandRFID(int pinDataZero,int pinDataOne);
    int getTag();
    void DataZeroInterrupt();
    void DataOneInterrupt(); 
    void reset();   
    int _pinDataZero;
    int _pinDataOne;
  private:
    // has to be a long - ints are 16 bits on this platform!
    volatile long lastDataTime;
    volatile int readerCount;
    //volatile byte reader[MAX_BIT_LENGTH];
    BitArray<MAX_BIT_LENGTH> bits;
    void resetInterruptPin(int i);
};

#endif
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.