Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom QrCode saving #552

Merged
merged 2 commits into from
Dec 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ Bruce3_*.bin
bruce.conf
_*.sh
tmp
ISSUES.md
ISSUES.md
31 changes: 31 additions & 0 deletions src/core/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ JsonDocument BruceConfig::toJson() const {
dm.add(disabledMenus[i]);
}

JsonArray qrArray = setting.createNestedArray("qrCodes");
for (const auto& entry : qrCodes) {
JsonObject qrEntry = qrArray.createNestedObject();
qrEntry["menuName"] = entry.menuName;
qrEntry["content"] = entry.content;
}

return jsonDoc;
}

Expand Down Expand Up @@ -136,6 +143,19 @@ void BruceConfig::fromFile() {
}
} else { count++; log_e("Fail"); }

if (!setting["qrCodes"].isNull()) {
qrCodes.clear();
JsonArray qrArray = setting["qrCodes"].as<JsonArray>();
for (JsonObject qrEntry : qrArray) {
String menuName = qrEntry["menuName"].as<String>();
String content = qrEntry["content"].as<String>();
qrCodes.push_back({menuName, content});
}
} else {
count++;
log_e("Fail to load qrCodes");
}

validateConfig();
if (count>0) saveFile();

Expand Down Expand Up @@ -421,3 +441,14 @@ void BruceConfig::addDisabledMenu(String value) {
disabledMenus.push_back(value);
saveFile();
}

void BruceConfig::addQrCodeEntry(const String& menuName, const String& content) {
qrCodes.push_back({menuName, content});
saveFile();
}

void BruceConfig::removeQrCodeEntry(const String& menuName) {
qrCodes.erase(std::remove_if(qrCodes.begin(), qrCodes.end(),
[&](const QrCodeEntry& entry) { return entry.menuName == menuName; }), qrCodes.end());
saveFile();
}
13 changes: 13 additions & 0 deletions src/core/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ class BruceConfig {
String user;
String pwd;
};
struct QrCodeEntry {
String menuName;
String content;
};

const char *filepath = "/bruce.conf";

Expand Down Expand Up @@ -70,6 +74,13 @@ class BruceConfig {

std::vector<String> disabledMenus = {};

std::vector<QrCodeEntry> qrCodes = {
{"Bruce AP", "WIFI:T:WPA;S:BruceNet;P:brucenet;;"},
{"Bruce Wiki", "https://github.com/pr3y/Bruce/wiki"},
{"Bruce Site", "https://bruce.computer"},
{"Rickroll", "https://youtu.be/dQw4w9WgXcQ"}
};

/////////////////////////////////////////////////////////////////////////////////////
// Constructor
/////////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -103,6 +114,8 @@ class BruceConfig {
void setWebUICreds(const String& usr, const String& pwd);
void setWifiApCreds(const String& ssid, const String& pwd);
void addWifiCredential(const String& ssid, const String& pwd);
void addQrCodeEntry(const String& menuName, const String& content);
void removeQrCodeEntry(const String& menuName);
String getWifiPassword(const String& ssid) const;

void setIrTxPin(int value);
Expand Down
89 changes: 78 additions & 11 deletions src/modules/others/qrcode_menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#include "core/display.h"
#include "core/settings.h"
#include "core/mykeyboard.h"
#include "qrcode_menu.h"
#include "core/config.h"


uint16_t crc_ccitt_update(uint16_t crc, uint8_t data) {
Expand Down Expand Up @@ -42,8 +44,8 @@ void qrcode_display(String qrcodeUrl) {
#endif
}

void custom_qrcode() {
String message = keyboard("", 100, "QRCode text:");
void display_custom_qrcode() {
String message = keyboard("", 100, "QRCode text:");
return qrcode_display(message);
}

Expand All @@ -63,19 +65,84 @@ void pix_qrcode() {
}

void qrcode_menu() {

std::vector<Option> options;

// Add QR codes from the config
for (const auto& entry : bruceConfig.qrCodes) {
options.emplace_back(std::string(entry.menuName.c_str()), [=]() { qrcode_display(entry.content); });
}

options.emplace_back("PIX", [=]() { pix_qrcode(); });
options.emplace_back("Custom", [=]() { custom_qrcode_menu(); });
options.emplace_back("Main menu", [=]() { backToMenu(); });

delay(200);
loopOptions(options);
delay(200);
}

void custom_qrcode_menu() {
options = {
{"Bruce AP", [=]() { qrcode_display("WIFI:T:WPA;S:BruceNet;P:brucenet;;"); }},
{"Bruce Repo", [=]() { qrcode_display("https://github.com/pr3y/Bruce"); }},
{"Rickroll", [=]() { qrcode_display("https://youtu.be/dQw4w9WgXcQ"); }},
{"HackerTyper", [=]() { qrcode_display("https://hackertyper.net/"); }},
{"ZomboCom", [=]() { qrcode_display("https://html5zombo.com/"); }},
{"PIX", [=]() { pix_qrcode(); }},
{"Custom", [=]() { custom_qrcode(); }},
{"Main menu", [=]() { backToMenu(); }},
{"Display", [=]() { display_custom_qrcode(); }},
{"Save&Display", [=]() { save_and_display_qrcode(); }},
{"Remove", [=]() { remove_custom_qrcode(); }},
{"Back", [=]() { qrcode_menu(); }}
};

delay(200);
loopOptions(options);
delay(200);
}

void save_and_display_qrcode() {

String name = keyboard("", 100, "QRCode name:");
if (name.isEmpty()){
displayError("Name cannot be empty!");
delay(1000);
return;
}

if (std::any_of(bruceConfig.qrCodes.begin(), bruceConfig.qrCodes.end(),
[&](const BruceConfig::QrCodeEntry &entry)
{
return entry.menuName == name;
}))
{
displayError("Name already exists!");
delay(1000);
return;
}

String text = keyboard("", 100, "QRCode text:");

bruceConfig.addQrCodeEntry(name, text);
return qrcode_display(text);
}

void remove_custom_qrcode() {
if (bruceConfig.qrCodes.empty()){
displayInfo("There is nothing to remove!");
delay(1000);
custom_qrcode_menu();
}
std::vector<Option> options;

// Populate options with the QR codes from the config
for (const auto& entry : bruceConfig.qrCodes) {
options.emplace_back(
std::string(entry.menuName.c_str()),
[=]() {
bruceConfig.removeQrCodeEntry(entry.menuName);
log_i("Removed QR code: %s", entry.menuName.c_str());
custom_qrcode_menu();
}
);
}

options.emplace_back("Back", [=]() { custom_qrcode_menu(); });

delay(200);
loopOptions(options);
delay(200);
}
10 changes: 9 additions & 1 deletion src/modules/others/qrcode_menu.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
#ifndef QR_CODE_MENU_H
#define QR_CODE_MENU_H

void qrcode_display(String qrcodeUrl);
void custom_qrcode();
void pix_qrcode();
void qrcode_menu();
void custom_qrcode_menu();
void display_custom_qrcode();
void save_and_display_qrcode();
void remove_custom_qrcode();

#endif
Loading