Compile errors with Neopixel library and Dotstar library on Raspberry Pi


#14

Maybe this explains something more:


#15

I had a look at that, but had the impression that it was written somewhat confusing :blush:


#16

Hi @ScruffR, I have received my strip of 144 WS2813 “SPI-addressable LEDs” some time ago but I only found time yesterday to try your test sketch with the Photon.

My objective: If that works, I want to make a 100-LED status panel with the RPi…


This is how the 1 meter long strip looks:

I guess the connections from top to bottom are as follows:

  • RED = “5V” (=> Vin)
  • GREEN = “IB” = “IN Backup data” (=> A3 = CLK)
  • YELLOW = “ID” = "IN Data (=> A5 = MOSI)
  • BLACK = “–>” (=> Gnd)

I have connected it as shown:


The sketch compiles fine and I flashed it to the Photon, but I can’t see any response… :frowning:

Possibly this stip is not compatible with the 3v3 data pulses, contrary to the classic addressable chips…
So, I can test it with an Arduino Nano if you believe I can use the same sketch…

Any suggestions meanwhile?
(I’ll be out till sunday…)

:wave::older_man:


#17

What is the exact type of that strip?
The wording “IN Backup Data” sounds puzzling. The CLK line does not carry any data (backup or not) but only provides the timing.


Update:
So I now found a datasheet of the WS2813 and as it turns out they are not comparable to the APA102 when it comes to protocol. These LEDs are still using a time critical protocl like the original NeoPixels but feature a sort of bypass feature where one broken LED in the chain shouldn’t affect the following (hence the naming “Backup Control data signal input”)
http://www.world-semi.com/DownLoadFile/136

So I’m afraid, my simple code won’t work on these but only on APA102 LEDs and compatible ones (WS2813 aren’t).


#18

Thanks for yr quick reply!
I am travelling and will try to respond from my iPhone:

I understand that “Backup data” sounds confusing… but the terminology for both datalines comes from the eBay seller.
If you want, I can send a screenshot when I’m back home…

For this LED, I find this link: http://www.world-semi.com/DownLoadFile/136

Greetz!


#19

Ok!
Never mind, I ordered another strip of REAL APA102 chips now:

APA102%20markings

Indeed, here you can see the correct markings: Data & Clock, In & Out!

APA102%20in%20action

I ordered the type with higher density: 144 LEDs/m, that’ll better for my status panel design…

Delivery end of july…
"I’ll be back!"
:older_man:


#20

Update for the “wrong DOTSTAR strip” (WS2813) I bought earlier:
indeed, using any of the 2 data wires, it works with the traditional “Neopixel” library.

Q: Any idea how we are supposed to connect both wires to a Photon? Both in parallel?

In that case, they could as well do that on the flexible PCB and let only one wire come out…

:thinking:


#21

The datasheet I linked above show how the backup data is supposed to be used, but since there are two ways to do it, you’d need to check which one the strip is using.
But in general I’d say you won’t need the BIN contact at all, it’s only there for daisy chaining strips.


#22

OK @ScruffR , here we are again, earlier than expected!
My 1 meter strip of APA102 pixels arrived today:

I have connected them to the (hardwire) SPI pins A3/A5:

Then I uploaded your simplified sketch (above) but nothing is happening…
When I upload the sample sketch available with the “Adafruit Dotstar” library, it shows a lighttrain of 10 dots changing from RED to GREEN and BLUE continuously. So, the strip is OK and so are my connections…

As I would like to use these dots for my “status panel”, driven from an RPi, I would prefer to use your approach…

Any idea what I could try?

:hand::older_man:


#23

I’ll look into it.


Update:
Looking at the code in this post
You never set needRefresh to true and hence the strip will never be actually updated.

Here you can find the intent of the two flags needRefresh and canRefresh

BTW, with 1m of that strip you should also change the settings at the top of your sketch correctly
Instead of

#define LEDCOUNT 300
uint32_t pxBuffer[LEDCOUNT];
// actually for the APA102 protocol you need some extra bytes for leadin/leadout
//uint32_t pxBuffer[1 + LEDCOUNT + LEDCOUNT/16 + 1]; // element 0 (leadin) ... LEDs ... 1 leadout clock per 2 LEDs (ceil)

you need

const int LEDCOUNT = 144;
uint32_t pxBuffer[1 + LEDCOUNT + LEDCOUNT/16 + 1]; // element 0 (leadin) ... LEDs ... 1 leadout clock per 2 LEDs (ceil)

#24

I wished I could follow you, but I am not experienced enough to fully understand all steps in this sketch. I’m trying…

The sketch I started from (above) was copied from your post: LINK

With your remarks, I edited it as below but now only the first led turns magenta and it stays…:

/* Replace these lines by lines below it
#define LEDCOUNT 144
uint32_t pxBuffer[LEDCOUNT];
// actually for the APA102 protocol you need some extra bytes for leadin/leadout
//uint32_t pxBuffer[1 + LEDCOUNT + LEDCOUNT/16 + 1]; // element 0 (leadin) ... LEDs ... 1 leadout clock per 2 LEDs (ceil)
*/

const int LEDCOUNT = 144;
uint32_t pxBuffer[1 + LEDCOUNT + LEDCOUNT/16 + 1]; // element 0 (leadin) ... LEDs ... 1 leadout clock per 2 LEDs (ceil)


// bool needRefresh; // Use for dynamic pattern...
bool needRefresh = true; // Use for static pattern: Make it true again when you want to refresh another pattern.
volatile bool canRefresh = true;




void setup()
{
  SPI.begin();
  SPI.setClockSpeed(8, MHZ);
  //SPI.setDataMode(0);
  SPI.setBitOrder(MSBFIRST);
}




uint32_t setAPA102Color(int px, uint8_t r, uint8_t g, uint8_t b, uint8_t brightness = 31)
{
  uint32_t color = 0;
  
  // due to big endianness in reverse order 
  color |= (brightness <<   0) | 0xE0;  //top 3 bits must be 111
  color |= (r          <<   8);
  color |= (g          <<  16);
  color |= (b          <<  24);

  pxBuffer[px] = color;

  return color;
}




void refreshDone()
{
  canRefresh = true;
}





void loop()
{
  // setup your pixels
  setAPA102Color(8, 30, 100, 40, 31);  // = Approx. "Set LED #8 colour to 15% RED, 50% GREEN and 20% RED, max brightness"
  setAPA102Color(67, 40, 0, 0, 31);    // = Approx. "Set LED #67 colour to 20% RED, max brightness"
  setAPA102Color(88, 40, 0, 140, 31);  // = Approx. "Set LED #88 colour to 20% RED and 70% BLUE, max brightness"
  
  needRefresh = true; // Test!
  
  if (needRefresh && canRefresh)
  {
    canRefresh = needRefresh = false;
    SPI.transfer(pxBuffer, NULL, sizeof(pxBuffer), refreshDone);
  }

  delay(2000); // Slow down loop...
}

:thinking:


#25

You can initialise needRefresh to be true on startup like this

bool needRefresh = true;

Currently it’s by default false.

But that would only help if you had a static pattern you want to display.
Whenever you intend to change the pattern, you need to set needRefresh = true again after you are finished updating the content of the buffer.

But you need to leave canRefresh alone, since that is in fact set by the refreshDone function after the transfer from the buffer to the strip is done.

So you need to revert back to

  if (needRefresh && canRefresh)
  {
    canRefresh = 
    needRefresh = false;
    SPI.transfer(pxBuffer, NULL, sizeof(pxBuffer), refreshDone);
  }

Otherwise you’d over and over start the transfer potentially even before it’s actually finished.
That’s the whole point of canRefresh to prevent that from happening.

BTW, unlike the comment, canRefresh wasn’t “incomplete” or “blank”, but this is just a lazy way to set multiple variables to the same value and could be written like that (which might be clearer to understand)

  canRefresh = needRefresh = false;

This would verbally mean: "Assign false to needRefresh and assign that in turn to canRefresh" (assignment execution in right to left order).


#26

I edited the sketch (and replaced the one in my previous post) reverted to the “lazy” lines :wink: and now there is an extra ‘refresh’ line in the loop():

  needRefresh = true;

Result: Now only the first led turns magenta and it stays like that…


#27

For me the sketch works as intended but you can try this which allows you to set each individual LED via a Particle.function()

const int LEDCOUNT = 144;
uint32_t pxBuffer[1 + LEDCOUNT + LEDCOUNT/16 + 1]; // element 0 (leadin) ... LEDs ... 1 leadout clock per 2 LEDs (ceil)

bool needRefresh = true;
volatile bool canRefresh = true;

int      setLEDs(const char* arg); // format "pixel,r,g,b[,brightness]"
void     refreshDone();
uint32_t setAPA102Color(int px, uint8_t r, uint8_t g, uint8_t b, uint8_t brightness = 31);

void setup()
{
  Particle.function("setLED", setLEDs);
  setLEDs("all off");
  
  SPI.begin();
  SPI.setClockSpeed(8, MHZ);
  //SPI.setDataMode(0);
  SPI.setBitOrder(MSBFIRST);
}

void loop()
{
  if (needRefresh && canRefresh)
  {
    canRefresh = 
    needRefresh = false;
    SPI.transfer(pxBuffer, NULL, sizeof(pxBuffer), refreshDone);
  }
}

int setLEDs(const char* arg)
{
  int px  = 0;          // default all LEDs
  int px_ = LEDCOUNT;   // 
  int br  = 31;         // full brightness
  int r   = 0;          // but now colour
  int g   = 0;          //
  int b   = 0;          //
    
  if (sscanf(arg, "%d,%d,%d,%d,%d", &px, &r, &g, &b, &br) >= 4)
  {
    
    if (px > 0)         // set LED with number px
      px_ = px + 1;
    else if (px < 0)    // set LEDs 1 .. px
    {
      px_ = abs(px);
      px = 0;
    }
  }

  for (; px < px_; px++)
    setAPA102Color(px, r, g, b, br);

  needRefresh = true;

  return px;
}

void refreshDone()
{
  canRefresh = true;
}

uint32_t setAPA102Color(int px, uint8_t r, uint8_t g, uint8_t b, uint8_t brightness)
{
  uint32_t color = 0;
  
  // due to big endianness in revers order 
  color |= (brightness <<   0) | 0xE0;  //top 3 bits must be 111
  color |= (b          <<   8);
  color |= (g          <<  16);
  color |= (r          <<  24);

  pxBuffer[px] = color;

  return color;
}

With arguments

  • 10,255,0,128,31 would set pixel #10 to 100% red, 0% green, 50% blue, 100% brightness
  • -20,128,128,0,15 would set pixels #1#20 to 50% red, 50% green, 0% blue, 50% brightness
  • 0,0,0,255 would set all LEDs to 0% red, 0% green, 100% blue and default to 100% brightness
  • any other parameter that doesn’t follow above standard will switch all LEDs off

Notice, while the colour components range 0 … 255 the brighness only allows values ranging 0 … 31.


#28

WOW! :heart_eyes:

This is exactly what I need @ScruffR !
I can use it also with a normal function and it works like a charm!!!

Thanks for this great sketch, I will try to use it straight away with the RPi!
Only, I am not sure if I can connect the strip the same way like the Photon…
The Rpi is not 5V tolerant…
So, I should protect the SPI ports, right?

:wave::older_man:


#29

There are variations of course (zener diodes, level converters etc…) but this is the simple protection method I 'd like to use for ALL RPI GPIOs:

28

Any comments somebody?


#30

For one, this is a silicon diode, so it has a forward voltage of ~0.7 V which will allow the voltage on the GPIO rise to ~4.0V before the diode actually opens and then you are back-feeding ~4.3V into the 3.3V out side of the regulator, which might not be happy with it (the Particle device’s regulators will definetly not, and since the µC sits on the same rail it won’t be either).

I can’t talk for the RPi’s regulator, but I’d at least go for a Germanium Schottky diode which will have a forward voltage of only ~0.2V


#31

That’s right @ScruffR, I hesitate also, but I read in a RPi blog that this should be fine…

Is there an IC with 8 or so bidirectional logic level shifters?


#32

Yes, there are some.
That’s one of them http://www.ti.com/lit/ds/symlink/txb0108.pdf


#33

Great tip @ScruffR, thanks!
I ordered these for $1/pce:
22
:ok_hand: