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
use command::Command;
use registers::{Register, Config, Status};
pub trait Device {
type Error;
fn ce_enable(&mut self);
fn ce_disable(&mut self);
fn with_ce_disabled<F, R>(&mut self, f: F) -> R
where
F: FnOnce(&mut Self) -> R,
{
self.ce_disable();
let r = f(self);
self.ce_enable();
r
}
fn send_command<C: Command>(&mut self, command: &C) -> Result<(Status, C::Response), Self::Error>;
fn write_register<R: Register>(&mut self, register: R) -> Result<Status, Self::Error>;
fn read_register<R: Register>(&mut self) -> Result<(Status, R), Self::Error>;
fn update_register<Reg, F, R>(&mut self, f: F) -> Result<R, Self::Error>
where
Reg: Register + PartialEq + Clone,
F: FnOnce(&mut Reg) -> R,
{
assert!(Reg::addr() != 0x00);
let (_, old_register) = self.read_register::<Reg>()?;
let mut register = old_register.clone();
let result = f(&mut register);
if register != old_register {
self.write_register(register)?;
}
Ok(result)
}
fn update_config<F, R>(&mut self, f: F) -> Result<R, Self::Error>
where F: FnOnce(&mut Config) -> R;
}