forked from Tinkerforge/weather-station
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGuitarStation.java
200 lines (173 loc) · 5.29 KB
/
GuitarStation.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
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
import com.tinkerforge.BrickletLCD20x4;
import com.tinkerforge.IPConnection;
import javax.sound.midi.*;
import java.util.*;
class GameHandle extends Thread {
private static final String HOST = "localhost";
private static final int PORT = 4223;
private BrickletLCD20x4 lcd = null;
private IPConnection ipcon = null;
private Synthesizer synthesizer = null;
private MidiChannel[] channels = null;
private static final int ACOUSTIC_GUITAR_NYLON = 24;
private static final int ACOUSTIC_GUITAR_STEEL = 25;
private static final int ELECTRIC_GUITAR_JAZZ = 26;
private static final int ELECTRIC_GUITAR_CLEAN = 27;
private static final int ELECTRIC_GUITAR_MUTED = 28;
private static final int OVERDRIVEN_GUITAR = 29;
private static final int DISTORTION_GUITAR = 30;
private static final int GUITAR_HARMONICS = 31;
private static final int CHOIR = 52;
private static final int TRUMPET = 56;
private static final int OBOE = 68;
private static final int PAN_FLUTE = 75;
private static final int BLOWN_BOTTLE = 76;
private static final int SYNTH_PAD = 88;
private static final int SYNTH_POLY = 90;
// Here you can select other instruments
private static final int INSTRUMENT = SYNTH_PAD;
private static final int MAX_BAR_LENGTH = 15;
private static final double BAR_PROPABILITY = 0.95;
private static final int SPEED = 300;
// Maps a normal UTF-16 encoded string to the LCD charset
static String createLCDString(String utf16)
{
String ks0066u = "";
char c;
for (int i = 0; i < utf16.length(); i++) {
int codePoint = utf16.codePointAt(i);
switch (codePoint) {
case ' ': c = ' '; break;
default:
c = (char)0xff; break; // BLACK SQUARE
}
ks0066u += c;
}
return ks0066u;
}
public GameHandle () {
this.ipcon = new IPConnection();
// Connect to Brickd
System.out.println("Waiting for Brickd...");
while(true) {
try {
ipcon.connect(HOST, PORT);
break;
}
catch(Exception e) {}
}
ipcon.addEnumerateListener(new IPConnection.EnumerateListener() {
public void enumerate(String uid, String connectedUid, char position,
short[] hardwareVersion, short[] firmwareVersion,
int deviceIdentifier, short enumerationType) {
if(enumerationType == IPConnection.ENUMERATION_TYPE_CONNECTED ||
enumerationType == IPConnection.ENUMERATION_TYPE_AVAILABLE) {
if(deviceIdentifier == BrickletLCD20x4.DEVICE_IDENTIFIER) {
System.out.println("Found LCD");
try {
lcd = new BrickletLCD20x4(uid, ipcon);
lcd.clearDisplay();
lcd.backlightOn();
synthesizer = MidiSystem.getSynthesizer();
synthesizer.open();
Instrument[] instr = synthesizer.getDefaultSoundbank().getInstruments();
synthesizer.loadInstrument(instr[INSTRUMENT]);
channels = synthesizer.getChannels();
channels[0].programChange(INSTRUMENT);
lcd.writeLine((short)0, (short)0, createLCDString(" XXX "));
lcd.addButtonPressedListener(new BrickletLCD20x4.ButtonPressedListener() {
public void buttonPressed(short button) {
System.out.println("Pressed: " + button);
channels[0].noteOn(40+10*button, 100);
}
});
lcd.addButtonReleasedListener(new BrickletLCD20x4.ButtonReleasedListener() {
public void buttonReleased(short button) {
System.out.println("Released: " + button);
channels[0].noteOff(40+10*button);
}
});
}
catch(Exception e)
{
System.out.println("LCD Initialization Failed");
}
}
}
}
});
// Try to enumerate
System.out.println("Enummerate...");
while(true) {
try {
ipcon.enumerate();
break;
}
catch(Exception e) {}
}
}
public void run() {
char mask[][] = new char[4][20];
int barlist[] = {0,0,0,0};
System.out.println("Start thread");
// initial drawing empty mask
for(int i=0; i < 4; i++) {
try {
lcd.writeLine((short)i, (short)0, createLCDString(new String(mask[i])));
}
catch (Exception e) {}
}
while(true) {
try {
Thread.sleep(SPEED);
} catch(Exception e) {}
// move mask
for(int i=0; i < 4; i++) {
for(int j=0; j < 19; j++) {
mask[i][19-j] = mask[i][19-j-1];
}
}
// decrement counters
for(int i=0; i < 4; i++) {
if(barlist[i] > 0) {
barlist[i] = barlist[i] - 1;
}
}
// create new bars
for(int i=0; i < 4; i++) {
if(barlist[i] == 0) {
Random rand = new Random();
if(rand.nextDouble() >= BAR_PROPABILITY) {
barlist[i] = (int)(MAX_BAR_LENGTH*rand.nextDouble());
}
}
}
// create first new line based on bars
for(int i=0; i < 4; i++) {
if(barlist[i] > 0) {
mask[i][0] = 'X';
}
else {
mask[i][0] = ' ';
}
}
// print mask
for(int i=0; i < 4; i++) {
try {
lcd.writeLine((short)i, (short)0, createLCDString(new String(mask[i])));
}
catch (Exception e) {}
}
}
}
}
public class GuitarStation {
// Note: To make the example code cleaner we do not handle exceptions. Exceptions you
// might normally want to catch are described in the documentation
public static void main(String args[]) throws Exception {
(new GameHandle()).start();
System.out.println("Press key to exit"); System.in.read();
// Try to disconnect
System.out.println("Disconnect...");
}
}