forked from Makuna/NeoPixelBus
Lpd8806 (#291)
* SpecificMethod * DotStar and Lpd8806 SPI speeds selectable
This commit is contained in:
1 parent
d83c0c7088
commit
0177b8d519
13 files changed
+686
-359
No files matched your search
@@ -26,26 +26,33 @@ License along with NeoPixel. If not, see
|
||||
|
||||
#pragma once
|
||||
|
||||
class DotStarGenericMethod
|
||||
// must also check for arm due to Teensy incorrectly having ARDUINO_ARCH_AVR set
|
||||
#if defined(ARDUINO_ARCH_AVR) && !defined(__arm__)
|
||||
#include "TwoWireBitBangImpleAvr.h"
|
||||
#else
|
||||
#include "TwoWireBitBangImple.h"
|
||||
#endif
|
||||
|
||||
|
||||
template<typename T_TWOWIRE> class DotStarMethodBase
|
||||
{
|
||||
public:
|
||||
DotStarGenericMethod(uint8_t pinClock, uint8_t pinData, uint16_t pixelCount, size_t elementSize) :
|
||||
_pinClock(pinClock),
|
||||
_pinData(pinData),
|
||||
_sizePixels(pixelCount * elementSize)
|
||||
DotStarMethodBase(uint8_t pinClock, uint8_t pinData, uint16_t pixelCount, size_t elementSize) :
|
||||
_sizePixels(pixelCount * elementSize),
|
||||
_sizeEndFrame((pixelCount + 15) / 16), // 16 = div 2 (bit for every two pixels) div 8 (bits to bytes)
|
||||
_wire(pinClock, pinData)
|
||||
{
|
||||
pinMode(pinClock, OUTPUT);
|
||||
pinMode(pinData, OUTPUT);
|
||||
|
||||
_pixels = (uint8_t*)malloc(_sizePixels);
|
||||
memset(_pixels, 0, _sizePixels);
|
||||
}
|
||||
|
||||
~DotStarGenericMethod()
|
||||
{
|
||||
pinMode(_pinClock, INPUT);
|
||||
pinMode(_pinData, INPUT);
|
||||
DotStarMethodBase(uint16_t pixelCount, size_t elementSize) :
|
||||
DotStarMethodBase(SCK, MOSI, pixelCount, elementSize)
|
||||
{
|
||||
}
|
||||
|
||||
~DotStarMethodBase()
|
||||
{
|
||||
free(_pixels);
|
||||
}
|
||||
|
||||
@@ -54,38 +61,38 @@ public:
|
||||
return true; // dot stars don't have a required delay
|
||||
}
|
||||
|
||||
#if defined(ARDUINO_ARCH_ESP32)
|
||||
void Initialize(int8_t sck, int8_t miso, int8_t mosi, int8_t ss)
|
||||
{
|
||||
_wire.begin(sck, miso, mosi, ss);
|
||||
}
|
||||
#endif
|
||||
|
||||
void Initialize()
|
||||
{
|
||||
digitalWrite(_pinClock, LOW);
|
||||
digitalWrite(_pinData, LOW);
|
||||
_wire.begin();
|
||||
}
|
||||
|
||||
void Update(bool)
|
||||
{
|
||||
const uint8_t startFrame[4] = { 0x00 };
|
||||
|
||||
_wire.beginTransaction();
|
||||
|
||||
// start frame
|
||||
for (int startFrameByte = 0; startFrameByte < 4; startFrameByte++)
|
||||
{
|
||||
_transmitByte(0x00);
|
||||
}
|
||||
_wire.transmitBytes(startFrame, sizeof(startFrame));
|
||||
|
||||
// data
|
||||
uint8_t* data = _pixels;
|
||||
const uint8_t* endData = _pixels + _sizePixels;
|
||||
while (data < endData)
|
||||
{
|
||||
_transmitByte(*data++);
|
||||
}
|
||||
_wire.transmitBytes(_pixels, _sizePixels);
|
||||
|
||||
// end frame
|
||||
// one bit for every two pixels with no less than 1 byte
|
||||
const uint16_t countEndFrameBytes = ((_sizePixels / 4) + 15) / 16;
|
||||
for (uint16_t endFrameByte = 0; endFrameByte < countEndFrameBytes; endFrameByte++)
|
||||
{
|
||||
_transmitByte(0xff);
|
||||
}
|
||||
// one bit for every two pixels with no less than 1 byte
|
||||
for (size_t endFrameByte = 0; endFrameByte < _sizeEndFrame; endFrameByte++)
|
||||
{
|
||||
_wire.transmitByte(0xff);
|
||||
}
|
||||
|
||||
// set clock and data back to low between updates
|
||||
digitalWrite(_pinData, LOW);
|
||||
_wire.endTransaction();
|
||||
}
|
||||
|
||||
uint8_t* getPixels() const
|
||||
@@ -99,30 +106,22 @@ public:
|
||||
};
|
||||
|
||||
private:
|
||||
const uint8_t _pinClock; // output pin number for clock line
|
||||
const uint8_t _pinData; // output pin number for data line
|
||||
const size_t _sizePixels; // Size of '_pixels' buffer below
|
||||
const size_t _sizePixels; // Size of '_pixels' buffer below
|
||||
const size_t _sizeEndFrame;
|
||||
|
||||
T_TWOWIRE _wire;
|
||||
uint8_t* _pixels; // Holds LED color values
|
||||
|
||||
void _transmitByte(uint8_t data)
|
||||
{
|
||||
for (int bit = 7; bit >= 0; bit--)
|
||||
{
|
||||
// set data bit on pin
|
||||
digitalWrite(_pinData, (data & 0x80) == 0x80 ? HIGH : LOW);
|
||||
|
||||
// set clock high as data is ready
|
||||
digitalWrite(_pinClock, HIGH);
|
||||
|
||||
data <<= 1;
|
||||
|
||||
// set clock low as data pin is changed
|
||||
digitalWrite(_pinClock, LOW);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
typedef DotStarGenericMethod DotStarMethod;
|
||||
typedef DotStarMethodBase<TwoWireBitBangImple> DotStarMethod;
|
||||
|
||||
#if !defined(__AVR_ATtiny85__)
|
||||
#include "TwoWireSpiImple.h"
|
||||
typedef DotStarMethodBase<TwoWireSpiImple<SpiSpeed20Mhz>> DotStarSpi20MhzMethod;
|
||||
typedef DotStarMethodBase<TwoWireSpiImple<SpiSpeed10Mhz>> DotStarSpi10MhzMethod;
|
||||
typedef DotStarMethodBase<TwoWireSpiImple<SpiSpeed2Mhz>> DotStarSpi2MhzMethod;
|
||||
typedef DotStarSpi10MhzMethod DotStarSpiMethod;
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user