pub struct Buttons {
    pub current: PDButtons,
    pub pushed: PDButtons,
    pub released: PDButtons,
}
Expand description

Wrapped Buttons API.

Represents buttons state.

  • current indicates which buttons are currently down.
  • pushed and released reflects which buttons were pushed or released over the previous update cycle.

Note: at the nominal frame rate of 50 ms, fast button presses can be missed if you just poll the instantaneous state.

Fields§

§current: PDButtons

Indicating which buttons are currently down.

§pushed: PDButtons

Reflect which buttons were pushed over the previous update cycle. See struct doc.

§released: PDButtons

Reflect which buttons were released over the previous update cycle. See struct doc.

Implementations§

source§

impl Buttons

source

pub fn get() -> Option<Self>

Examples found in repository?
examples/buttons.rs (line 47)
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
	fn update(&mut self) -> Option<()> {
		// get buttons state
		let buttons = controls::peripherals::Buttons::get()?;

		unsafe {
			let graphics = (*sys::API).graphics;
			(*graphics).clear?(LCDSolidColor::kColorWhite as LCDColor);

			// render buttons state to string
			let text: Cow<str> = if buttons.current.is_empty() {
				"[Press any button]".into()
			} else {
				format!("{:?}", buttons.current.iter().singles().collect::<Vec<_>>()).into()
			};

			let c_text = CString::new(text.as_ref()).ok()?;
			// get width (screen-size) of text
			let text_width = (*graphics).getTextWidth?(
			                                           core::ptr::null_mut(),
			                                           c_text.as_ptr() as *const _,
			                                           text.len(),
			                                           PDStringEncoding::kUTF8Encoding,
			                                           0,
			);
			// render text
			(*graphics).drawText?(
			                      c_text.as_ptr() as *const _,
			                      text.len(),
			                      PDStringEncoding::kUTF8Encoding,
			                      self.pos.x,
			                      self.pos.y,
			);

			// move the label
			const SPEED: i32 = 4;
			if buttons.current.right() {
				self.pos.x += SPEED;
			}
			if buttons.current.left() {
				self.pos.x -= SPEED;
			}
			if buttons.current.up() {
				self.pos.y -= SPEED;
			}
			if buttons.current.down() {
				self.pos.y += SPEED;
			}

			// check screen boundaries
			if self.pos.x < 0 {
				self.pos.x = 0
			} else if self.pos.x > LCD_COLUMNS as i32 - text_width {
				self.pos.x = LCD_COLUMNS as i32 - text_width
			}
			if self.pos.y < 0 {
				self.pos.y = 0
			} else if self.pos.y > LCD_ROWS as i32 - TEXT_HEIGHT as i32 {
				self.pos.y = LCD_ROWS as i32 - TEXT_HEIGHT as i32
			}

			(*(*sys::API).system).drawFPS?(0, 0);
			Some(())
		}
	}
source

pub fn get_to( current: &mut PDButtons, pushed: &mut PDButtons, released: &mut PDButtons ) -> Option<()>

source

pub fn get_current() -> Option<PDButtons>

Uses getButtonState.

Requests & returns only current part of state, see Self::current

source

pub fn get_pushed() -> Option<PDButtons>

Uses getButtonState.

Requests & returns only current part of state, see Self::pushed

source

pub fn get_released() -> Option<PDButtons>

Uses getButtonState.

Requests & returns only current part of state, see Self::released

Trait Implementations§

source§

impl Debug for Buttons

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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 Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.