From d1d68621cc4ffdfe922ab10ba707b87e036563ea Mon Sep 17 00:00:00 2001 From: Dakamaster <644165318.tj@gmail.com> Date: Mon, 17 Jul 2023 14:46:20 +0800 Subject: [PATCH] Update WS2812B source code and examples (#186) 1. Add new variable TOTAL_NUM_OF_LED in constructor led() to allow full reset the WS2812B module. 2. Modify error message printf() function in WS2812B.cpp source code Verification: Verified on RTL8722DM EVB with V3.1.7 SDK Co-authored-by: Zhu Qi --- .../WS2812B/examples/WS2812B_Basics/WS2812B_Basics.ino | 4 +++- .../WS2812B/examples/WS2812B_Patterns/WS2812B_Patterns.ino | 3 ++- Arduino_package/hardware/libraries/WS2812B/src/WS2812B.cpp | 6 +++--- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/Arduino_package/hardware/libraries/WS2812B/examples/WS2812B_Basics/WS2812B_Basics.ino b/Arduino_package/hardware/libraries/WS2812B/examples/WS2812B_Basics/WS2812B_Basics.ino index 7c927385..4e245b64 100644 --- a/Arduino_package/hardware/libraries/WS2812B/examples/WS2812B_Basics/WS2812B_Basics.ino +++ b/Arduino_package/hardware/libraries/WS2812B/examples/WS2812B_Basics/WS2812B_Basics.ino @@ -6,7 +6,9 @@ #include "WS2812B.h" +#define TOTAL_NUM_OF_LED 16 #define NUM_OF_LEDS 8 + // There are mutiple choice of SPI_MOSI pins depends on different boards. The default is SPI_MOSI/SPI1_MOSI // AMB21/AMB22 pin 11 / pin21 // AMB23 pin 9 / pin 4 @@ -14,7 +16,7 @@ // AW-CU488 ThingPlus pin 1 / pin 14 // AMB25/AMB26 pin 17 / pin 3 -WS2812B led(SPI_MOSI, NUM_OF_LEDS); +WS2812B led(SPI_MOSI, TOTAL_NUM_OF_LED); void setup() { Serial.begin(115200); diff --git a/Arduino_package/hardware/libraries/WS2812B/examples/WS2812B_Patterns/WS2812B_Patterns.ino b/Arduino_package/hardware/libraries/WS2812B/examples/WS2812B_Patterns/WS2812B_Patterns.ino index c5829b83..f639f74e 100644 --- a/Arduino_package/hardware/libraries/WS2812B/examples/WS2812B_Patterns/WS2812B_Patterns.ino +++ b/Arduino_package/hardware/libraries/WS2812B/examples/WS2812B_Patterns/WS2812B_Patterns.ino @@ -6,6 +6,7 @@ #include "WS2812B.h" +#define TOTAL_NUM_OF_LED 16 #define NUM_OF_LEDS 8 uint32_t rgbhue; @@ -17,7 +18,7 @@ uint32_t rgbhue; // AW-CU488 ThingPlus pin 1 / pin 14 // AMB25/AMB26 pin 17 / pin 3 -WS2812B led(SPI_MOSI,NUM_OF_LEDS); +WS2812B led(SPI_MOSI,TOTAL_NUM_OF_LED); void setup() { Serial.begin(115200); diff --git a/Arduino_package/hardware/libraries/WS2812B/src/WS2812B.cpp b/Arduino_package/hardware/libraries/WS2812B/src/WS2812B.cpp index a760ec6c..acec214a 100644 --- a/Arduino_package/hardware/libraries/WS2812B/src/WS2812B.cpp +++ b/Arduino_package/hardware/libraries/WS2812B/src/WS2812B.cpp @@ -126,7 +126,7 @@ void WS2812B::setLEDCount (uint16_t num_leds) { _led_array = (pixel*)realloc(_led_array, num_leds*sizeof(pixel)); //Check if memory allocation is successful if (_led_array == NULL) { - printf("Insufficient memory avaliable"); + printf("Insufficient memory avaliable \r\n"); _num_leds = 0; } else { memset(_led_array, 0, num_leds*sizeof(pixel)); @@ -137,7 +137,7 @@ void WS2812B::setLEDCount (uint16_t num_leds) { void WS2812B::setPixelColor(uint16_t led_Number, uint8_t rColor, uint8_t gColor, uint8_t bColor) { //Verify that memory was successfully allocated if (_led_array == NULL) { - printf("set LED count first"); + printf("set LED count first \r\n"); return; } if (led_Number < _num_leds) { @@ -145,7 +145,7 @@ void WS2812B::setPixelColor(uint16_t led_Number, uint8_t rColor, uint8_t gColor, _led_array[led_Number].green = gColor; _led_array[led_Number].blue = bColor; } else { - printf("This LED does not exist"); + printf("This LED does not exist \r\n"); } }