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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
use pm;
use color::nearest_palette;
pub type Color = u8;
pub struct LaunchpadMk2 {
input_port: pm::InputPort,
output_port: pm::OutputPort,
midi: Option<pm::PortMidi>,
}
#[derive(Debug)]
pub struct ColorLed {
pub color: Color,
pub position: u8,
}
#[derive(Debug)]
pub struct ColorColumn {
pub color: Color,
pub column: u8,
}
#[derive(Debug)]
pub struct ColorRow {
pub color: Color,
pub row: u8,
}
pub const SCROLL_SLOWEST: &'static str = "\u{01}";
pub const SCROLL_SLOWER: &'static str = "\u{02}";
pub const SCROLL_SLOW: &'static str = "\u{03}";
pub const SCROLL_NORMAL: &'static str = "\u{04}";
pub const SCROLL_FAST: &'static str = "\u{05}";
pub const SCROLL_FASTER: &'static str = "\u{06}";
pub const SCROLL_FASTEST: &'static str = "\u{07}";
impl LaunchpadMk2 {
pub fn guess() -> LaunchpadMk2 {
let midi = pm::PortMidi::new().expect("Failed to open PortMidi Instance!");
let mut retval = Self::guess_from(&midi);
retval.midi = Some(midi);
retval
}
pub fn guess_from(midi: &pm::PortMidi) -> LaunchpadMk2 {
let devs = midi.devices().expect("Failed to get Midi Device!");
let mut input: Option<i32> = None;
let mut output: Option<i32> = None;
for d in devs {
if !d.name().contains("Launchpad MK2") {
continue;
}
if input.is_none() && d.is_input() {
input = Some(d.id() as i32);
}
if output.is_none() && d.is_output() {
output = Some(d.id() as i32);
}
if input.is_some() && output.is_some() {
break;
}
}
let input_port = input.expect("No Launchpad Mk2 Input Found!");
let output_port = output.expect("No Launchpad Mk2 Output Found!");
let input_device = midi.device(input_port)
.expect("No Launchpad Input found!");
let output_device = midi.device(output_port)
.expect("No Launchpad Output found!");
let input = midi.input_port(input_device, 1024)
.expect("Failed to open port");
let output = midi.output_port(output_device, 1024)
.expect("Failed to open port");
LaunchpadMk2 {
input_port: input,
output_port: output,
midi: None,
}
}
pub fn light_all(&mut self, color: Color) {
assert_color(color);
self.output_port
.write_sysex(0, &[0xF0, 0x00, 0x20, 0x29, 0x02, 0x18, 0x0E, color, 0xF7])
.expect("Fail");
}
pub fn flash_single(&mut self, led: &ColorLed) {
assert_position(led.position);
assert_color(led.color);
self.output_port.write_message([0x91, led.position, led.color]).expect("Fail");
}
pub fn pulse_single(&mut self, led: &ColorLed) {
self.output_port.write_message([0x92, led.position, led.color]).expect("Fail");
}
pub fn light_led(&mut self, led: &ColorLed) {
self.light_leds(&[led])
}
pub fn light_leds(&mut self, leds: &[&ColorLed]) {
assert!(leds.len() <= 80);
for led in leds {
assert_position(led.position);
assert_color(led.color);
self.output_port
.write_sysex(0,
&[0xF0,
0x00,
0x20,
0x29,
0x02,
0x18,
0x0A,
led.position,
led.color,
0xF7])
.expect("Fail");
}
}
pub fn light_column(&mut self, col: &ColorColumn) {
self.light_columns(&[col])
}
pub fn light_columns(&mut self, cols: &[&ColorColumn]) {
assert!(cols.len() <= 9);
for col in cols {
assert_column(col.column);
assert_color(col.color);
self.output_port
.write_sysex(0,
&[0xF0, 0x00, 0x20, 0x29, 0x02, 0x18, 0x0C, col.column, col.color,
0xF7])
.expect("Fail");
}
}
pub fn light_row(&mut self, row: &ColorRow) {
self.light_rows(&[row])
}
pub fn light_rows(&mut self, rows: &[&ColorRow]) {
assert!(rows.len() <= 9);
for row in rows {
assert_row(row.row);
assert_color(row.color);
self.output_port
.write_sysex(0,
&[0xF0, 0x00, 0x20, 0x29, 0x02, 0x18, 0x0D, row.row, row.color, 0xF7])
.expect("Fail");
}
}
pub fn scroll_text(&mut self, color: Color, doloop: bool, text: &str) {
assert_color(color);
let mut msg: Vec<u8> =
vec![0xF0, 0x00, 0x20, 0x29, 0x02, 0x18, 0x14, color, if doloop { 0x01 } else { 0x00 }];
msg.extend_from_slice(text.as_bytes());
msg.push(0xF7);
self.output_port.write_sysex(0, &msg).expect("Fail");
}
pub fn light_fuzzy_rgb(&mut self, position: u8, red: u8, green: u8, blue: u8) {
self.light_led(&ColorLed {
position: position,
color: nearest_palette(red, green, blue),
})
}
pub fn poll(&self) -> Option<Vec<pm::MidiEvent>> {
self.input_port.poll().expect("Closed Stream");
self.input_port.read_n(1024).expect("Failed to read")
}
}
fn assert_position(pos: u8) {
if !match pos {
11...19 => true,
21...29 => true,
31...39 => true,
41...49 => true,
51...59 => true,
61...69 => true,
71...79 => true,
81...89 => true,
104...111 => true,
_ => false,
} {
panic!("Bad Positon!")
}
}
fn assert_color(clr: u8) {
if clr > 127 {
panic!("Bad Color!");
}
}
fn assert_column(col: u8) {
if col > 8 {
panic!("Bad Column");
}
}
fn assert_row(row: u8) {
if row > 8 {
panic!("Bad Row");
}
}