Added blink interval parameter to LED command

This commit is contained in:
Spacehuhn 2022-07-16 16:09:54 +02:00
parent aece22f9a8
commit 9c0f0a2a35
6 changed files with 36 additions and 44 deletions

View File

@ -24,7 +24,7 @@ void setup() {
// Initialize memory and check for problems
if (!msc::init()) {
led::startBlink(255, 0, 0, 200);
led::setColor(255, 0, 0, 200);
return;
}

View File

@ -91,9 +91,6 @@ namespace attack {
msc::open(path.c_str());
}
// Stop blinking if no script is running
led::stopBlink();
debugln("OK");
}
}

View File

@ -309,13 +309,13 @@ namespace duckparser {
led::setMode(color, mode);
}
// i.e. LED 128 23 42
// i.e. LED 128 23 42 0 (r,g,b, blink)
else {
word_node* w = cmd->next;
int c[3];
int c[4];
for (uint8_t i = 0; i<3; ++i) {
for (uint8_t i = 0; i<4; ++i) {
if (w) {
c[i] = toInt(w->str, w->len);
w = w->next;
@ -324,7 +324,7 @@ namespace duckparser {
}
}
led::setColor(c[0], c[1], c[2]);
led::setColor(c[0], c[1], c[2], c[3]);
}
ignore_delay = true;

View File

@ -11,10 +11,18 @@ namespace led {
// ========== PRIVATE ========= //
Adafruit_NeoPixel led { 1, LED_PIN, NEO_GRB + NEO_KHZ800 };
uint8_t blink_color[3] { 0, 0, 0 };
int blink_color[3] { 0, 0, 0 };
unsigned long blink_intv { 0 };
bool blink_flag { false };
unsigned long last_blink { 0 };
bool blink_flag { false };
void change_color(int r, int g, int b) {
for (size_t i = 0; i<led.numPixels(); i++) {
led.setPixelColor(i, r, g, b);
}
led.show();
}
// ========== PUBLIC ========= //
void init() {
@ -31,26 +39,17 @@ namespace led {
}
void setColor(int* color) {
setColor(color[0], color[1], color[2]);
setColor(color[0], color[1], color[2], color[3]);
}
void setColor(int r, int g, int b) {
for (size_t i = 0; i<led.numPixels(); i++) {
led.setPixelColor(i, r, g, b);
}
void setColor(int r, int g, int b, unsigned long intv) {
change_color(r, g, b);
led.show();
}
void startBlink(uint8_t r, uint8_t g, uint8_t b, unsigned long intv) {
blink_color[0] = r;
blink_color[1] = g;
blink_color[2] = b;
blink_intv = intv;
}
void stopBlink() {
blink_intv = 0;
blink_flag = false;
}
void setMode(Color color, Mode mode) {
@ -63,14 +62,13 @@ namespace led {
setColor(r, g, b);
break;
case SLOW:
startBlink(r, g, b, 1000);
setColor(r, g, b, 1000);
break;
case FAST:
startBlink(r, g, b, 200);
setColor(r, g, b, 200);
break;
default:
setColor(0, 0, 0);
stopBlink();
}
}
@ -80,9 +78,9 @@ namespace led {
blink_flag = !blink_flag;
if (blink_flag) {
setColor(blink_color[0], blink_color[1], blink_color[2]);
change_color(blink_color[0], blink_color[1], blink_color[2]);
} else {
setColor(0, 0, 0);
change_color(0, 0, 0);
}
}
}

View File

@ -22,10 +22,7 @@ namespace led {
void setEnable(bool enabled);
void setColor(int* color);
void setColor(int r, int g, int b);
void startBlink(uint8_t r, uint8_t g, uint8_t b, unsigned long intv);
void stopBlink();
void setColor(int r, int g, int b, unsigned long intv = 0);
void setMode(Color color, Mode mode);

View File

@ -37,9 +37,9 @@ namespace preferences {
std::string main_script { "main.script" };
int attack_color[3] { 128, 0, 0 };
int setup_color[3] { 0, 0, 20 };
int idle_color[3] { 0, 30, 0 };
int attack_color[4] { 128, 0, 0, 0 };
int setup_color[4] { 0, 0, 20, 0 };
int idle_color[4] { 0, 30, 0, 0 };
bool format { false };
std::string drive_name { "USB Nova" };
@ -80,9 +80,9 @@ namespace preferences {
root["main_script"] = main_script;
add_array(root, "attack_color", attack_color, 3);
add_array(root, "setup_color", setup_color, 3);
add_array(root, "idle_color", idle_color, 3);
add_array(root, "attack_color", attack_color, 4);
add_array(root, "setup_color", setup_color, 4);
add_array(root, "idle_color", idle_color, 4);
root["disable_capslock"] = disable_capslock;
root["run_on_indicator"] = run_on_indicator;
@ -127,9 +127,9 @@ namespace preferences {
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, 3);
if (!config_doc.containsKey("setup_color")) add_array(config_doc, "setup_color", setup_color, 3);
if (!config_doc.containsKey("idle_color")) add_array(config_doc, "idle_color", idle_color, 3);
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);
if (!config_doc.containsKey("disable_capslock")) config_doc["disable_capslock"] = disable_capslock;
if (!config_doc.containsKey("run_on_indicator")) config_doc["run_on_indicator"] = run_on_indicator;
@ -151,9 +151,9 @@ namespace preferences {
main_script = config_doc["main_script"].as<std::string>();
read_array(config_doc, "attack_color", attack_color, 3);
read_array(config_doc, "setup_color", setup_color, 3);
read_array(config_doc, "idle_color", idle_color, 3);
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);
// Format Flash (Drive name/Disk label max 11 characters)
format = config_doc.containsKey("format");