forked from CS-4620/PA1
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMeggy.java
More file actions
318 lines (271 loc) · 10.8 KB
/
Copy pathMeggy.java
File metadata and controls
318 lines (271 loc) · 10.8 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
/**
* Meggy.java
*
* The Java-only definition of the meggy interface for use in the MeggyJava
* language. See the grammar for the MeggyJava language at
* http://www.cs.colostate.edu/~cs453/yr2011/MeggyJavaInfo/meggy-java-grammar.html
*
* NOTES:
* - Java does not have unsigned types and the MeggyJrSimple and MeggyJr
* libraries do use unsigned types. For the Java-only package I am
* going to use signed bytes for things like the x,y coordinates because
* they don't need all 256 positive values. For constants like colors
* we use enumerated types. For number of milliseconds using integers
* even though that will restrict the range somewhat.
*
* 12/27/10, 1/4/11, MMS
* Fall 2010, started by Ryan Moore
**/
package meggy;
// Imports
import java.io.*;
import java.util.*;
public final class Meggy
{
/** Specifies LED color, MJ compiler will translate to MeggyJrSimple
* color index.
*/
static public enum Color { DARK,
RED,
ORANGE,
YELLOW,
GREEN,
BLUE,
VIOLET,
WHITE }
/** Specifies which tone, MJ compiler will translate to MeggyJrSimple
* tone constants.
*/
static public enum Tone { C3,
Cs3,
D3,
Ds3,
E3,
F3,
Fs3,
G3,
Gs3,
A3,
As3,
B3 }
/** Specifies which button, MJ compiler will translate to MeggyJrSimple
* button constants.
*/
static public enum Button { B,
A,
Up,
Down,
Left,
Right }
/** The private array that stores the current color settings in the screen.
*/
static private final byte sScreenSize = 8;
static private final Color[][] sScreenBuffer
= {{Color.DARK,Color.DARK,Color.DARK,Color.DARK,Color.DARK,Color.DARK,Color.DARK,Color.DARK},
{Color.DARK,Color.DARK,Color.DARK,Color.DARK,Color.DARK,Color.DARK,Color.DARK,Color.DARK},
{Color.DARK,Color.DARK,Color.DARK,Color.DARK,Color.DARK,Color.DARK,Color.DARK,Color.DARK},
{Color.DARK,Color.DARK,Color.DARK,Color.DARK,Color.DARK,Color.DARK,Color.DARK,Color.DARK},
{Color.DARK,Color.DARK,Color.DARK,Color.DARK,Color.DARK,Color.DARK,Color.DARK,Color.DARK},
{Color.DARK,Color.DARK,Color.DARK,Color.DARK,Color.DARK,Color.DARK,Color.DARK,Color.DARK},
{Color.DARK,Color.DARK,Color.DARK,Color.DARK,Color.DARK,Color.DARK,Color.DARK,Color.DARK},
{Color.DARK,Color.DARK,Color.DARK,Color.DARK,Color.DARK,Color.DARK,Color.DARK,Color.DARK}
};
// = new Color[sScreenSize][sScreenSize];
// Max value for the bit array
static private final int sMaxBitArrayForLEDs = 255;
/** Input file with parameters for the run.
**/
static private boolean sAlreadyInit = false;
static private int sMaxCalls = 0; // Max number of calls to this interface before halting
static private boolean sDelaySim = true; // Are we simulating delays in Java.
// A list of button press sets. Each phase has a set. Phases are
// occur between delay calls.
static private LinkedList<HashSet<Meggy.Button>> sButtonPresses
= new LinkedList<HashSet<Meggy.Button>>();
static private int sCurrPhase = 0;
static private void init() {
sAlreadyInit = true;
HashSet<Meggy.Button> currSet;
// If you run the MeggyJava programs from the command line
// then your user.dir should be the directory the file is in.
// If you run it in Eclipse, your user.dir may be something different
// and you might have to change the relative path for arg_opts below.
//System.out.println(System.getProperty("user.dir"));
sButtonPresses.add(currSet = new HashSet<Meggy.Button>()); // zeroth phase
try {
// When run on command line
Scanner sc = new Scanner( new File("meggy/arg_opts") );
// When run from within Eclipse
//Scanner sc = new Scanner( new File("TestCasesMeggy/meggy/arg_opts") );
String line = sc.nextLine();
sMaxCalls = Integer.valueOf( line );
sDelaySim = Boolean.parseBoolean( sc.nextLine() );
//System.out.println("Steps=" + sMaxCalls); // debug only
//System.out.println("Delay=" + sDelaySim); // debug only
// Parse and run commands
while(sc.hasNextLine()){
String st = sc.nextLine().trim();
//System.out.println( st ); // debug only
if (st.equals("delay")) {
sButtonPresses.add(currSet = new HashSet<Meggy.Button>());
} else {
currSet.add(Meggy.Button.valueOf(st));
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
/** private constructor, MeggyJava programs don't instantiate an instance
* IOW, the Meggy class is really just a module wrapper for
* the interface and a single global array of pixel values.
* Meggy object instances should not be created.
*/
private Meggy()
{
}
/**
* Sets the pixel at position x,y to the given color and
* prints a gradeable message to indicate as such to
* stdout. Also checks input value ranges.
*
* The coordinate system starts at the lower left corner
* of the device at <0,0>
*
* @param x is the horizontal coordinate, indicates column
* @param y is the vertical coordinate, indicates row
* @param meggyColor is the color to for the pixel
**/
public static void setPixel(byte x, byte y, Color meggyColor)
{
if (!sAlreadyInit) { init(); }
sMaxCalls--; if (sMaxCalls<0) { System.exit(0); }
// Check that all input values fall within possible ranges.
if (!(0<=x && x<sScreenSize)) throw new MeggyException("x coordinate out of bounds, x = "+x);
if (!(0<=y && y<sScreenSize)) throw new MeggyException("y coordinate out of bounds, y = "+y);
// Set pixel in screen.
sScreenBuffer[x][y] = meggyColor;
// Print a gradeable output message.
System.out.println("Setting pixel ("+x+","+y+") to " + meggyColor);
}
/**
* Gets the pixel color at position x,y and
* prints a gradeable message to indicate as such to
* stdout. Also checks input value ranges.
*
* The coordinate system starts at the lower left corner
* of the device at <0,0>
*
* @param x is the horizontal coordinate
* @param y is the vertical coordinate
**/
public static Color getPixel( byte x, byte y )
{
if (!sAlreadyInit) { init(); }
sMaxCalls--; if (sMaxCalls<0) { System.exit(0); }
// Check that all input values fall within possible ranges.
if (!(0<=x && x<sScreenSize)) throw new MeggyException("x coordinate out of bounds, x = "+x);
if (!(0<=y && y<sScreenSize)) throw new MeggyException("y coordinate out of bounds, y = "+y);
// Print a gradeable output message.
System.out.println("Getting pixel ("+x+","+y+") ==>"
+ sScreenBuffer[x][y]);
// Get pixel value.
return sScreenBuffer[x][y];
}
/**
* Given a number from 0 to 255, this routine sets the
* corresponding auxiliary lights by treating the
* number like a bit array. The least significant bit
* represents the left-most auxiliary light.
*
* @param bitarray value between 0 and 255
**/
public static void setAuxLEDs( int bitarray )
{
if (!sAlreadyInit) { init(); }
sMaxCalls--; if (sMaxCalls<0) { System.exit(0); }
// Check that all input values fall within possible ranges.
if (!(0<=bitarray && bitarray<=sMaxBitArrayForLEDs))
throw new MeggyException("bitarray out of bounds, bitarray = "+bitarray);
// Print a gradeable output message.
System.out.print(
"Setting auxiliary LEDs from left to right as follows: ");
for (int i=1; i<=128; i=i*2) {
// check if a given bit is set
if ((bitarray & i)!=0) {
System.out.print(" 1 ");
} else {
System.out.print(" 0 ");
}
}
System.out.println();
}
/**
* Plays the specified tone for a specified amount of time.
*
* @param t which tone to play
* @param duration_ms duration of the tone in milliseconds
**/
public static void toneStart( Tone t, int duration_ms )
{
if (!sAlreadyInit) { init(); }
sMaxCalls--; if (sMaxCalls<0) { System.exit(0); }
// Check that the duration is positive.
if (!(0<duration_ms))
throw new MeggyException("duration must be positive, duration_ms="
+duration_ms);
// Print a gradeable output message.
System.out.println("Playing tone "+ t + " for "
+ duration_ms + " milliseconds");
}
/**
* Always indicates that the given button has been pressed.
*
* Since these routines are only being used for grading this
* simplifying assumption will enable deterministic grading.
*
* @param b which button to check
**/
public static boolean checkButton( Button b )
{
if (!sAlreadyInit) { init(); }
sMaxCalls--; if (sMaxCalls<0) { System.exit(0); }
// Do we have a phase for the current phase?
// && Check to see if it's a button press for this phase
if (sButtonPresses.size()>sCurrPhase &&
sButtonPresses.get(sCurrPhase).contains(b) )
{
// Print a gradeable output message.
System.out.println("Checking button "+ b + " and returning true");
return true;
} else {
// Print a gradeable output message.
System.out.println("Checking button "+ b + " and returning false");
return false;
}
}
/**
* Prints a gradeable delay message.
*
* @param duration_ms the number of milliseconds for the delay
**/
public static void delay( int duration_ms )
{
if (!sAlreadyInit) { init(); }
sMaxCalls--; if (sMaxCalls<0) { System.exit(0); }
// Keep track of phase we are in for pre-recorded button presses.
sCurrPhase += 1;
// Check that the duration is positive.
if (!(0<duration_ms)) {
throw new MeggyException("duration must be positive, duration_ms="
+duration_ms);
}
// Print a gradeable output message.
System.out.println("Delaying for "+ duration_ms + " milliseconds");
// Only perform delay if flag is set
if(sDelaySim) {
try { Thread.sleep(duration_ms); }
catch(InterruptedException e) { e.printStackTrace(); }
}
}
}