mirror of
				https://github.com/hyperion-project/hyperion.ng.git
				synced 2025-03-01 10:33:28 +00:00 
			
		
		
		
	Merge pull request #484 from wisc17/apa102
Update LedDeviceAPA102.cpp by pcaffardi Former-commit-id: 0e586eda7636b4abb378fe8adf1b35145e63eb1f
This commit is contained in:
		@@ -1,9 +1,7 @@
 | 
				
			|||||||
 | 
					 | 
				
			||||||
// STL includes
 | 
					// STL includes
 | 
				
			||||||
#include <cstring>
 | 
					#include <cstring>
 | 
				
			||||||
#include <cstdio>
 | 
					#include <cstdio>
 | 
				
			||||||
#include <iostream>
 | 
					#include <iostream>
 | 
				
			||||||
#include <algorithm>
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
// Linux includes
 | 
					// Linux includes
 | 
				
			||||||
#include <fcntl.h>
 | 
					#include <fcntl.h>
 | 
				
			||||||
@@ -19,27 +17,46 @@ LedDeviceAPA102::LedDeviceAPA102(const std::string& outputDevice, const unsigned
 | 
				
			|||||||
	// empty
 | 
						// empty
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#define MIN(a,b)	((a)<(b)?(a):(b))
 | 
				
			||||||
 | 
					#define MAX(a,b)	((a)>(b)?(a):(b))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					#define APA102_START_FRAME_BYTES	4
 | 
				
			||||||
 | 
					#define APA102_LED_BYTES		4
 | 
				
			||||||
 | 
					#define APA102_END_FRAME_BITS_MIN	32
 | 
				
			||||||
 | 
					#define APA102_END_FRAME_BITS(leds)	MAX((((leds-1)/2)+1),APA102_END_FRAME_BITS_MIN)
 | 
				
			||||||
 | 
					#define APA102_END_FRAME_BYTES(leds)	(((APA102_END_FRAME_BITS(leds)-1)/8)+1)
 | 
				
			||||||
 | 
					#define APA102_LED_HEADER		0xe0
 | 
				
			||||||
 | 
					#define APA102_LED_MAX_INTENSITY	0x1f
 | 
				
			||||||
 | 
					
 | 
				
			||||||
int LedDeviceAPA102::write(const std::vector<ColorRgb> &ledValues)
 | 
					int LedDeviceAPA102::write(const std::vector<ColorRgb> &ledValues)
 | 
				
			||||||
{
 | 
					{
 | 
				
			||||||
	const unsigned int startFrameSize = 4;
 | 
						const unsigned int startFrameSize = APA102_START_FRAME_BYTES;
 | 
				
			||||||
	const unsigned int endFrameSize = std::max<unsigned int>(((ledValues.size() + 15) / 16), 4);
 | 
						const unsigned int ledsCount = ledValues.size() ;
 | 
				
			||||||
	const unsigned int mLedCount = (ledValues.size() * 4) + startFrameSize + endFrameSize;
 | 
						const unsigned int ledsSize = ledsCount * APA102_LED_BYTES ;
 | 
				
			||||||
	if(_ledBuffer.size() != mLedCount){
 | 
						const unsigned int endFrameBits = APA102_END_FRAME_BITS(ledsCount) ;
 | 
				
			||||||
		_ledBuffer.resize(mLedCount, 0xFF);
 | 
						const unsigned int endFrameSize = APA102_END_FRAME_BYTES(ledsCount) ;
 | 
				
			||||||
		_ledBuffer[0] = 0x00; 
 | 
						const unsigned int transferSize = startFrameSize + ledsSize + endFrameSize ;
 | 
				
			||||||
		_ledBuffer[1] = 0x00; 
 | 
					
 | 
				
			||||||
		_ledBuffer[2] = 0x00; 
 | 
						if(_ledBuffer.size() != transferSize){
 | 
				
			||||||
		_ledBuffer[3] = 0x00; 
 | 
							_ledBuffer.resize(transferSize, 0x00);
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	for (unsigned iLed=1; iLed<=ledValues.size(); ++iLed) {
 | 
						unsigned idx = 0, i;
 | 
				
			||||||
		const ColorRgb& rgb = ledValues[iLed-1];
 | 
						for (i=0; i<APA102_START_FRAME_BYTES; i++) {
 | 
				
			||||||
		_ledBuffer[iLed*4]   = 0xFF;
 | 
							_ledBuffer[idx++] = 0x00 ;
 | 
				
			||||||
		_ledBuffer[iLed*4+1] = rgb.red;
 | 
					 | 
				
			||||||
		_ledBuffer[iLed*4+2] = rgb.green;
 | 
					 | 
				
			||||||
		_ledBuffer[iLed*4+3] = rgb.blue;
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						for (i=0; i<ledsCount; i++) {
 | 
				
			||||||
 | 
							const ColorRgb& rgb = ledValues[i];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
							_ledBuffer[idx++]   = APA102_LED_HEADER + APA102_LED_MAX_INTENSITY;
 | 
				
			||||||
 | 
							_ledBuffer[idx++] = rgb.red;
 | 
				
			||||||
 | 
							_ledBuffer[idx++] = rgb.green;
 | 
				
			||||||
 | 
							_ledBuffer[idx++] = rgb.blue;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						for(i=0; i<endFrameSize; i++)
 | 
				
			||||||
 | 
							_ledBuffer[idx++] = 0x00 ;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return writeBytes(_ledBuffer.size(), _ledBuffer.data());
 | 
						return writeBytes(_ledBuffer.size(), _ledBuffer.data());
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user