173 lines
3.9 KiB
Arduino
173 lines
3.9 KiB
Arduino
|
|
||
|
// this function ready the Display Buttons (analog pin)
|
||
|
// the values from analogRead are due tu a Voltage Divider
|
||
|
inline byte readButtons() {
|
||
|
int c = analogRead(BTN_PIN);
|
||
|
|
||
|
if(c > 1000) {
|
||
|
return 0;
|
||
|
} else if(c < 650 && c > 630) {
|
||
|
return 1; // select
|
||
|
} else if(c < 420 && c > 370) {
|
||
|
return 2; // left
|
||
|
} else if(c < 280 && c > 200) {
|
||
|
return 3; // down
|
||
|
}
|
||
|
|
||
|
return 4; // up
|
||
|
}
|
||
|
|
||
|
// switch the output Relaid on/off
|
||
|
void set_output(boolean on) {
|
||
|
if(on) {
|
||
|
digitalWrite(A4, LOW);
|
||
|
} else {
|
||
|
digitalWrite(A4, HIGH);
|
||
|
}
|
||
|
outputOn = on;
|
||
|
}
|
||
|
|
||
|
// switch the LDC Backlight on/off
|
||
|
void lcd_backlight(boolean on) {
|
||
|
if(on) {
|
||
|
digitalWrite(LCD_BACKLIGHT, HIGH);
|
||
|
} else {
|
||
|
digitalWrite(LCD_BACKLIGHT, LOW);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// print a Mifare UID at current LDC position
|
||
|
void lcd_print_uid(MFRC522::Uid *uid) {
|
||
|
for (byte i=0; i < uid->size; i++) {
|
||
|
lcd.print(uid->uidByte[i] < 0x10 ? " 0" : " ");
|
||
|
lcd.print(uid->uidByte[i], HEX);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// gives index of stored card uid if any, otherwise -1
|
||
|
int check_uid(MFRC522::Uid *uid) {
|
||
|
MFRC522::Uid uid2;
|
||
|
|
||
|
for(byte i=0; i < 1+EEP_CLIENT_MAX; i++) {
|
||
|
uid2 = get_uid(i, EEP_ADDR_START);
|
||
|
|
||
|
if(compare_uids(uid, &uid2)) {
|
||
|
return i;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
|
boolean compare_uids(MFRC522::Uid *uid1, MFRC522::Uid *uid2) {
|
||
|
|
||
|
if(uid1->size != uid2->size) {
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
for(byte j=0;j<uid1->size;j++) {
|
||
|
if(uid1->uidByte[j] != uid2->uidByte[j]) {
|
||
|
return false;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
// get a uid from EEPROM at given index + start Address
|
||
|
MFRC522::Uid get_uid(byte index, int startAddress) {
|
||
|
MFRC522::Uid uid;
|
||
|
|
||
|
int address = startAddress + index * EEP_ADDR_UID_INCREMENT;
|
||
|
uid.size = 0;
|
||
|
EEPROM.get(address, uid);
|
||
|
return uid;
|
||
|
}
|
||
|
|
||
|
// delete a UID at the given index + start Address
|
||
|
void delete_uid(int index, int startAddress) {
|
||
|
|
||
|
int address = startAddress + index * EEP_ADDR_UID_INCREMENT;
|
||
|
|
||
|
for(byte i=0; i < EEP_ADDR_UID_INCREMENT; i++) {
|
||
|
EEPROM.write(address+i, 0);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// store the master UID at first address of EEPROM
|
||
|
void save_master_uid(MFRC522::Uid *uid) {
|
||
|
EEPROM.put(EEP_ADDR_START, *uid);
|
||
|
}
|
||
|
|
||
|
|
||
|
boolean save_client_uid(MFRC522::Uid *uid) {
|
||
|
byte index = EEP_CLIENT_MAX;
|
||
|
|
||
|
// try to find a free slot in EEPROM
|
||
|
for(byte i=0; i<EEP_CLIENT_MAX; i++) {
|
||
|
|
||
|
// we rely on the fact that uid.size is the first byte of the struct, so we can scan faster
|
||
|
byte value = EEPROM.read(EEP_ADDR_START + (i+1)*EEP_ADDR_UID_INCREMENT);
|
||
|
|
||
|
// a empty slot is found if size is 0 or 0xff which is EEPROM default value
|
||
|
if(value == 0 || value == 0xff) {
|
||
|
index = i;
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if(index == EEP_CLIENT_MAX) {
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
// address is START + Master Slot + Index*Increment
|
||
|
int address = EEP_ADDR_START + EEP_ADDR_UID_INCREMENT + index * EEP_ADDR_UID_INCREMENT;
|
||
|
EEPROM.put(address, *uid);
|
||
|
return true;
|
||
|
}
|
||
|
|
||
|
|
||
|
void add_log(MFRC522::Uid *uid) {
|
||
|
|
||
|
MFRC522::Uid temp;
|
||
|
|
||
|
//TODO: rather than moving the log every time around, we could
|
||
|
// use a pointer the the start index and just increment this and append
|
||
|
// the log a position startindex-1 und decrement the index
|
||
|
|
||
|
// move all entries one back
|
||
|
for(byte i=EEP_ADDR_LOG_LENGTH - 1; i> 0; i--) {
|
||
|
temp.size = 0;
|
||
|
|
||
|
EEPROM.get(EEP_ADDR_LOG_START + (i - 1) * EEP_ADDR_UID_INCREMENT, temp);
|
||
|
|
||
|
if(temp.size != 0 && temp.size != 255) {
|
||
|
EEPROM.put(EEP_ADDR_LOG_START + i * EEP_ADDR_UID_INCREMENT, temp);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
EEPROM.put(EEP_ADDR_LOG_START, *uid);
|
||
|
}
|
||
|
|
||
|
// a debug function for showing all UID from EEPROM over Serial
|
||
|
void dump_uid(byte count, int address) {
|
||
|
MFRC522::Uid uid;
|
||
|
|
||
|
for(byte i=0; i< count; i++) {
|
||
|
uid = get_uid(i, address);
|
||
|
|
||
|
Serial.print("slot ");
|
||
|
if(i < 10) Serial.print(" ");
|
||
|
Serial.print(i);
|
||
|
Serial.print(":");
|
||
|
for (byte j=0;j<10;j++) {
|
||
|
Serial.print(uid.uidByte[j] < 0x10 ? " 0" : " ");
|
||
|
Serial.print(uid.uidByte[j], HEX);
|
||
|
}
|
||
|
Serial.println(" ");
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
|