hyperion.ng/libsrc/leddevice/LedDeviceWs2812SPI.cpp
penfold42 a960894d14 Another option for Ws2812 - direct spi from the pi with 1 wire (#631)
* Removed -HUP so the default -TERM signal is sent instead.
- hyperiond only listens for TERM and INT. HUP is often used to get an exe to reread its config

Changed pgrep to add '-x' so it wont partial match on the exe name.
- I have multiple instances with multiple hyperiond-instance1 names
- this ensures the service script only kills the right process

* reversing errant change to hyperion.systemd.sh

* This add a new device type - ws2812spi.

I've (ab)used the SPI interface to send the correct timing pulses to keep the ws2812 happy.
THE RATE IS IMPORTANT!
A FIXED CORE_CLK IS IMPORTANT!

Attach the SPI MOSI pin on the Pi to the DIN pin on your ws2812

        "device" :
        {
                "name"       : "MyPi",
                "type"       : "ws2812spi",
                "colorOrder" : "grb",
                "output"     : "/dev/spidev0.0",
                "rate"       : 3800000
        },

* updated hyperiond.test-binary

* Updated default SPI speed to the "correct" value.
My Pi was undervolted so was dropping the core clock confusing everything

* Code cleanups
explicitly set the final 3 bytes to 0

* Removed latchtime option - not applicable

* updated test binary


Former-commit-id: d3c19c8374999f7a554bb25ca181a8a483f86289
2016-05-15 18:39:17 +02:00

58 lines
1.4 KiB
C++

// STL includes
#include <cstring>
#include <cstdio>
#include <iostream>
// Linux includes
#include <fcntl.h>
#include <sys/ioctl.h>
// hyperion local includes
#include "LedDeviceWs2812SPI.h"
LedDeviceWs2812SPI::LedDeviceWs2812SPI(const std::string& outputDevice, const unsigned baudrate) :
LedSpiDevice(outputDevice, baudrate, 0),
mLedCount(0)
{
// empty
}
int LedDeviceWs2812SPI::write(const std::vector<ColorRgb> &ledValues)
{
mLedCount = ledValues.size();
// 3 colours, 4 spi bytes per colour + 3 frame end latch bytes
#define COLOURS_PER_LED 3
#define SPI_BYTES_PER_COLOUR 4
#define SPI_BYTES_PER_LED COLOURS_PER_LED * SPI_BYTES_PER_COLOUR
unsigned spi_size = mLedCount * SPI_BYTES_PER_LED + 3;
if(_spiBuffer.size() != spi_size){
_spiBuffer.resize(spi_size, 0x00);
}
unsigned spi_ptr = 0;
for (unsigned i=0; i< mLedCount; ++i) {
uint32_t colorBits = ((unsigned int)ledValues[i].red << 16)
| ((unsigned int)ledValues[i].green << 8)
| ledValues[i].blue;
for (int j=SPI_BYTES_PER_LED - 1; j>=0; j--) {
_spiBuffer[spi_ptr+j] = bitpair_to_byte[ colorBits & 0x3 ];
colorBits >>= 2;
}
spi_ptr += SPI_BYTES_PER_LED;
}
_spiBuffer[spi_ptr++] = 0;
_spiBuffer[spi_ptr++] = 0;
_spiBuffer[spi_ptr++] = 0;
return writeBytes(spi_size, _spiBuffer.data());
}
int LedDeviceWs2812SPI::switchOff()
{
return write(std::vector<ColorRgb>(mLedCount, ColorRgb{0,0,0}));
}