spectrusty_peripherals/
joystick.rs1use core::fmt::Debug;
10
11pub mod cursor;
12pub mod fuller;
13pub mod kempston;
14pub mod sinclair;
15
16bitflags! {
17 #[derive(Default)]
21 pub struct Directions: u8 {
22 const UP = 0b0000_0001;
23 const RIGHT = 0b0000_0010;
24 const DOWN = 0b0000_0100;
25 const LEFT = 0b0000_1000;
26 }
27}
28
29pub enum JoyDirection {
31 Center,
32 Up,
33 UpRight,
34 Right,
35 DownRight,
36 Down,
37 DownLeft,
38 Left,
39 UpLeft
40}
41
42pub trait JoystickInterface {
44 fn fire(&mut self, btn: u8, pressed: bool);
49 fn get_fire(&self, btn: u8) -> bool;
51 fn set_directions(&mut self, dir: Directions);
53 fn get_directions(&self) -> Directions;
55 #[inline]
57 fn direction(&mut self, dir: JoyDirection) {
58 self.set_directions(match dir {
59 JoyDirection::Center => Directions::empty(),
60 JoyDirection::Up => Directions::UP,
61 JoyDirection::UpRight => Directions::UP|Directions::RIGHT,
62 JoyDirection::Right => Directions::RIGHT,
63 JoyDirection::DownRight => Directions::DOWN|Directions::RIGHT,
64 JoyDirection::Down => Directions::DOWN,
65 JoyDirection::DownLeft => Directions::DOWN|Directions::LEFT,
66 JoyDirection::Left => Directions::LEFT,
67 JoyDirection::UpLeft => Directions::UP|Directions::LEFT,
68 })
69 }
70 #[inline]
72 fn center(&mut self) {
73 self.set_directions(Directions::empty());
74 }
75 #[inline]
77 fn is_up(&self) -> bool {
78 self.get_directions().intersects(Directions::UP)
79 }
80 #[inline]
82 fn is_right(&self) -> bool {
83 self.get_directions().intersects(Directions::RIGHT)
84 }
85 #[inline]
87 fn is_left(&self) -> bool {
88 self.get_directions().intersects(Directions::LEFT)
89 }
90 #[inline]
92 fn is_down(&self) -> bool {
93 self.get_directions().intersects(Directions::DOWN)
94 }
95 #[inline]
97 fn is_center(&self) -> bool {
98 self.get_directions().intersects(Directions::empty())
99 }
100}
101
102pub trait JoystickDevice: Debug {
104 fn port_read(&self, port: u16) -> u8;
106 fn port_write(&mut self, _port: u16, _data: u8) -> bool { false }
111}
112
113#[derive(Clone, Copy, Default, Debug)]
115pub struct NullJoystickDevice;
116
117impl JoystickDevice for NullJoystickDevice {
118 fn port_read(&self, _port: u16) -> u8 {
119 u8::max_value()
120 }
121}
122
123impl JoystickInterface for NullJoystickDevice {
124 fn fire(&mut self, _btn: u8, _pressed: bool) {}
125 fn get_fire(&self, _btn: u8) -> bool { false }
126 fn set_directions(&mut self, _dir: Directions) {}
127 fn get_directions(&self) -> Directions {
128 Directions::empty()
129 }
130}