Skip to content

Commit

Permalink
Merge pull request #666 from rennancockles/dev
Browse files Browse the repository at this point in the history
RFID multiple keys for MIFARE
  • Loading branch information
rennancockles authored Jan 9, 2025
2 parents 636affe + 29c841f commit a39166f
Show file tree
Hide file tree
Showing 15 changed files with 250 additions and 130 deletions.
25 changes: 25 additions & 0 deletions src/core/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ JsonDocument BruceConfig::toJson() const {

setting["rfidModule"] = rfidModule;

JsonArray _mifareKeys = setting.createNestedArray("mifareKeys");
for (auto key : mifareKeys) _mifareKeys.add(key);

setting["gpsBaudrate"] = gpsBaudrate;

setting["startupApp"] = startupApp;
Expand Down Expand Up @@ -134,6 +137,11 @@ void BruceConfig::fromFile() {
if(!setting["rfScanRange"].isNull()) { rfScanRange = setting["rfScanRange"].as<int>(); } else { count++; log_e("Fail"); }

if(!setting["rfidModule"].isNull()) { rfidModule = setting["rfidModule"].as<int>(); } else { count++; log_e("Fail"); }
if(!setting["mifareKeys"].isNull()) {
mifareKeys.clear();
JsonArray _mifareKeys = setting["mifareKeys"].as<JsonArray>();
for (JsonVariant key : _mifareKeys) mifareKeys.insert(key.as<String>());
} else { count++; log_e("Fail"); }

if(!setting["gpsBaudrate"].isNull()) { gpsBaudrate = setting["gpsBaudrate"].as<int>(); } else { count++; log_e("Fail"); }

Expand Down Expand Up @@ -205,6 +213,7 @@ void BruceConfig::validateConfig() {
validateRfScanRangeValue();
validateRfModuleValue();
validateRfidModuleValue();
validateMifareKeysItems();
validateGpsBaudrateValue();
validateDevModeValue();
}
Expand Down Expand Up @@ -432,6 +441,22 @@ void BruceConfig::validateRfidModuleValue() {
}


void BruceConfig::addMifareKey(String value) {
if (value.length() != 12) return;
mifareKeys.insert(value);
validateMifareKeysItems();
saveFile();
}


void BruceConfig::validateMifareKeysItems() {
for (auto key = mifareKeys.begin(); key != mifareKeys.end();) {
if (key->length() != 12) key = mifareKeys.erase(key);
else ++key;
}
}


void BruceConfig::setGpsBaudrate(int value) {
gpsBaudrate = value;
validateGpsBaudrateValue();
Expand Down
4 changes: 4 additions & 0 deletions src/core/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <ArduinoJson.h>
#include <map>
#include <vector>
#include <set>

#define DEFAULT_PRICOLOR 0xA80F

Expand Down Expand Up @@ -74,6 +75,7 @@ class BruceConfig {

// RFID
int rfidModule = M5_RFID2_MODULE;
std::set<String> mifareKeys = {};

// GPS
int gpsBaudrate = 9600;
Expand Down Expand Up @@ -155,6 +157,8 @@ class BruceConfig {
// RFID
void setRfidModule(RFIDModules value);
void validateRfidModuleValue();
void addMifareKey(String value);
void validateMifareKeysItems();

// GPS
void setGpsBaudrate(int value);
Expand Down
1 change: 1 addition & 0 deletions src/core/menu_items/RFIDMenu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ void RFIDMenu::optionsMenu() {
void RFIDMenu::configMenu() {
options = {
{"RFID Module", [=]() { setRFIDModuleMenu(); }},
{"Add MIF Key", [=]() { addMifareKeyMenu(); }},
{"Back", [=]() { optionsMenu(); }},
};

Expand Down
31 changes: 26 additions & 5 deletions src/core/sd_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include "modules/rf/rf.h"
#include "modules/ir/TV-B-Gone.h"
#include "modules/ir/custom_ir.h"
#include "modules/wifi/wigle.h"
#include "modules/gps/wigle.h"
#include "modules/others/bad_usb.h"
#include "modules/others/qrcode_menu.h"
#include "modules/bjs_interpreter/interpreter.h"
Expand Down Expand Up @@ -445,15 +445,15 @@ void readFs(FS fs, String folder, String allowed_ext) {
** Function: loopSD
** Where you choose what to do with your SD Files
**********************************************************************/
String loopSD(FS &fs, bool filePicker, String allowed_ext) {
String loopSD(FS &fs, bool filePicker, String allowed_ext, String rootPath) {
Opt_Coord coord;
String result = "";
bool reload=false;
bool redraw = true;
int index = 0;
int maxFiles = 0;
String Folder = "/";
String PreFolder = "/";
String Folder = rootPath;
String PreFolder = rootPath;
tft.fillScreen(bruceConfig.bgColor);
tft.drawRoundRect(5,5,tftWidth-10,tftHeight-10,5,bruceConfig.priColor);
if(&fs==&SD) {
Expand Down Expand Up @@ -498,7 +498,7 @@ String loopSD(FS &fs, bool filePicker, String allowed_ext) {
displayScrollingText(fileList[index].filename, coord);

#ifdef HAS_KEYBOARD
const short PAGE_JUMP_SIZE = 5;
const short PAGE_JUMP_SIZE = 5;
char pressed_letter = checkLetterShortcutPress();
if(check(EscPress)) goto BACK_FOLDER; // quit

Expand Down Expand Up @@ -834,3 +834,24 @@ void fileInfo(FS fs, String filepath) {

return;
}

/*********************************************************************
** Function: createNewFile
** Function will save a file into FS. If file already exists it will
** append a version number to the file name.
**********************************************************************/
File createNewFile(FS *&fs, String filepath) {
int extIndex = filepath.lastIndexOf('.');
String filename = filepath.substring(0, extIndex);
String ext = filepath.substring(extIndex);

if ((*fs).exists(filename + ext)) {
int i = 1;
filename += "_";
while((*fs).exists(filename + String(i) + ext)) i++;
filename += String(i);
}

File file = (*fs).open(filename + ext, FILE_WRITE);
return file;
}
4 changes: 3 additions & 1 deletion src/core/sd_functions.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ void readFs(FS fs, String folder, String allowed_ext = "*");

bool sortList(const FileList& a, const FileList& b);

String loopSD(FS &fs, bool filePicker = false, String allowed_ext = "*");
String loopSD(FS &fs, bool filePicker = false, String allowed_ext = "*", String rootPath = "/");

void viewFile(FS fs, String filepath);

Expand All @@ -56,4 +56,6 @@ bool getFsStorage(FS *&fs);

void fileInfo(FS fs, String filepath);

File createNewFile(FS *&fs, String filepath);

#endif
15 changes: 12 additions & 3 deletions src/core/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,14 @@ int gsetRotation(bool set){

if(result & 0b01) { // if 1 or 3
tftWidth=TFT_HEIGHT;
#if defined(HAS_TOUCH)
#if defined(HAS_TOUCH)
tftHeight=TFT_WIDTH - 20;
#else
#else
tftHeight=TFT_WIDTH;
#endif
} else { // if 2 or 0
tftWidth=TFT_WIDTH;
#if defined(HAS_TOUCH)
#if defined(HAS_TOUCH)
tftHeight=TFT_HEIGHT-20;
#else
tftHeight=TFT_HEIGHT;
Expand Down Expand Up @@ -289,6 +289,15 @@ void setRFIDModuleMenu() {
loopOptions(options, bruceConfig.rfidModule);
}

/*********************************************************************
** Function: addMifareKeyMenu
** Handles Menu to add MIFARE keys into config list
**********************************************************************/
void addMifareKeyMenu() {
String key = keyboard("", 12, "MIFARE key");
bruceConfig.addMifareKey(key);
}


/*********************************************************************
** Function: setClock
Expand Down
2 changes: 2 additions & 0 deletions src/core/settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ void setRFFreqMenu();

void setRFIDModuleMenu();

void addMifareKeyMenu();

void setSleepMode();

void setDimmerTimeMenu();
Expand Down
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit a39166f

Please sign in to comment.