Skip to content

Commit 74fb97c

Browse files
committed
Update DriverSpiSk6812SPI.cpp
1 parent bcdc43e commit 74fb97c

File tree

1 file changed

+12
-21
lines changed

1 file changed

+12
-21
lines changed

sources/led-drivers/spi/DriverSpiSk6812SPI.cpp

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
#include <led-drivers/spi/DriverSpiSk6812SPI.h>
22
#include <infinite-color-engine/ColorSpace.h>
33

4+
namespace {
5+
constexpr int SPI_FRAME_END_LATCH_BYTES = 4;
6+
}
7+
48
DriverSpiSk6812SPI::DriverSpiSk6812SPI(const QJsonObject& deviceConfig)
59
: ProviderSpi(deviceConfig)
610
, _whiteAlgorithm(RGBW::WhiteAlgorithm::HYPERSERIAL_COLD_WHITE)
@@ -11,9 +15,9 @@ DriverSpiSk6812SPI::DriverSpiSk6812SPI(const QJsonObject& deviceConfig)
1115
, SPI_BYTES_PER_COLOUR(4)
1216
, bitpair_to_byte{
1317
0b10001000,
14-
0b10001100,
15-
0b11001000,
16-
0b11001100}
18+
0b10001110,
19+
0b11101000,
20+
0b11101110}
1721
, _enable_ice_rgbw(false)
1822
, _ice_white_temperatur{ 1.0f, 1.0f, 1.0f }
1923
, _ice_white_mixer_threshold(0.0f)
@@ -23,7 +27,7 @@ DriverSpiSk6812SPI::DriverSpiSk6812SPI(const QJsonObject& deviceConfig)
2327

2428
bool DriverSpiSk6812SPI::init(QJsonObject deviceConfig)
2529
{
26-
deviceConfig["rate"] = 3000000;
30+
deviceConfig["rate"] = 3200000;
2731

2832
bool isInitOK = false;
2933

@@ -71,10 +75,9 @@ bool DriverSpiSk6812SPI::init(QJsonObject deviceConfig)
7175

7276
auto rateHz = getRate();
7377

74-
WarningIf((rateHz < 2050000 || rateHz > 4000000), _log, "SPI rate {:d} outside recommended range (2050000 -> 4000000)", rateHz);
78+
WarningIf((rateHz < 3100000 || rateHz > 3300000), _log, "SPI rate {:d} outside recommended range (3200000)", rateHz);
7579

76-
const int SPI_FRAME_END_LATCH_BYTES = 3;
77-
_ledBuffer.resize(_ledRGBWCount * SPI_BYTES_PER_COLOUR + SPI_FRAME_END_LATCH_BYTES, 0x00);
80+
_ledBuffer.assign(_ledRGBWCount * SPI_BYTES_PER_COLOUR + SPI_FRAME_END_LATCH_BYTES, 0x00);
7881

7982
isInitOK = true;
8083
}
@@ -92,9 +95,7 @@ int DriverSpiSk6812SPI::writeFiniteColors(const std::vector<ColorRgb>& ledValues
9295
Warning(_log, "Sk6812SPI led's number has changed (old: {:d}, new: {:d}). Rebuilding buffer.", _ledCount, ledValues.size());
9396
_ledCount = static_cast<uint>(ledValues.size());
9497

95-
const int SPI_FRAME_END_LATCH_BYTES = 3;
96-
_ledBuffer.resize(0, 0x00);
97-
_ledBuffer.resize(_ledRGBWCount * SPI_BYTES_PER_COLOUR + SPI_FRAME_END_LATCH_BYTES, 0x00);
98+
_ledBuffer.assign(SPI_BYTES_PER_LED * _ledCount + SPI_FRAME_END_LATCH_BYTES, 0x00);
9899
}
99100

100101
for (const ColorRgb& color : ledValues)
@@ -116,10 +117,6 @@ int DriverSpiSk6812SPI::writeFiniteColors(const std::vector<ColorRgb>& ledValues
116117
spi_ptr += SPI_BYTES_PER_LED;
117118
}
118119

119-
_ledBuffer[spi_ptr++] = 0;
120-
_ledBuffer[spi_ptr++] = 0;
121-
_ledBuffer[spi_ptr++] = 0;
122-
123120
return writeBytes(static_cast<unsigned int>(_ledBuffer.size()), _ledBuffer.data());
124121
}
125122

@@ -138,9 +135,7 @@ std::pair<bool, int> DriverSpiSk6812SPI::writeInfiniteColors(SharedOutputColors
138135
Warning(_log, "Sk6812SPI led's number has changed (old: {:d}, new: {:d}). Rebuilding buffer.", _ledCount, nonlinearRgbColors->size());
139136
_ledCount = static_cast<uint>(nonlinearRgbColors->size());
140137

141-
const int SPI_FRAME_END_LATCH_BYTES = 3;
142-
_ledBuffer.resize(0, 0x00);
143-
_ledBuffer.resize(_ledRGBWCount * SPI_BYTES_PER_COLOUR + SPI_FRAME_END_LATCH_BYTES, 0x00);
138+
_ledBuffer.assign(SPI_BYTES_PER_LED * _ledCount + SPI_FRAME_END_LATCH_BYTES, 0x00);
144139
}
145140

146141
////////////////////////////////////////////////////////////////////////////
@@ -169,10 +164,6 @@ std::pair<bool, int> DriverSpiSk6812SPI::writeInfiniteColors(SharedOutputColors
169164
spi_ptr += SPI_BYTES_PER_LED;
170165
}
171166

172-
_ledBuffer[spi_ptr++] = 0;
173-
_ledBuffer[spi_ptr++] = 0;
174-
_ledBuffer[spi_ptr++] = 0;
175-
176167
return { true, writeBytes(static_cast<unsigned int>(_ledBuffer.size()), _ledBuffer.data()) };
177168
}
178169

0 commit comments

Comments
 (0)