Skip to content
This repository was archived by the owner on May 6, 2021. It is now read-only.

Commit 9f63b95

Browse files
committed
Rewrote the class to make it work with apa102 led strips
1 parent 4d7c888 commit 9f63b95

File tree

2 files changed

+16
-20
lines changed

2 files changed

+16
-20
lines changed

libsrc/leddevice/LedDeviceAPA102.cpp

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,35 +13,30 @@
1313

1414
LedDeviceAPA102::LedDeviceAPA102(const std::string& outputDevice, const unsigned baudrate) :
1515
LedSpiDevice(outputDevice, baudrate, 500000),
16-
mLedCount(0)
16+
_ledBuffer(0)
1717
{
1818
// empty
1919
}
2020

2121
int LedDeviceAPA102::write(const std::vector<ColorRgb> &ledValues)
2222
{
23-
mLedCount = ledValues.size();
24-
25-
const unsigned dataLen = 8 + (ledValues.size() * (sizeof(ColorRgb) + 1));
26-
const uint8_t data[dataLen] = { 0x00, 0x00, 0x00, 0x00 };
27-
int position = 4;
28-
for (ColorRgb* rgb : ledValues ){
29-
data[i++] = 0xFF;
30-
data[i++] = rgb.red;
31-
data[i++] = rgb.green;
32-
data[i++] = rgb.blue;
23+
const unsigned int mLedCount = (ledValues.size() * 4) + 8;
24+
if(_ledBuffer.size() != mLedCount){
25+
_ledBuffer.resize(mLedCount, 0x00);
3326
}
3427

35-
// Write end frame
36-
data[i++] = 0xFF;
37-
data[i++] = 0xFF;
38-
data[i++] = 0xFF;
39-
data[i++] = 0xFF;
28+
for (unsigned iLed=1; iLed<=ledValues.size(); ++iLed) {
29+
const ColorRgb& rgb = ledValues[iLed];
30+
_ledBuffer[iLed*4] = 0xFF;
31+
_ledBuffer[iLed*4+1] = rgb.red;
32+
_ledBuffer[iLed*4+2] = rgb.green;
33+
_ledBuffer[iLed*4+3] = rgb.blue;
34+
}
4035

41-
return writeBytes(dataLen, data);
36+
return writeBytes(_ledBuffer.size(), _ledBuffer.data());
4237
}
4338

4439
int LedDeviceAPA102::switchOff()
4540
{
46-
return write(std::vector<ColorRgb>(mLedCount, ColorRgb{0,0,0}));
41+
return write(std::vector<ColorRgb>(_ledBuffer.size(), ColorRgb{0,0,0}));
4742
}

libsrc/leddevice/LedDeviceAPA102.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class LedDeviceAPA102 : public LedSpiDevice
3636

3737
private:
3838

39-
/// the number of leds (needed when switching off)
40-
size_t mLedCount;
39+
/// The buffer containing the packed RGB values
40+
std::vector<uint8_t> _ledBuffer;
41+
4142
};

0 commit comments

Comments
 (0)