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
|
#![allow(dead_code)]
use utils::{Read, Write};
pub trait Controller {
fn read(&self) -> u8;
fn write(&self, data: u8);
fn load(&mut self, reader: &mut Read) -> bool;
fn save(&self, writer: &mut Write) -> bool;
}
pub trait InputPoller {
fn poll(&self) -> u8;
}
pub mod stdctl {
use utils::{Read, Write, load_prefix, save_prefix};
use core::cell::Cell;
use controller::{Controller, InputPoller};
pub const A: u8 = 1 << 0;
pub const B: u8 = 1 << 1;
pub const SELECT: u8 = 1 << 2;
pub const START: u8 = 1 << 3;
pub const UP: u8 = 1 << 4;
pub const DOWN: u8 = 1 << 5;
pub const LEFT: u8 = 1 << 6;
pub const RIGHT: u8 = 1 << 7;
pub const NULL: u8 = 0;
#[repr(C)]
pub struct Joystick<'a> {
strobe: Cell<bool>,
reg: Cell<u8>,
poller: &'a InputPoller,
}
impl<'a> Joystick<'a> {
pub fn new(poller: &'a InputPoller) -> Self {
Joystick{
reg: Cell::new(0),
strobe: Cell::new(false),
poller
}
}
}
impl<'a> Controller for Joystick<'a> {
fn read(&self) -> u8 {
if self.strobe.get() {
self.reg.set(self.poller.poll());
self.reg.get() & 1
} else {
let old = self.reg.get();
self.reg.set(old >> 1);
old & 1
}
}
fn write(&self, data: u8) {
self.strobe.set(data & 1 == 1);
if self.strobe.get() {
self.reg.set(self.poller.poll())
}
}
fn load(&mut self, reader: &mut Read) -> bool {
load_prefix(self, 0, reader)
}
fn save(&self, writer: &mut Write) -> bool {
save_prefix(self, 0, writer)
}
}
}
|