-
Notifications
You must be signed in to change notification settings - Fork 0
/
PowerButton.java
executable file
·170 lines (150 loc) · 4.19 KB
/
PowerButton.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
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
package peter;
import java.util.Enumeration;
import java.util.Hashtable;
import lejos.nxt.Button;
import lejos.nxt.ButtonListener;
import lejos.nxt.Sound;
/**
* This gives buttons more power, including the ability to change and remove button listeners. There can be up
* to four instances of PowerButton per program, as there can only be four buttonlisteners per button per
* program. Can only handle one wait for press at a time.
*
* @author Peter Ehrlich
*
*/
public class PowerButton {
private String[] mode = new String[9]; // these three lines would be better to add Button
private boolean[] pressed = new boolean[9]; // where 'pressed' means was pressed
private boolean[] punched = new boolean[9]; // where 'punched' means was pressed and
// released
private Hashtable modes = new Hashtable();
public static final String NONE = "_none_";
public Button ENTER = Button.ENTER;
public Button ESCAPE = Button.ESCAPE;
public Button LEFT = Button.LEFT;
public Button RIGHT = Button.RIGHT;
public PowerButton() {
modes.put(NONE, new ButtonMode() {
public void action() {
}
});
mode[RIGHT.getId()] = NONE;
mode[LEFT.getId()] = NONE;
mode[ENTER.getId()] = NONE;
mode[ESCAPE.getId()] = NONE;
Button.setKeyClickTone(ENTER.getId(), 0);
Button.setKeyClickTone(RIGHT.getId(), 0);
Button.setKeyClickTone(LEFT.getId(), 0);
Button.setKeyClickTone(ESCAPE.getId(), 0);
resetPress();
ButtonListener universal = new ButtonListener() {
public void buttonPressed(Button b) {
if (!pressed[b.getId()]) {
pressed[b.getId()] = true;
if (!mode[b.getId()].equals(NONE)) {
Sound.beep(); // TODO: make this pretty. Use an array of sounds for each button
((ButtonMode) modes.get(mode[b.getId()])).action();
}
}
}
public void buttonReleased(Button b) {
if (pressed[b.getId()]) {
pressed[b.getId()] = false;
punched[b.getId()] = true;
}
}
};
ENTER.addButtonListener(universal);
ESCAPE.addButtonListener(universal);
LEFT.addButtonListener(universal);
RIGHT.addButtonListener(universal);
}
/**
* Waits for press and release on button, checking every 100ms
*
* @param b
* @throws InterruptedException
*/
public Button waitForPunch(Button[] b) throws InterruptedException {
String[] oldmode = new String[b.length];
for (int i = 0; i < b.length; i++) {
oldmode[i] = mode[b[i].getId()];
mode[b[i].getId()] = NONE;
}
resetPress();
while (true) {
for (int i = 0; i < b.length; i++) {
if (punched[b[i].getId()]) {
for (int j = 0; j < b.length; j++) {
mode[b[j].getId()] = oldmode[j];
}
Sound.beep(); // TODO: make this pretty
return b[i];
}
Thread.sleep(100);
}
}
}
public void waitForPunch(Button b) throws InterruptedException {
String oldmode = mode[b.getId()];
mode[b.getId()] = NONE;
resetPress();
while (true) {
if (punched[b.getId()]) {
Sound.beep(); // TODO: make this pretty
mode[b.getId()] = oldmode;
return;
}
Thread.sleep(100);
}
}
private void resetPress() {
pressed[ENTER.getId()] = false;
pressed[ESCAPE.getId()] = false;
pressed[LEFT.getId()] = false;
pressed[RIGHT.getId()] = false;
punched[ENTER.getId()] = false;
punched[ESCAPE.getId()] = false;
punched[LEFT.getId()] = false;
punched[RIGHT.getId()] = false;
}
/**
* Sets the mode of a button. Mode must exist. To remove all modes, set the mode to PowerButton.NONE;
*
* @param b
* @param mode
*/
public void setMode(Button b, String modeName) throws NullPointerException {
if (modes.get(modeName) == null) { // first check is named mode is in the map
throw new NullPointerException();
}
this.mode[b.getId()] = modeName;
}
/**
* Adds a new mode, of name name.
*
* @param bm
* @param name
* How this mode will be referred to
*/
public void addMode(String name, ButtonMode bm) {
modes.put(name, bm);
}
/**
* Gets the names of all the modes, or possible actions, known to this PowerButton instance.
*
* @return
*/
public Enumeration getModeName() {
return modes.keys();
}
/**
* Gets the current action, or "mode" of a button.
*
* @param b
* @return
*/
public String getMode(Button b) {
return mode[b.getId()];
}
}