Skip to content

Commit a65cd09

Browse files
authored
Improve documentation for BitrateBuilder (#102)
* add example * add to docs * add missing trait
1 parent c0facb6 commit a65cd09

6 files changed

Lines changed: 94 additions & 0 deletions

File tree

.github/workflows/linux-windows.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,5 @@ jobs:
1919
sudo apt-get -y install clang gcc-mingw-w64-x86-64 libclang-dev
2020
- name: Build
2121
run: cargo build --verbose --target=x86_64-pc-windows-gnu --features=all
22+
- name: Build Examples
23+
run: cargo build --verbose --target=x86_64-pc-windows-gnu --features=all --examples

.github/workflows/linux.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,7 @@ jobs:
1919
scripts/set_up_vcan.sh
2020
- name: Build
2121
run: cargo build --verbose --features=serde
22+
- name: Build Examples
23+
run: cargo build --verbose --features=serde --examples
2224
- name: Run tests
2325
run: cargo test --features=test-vcan,serde --verbose

.github/workflows/macos.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,7 @@ jobs:
1313
- uses: actions/checkout@v3
1414
- name: Build
1515
run: cargo build --verbose --features=serde
16+
- name: Build Examples
17+
run: cargo build --verbose --features=serde --examples
1618
- name: Run tests
1719
run: cargo test --features=serde --verbose

.github/workflows/windows.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,7 @@ jobs:
1313
- uses: actions/checkout@v3
1414
- name: Build
1515
run: cargo build --verbose --features=all
16+
- name: Build Examples
17+
run: cargo build --verbose --features=all --examples
1618
- name: Run tests
1719
run: cargo test --features=all --verbose

examples/configure_bitrate.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#[cfg(all(target_os = "windows", feature = "vector-xl"))]
2+
pub async fn run() -> automotive::Result<()> {
3+
use automotive::can::bitrate::BitrateBuilder;
4+
use automotive::vector::VectorCan;
5+
use automotive::StreamExt;
6+
7+
tracing_subscriber::fmt::init();
8+
9+
let bitrate_cfg = BitrateBuilder::new::<VectorCan>()
10+
.bitrate(500_000)
11+
.sample_point(0.8)
12+
.data_bitrate(2_000_000)
13+
.data_sample_point(0.8)
14+
.build()
15+
.unwrap();
16+
17+
let adapter = VectorCan::new_async(0, &Some(bitrate_cfg.into()))?;
18+
let mut stream = adapter.recv();
19+
20+
while let Some(frame) = stream.next().await {
21+
println!("{:?}", frame);
22+
}
23+
24+
Ok(())
25+
}
26+
27+
#[cfg(all(target_os = "windows", feature = "vector-xl"))]
28+
#[tokio::main]
29+
async fn main() -> automotive::Result<()> {
30+
run().await
31+
}
32+
33+
#[cfg(not(all(target_os = "windows", feature = "vector-xl")))]
34+
fn main() {
35+
eprintln!("This example requires Windows and the `vector-xl` feature.");
36+
}

src/can/bitrate.rs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,56 @@
1515
//! The resulting [`BitrateConfig`] contains:
1616
//! - nominal/arbitration phase settings as one [`AdapterBitTiming`]
1717
//! - optional CAN-FD data phase settings as one [`AdapterBitTiming`]
18+
//!
19+
//! ## Example
20+
//!
21+
//! Build a 500 kbit/s nominal bitrate configuration using the timing constants
22+
//! exposed by an adapter type:
23+
//!
24+
//! ```rust
25+
//! use automotive::can::bitrate::{AdapterTimingConst, BitTimingConst, BitrateBuilder};
26+
//! use automotive::can::{CanAdapter, Frame};
27+
//! use std::collections::VecDeque;
28+
//!
29+
//! # const TIMING: AdapterTimingConst = AdapterTimingConst {
30+
//! # nominal: BitTimingConst {
31+
//! # clock_hz: 80_000_000,
32+
//! # tseg1_min: 1,
33+
//! # tseg1_max: 16,
34+
//! # tseg2_min: 1,
35+
//! # tseg2_max: 8,
36+
//! # sjw_max: 4,
37+
//! # brp_min: 1,
38+
//! # brp_max: 1024,
39+
//! # brp_inc: 1,
40+
//! # },
41+
//! # data: None,
42+
//! # };
43+
//! # struct DummyAdapter;
44+
//! # impl CanAdapter for DummyAdapter {
45+
//! # fn send(&mut self, _frames: &mut VecDeque<Frame>) -> automotive::Result<()> {
46+
//! # unreachable!()
47+
//! # }
48+
//! #
49+
//! # fn recv(&mut self) -> automotive::Result<Vec<Frame>> {
50+
//! # unreachable!()
51+
//! # }
52+
//! #
53+
//! # fn timing_const() -> AdapterTimingConst
54+
//! # where
55+
//! # Self: Sized,
56+
//! # {
57+
//! # TIMING
58+
//! # }
59+
//! # }
60+
//! let bitrate_cfg = BitrateBuilder::new::<DummyAdapter>()
61+
//! .bitrate(500_000)
62+
//! .sample_point(0.8)
63+
//! .build()?;
64+
//!
65+
//! assert_eq!(bitrate_cfg.nominal.bitrate, 500_000);
66+
//! # Ok::<(), automotive::can::bitrate::BitrateError>(())
67+
//! ```
1868
1969
use thiserror::Error;
2070

0 commit comments

Comments
 (0)