Skip to content

Commit

Permalink
Update WS2812B source code and examples (#186)
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
S10143806H and Zhu Qi committed Jul 17, 2023
1 parent f9e1af7 commit d1d6862
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@

#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
// BW16/BW16 Type C pin 12
// 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "WS2812B.h"

#define TOTAL_NUM_OF_LED 16
#define NUM_OF_LEDS 8

uint32_t rgbhue;
Expand All @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions Arduino_package/hardware/libraries/WS2812B/src/WS2812B.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -137,15 +137,15 @@ 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) {
_led_array[led_Number].red = rColor;
_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");
}
}

Expand Down

0 comments on commit d1d6862

Please sign in to comment.