|
| 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 |
| 12 | +#include "LedDeviceAdalightApa102.h" |
| 13 | + |
| 14 | +LedDeviceAdalightApa102::LedDeviceAdalightApa102(const std::string& outputDevice, const unsigned baudrate, int delayAfterConnect_ms) : |
| 15 | + LedDeviceAdalight(outputDevice, baudrate, delayAfterConnect_ms), |
| 16 | + _ledBuffer(0), |
| 17 | + _timer() |
| 18 | +{ |
| 19 | +} |
| 20 | +//comparing to ws2801 adalight, the following changes were needed: |
| 21 | +// 1- differnt data frame (4 bytes instead of 3) |
| 22 | +// 2 - in order to accomodate point 1 above, number of leds sent to adalight is increased by 1/3rd |
| 23 | +int LedDeviceAdalightApa102::write(const std::vector<ColorRgb> & ledValues) |
| 24 | +{ |
| 25 | + const unsigned int startFrameSize = 4; |
| 26 | + const unsigned int endFrameSize = (ledValues.size() + 63) / 64 * 4; |
| 27 | + const unsigned int mLedCount = (ledValues.size() * 4) + startFrameSize + endFrameSize; |
| 28 | + if(_ledBuffer.size() != mLedCount){ |
| 29 | + _ledBuffer.resize(mLedCount, 0x00); |
| 30 | + _ledBuffer[0] = 'A'; |
| 31 | + _ledBuffer[1] = 'd'; |
| 32 | + _ledBuffer[2] = 'a'; |
| 33 | + _ledBuffer[3] = (((unsigned int)(ledValues.size() * 1.33) - 1) >> 8) & 0xFF; // LED count high byte |
| 34 | + _ledBuffer[4] = ((unsigned int)(ledValues.size() * 1.33) - 1) & 0xFF; // LED count low byte |
| 35 | + _ledBuffer[5] = _ledBuffer[3] ^ _ledBuffer[4] ^ 0x55; // Checksum |
| 36 | + } |
| 37 | + |
| 38 | + for (unsigned iLed=1; iLed<=ledValues.size(); iLed++) { |
| 39 | + const ColorRgb& rgb = ledValues[iLed]; |
| 40 | + _ledBuffer[iLed*4+6] = 0xFF; |
| 41 | + _ledBuffer[iLed*4+1+6] = rgb.red; |
| 42 | + _ledBuffer[iLed*4+2+6] = rgb.green; |
| 43 | + _ledBuffer[iLed*4+3+6] = rgb.blue; |
| 44 | + } |
| 45 | + |
| 46 | + // restart the timer |
| 47 | + _timer.start(); |
| 48 | + |
| 49 | + // write data |
| 50 | + return writeBytes(_ledBuffer.size(), _ledBuffer.data()); |
| 51 | +} |
| 52 | + |
| 53 | + |
0 commit comments