DotStar support

APA102
This commit is contained in:
Michael Miller
2016-09-01 18:34:21 -07:00
parent 95b32de1ef
commit c2bb0fe855
8 changed files with 721 additions and 5 deletions
+153
View File
@@ -0,0 +1,153 @@
/*-------------------------------------------------------------------------
NeoPixel library helper functions for DotStars on AVR (APA102).
Written by Michael C. Miller.
I invest time and resources providing this open source code,
please support me by dontating (see https://github.com/Makuna/NeoPixelBus)
-------------------------------------------------------------------------
This file is part of the Makuna/NeoPixelBus library.
NeoPixelBus is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
NeoPixelBus is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with NeoPixel. If not, see
<http://www.gnu.org/licenses/>.
-------------------------------------------------------------------------*/
#pragma once
// must also check for arm due to Teensy incorrectly having ARDUINO_ARCH_AVR set
#if defined(ARDUINO_ARCH_AVR) && !defined(__arm__)
class DotStarAvrMethod
{
public:
DotStarAvrMethod(uint8_t pinClock, uint8_t pinData, uint16_t pixelCount, size_t elementSize) :
_pinClock(pinClock),
_pinData(pinData),
_sizePixels(pixelCount * elementSize)
{
pinMode(pinClock, OUTPUT);
pinMode(pinData, OUTPUT);
_pixels = (uint8_t*)malloc(_sizePixels);
memset(_pixels, 0, _sizePixels);
_portClock = portOutputRegister(digitalPinToPort(_pinClock));
_pinMaskClock = digitalPinToBitMask(_pinClock);
_portData = portOutputRegister(digitalPinToPort(_pinData));
_pinMaskData = digitalPinToBitMask(_pinData);
}
~DotStarAvrMethod()
{
pinMode(_pinClock, INPUT);
pinMode(_pinData, INPUT);
free(_pixels);
}
bool IsReadyToUpdate() const
{
return true; // dot stars don't have a required delay
}
void Initialize()
{
digitalWrite(_pinClock, LOW);
digitalWrite(_pinData, LOW);
}
void Update()
{
// start frame
for (int startFrameByte = 0; startFrameByte < 4; startFrameByte++)
{
_transmitByte(0x00);
}
// data
uint8_t* data = _pixels;
const uint8_t* endData = _pixels + _sizePixels;
while (data < endData)
{
_transmitByte(*data++);
}
// 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);
}
// set clock and data back to low between updates
digitalWrite(_pinData, LOW);
}
uint8_t* getPixels() const
{
return _pixels;
};
size_t getPixelsSize() const
{
return _sizePixels;
};
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
uint8_t* _pixels; // Holds LED color values
volatile uint8_t* _portData; // Output PORT register
uint8_t _pinMaskData; // Output PORT bitmask
volatile uint8_t* _portClock; // Output PORT register
uint8_t _pinMaskClock; // Output PORT bitmask
void _transmitByte(uint8_t data)
{
for (int bit = 7; bit >= 0; bit--)
{
// set data bit on pin
// digitalWrite(_pinData, (data & 0x80) == 0x80 ? HIGH : LOW);
if (data & 0x80)
{
*_portData |= _pinMaskData;
}
else
{
*_portData &= ~_pinMaskData;
}
// set clock high as data is ready
// digitalWrite(_pinClock, HIGH);
*_portClock |= _pinMaskClock;
// done between clock toggle to give a little time
data <<= 1;
// set clock low as data pin is changed
// digitalWrite(_pinClock, LOW);
*_portClock &= ~_pinMaskClock;
}
}
};
typedef DotStarAvrMethod DotStarMethod;
#endif
+216
View File
@@ -0,0 +1,216 @@
/*-------------------------------------------------------------------------
DotStarColorFeatures provides feature classes to describe color order and
color depth for NeoPixelBus template class when used with DotStars
Written by Michael C. Miller.
I invest time and resources providing this open source code,
please support me by dontating (see https://github.com/Makuna/NeoPixelBus)
-------------------------------------------------------------------------
This file is part of the Makuna/NeoPixelBus library.
NeoPixelBus is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
NeoPixelBus is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with NeoPixel. If not, see
<http://www.gnu.org/licenses/>.
-------------------------------------------------------------------------*/
#pragma once
class DotStar4Elements
{
public:
static const size_t PixelSize = 4;
static uint8_t* getPixelAddress(uint8_t* pPixels, uint16_t indexPixel)
{
return pPixels + indexPixel * PixelSize;
}
static const uint8_t* getPixelAddress(const uint8_t* pPixels, uint16_t indexPixel)
{
return pPixels + indexPixel * PixelSize;
}
static void replicatePixel(uint8_t* pPixelDest, const uint8_t* pPixelSrc, uint16_t count)
{
uint8_t* pEnd = pPixelDest + (count * PixelSize);
while (pPixelDest < pEnd)
{
*pPixelDest++ = pPixelSrc[0];
*pPixelDest++ = pPixelSrc[1];
*pPixelDest++ = pPixelSrc[2];
*pPixelDest++ = pPixelSrc[3];
}
}
static void movePixelsInc(uint8_t* pPixelDest, const uint8_t* pPixelSrc, uint16_t count)
{
uint8_t* pEnd = pPixelDest + (count * PixelSize);
while (pPixelDest < pEnd)
{
*pPixelDest++ = *pPixelSrc++;
*pPixelDest++ = *pPixelSrc++;
*pPixelDest++ = *pPixelSrc++;
*pPixelDest++ = *pPixelSrc++;
}
}
static void movePixelsInc_P(uint8_t* pPixelDest, PGM_VOID_P pPixelSrc, uint16_t count)
{
uint8_t* pEnd = pPixelDest + (count * PixelSize);
const uint8_t* pSrc = (const uint8_t*)pPixelSrc;
while (pPixelDest < pEnd)
{
*pPixelDest++ = pgm_read_byte(pSrc++);
*pPixelDest++ = pgm_read_byte(pSrc++);
*pPixelDest++ = pgm_read_byte(pSrc++);
*pPixelDest++ = pgm_read_byte(pSrc++);
}
}
static void movePixelsDec(uint8_t* pPixelDest, const uint8_t* pPixelSrc, uint16_t count)
{
uint8_t* pDestBack = pPixelDest + (count * PixelSize);
const uint8_t* pSrcBack = pPixelSrc + (count * PixelSize);
while (pDestBack > pPixelDest)
{
*--pDestBack = *--pSrcBack;
*--pDestBack = *--pSrcBack;
*--pDestBack = *--pSrcBack;
*--pDestBack = *--pSrcBack;
}
}
typedef RgbwColor ColorObject;
};
class DotStarBgrFeature : public DotStar4Elements
{
public:
static void applyPixelColor(uint8_t* pPixels, uint16_t indexPixel, ColorObject color)
{
uint8_t* p = getPixelAddress(pPixels, indexPixel);
*p++ = 0xff; // upper three bits are always 111 and brightness at max
*p++ = color.B;
*p++ = color.G;
*p = color.R;
}
static ColorObject retrievePixelColor(uint8_t* pPixels, uint16_t indexPixel)
{
ColorObject color;
uint8_t* p = getPixelAddress(pPixels, indexPixel);
p++; // ignore the first byte
color.B = *p++;
color.G = *p++;
color.R = *p;
return color;
}
static ColorObject retrievePixelColor_P(PGM_VOID_P pPixels, uint16_t indexPixel)
{
ColorObject color;
const uint8_t* p = getPixelAddress((const uint8_t*)pPixels, indexPixel);
pgm_read_byte(p++); // ignore the first byte
color.B = pgm_read_byte(p++);
color.G = pgm_read_byte(p++);
color.R = pgm_read_byte(p);
return color;
}
};
class DotStarLbgrFeature : public DotStar4Elements
{
public:
static void applyPixelColor(uint8_t* pPixels, uint16_t indexPixel, ColorObject color)
{
uint8_t* p = getPixelAddress(pPixels, indexPixel);
*p++ = 0xE0 | min(color.W, 31); // upper three bits are always 111
*p++ = color.B;
*p++ = color.G;
*p = color.R;
}
static ColorObject retrievePixelColor(uint8_t* pPixels, uint16_t indexPixel)
{
ColorObject color;
uint8_t* p = getPixelAddress(pPixels, indexPixel);
color.W = (*p++) & 0x1F; // mask out upper three bits
color.B = *p++;
color.G = *p++;
color.R = *p;
return color;
}
static ColorObject retrievePixelColor_P(PGM_VOID_P pPixels, uint16_t indexPixel)
{
ColorObject color;
const uint8_t* p = getPixelAddress((const uint8_t*)pPixels, indexPixel);
color.W = pgm_read_byte(p++) & 0x1F; // mask out upper three bits
color.B = pgm_read_byte(p++);
color.G = pgm_read_byte(p++);
color.R = pgm_read_byte(p);
return color;
}
};
class DotStarWbgrFeature : public DotStar4Elements
{
public:
static void applyPixelColor(uint8_t* pPixels, uint16_t indexPixel, ColorObject color)
{
uint8_t* p = getPixelAddress(pPixels, indexPixel);
*p++ = color.W;
*p++ = color.B;
*p++ = color.G;
*p = color.R;
}
static ColorObject retrievePixelColor(uint8_t* pPixels, uint16_t indexPixel)
{
ColorObject color;
uint8_t* p = getPixelAddress(pPixels, indexPixel);
color.W = *p++;
color.B = *p++;
color.G = *p++;
color.R = *p;
return color;
}
static ColorObject retrievePixelColor_P(PGM_VOID_P pPixels, uint16_t indexPixel)
{
ColorObject color;
const uint8_t* p = getPixelAddress((const uint8_t*)pPixels, indexPixel);
color.W = pgm_read_byte(p++);
color.B = pgm_read_byte(p++);
color.G = pgm_read_byte(p++);
color.R = pgm_read_byte(p);
return color;
}
};
+128
View File
@@ -0,0 +1,128 @@
/*-------------------------------------------------------------------------
NeoPixel library helper functions for DotStars using general Pins (APA102).
Written by Michael C. Miller.
I invest time and resources providing this open source code,
please support me by dontating (see https://github.com/Makuna/NeoPixelBus)
-------------------------------------------------------------------------
This file is part of the Makuna/NeoPixelBus library.
NeoPixelBus is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
NeoPixelBus is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with NeoPixel. If not, see
<http://www.gnu.org/licenses/>.
-------------------------------------------------------------------------*/
#pragma once
class DotStarGenericMethod
{
public:
DotStarGenericMethod(uint8_t pinClock, uint8_t pinData, uint16_t pixelCount, size_t elementSize) :
_pinClock(pinClock),
_pinData(pinData),
_sizePixels(pixelCount * elementSize)
{
pinMode(pinClock, OUTPUT);
pinMode(pinData, OUTPUT);
_pixels = (uint8_t*)malloc(_sizePixels);
memset(_pixels, 0, _sizePixels);
}
~DotStarGenericMethod()
{
pinMode(_pinClock, INPUT);
pinMode(_pinData, INPUT);
free(_pixels);
}
bool IsReadyToUpdate() const
{
return true; // dot stars don't have a required delay
}
void Initialize()
{
digitalWrite(_pinClock, LOW);
digitalWrite(_pinData, LOW);
}
void Update()
{
// start frame
for (int startFrameByte = 0; startFrameByte < 4; startFrameByte++)
{
_transmitByte(0x00);
}
// data
uint8_t* data = _pixels;
const uint8_t* endData = _pixels + _sizePixels;
while (data < endData)
{
_transmitByte(*data++);
}
// 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);
}
// set clock and data back to low between updates
digitalWrite(_pinData, LOW);
}
uint8_t* getPixels() const
{
return _pixels;
};
size_t getPixelsSize() const
{
return _sizePixels;
};
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
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;
+110
View File
@@ -0,0 +1,110 @@
/*-------------------------------------------------------------------------
NeoPixel library helper functions for DotStars using SPI hardware (APA102).
Written by Michael C. Miller.
I invest time and resources providing this open source code,
please support me by dontating (see https://github.com/Makuna/NeoPixelBus)
-------------------------------------------------------------------------
This file is part of the Makuna/NeoPixelBus library.
NeoPixelBus is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as
published by the Free Software Foundation, either version 3 of
the License, or (at your option) any later version.
NeoPixelBus is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with NeoPixel. If not, see
<http://www.gnu.org/licenses/>.
-------------------------------------------------------------------------*/
#pragma once
#include <SPI.h>
class DotStarSpiMethod
{
public:
DotStarSpiMethod(uint16_t pixelCount, size_t elementSize) :
_sizePixels(pixelCount * elementSize)
{
_pixels = (uint8_t*)malloc(_sizePixels);
memset(_pixels, 0, _sizePixels);
}
~DotStarSpiMethod()
{
SPI.end();
free(_pixels);
}
bool IsReadyToUpdate() const
{
return true; // dot stars don't have a required delay
}
void Initialize()
{
SPI.begin();
#if defined(ARDUINO_ARCH_ESP8266)
SPI.setFrequency(8000000L);
#elif defined(ARDUINO_ARCH_AVR)
SPI.setClockDivider(SPI_CLOCK_DIV2); // 8 MHz (6 MHz on Pro Trinket 3V)
#else
SPI.setClockDivider((F_CPU + 4000000L) / 8000000L); // 8-ish MHz on Due
#endif
SPI.setBitOrder(MSBFIRST);
SPI.setDataMode(SPI_MODE0);
}
void Update()
{
// start frame
for (int startFrameByte = 0; startFrameByte < 4; startFrameByte++)
{
SPI.transfer(0x00);
}
// data
uint8_t* data = _pixels;
const uint8_t* endData = _pixels + _sizePixels;
while (data < endData)
{
SPI.transfer(*data++);
}
// 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++)
{
SPI.transfer(0xff);
}
}
uint8_t* getPixels() const
{
return _pixels;
};
size_t getPixelsSize() const
{
return _sizePixels;
};
private:
const size_t _sizePixels; // Size of '_pixels' buffer below
uint8_t* _pixels; // Holds LED color values
};
+1 -1
View File
@@ -90,7 +90,7 @@ struct HtmlColor
// ------------------------------------------------------------------------
HtmlColor(const RgbColor& color)
{
Color = color.R << 16 | color.G << 8 | color.B;
Color = (uint32_t)color.R << 16 | (uint32_t)color.G << 8 | (uint32_t)color.B;
}
// ------------------------------------------------------------------------
-1
View File
@@ -60,7 +60,6 @@ void ICACHE_RAM_ATTR bitbang_send_pixels_800(uint8_t* pixels, uint8_t* end, uint
// do the checks here while we are waiting on time to pass
uint32_t cyclesBit = ((subpix & mask)) ? CYCLES_800_T1H : CYCLES_800_T0H;
uint32_t cyclesNext = cyclesStart;
uint32_t delta;
// after we have done as much work as needed for this next bit
// now wait for the HIGH