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
use multiinput::*;
use std::{sync::mpsc, thread};
pub struct Launchpad {
conn_out: midir::MidiOutputConnection,
events_rx: mpsc::Receiver<ControlEvent>,
}
#[derive(Debug, PartialEq)]
pub enum ControlEvent {
RotateRight,
RotateLeft,
MoveRight,
MoveLeft,
MoveUp,
MoveDown,
DropBlock,
SpeedChange(u8),
ExitGame,
}
#[derive(Copy, Clone, PartialEq, Debug)]
pub struct Pad {
pub x: u8,
pub y: u8,
}
impl Pad {
pub fn note(&self) -> u8 {
10 * (self.y + 1) + (self.x + 1)
}
pub fn from_note(note: u8) -> Pad {
let ones = note % 10;
let tens = (note.saturating_sub(ones)) / 10;
let x = ones.saturating_sub(1);
let y = tens.saturating_sub(1);
Pad { x, y }
}
}
impl Launchpad {
pub fn new() -> Launchpad {
let midi_out = midir::MidiOutput::new("Launchpad MK2").unwrap();
let mut out_port: Option<usize> = None;
for i in 0..midi_out.port_count() {
if midi_out.port_name(i).unwrap().contains("Launchpad MK2") {
out_port = Some(i);
}
}
let out_port = out_port.expect("Couldn't find launchpad!");
let conn_out = midi_out
.connect(out_port, "")
.expect("Failed to open connection");
let (events_tx, events_rx) = mpsc::channel();
let mut manager = RawInputManager::new().unwrap();
manager.register_devices(DeviceType::Keyboards);
thread::spawn(move || loop {
if let Some(event) = manager.get_event() {
if let Some(msg) = input_map(event) {
events_tx.send(msg).ok();
}
}
});
Launchpad {
conn_out,
events_rx,
}
}
pub fn close(self) {
self.conn_out.close();
}
}
impl Launchpad {
pub fn send_sysex(&mut self, msg_type: u8, data: &[u8]) {
let mut msg_data = vec![0xF0, 0x00, 0x20, 0x29, 0x02, 0x18, msg_type];
msg_data.extend(data);
msg_data.push(0xF7);
self.conn_out.send(&msg_data).unwrap();
}
pub fn clear(&mut self) {
self.send_sysex(0x0E, &[0]);
}
pub fn send_note(&mut self, note: u8, velocity: u8) {
self.send_sysex(0x0A, &[note, velocity]);
}
pub fn send_matrix(&mut self, matrix: array2d::Array2D<u8>) {
let msg = {
let mut msg = Vec::new();
for y in 0..8 {
for x in 0..8 {
let pad = Pad { x, y };
msg.push(pad.note());
msg.push(matrix[(y as usize, x as usize)]);
}
}
msg
};
self.send_sysex(0x0A, &msg);
}
}
impl Launchpad {
pub fn poll_input(&self) -> Option<ControlEvent> {
self.events_rx.try_recv().ok()
}
}
pub fn input_map(event: RawEvent) -> Option<ControlEvent> {
match event {
RawEvent::KeyboardEvent(_, KeyId::A, State::Pressed) => Some(ControlEvent::RotateLeft),
RawEvent::KeyboardEvent(_, KeyId::D, State::Pressed) => Some(ControlEvent::RotateRight),
RawEvent::KeyboardEvent(_, KeyId::Left, State::Pressed) => Some(ControlEvent::MoveLeft),
RawEvent::KeyboardEvent(_, KeyId::Right, State::Pressed) => Some(ControlEvent::MoveRight),
RawEvent::KeyboardEvent(_, KeyId::Up, State::Pressed) => Some(ControlEvent::MoveUp),
RawEvent::KeyboardEvent(_, KeyId::Down, State::Pressed) => Some(ControlEvent::MoveDown),
RawEvent::KeyboardEvent(_, KeyId::Space, State::Pressed) => Some(ControlEvent::DropBlock),
RawEvent::KeyboardEvent(_, KeyId::Backspace, State::Pressed) => {
Some(ControlEvent::ExitGame)
}
_ => None,
}
}