-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLedCustomAnimations.java
112 lines (91 loc) · 2.97 KB
/
LedCustomAnimations.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
111
112
package frc.util;
import edu.wpi.first.wpilibj.AddressableLED;
import edu.wpi.first.wpilibj.AddressableLEDBuffer;
import edu.wpi.first.wpilibj.Filesystem;
import java.io.File;
import java.io.FileReader;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
public class LedCustomAnimations {
private final AddressableLED m_Led;
private final AddressableLEDBuffer ledBuffer;
private boolean isLoop;
private boolean isActive;
private final String name;
private int Timer;
private final JSONArray json;
public LedCustomAnimations(
AddressableLED m_Led,
AddressableLEDBuffer ledBuffer,
String AnimationPath,
int startTime,
boolean isLoop) {
this.name = AnimationPath;
this.m_Led = m_Led;
this.ledBuffer = ledBuffer;
this.isLoop = isLoop;
this.isActive = false;
this.Timer = -startTime;
json = loadPath(AnimationPath);
}
public String getName(){
return name;
}
public void setIsActive(boolean value){
isActive = value;
}
public boolean isActive(){
return isActive;
}
public int getAnimationLength() {
return json.toArray().length;
}
public void reset(){
this.Timer = 0;
}
public void setLoop(boolean value) {
this.isLoop = value;
}
public void setAnimation() {
if (Timer < 0) {
Timer++;
return;
}
if (Timer >= getAnimationLength() && isLoop) Timer = 0;
if (Timer >= getAnimationLength() && !isLoop) return;
JSONObject frame = (JSONObject) json.get(Timer);
long red = (long) frame.get("r");
long green = (long) frame.get("g");
long blue = (long) frame.get("b");
long length = (long) frame.get("length");
for (int i = 0; i < ledBuffer.getLength(); i++) {
ledBuffer.setRGB(i, 0, 0, 0);
}
for (int i = 0; i < Math.floor(ledBuffer.getLength() * length/100); i++) {
ledBuffer.setRGB(i, (int) red, (int) green, (int) blue);
}
m_Led.setData(ledBuffer);
m_Led.start();
Timer++;
}
public JSONArray loadPath(String name) {
JSONParser jsonParser = new JSONParser();
// try (FileReader file = new FileReader(Filesystem.getDeployDirectory() + "5829LedAnimations/" + name + ".json")) {
try (FileReader file = new FileReader(new File(Filesystem.getDeployDirectory(), "5829LedAnimations/" + name + ".json"))){
Object obj = jsonParser.parse(file);
JSONArray json = (JSONArray) obj;
return json;
} catch (Exception e) {
e.printStackTrace();
return new JSONArray();
}
}
public void end(){
setLoop(false);
Timer = getAnimationLength();
}
public boolean isFinished(){
return Timer >= getAnimationLength();
}
}