Select script name based on switch position

This commit is contained in:
Spacehuhn 2022-11-01 13:55:36 +01:00
parent ddc58a9221
commit be28073d53
8 changed files with 89 additions and 30 deletions

View File

@ -20,7 +20,7 @@
#define SD_READ_BUFFER 2048
// ===== SELECTOR SWITCH ===== //
#define SELECTOR A7
#define SELECTOR_1 A7
#define SELECTOR_2 A6
#define SELECTOR_3 A3

View File

@ -11,6 +11,7 @@
#include "../led/led.h"
#include "../hid/hid.h"
#include "../hid/keyboard.h"
#include "../selector/selector.h"
namespace attack {
// ====== PRIVATE ====== //
@ -31,7 +32,7 @@ namespace attack {
}
// Open main BadUSB script
msc::open(preferences::getMainScript().c_str());
msc::open(path);
// Read and parse file
char buffer[READ_BUFFER];
@ -98,6 +99,13 @@ namespace attack {
}
void start() {
start(preferences::getMainScript().c_str());
// Get switch position
int pos = selector::position();
// Find file that starts with that number
std::string path = msc::find(pos);
// If script was found, start running it
if(path != "") start(path.c_str());
}
}

View File

@ -138,6 +138,17 @@ namespace cli {
led::setColor(preferences::getIdleColor());
}
}).setDescription(" Start a BadUSB Script.");
// find
cli.addSingleArgCmd("find", [](cmd* c) {
const int pos { selector::position() };
const std::string file { msc::find(pos) };
Serial.print("Position: ");
Serial.println(pos);
Serial.print("Script: ");
Serial.println(file.c_str());
}).setDescription(" Find script based on switch position.");
}
void update() {

View File

@ -5,7 +5,6 @@
#include "../../config.h"
#include "../../debug.h"
#include <string>
#include <stack>
#include <SPI.h>
@ -90,6 +89,7 @@ namespace msc {
void print() {
Serial.println("Available files:");
if (file.isOpen()) file.close();
SdFile root;
root.open("/");
@ -97,8 +97,7 @@ namespace msc {
file.printFileSize(&Serial);
Serial.write(' ');
file.printName(&Serial);
if ( file.isDir() )
{
if ( file.isDir() ) {
// Indicate a directory.
Serial.write('/');
}
@ -273,4 +272,26 @@ namespace msc {
return written;
}
std::string find(const int num) {
char buffer[64];
std::string file_name;
bool found { false };
// Go through file in root directory
if (file.isOpen()) file.close();
SdFile root;
root.open("/");
while (!found && file.openNext(&root, O_RDONLY)) {
file.getName(buffer, 64);
file_name = std::string(buffer);
found = !file.isDir() && (buffer[0] == (num+'0'));
file.close();
}
root.close();
return file_name;
}
}

View File

@ -4,6 +4,7 @@
#include <cstdint> // uint8_t
#include <cstddef> // size_t
#include <string> // std::string
namespace msc {
bool init();
@ -29,4 +30,6 @@ namespace msc {
bool getInLine();
size_t write(const char* path, const char* buffer, size_t len);
std::string find(const int num);
}

View File

@ -34,10 +34,10 @@ namespace preferences {
std::string default_layout { "US" };
int default_delay { 5 };
/*
std::string main_script { "main.script" };
int attack_color[4] { 0, 0, 0, 0 };
*/
int attack_color[4] { 255, 0, 0, 1000 };
int setup_color[4] { 255, 0, 0, 0 };
int idle_color[4] { 0, 0, 0, 0 };
@ -79,9 +79,9 @@ namespace preferences {
root["default_layout"] = default_layout;
root["default_delay"] = default_delay;
root["main_script"] = main_script;
/*
root["main_script"] = main_script;
add_array(root, "attack_color", attack_color, 4);
add_array(root, "setup_color", setup_color, 4);
add_array(root, "idle_color", idle_color, 4);
@ -128,9 +128,9 @@ namespace preferences {
if (!config_doc.containsKey("default_layout")) config_doc["default_layout"] = default_layout;
if (!config_doc.containsKey("default_delay")) config_doc["default_delay"] = default_delay;
if (!config_doc.containsKey("main_script")) config_doc["main_script"] = main_script;
/*
if (!config_doc.containsKey("main_script")) config_doc["main_script"] = main_script;
if (!config_doc.containsKey("attack_color")) add_array(config_doc, "attack_color", attack_color, 4);
if (!config_doc.containsKey("setup_color")) add_array(config_doc, "setup_color", setup_color, 4);
if (!config_doc.containsKey("idle_color")) add_array(config_doc, "idle_color", idle_color, 4);
@ -154,9 +154,9 @@ namespace preferences {
default_layout = config_doc["default_layout"].as<std::string>();
default_delay = config_doc["default_delay"].as<int>();
main_script = config_doc["main_script"].as<std::string>();
/*
main_script = config_doc["main_script"].as<std::string>();
read_array(config_doc, "attack_color", attack_color, 4);
read_array(config_doc, "setup_color", setup_color, 4);
read_array(config_doc, "idle_color", idle_color, 4);
@ -208,9 +208,9 @@ namespace preferences {
default_layout = "US";
default_delay = 5;
main_script = "main.script";
/*
main_script = "main.script";
attack_color[0] = 0;
attack_color[1] = 0;
attack_color[2] = 0;
@ -291,11 +291,11 @@ namespace preferences {
int getDefaultDelay() {
return default_delay;
}
/*
std::string getMainScript() {
return main_script;
}
*/
int* getAttackColor() {
return attack_color;
}

View File

@ -21,9 +21,11 @@ namespace selector {
// ===== PUBLIC ===== //
void init() {
pinMode(SELECTOR, INPUT_PULLUP);
pinMode(SELECTOR_1, INPUT_PULLUP);
pinMode(SELECTOR_2, INPUT_PULLUP);
pinMode(SELECTOR_3, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(SELECTOR), isr, CHANGE);
attachInterrupt(digitalPinToInterrupt(SELECTOR_3), isr, CHANGE);
initial_mode = read();
}
@ -33,7 +35,19 @@ namespace selector {
}
Mode read() {
return digitalRead(SELECTOR) ? SETUP : ATTACK;
return (position() == 3) ? SETUP : ATTACK;
}
int position() {
if (!digitalRead(SELECTOR_1)) {
return 1;
} else if (!digitalRead(SELECTOR_2)) {
return 2;
} else if (!digitalRead(SELECTOR_3)) {
return 3;
} else {
return 0;
}
}
bool changed() {

View File

@ -12,5 +12,7 @@ namespace selector {
Mode mode();
Mode read();
int position();
bool changed();
}