pub struct Accelerometer;
Expand description

Wrapped Accelerometer API.

Implementations§

source§

impl Accelerometer

source

pub fn enable() -> Option<()>

Enable accelerometer only. By default, the accelerometer is disabled to save (a small amount of) power. To use a peripheral, it must first be enabled via this function. Accelerometer data is not available until the next update cycle after it’s enabled.

Uses setPeripheralsEnabled.

Examples found in repository?
examples/accelerometer.rs (line 96)
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
	fn event(&mut self, event: PDSystemEvent) -> Option<()> {
		match event {
			// initial setup
			PDSystemEvent::kEventInit => {
				unsafe { (*(*sys::API).display).setRefreshRate?(20.0) }
				// turn on the accelerometer
				controls::peripherals::Accelerometer::enable()?;
			},
			PDSystemEvent::kEventTerminate => {
				// turn off the accelerometer
				controls::peripherals::Accelerometer::disable()?;
			},
			_ => {},
		}
		Some(())
	}
source

pub fn disable() -> Option<()>

Uses setPeripheralsEnabled.

⚠️ Disables all peripherals. Currently it doesn’t matter because there’s just one peripheral - accelerometer.

Examples found in repository?
examples/accelerometer.rs (line 100)
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
	fn event(&mut self, event: PDSystemEvent) -> Option<()> {
		match event {
			// initial setup
			PDSystemEvent::kEventInit => {
				unsafe { (*(*sys::API).display).setRefreshRate?(20.0) }
				// turn on the accelerometer
				controls::peripherals::Accelerometer::enable()?;
			},
			PDSystemEvent::kEventTerminate => {
				// turn off the accelerometer
				controls::peripherals::Accelerometer::disable()?;
			},
			_ => {},
		}
		Some(())
	}
source

pub fn get() -> Option<(c_float, c_float, c_float)>

Uses getAccelerometer.

Returns (x, y, z) accelerometer data.

Examples found in repository?
examples/accelerometer.rs (line 41)
39
40
41
42
43
44
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
	fn update(&mut self) -> Option<()> {
		// get accelerometer data
		let (x, y, z) = controls::peripherals::Accelerometer::get()?;

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

			// render state to string
			let text = format!("[{x:.2},{y:.2},{z:.2}]");
			let c_text = CString::new(text.as_str()).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,
			);

			// update label position
			self.pos.x = (LCD_COLUMNS as f32 * x) as _;
			self.pos.y = (LCD_ROWS as f32 * y) as _;

			// 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( outx: &mut c_float, outy: &mut c_float, outz: &mut c_float ) -> Option<()>

Gets accelerometer data directly to x, y and z.

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.