Skip to content

Commit

Permalink
SPI leds: fix for neopixelbus late constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
awawa-dev committed Aug 24, 2021
1 parent 32455cb commit 2314f98
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 32 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ Statistics are sent to serial port monitor when there is no data incoming. You c
# Flashing
For <b>RGBW LED strip</b> like RGBW SK6812 NEUTRAL white choose: <i>HyperSerialEsp8266.ino.d1_mini_RGBW_FIRSTLED_xxxxxx.bin</i><br/><br/>
For <b>RGBW LED strip</b> like RGBW SK6812 COLD white choose: <i>HyperSerialEsp8266.ino.d1_mini_RGBW_COLD_WHITE_FIRSTLED_xxxxxx.bin</i><br/><br/>
For <b>RGB LED strip</b> like WS8212b or RGB SK6812 variant choose: <i>HyperSerialEsp8266.ino.d1_mini_RGB_FIRSTLED_xxxxxx.bin</i><br/><br/><br/>
For <b>RGB LED strip</b> like WS8212b or RGB SK6812 variant choose: <i>HyperSerialEsp8266.ino.d1_mini_RGB_FIRSTLED_xxxxxx.bin</i><br/><br/>
For <b>SPI driven RGB LED strip</b> like WS2801 or APA102/SK9812 you must build the image yourself that fits you. Especially for WS2801 there is a lot of options related to color order.<br/><br/>
If the first LED in the strip should be enabled or set to black is your choice.<br/>
For the RGBW firmware the white channel is automatically calculated and R,G,B channels are corrected.<br/><br/>

Expand Down Expand Up @@ -56,6 +57,10 @@ Serial port speed:<br/>
<i>int serialSpeed = 2000000;</i><br/><br/>
Don't change LED's count as it is dynamic.<br/>

# Pinout

For non-SPI LED strip data pin is GPIO2 (usually D4).
For SPI LED strip is: GPIO13 (MOSI, usually D7) and GPIO14 (SCLK/CLOCK, usually D5).

# Disclaimer
You use it on your own risk.<br/>Don't touch these firmwares if you don't know how to put the device in the programming mode if something goes wrong.<br/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,34 @@
///////////////////////// CONFIG SECTION STARTS /////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////

bool skipFirstLed = true; // if set the first led in the strip will be set to black (for level shifters)
bool skipFirstLed = false; // if set the first led in the strip will be set to black (for level shifters)
int serialSpeed = 2000000; // serial port speed
// data output on the hardware SPI pins: MOSI for data, and MSCLK or CLK for clock
#define is_WS2801 // for WS2801 use NeoPixelBus<NeoRgbFeature, NeoWs2801Spi2MhzMethod>, otherwise comment it with //, choose NeoxxxFeature that fits you
////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////// CONFIG SECTION ENDS /////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////

int pixelCount = 16; // This is dynamic, don't change it
int pixelCount = 0; // This is dynamic, don't change it

NeoPixelBus<DotStarBgrFeature, DotStarSpiMethod>* strip = NULL;
#ifdef is_WS2801
NeoPixelBus<NeoRbgFeature, NeoWs2801Spi2MhzMethod>* strip = NULL;
#else
NeoPixelBus<DotStarBgrFeature, DotStarSpiMethod>* strip = NULL;
#endif

void Init(int count)
{
if (strip != NULL)
delete strip;

pixelCount = count;
strip = new NeoPixelBus<DotStarBgrFeature, DotStarSpiMethod>(pixelCount);
#ifdef is_WS2801
strip = new NeoPixelBus<NeoRbgFeature, NeoWs2801Spi2MhzMethod>(pixelCount);
#else
strip = new NeoPixelBus<DotStarBgrFeature, DotStarSpiMethod>(pixelCount);
#endif
strip->Begin();
}

enum class AwaProtocol {
Expand Down Expand Up @@ -60,7 +69,7 @@ bool wantShow = false;

inline void ShowMe()
{
if (wantShow == true && strip->CanShow())
if (wantShow == true && strip != NULL && strip->CanShow())
{
stat_good++;;
wantShow = false;
Expand Down Expand Up @@ -147,7 +156,13 @@ void readSerialData()
case AwaProtocol::HEADER_CRC:
if (CRC == input)
{
if (count+1 != pixelCount) Init(count+1);
if (count+1 != pixelCount)
{
if (strip != NULL)
ESP.restart();
else
Init(count+1);
}
state = AwaProtocol::RED;
}
else
Expand Down Expand Up @@ -206,7 +221,7 @@ void readSerialData()

inline void setStripPixel(uint16_t pix, RgbColor& inputColor)
{
if (pix < pixelCount)
if (pix < pixelCount && strip != NULL)
{
strip->SetPixelColor(pix, inputColor);
}
Expand All @@ -227,29 +242,7 @@ void setup()
if (skipFirstLed)
Serial.write("First LED: disabled\r\n");
else
Serial.write("First LED: enabled\r\n");

// Init NeoPixelBus
Init(pixelCount);
strip->Begin();

// Say "Hello" to the world using first led
for (int i = 0; i < 9; i++)
{
if (i < 3)
strip->SetPixelColor(0, RgbColor(((i + 1) * 80) % 255, 0, 0));
else if (i < 6)
strip->SetPixelColor(0, RgbColor(0, ((6 - i) * 80) % 255, 0));
else
strip->SetPixelColor(0, RgbColor(0, 0, ((i - 5) * 80) % 255));

strip->Show();
delay(200);
}

// Clear it
strip->SetPixelColor(0, RgbColor(0, 0, 0));
strip->Show();
Serial.write("First LED: enabled\r\n");
}

void loop()
Expand Down

0 comments on commit 2314f98

Please sign in to comment.