// MIT License // // Copyright 2023 Michael Büsch // Copyright © 2020-present, Michael Cummings . // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in all // copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE // SOFTWARE. //! Contains the I²C driver for the device. use crate::{ Adxl345, Adxl345AccExtract, Adxl345Init, Adxl345Reader, Adxl345Writer, AdxlError, AdxlResult, Result, }; use embedded_hal::i2c::{I2c, SevenBitAddress}; /// I²C driver structure for the device. #[derive(Debug)] pub struct Device { /// Any bus object implementing the `embedded_hal` `I2c` trait. bus: B, /// The 7 bit address of the device. address: SevenBitAddress, } impl Device { /// Constructor with default slave address `0x53`. /// /// ## Arguments /// * `bus` - Any object implementing the `embedded_hal` `I2c` trait. pub fn new(bus: B) -> AdxlResult { Self::with_address(bus, 0x53) } /// Constructor with explicit slave address. /// /// The device only has two addresses `0x53` or `0x1d` depending on the low or /// high logic level on the `ALT ADDRESS` pin. /// /// ## Arguments /// * `bus` - Any object implementing the `embedded_hal` `I2c` trait. /// * `address` - Address of ADXL345 device. pub fn with_address(bus: B, address: u8) -> AdxlResult { let mut device = Device { bus, address }; device.init()?; Ok(device) } } impl Adxl345 for Device {} impl Adxl345Init for Device {} impl Adxl345AccExtract for Device {} impl Adxl345Reader for Device { fn access(&mut self, register: u8) -> AdxlResult { let mut buf = [0u8; 1]; if self .bus .write_read(self.address, &[register], &mut buf) .is_err() { return Err(AdxlError::I2c()); } Ok(buf[0]) } fn acceleration(&mut self) -> AdxlResult<(i16, i16, i16)> { let register = 0x32; let mut buf = [0u8; 6]; if self .bus .write_read(self.address, &[register], &mut buf) .is_err() { return Err(AdxlError::I2c()); } Ok(self.extract_acceleration(&buf)) } } impl Adxl345Writer for Device { fn command(&mut self, register: u8, byte: u8) -> Result { if self.bus.write(self.address, &[register, byte]).is_err() { return Err(AdxlError::I2c()); } Ok(()) } fn init(&mut self) -> Result { self.init_registers(false) } }