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

Commit 77892a5

Browse files
committed
Slight style changes to the pull of the P9813 device
1 parent 258161c commit 77892a5

File tree

4 files changed

+40
-30
lines changed

4 files changed

+40
-30
lines changed

libsrc/leddevice/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,16 @@ if(ENABLE_SPIDEV)
5454
${CURRENT_SOURCE_DIR}/LedSpiDevice.h
5555
${CURRENT_SOURCE_DIR}/LedDeviceLpd6803.h
5656
${CURRENT_SOURCE_DIR}/LedDeviceLpd8806.h
57-
${CURRENT_SOURCE_DIR}/LedDeviceWs2801.h
5857
${CURRENT_SOURCE_DIR}/LedDeviceP9813.h
58+
${CURRENT_SOURCE_DIR}/LedDeviceWs2801.h
5959
)
6060
SET(Leddevice_SOURCES
6161
${Leddevice_SOURCES}
6262
${CURRENT_SOURCE_DIR}/LedSpiDevice.cpp
6363
${CURRENT_SOURCE_DIR}/LedDeviceLpd6803.cpp
6464
${CURRENT_SOURCE_DIR}/LedDeviceLpd8806.cpp
65-
${CURRENT_SOURCE_DIR}/LedDeviceWs2801.cpp
6665
${CURRENT_SOURCE_DIR}/LedDeviceP9813.cpp
66+
${CURRENT_SOURCE_DIR}/LedDeviceWs2801.cpp
6767
)
6868
endif(ENABLE_SPIDEV)
6969

libsrc/leddevice/LedDeviceFactory.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
#ifdef ENABLE_SPIDEV
1010
#include "LedDeviceLpd6803.h"
1111
#include "LedDeviceLpd8806.h"
12-
#include "LedDeviceWs2801.h"
1312
#include "LedDeviceP9813.h"
13+
#include "LedDeviceWs2801.h"
1414
#endif
1515

1616
#include "LedDeviceAdalight.h"

libsrc/leddevice/LedDeviceP9813.cpp

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -13,44 +13,44 @@
1313

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

2121
int LedDeviceP9813::write(const std::vector<ColorRgb> &ledValues)
2222
{
23-
mLedCount = ledValues.size();
24-
25-
const unsigned dataLen = ledValues.size() * 4 + 8;
26-
uint8_t data[dataLen];
27-
28-
memset(data, 0x00, dataLen);
29-
30-
int j = 4;
31-
for (unsigned i = 0; i < mLedCount; i++){
32-
data[j++] = calculateChecksum(ledValues[i]);
33-
data[j++] = ledValues[i].blue;
34-
data[j++] = ledValues[i].green;
35-
data[j++] = ledValues[i].red;
36-
}
37-
38-
return writeBytes(dataLen, data);
23+
if (_ledCount != ledValues.size())
24+
{
25+
_ledBuf.resize(ledValues.size() * 4 + 8, 0x00);
26+
_ledCount = ledValues.size();
27+
}
28+
29+
uint8_t * dataPtr = _ledBuf.data();
30+
for (const ColorRgb & color : ledValues)
31+
{
32+
*dataPtr++ = calculateChecksum(color);
33+
*dataPtr++ = color.blue;
34+
*dataPtr++ = color.green;
35+
*dataPtr++ = color.red;
36+
}
37+
38+
return writeBytes(_ledBuf.size(), _ledBuf.data());
3939
}
4040

4141
int LedDeviceP9813::switchOff()
4242
{
43-
return write(std::vector<ColorRgb>(mLedCount, ColorRgb{0,0,0}));
43+
return write(std::vector<ColorRgb>(_ledCount, ColorRgb{0,0,0}));
4444
}
4545

46-
const uint8_t LedDeviceP9813::calculateChecksum(const ColorRgb color)
46+
uint8_t LedDeviceP9813::calculateChecksum(const ColorRgb & color) const
4747
{
48-
uint8_t res = 0;
48+
uint8_t res = 0;
4949

50-
res |= (uint8_t)0x03 << 6;
51-
res |= (uint8_t)(~(color.blue >> 6) & 0x03) << 4;
52-
res |= (uint8_t)(~(color.green >> 6) & 0x03) << 2;
53-
res |= (uint8_t)(~(color.red >> 6) & 0x03);
50+
res |= (uint8_t)0x03 << 6;
51+
res |= (uint8_t)(~(color.blue >> 6) & 0x03) << 4;
52+
res |= (uint8_t)(~(color.green >> 6) & 0x03) << 2;
53+
res |= (uint8_t)(~(color.red >> 6) & 0x03);
5454

55-
return res;
55+
return res;
5656
}

libsrc/leddevice/LedDeviceP9813.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,16 @@ class LedDeviceP9813 : public LedSpiDevice
3535
private:
3636

3737
/// the number of leds
38-
size_t mLedCount;
39-
const uint8_t calculateChecksum(const ColorRgb color);
38+
size_t _ledCount;
39+
40+
/// Buffer for writing/written led data
41+
std::vector<uint8_t> _ledBuf;
42+
43+
///
44+
/// Calculates the required checksum for one led
45+
///
46+
/// @param color The color of the led
47+
/// @return The checksum for the led
48+
///
49+
uint8_t calculateChecksum(const ColorRgb & color) const;
4050
};

0 commit comments

Comments
 (0)