use core::ffi::c_float;
use sys::ffi::PDButtons;
use sys::ffi::PDPeripherals;
use sys::api;
macro_rules! sysfn {
($name:ident) => {{ unsafe { (*($crate::sys::api()?.system)).$name?() } }};
($name:ident, $($arg:tt)*) => {{ unsafe { (*($crate::sys::api()?.system)).$name?($($arg)*) } }};
}
pub struct Peripherals;
impl Peripherals {
#![allow(non_snake_case)]
pub fn enable(value: PDPeripherals) -> Option<()> {
unsafe { (*(api()?.system)).setPeripheralsEnabled?(value) }.into()
}
pub fn enable_accelerometer() -> Option<()> {
unsafe { (*(api()?.system)).setPeripheralsEnabled?(Self::Accelerometer()) }.into()
}
pub fn enable_all() -> Option<()> { unsafe { (*(api()?.system)).setPeripheralsEnabled?(Self::All()) }.into() }
pub fn disable_all() -> Option<()> {
unsafe { (*(api()?.system)).setPeripheralsEnabled?(Self::None()) }.into()
}
pub const fn None() -> PDPeripherals { PDPeripherals::kNone }
pub const fn Accelerometer() -> PDPeripherals { PDPeripherals::kAccelerometer }
pub const fn All() -> PDPeripherals { PDPeripherals::kAllPeripherals }
}
pub struct Accelerometer;
impl Accelerometer {
pub fn enable() -> Option<()> {
unsafe { (*(api()?.system)).setPeripheralsEnabled?(PDPeripherals::kAccelerometer) }.into()
}
pub fn disable() -> Option<()> {
unsafe { (*(api()?.system)).setPeripheralsEnabled?(PDPeripherals::kNone) }.into()
}
pub fn get() -> Option<(c_float, c_float, c_float)> {
let mut outx: c_float = 0.0;
let mut outy: c_float = 0.0;
let mut outz: c_float = 0.0;
unsafe { (*(api()?.system)).getAccelerometer?(&mut outx, &mut outy, &mut outz) }
Some((outx, outy, outz))
}
pub fn get_to(outx: &mut c_float, outy: &mut c_float, outz: &mut c_float) -> Option<()> {
unsafe { (*(api()?.system)).getAccelerometer?(outx, outy, outz) }.into()
}
}
pub struct Buttons {
pub current: PDButtons,
pub pushed: PDButtons,
pub released: PDButtons,
}
impl Buttons {
pub fn get() -> Option<Self> {
let mut current = PDButtons(0);
let mut pushed = PDButtons(0);
let mut released = PDButtons(0);
sysfn!(getButtonState, &mut current, &mut pushed, &mut released);
Self { current,
pushed,
released }.into()
}
pub fn get_to(current: &mut PDButtons, pushed: &mut PDButtons, released: &mut PDButtons) -> Option<()> {
sysfn!(getButtonState, current, pushed, released).into()
}
pub fn get_current() -> Option<PDButtons> {
use core::ptr::null_mut;
let mut current = PDButtons(0);
sysfn!(getButtonState, &mut current, null_mut(), null_mut());
current.into()
}
pub fn get_pushed() -> Option<PDButtons> {
use core::ptr::null_mut;
let mut pushed = PDButtons(0);
sysfn!(getButtonState, null_mut(), &mut pushed, null_mut());
pushed.into()
}
pub fn get_released() -> Option<PDButtons> {
use core::ptr::null_mut;
let mut released = PDButtons(0);
sysfn!(getButtonState, null_mut(), null_mut(), &mut released);
released.into()
}
}
impl core::fmt::Debug for Buttons {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
use crate::buttons::PDButtonsFmt;
f.debug_struct("Buttons")
.field("current", &self.current.display())
.field("pushed", &self.pushed.display())
.field("released", &self.released.display())
.finish()
}
}
pub struct Crank;
impl Crank {
pub fn docked() -> Option<bool> { Some(sysfn!(isCrankDocked) == 1) }
pub fn angle() -> Option<c_float> { Some(sysfn!(getCrankAngle)).into() }
pub fn change() -> Option<c_float> { Some(sysfn!(getCrankChange)).into() }
pub fn disable_sounds(disable: bool) -> Option<bool> {
Some(sysfn!(setCrankSoundsDisabled, disable as _) == 1)
}
}