Skip to content

Commit 954112a

Browse files
authored
APA102 protocol fix (#1352) (#1361)
* Fix APA102 protocoll * Minor clean-ups * Revert updates
1 parent ba32b87 commit 954112a

File tree

7 files changed

+65
-28
lines changed

7 files changed

+65
-28
lines changed

bin/create_all_releases.sh

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ make_release()
1616
mkdir -p deploy/${RELEASE}
1717

1818
cd build-${RELEASE}
19+
1920
cmake -DCMAKE_INSTALL_PREFIX=/usr -DPLATFORM=${PLATFORM} $@ -DCMAKE_BUILD_TYPE=Release -Wno-dev .. || exit 1
21+
#cmake -DCMAKE_INSTALL_PREFIX=/usr -DPLATFORM=${PLATFORM} $@ -DCMAKE_BUILD_TYPE=Debug .. || exit 1
2022
make -j $(nproc) || exit 1
21-
#strip bin/*
23+
strip bin/*
2224
make package -j $(nproc)
2325
mv Hyperion-* ../deploy/${RELEASE}
2426
cd ..
@@ -30,7 +32,7 @@ CMAKE_FLATC_FLAG="-DIMPORT_FLATC=../build-x86x64/flatc_export.cmake"
3032

3133
make_release x86x64 x11
3234
#make_release x32 x11 -DCMAKE_TOOLCHAIN_FILE="../cmake/Toolchain-x32.cmake" ${CMAKE_PROTOC_FLAG} ${CMAKE_FLATC_FLAG}
33-
make_release rpi rpi -DCMAKE_TOOLCHAIN_FILE="../cmake/Toolchain-rpi.cmake" ${CMAKE_PROTOC_FLAG} ${CMAKE_FLATC_FLAG}
35+
#make_release rpi rpi -DCMAKE_TOOLCHAIN_FILE="../cmake/Toolchain-rpi.cmake" ${CMAKE_PROTOC_FLAG} ${CMAKE_FLATC_FLAG}
3436
#make_release wetek wetek -DCMAKE_TOOLCHAIN_FILE="../cmake/Toolchain-rpi.cmake" ${CMAKE_PROTOC_FLAG} ${CMAKE_FLATC_FLAG}
3537
#make_release imx6 imx6 -DCMAKE_TOOLCHAIN_FILE="../cmake/Toolchain-imx6.cmake" ${CMAKE_PROTOC_FLAG} ${CMAKE_FLATC_FLAG}
3638

libsrc/leddevice/LedDevice.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@ bool LedDevice::switchOn()
275275
if ( powerOn() )
276276
{
277277
_isOn = true;
278+
_isInSwitchOff = false;
278279
rc = true;
279280
}
280281
}
Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
11
#include "LedDeviceAPA102.h"
22

3+
// Constants
4+
namespace {
5+
6+
/// The value that determines the higher bits of the APA102 brightness control field
7+
const int APA102_LEDFRAME_UPPER_BITS = 0xE0;
8+
9+
} //End of constants
10+
11+
312
LedDeviceAPA102::LedDeviceAPA102(const QJsonObject &deviceConfig)
413
: ProviderSpi(deviceConfig)
514
{
15+
// Overwrite non supported/required features
16+
_latchTime_ms = 0;
617
}
718

819
LedDevice* LedDeviceAPA102::construct(const QJsonObject &deviceConfig)
@@ -17,32 +28,44 @@ bool LedDeviceAPA102::init(const QJsonObject &deviceConfig)
1728
// Initialise sub-class
1829
if ( ProviderSpi::init(deviceConfig) )
1930
{
31+
_brightnessControlMaxLevel = deviceConfig["brightnessControlMaxLevel"].toInt(APA102_BRIGHTNESS_MAX_LEVEL);
32+
Info(_log, "[%s] Setting maximum brightness to [%d] = %d%%", QSTRING_CSTR(_activeDeviceType), _brightnessControlMaxLevel, _brightnessControlMaxLevel * 100 / APA102_BRIGHTNESS_MAX_LEVEL);
2033

2134
const unsigned int startFrameSize = 4;
22-
const unsigned int endFrameSize = qMax<unsigned int>(((_ledCount + 15) / 16), 4);
35+
//Endframe, add additional 4 bytes to cover SK9922 Reset frame (in case SK9922 were sold as AP102) - has no effect on APA102
36+
const unsigned int endFrameSize = (_ledCount/32) * 4 + 4;
2337
const unsigned int APAbufferSize = (_ledCount * 4) + startFrameSize + endFrameSize;
2438

25-
_ledBuffer.resize(APAbufferSize, 0xFF);
26-
_ledBuffer[0] = 0x00;
27-
_ledBuffer[1] = 0x00;
28-
_ledBuffer[2] = 0x00;
29-
_ledBuffer[3] = 0x00;
39+
_ledBuffer.resize(APAbufferSize, 0x00);
3040

3141
isInitOK = true;
32-
3342
}
3443
return isInitOK;
3544
}
3645

46+
void LedDeviceAPA102::bufferWithBrightness(std::vector<uint8_t> &txBuf, const std::vector<ColorRgb> & ledValues, const int brightness) {
47+
const int ledCount = static_cast<int>(_ledCount);
48+
49+
for (int iLed = 0; iLed < ledCount; ++iLed)
50+
{
51+
const ColorRgb &rgb = ledValues[iLed];
52+
const uint8_t red = rgb.red;
53+
const uint8_t green = rgb.green;
54+
const uint8_t blue = rgb.blue;
55+
56+
/// The LED index in the buffer
57+
const int b = 4 + iLed * 4;
58+
59+
txBuf[b + 0] = brightness | APA102_LEDFRAME_UPPER_BITS;
60+
txBuf[b + 1] = blue;
61+
txBuf[b + 2] = green;
62+
txBuf[b + 3] = red;
63+
}
64+
}
65+
3766
int LedDeviceAPA102::write(const std::vector<ColorRgb> &ledValues)
3867
{
39-
for (signed iLed=0; iLed < static_cast<int>( _ledCount); ++iLed) {
40-
const ColorRgb& rgb = ledValues[iLed];
41-
_ledBuffer[4+iLed*4] = 0xFF;
42-
_ledBuffer[4+iLed*4+1] = rgb.red;
43-
_ledBuffer[4+iLed*4+2] = rgb.green;
44-
_ledBuffer[4+iLed*4+3] = rgb.blue;
45-
}
68+
this->bufferWithBrightness(_ledBuffer, ledValues, _brightnessControlMaxLevel);
4669

4770
return writeBytes(_ledBuffer.size(), _ledBuffer.data());
4871
}

libsrc/leddevice/dev_spi/LedDeviceAPA102.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
// hyperion includes
55
#include "ProviderSpi.h"
66

7+
/// The maximal level supported by the APA brightness control field, 31
8+
const int APA102_BRIGHTNESS_MAX_LEVEL = 31;
9+
710
///
811
/// Implementation of the LedDevice interface for writing to APA102 led device.
912
///
@@ -43,6 +46,18 @@ class LedDeviceAPA102 : public ProviderSpi
4346
/// @return Zero on success, else negative
4447
///
4548
int write(const std::vector<ColorRgb> & ledValues) override;
49+
50+
///
51+
/// @brief Writes the RGB-Color values to the SPI Tx buffer setting considering a given brightness level
52+
///
53+
/// @param[in,out] txBuf The packed spi transfer buffer of the LED's color values
54+
/// @param[in] ledValues The RGB-color per LED
55+
/// @param[in] brightness The current brightness level 1 .. 31
56+
///
57+
void bufferWithBrightness(std::vector<uint8_t> &txBuf, const std::vector<ColorRgb> & ledValues, const int brightness = APA102_BRIGHTNESS_MAX_LEVEL);
58+
59+
/// The brighness level. Possibile values 1 .. 31.
60+
int _brightnessControlMaxLevel;
4661
};
4762

4863
#endif // LEDEVICEAPA102_H

libsrc/leddevice/dev_spi/LedDeviceSK9822.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,6 @@ bool LedDeviceSK9822::init(const QJsonObject &deviceConfig)
3939

4040
_ledBuffer.resize(0, 0x00);
4141
_ledBuffer.resize(bufferSize, 0x00);
42-
//_ledBuffer[0] = 0x00;
43-
//_ledBuffer[1] = 0x00;
44-
//_ledBuffer[2] = 0x00;
45-
//_ledBuffer[3] = 0x00;
4642

4743
isInitOK = true;
4844
}

libsrc/leddevice/dev_spi/ProviderSpi.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ ProviderSpi::ProviderSpi(const QJsonObject &deviceConfig)
3737
{
3838
memset(&_spi, 0, sizeof(_spi));
3939
_latchTime_ms = 1;
40+
_isInSwitchOff = false;
4041
}
4142

4243
ProviderSpi::~ProviderSpi()
@@ -68,6 +69,7 @@ int ProviderSpi::open()
6869
int retval = -1;
6970
QString errortext;
7071
_isDeviceReady = false;
72+
_isInSwitchOff = false;
7173

7274
const int bitsPerWord = 8;
7375

libsrc/leddevice/schemas/schema-apa102.json

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,18 @@
1919
"default": false,
2020
"propertyOrder" : 3
2121
},
22-
"latchTime": {
22+
"brightnessControlMaxLevel": {
2323
"type": "integer",
24-
"title":"edt_dev_spec_latchtime_title",
25-
"default": 0,
26-
"append" : "edt_append_ms",
27-
"minimum": 0,
28-
"maximum": 1000,
29-
"access" : "expert",
24+
"title":"edt_conf_color_brightness_title",
25+
"default": 31,
26+
"minimum": 1,
27+
"maximum": 31,
3028
"propertyOrder" : 4
3129
},
3230
"rewriteTime": {
3331
"type": "integer",
3432
"title":"edt_dev_general_rewriteTime_title",
35-
"default": 1000,
33+
"default": 0,
3634
"append" : "edt_append_ms",
3735
"minimum": 0,
3836
"access" : "expert",

0 commit comments

Comments
 (0)