implmented maybe_async
This commit is contained in:
34
src/lib.rs
34
src/lib.rs
@@ -1,13 +1,19 @@
|
|||||||
#![no_std]
|
#![no_std]
|
||||||
|
|
||||||
#[cfg(feature = "sync")]
|
#[cfg(feature = "sync")]
|
||||||
use embedded_hal::i2c::{self};
|
use embedded_hal::{
|
||||||
|
delay::DelayNs,
|
||||||
|
i2c::{self},
|
||||||
|
};
|
||||||
|
|
||||||
use embedded_hal_async::delay::DelayNs;
|
|
||||||
#[cfg(not(feature = "sync"))]
|
#[cfg(not(feature = "sync"))]
|
||||||
use embedded_hal_async::i2c::{self};
|
use embedded_hal_async::{
|
||||||
|
delay::DelayNs,
|
||||||
|
i2c::{self},
|
||||||
|
};
|
||||||
|
|
||||||
use bitflags::bitflags;
|
use bitflags::bitflags;
|
||||||
|
use maybe_async::maybe_async;
|
||||||
|
|
||||||
const REG_OUT_START: u8 = 0x00;
|
const REG_OUT_START: u8 = 0x00;
|
||||||
|
|
||||||
@@ -241,6 +247,7 @@ where
|
|||||||
data_rate: DataRate,
|
data_rate: DataRate,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[maybe_async]
|
||||||
impl<I, D> MMC56X3<I, D>
|
impl<I, D> MMC56X3<I, D>
|
||||||
where
|
where
|
||||||
I: i2c::I2c,
|
I: i2c::I2c,
|
||||||
@@ -257,6 +264,7 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[maybe_async]
|
||||||
pub async fn init(&mut self) -> Result<(), Error<I::Error>> {
|
pub async fn init(&mut self) -> Result<(), Error<I::Error>> {
|
||||||
self.reset().await?;
|
self.reset().await?;
|
||||||
|
|
||||||
@@ -264,6 +272,7 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Resets the sensor to an initial state
|
/// Resets the sensor to an initial state
|
||||||
|
#[maybe_async]
|
||||||
pub async fn reset(&mut self) -> Result<(), Error<I::Error>> {
|
pub async fn reset(&mut self) -> Result<(), Error<I::Error>> {
|
||||||
self.write_reg_controll_1(Control1RegisterFlags::SW_RESET)
|
self.write_reg_controll_1(Control1RegisterFlags::SW_RESET)
|
||||||
.await?;
|
.await?;
|
||||||
@@ -284,6 +293,7 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Pulse large currents through the sense coils to clear any offset
|
/// Pulse large currents through the sense coils to clear any offset
|
||||||
|
#[maybe_async]
|
||||||
pub async fn magnet_set_reset(&mut self) -> Result<(), Error<I::Error>> {
|
pub async fn magnet_set_reset(&mut self) -> Result<(), Error<I::Error>> {
|
||||||
self.write_reg_controll_0(self.ctrl0 | Control0RegisterFlags::DO_SET)
|
self.write_reg_controll_0(self.ctrl0 | Control0RegisterFlags::DO_SET)
|
||||||
.await?;
|
.await?;
|
||||||
@@ -300,6 +310,7 @@ where
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[maybe_async]
|
||||||
pub async fn set_continuous_mode(&mut self, enable: bool) -> Result<(), Error<I::Error>> {
|
pub async fn set_continuous_mode(&mut self, enable: bool) -> Result<(), Error<I::Error>> {
|
||||||
if enable {
|
if enable {
|
||||||
if matches!(self.data_rate, DataRate::Unset) {
|
if matches!(self.data_rate, DataRate::Unset) {
|
||||||
@@ -323,6 +334,7 @@ where
|
|||||||
self.ctrl2.contains(Control2RegisterFlags::CMM_EN)
|
self.ctrl2.contains(Control2RegisterFlags::CMM_EN)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[maybe_async]
|
||||||
pub async fn set_data_rate(&mut self, rate: DataRate) -> Result<(), Error<I::Error>> {
|
pub async fn set_data_rate(&mut self, rate: DataRate) -> Result<(), Error<I::Error>> {
|
||||||
match rate {
|
match rate {
|
||||||
DataRate::Hz(hz) => {
|
DataRate::Hz(hz) => {
|
||||||
@@ -352,11 +364,13 @@ where
|
|||||||
/// Set the bandwidth selection bits to adjust the length of the decimation filter. They control the duration
|
/// Set the bandwidth selection bits to adjust the length of the decimation filter. They control the duration
|
||||||
/// of each measurement. They also impacts the maximum data rate.
|
/// of each measurement. They also impacts the maximum data rate.
|
||||||
#[inline]
|
#[inline]
|
||||||
|
#[maybe_async]
|
||||||
pub async fn set_bandwidth(&mut self, bandwidth: Bandwidth) -> Result<(), Error<I::Error>> {
|
pub async fn set_bandwidth(&mut self, bandwidth: Bandwidth) -> Result<(), Error<I::Error>> {
|
||||||
self.write_reg_controll_1(bandwidth.to_flags()).await
|
self.write_reg_controll_1(bandwidth.to_flags()).await
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Enable or disable the automatic set/reset. This also impacts the maximum data rate.
|
/// Enable or disable the automatic set/reset. This also impacts the maximum data rate.
|
||||||
|
#[maybe_async]
|
||||||
pub async fn set_auto_set_reset(&mut self, enable: bool) -> Result<(), Error<I::Error>> {
|
pub async fn set_auto_set_reset(&mut self, enable: bool) -> Result<(), Error<I::Error>> {
|
||||||
self.ctrl0
|
self.ctrl0
|
||||||
.set(Control0RegisterFlags::AUTO_SET_RESET_EN, enable);
|
.set(Control0RegisterFlags::AUTO_SET_RESET_EN, enable);
|
||||||
@@ -365,6 +379,7 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Read temperature in Celcius with steps of 0.8 C
|
/// Read temperature in Celcius with steps of 0.8 C
|
||||||
|
#[maybe_async]
|
||||||
pub async fn read_temperature(&mut self) -> Result<f32, Error<I::Error>> {
|
pub async fn read_temperature(&mut self) -> Result<f32, Error<I::Error>> {
|
||||||
if self.is_continuous_mode() {
|
if self.is_continuous_mode() {
|
||||||
return Err(Error::NotAvailableInContinuousMode);
|
return Err(Error::NotAvailableInContinuousMode);
|
||||||
@@ -385,6 +400,7 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Read the last measurement
|
/// Read the last measurement
|
||||||
|
#[maybe_async]
|
||||||
pub async fn read_messurement(&mut self) -> Result<MagneticMessurement, Error<I::Error>> {
|
pub async fn read_messurement(&mut self) -> Result<MagneticMessurement, Error<I::Error>> {
|
||||||
let mut data = [0u8; 9];
|
let mut data = [0u8; 9];
|
||||||
self.read_registers(REG_OUT_START, &mut data).await?;
|
self.read_registers(REG_OUT_START, &mut data).await?;
|
||||||
@@ -411,6 +427,7 @@ where
|
|||||||
|
|
||||||
/// Trigger a new measurement if not in continuous mode.
|
/// Trigger a new measurement if not in continuous mode.
|
||||||
/// Waits for the measurement to complete.
|
/// Waits for the measurement to complete.
|
||||||
|
#[maybe_async]
|
||||||
pub async fn trigger_messurement(&mut self) -> Result<(), Error<I::Error>> {
|
pub async fn trigger_messurement(&mut self) -> Result<(), Error<I::Error>> {
|
||||||
self.write_reg_controll_0(self.ctrl0 | Control0RegisterFlags::TAKE_MESSUREMENT_M)
|
self.write_reg_controll_0(self.ctrl0 | Control0RegisterFlags::TAKE_MESSUREMENT_M)
|
||||||
.await?;
|
.await?;
|
||||||
@@ -420,10 +437,12 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
|
#[maybe_async]
|
||||||
pub async fn read_product_id(&mut self) -> Result<u8, Error<I::Error>> {
|
pub async fn read_product_id(&mut self) -> Result<u8, Error<I::Error>> {
|
||||||
self.read_register(REG_PRODUCT_ID).await
|
self.read_register(REG_PRODUCT_ID).await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[maybe_async]
|
||||||
async fn wait_for_status_flag(
|
async fn wait_for_status_flag(
|
||||||
&mut self,
|
&mut self,
|
||||||
flag: StatusRegisterFlags,
|
flag: StatusRegisterFlags,
|
||||||
@@ -440,11 +459,13 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
|
#[maybe_async]
|
||||||
async fn read_reg_temperature(&mut self) -> Result<u8, Error<I::Error>> {
|
async fn read_reg_temperature(&mut self) -> Result<u8, Error<I::Error>> {
|
||||||
self.read_register(REG_TOUT).await
|
self.read_register(REG_TOUT).await
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
|
#[maybe_async]
|
||||||
async fn read_reg_status(&mut self) -> Result<StatusRegisterFlags, Error<I::Error>> {
|
async fn read_reg_status(&mut self) -> Result<StatusRegisterFlags, Error<I::Error>> {
|
||||||
Ok(StatusRegisterFlags::from_bits_truncate(
|
Ok(StatusRegisterFlags::from_bits_truncate(
|
||||||
self.read_register(REG_STATUS1).await?,
|
self.read_register(REG_STATUS1).await?,
|
||||||
@@ -452,11 +473,13 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
|
#[maybe_async]
|
||||||
async fn write_reg_odr(&mut self, data: u8) -> Result<(), Error<I::Error>> {
|
async fn write_reg_odr(&mut self, data: u8) -> Result<(), Error<I::Error>> {
|
||||||
self.write_register(REG_ODR, data).await
|
self.write_register(REG_ODR, data).await
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
|
#[maybe_async]
|
||||||
async fn write_reg_controll_0(
|
async fn write_reg_controll_0(
|
||||||
&mut self,
|
&mut self,
|
||||||
value: Control0RegisterFlags,
|
value: Control0RegisterFlags,
|
||||||
@@ -465,6 +488,7 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
|
#[maybe_async]
|
||||||
async fn write_reg_controll_1(
|
async fn write_reg_controll_1(
|
||||||
&mut self,
|
&mut self,
|
||||||
value: Control1RegisterFlags,
|
value: Control1RegisterFlags,
|
||||||
@@ -473,6 +497,7 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
|
#[maybe_async]
|
||||||
async fn write_reg_controll_2(
|
async fn write_reg_controll_2(
|
||||||
&mut self,
|
&mut self,
|
||||||
value: Control2RegisterFlags,
|
value: Control2RegisterFlags,
|
||||||
@@ -481,11 +506,13 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
|
#[maybe_async]
|
||||||
async fn write_register(&mut self, reg: u8, value: u8) -> Result<(), Error<I::Error>> {
|
async fn write_register(&mut self, reg: u8, value: u8) -> Result<(), Error<I::Error>> {
|
||||||
self.i2c.write(DEFAULT_ADDRESS, &[reg, value]).await?;
|
self.i2c.write(DEFAULT_ADDRESS, &[reg, value]).await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[maybe_async]
|
||||||
async fn read_register(&mut self, reg: u8) -> Result<u8, Error<I::Error>> {
|
async fn read_register(&mut self, reg: u8) -> Result<u8, Error<I::Error>> {
|
||||||
let mut data = [0u8; 1];
|
let mut data = [0u8; 1];
|
||||||
self.i2c
|
self.i2c
|
||||||
@@ -495,6 +522,7 @@ where
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
|
#[maybe_async]
|
||||||
async fn read_registers(&mut self, reg: u8, buffer: &mut [u8]) -> Result<(), Error<I::Error>> {
|
async fn read_registers(&mut self, reg: u8, buffer: &mut [u8]) -> Result<(), Error<I::Error>> {
|
||||||
self.i2c.write_read(DEFAULT_ADDRESS, &[reg], buffer).await?;
|
self.i2c.write_read(DEFAULT_ADDRESS, &[reg], buffer).await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|||||||
Reference in New Issue
Block a user