
Rust, a Raspberry Pi and Addressable LEDs - slicedbrandy
https://blog.tomarrell.com/post/rust_and_leds
======
slicedbrandy
So I finally got around to writing about a project using the Raspberry Pi to
power a panel of WS2812b LED's.

Let me know what you guys think.

~~~
JoshMcguigan
Thanks for sharing.

One question about the code, why does the LedPanel struct use a String as a
buffer rather than using a Vec<u8>?

edit - I think I see the reason now. Using a string made it easy to print the
u8 as a string of bits, which you could then iterate over using chars. This
was used to help perform the conversion from bits directly representing colors
to the on the wire representation.

~~~
slicedbrandy
That would certainly work as well, and ultimately it does get converted into a
Vec<u8>. I did consider just having the buffer as it from the start, but by
using a String it was just a little more intuitive when it came to pushing
more bits onto the buffer, as well as doing the padding operation at the end.
More just to reduce complexity during development than anything I guess.

~~~
JoshMcguigan
I may have overlooked some things, but I made a quick PR[0] just to see what
this would look like using a Vec<u8>. Feel free to close if you prefer the
original.

[0]: [https://github.com/tomarrell/rasp-
ws2812b/pull/2](https://github.com/tomarrell/rasp-ws2812b/pull/2)

------
joe_xyz
Very cool!

We built a similar WS2812b array at university for an embedded systems design
project: [https://github.com/joe-wright/wiisel](https://github.com/joe-
wright/wiisel)

We looked at using a Raspberry Pi but instead settled on an mbed FRDM-KL25Z so
we had more control over the signal timing. The trade-off was that the KL25Z
only had 16KB memory instead of the Pi's 512MB, so getting everything to fit
took a bit of creativity and a few late nights...

------
FraKtus
For that to work correctly, the piece of code generating the signal should
never be interrupted.

To my knowledge, this is not possible with a Raspberry Pi. You should see
glitches on the output when your loop generating the 1s and 0s is interrupted.

That may be ok it if you just when to send one static light pattern but the
glitches should be more evident if you want to create beautiful, flowing light
patterns.

That's why it's better to drive those WS2812b with an Arduino that you code
bare metal.

~~~
Ao7bei3s
APA102 is strictly better than WS2812B (standard SPI, much less sensitive to
timing, way less PWM flicker, similar price/availability).

When you still want bare metal, pick a modern uC like ESP8266 (cheap, 32 bit,
integrated WiFi for trivial connectivity) instead of "classic" overpriced,
outdated 8/16 bit AVR (Attiny/Atmega) based Arduino hardware.

Arduino the software platform still works great of course, but so does NodeMCU
which is the go to stack on ESP8266.

~~~
simcop2387
I'd recommend looking at some of the ARM dev boards instead of the ESP8266 for
general purpose stuff and if you need the wifi, the ESP32. This is mostly
because the ESP8266 is missing a lot of hardware functionality that you'll
find in the ESP32 or any arm boards. In particular UARTs (serial ports), I2C,
and SPI periphrials that can make working with other things like the APA102 a
lot nicer to code for. With the arm chips you usually even can setup the SPI
transfers as DMA so that you don't have to actually do any work on the cpu
itself to drive the lights.

That said the ESP8266 still has a place because of how cheap you can usually
find them. Lowest price arm board that i know of from a manufacturer is the
Teensy LC at ~$12USD, but there are cheaper ones out there on ebay and
aliexpress. I just can't say anything about how well they work or are
supported by anyone.

~~~
wdfx
I bought a pair of esp8266 off Amazon for about £10 recently; they both look
like some sort of knock-off, the PCBs are unbranded and the soldering looks
somewhat less than perfect in places - however, both devices function
perfectly.

(WIP modular synth sequencer:
[https://photos.app.goo.gl/PcMqnUThygRG3T8v7](https://photos.app.goo.gl/PcMqnUThygRG3T8v7)
)

I also have since bought a couple of ESP32 and think they are awesome.

~~~
codebje
Check the regulators on those cheap boards, they're often well under spec and
cause brown-out reboots when the wifi gets working.

If you don't use the more power hungry board features they still work fine.

------
teambob
You need to use the timer to bit bang - which may require writing a device
driver

Source: wrote a software UART for an embedded project

