|
19 | 19 | - [Removed blanket implementations](#removed-blanket-implementations)
|
20 | 20 | - [Cargo Features](#cargo-features)
|
21 | 21 | - [Companion crates](#companion-crates)
|
| 22 | +- [Supporting both 0.2 and 1.0 in the same HAL](#supporting-both-02-and-10-in-the-same-hal) |
| 23 | +- [`embedded-hal-compat`](#embedded-hal-compat) |
22 | 24 |
|
23 | 25 | ## Overview and reasoning
|
24 | 26 |
|
@@ -372,3 +374,59 @@ The `embedded-hal` project now spans several crates, where some functionality ha
|
372 | 374 | | [embedded-io-async](./embedded-io-async) | [](https://crates.io/crates/embedded-io-async) | [](https://docs.rs/embedded-io-async) | I/O traits, async version |
|
373 | 375 | | [embedded-io-adapters](./embedded-io-adapters) | [](https://crates.io/crates/embedded-io-adapters) | [](https://docs.rs/embedded-io-adapters) | Adapters between the [`embedded-io`](https://crates.io/crates/embedded-io) and [`embedded-io-async`](https://crates.io/crates/embedded-io-async) traits and other IO traits (`std`, `tokio`, `futures`...) |
|
374 | 376 |
|
| 377 | +## Supporting both 0.2 and 1.0 in the same HAL |
| 378 | + |
| 379 | +It is strongly recommended that HAL implementation crates provide implementations for both the `embedded-hal` v0.2 and v1.0 traits. |
| 380 | +This allows users to use drivers using either version seamlessly. |
| 381 | + |
| 382 | +The way you do it is adding a dependency on both versions in `Cargo.toml` like this: |
| 383 | + |
| 384 | +```toml |
| 385 | +[dependencies] |
| 386 | +embedded-hal-02 = { package = "embedded-hal", version = "0.2.7", features = ["unproven"] } |
| 387 | +embedded-hal-1 = { package = "embedded-hal", version = "1.0" } |
| 388 | +``` |
| 389 | + |
| 390 | +This allows you to refer to the v0.2 traits under the `embedded_hal_02` name, and the v1.0 traits under |
| 391 | +`embedded_hal_1`. Implement both versions on the same struct. For example, for an input pin: |
| 392 | + |
| 393 | +```rust |
| 394 | +/// The HAL's input pin struct |
| 395 | +struct Input {...} |
| 396 | + |
| 397 | +/// Implement the v0.2 traits on the struct. |
| 398 | +impl embedded_hal_02::digital::v2::InputPin for Input { |
| 399 | + type Error = Infallible; |
| 400 | + |
| 401 | + fn is_high(&self) -> Result<bool, Self::Error> { |
| 402 | + ... |
| 403 | + } |
| 404 | + |
| 405 | + fn is_low(&self) -> Result<bool, Self::Error> { |
| 406 | + ... |
| 407 | + } |
| 408 | +} |
| 409 | + |
| 410 | +/// ... and implement the v1.0 traits on the *same* struct. |
| 411 | +impl embedded_hal_1::digital::ErrorType for Input { |
| 412 | + type Error = Infallible; |
| 413 | +} |
| 414 | + |
| 415 | +impl embedded_hal_1::digital::InputPin for Input { |
| 416 | + fn is_high(&mut self) -> Result<bool, Self::Error> { |
| 417 | + ... |
| 418 | + } |
| 419 | + |
| 420 | + fn is_low(&mut self) -> Result<bool, Self::Error> { |
| 421 | + ... |
| 422 | + } |
| 423 | +} |
| 424 | +``` |
| 425 | + |
| 426 | +## `embedded-hal-compat` |
| 427 | + |
| 428 | +For HAL implementation crates that haven't been updated yet, [embedded-hal-compat](https://github.com/ryankurte/embedded-hal-compat) |
| 429 | +provides shims to support interoperability between `embedded-hal` v0.2 and v1.0. |
| 430 | + |
| 431 | +This allows using a driver requiring v1.0 with a HAL crate implementing only v0.2 or vice-versa, (generally) without alteration. |
| 432 | +See the [docs](https://docs.rs/embedded-hal-compat/) for examples. |
0 commit comments