aboutsummaryrefslogtreecommitdiff
path: root/PunchingBag/src/PunchingBag.java
blob: 0f08276151c60193732639af2ce332b12cea4eaf (plain)
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
import javax.swing.event.EventListenerList;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;

// TODO: Loads of threads in this class, make sure everything is thread safe.

public class PunchingBag implements Runnable {
	boolean ledsChanged = false;
	final public int ledHeight = 20;
	final public int ledWidth = 9;
	private byte[] rawLeds = new byte[6 * 8];
	private Colour[][] leds = new Colour[ledWidth][ledHeight];
	private byte[] ledGridIntensities = new byte[6];
	final public int buttonHeight = 19;
	final public int buttonWidth = 8;
	private boolean[][] buttons = new boolean[buttonWidth][buttonHeight];
	boolean buttonsChanged = false;

	private ArrayList<Effect> runningEffects = new ArrayList<Effect>();
	
	ButtonArduino buttonArduino = new ButtonArduino();

	public enum Colour {
		None, Red, Yellow, Green
	};

	// Is there a better class for this? One that it is not a swing class.
	private EventListenerList buttonListenerList = new EventListenerList();
	private EventListenerList ledListenerList = new EventListenerList();
	private EventListenerList accelListenerList = new EventListenerList();


	public PunchingBag() {
		new Thread(this).start();
	}

	/**
	 * Adds an <code>ActionListener</code> to the button.
	 * 
	 * @param l
	 *            the <code>ActionListener</code> to be added
	 */
	public void addButtonListener(ButtonListener l) {
		buttonListenerList.add(ButtonListener.class, l);
	}

	public void addLEDChangeListener(LEDListener l) {
		ledListenerList.add(LEDListener.class, l);
	}
	
	public void addLEDChangeListener(AccelListener l) {
		accelListenerList.add(AccelListener.class, l);
	}

	public Colour getLED(int x, int y) {
		return leds[x][y];
	}

	public boolean setLED(int x, int y, Colour colour) {
		if (x >= 0 && x < ledWidth && y >= 0 && y < ledHeight) {
			if (leds[x][y] != colour) {
				leds[x][y] = colour;
				ledsChanged = true;

			}
			return true;
		} else {
			return false;
		}
	}

	void CircleExpand(int x, int y, int intensity) {
		runningEffects.add(new CircleExpand(x, y, 100));
	}

	abstract class Effect {
		long lastRefresh = 0;
		boolean stop = false;

		// int refreshRate = 60; // Times per second to refresh

		public void stop() {
			stop = true;
		}

		abstract public void draw();
	}

	class CircleExpand extends Effect {
		final int x;
		final int y;
		int intensity;
		boolean drawnSomething = true;

		float currentRadius = 0;

		public CircleExpand(int x, int y, int intensity) {
			this.x = x;
			this.y = y;
			this.intensity = intensity;
		}

		public void draw() {
			Colour colour;

			if (intensity >= 10) {
				colour = Colour.Red;
			} else if (intensity >= 5) {
				colour = Colour.Yellow;
			} else {
				colour = Colour.Green;
			}

			intensity -= 5;

			currentRadius += 0.1 + 0.1 + (currentRadius / 20);
			// currentRadius += 0.01;
			// longhand: currentRadius = currentRadius + 0.1 + (currentRadius /
			// 10);

			if (!DrawEllipse(x, y, (int) currentRadius, (int) currentRadius,
					colour))
				stop = true;
		}

	}

	public boolean DrawEllipse(int x, int y, int radx, int rady, Colour colour) {
		if (radx == 0 && rady == 0) {
			return setLED(x, y, colour);
		}

		boolean doneSomething = false;

		int dx = 0, dy = rady; /* first quadrant from top left to bottom right */
		int a2 = radx * radx, b2 = rady * rady;
		int err = b2 - (2 * rady - 1) * a2, e2; /* error value in the first step */

		do {
			if (setLED(x + dx, y + dy, colour)) {/* I. Quadrant */
				doneSomething = true;
			}
			if (setLED(x - dx, y + dy, colour)) {/* II. Quadrant */
				doneSomething = true;
			}
			if (setLED(x - dx, y - dy, colour)) {/* III. Quadrant */
				doneSomething = true;
			}
			if (setLED(x + dx, y - dy, colour)) {/* IV. Quadrant */
				doneSomething = true;
			}

			e2 = 2 * err;
			if (e2 < (2 * dx + 1) * b2) {
				dx++;
				err += (2 * dx + 1) * b2;
			}
			if (e2 > -(2 * dy - 1) * a2) {
				dy--;
				err -= (2 * dy - 1) * a2;
			}
		} while (dy >= 0);

		while (dx++ < radx) { /* correction for flat ellipses (b=1) */
			if (setLED(x + dx, y, colour)) {
				doneSomething = true;
			}
			if (setLED(x - dx, y, colour)) {
				doneSomething = true;
			}
		}
		return doneSomething;
	}

	private void calculateRawLeds() {
		for (int y = 0; y <= 18; y++) {
			for (int x = 0; x <= 6; x++) {
				if ((y % 2) == 0) {
					if (leds[x][y] == Colour.Green
							|| leds[x][y] == Colour.Yellow) {
						rawLeds[(int) Math.floor(y / 4)] = (byte) (rawLeds[(int) Math
								.floor(y / 4)] | (1 << (7 - x)));
					}
				} else {
					if (leds[x][y] == Colour.Red || leds[x][y] == Colour.Yellow) {
						rawLeds[(int) Math.floor(y / 4)] = (byte) (rawLeds[(int) Math
								.floor(y / 4)] | (1 << (7 - x)));
					}
				}
			}
		}
		int x = 7;
		// TODO: Complete and test, or rethink the following?
		for (int startY = 0; startY <= 4; startY++) {
			for (int y = 0; y <= 3; y++) {
				if (leds[x][startY + (y * 4)] == Colour.Red
						|| leds[x][startY + (y * 4)] == Colour.Yellow) {
					rawLeds[(5 * 8) + startY] = (byte) (rawLeds[(int) Math
							.floor(y / 4)] | (1 << (7 - x)));
				}
				if (leds[x][y] == Colour.Green || leds[x][y] == Colour.Yellow) {
					rawLeds[(int) Math.floor(y / 4)] = (byte) (rawLeds[(int) Math
							.floor(y / 4)] | (1 << (7 - x)));
				}
			}
		}
	}

	/* Clears the led grid and stops all running effects */
	void clearLEDGrid() {
		/*
		 * for (Iterator<Effect> iter = runningEffects.iterator();
		 * iter.hasNext();) { ((Effect) iter.next()).stop(); }
		 */
		for (int x = 0; x < ledWidth; x++) {
			for (int y = 0; y < ledHeight; y++) {
				leds[x][y] = Colour.None;
			}
		}
		// Arrays.fill(leds,Colour.None);
	}

	public void setLEDGridIntensity(byte grid, byte intensity) {
		ledGridIntensities[grid] = intensity;
	}

	public void setLEDIntensities(byte intensity) {
		Arrays.fill(ledGridIntensities, intensity);
	}

	public void buttonPressed(int x, int y) {
		buttons[x][y] = true;
		buttonsChanged = true;
	}

	public void run() {
		while (true) {
			synchronized (runningEffects) { // Should prevent
				// ConcurrentModificationException's
				// (havent managed to produce one
				// though
				for (Iterator<Effect> iter = runningEffects.iterator(); iter
						.hasNext();) {
					Effect ef = (Effect) iter.next();
					if (ef.stop) {
						iter.remove();
					} else {// if ((ef.lastRefresh + (1000 / ef.refreshRate)) <=
						// Systems.currentTimeMillis()) {
						ef.draw();
						ef.lastRefresh = System.currentTimeMillis();
					}
				}
			}

			if (ledsChanged) {
				LEDListener[] LEDListeners = ledListenerList
						.getListeners(LEDListener.class);
				for (int i = 0; i < ledListenerList.getListenerCount(); i++) {
					LEDListeners[i].LEDchanged();
				}
			}

			if (buttonsChanged) {
				ButtonListener[] buttonListeners = buttonListenerList
						.getListeners(ButtonListener.class);
				for (int i = 0; i < buttonListenerList.getListenerCount(); i++) {
					for (int x = 0; x < buttonWidth; x++) {
						for (int y = 0; y < buttonHeight; y++) {
							if (buttons[x][y])
								buttonListeners[i].buttonPressed(x, y);
						}
					}
				}
			}

			// TODO: Hack?
			for (int x = 0; x < buttonWidth; x++) {
				for (int y = 0; y < buttonHeight; y++) {
					buttons[x][y] = false;
				}
			}

			// TODO: Hack?
			clearLEDGrid();

			try {
				Thread.sleep(1000 / 60);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}