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,
}
/// Represents a pad on the Launchpad
/// Provides methods to convert to and from a note byte
#[derive(Copy, Clone, PartialEq, Debug)]
pub struct Pad {
    pub x: u8,
    pub y: u8,
}

// Pad impl
impl Pad {
    /// Returns the MIDI note corresponding to the pad.
    /// ```
    /// # use lp_tetris::Pad;
    /// assert_eq!(Pad { x: 0, y: 0 }.note(), 11);
    ///
    /// assert_eq!(Pad { x: 7, y: 0 }.note(), 18);
    ///
    /// assert_eq!(Pad { x: 0, y: 7 }.note(), 81);
    ///
    /// assert_eq!(Pad { x: 7, y: 7 }.note(), 88);
    /// ```
    /// This is the inverse of ::from_note
    /// ```
    /// # use lp_tetris::Pad;
    /// let pad = Pad { x: 3, y: 5 };
    /// assert_eq!(pad, Pad::from_note(pad.note()));
    /// ```
    pub fn note(&self) -> u8 {
        10 * (self.y + 1) + (self.x + 1)
    }
    /// Returns a Pad corresponding to the given MIDI note.
    /// ```
    /// # use lp_tetris::Pad;
    /// assert_eq!(Pad { x: 0, y: 0 }, Pad::from_note(11));
    ///
    /// assert_eq!(Pad { x: 7, y: 0 }, Pad::from_note(18));
    ///
    /// assert_eq!(Pad { x: 0, y: 7 }, Pad::from_note(81));
    ///
    /// assert_eq!(Pad { x: 7, y: 7 }, Pad::from_note(88));
    /// ```
    /// This is the inverse of .note
    /// ```
    /// # use lp_tetris::Pad;
    /// let note = 0x22;
    /// assert_eq!(note, Pad::from_note(note).note());
    /// ```
    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 }
    }
}
// Core defs
impl Launchpad {
    /// Finds a launchpad and returns it.
    /// Handles finding the port as well as connecting to it.
    /// Panics if a launchpad is not found.
    pub fn new() -> Launchpad {
        // Find output port
        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 (c_tx, c_rx) = mpsc::channel();
        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,
        }
    }
    /// Closes the underlying midi connection to the launchpad
    pub fn close(self) {
        self.conn_out.close();
    }
    // pub fn poll_inputs(&mut self) -> ControlEvent {
    //     match self.input_buffer.try_recv() {
    //         Ok(event) => event,
    //         Err(_) => ControlEvent::None
    //     }
    // }
}
// Render defs
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);
    }
}
// Input defs
impl Launchpad {
    /// Get next ControlEvent
    pub fn poll_input(&self) -> Option<ControlEvent> {
        self.events_rx.try_recv().ok()
    }
}
/// Given an rdev::EventType, return a ControlEvent.
/// This is the method to modify if you want to change/add input mappings.
///
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::One, State::Pressed) => Some(ControlEvent::SpeedChange(0)),
        // RawEvent::KeyboardEvent(_, KeyId::Two, State::Pressed) => Some(ControlEvent::SpeedChange(1)),
        // RawEvent::KeyboardEvent(_, KeyId::Three, State::Pressed) => Some(ControlEvent::SpeedChange(2)),
        // RawEvent::KeyboardEvent(_, KeyId::Four, State::Pressed) => Some(ControlEvent::SpeedChange(3)),
        // RawEvent::KeyboardEvent(_, KeyId::Five, State::Pressed) => Some(ControlEvent::SpeedChange(4)),
        // RawEvent::KeyboardEvent(_, KeyId::Six, State::Pressed) => Some(ControlEvent::SpeedChange(5)),
        // RawEvent::KeyboardEvent(_, KeyId::Seven, State::Pressed) => Some(ControlEvent::SpeedChange(6)),
        // RawEvent::KeyboardEvent(_, KeyId::Eight, State::Pressed) => Some(ControlEvent::SpeedChange(7)),
        // RawEvent::KeyboardEvent(_, KeyId::Nine, State::Pressed) => Some(ControlEvent::SpeedChange(8)),
        RawEvent::KeyboardEvent(_, KeyId::Backspace, State::Pressed) => {
            Some(ControlEvent::ExitGame)
        }
        _ => None,
    }
}