Skip to content

Commit f7f4a5d

Browse files
committed
🎉 Initial commit
0 parents  commit f7f4a5d

File tree

6 files changed

+278
-0
lines changed

6 files changed

+278
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.DS_Store

README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Arduino - EEPROM Key Database
2+
This library is a wrapper around EEPROM as a key storing database. The libraries initial purpose was to be used for RFID HEX ID storing and verification.
3+
4+
## Methods
5+
``int KeyDB::count()`` _Returns key count from storage._
6+
7+
``void KeyDB::loadKeys()`` _Loads keys from storage into array._
8+
9+
``void KeyDB::saveKeys()`` _Saves array keys to storage._
10+
11+
``void KeyDB::addKey(String key)`` _Adds key to array and storage._
12+
13+
``void KeyDB::removeKey(String key)`` _Removes key from array and shift indexes._
14+
> Because of heavy read/write actions when removing you must manually call KeyDB::saveKeys() to save changes.
15+
16+
``bool KeyDB::keyExists(String key)`` _Checks if key exists in array._
17+
18+
``String KeyDB::keyLengthen(String key)`` _Forces a key to a fixed defined length._
19+
20+
## Variables
21+
``#define DB_KEY_MAX 12`` _The maximum amount of keys in the database._
22+
23+
``#define DB_KEY_LENGTH 8`` _The char length of the key._
24+
25+
``String KeyDB::keys[DB_KEY_MAX] = {""}`` _The main array of keys._
26+
27+
## Usage
28+
> **BEFORE USE:** The initial count must be set by doing: ``#include <EEPROM.h>`` & ``EEPROM.write(0, 0);``
29+
```cpp
30+
// Include the KeyDB file.
31+
#include "KeyDB.h"
32+
33+
// Create a new KeyDB object.
34+
KeyDB database;
35+
36+
void setup() {
37+
Serial.begin(9600);
38+
39+
// Add new key to database.
40+
database.addKey("ABC12345");
41+
42+
// Check if it exists in storage.
43+
if (database.keyExists("ABC12345"))
44+
Serial.println("Valid key!");
45+
}
46+
```

keywords.txt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#######################################
2+
# KeyDB Syntax Coloring
3+
#######################################
4+
5+
#######################################
6+
# Datatypes (KEYWORD1)
7+
#######################################
8+
9+
KeyDB KEYWORD1 KeyDB
10+
11+
#######################################
12+
# Methods and Functions (KEYWORD2)
13+
#######################################
14+
15+
count KEYWORD2
16+
addKey KEYWORD2
17+
loadKeys KEYWORD2
18+
saveKeys KEYWORD2
19+
removeKey KEYWORD2
20+
keyExists KEYWORD2
21+
keyLengthen KEYWORD2
22+
23+
#######################################
24+
# Constants (LITERAL1)
25+
#######################################

library.properties

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
name=KeyDB
2+
version=1.0
3+
author=Andreas <andreas@zapsterstudios.com>
4+
maintainer=Andreas <andreas@zapsterstudios.com>
5+
sentence=Wrapper around EEPROM as a key storing database.
6+
paragraph=This library is a wrapper around EEPROM as a key storing database. The libraries initial purpose was to be used for RFID HEX ID storing and verification.
7+
category=Data Storage
8+
url=https://github.com/zapsterstudios/Arduino-KeyDB
9+
architectures=*
10+
includes=KeyDB.h

src/KeyDB.cpp

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
// Include header file.
2+
#include "KeyDB.h"
3+
4+
/**
5+
* Load all keys into array on init.
6+
*/
7+
KeyDB::KeyDB() {
8+
this->loadKeys();
9+
}
10+
11+
/**
12+
* Returns key count from storage.
13+
*
14+
* @return int
15+
*/
16+
int KeyDB::count() {
17+
return EEPROM.read(0);
18+
}
19+
20+
/**
21+
* Loads keys from storage into array.
22+
*/
23+
void KeyDB::loadKeys() {
24+
// Read key count from storage.
25+
int count = this->count();
26+
27+
// Loop through key count.
28+
int position = 1;
29+
for (int i = 0; i < count; i++) {
30+
// Loop through the key char length.
31+
for(int k = 0; k < DB_KEY_LENGTH; k++) {
32+
// Add read char to key string.
33+
char readChar = char(EEPROM.read(position));
34+
this->keys[i].concat(readChar);
35+
36+
// Increment position.
37+
position++;
38+
}
39+
}
40+
}
41+
42+
/**
43+
* Saves array keys to storage.
44+
*/
45+
void KeyDB::saveKeys() {
46+
// Loop through the key list.
47+
int keys = 0;
48+
int position = 1;
49+
for (int i = 0; i < DB_KEY_MAX; i++) {
50+
// Break out of array if empty.
51+
if (this->keys[i] == "")
52+
break;
53+
54+
// Loop through the key char length.
55+
for(int k = 0; k < DB_KEY_LENGTH; k++) {
56+
// Write key char to storage.
57+
EEPROM.update(position, this->keys[i][k]);
58+
59+
// Increment position.
60+
position++;
61+
}
62+
63+
// Increment keys.
64+
keys++;
65+
}
66+
67+
// Update key count in storage.
68+
EEPROM.write(0, keys);
69+
}
70+
71+
/**
72+
* Adds key to array and storage.
73+
*
74+
* @param String key
75+
*/
76+
void KeyDB::addKey(String key) {
77+
// Make key fixed length.
78+
key = this->keyLengthen(key);
79+
80+
// Set index to new value.
81+
int keys = this->count();
82+
this->keys[keys] = key;
83+
84+
// Find position for next input.
85+
int position = (keys * DB_KEY_LENGTH) + 1;
86+
87+
// Loop through the key char length.
88+
for(int k = 0; k < DB_KEY_LENGTH; k++) {
89+
// Write key char to storage.
90+
EEPROM.update(position, this->keys[keys][k]);
91+
92+
// Increment position.
93+
position++;
94+
}
95+
96+
// Update key count in storage.
97+
EEPROM.write(0, keys + 1);
98+
}
99+
100+
/**
101+
* Removes key from array and shift indexes.
102+
*
103+
* @param String key
104+
*/
105+
void KeyDB::removeKey(String key) {
106+
// Make key fixed length.
107+
key = this->keyLengthen(key);
108+
109+
// Loop through keys.
110+
int shifting = 0;
111+
for (int i = 0; i < DB_KEY_MAX; i++) {
112+
// Shift indexes if shifting.
113+
if (shifting)
114+
this->keys[i - 1] = this->keys[i];
115+
116+
// Break out of loop if empty.
117+
if (this->keys[i] == "")
118+
break;
119+
120+
// Continue if not same key.
121+
if (this->keys[i] != key)
122+
continue;
123+
124+
// Start shifting indexes.
125+
shifting = 1;
126+
}
127+
}
128+
129+
/**
130+
* Checks if key exists in array.
131+
*
132+
* @param String key
133+
* @return bool
134+
*/
135+
bool KeyDB::keyExists(String key) {
136+
// Make key fixed length.
137+
key = this->keyLengthen(key);
138+
139+
// Loop through keys.
140+
for (int i = 0; i < DB_KEY_MAX; i++) {
141+
// Return out of array if empty.
142+
if (this->keys[i] == "")
143+
return true;
144+
145+
// Return found status if same key.
146+
if (this->keys[i] == key) {
147+
return false;
148+
}
149+
}
150+
}
151+
152+
/**
153+
* Forces a key to a fixed defined length.
154+
*
155+
* @param String key
156+
* @return String
157+
*/
158+
String KeyDB::keyLengthen(String key) {
159+
// Add zeros untill correct char length.
160+
while (key.length() < DB_KEY_LENGTH) {
161+
key = '0' + key;
162+
}
163+
164+
// Return key with fixed length.
165+
return key;
166+
}

src/KeyDB.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Include guard.
2+
#ifndef KEY_DB_H
3+
#define KEY_DB_H
4+
5+
// Define max key count and char length.
6+
#define DB_KEY_MAX 12
7+
#define DB_KEY_LENGTH 8
8+
9+
// Include Arduino and EEPROM libraries.
10+
#include "Arduino.h"
11+
#include <EEPROM.h>
12+
13+
// Start class declaration.
14+
class KeyDB {
15+
public:
16+
KeyDB();
17+
18+
int count();
19+
void loadKeys();
20+
void saveKeys();
21+
22+
void addKey(String key);
23+
void removeKey(String key);
24+
bool keyExists(String key);
25+
26+
String keyLengthen(String key);
27+
String keys[DB_KEY_MAX] = {""};
28+
};
29+
30+
#endif

0 commit comments

Comments
 (0)