aboutsummaryrefslogtreecommitdiff
path: root/PunchingBag/src/PunchingBag.java
blob: b179e2f09356a22141e2e6825a53799a40072365 (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
import javax.swing.event.EventListenerList;
import java.util.Arrays;

public class PunchingBag implements Runnable {
	private byte[] rawLeds = new byte[6*8];
	private Colour[][] leds = new Colour[9][20];
	private byte[] ledGridIntensities = new byte[6];
	private boolean[][] buttons = new boolean[8][19];

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

	private EventListenerList buttonListenerList = new EventListenerList();
	private EventListenerList ledListenerList = 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 setLED(byte x, byte y, Colour colour) {
		leds[x][y] = colour;
	}

	public void drawRipple(byte x, byte y, byte intensity) {
		new Ripple(x, y, intensity);
	}
	
	class Effect implements Runnable {
		boolean stop = false;
		int refreshRate = 60; // Times per second to refresh
		
		public void stop() {
			stop = true;
		}
		
		public void run() {
			while (!stop) {
				draw();
				try {
					Thread.sleep(1000/60);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
		
		private void draw() {
			
		}
	}

	class Ripple extends Effect {
		double currentWidth;
		final byte x;
		final byte y;
		byte intensity;

		public Ripple(byte x, byte y, byte intensity) {
			this.x = x;
			this.y = y;
			this.intensity = intensity;
			new Thread(this).start();
		}

		@Override
		public void run() {
			Colour colour;
			boolean drawnSomething = true;

			while (drawnSomething) {

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

				drawnSomething = false;

				for (double i = 0; i < 360; i++) {
					//int x = x + (int) Math.round(currentWidth * Math.cos(i));
					//int y = y + (int) Math.round(currentWidth * Math.sin(i));

					if (x >= 0 && x <= 8 && y >= 0 && y <= 19) {
						setLED(x, y, colour);
						drawnSomething = true;
					}

				}

				currentWidth += 0.1 + (currentWidth / 10);
				try {
					Thread.sleep(1000/60);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}

	}



void CirclePulsate (int x, int y)
{
for ( int i= 0 ; i<= 15 ; i++ ) {
/*matrix. Clear ( ) ;
matrix. DrawEllipse ( AS1107:: Maxx / x , y ,i,i ) ;
matrix. Update ( ) ;
delay ( d ) ;*/
}
}

	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)));
				}
			}
		}
	}

	public void clearLEDGrid() {
		Arrays.fill(leds, Boolean.FALSE);
	}

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

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

	public void run() {

	}

}