|
13 | 13 |
|
14 | 14 | LedDeviceAPA102::LedDeviceAPA102(const std::string& outputDevice, const unsigned baudrate) :
|
15 | 15 | LedSpiDevice(outputDevice, baudrate, 500000),
|
16 |
| - mLedCount(0) |
| 16 | + _ledBuffer(0) |
17 | 17 | {
|
18 | 18 | // empty
|
19 | 19 | }
|
20 | 20 |
|
21 | 21 | int LedDeviceAPA102::write(const std::vector<ColorRgb> &ledValues)
|
22 | 22 | {
|
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); |
33 | 26 | }
|
34 | 27 |
|
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 | + } |
40 | 35 |
|
41 |
| - return writeBytes(dataLen, data); |
| 36 | + return writeBytes(_ledBuffer.size(), _ledBuffer.data()); |
42 | 37 | }
|
43 | 38 |
|
44 | 39 | int LedDeviceAPA102::switchOff()
|
45 | 40 | {
|
46 |
| - return write(std::vector<ColorRgb>(mLedCount, ColorRgb{0,0,0})); |
| 41 | + return write(std::vector<ColorRgb>(_ledBuffer.size(), ColorRgb{0,0,0})); |
47 | 42 | }
|
0 commit comments