Struct CanvasLayout

Source
pub struct CanvasLayout<'a> { /* private fields */ }
Expand description

Imagine this - you have multiple launchpads, you line them up, and now you use the Launchpads as if they were a single device?! You can do that, with CanvasLayout.

Create a layout, add Canvases to it at the position where they appear on your table, and you’re ready to rock!

Example:

let mut canvas_layout = CanvasLayout::new(|msg| println!("Got a message: {:?}", msg));

// Assuming you have a Launchpad MK2 and a Launchpad S lying next to it:
canvas_layout.add_by_guess::<launchy::mk2::Canvas>(0, 0)?;
canvas_layout.add_by_guess::<launchy::s::Canvas>(9, 0)?;

// Light the entire canvas layout red - i.e. both Launchpads will be red
for pad in canvas_layout.iter() {
    canvas_layout[pad] = launchy::Color::RED;
}
canvas_layout.flush()?;

Implementations§

Source§

impl<'a> CanvasLayout<'a>

Source

pub fn new(callback: impl Fn(CanvasMessage) + Send + Sync + 'static) -> Self

Create a new CanvasLayout that sends messages to the provided callback. The callback must implement Fn because it may be called from multiple devices concurrently.

Source

pub fn new_polling() -> (Self, CanvasLayoutPoller)

Create a new CanvasLayout, plus an input handler object that you can use to poll messages.

Source

pub fn light_threshold(&self) -> f32

Source

pub fn set_light_threshold(&mut self, value: f32)

Source

pub fn add<C: 'a + Canvas, F, E>( &mut self, x_offset: u32, y_offset: u32, rotation: Rotation, creator: F, ) -> Result<(), E>
where F: FnOnce(Box<dyn Fn(CanvasMessage) + Send + Sync + 'static>) -> Result<C, E>,

Add a new device to this canvas layout, at the specified x and y coordinate.

The usage of this method is a bit awkward out of necessity. You need to provide a closure which, when called with a message callback, is expected to return a Canvas that is set up to deliver messsages to the provided message callback.

The Result which the closure returns will be propagated.

Example:

canvas_layout.add(0, 0, Rotation::None, |callback| launchy::mk2::Canvas::guess(callback))?;

// or even nested layouts:
canvas_layout.add(0, 0, Rotation::None, |callback| {
    let mut canvas_layout = CanvasLayout::new(callback);
    canvas_layout.add(0, 0, Rotation::None, |callback| launchy::mk2::Canvas::guess(callback))?;
    Ok::<_, launchy::MidiError>(canvas_layout)
})?;

If you want an easier way to add simple devices, see add_by_guess.

Source

pub fn add_by_guess<E: 'a + DeviceCanvasTrait>( &mut self, x: u32, y: u32, ) -> Result<(), MidiError>

Add a new device to this canvas, at the specified x and y coordinates. The MIDI connections used for communication with the underlying hardware are determined by guessing based on the device name.

Specifiy the type of device using a generic Canvas type parameter.

Example

// Assuming a Launchpad MK2 and a Launchpad S next to it:
canvas_layout.add_by_guess::<launchy::mk2::Canvas>(0, 0)?;
canvas_layout.add_by_guess::<launchy::s::Canvas>(9, 0)?;
Source

pub fn add_by_guess_rotated<E: 'a + DeviceCanvasTrait>( &mut self, x: u32, y: u32, rotation: Rotation, ) -> Result<(), MidiError>

Like add_by_guess, but with a parameter for the rotation of the Launchpad.

Trait Implementations§

Source§

impl Canvas for CanvasLayout<'_>

Source§

fn lowest_visible_brightness(&self) -> f32

The lowest visible brightness on this canvas. Used to calibrate brightness across Launchpads; users of the library probably don’t need to worry about this
Source§

fn bounding_box(&self) -> (u32, u32)

The width and height of the smallest rectangle that still fully encapsulates the shape of this canvas Read more
Source§

fn low_level_get_pending(&self, x: u32, y: u32) -> Option<&Color>

Returns a reference to the in-buffer/unflushed color at the given position
Source§

fn low_level_get_pending_mut(&mut self, x: u32, y: u32) -> Option<&mut Color>

Returns a mutable reference to the in-buffer/unflushed color at the given position
Source§

fn low_level_get(&self, x: u32, y: u32) -> Option<&Color>

Returns a reference to the currently displayed color at the given position
Source§

fn flush(&mut self) -> Result<(), MidiError>

Flush the accumulated changes to the underlying device Read more
Source§

fn get(&self, pad: Pad) -> Option<Color>

Returns the currently displayed color at the given position, or None if out of bounds Read more
Source§

fn get_pending(&self, pad: Pad) -> Option<Color>

Returns the buffered/unflushed color at the given position, or None if out of bounds Read more
Source§

fn set(&mut self, pad: Pad, color: Color) -> Option<()>

Sets the color at the given position. Returns None if out of bounds Read more
Source§

fn iter(&self) -> CanvasIterator

An iterator over the buttons of a given Canvas. Create an iterator by calling .iter() on a Canvas. Read more
Source§

fn toggle(&mut self, pad: Pad, color: Color) -> Option<()>

Toggles the button at the specified coordinate with the given color. Read more
Source§

fn clear(&mut self)
where Self: Sized,

Clear the entire canvas by setting all buttons to black. Read more
Source§

fn into_padded(self) -> PaddingCanvas<Self>
where Self: Sized,

See PaddingCanvas for more details
Source§

fn is_valid(&self, pad: Pad) -> bool

Source§

impl<'a> Index<Pad> for CanvasLayout<'a>

Source§

type Output = Color

The returned type after indexing.
Source§

fn index(&self, pad: Pad) -> &Color

Performs the indexing (container[index]) operation. Read more
Source§

impl<'a> IndexMut<Pad> for CanvasLayout<'a>

Source§

fn index_mut(&mut self, pad: Pad) -> &mut Color

Performs the mutable indexing (container[index]) operation. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for CanvasLayout<'a>

§

impl<'a> !RefUnwindSafe for CanvasLayout<'a>

§

impl<'a> !Send for CanvasLayout<'a>

§

impl<'a> !Sync for CanvasLayout<'a>

§

impl<'a> Unpin for CanvasLayout<'a>

§

impl<'a> !UnwindSafe for CanvasLayout<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.