-
Notifications
You must be signed in to change notification settings - Fork 1
/
ProtocolStep.java
110 lines (86 loc) · 2.85 KB
/
ProtocolStep.java
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package com.fyayc.essen.busylight.core.protocol;
import com.fyayc.essen.busylight.core.protocol.SpecConstants.Light;
import com.fyayc.essen.busylight.core.protocol.bytes.StepByte;
import com.google.common.base.MoreObjects;
import java.util.stream.Stream;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
/** a protocol step */
public class ProtocolStep {
public static final int BYTE_LENGTH = 8;
protected static final Logger logger = LogManager.getLogger(ProtocolStep.class);
private final StepByte[] stepBytes;
public ProtocolStep(StepByte[] stepBytes) {
this.stepBytes = stepBytes;
}
public static ProtocolStep empty() {
StepByte[] steps = new StepByte[BYTE_LENGTH];
for (int i = 0; i < BYTE_LENGTH; i++) {
steps[i] = StepByte.EMPTY;
}
return new ProtocolStep(steps);
}
public static StepBuilder builder() {
return new StepBuilder();
}
void setByte(StepByte stepByte, int index) {
if (index < 0 || index > BYTE_LENGTH - 1) {
throw new IllegalArgumentException("index must be between 0 and 7");
}
stepBytes[index] = stepByte;
}
public byte[] getBytes() {
byte[] bytes = new byte[stepBytes.length];
for (int i = 0; i < stepBytes.length; i++) {
bytes[i] = stepBytes[i].getCurrentByte();
}
return bytes;
}
public int checkSum() {
return Stream.of(stepBytes).map(StepByte::toInt).reduce(0, (a, b) -> a + b);
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this).add("stepBytes", stepBytes).toString();
}
public String toHexString() {
return Stream.of(stepBytes).map(b -> b.hex() + " ").reduce(" ", (a, b) -> a + b);
}
public static class StepBuilder {
private StepByte[] steps;
public StepBuilder() {
this.steps = new StepByte[ProtocolStep.BYTE_LENGTH];
for (int i = 0; i < steps.length; i++) {
steps[i] = StepByte.EMPTY;
}
}
public StepBuilder add(int index, StepByte sbyte) {
if (index < 0 || index > BYTE_LENGTH - 1) {
throw new IllegalArgumentException("index must be between 0 and 7");
}
steps[index] = sbyte;
return this;
}
public StepBuilder command(StepByte command) {
return add(0, command);
}
public StepBuilder repeat(StepByte repeat) {
return add(1, repeat);
}
public StepBuilder light(StepByte red, StepByte green, StepByte blue) {
return add(2, red).add(3, green).add(4, blue);
}
public StepBuilder light(Light status) {
return light(status.rgbBytes[0], status.rgbBytes[1], status.rgbBytes[2]);
}
public StepBuilder lightDuration(StepByte on, StepByte off) {
return add(5, on).add(6, off);
}
public StepBuilder tone(StepByte tone) {
return add(7, tone);
}
public ProtocolStep build() {
return new ProtocolStep(steps);
}
}
}