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
#![deny(unsafe_code)]
#![deny(missing_docs)]
#![cfg_attr(not(test), no_std)]
use configuration::Configuration;
use embedded_hal::blocking::i2c;
use error::Error;
use register::Register;
pub mod configuration;
pub mod constants;
pub mod error;
pub(crate) mod register;
pub mod status;
#[cfg(test)]
mod test;
#[derive(Debug)]
pub struct As5600<I2C, D> {
i2c: I2C,
address: u8,
delay: D,
}
impl<I2C, D, E> As5600<I2C, D>
where
I2C: i2c::Read<Error = E> + i2c::Write<Error = E> + i2c::WriteRead<Error = E>,
{
pub fn new(i2c: I2C, address: u8, delay: D) -> Self {
Self {
i2c,
address,
delay,
}
}
pub fn release(self) -> (I2C, D) {
(self.i2c, self.delay)
}
pub fn get_raw_angle(&mut self) -> Result<u16, Error<E>> {
Ok(self.read_u16(Register::RawAngle)? & 0x0FFF)
}
pub fn get_angle(&mut self) -> Result<u16, Error<E>> {
Ok(self.read_u16(Register::Angle)? & 0x0FFF)
}
pub fn get_zmco(&mut self) -> Result<u8, E> {
let mut buffer = [0u8; 1];
self.i2c
.write_read(self.address, &[Register::Zmco.into()], &mut buffer)?;
Ok(buffer[0] & 0b0000_0011)
}
pub fn magnet_status(&mut self) -> Result<status::Status, Error<E>> {
let mut buffer = [0u8; 1];
self.i2c
.write_read(self.address, &[Register::Status.into()], &mut buffer)?;
status::Status::try_from(buffer).map_err(Error::Status)
}
pub fn get_zero_position(&mut self) -> Result<u16, Error<E>> {
Ok(self.read_u16(Register::Zpos)? & 0x0FFF)
}
pub fn get_maximum_position(&mut self) -> Result<u16, Error<E>> {
Ok(self.read_u16(Register::Mpos)? & 0x0FFF)
}
pub fn get_maximum_angle(&mut self) -> Result<u16, Error<E>> {
Ok(self.read_u16(Register::Mang)? & 0x0FFF)
}
pub fn get_config(&mut self) -> Result<Configuration, Error<E>> {
let bytes = self.read_u16(Register::Conf)?;
configuration::Configuration::try_from(bytes).map_err(Error::Configuration)
}
pub fn get_automatic_gain_control(&mut self) -> Result<u8, E> {
let mut buffer = [0u8; 1];
self.i2c.write_read(self.address, &[0x1a], &mut buffer)?;
Ok(buffer[0])
}
pub fn get_magnitude(&mut self) -> Result<u16, Error<E>> {
Ok(self.read_u16(Register::Magnitude)? & 0x0FFF)
}
fn read_u16(&mut self, command: Register) -> Result<u16, Error<E>> {
let mut buffer = [0u8; 2];
self.i2c
.write_read(self.address, &[command.into()], &mut buffer)?;
Ok(u16::from_be_bytes(buffer))
}
}