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
use embedded_hal::blocking::delay::DelayUs;
use rppal::i2c::{self, Error as I2CError, I2c};
#[macro_use]
extern crate log;
use thiserror::Error;
mod regs;
const STD_PROCESSING_DELAY_MICROS: u16 = 125;
const SENSOR_START_ADDR: u16 = 0x36;
const SENSOR_END_ADDR: u16 = 0x39;
pub type Result<T> = std::result::Result<T, SoilSensErr>;
pub struct SoilSensor<D: DelayUs<u16>> {
channel: I2c,
delay: D,
}
impl<D: DelayUs<u16>> SoilSensor<D> {
pub fn init(mut delay: D) -> Result<Self> {
let mut channel = i2c::I2c::new()?;
let mut hw_found: bool = false;
for adr in SENSOR_START_ADDR..=SENSOR_END_ADDR {
channel.set_slave_address(adr)?;
debug!("Connecting to adr: {:#X}", adr);
match init::channel_init(&mut delay, &mut channel) {
Ok(()) => {
hw_found = true;
break;
}
Err(SoilSensErr::HardwareMismatch(..)) => continue,
Err(SoilSensErr::InvalidSlaveAddress(..)) => continue,
Err(e) => return Err(e),
}
}
if !hw_found {
return Err(SoilSensErr::HwNotFound);
}
Ok(SoilSensor { channel, delay })
}
pub fn init_with_channel(mut delay: D, channel: I2c) -> Result<Self> {
let mut channel = channel;
init::channel_init(&mut delay, &mut channel)?;
Ok(SoilSensor { channel, delay })
}
pub fn get_temp(&mut self) -> Result<f32> {
let l_reg = regs::base::SEESAW_STATUS_BASE;
let h_reg = regs::func::SEESAW_STATUS_TEMP;
let delay = STD_PROCESSING_DELAY_MICROS;
let mut buffer = [0u8; 4];
self.read(l_reg, h_reg, &mut buffer[..], delay)?;
let tmp_val = i32::from_be_bytes(buffer) as f32;
let temp_celsius = (1.0 / (1u32 << 16) as f32) * tmp_val;
Ok(temp_celsius)
}
pub fn get_capacitance(&mut self) -> Result<u16> {
let l_reg: u8 = regs::base::SEESAW_TOUCH_BASE;
let h_reg: u8 = regs::touch::SEESAW_TOUCH_CHANNEL_OFFSET;
let mut buff = [0u8; 2];
let mut retry_counter = 0;
while retry_counter < 3 {
self.delay.delay_us(1000);
if let Err(e) = self.read(l_reg, h_reg, &mut buff, 5000) {
debug!("Error reading capacitance: {}. Retry: {}", e, retry_counter + 1);
retry_counter += 1;
continue;
}
let cap = u16::from_be_bytes(buff);
if cap < u16::max_value() {
return Ok(cap);
}
}
Err(SoilSensErr::MoistureReadErr)
}
fn read(&mut self, reg_low: u8, reg_high: u8, buff: &mut [u8], delay_us: u16) -> Result<()> {
self.channel.write(&[reg_low, reg_high])?;
self.delay.delay_us(delay_us);
self.channel.read(buff)?;
debug!("Received: {:?}", buff);
Ok(())
}
}
mod init {
use super::*;
pub fn channel_init<D: DelayUs<u16>>(delay: &mut D, chan: &mut I2c) -> Result<()> {
match try_read_chan(chan, delay) {
Ok(resp) => {
debug!("Found device with HW id: {}", resp);
if resp != regs::SEESAW_HW_ID_CODE {
return Err(SoilSensErr::HardwareMismatch(regs::SEESAW_HW_ID_CODE, resp));
} else {
debug!("HW ID match: exp {}, got: {}", resp, regs::SEESAW_HW_ID_CODE);
return Ok(());
}
}
Err(SoilSensErr::I2C {
source: I2CError::InvalidSlaveAddress(adr),
}) => {
debug!("Invalid address: {}", adr);
return Err(SoilSensErr::InvalidSlaveAddress(adr));
}
Err(e) => {
debug!("Unexpected err: {}", e);
return Err(e);
}
}
}
fn try_read_chan<D: DelayUs<u16>>(chan: &mut I2c, delay: &mut D) -> Result<u8> {
let reg_high = regs::base::SEESAW_STATUS_BASE;
let reg_low = regs::func::SEESAW_STATUS_HW_ID;
chan.write(&[reg_high, reg_low])?;
let mut buffer = [0];
delay.delay_us(STD_PROCESSING_DELAY_MICROS);
chan.read(&mut buffer)?;
debug!("Got: {:?}", buffer);
Ok(buffer[0])
}
}
#[derive(Debug, Error)]
pub enum SoilSensErr {
#[error("Couldn't get a valid reading from the moisture sensor.")]
MoistureReadErr,
#[error("Couldn't connect to the sensor.")]
HwNotFound,
#[error("Invalid Hardware ID. Expected {0}, got {1}")]
HardwareMismatch(u8, u8),
#[error("invalid slave address: {0:#X}")]
InvalidSlaveAddress(u16),
#[error("I2C connection error. {source}")]
I2C {
#[from]
source: i2c::Error,
},
}