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 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
//! Type definitions. use core::marker::PhantomData; /// Errors in this crate #[derive(Debug)] pub enum Error<E> { /// I²C bus error I2C(E), /// Invalid input data provided InvalidInputData, } /// Error type for mode changes. /// /// This allows to retrieve the unchanged device in case of an error. pub enum ModeChangeError<E, DEV> { /// I²C bus error while changing mode. /// /// `E` is the error that happened. /// `DEV` is the device with the mode unchanged. I2C(E, DEV), } /// Mode marker types pub mod mode { /// One-shot operating mode / power-down state (default) pub struct OneShot(()); /// Continuous conversion mode pub struct Continuous(()); } /// Data rate for ADS1013, ADS1014, ADS1015 #[derive(Debug, Clone, Copy, PartialEq)] pub enum DataRate12Bit { /// 128 SPS Sps128, /// 250 SPS Sps250, /// 490 SPS Sps490, /// 920 SPS Sps920, /// 1600 SPS (default) Sps1600, /// 2400 SPS Sps2400, /// 3300 SPS Sps3300, } /// Data rate for ADS1113, ADS1114, ADS1115 #[derive(Debug, Clone, Copy, PartialEq)] pub enum DataRate16Bit { /// 8 SPS Sps8, /// 16 SPS Sps16, /// 32 SPS Sps32, /// 64 SPS Sps64, /// 128 SPS (default) Sps128, /// 250 SPS Sps250, /// 475 SPS Sps475, /// 860 SPS Sps860, } /// Comparator mode (only for ADS1x14, ADS1x15) #[derive(Debug, Clone, Copy, PartialEq)] pub enum ComparatorMode { /// Traditional comparator (default) /// /// In this mode the ALERT/RDY pin asserts (according to selected active /// polarity) when the conversion data exceeds the limit set as *high* /// threshold and remains active until the conversion data falls below the /// value set as *low* threshold. Traditional, /// Window comparator /// /// In this mode the ALERT/RDY pin asserts (according to selected active /// polarity) when the conversion data exceeds the value set as *high* /// threshold or goes below the value set as *low* temperature threshold. Window, } /// Comparator polarity (only for ADS1x14, ADS1x15) #[derive(Debug, Clone, Copy, PartialEq)] pub enum ComparatorPolarity { /// Active low (default) ActiveLow, /// Active high ActiveHigh, } /// Comparator polarity (only for ADS1x14, ADS1x15) /// /// Select whether the ALERT/RDY pin latches after being asserted or clears /// after conversions are within the margin of the upper and lower /// threshold values. #[derive(Debug, Clone, Copy, PartialEq)] pub enum ComparatorLatching { /// Nonlatching (default) /// /// The ALERT/RDY pin does not latch when asserted. Nonlatching, /// Latching /// /// The asserted ALERT/RDY pin remains latched until conversion data are /// read by the master or an appropriate SMBus alert response is sent by /// the master. The device responds with its address, and it is the lowest /// address currently asserting the ALERT/RDY bus line. Latching, } /// Comparator alert queue (only for ADS1x14, ADS1x15) /// /// The default state of the comparator is deactivated. It can be activated by setting /// the comparator queue. #[derive(Debug, Clone, Copy, PartialEq)] pub enum ComparatorQueue { /// Activate comparator and assert after one conversion exceeding thresholds One, /// Activate comparator and assert after two consecutive conversions exceeding thresholds Two, /// Activate comparator and assert after four consecutive conversions exceeding thresholds Four, } /// Full-scale range configuration for the programmable gain amplifier (PGA) (only for ADS1x14, ADS1x15) /// /// This sets the input voltage measurable range. /// The FSR is fixed at ±2.048 V in the ADS1x13. #[derive(Clone, Copy, Debug, PartialEq)] #[allow(non_camel_case_types)] pub enum FullScaleRange { /// The measurable range is ±6.144V. Within6_144V, /// The measurable range is ±4.096V. Within4_096V, /// The measurable range is ±2.048V. (default) Within2_048V, /// The measurable range is ±1.024V. Within1_024V, /// The measurable range is ±0.512V. Within0_512V, /// The measurable range is ±0.256V. Within0_256V, } /// Possible slave addresses #[derive(Debug, Clone, Copy, PartialEq)] pub enum SlaveAddr { /// Default slave address Default, /// Alternative slave address using the provided values /// for the last two bits (A1, A0) Alternative(bool, bool), } impl Default for SlaveAddr { /// Default slave address fn default() -> Self { SlaveAddr::Default } } impl SlaveAddr { pub(crate) fn addr(self, default: u8) -> u8 { match self { SlaveAddr::Default => default, SlaveAddr::Alternative(a1, a0) => default | ((a1 as u8) << 1) | a0 as u8, } } /// Create `SlaveAddr` instance corresponding to the address /// effective when connecting the pin `ADDR` to GND (0x48). /// /// See [Table 4 in the datasheet](https://www.ti.com/lit/ds/symlink/ads1115.pdf#%5B%7B%22num%22%3A716%2C%22gen%22%3A0%7D%2C%7B%22name%22%3A%22XYZ%22%7D%2C0%2C602.2%2C0%5D). pub fn new_gnd() -> Self { SlaveAddr::default() } /// Create `SlaveAddr` instance corresponding to the address /// effective when connecting the pin `ADDR` to VDD (0x49). /// /// See [Table 4 in the datasheet](https://www.ti.com/lit/ds/symlink/ads1115.pdf#%5B%7B%22num%22%3A716%2C%22gen%22%3A0%7D%2C%7B%22name%22%3A%22XYZ%22%7D%2C0%2C602.2%2C0%5D). pub fn new_vdd() -> Self { SlaveAddr::Alternative(false, true) } /// Create `SlaveAddr` instance corresponding to the address /// effective when connecting the pin `ADDR` to SDA (0x4A). /// /// See [Table 4 in the datasheet](https://www.ti.com/lit/ds/symlink/ads1115.pdf#%5B%7B%22num%22%3A716%2C%22gen%22%3A0%7D%2C%7B%22name%22%3A%22XYZ%22%7D%2C0%2C602.2%2C0%5D). pub fn new_sda() -> Self { SlaveAddr::Alternative(true, false) } /// Create `SlaveAddr` instance corresponding to the address /// effective when connecting the pin `ADDR` to SCL (0x4B). /// /// See [Table 4 in the datasheet](https://www.ti.com/lit/ds/symlink/ads1115.pdf#%5B%7B%22num%22%3A716%2C%22gen%22%3A0%7D%2C%7B%22name%22%3A%22XYZ%22%7D%2C0%2C602.2%2C0%5D). pub fn new_scl() -> Self { SlaveAddr::Alternative(true, true) } } #[derive(Debug, Clone, PartialEq)] pub(crate) struct Config { pub(crate) bits: u16, } impl Config { pub(crate) fn is_high(&self, mask: u16) -> bool { (self.bits & mask) != 0 } pub(crate) fn with_high(&self, mask: u16) -> Self { Config { bits: self.bits | mask, } } pub(crate) fn with_low(&self, mask: u16) -> Self { Config { bits: self.bits & !mask, } } } impl Default for Config { fn default() -> Self { Config { bits: 0x8583 } } } impl Default for FullScaleRange { fn default() -> Self { FullScaleRange::Within2_048V } } /// ADS1x1x ADC driver #[derive(Debug, Default)] pub struct Ads1x1x<DI, IC, CONV, MODE> { pub(crate) iface: DI, pub(crate) config: Config, pub(crate) fsr: FullScaleRange, pub(crate) a_conversion_was_started: bool, pub(crate) _conv: PhantomData<CONV>, pub(crate) _ic: PhantomData<IC>, pub(crate) _mode: PhantomData<MODE>, } #[cfg(test)] mod tests { use crate::DEVICE_BASE_ADDRESS as ADDR; use crate::{FullScaleRange, SlaveAddr}; #[test] fn can_get_default_address() { let addr = SlaveAddr::default(); assert_eq!(ADDR, addr.addr(ADDR)); } #[test] fn can_generate_alternative_addresses() { assert_eq!(0b100_1000, SlaveAddr::Alternative(false, false).addr(ADDR)); assert_eq!(0b100_1001, SlaveAddr::Alternative(false, true).addr(ADDR)); assert_eq!(0b100_1010, SlaveAddr::Alternative(true, false).addr(ADDR)); assert_eq!(0b100_1011, SlaveAddr::Alternative(true, true).addr(ADDR)); } #[test] fn can_generate_alternative_addresses_using_helper_constructors() { assert_eq!(0b100_1000, SlaveAddr::new_gnd().addr(ADDR)); assert_eq!(0b100_1001, SlaveAddr::new_vdd().addr(ADDR)); assert_eq!(0b100_1010, SlaveAddr::new_sda().addr(ADDR)); assert_eq!(0b100_1011, SlaveAddr::new_scl().addr(ADDR)); } #[test] fn default_full_scale_range() { assert_eq!(FullScaleRange::Within2_048V, FullScaleRange::default()); } }