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

feat: add defmt and add rust-version (MSRV) of 1.62 #29

Merged
merged 9 commits into from
Jan 26, 2024
Merged
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
87 changes: 56 additions & 31 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,38 +10,63 @@ env:
CARGO_TERM_COLOR: always

jobs:
build:
lints-and-checks:
name: Lints and checks
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
submodules: true
- uses: dtolnay/rust-toolchain@nightly
with:
components: rustfmt, clippy, rust-docs

- name: Rustfmt lints
run: cargo fmt --all -- --check

- name: Clippy lints
run: cargo clippy --no-deps -- -D warnings

- name: Build docs
run: RUSTDOCFLAGS="--cfg docsrs" cargo +nightly doc --all-features --no-deps

build:
runs-on: ubuntu-latest
strategy:
matrix:
rust:
- stable
- beta
- nightly
- "1.65" # MSRV

steps:
- name: Install Rust (thumbv7em)
uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: nightly
override: true
target: thumbv7em-none-eabihf
components: clippy
- name: Checkout Sources
uses: actions/checkout@v2
- name: Build - default features
run: cargo build
- name: Build - std
run: cargo build --features "std"
- name: Build - serde
run: cargo build --features "serde"
- name: Build - examples
run: cargo build --examples
- name: Test - docs
run: cargo test --doc
- name: Clippy
uses: actions-rs/clippy-check@v1
with:
token: ${{ secrets.GITHUB_TOKEN }}
args: --all-features
- name: Fuzz
run: |
cargo install cargo-fuzz
cargo fuzz run init -- -max_total_time=900
cargo fuzz run calibrate -- -max_total_time=1800
- name: Checkout Sources
uses: actions/checkout@v4
- name: Install Rust (thumbv7em)
uses: dtolnay/rust-toolchain@master
with:
toolchain: ${{ matrix.rust }}
target: thumbv7em-none-eabihf
- name: Build - default features
run: cargo build
- name: Build - std
run: cargo build --features "std"
- name: Build - serde
run: cargo build --features "serde"
- name: Build - defmt-03
run: cargo build --features "defmt-03"
- name: Build - examples
run: cargo build --examples
- name: Run tests
run: cargo test
- name: Install cargo-fuzz
# pre-build binaries
uses: taiki-e/install-action@v2
with:
tool: cargo-fuzz
- name: Fuzz (nightly only)
if: ${{ matrix.rust == 'nightly' }}
run: |
cargo +nightly fuzz run init -- -max_total_time=900
cargo +nightly fuzz run calibrate -- -max_total_time=1800
8 changes: 8 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ keywords = [
license-file = "LICENSE"
readme = "README.md"

# linux-embedded-hal requires 1.65 because of nix
rust-version = "1.65"

[dependencies]
byteorder = { version = "1", default-features = false }
serde = { version = "1.0", default-features = false, features = ["derive"], optional = true }
Expand All @@ -25,11 +28,16 @@ bitflags = "2"
num-traits = { version = "0.2.15", default-features = false, features = ["libm"] }
num-derive = "0.4.1"

defmt = { version = "0.3", optional = true }

embedded-hal = { version = "1.0" }

[features]
default = []
std = []

defmt-03 = ["dep:defmt", "embedded-hal/defmt-03"]
serde = ["dep:serde", "mint/serde"]

[dev-dependencies]
linux-embedded-hal = "0.4"
3 changes: 2 additions & 1 deletion examples/calibrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ fn main() {
let dev = I2cdev::new("/dev/i2c-0").unwrap();
let mut delay = Delay {};
let mut imu = Bno055::new(dev).with_alternative_address();
imu.init(&mut delay).expect("An error occurred while building the IMU");
imu.init(&mut delay)
.expect("An error occurred while building the IMU");

imu.set_mode(BNO055OperationMode::NDOF, &mut delay)
.expect("An error occurred while setting the IMU mode");
Expand Down
12 changes: 12 additions & 0 deletions src/acc_config.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
use num_derive::FromPrimitive;
use num_traits::FromPrimitive;

#[allow(clippy::unusual_byte_groupings)]
const ACC_G_RANGE_MASK: u8 = 0b000_000_11;
#[allow(clippy::unusual_byte_groupings)]
const ACC_BANDWIDTH_MASK: u8 = 0b000_111_00;
#[allow(clippy::unusual_byte_groupings)]
const ACC_OPERATION_MODE_MASK: u8 = 0b111_000_00;

#[derive(Debug)]
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
#[allow(clippy::enum_variant_names)]
pub enum Error {
BadAccGRange,
BadAccBandwidth,
BadAccOperationMode,
}

#[derive(Debug, Clone, Copy, FromPrimitive)]
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
#[repr(u8)]
#[allow(clippy::unusual_byte_groupings)]
pub enum AccGRange {
G2 = 0b000_000_00,
G4 = 0b000_000_01,
Expand All @@ -22,7 +29,9 @@ pub enum AccGRange {
}

#[derive(Debug, Clone, Copy, FromPrimitive)]
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
#[repr(u8)]
#[allow(clippy::unusual_byte_groupings)]
pub enum AccBandwidth {
Hz7_81 = 0b000_000_00,
Hz15_63 = 0b000_001_00,
Expand All @@ -35,7 +44,9 @@ pub enum AccBandwidth {
}

#[derive(Debug, Clone, Copy, FromPrimitive)]
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
#[repr(u8)]
#[allow(clippy::unusual_byte_groupings)]
pub enum AccOperationMode {
Normal = 0b000_000_00,
Suspend = 0b001_000_00,
Expand All @@ -46,6 +57,7 @@ pub enum AccOperationMode {
}

#[derive(Debug, Clone)]
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
pub struct AccConfig {
g_range: AccGRange,
bandwidth: AccBandwidth,
Expand Down
34 changes: 24 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
#![doc(html_root_url = "https://docs.rs/bno055/0.3.3")]
#![cfg_attr(not(feature = "std"), no_std)]
#![allow(clippy::bad_bit_mask)]

///! Bosch Sensortec BNO055 9-axis IMU sensor driver.
///! Datasheet: https://ae-bst.resource.bosch.com/media/_tech/media/datasheets/BST-BNO055-DS000.pdf
//! Bosch Sensortec BNO055 9-axis IMU sensor driver.
//! Datasheet: https://ae-bst.resource.bosch.com/media/_tech/media/datasheets/BST-BNO055-DS000.pdf
use embedded_hal::{
delay::DelayNs,
i2c::{I2c, SevenBitAddress},
};

#[cfg(not(feature = "defmt-03"))]
use bitflags::bitflags;
#[cfg(feature = "defmt-03")]
use defmt::bitflags;

use byteorder::{ByteOrder, LittleEndian};
pub use mint;
#[cfg(feature = "serde")]
Expand All @@ -24,6 +29,7 @@ pub use regs::BNO055_ID;

/// All possible errors in this crate
#[derive(Debug)]
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
pub enum Error<E> {
/// I2C bus error
I2c(E),
Expand All @@ -38,6 +44,7 @@ pub enum Error<E> {
AccConfig(acc_config::Error),
}

#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
pub struct Bno055<I> {
i2c: I,
pub mode: BNO055OperationMode,
Expand Down Expand Up @@ -654,14 +661,15 @@ where
}

bitflags! {
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(not(feature = "defmt-03"), derive(Debug, Clone, Copy, PartialEq, Eq))]
pub struct BNO055AxisConfig: u8 {
const AXIS_AS_X = 0b00;
const AXIS_AS_Y = 0b01;
const AXIS_AS_Z = 0b10;
}
}

#[allow(clippy::misnamed_getters)]
impl AxisRemap {
pub fn x(&self) -> BNO055AxisConfig {
self.x
Expand All @@ -683,6 +691,7 @@ pub struct AxisRemap {
z: BNO055AxisConfig,
}

#[derive(Debug)]
pub struct AxisRemapBuilder {
remap: AxisRemap,
}
Expand Down Expand Up @@ -755,6 +764,7 @@ impl AxisRemapBuilder {
self.remap.x == self.remap.y || self.remap.y == self.remap.z || self.remap.z == self.remap.x
}

#[allow(clippy::result_unit_err)]
pub fn build(self) -> Result<AxisRemap, ()> {
if self.is_invalid() {
Err(())
Expand All @@ -765,7 +775,7 @@ impl AxisRemapBuilder {
}

bitflags! {
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(not(feature = "defmt-03"), derive(Debug, Clone, Copy, PartialEq, Eq))]
pub struct BNO055AxisSign: u8 {
const X_NEGATIVE = 0b100;
const Y_NEGATIVE = 0b010;
Expand All @@ -774,7 +784,7 @@ bitflags! {
}

bitflags! {
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(not(feature = "defmt-03"), derive(Debug, Clone, Copy, PartialEq, Eq))]
pub struct BNO055SystemStatusCode: u8 {
const SYSTEM_IDLE = 0;
const SYSTEM_ERROR = 1;
Expand All @@ -788,7 +798,7 @@ bitflags! {

bitflags! {
/// Possible BNO055 errors.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(not(feature = "defmt-03"), derive(Debug, Clone, Copy, PartialEq, Eq))]
pub struct BNO055SystemErrorCode: u8 {
const NONE = 0;
const PERIPHERAL_INIT = 1;
Expand All @@ -806,7 +816,7 @@ bitflags! {

bitflags! {
/// BNO055 self-test status bit flags.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(not(feature = "defmt-03"), derive(Debug, Clone, Copy, PartialEq, Eq))]
pub struct BNO055SelfTestStatus: u8 {
const ACC_OK = 0b0001;
const MAG_OK = 0b0010;
Expand All @@ -816,13 +826,15 @@ bitflags! {
}

#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
pub struct BNO055SystemStatus {
status: BNO055SystemStatusCode,
selftest: Option<BNO055SelfTestStatus>,
error: BNO055SystemErrorCode,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
pub struct BNO055Revision {
pub software: u16,
pub bootloader: u8,
Expand All @@ -833,6 +845,7 @@ pub struct BNO055Revision {

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
#[repr(C)]
pub struct BNO055Calibration {
pub acc_offset_x_lsb: u8,
Expand Down Expand Up @@ -881,6 +894,7 @@ impl BNO055Calibration {
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
pub struct BNO055CalibrationStatus {
pub sys: u8,
pub gyr: u8,
Expand All @@ -890,7 +904,7 @@ pub struct BNO055CalibrationStatus {

bitflags! {
/// Possible BNO055 register map pages.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(not(feature = "defmt-03"), derive(Debug, Clone, Copy, PartialEq, Eq))]
pub struct BNO055RegisterPage: u8 {
const PAGE_0 = 0;
const PAGE_1 = 1;
Expand All @@ -899,7 +913,7 @@ bitflags! {

bitflags! {
/// Possible BNO055 power modes.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(not(feature = "defmt-03"), derive(Debug, Clone, Copy, PartialEq, Eq))]
pub struct BNO055PowerMode: u8 {
const NORMAL = 0b00;
const LOW_POWER = 0b01;
Expand All @@ -909,7 +923,7 @@ bitflags! {

bitflags! {
/// Possible BNO055 operation modes.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(not(feature = "defmt-03"), derive(Debug, Clone, Copy, PartialEq, Eq))]
pub struct BNO055OperationMode: u8 {
const CONFIG_MODE = 0b0000;
const ACC_ONLY = 0b0001;
Expand Down