Enable/Disable LED from preferences file

This commit is contained in:
Spacehuhn 2022-07-06 21:41:52 +02:00
parent 9620e0721a
commit ee15afa9e3
7 changed files with 105 additions and 15 deletions

View File

@ -11,6 +11,7 @@
#include "src/msc/msc.h"
#include "src/selector/selector.h"
#include "src/attack/attack.h"
#include "src/preferences/preferences.h"
enum Mode {
SETUP, ATTACK
@ -19,23 +20,18 @@ enum Mode {
Mode mode;
void setup() {
// Initialize all the things
debug_init();
debugln("Started");
msc::init();
keyboard::init();
selector::init();
led::init();
preferences::load();
/*
// Wait until Serial Monitor was opened
while (!Serial) {
delay(1);
}
*/
debugln("[Started]");
// Preferences
led::setEnable(preferences::ledEnabled());
// Start doing the work
mode = selector::read() ? SETUP : ATTACK;
// ========== Setup Mode ========== //
@ -49,7 +45,7 @@ void setup() {
if (mode == ATTACK) {
led::setColor(255, 0, 0); // Turn LED red
attack::start(); // Start attack
attack::start(); // Start attack
led::setColor(0, 255, 0); // Turn LED green
mode = SETUP;
}

View File

@ -47,6 +47,14 @@ namespace led {
led.show();
}
void setEnable(bool enabled) {
if (enabled) {
led.setBrightness(255);
} else {
led.setBrightness(0);
}
}
void setColor(int r, int g, int b) {
for (size_t i = 0; i<led.numPixels(); i++) {
led.setPixelColor(i, r, g, b);

View File

@ -6,6 +6,7 @@
namespace led {
void init();
void setEnable(bool enabled);
void setColor(int r, int g, int b);
void start_blink(uint8_t r, uint8_t g, uint8_t b, unsigned long intv);
void stop_blink();

View File

@ -98,7 +98,7 @@ namespace msc {
}
// If a file is already open, close it
if(file.isOpen()) file.close();
if (file.isOpen()) file.close();
// Create a new file element and push it to the stack
file_element_t file_element;
@ -125,11 +125,11 @@ namespace msc {
debugln(file_stack.size());
// If stack is now empty, we're done
if (file_stack.empty()){
if (file_stack.empty()) {
debugln("Stack is empty");
return false;
}
debugln(file_stack.top().path.c_str());
// Get the next file from the stack
@ -157,12 +157,16 @@ namespace msc {
file.seekSet(pos);
}
size_t read(char* buffer, size_t len) {
return file.read(buffer, len);
}
size_t readLine(char* buffer, size_t len) {
size_t read { 0 };
// Read as long as the file has data and buffer is not full
// -1 to compensate for a extra linebreak at the end of the file
while(file.isOpen() && file.available() > 0 && read < len-1) {
while (file.isOpen() && file.available() > 0 && read < len-1) {
// Read character by character
char c = file.read();

View File

@ -13,6 +13,7 @@ namespace msc {
uint32_t getPosition();
void gotoPosition(uint32_t pos);
size_t read(char* buffer, size_t len);
size_t readLine(char* buffer, size_t len);
bool getInLine();
}

View File

@ -0,0 +1,65 @@
/* This software is licensed under the MIT License: https://github.com/spacehuhntech/usbnova */
#include "preferences.h"
#include "../../config.h"
#include "../../debug.h"
#include <ArduinoJson.h>
namespace preferences {
// ========== PRIVATE ========= //
StaticJsonDocument<256> config_doc;
bool enable_msc;
bool enable_led;
std::string vid;
std::string pid;
std::string default_layout;
// ======== PUBLIC ======== //
void load() {
// Read config file
char buffer[256];
msc::open("preferences.json");
msc::read(buffer, sizeof(buffer));
// Deserialize the JSON document
DeserializationError error = deserializeJson(config_doc, json);
// Test if parsing succeeds.
if (error) {
debug("deserializeJson() failed: ");
debugln(error.f_str());
return;
}
// Fetch values.
enable_msc = config_doc["enable_msc"];
enable_led = config_doc["enable_led"];
vid = config_doc["vid"];
pid = config_doc["pid"];
default_layout = config_doc["default_layout"];
}
bool mscEnabled() {
return enable_msc;
}
bool ledEnabled() {
return enable_led;
}
std::string getVid() {
return vid;
}
std::string getPid() {
return pid;
}
std::string getDefaultLayout() {
return default_layout;
}
}

View File

@ -0,0 +1,15 @@
/* This software is licensed under the MIT License: https://github.com/spacehuhntech/usbnova */
#pragma once
#include <string>
namespace preferences {
void load();
bool mscEnabled();
bool ledEnabled();
std::string getVid();
std::string getPid();
std::string getDefaultLayout();
}