use core::ffi::c_float;
use sys::ffi::PDButtons;
use sys::ffi::PDPeripherals;
use crate::api;
#[derive(Debug, Clone, Copy)]
pub struct Peripherals<Api = api::Default>(Api);
impl Peripherals<api::Default> {
#[allow(non_snake_case)]
pub fn Default() -> Self { Self(Default::default()) }
}
impl Peripherals<api::Cache> {
#[allow(non_snake_case)]
pub fn Cached() -> Self { Self(Default::default()) }
}
impl<Api: Default + api::Api> Default for Peripherals<Api> {
fn default() -> Self { Self(Default::default()) }
}
impl<Api: Default + api::Api> Peripherals<Api> {
pub fn new() -> Self { Self(Default::default()) }
}
impl<Api: api::Api> Peripherals<Api> {
pub const fn new_with(api: Api) -> Self { Self(api) }
}
impl<Api: api::Api> Peripherals<Api> where Api: Copy {
pub const fn accelerometer(&self) -> Accelerometer<Api> { Accelerometer(self.0) }
pub const fn buttons(&self) -> Buttons<Api> { Buttons(self.0) }
pub const fn crank(&self) -> Crank<Api> { Crank(self.0) }
}
impl<Api: api::Api> Peripherals<Api> {
#[doc(alias = "sys::ffi::playdate_sys::setPeripheralsEnabled")]
pub fn enable(&self, value: PDPeripherals) {
let f = self.0.set_peripherals_enabled();
unsafe { f(value) }
}
#[doc(alias = "sys::ffi::playdate_sys::setPeripheralsEnabled")]
#[inline(always)]
pub fn enable_accelerometer(&self) { self.enable(Peripherals::Accelerometer) }
#[doc(alias = "sys::ffi::playdate_sys::setPeripheralsEnabled")]
#[inline(always)]
pub fn enable_all(&self) { self.enable(Peripherals::All) }
#[doc(alias = "sys::ffi::playdate_sys::setPeripheralsEnabled")]
#[inline(always)]
pub fn disable_all(&self) { self.enable(Peripherals::None) }
}
impl Peripherals<api::Default> {
#![allow(non_upper_case_globals)]
pub const None: PDPeripherals = PDPeripherals::kNone;
pub const Accelerometer: PDPeripherals = PDPeripherals::kAccelerometer;
pub const All: PDPeripherals = PDPeripherals::kAllPeripherals;
}
pub trait SystemExt<Api: api::Api + Copy> {
fn peripherals(&self) -> Peripherals<Api>;
fn accelerometer(&self) -> Accelerometer<Api>;
fn buttons(&self) -> Buttons<Api>;
fn crank(&self) -> Crank<Api>;
}
impl<Api: system::api::Api + api::Api + Copy> SystemExt<Api> for system::System<Api> {
fn peripherals(&self) -> Peripherals<Api> { Peripherals::new_with(self.inner()) }
fn accelerometer(&self) -> Accelerometer<Api> { Accelerometer::new_with(self.inner()) }
fn buttons(&self) -> Buttons<Api> { Buttons::new_with(self.inner()) }
fn crank(&self) -> Crank<Api> { Crank::new_with(self.inner()) }
}
#[derive(Debug, Clone, Copy)]
pub struct Accelerometer<Api = api::Default>(Api);
impl Accelerometer<api::Default> {
#[allow(non_snake_case)]
pub fn Default() -> Self { Self(Default::default()) }
}
impl Accelerometer<api::Cache> {
#[allow(non_snake_case)]
pub fn Cached() -> Self { Self(Default::default()) }
}
impl<Api: Default + api::Api> Default for Accelerometer<Api> {
fn default() -> Self { Self(Default::default()) }
}
impl<Api: Default + api::Api> Accelerometer<Api> {
pub fn new() -> Self { Self(Default::default()) }
}
impl<Api: api::Api> Accelerometer<Api> {
pub const fn new_with(api: Api) -> Self { Self(api) }
}
impl<Api: api::Api> Accelerometer<Api> {
#[doc(alias = "sys::ffi::playdate_sys::setPeripheralsEnabled")]
pub fn enable(&self) {
let f = self.0.set_peripherals_enabled();
unsafe { f(Peripherals::Accelerometer) }
}
#[doc(alias = "sys::ffi::playdate_sys::setPeripheralsEnabled")]
pub fn disable(&self) {
let f = self.0.set_peripherals_enabled();
unsafe { f(Peripherals::None) }
}
#[doc(alias = "sys::ffi::playdate_sys::getAccelerometer")]
pub fn get(&self) -> (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;
let f = self.0.get_accelerometer();
unsafe { f(&mut outx, &mut outy, &mut outz) };
(outx, outy, outz)
}
#[doc(alias = "sys::ffi::playdate_sys::getAccelerometer")]
pub fn get_to(&self, outx: &mut c_float, outy: &mut c_float, outz: &mut c_float) {
let f = self.0.get_accelerometer();
unsafe { f(outx, outy, outz) }
}
}
#[derive(Debug, Clone, Copy)]
pub struct Buttons<Api = api::Default>(Api);
impl Buttons<api::Default> {
#[allow(non_snake_case)]
pub fn Default() -> Self { Self(Default::default()) }
}
impl Buttons<api::Cache> {
#[allow(non_snake_case)]
pub fn Cached() -> Self { Self(Default::default()) }
}
impl<Api: Default + api::Api> Default for Buttons<Api> {
fn default() -> Self { Self(Default::default()) }
}
impl<Api: Default + api::Api> Buttons<Api> {
pub fn new() -> Self { Self(Default::default()) }
}
impl<Api: api::Api> Buttons<Api> {
pub const fn new_with(api: Api) -> Self { Self(api) }
}
impl<Api: api::Api> Buttons<Api> {
#[doc(alias = "sys::ffi::playdate_sys::getButtonState")]
pub fn get(&self) -> State {
let mut current = PDButtons(0);
let mut pushed = PDButtons(0);
let mut released = PDButtons(0);
let f = self.0.get_button_state();
unsafe { f(&mut current, &mut pushed, &mut released) }
State { current,
pushed,
released }
}
#[doc(alias = "sys::ffi::playdate_sys::getButtonState")]
pub fn get_to(&self, state: &mut State) {
let f = self.0.get_button_state();
unsafe { f(&mut state.current, &mut state.pushed, &mut state.released) }
}
#[doc(alias = "sys::ffi::playdate_sys::getButtonState")]
pub fn get_to_raw(&self, current: &mut PDButtons, pushed: &mut PDButtons, released: &mut PDButtons) {
let f = self.0.get_button_state();
unsafe { f(current, pushed, released) }
}
#[doc(alias = "sys::ffi::playdate_sys::getButtonState")]
pub fn current(&self) -> PDButtons {
use core::ptr::null_mut;
let mut current = PDButtons(0);
let f = self.0.get_button_state();
unsafe { f(&mut current, null_mut(), null_mut()) }
current
}
#[doc(alias = "sys::ffi::playdate_sys::getButtonState")]
pub fn pushed(&self) -> PDButtons {
use core::ptr::null_mut;
let mut pushed = PDButtons(0);
let f = self.0.get_button_state();
unsafe { f(null_mut(), &mut pushed, null_mut()) }
pushed
}
#[doc(alias = "sys::ffi::playdate_sys::getButtonState")]
pub fn released(&self) -> PDButtons {
use core::ptr::null_mut;
let mut released = PDButtons(0);
let f = self.0.get_button_state();
unsafe { f(null_mut(), null_mut(), &mut released) }
released
}
}
pub struct State {
pub current: PDButtons,
pub pushed: PDButtons,
pub released: PDButtons,
}
impl core::fmt::Debug for State {
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()
}
}
#[derive(Debug, Clone, Copy)]
pub struct Crank<Api = api::Default>(Api);
impl Crank<api::Default> {
#[allow(non_snake_case)]
pub fn Default() -> Self { Self(Default::default()) }
}
impl Crank<api::Cache> {
#[allow(non_snake_case)]
pub fn Cached() -> Self { Self(Default::default()) }
}
impl<Api: Default + api::Api> Default for Crank<Api> {
fn default() -> Self { Self(Default::default()) }
}
impl<Api: Default + api::Api> Crank<Api> {
pub fn new() -> Self { Self(Default::default()) }
}
impl<Api: api::Api> Crank<Api> {
pub const fn new_with(api: Api) -> Self { Self(api) }
}
impl<Api: api::Api> Crank<Api> {
#[doc(alias = "sys::ffi::playdate_sys::isCrankDocked")]
pub fn docked(&self) -> bool {
let f = self.0.is_crank_docked();
unsafe { f() == 1 }
}
#[doc(alias = "sys::ffi::playdate_sys::getCrankAngle")]
pub fn angle(&self) -> c_float {
let f = self.0.get_crank_angle();
unsafe { f() }
}
#[doc(alias = "sys::ffi::playdate_sys::getCrankChange")]
pub fn change(&self) -> c_float {
let f = self.0.get_crank_change();
unsafe { f() }
}
#[doc(alias = "sys::ffi::playdate_sys::setCrankSoundsDisabled")]
pub fn disable_sounds(&self, disable: bool) -> bool {
let f = self.0.set_crank_sounds_disabled();
unsafe { f(disable as _) == 1 }
}
}