| 60 | } |
| 61 | |
| 62 | void BlinkyTapeController::SetLEDs(std::vector<RGBColor> colors) |
| 63 | { |
| 64 | if(serialport == nullptr) |
| 65 | { |
| 66 | return; |
| 67 | } |
| 68 | |
| 69 | /*-------------------------------------------------------------*\ |
| 70 | | BlinkyTape Protocol | |
| 71 | | | |
| 72 | | Packet size: Number of data bytes + 1 | |
| 73 | | | |
| 74 | | 0-n: Data Byte (0-254) | |
| 75 | | n+1: Packet End Byte (0xFF) | |
| 76 | \*-------------------------------------------------------------*/ |
| 77 | const unsigned int payload_size = (colors.size() * 3); |
| 78 | const unsigned int packet_size = payload_size + 1; |
| 79 | |
| 80 | std::vector<unsigned char> serial_buf(packet_size); |
| 81 | |
| 82 | /*-------------------------------------------------------------*\ |
| 83 | | Set up end byte | |
| 84 | \*-------------------------------------------------------------*/ |
| 85 | serial_buf[packet_size - 1] = 0xFF; |
| 86 | |
| 87 | /*-------------------------------------------------------------*\ |
| 88 | | Copy in color data in RGB order | |
| 89 | \*-------------------------------------------------------------*/ |
| 90 | for(unsigned int color_idx = 0; color_idx < colors.size(); color_idx++) |
| 91 | { |
| 92 | const unsigned int color_offset = color_idx * 3; |
| 93 | |
| 94 | serial_buf[0x00 + color_offset] = std::min((unsigned int)254, RGBGetRValue(colors[color_idx])); |
| 95 | serial_buf[0x01 + color_offset] = std::min((unsigned int)254, RGBGetGValue(colors[color_idx])); |
| 96 | serial_buf[0x02 + color_offset] = std::min((unsigned int)254, RGBGetBValue(colors[color_idx])); |
| 97 | } |
| 98 | |
| 99 | /*-------------------------------------------------------------*\ |
| 100 | | Send the packet | |
| 101 | \*-------------------------------------------------------------*/ |
| 102 | serialport->serial_write((char *)serial_buf.data(), packet_size); |
| 103 | } |
| 104 |
no test coverage detected