-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathColorSensor.java
More file actions
214 lines (177 loc) · 4.08 KB
/
Copy pathColorSensor.java
File metadata and controls
214 lines (177 loc) · 4.08 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
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package ev3.exercises.library;
import java.util.ArrayList;
import lejos.hardware.port.Port;
import lejos.hardware.sensor.EV3ColorSensor;
import lejos.robotics.Color;
import lejos.robotics.ColorDetector;
import lejos.robotics.ColorIdentifier;
public class ColorSensor implements ColorDetector, ColorIdentifier
{
EV3ColorSensor sensor;
float[] sample;
/**
* Creates ColorSensor object. This is a wrapper class for EV3ColorSensor.
* @param port SensorPort of EV3ColorSensor device.
*/
public ColorSensor(Port port)
{
sensor = new EV3ColorSensor(port);
setAmbientMode();
setFloodLight(false);
}
/**
* Returns the underlying EV3ColorSensor object.
* @return Sensor object reference.
*/
public EV3ColorSensor getSensor()
{
return sensor;
}
/**
* Set color sensor to ambient light level mode.
*/
public void setAmbientMode()
{
sensor.setCurrentMode("Ambient");
sample = new float[sensor.sampleSize()];
}
/**
* Set color sensor to RED light level mode.
*/
public void setRedMode()
{
sensor.setCurrentMode("Red");
sample = new float[sensor.sampleSize()];
}
/**
* Set color sensor to color id mode.
*/
public void setColorIdMode()
{
sensor.setCurrentMode("ColorID");
sample = new float[sensor.sampleSize()];
}
/**
* Set color sensor to RGB mode.
*/
public void setRGBMode()
{
sensor.setCurrentMode("RGB");
sample = new float[sensor.sampleSize()];
}
/**
* Returns current detected color. Use with Color Id mode.
* @return Color id. Color ids are in the Color object.
*/
@Override
public int getColorID()
{
sensor.fetchSample(sample, 0);
return (int) sample[0];
}
/**
* Returns Color object with current detected color. Use with
* RGB mode and white light on target. Note that these values are
* the relative intensity of the reflected light of the primary colors.
* This is not the actual RGB value that would reproduce the color of
* the target surface.
* @return Color object with RGB intensity values of detected color.
*/
@Override
public Color getColor()
{
sensor.fetchSample(sample, 0);
return new Color((int)(sample[0] * 255), (int)(sample[1] * 255), (int)(sample[2] * 255));
}
/**
* Return ambient light level. Use with Ambient mode. Sensor led should be off.
* @return Light level as range 0 to 1.
*/
public float getAmbient()
{
sensor.fetchSample(sample, 0);
return sample[0];
}
/**
* Return Red light level. Use with Red mode. Sensor led should be red.
* @return Light level as range 0 to 1.
*/
public float getRed()
{
sensor.fetchSample(sample, 0);
return sample[0];
}
/**
* Release resources.
*/
public void close()
{
sensor.close();
}
/**
* Return current status of floodlight led.
* @return True if on, false if off.
*/
public boolean isFloodLightOn()
{
return sensor.isFloodlightOn();
}
/**
* Set floodlight led on/off with default color.
* @param on True to turn floodlight on, false to turn off.
*/
public void setFloodLight(boolean on)
{
sensor.setFloodlight(on);
}
/**
* Set floodlight default led color.
* @param color Color id value from Color object.
*/
public void setFloodLight(int color)
{
sensor.setFloodlight(color);
}
/**
* Map color integer to name.
* @param color Color id value.
* @return String with color name.
*/
public static String colorName(int color)
{
switch (color)
{
case Color.NONE:
return "None";
case Color.BLACK:
return "Black";
case Color.BLUE:
return "Blue";
case Color.BROWN:
return "Brown";
case Color.CYAN:
return "Cyan";
case Color.DARK_GRAY:
return "Dark Gray";
case Color.GRAY:
return "Gray";
case Color.GREEN:
return "Green";
case Color.LIGHT_GRAY:
return "Light Gray";
case Color.MAGENTA:
return "Magenta";
case Color.ORANGE:
return "Orange";
case Color.PINK:
return "Pink";
case Color.RED:
return "Red";
case Color.WHITE:
return "White";
case Color.YELLOW:
return "Yellow";
}
return "";
}
}