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
use std::ops::Deref;
use std::slice;
use libc::c_int;
use {ffi, Led, Strip};
use super::ChannelRef;

/// A mutable reference to a `Channel`.
#[derive(Debug)]
pub struct Ref<'a>(*mut ffi::ws2811_channel_t, ChannelRef<'a>);

impl<'a> Ref<'a> {
	#[doc(hidden)]
	pub unsafe fn wrap(ptr: *mut ffi::ws2811_channel_t) -> Self {
		Ref(ptr, ChannelRef::wrap(ptr as *const _))
	}

	/// Change the GPIO pin.
	pub fn set_pin(&mut self, value: i32) {
		unsafe {
			(*self.0).gpionum = value as c_int;
		}
	}

	/// Change the inversion.
	pub fn set_invert(&mut self, value: bool) {
		unsafe {
			(*self.0).invert = if value { 1 } else { 0 };
		}
	}

	/// Change the brightness.
	pub fn set_brightness(&mut self, value: i32) {
		unsafe {
			(*self.0).brightness = value as c_int;
		}
	}

	/// Change the strip type.
	pub fn set_strip(&mut self, value: Strip) {
		unsafe {
			(*self.0).strip_type = value.into();
		}
	}

	/// Get the leds mutably.
	pub fn leds_mut(&mut self) -> &mut [Led] {
		unsafe {
			slice::from_raw_parts_mut((*self.0).leds, (*self.0).count as usize)
		}
	}
}

impl<'a> Deref for Ref<'a> {
	type Target = ChannelRef<'a>;

	fn deref(&self) -> &Self::Target {
		&self.1
	}
}