-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLEDStrip.java
119 lines (104 loc) · 3.76 KB
/
LEDStrip.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
113
114
115
116
117
118
119
package org.usfirst.frc.team2465.robot;
import edu.wpi.first.wpilibj.SPI;
import java.util.Random;
import edu.wpi.first.wpilibj.SPI.Port;
import java.util.Arrays;
/**
* @author Andrew Cadwallader for FRC Team 2465 Kaua'iBots
*
*/
public class LEDStrip {
SPI spi;
int bitrate = 4000000;
int striplength = 120;
LEDStrip(){
spi = new SPI(Port.kOnboardCS0);
spi.setClockRate(bitrate);
spi.setMSBFirst();
spi.setSampleDataOnFalling();
spi.setClockActiveLow();
spi.setChipSelectActiveLow();
}
public boolean startFrame(){
byte[] header = new byte[4];
Arrays.fill(header, (byte)0x00);
return true;
}
public boolean emptyFrame(){
byte[] cmd = new byte[4];
Arrays.fill(cmd, (byte)0x00);
cmd[0] = (byte)0xE0;
return true;
}
public boolean randomFrame(){
byte[] cmd = new byte[4];//Create Array of bytes
Arrays.fill(cmd, (byte)0x00);//Fill Start Frame and Pololu End Frame
byte head = (byte)0xE0;//Set head to 11100000
Random random = new Random();
lum = (byte)0x01;
cmd[0] = (byte)(lum | head);//Header plus Brightness
rgb = (byte)random.nextInt(256);
cmd[1]=rgb;//Blue
rgb = (byte)random.nextInt(256);
cmd[2]=rgb;//Green
rgb = (byte)random.nextInt(256);
cmd[3]=rgb;//Red
return true;
}
public boolean writeFrame(byte l, byte r, byte g, byte b){
byte[] cmd = new byte[4];//Create Array of bytes
Arrays.fill(cmd, (byte)0x00);//Fill Start Frame and Pololu End Frame
byte head = (byte)0xE0;//Set head to 11100000
cmd[0]=(byte)(l | head);//Header plus Brightness
cmd[1]=b;//Blue
cmd[2]=g;//Green
cmd[3]=r;//Red
return true;
}
public boolean write() {
byte[] cmd = new byte[cmdsize];//Create Array of bytes
Arrays.fill(cmd, (byte)0x00);//Fill Start Frame and Pololu End Frame
byte head = (byte)0xE0;//Set head to 11100000
byte lum = (byte)0x00;//Set brightness
byte rgb = (byte)0x00;//Set color
for(int i=4 ; i<(striplength+1)*4 ; i+=4){cmd[i] = head;}//Attach header to command frames
Random random = new Random();
//int dim = 10;//Probability of an LED to light up
//int draw = random.nextInt(striplength);
//draw = draw/dim;
int draw = 10;//Uncomment to set fixed number of lights on
//To randomly select which LEDs to turn on
//Fill an array of length "draw" with random integers from 1 through "striplength" (inclusive)
int[] array = new int[draw];
for(int item : array){
array[item] = random.nextInt(striplength)+1;
}
//To turn on each LED selected above, set random Brightness and Color
for(int index=4 ; index<(striplength+1)*4 ; index+=4){
//int index = (i)*4;
//lum = (byte)random.nextInt(256);
lum = (byte)0x01;
cmd[index] = (byte)(lum | head);//Header plus Brightness
rgb = (byte)random.nextInt(256);
cmd[index+1]=rgb;//Blue
rgb = (byte)random.nextInt(256);
cmd[index+2]=rgb;//Green
rgb = (byte)random.nextInt(256);
cmd[index+3]=rgb;//Red
}
if (spi.write(cmd, cmd.length) != cmd.length) {
return false; // WRITE ERROR
}
return true;
}
public boolean test() {
byte[] cmd = new byte[cmdsize];//Create Array of bytes
Arrays.fill(cmd, (byte)0x00);//Fill Start Frame and Pololu End Frame
byte head = (byte)0xE0;//Set head to 11100000
for(int i=4 ; i<cmdsize ; i+=4){cmd[i] = head;}//Attach header to command frames
if (spi.write(cmd, cmd.length) != cmd.length) {
return false; // WRITE ERROR
}
return true;
}
}