Files
NeoPixelBus/src/internal/DotStarGenericMethod.h
T

127 lines
3.5 KiB
C++
Raw Normal View History

2016-09-01 18:34:21 -07:00
/*-------------------------------------------------------------------------
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
2019-09-22 22:00:00 -07:00
// 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
2016-09-01 18:34:21 -07:00
{
public:
2019-09-22 22:00:00 -07:00
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)
2016-09-01 18:34:21 -07:00
{
_pixels = (uint8_t*)malloc(_sizePixels);
memset(_pixels, 0, _sizePixels);
}
2019-09-22 22:00:00 -07:00
DotStarMethodBase(uint16_t pixelCount, size_t elementSize) :
DotStarMethodBase(SCK, MOSI, pixelCount, elementSize)
{
}
2016-09-01 18:34:21 -07:00
2019-09-22 22:00:00 -07:00
~DotStarMethodBase()
{
2016-09-01 18:34:21 -07:00
free(_pixels);
}
bool IsReadyToUpdate() const
{
return true; // dot stars don't have a required delay
}
2019-09-22 22:00:00 -07:00
#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
2016-09-01 18:34:21 -07:00
void Initialize()
{
2019-09-22 22:00:00 -07:00
_wire.begin();
2016-09-01 18:34:21 -07:00
}
2019-06-12 23:10:42 -07:00
void Update(bool)
2016-09-01 18:34:21 -07:00
{
2019-09-22 22:00:00 -07:00
const uint8_t startFrame[4] = { 0x00 };
_wire.beginTransaction();
2016-09-01 18:34:21 -07:00
// start frame
2019-09-22 22:00:00 -07:00
_wire.transmitBytes(startFrame, sizeof(startFrame));
2016-09-01 18:34:21 -07:00
// data
2019-09-22 22:00:00 -07:00
_wire.transmitBytes(_pixels, _sizePixels);
2016-09-01 18:34:21 -07:00
// end frame
2019-09-22 22:00:00 -07:00
// one bit for every two pixels with no less than 1 byte
for (size_t endFrameByte = 0; endFrameByte < _sizeEndFrame; endFrameByte++)
{
_wire.transmitByte(0xff);
}
2016-09-01 18:34:21 -07:00
2019-09-22 22:00:00 -07:00
_wire.endTransaction();
2016-09-01 18:34:21 -07:00
}
uint8_t* getPixels() const
{
return _pixels;
};
size_t getPixelsSize() const
{
return _sizePixels;
};
private:
2019-09-22 22:00:00 -07:00
const size_t _sizePixels; // Size of '_pixels' buffer below
const size_t _sizeEndFrame;
2016-09-01 18:34:21 -07:00
2019-09-22 22:00:00 -07:00
T_TWOWIRE _wire;
2016-09-01 18:34:21 -07:00
uint8_t* _pixels; // Holds LED color values
};
2019-09-22 22:00:00 -07:00
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
2016-09-01 18:34:21 -07:00