Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add very very basic support for launchpad1 #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions src/launchpad/input.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
pub use crate::protocols::query::*;

use super::Button;

#[derive(Debug, Eq, PartialEq, Hash, Clone)]
/// A Launchpad MK2 input message
pub enum Message {
/// A button was pressed
Press { button: Button },
/// A button was released
Release { button: Button },
/// Emitted after a text scroll was initiated
TextEndedOrLooped,
/// The response to a [device inquiry request](super::Output::request_device_inquiry)
DeviceInquiry(DeviceInquiry),
/// The response to a [version inquiry request](super::Output::request_version_inquiry)
VersionInquiry(VersionInquiry),
/// Emitted when a fader was changed by the user, in [fader
/// mode](super::Output::enter_fader_mode)
FaderChange { index: u8, value: u8 },
}

/// The Launchpad MK2 input connection creator.
pub struct Input;

fn decode_grid_button(btn: u8) -> Button {
let y = btn / 16;
let x = btn - y * 16;
Button::GridButton { x, y }
}

impl crate::InputDevice for Input {
const MIDI_DEVICE_KEYWORD: &'static str = "Launchpad MIDI";
const MIDI_CONNECTION_NAME: &'static str = "Launchy Lauchpad Input";
type Message = Message;

fn decode_message(_timestamp: u64, data: &[u8]) -> Message {

if let Some(device_inquiry) = parse_device_query(data) {
return Message::DeviceInquiry(device_inquiry);
}

if let Some(version_inquiry) = parse_version_query(data) {
return Message::VersionInquiry(version_inquiry);
}

println!("btn {:?}", data[1]);

match data {
&[144, button, velocity] => {
let button = decode_grid_button(button);

match velocity {
0 => Message::Release { button },
127 => Message::Press { button },
other => panic!("Unexpected grid note-on velocity {}", other),
}
}
// Controller change
&[176, number @ 104..=111, velocity] => {
let button = Button::ControlButton {
index: number - 104,
};

match velocity {
0 => Message::Release { button },
127 => Message::Press { button },
other => panic!("Unexpected control note-on velocity {}", other),
}
}
/*
// Fader change
&[0xB0, number @ 21..=28, value] => Message::FaderChange {
index: number - 21,
value,
},
&[240, 0, 32, 41, 2, 24, 21, 247] => Message::TextEndedOrLooped,
*/
other => panic!("Unexpected midi message: {:?}", other),
}
}
}
68 changes: 68 additions & 0 deletions src/launchpad/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*!
# Launchpad MK2 low-level API

![Picture](https://imgur.com/PXeHwre.png)
*/

mod input;
pub use input::*;

mod output;
pub use output::*;

pub use crate::protocols::Button80 as Button;

#[doc(hidden)]
pub struct Spec;

impl crate::DeviceSpec for Spec {
const BOUNDING_BOX_WIDTH: u32 = 9;
const BOUNDING_BOX_HEIGHT: u32 = 9;
const COLOR_PRECISION: u16 = 64;

type Input = Input;
type Output = Output;

fn is_valid(x: u32, y: u32) -> bool {
if x > 8 || y > 8 {
return false;
}
if x == 8 && y == 0 {
return false;
}
true
}

fn flush(
canvas: &mut crate::DeviceCanvas<Self>,
changes: &[(u32, u32, (u8, u8, u8))],
) -> Result<(), crate::MidiError> {
let changes = changes.iter().map(|&(x, y, (r, g, b))| {
let color = RgbColor::new(r, g, b);

let button = Button::from_abs(x as u8, y as u8);

(button, color)
});
canvas.output.light_multiple_rgb(changes)
}

fn convert_message(msg: Message) -> Option<crate::CanvasMessage> {
match msg {
Message::Press { button } => Some(crate::CanvasMessage::Press {
x: button.abs_x() as u32,
y: button.abs_y() as u32,
}),
Message::Release { button } => Some(crate::CanvasMessage::Release {
x: button.abs_x() as u32,
y: button.abs_y() as u32,
}),
Message::TextEndedOrLooped
| Message::DeviceInquiry(_)
| Message::VersionInquiry(_)
| Message::FaderChange { .. } => None,
}
}
}

pub type Canvas<'a> = crate::DeviceCanvas<Spec>;
Loading