Skip to content

Commit

Permalink
Added contains24 method which checks whether an id is in the database…
Browse files Browse the repository at this point in the history
… based on the low 24-bits.

This is useful when using Wiegand26 readers with full 32 bit ids in the database.
  • Loading branch information
gregington committed Feb 9, 2016
1 parent 4811e08 commit 28a1e7e
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 8 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,18 @@ if (db.contains(10042)) {
} else {
Serial.println("Not in database");
}
```

## contains24
Returns 2 if the low order 24-bits of the specified id match the low order
24 bits of any id in the databse. This us useful if 32 bit ids are stored in
the database and the ids are read using a Wiegand26 reader which returns
24 bits.

```c++
if (db.contains24(10042)) {
Serial.println("In database");
} else {
Serial.println("Not in database");
}
```
24 changes: 21 additions & 3 deletions RfidDb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,29 @@ bool RfidDb::contains(uint32_t id) {
return posOf(id) != -1;
}

// Returns the position of given id in the database or -1
// if the id is not in the database
bool RfidDb::contains24(uint32_t id) {
return posOf24(id) != -1;
}

// Returns the position of the given id in the database or -1 if the id is
// not in the database
int16_t RfidDb::posOf(uint32_t id) {
return posOf(id, 0xFFFFFFFF);
}

// Returns the position of the given id in the database when compared on the
// low 24 bits of the id.
int16_t RfidDb::posOf24(uint32_t id) {
return posOf(id, 0x00FFFFFF);
}

// Returns the position of the given id when compared with ids in the datbase
// after both the database id and the given id are bit masked with the given
// mask
int16_t RfidDb::posOf(uint32_t id, uint32_t mask) {
uint32_t maskedId = id & mask;
for (uint8_t i = 0, n = count(); i < n; i++) {
if (id == getId(i)) {
if (maskedId == (getId(i) & mask)) {
return i;
}
}
Expand Down
8 changes: 8 additions & 0 deletions RfidDb.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,20 @@ class RfidDb {
// Returns whether the database contains the given identifier.
bool contains(uint32_t id);

// Returns whether the database contains an indentifier where the low 24
// bits matches the low 24 bits of the given identifier. This is useful
// when reading from a Wiegand 26 device (that returns a 24 bit id) with
// identifers stored as 32 vit ids.
bool contains24(uint32_t id);

private:
uint16_t _eepromOffset;
uint8_t _maxSize;
uint8_t _maxNameLength;

int16_t posOf(uint32_t id);
int16_t posOf24(uint32_t id);
int16_t posOf(uint32_t id, uint32_t mask);
uint32_t getId(uint8_t pos);
void writeId(uint8_t pos, uint32_t id);
void writeName(uint8_t pos, const char* name);
Expand Down
33 changes: 28 additions & 5 deletions examples/RfidDbTest/RfidDbTest.ino
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@

RfidDb db = RfidDb(4, 0, 4);

uint32_t id1 = 1000000001;
uint32_t id2 = 1000000002;
uint32_t id3 = 1000000003;
uint32_t id4 = 1000000004;
uint32_t id5 = 1000000005;
uint32_t id1 = 0xFFFFFF01;
uint32_t id2 = 0xFFFFFF02;
uint32_t id3 = 0xFFFFFF03;
uint32_t id4 = 0xFFFFFF04;
uint32_t id5 = 0xFFFFFF05;

void setup() {
Serial.begin(9600);
Expand All @@ -25,51 +25,61 @@ void setup() {
Serial.println(db.insert(id1, "AAA"));
dumpState();
dumpContains();
dumpContains24();

Serial.print("Inserting id2 = ");
Serial.println(db.insert(id2, "BBB"));
dumpState();
dumpContains();
dumpContains24();

Serial.print("Inserting id3 = ");
Serial.println(db.insert(id3, "CCC"));
dumpState();
dumpContains();
dumpContains24();

Serial.print("Inserting id4 = ");
Serial.println(db.insert(id4, "DDD"));
dumpState();
dumpContains();
dumpContains24();

Serial.print("Inserting id5 = ");
Serial.println(db.insert(id5, "EEE"));
dumpState();
dumpContains();
dumpContains24();

Serial.println("Removing id5");
db.remove(id5);
dumpState();
dumpContains();
dumpContains24();

Serial.println("Removing id1");
db.remove(id1);
dumpState();
dumpContains();
dumpContains24();

Serial.println("Removing id3");
db.remove(id3);
dumpState();
dumpContains();
dumpContains24();

Serial.println("Removing id2");
db.remove(id2);
dumpState();
dumpContains();
dumpContains24();

Serial.println("Removing id4");
db.remove(id4);
dumpState();
dumpContains();
dumpContains24();
}

void loop() {
Expand Down Expand Up @@ -110,4 +120,17 @@ void dumpContains() {
Serial.println(db.contains(id4));
Serial.print("Contains id5 = ");
Serial.println(db.contains(id5));
}

void dumpContains24() {
Serial.print("Contains (24 bit) id1 = ");
Serial.println(db.contains(id1));
Serial.print("Contains (24 bit) id2 = ");
Serial.println(db.contains(id2));
Serial.print("Contains (24 bit) id3 = ");
Serial.println(db.contains(id3));
Serial.print("Contains (24 bit) id4 = ");
Serial.println(db.contains(id4));
Serial.print("Contains (24 bit) id5 = ");
Serial.println(db.contains(id5));
}

0 comments on commit 28a1e7e

Please sign in to comment.