Skip to content

Commit d6a34ed

Browse files
authored
Leddevice refactoring the next part (#253)
* add general switchOff * refactoring of leddevices regarding ledcount and switchoff * remove obsolete includes
1 parent 5aeec2e commit d6a34ed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+276
-1075
lines changed

include/leddevice/LedDevice.h

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
#pragma once
22

3+
#include <QObject>
4+
#include <QString>
5+
36
// STL incldues
47
#include <vector>
5-
#include <QObject>
8+
#include <string>
69
#include <map>
10+
#include <algorithm>
711

812
// Utility includes
913
#include <utils/ColorRgb.h>
@@ -32,17 +36,10 @@ class LedDevice : public QObject
3236
///
3337
virtual ~LedDevice() {}
3438

35-
///
36-
/// Writes the RGB-Color values to the leds.
37-
///
38-
/// @param[in] ledValues The RGB-color per led
39-
///
40-
/// @return Zero on success else negative
41-
///
42-
virtual int write(const std::vector<ColorRgb>& ledValues) = 0;
43-
4439
/// Switch the leds off
45-
virtual int switchOff() = 0;
40+
virtual int switchOff();
41+
42+
virtual int setLedValues(const std::vector<ColorRgb>& ledValues);
4643

4744
///
4845
/// Opens and configures the output device
@@ -58,6 +55,15 @@ class LedDevice : public QObject
5855
static Json::Value getLedDeviceSchemas();
5956

6057
protected:
58+
///
59+
/// Writes the RGB-Color values to the leds.
60+
///
61+
/// @param[in] ledValues The RGB-color per led
62+
///
63+
/// @return Zero on success else negative
64+
///
65+
virtual int write(const std::vector<ColorRgb>& ledValues) = 0;
66+
6167
/// The common Logger instance for all LedDevices
6268
Logger * _log;
6369

libsrc/hyperion/Hyperion.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,9 +1012,9 @@ void Hyperion::update()
10121012

10131013
// Write the data to the device
10141014
if (_deviceSmooth->enabled())
1015-
_deviceSmooth->write(_ledBuffer);
1015+
_deviceSmooth->setLedValues(_ledBuffer);
10161016
else
1017-
_device->write(_ledBuffer);
1017+
_device->setLedValues(_ledBuffer);
10181018

10191019
// Start the timeout-timer
10201020
if (priorityInfo.timeoutTime_ms == -1)

libsrc/hyperion/LinearColorSmoothing.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ void LinearColorSmoothing::queueColors(const std::vector<ColorRgb> & ledColors)
112112
{
113113
// No output delay => immediate write
114114
if ( _writeToLedsEnable )
115-
_ledDevice->write(ledColors);
115+
_ledDevice->setLedValues(ledColors);
116116
}
117117
else
118118
{
@@ -125,7 +125,7 @@ void LinearColorSmoothing::queueColors(const std::vector<ColorRgb> & ledColors)
125125
{
126126
if ( _outputQueue.size() > _outputDelay || !_writeToLedsEnable )
127127
{
128-
_ledDevice->write(_outputQueue.front());
128+
_ledDevice->setLedValues(_outputQueue.front());
129129
_outputQueue.pop_front();
130130
}
131131
}

libsrc/leddevice/LedDevice.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,18 @@ Json::Value LedDevice::getLedDeviceSchemas()
6767

6868
return result;
6969
}
70+
71+
72+
int LedDevice::setLedValues(const std::vector<ColorRgb>& ledValues)
73+
{
74+
_ledCount = ledValues.size();
75+
return write(ledValues);
76+
}
77+
78+
int LedDevice::switchOff()
79+
{
80+
return write(std::vector<ColorRgb>(_ledCount, ColorRgb::BLACK ));
81+
}
82+
83+
84+

libsrc/leddevice/LedDeviceAPA102.cpp

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,9 @@
1-
2-
// STL includes
3-
#include <cstring>
4-
#include <cstdio>
5-
#include <iostream>
6-
#include <algorithm>
7-
8-
// Linux includes
9-
#include <fcntl.h>
10-
#include <sys/ioctl.h>
11-
12-
// hyperion local includes
131
#include "LedDeviceAPA102.h"
142

153
LedDeviceAPA102::LedDeviceAPA102(const Json::Value &deviceConfig)
164
: ProviderSpi(deviceConfig)
175
{
18-
_latchTime_ns = 500000;
6+
_latchTime_ns = 500000; // fixed latchtime
197
}
208

219
LedDevice* LedDeviceAPA102::construct(const Json::Value &deviceConfig)
@@ -25,7 +13,6 @@ LedDevice* LedDeviceAPA102::construct(const Json::Value &deviceConfig)
2513

2614
int LedDeviceAPA102::write(const std::vector<ColorRgb> &ledValues)
2715
{
28-
_ledCount = ledValues.size();
2916
const unsigned int startFrameSize = 4;
3017
const unsigned int endFrameSize = std::max<unsigned int>(((_ledCount + 15) / 16), 4);
3118
const unsigned int APAbufferSize = (_ledCount * 4) + startFrameSize + endFrameSize;
@@ -48,8 +35,3 @@ int LedDeviceAPA102::write(const std::vector<ColorRgb> &ledValues)
4835

4936
return writeBytes(_ledBuffer.size(), _ledBuffer.data());
5037
}
51-
52-
int LedDeviceAPA102::switchOff()
53-
{
54-
return write(std::vector<ColorRgb>(_ledCount, ColorRgb{0,0,0}));
55-
}

libsrc/leddevice/LedDeviceAPA102.h

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
#pragma once
22

3-
// STL includes
4-
#include <string>
5-
63
// hyperion incluse
74
#include "ProviderSpi.h"
8-
#include <json/json.h>
5+
96

107
///
118
/// Implementation of the LedDevice interface for writing to APA102 led device.
@@ -23,15 +20,12 @@ class LedDeviceAPA102 : public ProviderSpi
2320
/// constructs leddevice
2421
static LedDevice* construct(const Json::Value &deviceConfig);
2522

26-
23+
private:
2724
///
2825
/// Writes the led color values to the led-device
2926
///
3027
/// @param ledValues The color-value per led
3128
/// @return Zero on succes else negative
3229
///
3330
virtual int write(const std::vector<ColorRgb> &ledValues);
34-
35-
/// Switch the leds off
36-
virtual int switchOff();
3731
};

libsrc/leddevice/LedDeviceAdalight.cpp

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,4 @@
1-
2-
// STL includes
3-
#include <cstring>
4-
#include <cstdio>
5-
#include <iostream>
6-
7-
// Linux includes
8-
#include <fcntl.h>
9-
#include <sys/ioctl.h>
10-
11-
// hyperion local includes
121
#include "LedDeviceAdalight.h"
13-
#include <leddevice/LedDevice.h>
142

153
LedDeviceAdalight::LedDeviceAdalight(const Json::Value &deviceConfig)
164
: ProviderRs232(deviceConfig)
@@ -60,16 +48,6 @@ int LedDeviceAdalight::write(const std::vector<ColorRgb> & ledValues)
6048
return writeBytes(_ledBuffer.size(), _ledBuffer.data());
6149
}
6250

63-
int LedDeviceAdalight::switchOff()
64-
{
65-
// restart the timer
66-
_timer.start();
67-
68-
// write data
69-
memset(6 + _ledBuffer.data(), 0, _ledBuffer.size()-6);
70-
return writeBytes(_ledBuffer.size(), _ledBuffer.data());
71-
}
72-
7351
void LedDeviceAdalight::rewriteLeds()
7452
{
7553
writeBytes(_ledBuffer.size(), _ledBuffer.data());

libsrc/leddevice/LedDeviceAdalight.h

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
#pragma once
22

3-
// STL includes
4-
#include <string>
5-
63
// Qt includes
74
#include <QTimer>
85

@@ -27,6 +24,11 @@ class LedDeviceAdalight : public ProviderRs232
2724
/// constructs leddevice
2825
static LedDevice* construct(const Json::Value &deviceConfig);
2926

27+
private slots:
28+
/// Write the last data to the leds again
29+
void rewriteLeds();
30+
31+
protected:
3032
///
3133
/// Writes the led color values to the led-device
3234
///
@@ -35,14 +37,6 @@ class LedDeviceAdalight : public ProviderRs232
3537
///
3638
virtual int write(const std::vector<ColorRgb> & ledValues);
3739

38-
/// Switch the leds off
39-
virtual int switchOff();
40-
41-
private slots:
42-
/// Write the last data to the leds again
43-
void rewriteLeds();
44-
45-
protected:
4640
/// Timer object which makes sure that led data is written at a minimum rate
4741
/// The Adalight device will switch off when it does not receive data at least
4842
/// every 15 seconds
Lines changed: 8 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,3 @@
1-
2-
// STL includes
3-
#include <cstring>
4-
#include <cstdio>
5-
#include <iostream>
6-
7-
// Linux includes
8-
#include <fcntl.h>
9-
#include <sys/ioctl.h>
10-
11-
// hyperion local includes
121
#include "LedDeviceAdalightApa102.h"
132

143
LedDeviceAdalightApa102::LedDeviceAdalightApa102(const Json::Value &deviceConfig)
@@ -27,30 +16,24 @@ LedDevice* LedDeviceAdalightApa102::construct(const Json::Value &deviceConfig)
2716
// 2 - in order to accomodate point 1 above, number of leds sent to adalight is increased by 1/3rd
2817
int LedDeviceAdalightApa102::write(const std::vector<ColorRgb> & ledValues)
2918
{
30-
_ledCount = ledValues.size();
3119
const unsigned int startFrameSize = 4;
3220
const unsigned int endFrameSize = std::max<unsigned int>(((_ledCount + 15) / 16), 4);
3321
const unsigned int mLedCount = (_ledCount * 4) + startFrameSize + endFrameSize;
34-
if(_ledBuffer.size() != mLedCount+6){
22+
if(_ledBuffer.size() != mLedCount+6)
23+
{
3524
_ledBuffer.resize(mLedCount+6, 0x00);
3625
_ledBuffer[0] = 'A';
3726
_ledBuffer[1] = 'd';
3827
_ledBuffer[2] = 'a';
39-
_ledBuffer[3] = (((unsigned int)(ledValues.size())) >> 8) & 0xFF; // LED count high byte
40-
_ledBuffer[4] = ((unsigned int)(ledValues.size())) & 0xFF; // LED count low byte
28+
_ledBuffer[3] = (((unsigned int)(_ledCount)) >> 8) & 0xFF; // LED count high byte
29+
_ledBuffer[4] = ((unsigned int)(_ledCount)) & 0xFF; // LED count low byte
4130
_ledBuffer[5] = _ledBuffer[3] ^ _ledBuffer[4] ^ 0x55; // Checksum
42-
Debug( _log, "Adalight header for %d leds: %c%c%c 0x%02x 0x%02x 0x%02x",
43-
ledValues.size(),
44-
_ledBuffer[0],
45-
_ledBuffer[1],
46-
_ledBuffer[2],
47-
_ledBuffer[3],
48-
_ledBuffer[4],
49-
_ledBuffer[5]
50-
);
31+
Debug( _log, "Adalight header for %d leds: %c%c%c 0x%02x 0x%02x 0x%02x", _ledCount,
32+
_ledBuffer[0], _ledBuffer[1], _ledBuffer[2], _ledBuffer[3], _ledBuffer[4], _ledBuffer[5] );
5133
}
5234

53-
for (signed iLed=1; iLed<=_ledCount; iLed++) {
35+
for (signed iLed=1; iLed<=_ledCount; iLed++)
36+
{
5437
const ColorRgb& rgb = ledValues[iLed-1];
5538
_ledBuffer[iLed*4+6] = 0xFF;
5639
_ledBuffer[iLed*4+1+6] = rgb.red;
@@ -65,19 +48,3 @@ int LedDeviceAdalightApa102::write(const std::vector<ColorRgb> & ledValues)
6548
return writeBytes(_ledBuffer.size(), _ledBuffer.data());
6649
}
6750

68-
int LedDeviceAdalightApa102::switchOff()
69-
{
70-
for (signed iLed=1; iLed<=_ledCount; iLed++) {
71-
_ledBuffer[iLed*4+6] = 0xFF;
72-
_ledBuffer[iLed*4+1+6] = 0x00;
73-
_ledBuffer[iLed*4+2+6] = 0x00;
74-
_ledBuffer[iLed*4+3+6] = 0x00;
75-
}
76-
77-
// restart the timer
78-
_timer.start();
79-
80-
// write data
81-
return writeBytes(_ledBuffer.size(), _ledBuffer.data());
82-
83-
}

libsrc/leddevice/LedDeviceAdalightApa102.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
#pragma once
22

3-
// STL includes
4-
#include <string>
5-
63
// hyperion include
74
#include "LedDeviceAdalight.h"
85

@@ -25,15 +22,13 @@ class LedDeviceAdalightApa102 : public LedDeviceAdalight
2522
/// create leddevice when type in config is set to this type
2623
static LedDevice* construct(const Json::Value &deviceConfig);
2724

25+
protected:
2826
///
2927
/// Writes the led color values to the led-device
3028
///
3129
/// @param ledValues The color-value per led
3230
/// @return Zero on succes else negative
3331
///
3432
virtual int write(const std::vector<ColorRgb> & ledValues);
35-
36-
/// Switch the leds off
37-
virtual int switchOff();
3833
};
3934

libsrc/leddevice/LedDeviceAtmo.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,14 @@ int LedDeviceAtmo::write(const std::vector<ColorRgb> &ledValues)
2121
{
2222
// The protocol is shomehow limited. we always need to send exactly 5 channels + header
2323
// (19 bytes) for the hardware to recognize the data
24-
if (ledValues.size() != 5)
24+
if (_ledCount != 5)
2525
{
26-
Error( _log, "%d channels configured. This should always be 5!", ledValues.size());
26+
Error( _log, "%d channels configured. This should always be 5!", _ledCount);
2727
return 0;
2828
}
2929

3030
// write data
31-
memcpy(4 + _ledBuffer.data(), ledValues.data(), ledValues.size() * sizeof(ColorRgb));
31+
memcpy(4 + _ledBuffer.data(), ledValues.data(), _ledCount * sizeof(ColorRgb));
3232
return writeBytes(_ledBuffer.size(), _ledBuffer.data());
3333
}
3434

35-
int LedDeviceAtmo::switchOff()
36-
{
37-
memset(4 + _ledBuffer.data(), 0, _ledBuffer.size() - 4);
38-
return writeBytes(_ledBuffer.size(), _ledBuffer.data());
39-
}

libsrc/leddevice/LedDeviceAtmo.h

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
#pragma once
22

3-
// STL includes
4-
#include <string>
5-
63
// hyperion incluse
74
#include "ProviderRs232.h"
85

@@ -22,14 +19,12 @@ class LedDeviceAtmo : public ProviderRs232
2219
/// constructs leddevice
2320
static LedDevice* construct(const Json::Value &deviceConfig);
2421

22+
private:
2523
///
2624
/// Writes the led color values to the led-device
2725
///
2826
/// @param ledValues The color-value per led
2927
/// @return Zero on succes else negative
3028
///
3129
virtual int write(const std::vector<ColorRgb> &ledValues);
32-
33-
/// Switch the leds off
34-
virtual int switchOff();
3530
};

0 commit comments

Comments
 (0)