From 030a37daf661bc88f5d930ad7a08a95666a374e7 Mon Sep 17 00:00:00 2001 From: Fabian Schlenz Date: Sun, 19 Jul 2020 20:11:11 +0200 Subject: [PATCH] It's working now. Fixes include: * It's working now. ;-) * Added new commands: * ? returns a help via Serial * r sets every LED to a random color * t sets the whole strip to green * Afte receiving the first command, the status LED changes for every received command. --- .gitignore | 5 ++++ include/main.h | 4 +++ src/main.cpp | 80 +++++++++++++++++++++++++++++++++++++++++--------- 3 files changed, 75 insertions(+), 14 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..89cc49c --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +.pio +.vscode/.browse.c_cpp.db* +.vscode/c_cpp_properties.json +.vscode/launch.json +.vscode/ipch diff --git a/include/main.h b/include/main.h index 9902271..6cab8b4 100644 --- a/include/main.h +++ b/include/main.h @@ -5,8 +5,12 @@ void cmd_output(); void cmd_clear(); void cmd_version(); void cmd_get_max(); +void cmd_random_color(); +void cmd_test(); +void cmd_help(); void send_nack(); void send_ack(); +void send(byte data); void status_led_on(); void status_led_off(); void status_led_toggle(); diff --git a/src/main.cpp b/src/main.cpp index 0979e04..91e5da5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -4,7 +4,7 @@ #define WS2812B_PIN 14 #define LED_DATA_PIN 13 // D7 // Hardware-MOSI #define LED_CLOCK_PIN 14 // D5 // Hardware-CLK -#define STATUS_LED_PIN 1 +#define STATUS_LED_PIN 16 #define FIRMWARE_VERSION_MAJOR 1 #define FIRMWARE_VERSION_MINOR 3 #define MAX_LED_COUNT 10000 @@ -17,11 +17,13 @@ unsigned long blink_mode_timeout_timer_ts; void setup() { - Serial.begin(115200); + Serial.begin(9600); + Serial.setTimeout(1); + //Serial.println("Online!"); led_count = 256; leds = new CRGB[256]; //FastLED.addLeds(leds, 256); - FastLED.addLeds(leds, 256); + FastLED.addLeds(leds, 256); FastLED[0].setLeds(leds, 256); pinMode(STATUS_LED_PIN, OUTPUT); set_blink(0); @@ -29,9 +31,11 @@ void setup() { } void loop() { - if (Serial.available()) { - byte recv = Serial.read(); - switch(recv) { + Serial.setTimeout(1); + byte buffer=0; + size_t received = Serial.readBytes(&buffer, 1); + if (received==1) { + switch(buffer) { case 'L': cmd_length(); break; case 'F': cmd_fill(); break; case 'R': cmd_receive(); break; @@ -39,6 +43,9 @@ void loop() { case 'C': cmd_clear(); break; case 'V': cmd_version(); break; case 'M': cmd_get_max(); break; + case 'r': cmd_random_color(); break; + case 't': cmd_test(); break; + case '?': cmd_help(); break; default: send_nack(); break; } set_blink(1); @@ -68,7 +75,7 @@ void blink() { } break; case 1: // active after receiving a command - ends after 500ms - if(blink_timer > 30) { + /*if(blink_timer > 30) { blink_timer_ts = now; status_led_toggle(); } @@ -88,7 +95,11 @@ void blink() { } else { blink_timer_ts = now; status_led_off(); - } + }*/ + status_led_toggle(); + set_blink(2); + break; + case 2: // do nothing break; default: // should newer be reached if (blink_timer > 1000) { @@ -123,7 +134,7 @@ void cmd_fill() { void cmd_receive() { uint16_t first_led = receive_word(); uint16_t cmd_leds = receive_word(); - if (first_led <= led_count && cmd_leds>0 && first_led + cmd_leds + 1 <= led_count) { + if (first_led <= led_count && cmd_leds>0 && first_led + cmd_leds <= led_count) { uint16_t end_led = first_led + cmd_leds; for(uint16_t i=first_led; i> 8); - Serial.write(MAX_LED_COUNT & 0xFF); + send((byte)(MAX_LED_COUNT & 0xFF)); + send_ack(); +} + +void cmd_random_color() { + for (int i=0; i Set new LED strip length."); + Serial.println("F Fills the given part with color."); + Serial.println("R [] Sets colors for the fiven area."); + Serial.println("O Shows the set colors on the strip."); + Serial.println("C Sets all LEDs to black."); + Serial.println("V Returns the firmware version as two bytes."); + Serial.println("M Returns the maximum number of LEDs."); + Serial.println("r Sets all LEDs to random colors."); + Serial.println("t Sets the whole strip to red."); + Serial.println("? Displays this help."); send_ack(); } void send_ack() { - Serial.write('A'); + send('A'); } void send_nack() { - Serial.write('N'); + send('N'); +} + +void send(byte data) { + Serial.write(data); + //Serial.write(0x00); + Serial.flush(); } uint8_t receive_byte() { + /*byte buffer; + Serial.setTimeout(5); + size_t count = Serial.readBytes(&buffer, 1); + if (count==1) return buffer; + else return 0;*/ while (!Serial.available()) {} return Serial.read(); }