summaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
authorDiego Barrios Romero <eldruin@gmail.com>2021-07-29 23:14:25 +0200
committerDiego Barrios Romero <eldruin@gmail.com>2021-07-29 23:14:25 +0200
commit28126fee8ce253e67ab737927ff0d89ebc24e419 (patch)
tree503e6056682296c3ac3ead637cad7f5c2e6da8a3 /examples
parentdeb65c3a794bdc4e5c534983c8753072cec10adf (diff)
downloadads1x1x-async-28126fee8ce253e67ab737927ff0d89ebc24e419.tar.gz
ads1x1x-async-28126fee8ce253e67ab737927ff0d89ebc24e419.tar.xz
ads1x1x-async-28126fee8ce253e67ab737927ff0d89ebc24e419.zip
Add value-based one-shot trait to ease driver usage in functions
Diffstat (limited to 'examples')
-rw-r--r--examples/trait.rs24
1 files changed, 24 insertions, 0 deletions
diff --git a/examples/trait.rs b/examples/trait.rs
new file mode 100644
index 0000000..eda1f37
--- /dev/null
+++ b/examples/trait.rs
@@ -0,0 +1,24 @@
+// This example demonstrates the use of a type alias for the `Ads1x1x` struct
+// to ease usage in signatures.
+
+use linux_embedded_hal::I2cdev;
+use nb::block;
+
+use ads1x1x::{Ads1x1x, ChannelSelection, DynamicOneShot, SlaveAddr};
+
+/// Read a single value from channel A.
+/// Returns 0 on Error.
+pub fn read<E, A: DynamicOneShot<Error = E>>(adc: &mut A) -> i16 {
+ block!(adc.read(ChannelSelection::SingleA0)).unwrap_or(0)
+}
+
+fn main() {
+ let dev = I2cdev::new("/dev/i2c-1").unwrap();
+ let address = SlaveAddr::default();
+ let mut adc = Ads1x1x::new_ads1115(dev, address);
+
+ let value = read(&mut adc);
+ println!("Measurement: {}", value);
+ // get I2C device back
+ let _dev = adc.destroy_ads1115();
+}