Files
NeoPixelBus/src/internal/methods/TwoWireSpiImple.h
T

226 lines
5.2 KiB
C++
Raw Normal View History

2019-09-22 22:00:00 -07:00
/*-------------------------------------------------------------------------
NeoPixel library helper functions for DotStars using SPI (APA102/LPD8806).
2019-09-22 22:00:00 -07:00
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>
2020-04-03 01:10:55 -07:00
class SpiSpeed40Mhz
{
public:
2021-02-16 10:23:02 -08:00
typedef NeoNoSettings SettingsObject;
SpiSpeed40Mhz() {};
2022-04-22 12:35:14 -07:00
static void applySettings([[maybe_unused]] const SettingsObject& settings) {}
2021-02-16 10:23:02 -08:00
2024-03-16 11:21:56 -07:00
static constexpr uint32_t Clock = 40000000L;
2020-04-03 01:10:55 -07:00
};
class SpiSpeed30Mhz
{
public:
typedef NeoNoSettings SettingsObject;
SpiSpeed30Mhz() {};
2022-04-22 12:35:14 -07:00
static void applySettings([[maybe_unused]] const SettingsObject& settings) {}
2024-03-16 11:21:56 -07:00
static constexpr uint32_t Clock = 30000000L;
};
2019-09-22 22:00:00 -07:00
class SpiSpeed20Mhz
{
public:
2021-02-16 10:23:02 -08:00
typedef NeoNoSettings SettingsObject;
SpiSpeed20Mhz() {};
2022-04-22 12:35:14 -07:00
static void applySettings([[maybe_unused]] const SettingsObject& settings) {}
2021-02-16 10:23:02 -08:00
2024-03-16 11:21:56 -07:00
static constexpr uint32_t Clock = 20000000L;
2019-09-22 22:00:00 -07:00
};
class SpiSpeed15Mhz
{
public:
typedef NeoNoSettings SettingsObject;
SpiSpeed15Mhz() {};
2022-04-22 12:35:14 -07:00
static void applySettings([[maybe_unused]] const SettingsObject& settings) {}
2024-03-16 11:21:56 -07:00
static constexpr uint32_t Clock = 15000000L;
};
2019-09-22 22:00:00 -07:00
class SpiSpeed10Mhz
{
public:
2021-02-16 10:23:02 -08:00
typedef NeoNoSettings SettingsObject;
SpiSpeed10Mhz() {};
2022-04-22 12:35:14 -07:00
static void applySettings([[maybe_unused]] const SettingsObject& settings) {}
2021-02-16 10:23:02 -08:00
2024-03-16 11:21:56 -07:00
static constexpr uint32_t Clock = 10000000L;
2019-09-22 22:00:00 -07:00
};
class SpiSpeed5Mhz
{
public:
typedef NeoNoSettings SettingsObject;
SpiSpeed5Mhz() {};
2022-04-22 12:35:14 -07:00
static void applySettings([[maybe_unused]] const SettingsObject& settings) {}
2024-03-16 11:21:56 -07:00
static constexpr uint32_t Clock = 5000000L;
};
2019-09-22 22:00:00 -07:00
class SpiSpeed2Mhz
{
public:
2021-02-16 10:23:02 -08:00
typedef NeoNoSettings SettingsObject;
SpiSpeed2Mhz() {};
2022-04-22 12:35:14 -07:00
static void applySettings([[maybe_unused]] const SettingsObject& settings) {}
2021-02-16 10:23:02 -08:00
2024-03-16 11:21:56 -07:00
static constexpr uint32_t Clock = 2000000L;
2019-09-22 22:00:00 -07:00
};
2021-02-10 13:36:05 -08:00
class SpiSpeed1Mhz
{
public:
2021-02-16 10:23:02 -08:00
typedef NeoNoSettings SettingsObject;
SpiSpeed1Mhz() {};
2022-04-22 12:35:14 -07:00
static void applySettings([[maybe_unused]] const SettingsObject& settings) {}
2021-02-16 10:23:02 -08:00
2024-03-16 11:21:56 -07:00
static constexpr uint32_t Clock = 1000000L;
2021-02-10 13:36:05 -08:00
};
class SpiSpeed500Khz
{
public:
2021-02-16 10:23:02 -08:00
typedef NeoNoSettings SettingsObject;
SpiSpeed500Khz() {};
2022-04-22 12:35:14 -07:00
static void applySettings([[maybe_unused]] const SettingsObject& settings) {}
2021-02-16 10:23:02 -08:00
2024-03-16 11:21:56 -07:00
static constexpr uint32_t Clock = 500000L;
2021-02-10 13:36:05 -08:00
};
2021-02-16 10:23:02 -08:00
class NeoSpiSettings
{
public:
NeoSpiSettings(uint32_t clock) :
Clock(clock)
{
}
uint32_t Clock;
};
class SpiSpeedHz
{
public:
typedef NeoSpiSettings SettingsObject;
SpiSpeedHz() :
2024-03-16 11:21:56 -07:00
Clock(SpiSpeed10Mhz::Clock)
2021-02-16 10:23:02 -08:00
{};
2022-04-22 12:35:14 -07:00
void applySettings([[maybe_unused]] const SettingsObject& settings)
2021-02-16 10:23:02 -08:00
{
Clock = settings.Clock;
}
uint32_t Clock;
};
2024-03-16 11:21:56 -07:00
template <typename T_SPISPEED>
class TwoWireSpiImple
2019-09-22 22:00:00 -07:00
{
public:
2021-02-16 10:23:02 -08:00
typedef typename T_SPISPEED::SettingsObject SettingsObject;
2020-09-20 10:29:49 -07:00
TwoWireSpiImple(uint8_t, uint8_t) // clock and data pins ignored for hardware SPI
{
}
2019-09-22 22:00:00 -07:00
2020-09-20 10:29:49 -07:00
~TwoWireSpiImple()
{
SPI.end();
}
2019-09-22 22:00:00 -07:00
#if defined(ARDUINO_ARCH_ESP32)
2020-09-20 10:29:49 -07:00
// for cases where hardware SPI can have pins changed
void begin(int8_t sck, int8_t miso, int8_t mosi, int8_t ss)
{
SPI.begin(sck, miso, mosi, ss);
}
2019-09-22 22:00:00 -07:00
#endif
2020-09-20 10:29:49 -07:00
void begin()
{
SPI.begin();
}
2019-09-22 22:00:00 -07:00
2020-09-20 10:29:49 -07:00
void beginTransaction()
{
2021-02-16 10:23:02 -08:00
SPI.beginTransaction(SPISettings(_speed.Clock, MSBFIRST, SPI_MODE0));
2020-09-20 10:29:49 -07:00
}
2019-09-22 22:00:00 -07:00
2020-09-20 10:29:49 -07:00
void endTransaction()
{
SPI.endTransaction();
}
2019-09-22 22:00:00 -07:00
2020-09-20 10:29:49 -07:00
void transmitByte(uint8_t data)
{
SPI.transfer(data);
}
2019-09-22 22:00:00 -07:00
2020-09-20 10:29:49 -07:00
void transmitBytes(const uint8_t* data, size_t dataSize)
{
2019-09-22 22:00:00 -07:00
#if defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ESP32)
2020-09-20 10:29:49 -07:00
// ESPs have a method to write without inplace overwriting the send buffer
// since we don't care what gets received, use it for performance
// FIX: but for what ever reason on Esp32, its not const
SPI.writeBytes(const_cast<uint8_t*>(data), dataSize);
2019-09-22 22:00:00 -07:00
#else
2020-09-20 10:29:49 -07:00
// default ARDUINO transfer inplace overwrites the send buffer
// which is bad in this case, so we have to send one byte at a time
const uint8_t* endData = data + dataSize;
while (data < endData)
{
SPI.transfer(*data++);
}
2019-09-22 22:00:00 -07:00
#endif
2020-09-20 10:29:49 -07:00
}
2019-09-22 22:00:00 -07:00
2022-04-22 12:35:14 -07:00
void applySettings([[maybe_unused]] const SettingsObject& settings)
2021-02-16 10:23:02 -08:00
{
_speed.applySettings(settings);
}
2019-09-22 22:00:00 -07:00
private:
2021-02-16 10:23:02 -08:00
T_SPISPEED _speed;
2019-09-22 22:00:00 -07:00
};