feat: add display UI with splash screen, dashboard, and RSSI sparkline

Implement full SSD1306 OLED display support:
- Splash screen on boot (1.5s) with project name, version, board
- Dashboard showing radio state, config, counters, RSSI/SNR
- RSSI history sparkline (64-sample ring buffer, 2px-wide bars)
- DisplayOn/DisplayOff host commands routed via dedicated channel
- Pure render functions against generic DrawTarget (no hardware coupling)
- Display on/off control bypasses radio task entirely

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
swaitsandClaude Opus 4.6 committed 2026-03-29 20:42:54 -06:00
1 parent 7064110293
commit 3102fc747a
17624 files changed
+4564928 -38

No files matched your search

+1
View File
@@ -0,0 +1 @@
{"files":{".cargo_vcs_info.json":"d7cc7ceae3328a3fbfd33378110fe6c86ec39c72859f35285acc93b60ee7b153","CHANGELOG.md":"665798d36ee15585cd5a50fc5ebce03cbae066e14ef397581a0c256e8e92719a","Cargo.toml":"edea7ccf675416764eb01eb7876cc01bdae884d7b73735442446d2db1a4ea58c","Cargo.toml.orig":"388c7b05dccc40f5b98d13fbdc540bc58251663f0c3df1a991f2c924a3e552b5","LICENSE-APACHE":"a60eea817514531668d7e00765731449fe14d059d3249e0bc93b36de45f759f2","LICENSE-MIT":"47674f8b7d98c232c6f81346c4cfe48933d913a9e257d7a522ad9f42e3dd61e1","README.md":"303227b055e59d35498fa32dcd4abd9db10b3b3620d31b2c08b8cbb94be2eef6","src/impls/boxx.rs":"929b293ed5f618bf5d8b813704e20ff8e27523f69aeeba8bdd613cc8150e474c","src/impls/mod.rs":"f7bd7d91e7b9a39347af01b625e8cc8980ef7e5f3f3217053bfb92b8cdc7c3e6","src/impls/slice_mut.rs":"90c0055b9b3ec46079fbb7068d17f3ba38eb5ad8a52834b32bf82a73ff52e269","src/impls/slice_ref.rs":"634531f47ebbb9e60cac801699e826d20dc495c50fd9d051e27dcff5d6b7cbcd","src/impls/vec.rs":"2b5b2ed982f00d5bde7a3773b282e37c09722895ce915102cc8ccd78e391e866","src/lib.rs":"0a5648db93fef28326c355a0d3639deb0ff8366fec034d765617d78faa4ccbcb"},"package":"edd0f118536f44f5ccd48bcb8b111bdc3de888b58c74639dfb034a357d0f206d"}
+6
View File
@@ -0,0 +1,6 @@
{
"git": {
"sha1": "244f2b929a5f51379e4c910d8ab6cfb58fe049f9"
},
"path_in_vcs": "embedded-io"
}
+54
View File
@@ -0,0 +1,54 @@
# Changelog
All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## 0.6.1 - 2023-10-22
- Make `SliceWriteError` publicly available.
## 0.6.0 - 2023-10-02
- Prohibit `Write::write` implementations returning `Ok(0)` unless there is no
data to write; consequently remove `WriteAllError` and the `WriteAllError`
variant of `WriteFmtError`. Update the `&mut [u8]` impl to possibly return
a new `SliceWriteError` if the slice is full instead of `Ok(0)`.
- Add `WriteZero` variant to `ErrorKind` for implementations that previously
may have returned `Ok(0)` to indicate no further data could be written.
- `Write::write_all` now panics if the `write()` implementation returns `Ok(0)`.
## 0.5.0 - 2023-08-06
- Add `ReadReady`, `WriteReady` traits. They allow peeking whether the I/O handle is ready to read/write, so they allow using the traits in a non-blocking way.
- Add variants to `ErrorKind` mirroring `std::io::ErrorKind`.
- Add `From` impls to convert between `ErrorKind` and `std::io::ErrorKind`.
- Moved `embedded_io::blocking` to the crate root.
- Split async traits to the `embedded-io-async` crate.
- Split trait adapters to the `embedded-io-adapters` crate.
- Add `std::error` impls for `ReadExactError` & `WriteAllError`.
- Rename trait `Io` to `ErrorType`, for consistency with `embedded-hal`.
- Added optional `defmt` 0.3 support.
## 0.4.0 - 2022-11-25
- Switch all traits to use [`async_fn_in_trait`](https://blog.rust-lang.org/inside-rust/2022/11/17/async-fn-in-trait-nightly.html) (AFIT). Requires `nightly-2022-11-22` or newer.
## 0.3.1 - 2022-10-26
- Fix compilation on recent nightlies (#5)
## 0.3.0 - 2022-05-19
- `FromFutures` adapter now requires `futures` Cargo feature. (breaking change)
- Add `FromTokio` adapter.
- Add blanket impls for `&mut T`, `Box<T>`.
- Add impl `Read`, `BufRead` for `&[u8]`
- Add impl `Write` for `&mut [u8]`
- Add impl `Write` for `Vec<u8>`
- impl `std::error::Error` for `ReadExactError`, `WriteFmtError`.
## 0.2.0 - 2022-05-07
- First release
+40
View File
@@ -0,0 +1,40 @@
# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO
#
# When uploading crates to the registry Cargo will automatically
# "normalize" Cargo.toml files for maximal compatibility
# with all versions of Cargo and also rewrite `path` dependencies
# to registry (e.g., crates.io) dependencies.
#
# If you are reading this file be aware that the original Cargo.toml
# will likely look very different (and much more reasonable).
# See Cargo.toml.orig for the original contents.
[package]
edition = "2021"
name = "embedded-io"
version = "0.6.1"
description = "Embedded IO traits"
readme = "README.md"
categories = [
"embedded",
"no-std",
]
license = "MIT OR Apache-2.0"
repository = "https://github.com/rust-embedded/embedded-hal"
[package.metadata.docs.rs]
features = ["std"]
rustdoc-args = [
"--cfg",
"docsrs",
]
[dependencies.defmt-03]
version = "0.3"
optional = true
package = "defmt"
[features]
alloc = []
defmt-03 = ["dep:defmt-03"]
std = ["alloc"]
+201
View File
@@ -0,0 +1,201 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
APPENDIX: How to apply the Apache License to your work.
To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.
Copyright [yyyy] [name of copyright owner]
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
+25
View File
@@ -0,0 +1,25 @@
Copyright (c) 2023 The embedded-io authors
Permission is hereby granted, free of charge, to any
person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the
Software without restriction, including without
limitation the rights to use, copy, modify, merge,
publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software
is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice
shall be included in all copies or substantial portions
of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
+49
View File
@@ -0,0 +1,49 @@
[![crates.io](https://img.shields.io/crates/d/embedded-io.svg)](https://crates.io/crates/embedded-io)
[![crates.io](https://img.shields.io/crates/v/embedded-io.svg)](https://crates.io/crates/embedded-io)
[![Documentation](https://docs.rs/embedded-io/badge.svg)](https://docs.rs/embedded-io)
# `embedded-io`
This project is developed and maintained by the [HAL team](https://github.com/rust-embedded/wg#the-hal-team).
Input/Output traits for embedded systems.
Rust's `std::io` traits are not available in `no_std` targets, mainly because `std::io::Error`
requires allocation. This crate contains replacement equivalent traits, usable in `no_std`
targets.
## Differences with `std::io`
- `Error` is an associated type. This allows each implementor to return its own error type,
while avoiding `dyn` or `Box`. This is consistent with how errors are handled in [`embedded-hal`](https://github.com/rust-embedded/embedded-hal/).
- In `std::io`, the `Read`/`Write` traits might be blocking or non-blocking (i.e. returning `WouldBlock` errors) depending on the file descriptor's mode, which is only known at run-time. This allows passing a non-blocking stream to code that expects a blocking
stream, causing unexpected errors. To solve this, `embedded-io` specifies `Read`/`Write` are always blocking, and adds new `ReadReady`/`WriteReady` traits to allow using streams in a non-blocking way.
## Optional Cargo features
- **`std`**: Adds `From` impls to convert to/from `std::io` structs, adds `std::error::Error` impls.
- **`alloc`**: Adds blanket impls for `Box`, adds `Write` impl to `Vec`.
- **`defmt-03`**: Derive `defmt::Format` from `defmt` 0.3 for enums and structs.
## Minimum Supported Rust Version (MSRV)
This crate is guaranteed to compile on stable Rust 1.60 and up. It *might*
compile with older versions but that may change in any new patch release.
See [here](../docs/msrv.md) for details on how the MSRV may be upgraded.
## License
Licensed under either of
- Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or
<http://www.apache.org/licenses/LICENSE-2.0>)
- MIT license ([LICENSE-MIT](LICENSE-MIT) or <http://opensource.org/licenses/MIT>)
at your option.
### Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted
for inclusion in the work by you, as defined in the Apache-2.0 license, shall be
dual licensed as above, without any additional terms or conditions.
+63
View File
@@ -0,0 +1,63 @@
use crate::{BufRead, ErrorType, Read, ReadReady, Seek, Write, WriteReady};
use alloc::boxed::Box;
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))]
impl<T: ?Sized + ErrorType> ErrorType for Box<T> {
type Error = T::Error;
}
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))]
impl<T: ?Sized + Read> Read for Box<T> {
#[inline]
fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> {
T::read(self, buf)
}
}
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))]
impl<T: ?Sized + BufRead> BufRead for Box<T> {
fn fill_buf(&mut self) -> Result<&[u8], Self::Error> {
T::fill_buf(self)
}
fn consume(&mut self, amt: usize) {
T::consume(self, amt)
}
}
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))]
impl<T: ?Sized + Write> Write for Box<T> {
#[inline]
fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
T::write(self, buf)
}
#[inline]
fn flush(&mut self) -> Result<(), Self::Error> {
T::flush(self)
}
}
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))]
impl<T: ?Sized + Seek> Seek for Box<T> {
#[inline]
fn seek(&mut self, pos: crate::SeekFrom) -> Result<u64, Self::Error> {
T::seek(self, pos)
}
}
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))]
impl<T: ?Sized + ReadReady> ReadReady for Box<T> {
#[inline]
fn read_ready(&mut self) -> Result<bool, Self::Error> {
T::read_ready(self)
}
}
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))]
impl<T: ?Sized + WriteReady> WriteReady for Box<T> {
#[inline]
fn write_ready(&mut self) -> Result<bool, Self::Error> {
T::write_ready(self)
}
}
+7
View File
@@ -0,0 +1,7 @@
mod slice_mut;
mod slice_ref;
#[cfg(feature = "alloc")]
mod boxx;
#[cfg(feature = "alloc")]
mod vec;
+51
View File
@@ -0,0 +1,51 @@
use crate::{Error, ErrorKind, ErrorType, SliceWriteError, Write};
use core::mem;
impl Error for SliceWriteError {
fn kind(&self) -> ErrorKind {
match self {
SliceWriteError::Full => ErrorKind::WriteZero,
}
}
}
impl ErrorType for &mut [u8] {
type Error = SliceWriteError;
}
impl core::fmt::Display for SliceWriteError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{self:?}")
}
}
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
impl std::error::Error for SliceWriteError {}
/// Write is implemented for `&mut [u8]` by copying into the slice, overwriting
/// its data.
///
/// Note that writing updates the slice to point to the yet unwritten part.
/// The slice will be empty when it has been completely overwritten.
///
/// If the number of bytes to be written exceeds the size of the slice, write operations will
/// return short writes: ultimately, a `SliceWriteError::Full`.
impl Write for &mut [u8] {
#[inline]
fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
let amt = core::cmp::min(buf.len(), self.len());
if !buf.is_empty() && amt == 0 {
return Err(SliceWriteError::Full);
}
let (a, b) = mem::take(self).split_at_mut(amt);
a.copy_from_slice(&buf[..amt]);
*self = b;
Ok(amt)
}
#[inline]
fn flush(&mut self) -> Result<(), Self::Error> {
Ok(())
}
}
+41
View File
@@ -0,0 +1,41 @@
use crate::{BufRead, ErrorType, Read};
impl ErrorType for &[u8] {
type Error = core::convert::Infallible;
}
/// Read is implemented for `&[u8]` by copying from the slice.
///
/// Note that reading updates the slice to point to the yet unread part.
/// The slice will be empty when EOF is reached.
impl Read for &[u8] {
#[inline]
fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> {
let amt = core::cmp::min(buf.len(), self.len());
let (a, b) = self.split_at(amt);
// First check if the amount of bytes we want to read is small:
// `copy_from_slice` will generally expand to a call to `memcpy`, and
// for a single byte the overhead is significant.
if amt == 1 {
buf[0] = a[0];
} else {
buf[..amt].copy_from_slice(a);
}
*self = b;
Ok(amt)
}
}
impl BufRead for &[u8] {
#[inline]
fn fill_buf(&mut self) -> Result<&[u8], Self::Error> {
Ok(*self)
}
#[inline]
fn consume(&mut self, amt: usize) {
*self = &self[amt..];
}
}
+21
View File
@@ -0,0 +1,21 @@
use crate::{ErrorType, Write};
use alloc::vec::Vec;
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))]
impl ErrorType for Vec<u8> {
type Error = core::convert::Infallible;
}
#[cfg_attr(docsrs, doc(cfg(any(feature = "std", feature = "alloc"))))]
impl Write for Vec<u8> {
#[inline]
fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
self.extend_from_slice(buf);
Ok(buf.len())
}
#[inline]
fn flush(&mut self) -> Result<(), Self::Error> {
Ok(())
}
}
+553
View File
@@ -0,0 +1,553 @@
#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(docsrs, feature(doc_cfg))]
#![warn(missing_docs)]
#![doc = include_str!("../README.md")]
use core::fmt;
// needed to prevent defmt macros from breaking, since they emit code that does `defmt::blahblah`.
#[cfg(feature = "defmt-03")]
use defmt_03 as defmt;
#[cfg(feature = "alloc")]
extern crate alloc;
mod impls;
/// Enumeration of possible methods to seek within an I/O object.
///
/// This is the `embedded-io` equivalent of [`std::io::SeekFrom`].
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
pub enum SeekFrom {
/// Sets the offset to the provided number of bytes.
Start(u64),
/// Sets the offset to the size of this object plus the specified number of bytes.
End(i64),
/// Sets the offset to the current position plus the specified number of bytes.
Current(i64),
}
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
impl From<SeekFrom> for std::io::SeekFrom {
fn from(pos: SeekFrom) -> Self {
match pos {
SeekFrom::Start(n) => std::io::SeekFrom::Start(n),
SeekFrom::End(n) => std::io::SeekFrom::End(n),
SeekFrom::Current(n) => std::io::SeekFrom::Current(n),
}
}
}
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
impl From<std::io::SeekFrom> for SeekFrom {
fn from(pos: std::io::SeekFrom) -> SeekFrom {
match pos {
std::io::SeekFrom::Start(n) => SeekFrom::Start(n),
std::io::SeekFrom::End(n) => SeekFrom::End(n),
std::io::SeekFrom::Current(n) => SeekFrom::Current(n),
}
}
}
/// Possible kinds of errors.
///
/// This list is intended to grow over time and it is not recommended to
/// exhaustively match against it. In application code, use `match` for the `ErrorKind`
/// values you are expecting; use `_` to match "all other errors".
///
/// This is the `embedded-io` equivalent of [`std::io::ErrorKind`], except with the following changes:
///
/// - `WouldBlock` is removed, since `embedded-io` traits are always blocking. See the [crate-level documentation](crate) for details.
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
#[non_exhaustive]
pub enum ErrorKind {
/// Unspecified error kind.
Other,
/// An entity was not found, often a file.
NotFound,
/// The operation lacked the necessary privileges to complete.
PermissionDenied,
/// The connection was refused by the remote server.
ConnectionRefused,
/// The connection was reset by the remote server.
ConnectionReset,
/// The connection was aborted (terminated) by the remote server.
ConnectionAborted,
/// The network operation failed because it was not connected yet.
NotConnected,
/// A socket address could not be bound because the address is already in
/// use elsewhere.
AddrInUse,
/// A nonexistent interface was requested or the requested address was not
/// local.
AddrNotAvailable,
/// The operation failed because a pipe was closed.
BrokenPipe,
/// An entity already exists, often a file.
AlreadyExists,
/// A parameter was incorrect.
InvalidInput,
/// Data not valid for the operation were encountered.
///
/// Unlike [`InvalidInput`], this typically means that the operation
/// parameters were valid, however the error was caused by malformed
/// input data.
///
/// For example, a function that reads a file into a string will error with
/// `InvalidData` if the file's contents are not valid UTF-8.
///
/// [`InvalidInput`]: ErrorKind::InvalidInput
InvalidData,
/// The I/O operation's timeout expired, causing it to be canceled.
TimedOut,
/// This operation was interrupted.
///
/// Interrupted operations can typically be retried.
Interrupted,
/// This operation is unsupported on this platform.
///
/// This means that the operation can never succeed.
Unsupported,
/// An operation could not be completed, because it failed
/// to allocate enough memory.
OutOfMemory,
/// An attempted write could not write any data.
WriteZero,
}
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
impl From<ErrorKind> for std::io::ErrorKind {
fn from(value: ErrorKind) -> Self {
match value {
ErrorKind::NotFound => std::io::ErrorKind::NotFound,
ErrorKind::PermissionDenied => std::io::ErrorKind::PermissionDenied,
ErrorKind::ConnectionRefused => std::io::ErrorKind::ConnectionRefused,
ErrorKind::ConnectionReset => std::io::ErrorKind::ConnectionReset,
ErrorKind::ConnectionAborted => std::io::ErrorKind::ConnectionAborted,
ErrorKind::NotConnected => std::io::ErrorKind::NotConnected,
ErrorKind::AddrInUse => std::io::ErrorKind::AddrInUse,
ErrorKind::AddrNotAvailable => std::io::ErrorKind::AddrNotAvailable,
ErrorKind::BrokenPipe => std::io::ErrorKind::BrokenPipe,
ErrorKind::AlreadyExists => std::io::ErrorKind::AlreadyExists,
ErrorKind::InvalidInput => std::io::ErrorKind::InvalidInput,
ErrorKind::InvalidData => std::io::ErrorKind::InvalidData,
ErrorKind::TimedOut => std::io::ErrorKind::TimedOut,
ErrorKind::Interrupted => std::io::ErrorKind::Interrupted,
ErrorKind::Unsupported => std::io::ErrorKind::Unsupported,
ErrorKind::OutOfMemory => std::io::ErrorKind::OutOfMemory,
_ => std::io::ErrorKind::Other,
}
}
}
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
impl From<std::io::ErrorKind> for ErrorKind {
fn from(value: std::io::ErrorKind) -> Self {
match value {
std::io::ErrorKind::NotFound => ErrorKind::NotFound,
std::io::ErrorKind::PermissionDenied => ErrorKind::PermissionDenied,
std::io::ErrorKind::ConnectionRefused => ErrorKind::ConnectionRefused,
std::io::ErrorKind::ConnectionReset => ErrorKind::ConnectionReset,
std::io::ErrorKind::ConnectionAborted => ErrorKind::ConnectionAborted,
std::io::ErrorKind::NotConnected => ErrorKind::NotConnected,
std::io::ErrorKind::AddrInUse => ErrorKind::AddrInUse,
std::io::ErrorKind::AddrNotAvailable => ErrorKind::AddrNotAvailable,
std::io::ErrorKind::BrokenPipe => ErrorKind::BrokenPipe,
std::io::ErrorKind::AlreadyExists => ErrorKind::AlreadyExists,
std::io::ErrorKind::InvalidInput => ErrorKind::InvalidInput,
std::io::ErrorKind::InvalidData => ErrorKind::InvalidData,
std::io::ErrorKind::TimedOut => ErrorKind::TimedOut,
std::io::ErrorKind::Interrupted => ErrorKind::Interrupted,
std::io::ErrorKind::Unsupported => ErrorKind::Unsupported,
std::io::ErrorKind::OutOfMemory => ErrorKind::OutOfMemory,
_ => ErrorKind::Other,
}
}
}
/// Error trait.
///
/// This trait allows generic code to do limited inspecting of errors,
/// to react differently to different kinds.
pub trait Error: fmt::Debug {
/// Get the kind of this error.
fn kind(&self) -> ErrorKind;
}
impl Error for core::convert::Infallible {
fn kind(&self) -> ErrorKind {
match *self {}
}
}
impl Error for ErrorKind {
fn kind(&self) -> ErrorKind {
*self
}
}
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
impl Error for std::io::Error {
fn kind(&self) -> ErrorKind {
self.kind().into()
}
}
/// Base trait for all IO traits, defining the error type.
///
/// All IO operations of all traits return the error defined in this trait.
///
/// Having a shared trait instead of having every trait define its own
/// `Error` associated type enforces all impls on the same type use the same error.
/// This is very convenient when writing generic code, it means you have to
/// handle a single error type `T::Error`, instead of `<T as Read>::Error` and `<T as Write>::Error`
/// which might be different types.
pub trait ErrorType {
/// Error type of all the IO operations on this type.
type Error: Error;
}
impl<T: ?Sized + ErrorType> ErrorType for &mut T {
type Error = T::Error;
}
/// Error returned by [`Read::read_exact`]
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
pub enum ReadExactError<E> {
/// An EOF error was encountered before reading the exact amount of requested bytes.
UnexpectedEof,
/// Error returned by the inner Read.
Other(E),
}
impl<E> From<E> for ReadExactError<E> {
fn from(err: E) -> Self {
Self::Other(err)
}
}
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
impl From<ReadExactError<std::io::Error>> for std::io::Error {
fn from(err: ReadExactError<std::io::Error>) -> Self {
match err {
ReadExactError::UnexpectedEof => std::io::Error::new(
std::io::ErrorKind::UnexpectedEof,
"UnexpectedEof".to_owned(),
),
ReadExactError::Other(e) => std::io::Error::new(e.kind(), format!("{e:?}")),
}
}
}
impl<E: fmt::Debug> fmt::Display for ReadExactError<E> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{self:?}")
}
}
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
impl<E: fmt::Debug> std::error::Error for ReadExactError<E> {}
/// Errors that could be returned by `Write` on `&mut [u8]`.
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
#[non_exhaustive]
pub enum SliceWriteError {
/// The target slice was full and so could not receive any new data.
Full,
}
/// Error returned by [`Write::write_fmt`]
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
#[cfg_attr(feature = "defmt-03", derive(defmt::Format))]
pub enum WriteFmtError<E> {
/// An error was encountered while formatting.
FmtError,
/// Error returned by the inner Write.
Other(E),
}
impl<E> From<E> for WriteFmtError<E> {
fn from(err: E) -> Self {
Self::Other(err)
}
}
impl<E: fmt::Debug> fmt::Display for WriteFmtError<E> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{self:?}")
}
}
#[cfg(feature = "std")]
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
impl<E: fmt::Debug> std::error::Error for WriteFmtError<E> {}
/// Blocking reader.
///
/// This trait is the `embedded-io` equivalent of [`std::io::Read`].
pub trait Read: ErrorType {
/// Read some bytes from this source into the specified buffer, returning how many bytes were read.
///
/// If no bytes are currently available to read, this function blocks until at least one byte is available.
///
/// If bytes are available, a non-zero amount of bytes is read to the beginning of `buf`, and the amount
/// is returned. It is not guaranteed that *all* available bytes are returned, it is possible for the
/// implementation to read an amount of bytes less than `buf.len()` while there are more bytes immediately
/// available.
///
/// If the reader is at end-of-file (EOF), `Ok(0)` is returned. There is no guarantee that a reader at EOF
/// will always be so in the future, for example a reader can stop being at EOF if another process appends
/// more bytes to the underlying file.
///
/// If `buf.len() == 0`, `read` returns without blocking, with either `Ok(0)` or an error.
/// The `Ok(0)` doesn't indicate EOF, unlike when called with a non-empty buffer.
fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error>;
/// Read the exact number of bytes required to fill `buf`.
///
/// This function calls `read()` in a loop until exactly `buf.len()` bytes have
/// been read, blocking if needed.
///
/// If you are using [`ReadReady`] to avoid blocking, you should not use this function.
/// `ReadReady::read_ready()` returning true only guarantees the first call to `read()` will
/// not block, so this function may still block in subsequent calls.
fn read_exact(&mut self, mut buf: &mut [u8]) -> Result<(), ReadExactError<Self::Error>> {
while !buf.is_empty() {
match self.read(buf) {
Ok(0) => break,
Ok(n) => buf = &mut buf[n..],
Err(e) => return Err(ReadExactError::Other(e)),
}
}
if buf.is_empty() {
Ok(())
} else {
Err(ReadExactError::UnexpectedEof)
}
}
}
/// Blocking buffered reader.
///
/// This trait is the `embedded-io` equivalent of [`std::io::BufRead`].
pub trait BufRead: ErrorType {
/// Return the contents of the internal buffer, filling it with more data from the inner reader if it is empty.
///
/// If no bytes are currently available to read, this function blocks until at least one byte is available.
///
/// If the reader is at end-of-file (EOF), an empty slice is returned. There is no guarantee that a reader at EOF
/// will always be so in the future, for example a reader can stop being at EOF if another process appends
/// more bytes to the underlying file.
fn fill_buf(&mut self) -> Result<&[u8], Self::Error>;
/// Tell this buffer that `amt` bytes have been consumed from the buffer, so they should no longer be returned in calls to `fill_buf`.
fn consume(&mut self, amt: usize);
}
/// Blocking writer.
///
/// This trait is the `embedded-io` equivalent of [`std::io::Write`].
pub trait Write: ErrorType {
/// Write a buffer into this writer, returning how many bytes were written.
///
/// If the writer is not currently ready to accept more bytes (for example, its buffer is full),
/// this function blocks until it is ready to accept least one byte.
///
/// If it's ready to accept bytes, a non-zero amount of bytes is written from the beginning of `buf`, and the amount
/// is returned. It is not guaranteed that *all* available buffer space is filled, i.e. it is possible for the
/// implementation to write an amount of bytes less than `buf.len()` while the writer continues to be
/// ready to accept more bytes immediately.
///
/// Implementations must not return `Ok(0)` unless `buf` is empty. Situations where the
/// writer is not able to accept more bytes must instead be indicated with an error,
/// where the `ErrorKind` is `WriteZero`.
///
/// If `buf` is empty, `write` returns without blocking, with either `Ok(0)` or an error.
/// `Ok(0)` doesn't indicate an error.
fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error>;
/// Flush this output stream, blocking until all intermediately buffered contents reach their destination.
fn flush(&mut self) -> Result<(), Self::Error>;
/// Write an entire buffer into this writer.
///
/// This function calls `write()` in a loop until exactly `buf.len()` bytes have
/// been written, blocking if needed.
///
/// If you are using [`WriteReady`] to avoid blocking, you should not use this function.
/// `WriteReady::write_ready()` returning true only guarantees the first call to `write()` will
/// not block, so this function may still block in subsequent calls.
///
/// This function will panic if `write()` returns `Ok(0)`.
fn write_all(&mut self, mut buf: &[u8]) -> Result<(), Self::Error> {
while !buf.is_empty() {
match self.write(buf) {
Ok(0) => panic!("write() returned Ok(0)"),
Ok(n) => buf = &buf[n..],
Err(e) => return Err(e),
}
}
Ok(())
}
/// Write a formatted string into this writer, returning any error encountered.
///
/// This function calls `write()` in a loop until the entire formatted string has
/// been written, blocking if needed.
///
/// If you are using [`WriteReady`] to avoid blocking, you should not use this function.
/// `WriteReady::write_ready()` returning true only guarantees the first call to `write()` will
/// not block, so this function may still block in subsequent calls.
///
/// Unlike [`Write::write`], the number of bytes written is not returned. However, in the case of
/// writing to an `&mut [u8]` its possible to calculate the number of bytes written by subtracting
/// the length of the slice after the write, from the initial length of the slice.
///
/// ```rust
/// # use embedded_io::Write;
/// let mut buf: &mut [u8] = &mut [0u8; 256];
/// let start = buf.len();
/// let len = write!(buf, "{}", "Test").and_then(|_| Ok(start - buf.len()));
/// ```
fn write_fmt(&mut self, fmt: fmt::Arguments<'_>) -> Result<(), WriteFmtError<Self::Error>> {
// Create a shim which translates a Write to a fmt::Write and saves
// off I/O errors. instead of discarding them
struct Adapter<'a, T: Write + ?Sized + 'a> {
inner: &'a mut T,
error: Result<(), T::Error>,
}
impl<T: Write + ?Sized> fmt::Write for Adapter<'_, T> {
fn write_str(&mut self, s: &str) -> fmt::Result {
match self.inner.write_all(s.as_bytes()) {
Ok(()) => Ok(()),
Err(e) => {
self.error = Err(e);
Err(fmt::Error)
}
}
}
}
let mut output = Adapter {
inner: self,
error: Ok(()),
};
match fmt::write(&mut output, fmt) {
Ok(()) => Ok(()),
Err(..) => match output.error {
// check if the error came from the underlying `Write` or not
Err(e) => Err(WriteFmtError::Other(e)),
Ok(()) => Err(WriteFmtError::FmtError),
},
}
}
}
/// Blocking seek within streams.
///
/// This trait is the `embedded-io` equivalent of [`std::io::Seek`].
pub trait Seek: ErrorType {
/// Seek to an offset, in bytes, in a stream.
fn seek(&mut self, pos: SeekFrom) -> Result<u64, Self::Error>;
/// Rewind to the beginning of a stream.
fn rewind(&mut self) -> Result<(), Self::Error> {
self.seek(SeekFrom::Start(0))?;
Ok(())
}
/// Returns the current seek position from the start of the stream.
fn stream_position(&mut self) -> Result<u64, Self::Error> {
self.seek(SeekFrom::Current(0))
}
}
/// Get whether a reader is ready.
///
/// This allows using a [`Read`] or [`BufRead`] in a nonblocking fashion, i.e. trying to read
/// only when it is ready.
pub trait ReadReady: ErrorType {
/// Get whether the reader is ready for immediately reading.
///
/// This usually means that there is either some bytes have been received and are buffered and ready to be read,
/// or that the reader is at EOF.
///
/// If this returns `true`, it's guaranteed that the next call to [`Read::read`] or [`BufRead::fill_buf`] will not block.
fn read_ready(&mut self) -> Result<bool, Self::Error>;
}
/// Get whether a writer is ready.
///
/// This allows using a [`Write`] in a nonblocking fashion, i.e. trying to write
/// only when it is ready.
pub trait WriteReady: ErrorType {
/// Get whether the writer is ready for immediately writing.
///
/// This usually means that there is free space in the internal transmit buffer.
///
/// If this returns `true`, it's guaranteed that the next call to [`Write::write`] will not block.
fn write_ready(&mut self) -> Result<bool, Self::Error>;
}
impl<T: ?Sized + Read> Read for &mut T {
#[inline]
fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> {
T::read(self, buf)
}
}
impl<T: ?Sized + BufRead> BufRead for &mut T {
fn fill_buf(&mut self) -> Result<&[u8], Self::Error> {
T::fill_buf(self)
}
fn consume(&mut self, amt: usize) {
T::consume(self, amt);
}
}
impl<T: ?Sized + Write> Write for &mut T {
#[inline]
fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
T::write(self, buf)
}
#[inline]
fn flush(&mut self) -> Result<(), Self::Error> {
T::flush(self)
}
}
impl<T: ?Sized + Seek> Seek for &mut T {
#[inline]
fn seek(&mut self, pos: SeekFrom) -> Result<u64, Self::Error> {
T::seek(self, pos)
}
}
impl<T: ?Sized + ReadReady> ReadReady for &mut T {
#[inline]
fn read_ready(&mut self) -> Result<bool, Self::Error> {
T::read_ready(self)
}
}
impl<T: ?Sized + WriteReady> WriteReady for &mut T {
#[inline]
fn write_ready(&mut self) -> Result<bool, Self::Error> {
T::write_ready(self)
}
}