hyperion.ng/libsrc/leddevice/LedDeviceAPA102.cpp

61 lines
1.5 KiB
C++
Raw Normal View History

// STL includes
#include <cstring>
#include <cstdio>
#include <iostream>
#include <algorithm>
// Linux includes
#include <fcntl.h>
#include <sys/ioctl.h>
// hyperion local includes
#include "LedDeviceAPA102.h"
Apa102 fix if the led strip has more physical LEDs than you want to configure with hyperion (#626) * Removed -HUP so the default -TERM signal is sent instead. - hyperiond only listens for TERM and INT. HUP is often used to get an exe to reread its config Changed pgrep to add '-x' so it wont partial match on the exe name. - I have multiple instances with multiple hyperiond-instance1 names - this ensures the service script only kills the right process * reversing errant change to hyperion.systemd.sh * started bug fixes for APA strips with more physical leds than configured * 1st cut of changes to APA102. now looks at config for "leds" which is the # of hardware leds in the strip. This enables us to write the correct trailer length in the packet. Also fixed a bug where switchoff was writing the wrong # of leds * removed commented out old code removed debug printf * included prebuild test binary for Pi2 * Second attempt at the fix. BEFORE: If you have more physcial LEDs than defined in the "leds" "index" section of the config there is random junk added to the end of the strip. WITH THIS FIX: In the "device" section specify the number of physical leds with: "leds" : 212, If this is greater than the number of led indexes, additional LEDs are set to black If this is smaller than the number of led indexes, it is ignored * included test binary * Clean exit if the device config is missing or a non-supported device type is specified Former-commit-id: 8ae96188fa74d91b13a921e7d5faf6d6ac1c42ee
2016-05-10 12:16:19 +02:00
LedDeviceAPA102::LedDeviceAPA102(const std::string& outputDevice, const unsigned baudrate, const unsigned ledcount) :
LedSpiDevice(outputDevice, baudrate, 500000),
_ledBuffer(0)
{
Apa102 fix if the led strip has more physical LEDs than you want to configure with hyperion (#626) * Removed -HUP so the default -TERM signal is sent instead. - hyperiond only listens for TERM and INT. HUP is often used to get an exe to reread its config Changed pgrep to add '-x' so it wont partial match on the exe name. - I have multiple instances with multiple hyperiond-instance1 names - this ensures the service script only kills the right process * reversing errant change to hyperion.systemd.sh * started bug fixes for APA strips with more physical leds than configured * 1st cut of changes to APA102. now looks at config for "leds" which is the # of hardware leds in the strip. This enables us to write the correct trailer length in the packet. Also fixed a bug where switchoff was writing the wrong # of leds * removed commented out old code removed debug printf * included prebuild test binary for Pi2 * Second attempt at the fix. BEFORE: If you have more physcial LEDs than defined in the "leds" "index" section of the config there is random junk added to the end of the strip. WITH THIS FIX: In the "device" section specify the number of physical leds with: "leds" : 212, If this is greater than the number of led indexes, additional LEDs are set to black If this is smaller than the number of led indexes, it is ignored * included test binary * Clean exit if the device config is missing or a non-supported device type is specified Former-commit-id: 8ae96188fa74d91b13a921e7d5faf6d6ac1c42ee
2016-05-10 12:16:19 +02:00
_HW_ledcount = ledcount;
}
int LedDeviceAPA102::write(const std::vector<ColorRgb> &ledValues)
{
Apa102 fix if the led strip has more physical LEDs than you want to configure with hyperion (#626) * Removed -HUP so the default -TERM signal is sent instead. - hyperiond only listens for TERM and INT. HUP is often used to get an exe to reread its config Changed pgrep to add '-x' so it wont partial match on the exe name. - I have multiple instances with multiple hyperiond-instance1 names - this ensures the service script only kills the right process * reversing errant change to hyperion.systemd.sh * started bug fixes for APA strips with more physical leds than configured * 1st cut of changes to APA102. now looks at config for "leds" which is the # of hardware leds in the strip. This enables us to write the correct trailer length in the packet. Also fixed a bug where switchoff was writing the wrong # of leds * removed commented out old code removed debug printf * included prebuild test binary for Pi2 * Second attempt at the fix. BEFORE: If you have more physcial LEDs than defined in the "leds" "index" section of the config there is random junk added to the end of the strip. WITH THIS FIX: In the "device" section specify the number of physical leds with: "leds" : 212, If this is greater than the number of led indexes, additional LEDs are set to black If this is smaller than the number of led indexes, it is ignored * included test binary * Clean exit if the device config is missing or a non-supported device type is specified Former-commit-id: 8ae96188fa74d91b13a921e7d5faf6d6ac1c42ee
2016-05-10 12:16:19 +02:00
_mLedCount = ledValues.size();
const unsigned int max_leds = std::max(_mLedCount, _HW_ledcount);
const unsigned int startFrameSize = 4;
Apa102 fix if the led strip has more physical LEDs than you want to configure with hyperion (#626) * Removed -HUP so the default -TERM signal is sent instead. - hyperiond only listens for TERM and INT. HUP is often used to get an exe to reread its config Changed pgrep to add '-x' so it wont partial match on the exe name. - I have multiple instances with multiple hyperiond-instance1 names - this ensures the service script only kills the right process * reversing errant change to hyperion.systemd.sh * started bug fixes for APA strips with more physical leds than configured * 1st cut of changes to APA102. now looks at config for "leds" which is the # of hardware leds in the strip. This enables us to write the correct trailer length in the packet. Also fixed a bug where switchoff was writing the wrong # of leds * removed commented out old code removed debug printf * included prebuild test binary for Pi2 * Second attempt at the fix. BEFORE: If you have more physcial LEDs than defined in the "leds" "index" section of the config there is random junk added to the end of the strip. WITH THIS FIX: In the "device" section specify the number of physical leds with: "leds" : 212, If this is greater than the number of led indexes, additional LEDs are set to black If this is smaller than the number of led indexes, it is ignored * included test binary * Clean exit if the device config is missing or a non-supported device type is specified Former-commit-id: 8ae96188fa74d91b13a921e7d5faf6d6ac1c42ee
2016-05-10 12:16:19 +02:00
const unsigned int endFrameSize = std::max<unsigned int>(((max_leds + 15) / 16), 4);
const unsigned int APAbufferSize = (max_leds * 4) + startFrameSize + endFrameSize;
if(_ledBuffer.size() != APAbufferSize){
_ledBuffer.resize(APAbufferSize, 0xFF);
_ledBuffer[0] = 0x00;
_ledBuffer[1] = 0x00;
_ledBuffer[2] = 0x00;
_ledBuffer[3] = 0x00;
}
Apa102 fix if the led strip has more physical LEDs than you want to configure with hyperion (#626) * Removed -HUP so the default -TERM signal is sent instead. - hyperiond only listens for TERM and INT. HUP is often used to get an exe to reread its config Changed pgrep to add '-x' so it wont partial match on the exe name. - I have multiple instances with multiple hyperiond-instance1 names - this ensures the service script only kills the right process * reversing errant change to hyperion.systemd.sh * started bug fixes for APA strips with more physical leds than configured * 1st cut of changes to APA102. now looks at config for "leds" which is the # of hardware leds in the strip. This enables us to write the correct trailer length in the packet. Also fixed a bug where switchoff was writing the wrong # of leds * removed commented out old code removed debug printf * included prebuild test binary for Pi2 * Second attempt at the fix. BEFORE: If you have more physcial LEDs than defined in the "leds" "index" section of the config there is random junk added to the end of the strip. WITH THIS FIX: In the "device" section specify the number of physical leds with: "leds" : 212, If this is greater than the number of led indexes, additional LEDs are set to black If this is smaller than the number of led indexes, it is ignored * included test binary * Clean exit if the device config is missing or a non-supported device type is specified Former-commit-id: 8ae96188fa74d91b13a921e7d5faf6d6ac1c42ee
2016-05-10 12:16:19 +02:00
unsigned iLed=0;
for (iLed=0; iLed < _mLedCount; ++iLed) {
const ColorRgb& rgb = ledValues[iLed];
_ledBuffer[4+iLed*4] = 0xFF;
_ledBuffer[4+iLed*4+1] = rgb.red;
_ledBuffer[4+iLed*4+2] = rgb.green;
_ledBuffer[4+iLed*4+3] = rgb.blue;
}
Apa102 fix if the led strip has more physical LEDs than you want to configure with hyperion (#626) * Removed -HUP so the default -TERM signal is sent instead. - hyperiond only listens for TERM and INT. HUP is often used to get an exe to reread its config Changed pgrep to add '-x' so it wont partial match on the exe name. - I have multiple instances with multiple hyperiond-instance1 names - this ensures the service script only kills the right process * reversing errant change to hyperion.systemd.sh * started bug fixes for APA strips with more physical leds than configured * 1st cut of changes to APA102. now looks at config for "leds" which is the # of hardware leds in the strip. This enables us to write the correct trailer length in the packet. Also fixed a bug where switchoff was writing the wrong # of leds * removed commented out old code removed debug printf * included prebuild test binary for Pi2 * Second attempt at the fix. BEFORE: If you have more physcial LEDs than defined in the "leds" "index" section of the config there is random junk added to the end of the strip. WITH THIS FIX: In the "device" section specify the number of physical leds with: "leds" : 212, If this is greater than the number of led indexes, additional LEDs are set to black If this is smaller than the number of led indexes, it is ignored * included test binary * Clean exit if the device config is missing or a non-supported device type is specified Former-commit-id: 8ae96188fa74d91b13a921e7d5faf6d6ac1c42ee
2016-05-10 12:16:19 +02:00
for ( ; iLed < max_leds; ++iLed) {
_ledBuffer[4+iLed*4] = 0xFF;
_ledBuffer[4+iLed*4+1] = 0x00;
_ledBuffer[4+iLed*4+2] = 0x00;
_ledBuffer[4+iLed*4+3] = 0x00;
}
return writeBytes(_ledBuffer.size(), _ledBuffer.data());
}
int LedDeviceAPA102::switchOff()
{
Apa102 fix if the led strip has more physical LEDs than you want to configure with hyperion (#626) * Removed -HUP so the default -TERM signal is sent instead. - hyperiond only listens for TERM and INT. HUP is often used to get an exe to reread its config Changed pgrep to add '-x' so it wont partial match on the exe name. - I have multiple instances with multiple hyperiond-instance1 names - this ensures the service script only kills the right process * reversing errant change to hyperion.systemd.sh * started bug fixes for APA strips with more physical leds than configured * 1st cut of changes to APA102. now looks at config for "leds" which is the # of hardware leds in the strip. This enables us to write the correct trailer length in the packet. Also fixed a bug where switchoff was writing the wrong # of leds * removed commented out old code removed debug printf * included prebuild test binary for Pi2 * Second attempt at the fix. BEFORE: If you have more physcial LEDs than defined in the "leds" "index" section of the config there is random junk added to the end of the strip. WITH THIS FIX: In the "device" section specify the number of physical leds with: "leds" : 212, If this is greater than the number of led indexes, additional LEDs are set to black If this is smaller than the number of led indexes, it is ignored * included test binary * Clean exit if the device config is missing or a non-supported device type is specified Former-commit-id: 8ae96188fa74d91b13a921e7d5faf6d6ac1c42ee
2016-05-10 12:16:19 +02:00
return write(std::vector<ColorRgb>(_mLedCount, ColorRgb{0,0,0}));
}