Files
NeoPixelBus/src/internal/Ws2801GenericMethod.h
T

152 lines
4.2 KiB
C++
Raw Normal View History

2019-12-31 00:29:15 -08:00
/*-------------------------------------------------------------------------
NeoPixel library helper functions for WS2801
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 Ws2801MethodBase
{
public:
2021-02-16 10:23:02 -08:00
typedef typename T_TWOWIRE::SettingsObject SettingsObject;
2020-09-20 10:29:49 -07:00
Ws2801MethodBase(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
_wire(pinClock, pinData)
2019-12-31 00:29:15 -08: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-12-31 00:29:15 -08:00
}
#if !defined(__AVR_ATtiny85__) && !defined(ARDUINO_attiny)
2020-09-20 10:29:49 -07:00
Ws2801MethodBase(uint16_t pixelCount, size_t elementSize, size_t settingsSize) :
Ws2801MethodBase(SCK, MOSI, pixelCount, elementSize, settingsSize)
{
}
2019-12-31 00:29:15 -08:00
#endif
~Ws2801MethodBase()
{
2020-01-12 18:54:00 -08:00
free(_data);
2019-12-31 00:29:15 -08:00
}
bool IsReadyToUpdate() const
{
uint32_t delta = micros() - _endTime;
return (delta >= 500);
}
#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);
2021-01-25 15:13:56 -08:00
_endTime = micros();
2020-09-20 10:29:49 -07:00
}
2019-12-31 00:29:15 -08:00
#endif
void Initialize()
{
2020-09-20 10:29:49 -07:00
_wire.begin();
2019-12-31 00:29:15 -08:00
_endTime = micros();
}
void Update(bool)
{
while (!IsReadyToUpdate())
{
#if !defined(ARDUINO_TEEONARDU_LEO) && !defined(ARDUINO_TEEONARDU_FLORA)
yield(); // allows for system yield if needed
#endif
}
2020-09-20 10:29:49 -07:00
_wire.beginTransaction();
2019-12-31 00:29:15 -08:00
// data
2020-09-20 10:29:49 -07:00
_wire.transmitBytes(_data, _sizeData);
2019-12-31 00:29:15 -08:00
2020-09-20 10:29:49 -07:00
_wire.endTransaction();
2019-12-31 00:29:15 -08:00
// save EOD time for latch on next call
_endTime = micros();
}
2023-01-27 10:12:48 -08:00
bool AlwaysUpdate()
{
// this method requires update to be called only if changes to buffer
return false;
}
2020-01-12 18:54:00 -08:00
uint8_t* getData() const
2019-12-31 00:29:15 -08:00
{
2020-01-12 18:54:00 -08:00
return _data;
2019-12-31 00:29:15 -08:00
};
2020-01-12 18:54:00 -08:00
size_t getDataSize() const
2019-12-31 00:29:15 -08:00
{
2020-01-12 18:54:00 -08:00
return _sizeData;
2019-12-31 00:29:15 -08:00
};
2022-05-07 14:35:47 -07:00
void applySettings([[maybe_unused]] const SettingsObject& settings)
2021-02-16 10:23:02 -08:00
{
_wire.applySettings(settings);
}
2019-12-31 00:29:15 -08:00
private:
2020-01-12 18:54:00 -08:00
const size_t _sizeData; // Size of '_data' buffer below
2019-12-31 00:29:15 -08:00
2020-01-12 18:54:00 -08:00
uint32_t _endTime; // Latch timing reference
T_TWOWIRE _wire;
uint8_t* _data; // Holds LED color values
2019-12-31 00:29:15 -08:00
};
2023-04-03 16:48:08 -07:00
typedef Ws2801MethodBase<TwoWireBitBangImple> Ws2801Method;
2019-12-31 00:29:15 -08:00
#if !defined(__AVR_ATtiny85__) && !defined(ARDUINO_attiny)
#include "TwoWireSpiImple.h"
2023-04-03 16:48:08 -07:00
typedef Ws2801MethodBase<TwoWireSpiImple<SpiSpeed20Mhz>> Ws2801Spi20MhzMethod;
typedef Ws2801MethodBase<TwoWireSpiImple<SpiSpeed10Mhz>> Ws2801Spi10MhzMethod;
typedef Ws2801MethodBase<TwoWireSpiImple<SpiSpeed5Mhz>> Ws2801Spi5MhzMethod;
typedef Ws2801MethodBase<TwoWireSpiImple<SpiSpeed2Mhz>> Ws2801Spi2MhzMethod;
typedef Ws2801MethodBase<TwoWireSpiImple<SpiSpeed1Mhz>> Ws2801Spi1MhzMethod;
typedef Ws2801MethodBase<TwoWireSpiImple<SpiSpeed500Khz>> Ws2801Spi500KhzMethod;
2021-02-16 10:46:20 -08:00
2023-04-03 16:48:08 -07:00
typedef Ws2801MethodBase<TwoWireSpiImple<SpiSpeedHz>> Ws2801SpiHzMethod;
2021-02-16 10:46:20 -08:00
2023-04-03 16:48:08 -07:00
typedef Ws2801Spi10MhzMethod Ws2801SpiMethod;
2019-12-31 00:29:15 -08:00
#endif