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
/*!
# 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 }
		return 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);

			return (button, color);
		});
		return 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,
			}),
			_ => None,
		}
	}
}

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