commit 55298a05bf3a4ecf0dc0168c72f4cdceeeb232ed Author: skybldev Date: Tue Jan 11 18:20:52 2022 -0500 initial commit diff --git a/README.md b/README.md new file mode 100644 index 0000000..1c16697 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +Control an LED strip with an Arduino through a serial connection. diff --git a/algo.js b/algo.js new file mode 100755 index 0000000..df5af29 --- /dev/null +++ b/algo.js @@ -0,0 +1,40 @@ +const tps = 47; +const fade_ms = 500; // ms + +const current_vals = [10, 100, 45]; +const target_vals = [255, 255, 0]; +var fade_increments = [0, 0, 0]; + +var fade_ticks = Math.ceil(fade_ms / tps); // 32 + +var val_iterator = 0; +var tick_iterator = 0; + +console.log(`fade_ticks: ${fade_ticks}`); + +while (val_iterator < 3) { + + fade_increments[val_iterator] = -((current_vals[val_iterator] - target_vals[val_iterator]) / fade_ticks); + val_iterator++; + +} + +val_iterator = 0; + +console.log(`increments: ${fade_increments}`); + +while (tick_iterator < fade_ticks) { + + while (val_iterator < 3) { + + current_vals[val_iterator] += fade_increments[val_iterator]; + val_iterator++; + + } + + val_iterator = 0; + + console.log(current_vals.map((val) => Math.round(val))); + + tick_iterator++; +} \ No newline at end of file diff --git a/algonew.js b/algonew.js new file mode 100755 index 0000000..46db7f7 --- /dev/null +++ b/algonew.js @@ -0,0 +1,23 @@ +const ticks_per_second = 47; + +const current_color = [10, 100, 45]; + +const target_color = [255, 255, 0]; +const how_long_fade_is = 1500; // ms + +var how_many_ticks_it_takes = Math.floor(how_long_fade_is / ticks_per_second); // 32 +var percent_amount = 1; + +var tick_inc = 0; + +while (tick_inc < how_many_ticks_it_takes) { + let color_out = []; + for (let j = 0; j < 3; j++) { + color_out.push((current_color[j] * percent_amount) + (target_color[j] * (1 - percent_amount))); + } + + console.log(`${tick_inc}: ${color_out}`); + + tick_inc++; + percent_amount -= (1 / how_many_ticks_it_takes); +} \ No newline at end of file diff --git a/algotest.cpp b/algotest.cpp new file mode 100755 index 0000000..7aab081 --- /dev/null +++ b/algotest.cpp @@ -0,0 +1,48 @@ +#define TPS 47 +#define FADE_MS 500 + +#include +#include + +using namespace std; + +float current_vals[] = {10, 100, 45}; +int target_vals[] = {255, 255, 0}; +float fade_increments[] = {0.0, 0.0, 0.0}; + +int fade_ticks = ceil(FADE_MS / (float) TPS); + +int val_iterator = 0; +int tick_iterator = 0; + +int main() { + + cout << "fade_ticks: " << fade_ticks << endl; + + while (val_iterator < 3) { + fade_increments[val_iterator] = -((current_vals[val_iterator] - target_vals[val_iterator]) / (float) fade_ticks); + val_iterator++; + } + + val_iterator = 0; + + cout << "increments: " << fade_increments[0] << ", " << fade_increments[1] << ", " << fade_increments[2] << endl; + + while (tick_iterator < fade_ticks) { + + while (val_iterator < 3) { + + current_vals[val_iterator] += fade_increments[val_iterator]; + val_iterator++; + + } + + val_iterator = 0; + + cout << tick_iterator << ": " << round(current_vals[0]) << ", " << round(current_vals[1]) << ", " << round(current_vals[2]) << endl; + + tick_iterator++; + } + + return 0; +} \ No newline at end of file diff --git a/cmdparsermini.cpp b/cmdparsermini.cpp new file mode 100755 index 0000000..a623561 --- /dev/null +++ b/cmdparsermini.cpp @@ -0,0 +1,88 @@ +#include + +using namespace std; + +void callback(char command, char arg, int parsed_number) { + cout << "command: " << command << " | "; + cout << "arg: " << arg << " | "; + cout << "parsed_number: " << parsed_number << " | "; + cout << endl; +} + +int parse_string(char input[]) { + char current_char = input[0]; + char command = ' '; + char arg = ' '; + char current_number_string[4] = {' ', ' ', ' ', '\0'}; + + int line_position = 0; + int total_position = 0; + int current_number_len = 0; + int parsed_number = 0; + + while (current_char != '\0') { + if (line_position == 0) { + switch (current_char) { + case 'f': + case 't': + case 'b': + command = current_char; + break; + + default: + cout << "invalid command. " << endl; + return 1; + } + } else { + switch (current_char) { + case '0' ... '9': + if (current_number_len < 4) { + current_number_len++; + current_number_string[current_number_len - 1] = current_char; + } + break; + + case 'r': + case 'g': + case 'b': + case 't': + arg = current_char; + break; + + case ' ': + if (current_number_len > 0) parsed_number = stoi(current_number_string); + if (arg != ' ') callback(command, arg, parsed_number); + + arg = ' '; + current_number_len = 0; + fill(begin(current_number_string), end(current_number_string), ' '); + current_number_string[3] = '\0'; + + break; + + case '\n': + if (current_number_len > 0) parsed_number = stoi(current_number_string); + callback(command, arg, parsed_number); + + command = arg = ' '; + line_position = parsed_number = current_number_len = 0; + fill(begin(current_number_string), end(current_number_string), ' '); + current_number_string[3] = '\0'; + + break; + + default: + cout << "invalid syntax." << endl; + return 1; + } + } + + if (current_char != '\n') line_position++; + + total_position++; + current_char = input[total_position]; + } + + line_position = 0; + return 0; +} \ No newline at end of file diff --git a/cmdparsetest.cpp b/cmdparsetest.cpp new file mode 100755 index 0000000..ca902a4 --- /dev/null +++ b/cmdparsetest.cpp @@ -0,0 +1,138 @@ +#include +//#include + +using namespace std; + +int parse_string(char input[]); +void parse_string_callback(char command, char arg, int parsed_number); + + +int main() { + + char input1[] = "f r20 g120 b255\n"; + char input2[] = "b\n"; + char input3[] = "t r20\n"; + char input4[] = "t r20\nb\nf r20 g120 b255\n"; + char input5[] = "t'r3\n"; + + cout << "parsing string: " << input4 << endl; + + return parse_string(input4); + +} + +void callback(char command, char arg, int parsed_number) { + + cout << "command: " << command << " | "; + cout << "arg: " << arg << " | "; + cout << "parsed_number: " << parsed_number << " | "; + cout << endl; + +} + +int parse_string(char input[]) { + + char current_char = input[0]; + char command = ' '; + char arg = ' '; + char current_number_string[4] = {' ', ' ', ' ', '\0'}; + + int line_position = 0; + int total_position = 0; + int current_number_len = 0; + int parsed_number = 0; + + while (current_char != '\0') { + + if (line_position == 0) { + + switch (current_char) { + + case 'f': + case 't': + case 'b': + command = current_char; + break; + + default: + cout << "invalid command. " << endl; + return 1; + + } + + } else { + + switch (current_char) { + + case '0' ... '9': + if (current_number_len < 4) { + + current_number_len++; + current_number_string[current_number_len - 1] = current_char; + + } + + break; + + case 'r': + case 'g': + case 'b': + case 't': + arg = current_char; + break; + + case ' ': + if (current_number_len > 0) parsed_number = stoi(current_number_string); + if (arg != ' ') callback(command, arg, parsed_number); + + arg = ' '; + current_number_len = 0; + + fill(begin(current_number_string), end(current_number_string), ' '); + current_number_string[3] = '\0'; + + break; + + case '\n': + if (current_number_len > 0) parsed_number = stoi(current_number_string); + + callback(command, arg, parsed_number); + + command = arg = ' '; + line_position = parsed_number = current_number_len = 0; + + fill(begin(current_number_string), end(current_number_string), ' '); + current_number_string[3] = '\0'; + + break; + + default: + cout << "invalid syntax." << endl; + return 1; + + } + + } + + if (current_char != '\n') { + + //cout << "line_position: " << line_position << " | "; + //cout << "current_char: " << (current_char == '\n' ? '\\' : current_char) << " | "; + //cout << "parsed_number: " << parsed_number << " | "; + //cout << "current_number_string: " << current_number_string << " | "; + //cout << "command: " << command << endl; + + line_position++; + + } + + total_position++; + current_char = input[total_position]; + + } + + line_position = 0; + + return 0; + +} \ No newline at end of file diff --git a/main/README.md b/main/README.md new file mode 100755 index 0000000..26a57df --- /dev/null +++ b/main/README.md @@ -0,0 +1,29 @@ +# ledctl + +## Documentation + +Each command sent through Serial is a single character followed by arguments in g-code style. For example: `F T200 R0 G0 B107`. All characters must be capitalized. + +For any command's parameters, if it is not provided, the initial or last values will be used instead. + +If a parameter is repeated, or two parameters that change the same values are used at the same time, the right-most occurrence takes precedence. + +### `B` Breathe + +Usage `B` + +Changes into "Breathe" mode. + +### `F` Fade + +Usage: `F T