Repository navigation
Expand file tree
/
Copy pathEV3Key.java
More file actions
337 lines (294 loc) · 11.5 KB
/
Copy pathEV3Key.java
File metadata and controls
337 lines (294 loc) · 11.5 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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
package ev3dev.sensors;
import ev3dev.hardware.EV3DevPropertyLoader;
import lejos.hardware.Key;
import lejos.hardware.KeyListener;
import lejos.hardware.Keys;
import lombok.extern.slf4j.Slf4j;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.function.BiConsumer;
import java.util.stream.Collectors;
/**
* Implementation of the {@link Key} interface for EV3.
*
* <p>The easiest way to get an instance of this class is to use one of the static constants of
* the {@link Button} class.
*
* @author Anthony
* @author Juan Antonio Brenha Moral
* @author Jaroslav Tupy
*/
@Slf4j
public class EV3Key implements Key {
public static final int BUTTON_UP = 0x67;
public static final int BUTTON_DOWN = 0x6c;
public static final int BUTTON_LEFT = 0x69;
public static final int BUTTON_RIGHT = 0x6a;
public static final int BUTTON_ENTER = 0x1c;
public static final int BUTTON_BACKSPACE = 0x0e; // == BUTTON_ESCAPE; kept for backward compatibility
public static final int BUTTON_ESCAPE = 0x0e;
public static final int BUTTON_ALL = 0xff;
// VisibleForTesting
static final byte STATE_KEY_DOWN = 1;
static final byte STATE_KEY_UP = 0;
// to look at the raw values, use: $ hexdump -e '16/1 "%02x " "\n"' < /dev/input/by-path/platform-gpio-keys.0-event
private static final int EVENT_BUFFER_LEN = 16;
private static final int KEY_ID_INDEX = 10; // one of the BUTTON_* values
private static final int KEY_STATE_INDEX = 12; // 1 for down, 0 for up
// a bit-wise OR of the MASK_* bits above (except for MASK_ALL) depending on which keys are currently pressed
private static byte keyBits = 0;
private static Map<KeyType, Set<KeyListener>> keyListeners = new HashMap<>(KeyType.values().length);
static {
Arrays.stream(KeyType.values()).forEach((type) -> keyListeners.put(
type, new CopyOnWriteArraySet<>()));
// the CopyOfWriteArraySet will spare us a lot of hussle with thread safety
}
// a single ever-running (or waiting) thread keeping track of the currently pressed/released buttons
private static final Thread keyEventReader;
static {
keyEventReader = new Thread(() -> {
final byte[] event = new byte[EVENT_BUFFER_LEN];
final EV3DevPropertyLoader ev3DevPropertyLoader = new EV3DevPropertyLoader();
final Properties ev3DevProperties = ev3DevPropertyLoader.getEV3DevProperties();
final String SYSTEM_EVENT_PATH = ev3DevProperties.getProperty("ev3.key");
try (final DataInputStream in = new DataInputStream(new FileInputStream(SYSTEM_EVENT_PATH))) {
while (true) {
// reading the event
in.readFully(event);
// storing the change
final byte keyId = event[KEY_ID_INDEX];
final byte keyState = event[KEY_STATE_INDEX];
if (keyId != 0) {
// for some reason, every event is followed with another one
// where everything except the timestamp is zero
processKeyEvent(keyId, keyState);
}
}
} catch (final IOException e) {
LOGGER.error("Failed to read key press: " + e.getLocalizedMessage());
// TODO: something like an exponential backoff reading restart in an error case like this
}
});
keyEventReader.setDaemon(true); // this causes this thread to terminate when the main thread terminates
keyEventReader.start(); // non-blocking call to start the thread
}
// package-private such that it's visible from test
enum KeyType {
UP("UP", BUTTON_UP, Keys.ID_UP), // notice that the Keys.ID_* are single bit masks (verified in unit test)
DOWN("DOWN", BUTTON_DOWN, Keys.ID_DOWN),
LEFT("LEFT", BUTTON_LEFT, Keys.ID_LEFT),
RIGHT("RIGHT", BUTTON_RIGHT, Keys.ID_RIGHT),
ENTER("ENTER", BUTTON_ENTER, Keys.ID_ENTER),
ESCAPE("ESCAPE", BUTTON_ESCAPE, Keys.ID_ESCAPE),
ALL("ALL", BUTTON_ALL, 0xff);
// the 0xff must cover all bits of individual keys above (verified in unit tests)
private static final Map<Byte, KeyType> LOOKUP = Arrays.stream(KeyType.values())
.collect(Collectors.toMap(KeyType::getId, (keyType) -> keyType));
private final String name;
private final byte id;
private final byte bitMask;
KeyType(final String name, final int id, final int bitMask) {
this.name = name;
this.id = (byte) id;
this.bitMask = (byte) bitMask;
}
public static KeyType of(final int id) {
return Optional.ofNullable(LOOKUP.get((byte) id))
.orElseThrow(() -> new IllegalArgumentException(
String.format("No such keyType (%d), please use one of the EV3Key.BUTTON_* constants", id)));
}
public String getName() {
return name;
}
public byte getId() {
return id;
}
public byte getBitMask() {
return this.bitMask;
}
public boolean isPressed() {
return (KeyType.ALL == this ? (keyBits > 0) : ((keyBits & this.bitMask) > 0));
}
}
private KeyType keyType;
/**
* Create an Instance of EV3Key.
*
* @param keyType keyType
*/
public EV3Key(final KeyType keyType) {
this.keyType = Objects.requireNonNull(keyType);
}
/**
* Create an Instance of EV3Key by the numeric key ID.
*
* @param id id
*/
public EV3Key(final int id) {
this(KeyType.of(id));
}
/**
* Returns the ID of this key.
*
* @return one of the BUTTON_* constants
*/
@Override
public int getId() {
return this.keyType.getId();
}
/**
* A non-blocking check of whether this key is currently pressed.
*
* <p>This is
*
* @return true for pressed, false otherwise
*/
@Override
public boolean isDown() {
return this.keyType.isPressed();
}
/**
* A non-blocking check of whether this key is currently released.
*
* @return true for released, false otherwise
*/
@Override
public boolean isUp() {
return (!this.keyType.isPressed());
}
/**
* A blocking call that will return once this key gets pressed.
*
* <p>This method returns immediately if this key is pressed already.
*/
@Override
public void waitForPress() {
synchronized (keyEventReader) {
while (!this.isDown()) { // we return immediately if this key is already pressed
try {
keyEventReader.wait();
} catch (final InterruptedException e) {
LOGGER.warn("Interrupted while waiting for {} key press: {}",
this.keyType.getName(), e.getLocalizedMessage());
}
}
}
}
/**
* A blocking call that will first wait for this key being pressed and then it waits for this key being released.
*
* <p>Unlike {@link #waitForPress()}, this method will always block because this key cannot be already
* pressed and released at the same time. At the least, this key will be pressed so this method will only
* wait for it to be released.
*/
@Override
public void waitForPressAndRelease() {
this.waitForPress();
// this will either return immediately of this key is already pressed or wait until it gets pressed
synchronized (keyEventReader) {
while (this.isDown()) {
try {
keyEventReader.wait();
} catch (final InterruptedException e) {
LOGGER.warn("Interrupted while waiting for {} key release: {}",
this.keyType.getName(), e.getLocalizedMessage());
}
}
}
}
/**
* Adds a listener for this key's 'pressed' and 'released' events.
*
* <p>If this is the Button#ALL key, the listener will be getting notifications for any key events.
*
* @param keyListener keyListener
*/
@Override
public void addKeyListener(final KeyListener keyListener) {
keyListeners.get(this.keyType).add(Objects.requireNonNull(keyListener));
}
/**
* Removes the given key event listener.
*
* @param keyListener keyListener
*/
public void removeKeyListener(final KeyListener keyListener) {
keyListeners.get(this.keyType).remove(Objects.requireNonNull(keyListener));
}
@Override
public void simulateEvent(final int i) {
throw new UnsupportedOperationException("Not implemented");
}
/**
* Returns the name of this key.
*
* @return e.g. "LEFT" or "ENTER"
*/
@Override
public String getName() {
return this.keyType.getName();
}
/**
* @param obj obj
* @return true if the {@code obj} is an instance of {@link Key} and if the button IDs are the same.
*/
@Override
public boolean equals(final Object obj) {
if (obj == null || !(obj instanceof Key)) {
return false;
} else if (obj == this) { // pointer comparison
return true;
} else {
return ((int) this.keyType.getId() == ((Key) obj).getId());
}
}
/**
* @return the button ID based hash code
*/
@Override
public int hashCode() {
return Integer.hashCode(this.keyType.getId());
}
@Override
public String toString() {
return String.format("%s:%s", this.getClass().getSimpleName(), this.keyType.getName());
}
// package-private such that it's VisibleForTesting
static void processKeyEvent(final byte keyId, final byte keyState) {
final KeyType keyType = KeyType.of(keyId);
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("KeyType {} {}", keyType.name, (keyState == 0 ? "released" : "pressed"));
}
if (keyState == STATE_KEY_UP) {
keyBits ^= keyType.bitMask; // clearing the key bit
broadcastToListeners(keyType, KeyListener::keyReleased);
} else if (keyState == STATE_KEY_DOWN) {
keyBits |= keyType.bitMask; // setting the key bit
broadcastToListeners(keyType, KeyListener::keyPressed);
} else {
LOGGER.warn("Unexpected key state: " + keyState);
}
}
// TODO: parhaps notify listeners in a separate Thread to avoid blocking the key event reader thread
// TODO: ... or not - Threads consume resources and notifying in a separate Thread
// also means the events may no longer come in correct order
private static void broadcastToListeners(
final KeyType keyType, final BiConsumer<KeyListener, Key> notificationMethod) {
// key-specific listeners
keyListeners.get(keyType).forEach((listener) -> notificationMethod.accept(listener, new EV3Key(keyType)));
// all-key listeners
keyListeners.get(KeyType.ALL).forEach((listener) -> notificationMethod.accept(listener, new EV3Key(keyType)));
synchronized (keyEventReader) {
keyEventReader.notifyAll();
}
}
}