Struct playdate_controls::peripherals::Accelerometer
source · pub struct Accelerometer;Expand description
Wrapped Accelerometer API.
Implementations§
source§impl Accelerometer
impl Accelerometer
sourcepub fn enable() -> Option<()>
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?
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(())
}sourcepub fn disable() -> Option<()>
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?
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(())
}sourcepub fn get() -> Option<(c_float, c_float, c_float)>
pub fn get() -> Option<(c_float, c_float, c_float)>
Uses getAccelerometer.
Returns (x, y, z) accelerometer data.
Examples found in repository?
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(())
}
}Auto Trait Implementations§
impl RefUnwindSafe for Accelerometer
impl Send for Accelerometer
impl Sync for Accelerometer
impl Unpin for Accelerometer
impl UnwindSafe for Accelerometer
Blanket Implementations§
§impl<T> Any for Twhere
T: 'static + ?Sized,
impl<T> Any for Twhere T: 'static + ?Sized,
§impl<T> Borrow<T> for Twhere
T: ?Sized,
impl<T> Borrow<T> for Twhere T: ?Sized,
§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere T: ?Sized,
§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T, U> Into<U> for Twhere
U: From<T>,
impl<T, U> Into<U> for Twhere U: From<T>,
§impl<T, U> TryFrom<U> for Twhere
U: Into<T>,
impl<T, U> TryFrom<U> for Twhere U: Into<T>,
Layout§
Note: Most layout information is completely unstable and may even differ between compilations. The only exception is types with certain repr(...) attributes. Please see the Rust Reference’s “Type Layout” chapter for details on type layout guarantees.
Size:0 bytes