Files
NeoPixelBus/src/internal/Lpd8806GenericMethod.h
T

142 lines
4.0 KiB
C++
Raw Normal View History

2019-09-22 22:00:00 -07:00
/*-------------------------------------------------------------------------
NeoPixel library helper functions for LPD8806 using general Pins
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__)
#include "TwoWireBitBangImpleAvr.h"
#else
#include "TwoWireBitBangImple.h"
#endif
template<typename T_TWOWIRE> class Lpd8806MethodBase
{
public:
2021-02-16 10:23:02 -08:00
typedef typename T_TWOWIRE::SettingsObject SettingsObject;
2020-09-20 10:29:49 -07:00
Lpd8806MethodBase(uint8_t pinClock, uint8_t pinData, uint16_t pixelCount, size_t elementSize, size_t settingsSize) :
2020-01-12 18:54:00 -08:00
_sizeData(pixelCount * elementSize + settingsSize),
2020-09-20 10:29:49 -07:00
_sizeFrame((pixelCount + 31) / 32),
_wire(pinClock, pinData)
2019-09-22 22:00:00 -07:00
{
2020-01-12 18:54:00 -08:00
_data = static_cast<uint8_t*>(malloc(_sizeData));
2021-02-10 20:25:47 -08:00
// data cleared later in Begin()
2019-09-22 22:00:00 -07:00
}
2019-12-20 23:48:00 -08:00
#if !defined(__AVR_ATtiny85__) && !defined(ARDUINO_attiny)
2020-09-20 10:29:49 -07:00
Lpd8806MethodBase(uint16_t pixelCount, size_t elementSize, size_t settingsSize) :
Lpd8806MethodBase(SCK, MOSI, pixelCount, elementSize, settingsSize)
{
}
2019-12-20 23:48:00 -08:00
#endif
2019-09-22 22:00:00 -07:00
~Lpd8806MethodBase()
{
2020-01-12 18:54:00 -08:00
free(_data);
2019-09-22 22:00:00 -07:00
}
bool IsReadyToUpdate() const
{
return true; // dot stars don't have a required delay
}
#if defined(ARDUINO_ARCH_ESP32)
2020-09-20 10:29:49 -07:00
void Initialize(int8_t sck, int8_t miso, int8_t mosi, int8_t ss)
{
_wire.begin(sck, miso, mosi, ss);
}
2019-09-22 22:00:00 -07:00
#endif
void Initialize()
{
2020-09-20 10:29:49 -07:00
_wire.begin();
2019-09-22 22:00:00 -07:00
}
void Update(bool)
{
2020-09-20 10:29:49 -07:00
_wire.beginTransaction();
2019-09-22 22:00:00 -07:00
// start frame
2020-09-20 10:29:49 -07:00
for (size_t frameByte = 0; frameByte < _sizeFrame; frameByte++)
{
_wire.transmitByte(0x00);
}
2019-09-22 22:00:00 -07:00
// data
2020-09-20 10:29:49 -07:00
_wire.transmitBytes(_data, _sizeData);
2019-09-22 22:00:00 -07:00
// end frame
2020-09-20 10:29:49 -07:00
for (size_t frameByte = 0; frameByte < _sizeFrame; frameByte++)
{
_wire.transmitByte(0xff);
}
_wire.endTransaction();
2019-09-22 22:00:00 -07:00
}
2020-01-12 18:54:00 -08:00
uint8_t* getData() const
2019-09-22 22:00:00 -07:00
{
2020-01-12 18:54:00 -08:00
return _data;
2019-09-22 22:00:00 -07:00
};
2020-01-12 18:54:00 -08:00
size_t getDataSize() const
2019-09-22 22:00:00 -07:00
{
2020-01-12 18:54:00 -08:00
return _sizeData;
2019-09-22 22:00:00 -07:00
};
2021-02-16 10:23:02 -08:00
void applySettings(const SettingsObject& settings)
{
_wire.applySettings(settings);
}
2019-09-22 22:00:00 -07:00
private:
2020-09-20 10:29:49 -07:00
const size_t _sizeData; // Size of '_data' buffer below
const size_t _sizeFrame;
2019-09-22 22:00:00 -07:00
2020-09-20 10:29:49 -07:00
T_TWOWIRE _wire;
2020-01-12 18:54:00 -08:00
uint8_t* _data; // Holds LED color values
2019-09-22 22:00:00 -07:00
};
typedef Lpd8806MethodBase<TwoWireBitBangImple> Lpd8806Method;
2019-12-20 23:48:00 -08:00
#if !defined(__AVR_ATtiny85__) && !defined(ARDUINO_attiny)
2019-09-22 22:00:00 -07:00
#include "TwoWireSpiImple.h"
typedef Lpd8806MethodBase<TwoWireSpiImple<SpiSpeed20Mhz>> Lpd8806Spi20MhzMethod;
typedef Lpd8806MethodBase<TwoWireSpiImple<SpiSpeed10Mhz>> Lpd8806Spi10MhzMethod;
typedef Lpd8806MethodBase<TwoWireSpiImple<SpiSpeed2Mhz>> Lpd8806Spi2MhzMethod;
2021-02-10 13:36:05 -08:00
typedef Lpd8806MethodBase<TwoWireSpiImple<SpiSpeed1Mhz>> Lpd8806Spi1MhzMethod;
typedef Lpd8806MethodBase<TwoWireSpiImple<SpiSpeed500Khz>> Lpd8806Spi500KhzMethod;
2021-02-16 10:46:20 -08:00
typedef Lpd8806MethodBase<TwoWireSpiImple<SpiSpeedHz>> Lpd8806SpiHzMethod;
2019-09-22 22:00:00 -07:00
typedef Lpd8806Spi10MhzMethod Lpd8806SpiMethod;
#endif