1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
use core::ffi::c_float;
use sys::ffi::PDButtons;
use sys::ffi::PDPeripherals;
use crate::api;


/// Peripherals
#[derive(Debug, Clone, Copy)]
pub struct Peripherals<Api = api::Default>(Api);

impl Peripherals<api::Default> {
	/// Creates default [`Peripherals`] without type parameter requirement.
	///
	/// Uses ZST [`api::Default`].
	#[allow(non_snake_case)]
	pub fn Default() -> Self { Self(Default::default()) }
}

impl Peripherals<api::Cache> {
	/// Creates [`Peripherals`] without type parameter requirement.
	///
	/// Uses [`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> {
	/// Enables specified peripheral.
	///
	/// Equivalent to [`sys::ffi::playdate_sys::setPeripheralsEnabled`]
	#[doc(alias = "sys::ffi::playdate_sys::setPeripheralsEnabled")]
	pub fn enable(&self, value: PDPeripherals) {
		let f = self.0.set_peripherals_enabled();
		unsafe { f(value) }
	}

	/// 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.
	///
	/// Equivalent to [`sys::ffi::playdate_sys::setPeripheralsEnabled`]
	#[doc(alias = "sys::ffi::playdate_sys::setPeripheralsEnabled")]
	#[inline(always)]
	pub fn enable_accelerometer(&self) { self.enable(Peripherals::Accelerometer) }

	/// Enables all peripherals.
	///
	/// Equivalent to [`sys::ffi::playdate_sys::setPeripheralsEnabled`]
	#[doc(alias = "sys::ffi::playdate_sys::setPeripheralsEnabled")]
	#[inline(always)]
	pub fn enable_all(&self) { self.enable(Peripherals::All) }

	/// Disables all peripherals.
	///
	/// Equivalent to [`sys::ffi::playdate_sys::setPeripheralsEnabled`]
	#[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;
}


// TODO: SystemExt should be const_trait
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()) }
}


/// Accelerometer
#[derive(Debug, Clone, Copy)]
pub struct Accelerometer<Api = api::Default>(Api);

impl Accelerometer<api::Default> {
	/// Creates default [`Accelerometer`] without type parameter requirement.
	///
	/// Uses ZST [`api::Default`].
	#[allow(non_snake_case)]
	pub fn Default() -> Self { Self(Default::default()) }
}

impl Accelerometer<api::Cache> {
	/// Creates [`Accelerometer`] without type parameter requirement.
	///
	/// Uses [`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> {
	/// Enables 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.
	///
	/// Equivalent to [`sys::ffi::playdate_sys::setPeripheralsEnabled`].
	#[doc(alias = "sys::ffi::playdate_sys::setPeripheralsEnabled")]
	pub fn enable(&self) {
		let f = self.0.set_peripherals_enabled();
		unsafe { f(Peripherals::Accelerometer) }
	}

	/// ⚠️ Disables all peripherals including accelerometer.
	///
	/// Currently it doesn't matter because there's just one peripheral - accelerometer.
	///
	/// Equivalent to [`sys::ffi::playdate_sys::setPeripheralsEnabled`].
	#[doc(alias = "sys::ffi::playdate_sys::setPeripheralsEnabled")]
	pub fn disable(&self) {
		let f = self.0.set_peripherals_enabled();
		unsafe { f(Peripherals::None) }
	}

	/// Returns `(x, y, z)` accelerometer data.
	///
	/// Equivalent to [`sys::ffi::playdate_sys::getAccelerometer`].
	#[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)
	}

	/// Gets accelerometer data directly to `x`, `y` and `z`.
	///
	/// Equivalent to [`sys::ffi::playdate_sys::getAccelerometer`].
	#[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) }
	}
}


/// Buttons
#[derive(Debug, Clone, Copy)]
pub struct Buttons<Api = api::Default>(Api);

impl Buttons<api::Default> {
	/// Creates default [`Buttons`] without type parameter requirement.
	///
	/// Uses ZST [`api::Default`].
	#[allow(non_snake_case)]
	pub fn Default() -> Self { Self(Default::default()) }
}

impl Buttons<api::Cache> {
	/// Creates [`Buttons`] without type parameter requirement.
	///
	/// Uses [`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> {
	/// Returns the current buttons [`State`].
	///
	/// Equivalent to [`sys::ffi::playdate_sys::getButtonState`].
	#[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 }
	}

	/// Writes the current buttons state to given [`State`].
	///
	/// Updates all (current, pushed and released).
	///
	/// Equivalent to [`sys::ffi::playdate_sys::getButtonState`].
	#[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) }
	}

	/// Writes the current buttons state to given references.
	///
	/// Equivalent to [`sys::ffi::playdate_sys::getButtonState`].
	#[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) }
	}

	/// Equivalent to [`sys::ffi::playdate_sys::getButtonState`].
	#[doc(alias = "sys::ffi::playdate_sys::getButtonState")]
	///
	/// Requests & returns only `current` part of state, see [Self::current]
	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
	}

	/// Equivalent to [`sys::ffi::playdate_sys::getButtonState`].
	#[doc(alias = "sys::ffi::playdate_sys::getButtonState")]
	///
	/// Requests & returns only `current` part of state, see [Self::pushed]
	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
	}

	/// Equivalent to [`sys::ffi::playdate_sys::getButtonState`].
	#[doc(alias = "sys::ffi::playdate_sys::getButtonState")]
	///
	/// Requests & returns only `current` part of state, see [Self::released]
	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
	}
}

/// 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.
pub struct State {
	/// Indicating which buttons are __currently down__.
	pub current: PDButtons,
	/// Reflect which buttons were pushed over the previous update cycle.
	/// See [struct doc][Self].
	pub pushed: PDButtons,
	/// Reflect which buttons were released over the previous update cycle.
	/// See [struct doc][Self].
	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()
	}
}


/// Crank
#[derive(Debug, Clone, Copy)]
pub struct Crank<Api = api::Default>(Api);

impl Crank<api::Default> {
	/// Creates default [`Crank`] without type parameter requirement.
	///
	/// Uses ZST [`api::Default`].
	#[allow(non_snake_case)]
	pub fn Default() -> Self { Self(Default::default()) }
}

impl Crank<api::Cache> {
	/// Creates [`Crank`] without type parameter requirement.
	///
	/// Uses [`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> {
	/// Returns boolean indicating whether or not the crank is folded into the unit.
	///
	/// Equivalent to [`sys::ffi::playdate_sys::isCrankDocked`].
	#[doc(alias = "sys::ffi::playdate_sys::isCrankDocked")]
	pub fn docked(&self) -> bool {
		let f = self.0.is_crank_docked();
		unsafe { f() == 1 }
	}

	/// Returns the current position of the crank, in the range 0-360.
	/// Zero is pointing up, and the value increases as the crank moves clockwise, as viewed from the right side of the device.
	///
	/// Equivalent to [`sys::ffi::playdate_sys::getCrankAngle`].
	#[doc(alias = "sys::ffi::playdate_sys::getCrankAngle")]
	pub fn angle(&self) -> c_float {
		let f = self.0.get_crank_angle();
		unsafe { f() }
	}

	/// Returns the angle change of the crank since the last time this function was called.
	/// Negative values are anti-clockwise.
	///
	/// Equivalent to [`sys::ffi::playdate_sys::getCrankChange`].
	#[doc(alias = "sys::ffi::playdate_sys::getCrankChange")]
	pub fn change(&self) -> c_float {
		let f = self.0.get_crank_change();
		unsafe { f() }
	}

	/// Returns the previous value for this setting.
	///
	/// Equivalent to [`sys::ffi::playdate_sys::setCrankSoundsDisabled`].
	#[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 }
	}
}