-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpcInterpreter.cpp
More file actions
65 lines (53 loc) · 2.1 KB
/
Copy pathOpcInterpreter.cpp
File metadata and controls
65 lines (53 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include "OpcInterpreter.h"
#include <ctime>
#include <iostream>
#include <utility>
#include <memory>
// Protocol constants
const size_t LED_COUNT_HIGH_BYTE_OFFSET = 2;
const size_t LED_COUNT_LOW_BYTE_OFFSET = 3;
const size_t DATA_OFFSET = 4;
const size_t DATA_STRIDE = 3;
const size_t RED_OFFSET = 0;
const size_t GREEN_OFFSET = 1;
const size_t BLUE_OFFSET = 2;
OpcInterpreter::OpcInterpreter(std::shared_ptr<LedInterface> ledInterface)
: Interpreter(std::move(ledInterface))
{
}
uint32_t OpcInterpreter::getBufferSize()
{
return static_cast<uint32_t>(ledInterface->getLedCount() * 3 + DATA_OFFSET);
}
void OpcInterpreter::interpretData(uint8_t* data, size_t length)
{
if (length >= DATA_OFFSET) {
uint16_t ledCountReceived = (data[LED_COUNT_HIGH_BYTE_OFFSET] << 8) | data[LED_COUNT_LOW_BYTE_OFFSET];
if (ledCountReceived != ledInterface->getLedCount()) {
std::cout << "Warning: header contains data for " << ledCountReceived << " LEDs but server is configured to "
<< ledInterface->getLedCount() << " LEDs" << std::endl;
} else {
std::cout << "Received LED count: " << ledCountReceived << std::endl;
}
if (ledCountReceived * 3 + DATA_OFFSET != length) {
std::cout << "Warning: invalid data length " << length << " bytes for " << ledCountReceived << " LEDs" << std::endl;
} else {
std::cout << "Acceptable data length" << std::endl;
}
size_t ledCountLength = (length - DATA_OFFSET) / 3;
uint32_t ledCount = std::min(static_cast<uint32_t>(ledCountReceived), ledInterface->getLedCount());
if (ledCountLength < ledCount) {
ledCount = static_cast<uint16_t>(ledCountLength);
}
for (uint16_t pos = 0; pos < ledCount; pos++) {
size_t offset = DATA_OFFSET + pos * DATA_STRIDE;
if (pos == 0) {
std::cout << "Setting pixel color at position 0 to " << int(data[offset + RED_OFFSET]) << ", " << int(data[offset + GREEN_OFFSET])
<< ", " << int(data[offset + BLUE_OFFSET]) << std::endl;
}
ledInterface->set(pos, data[offset + RED_OFFSET], data[offset + GREEN_OFFSET], data[offset + BLUE_OFFSET]);
}
std::cout << "Displaying..." << std::endl;
ledInterface->transmit();
}
}