Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[enh] ported to v1.0.0 of embedded-hal #19

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions .github/workflows/vl6180x-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,22 @@ name: vl6180x-ci
on:
push:
branches:
- '**'
- "**"
tags:
- "*.*.*"
schedule:
- cron: 0 0 * * 0

env:
RUSTFLAGS: '--deny warnings'
RUSTFLAGS: "--deny warnings"

jobs:
build:
name: Build
runs-on: ubuntu-latest
strategy:
matrix:
rust: [stable, beta, nightly, 1.52.1, 1.49.0]
rust: [stable, beta, nightly, 1.70.0]
TARGET:
- x86_64-unknown-linux-gnu
- x86_64-unknown-linux-musl
Expand Down Expand Up @@ -47,9 +47,9 @@ jobs:
- name: Checkout CI scripts
uses: actions/checkout@v2
with:
repository: 'eldruin/rust-driver-ci-scripts'
ref: 'master'
path: 'ci'
repository: "eldruin/rust-driver-ci-scripts"
ref: "master"
path: "ci"

- run: ./ci/patch-no-std.sh
if: ${{ ! contains(matrix.TARGET, 'x86_64') }}
Expand Down
17 changes: 10 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
[package]
name = "vl6180x"
version = "0.2.0"
version = "1.0.0"
description = "A rust driver for the VL6180X (Time-of-Flight I2C laser-ranging module)"
authors = ["Luca Zulian <[email protected]>", "Shao Yuan <[email protected]>"]
authors = [
"Luca Zulian <[email protected]>",
"Shao Yuan <[email protected]>",
]
categories = ["embedded", "hardware-support", "no-std"]
keywords = ["hal", "IO", "embedded-hal-driver", "vl6180x", "tof"]
keywords = ["hal", "IO", "embedded-hal-driver", "vl6180x", "tof"]
license = "MIT"
readme = "README.md"
repository = "https://github.com/lucazulian/vl6180x"
edition = "2018"
exclude = [ "doc", "*.jpg", "*.png", "*.bmp" ]
exclude = ["doc", "*.jpg", "*.png", "*.bmp"]

[dependencies]
embedded-hal = {version = "0.2.7", features = ["unproven"]}
int-enum = {version = "0.4.0", default-features = false}
embedded-hal = "1.0.0"
int-enum = { version = "1.1.2", default-features = false }

[profile.release]
codegen-units = 1
debug = true
lto = true
lto = true
1 change: 0 additions & 1 deletion src/config/config_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,3 @@ fn set_range_max_convergence_time_value_valid() {
let mut config = Config::new();
assert_eq!(config.set_range_max_convergence_time(20), Ok(()))
}

7 changes: 2 additions & 5 deletions src/device_status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@ use crate::{
error::{Error, Error2},
register::{Register8Bit::*, SysInterruptClearCode},
};
use embedded_hal::{
blocking::i2c::{Write, WriteRead},
digital::v2::OutputPin,
};
use embedded_hal::{digital::OutputPin, i2c::I2c};

impl<MODE, I2C, E> VL6180X<MODE, I2C>
where
I2C: WriteRead<Error = E> + Write<Error = E>,
I2C: I2c<Error = E>,
{
pub(crate) fn read_model_id_direct(&mut self) -> Result<u8, Error<E>> {
let id = self.read_named_register(IDENTIFICATION__MODEL_ID)?;
Expand Down
2 changes: 1 addition & 1 deletion src/i2c_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::register::{Register16Bit, Register8Bit};

impl<MODE, I2C, E> VL6180X<MODE, I2C>
where
I2C: WriteRead<Error = E> + Write<Error = E>,
I2C: I2c<Error = E>,
{
/// Reads a named 8-bit register
pub(crate) fn read_named_register(&mut self, reg: Register8Bit) -> Result<u8, E> {
Expand Down
4 changes: 2 additions & 2 deletions src/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ use crate::register::{
Register16Bit::*, Register8Bit::*, SysModeGpio1Polarity, SysModeGpio1Select,
AMBIENT_ANALOGUE_GAIN_CODE, RANGE_SCALAR_CODE,
};
use embedded_hal::blocking::i2c::{Write, WriteRead};
use embedded_hal::i2c::I2c;

impl<MODE, I2C, E> VL6180X<MODE, I2C>
where
I2C: WriteRead<Error = E> + Write<Error = E>,
I2C: I2c<Error = E>,
{
/// Initialize sensor with settings from ST application note AN4545,
/// section "SR03 settings" - "Mandatory : private registers"
Expand Down
12 changes: 6 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
//!
//! for more examples please see [vl6180x_stm32f401_examples](https://github.com/shaoyuancc/vl6180x_stm32f401_examples)
//!
//! ```rust
//! ```ignore
//! #![no_std]
//! #![no_main]
//!
Expand Down Expand Up @@ -37,7 +37,7 @@
//! .set_open_drain();
//! let i2c = dp.I2C1.i2c((scl, sda), 400.kHz(), &clocks);
//!
//! //! To create sensor with default configuration:
//! // To create sensor with default configuration:
//! let mut tof = vl6180x::VL6180X::new(i2c).expect("vl");
//!
//! loop {
Expand Down Expand Up @@ -74,8 +74,8 @@
#![allow(dead_code)]
pub use crate::register::ResultInterruptStatusGpioCode;
pub use config::*;
use embedded_hal::blocking::i2c::{Write, WriteRead};
use embedded_hal::digital::v2::{InputPin, OutputPin};
use embedded_hal::digital::{InputPin, OutputPin};
use embedded_hal::i2c::I2c;
pub use error::Error;
pub use mode::*;
mod config;
Expand All @@ -90,15 +90,15 @@ mod start_stop_measurements;

/// VL6180 interface
#[derive(Debug, Clone, Copy)]
pub struct VL6180X<MODE, I2C: Write + WriteRead> {
pub struct VL6180X<MODE, I2C: I2c> {
mode: MODE,
com: I2C,
config: Config,
}

/// Convenience container for VL6180, x_shutdown_pin and interrupt_pin
#[derive(Debug, Clone, Copy)]
pub struct VL6180XwPins<MODE, I2C: Write + WriteRead, OP: OutputPin, IP: InputPin> {
pub struct VL6180XwPins<MODE, I2C: I2c, OP: OutputPin, IP: InputPin> {
/// VL6180
pub vl6180x: VL6180X<MODE, I2C>,
/// X Shutdown Pin, output high => powered on, output low => powered off.
Expand Down
14 changes: 7 additions & 7 deletions src/mode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ mod ready;

pub use continuous::*;
pub use dynamic::*;
use embedded_hal::blocking::i2c::{Write, WriteRead};
use embedded_hal::digital::v2::OutputPin;
use embedded_hal::digital::OutputPin;
use embedded_hal::i2c::I2c;
pub use powered_off::*;
pub use ready::*;

Expand All @@ -15,7 +15,7 @@ use crate::VL6180X;

impl<MODE, I2C, E> VL6180X<MODE, I2C>
where
I2C: WriteRead<Error = E> + Write<Error = E>,
I2C: I2c<Error = E>,
{
fn into_mode<MODE2>(self, mode: MODE2) -> VL6180X<MODE2, I2C> {
VL6180X {
Expand All @@ -42,7 +42,7 @@ pub trait AllowStartRangeSingle {}

impl<MODE, I2C, E> VL6180X<MODE, I2C>
where
I2C: WriteRead<Error = E> + Write<Error = E>,
I2C: I2c<Error = E>,
MODE: AllowReadMeasurement,
{
/// Blocking read of the range mesurement.
Expand Down Expand Up @@ -87,7 +87,7 @@ where

impl<MODE, I2C, E> VL6180X<MODE, I2C>
where
I2C: WriteRead<Error = E> + Write<Error = E>,
I2C: I2c<Error = E>,
MODE: AllowStartAmbientSingle,
{
/// Trigger ambient light measurement in a non-blocking way.
Expand All @@ -108,7 +108,7 @@ where

impl<MODE, I2C, E> VL6180X<MODE, I2C>
where
I2C: WriteRead<Error = E> + Write<Error = E>,
I2C: I2c<Error = E>,
MODE: AllowStartRangeSingle,
{
/// Trigger range mesurement in a non-blocking way.
Expand All @@ -127,7 +127,7 @@ where

impl<MODE, I2C, E> VL6180X<MODE, I2C>
where
I2C: WriteRead<Error = E> + Write<Error = E>,
I2C: I2c<Error = E>,
MODE: AllowCommunication,
{
/// Read the model id of the sensor. Should return 0xB4.
Expand Down
8 changes: 4 additions & 4 deletions src/mode/continuous.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{error::Error, AllowCommunication, VL6180X};
use embedded_hal::blocking::i2c::{Write, WriteRead};
use embedded_hal::i2c::I2c;

use super::{AllowReadMeasurement, AllowStartAmbientSingle, AllowStartRangeSingle, ReadyMode};

Expand All @@ -15,7 +15,7 @@ impl AllowCommunication for RangeContinuousMode {}

impl<I2C, E> VL6180X<RangeContinuousMode, I2C>
where
I2C: WriteRead<Error = E> + Write<Error = E>,
I2C: I2c<Error = E>,
{
/// Stops range continuous mode.
pub fn stop_range_continuous_mode(mut self) -> Result<VL6180X<ReadyMode, I2C>, Error<E>> {
Expand All @@ -36,7 +36,7 @@ impl AllowCommunication for AmbientContinuousMode {}

impl<I2C, E> VL6180X<AmbientContinuousMode, I2C>
where
I2C: WriteRead<Error = E> + Write<Error = E>,
I2C: I2c<Error = E>,
{
/// Stops ambient continuous mode.
pub fn stop_ambient_continuous_mode(mut self) -> Result<VL6180X<ReadyMode, I2C>, Error<E>> {
Expand All @@ -58,7 +58,7 @@ impl AllowCommunication for InterleavedContinuousMode {}

impl<I2C, E> VL6180X<InterleavedContinuousMode, I2C>
where
I2C: WriteRead<Error = E> + Write<Error = E>,
I2C: I2c<Error = E>,
{
/// Stops interleaved continuous mode.
pub fn stop_interleaved_continuous_mode(mut self) -> Result<VL6180X<ReadyMode, I2C>, Error<E>> {
Expand Down
7 changes: 2 additions & 5 deletions src/mode/dynamic.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
use crate::error::{Error, Error2};
use crate::VL6180X;
use embedded_hal::{
blocking::i2c::{Write, WriteRead},
digital::v2::OutputPin,
};
use embedded_hal::{digital::OutputPin, i2c::I2c};
use OperatingMode::*;

/// A mode where the state is kept track of at runtime, instead of being
Expand Down Expand Up @@ -41,7 +38,7 @@ impl DynamicMode {

impl<I2C, E> VL6180X<DynamicMode, I2C>
where
I2C: WriteRead<Error = E> + Write<Error = E>,
I2C: I2c<Error = E>,
{
/// Same functionality as [`poll_range_mm_single_blocking()`](VL6180X::poll_range_mm_single_blocking)
/// but with a check on the current [OperatingMode].
Expand Down
7 changes: 2 additions & 5 deletions src/mode/powered_off.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use embedded_hal::{
blocking::i2c::{Write, WriteRead},
digital::v2::OutputPin,
};
use embedded_hal::{digital::OutputPin, i2c::I2c};

use crate::{error::Error2, mode::ReadyMode, VL6180X};

Expand All @@ -11,7 +8,7 @@ pub struct PoweredOffMode {}

impl<I2C, E> VL6180X<PoweredOffMode, I2C>
where
I2C: WriteRead<Error = E> + Write<Error = E>,
I2C: I2c<Error = E>,
{
/// Powers on the sensor by setting the `x_shutdown_pin` high.
/// It then busy waits for the device to be booted and initializes the device.
Expand Down
4 changes: 2 additions & 2 deletions src/mode/ready.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{error::Error, Config};
use crate::{AllowCommunication, VL6180X};
use embedded_hal::blocking::i2c::{Write, WriteRead};
use embedded_hal::i2c::I2c;

use super::{
AllowReadMeasurement, AllowStartAmbientSingle, AllowStartRangeSingle, AmbientContinuousMode,
Expand All @@ -21,7 +21,7 @@ impl AllowStartAmbientSingle for ReadyMode {}

impl<I2C, E> VL6180X<ReadyMode, I2C>
where
I2C: WriteRead<Error = E> + Write<Error = E>,
I2C: I2c<Error = E>,
{
/// Create a new VL6180X driver
pub fn new(i2c: I2C) -> Result<Self, Error<E>> {
Expand Down
8 changes: 4 additions & 4 deletions src/read_measurements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ use crate::{
},
VL6180X,
};
use embedded_hal::blocking::i2c::{Write, WriteRead};
use embedded_hal::i2c::I2c;

impl<MODE, I2C, E> VL6180X<MODE, I2C>
where
I2C: WriteRead<Error = E> + Write<Error = E>,
I2C: I2c<Error = E>,
{
pub(crate) fn read_range_mm_blocking_direct(&mut self) -> Result<u16, Error<E>> {
let mut c = 0;
Expand Down Expand Up @@ -42,7 +42,7 @@ where
}

fn get_range_val_and_status(&mut self) -> Result<u16, Error<E>> {
let status = self.read_named_register(Register8Bit::RESULT__RANGE_STATUS)?;
let status = self.read_named_register(Register8Bit::RESULT__RANGE_STATUS)? >> 4;
self.clear_range_interrupt_direct()?;
let error = RangeStatusErrorCode::try_from(status)
.map_err(|_| Error::UnknownRegisterCode(status))?;
Expand Down Expand Up @@ -108,7 +108,7 @@ where
}

fn get_ambient_val_and_status(&mut self) -> Result<u16, Error<E>> {
let status = self.read_named_register(Register8Bit::RESULT__ALS_STATUS)?;
let status = self.read_named_register(Register8Bit::RESULT__ALS_STATUS)? >> 4;
self.clear_ambient_interrupt_direct()?;
let error = AmbientStatusErrorCode::try_from(status)
.map_err(|_| Error::UnknownRegisterCode(status))?;
Expand Down
16 changes: 1 addition & 15 deletions src/register.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use core::convert::TryFrom;
use int_enum::{IntEnum, IntEnumError};
use int_enum::IntEnum;

#[cfg(test)]
mod register_tests;
Expand Down Expand Up @@ -246,13 +245,6 @@ impl RangeStatusErrorCode {
}
}

impl TryFrom<u8> for RangeStatusErrorCode {
type Error = IntEnumError<Self>;
fn try_from(code: u8) -> Result<Self, Self::Error> {
RangeStatusErrorCode::from_int(code >> 4)
}
}

/// Errors from performing an ambient light measurement
/// See VL6180X datasheet section 6.2.38 RESULT__ALS_STATUS
// Bits 7:4 of what is returned from the register
Expand All @@ -273,12 +265,6 @@ impl AmbientStatusErrorCode {
}
}

impl TryFrom<u8> for AmbientStatusErrorCode {
type Error = IntEnumError<Self>;
fn try_from(code: u8) -> Result<Self, Self::Error> {
AmbientStatusErrorCode::from_int(code >> 4)
}
}
// RANGE_SCALER values for 1x, 2x, 3x scaling - see STSW-IMG003 core/src/vl6180x_api.c (ScalerLookUP[])
pub const RANGE_SCALAR_CODE: [u16; 4] = [0, 253, 127, 84];
/// See datasheet 2.10.6 for more details
Expand Down
Loading