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:
17624 files changed
+4564928
-38
No files matched your search
Vendored
+1
@@ -0,0 +1 @@
|
||||
{"files":{".cargo_vcs_info.json":"98027ea583d1a09725cf627a1926816c3d74c2c7439b7b924c672cd6e0e30ce9",".github/CODEOWNERS":"98381d61605d07c59738b89a670eef5c979388b42432d207ecbd5bc3f896cad0",".github/bors.toml":"3d14adf1ac73fed6f457184b8440c79f2d6c76290b365358b669acbba271e361",".github/workflows/ci.yml":"8471a0928d1a6506a31cde3745424e119c59dd02d649da959dc65aed61080e5c",".github/workflows/clippy.yml":"9f33f512b68c4f050fa4dfbb3eb6d3e352fd43b5a0dfbe29bb0d586edb53f9ab",".github/workflows/rustfmt.yml":"32bde2ef4e52e477334d47c175c43433115160a6e91c2d3659d257f2a30cad9a","CHANGELOG.md":"6656d9902a33a76e1f57ae269cc4290a20f0660b7098451f0222d6a2e05211a6","Cargo.toml":"f277cf77fc1263e49054d92acb079e11bb747b1a310a66d262f9824c7ebaf573","Cargo.toml.orig":"432cbe60f18e5d1c93ba4c9f974a92d1b22663baf6f280dd287e6d452f7ef8dd","LICENSE-APACHE":"a60eea817514531668d7e00765731449fe14d059d3249e0bc93b36de45f759f2","LICENSE-MIT":"035e70219855119df4273b3c5b97543ae82e0dd60c520416e759107c602f651b","README.md":"f58c64d699a84555e9542fad4acf71d3f4fb6bc266199efa9dde7d999ece9263","bors.toml":"b96eaac6b3dc8487a2bcc6cb415e745a28d9a61937090df48186c62d2b614aeb","src/lib.rs":"7dc2ec005f5307821c792536841c9db8285614ebff5471fc029de8300d410008"},"package":"801d31da0513b6ec5214e9bf433a77966320625a37860f910be265be6e18d06f"}
|
||||
Vendored
+5
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"git": {
|
||||
"sha1": "120963270339c1685822e68b7628520d4a52c68a"
|
||||
}
|
||||
}
|
||||
+1
@@ -0,0 +1 @@
|
||||
* @rust-embedded/hal
|
||||
+7
@@ -0,0 +1,7 @@
|
||||
block_labels = ["needs-decision"]
|
||||
delete_merged_branches = true
|
||||
required_approvals = 1
|
||||
status = [
|
||||
"ci-linux (stable, x86_64-unknown-linux-gnu)",
|
||||
"ci-linux (1.35.0, x86_64-unknown-linux-gnu)",
|
||||
]
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
on:
|
||||
push:
|
||||
branches: [ staging, trying, master ]
|
||||
pull_request:
|
||||
|
||||
name: Continuous integration
|
||||
|
||||
env:
|
||||
RUSTFLAGS: '-D warnings'
|
||||
|
||||
jobs:
|
||||
ci-linux:
|
||||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
matrix:
|
||||
# All generated code should be running on stable now
|
||||
rust: [stable]
|
||||
|
||||
# The default target we're compiling on and for
|
||||
TARGET: [x86_64-unknown-linux-gnu]
|
||||
|
||||
include:
|
||||
# Test MSRV
|
||||
- rust: 1.35.0
|
||||
TARGET: x86_64-unknown-linux-gnu
|
||||
|
||||
# Test nightly but don't fail
|
||||
- rust: nightly
|
||||
experimental: true
|
||||
TARGET: x86_64-unknown-linux-gnu
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: actions-rs/toolchain@v1
|
||||
with:
|
||||
profile: minimal
|
||||
toolchain: ${{ matrix.rust }}
|
||||
target: ${{ matrix.TARGET }}
|
||||
override: true
|
||||
- uses: actions-rs/cargo@v1
|
||||
with:
|
||||
command: check
|
||||
args: --target=${{ matrix.TARGET }}
|
||||
- uses: actions-rs/cargo@v1
|
||||
with:
|
||||
command: test
|
||||
args: --target=${{ matrix.TARGET }}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
on:
|
||||
push:
|
||||
branches: [ staging, trying, master ]
|
||||
pull_request:
|
||||
|
||||
name: Clippy check
|
||||
|
||||
env:
|
||||
RUSTFLAGS: '-D warnings'
|
||||
|
||||
jobs:
|
||||
clippy_check:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: actions-rs/toolchain@v1
|
||||
with:
|
||||
profile: minimal
|
||||
toolchain: stable
|
||||
override: true
|
||||
components: clippy
|
||||
- uses: actions-rs/clippy-check@v1
|
||||
with:
|
||||
token: ${{ secrets.GITHUB_TOKEN }}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
on:
|
||||
push:
|
||||
branches: [ staging, trying, master ]
|
||||
pull_request:
|
||||
|
||||
name: Code formatting check
|
||||
|
||||
jobs:
|
||||
fmt:
|
||||
name: Rustfmt
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v2
|
||||
- uses: actions-rs/toolchain@v1
|
||||
with:
|
||||
profile: minimal
|
||||
toolchain: stable
|
||||
override: true
|
||||
components: rustfmt
|
||||
- uses: actions-rs/cargo@v1
|
||||
with:
|
||||
command: fmt
|
||||
args: --all -- --check
|
||||
Vendored
+37
@@ -0,0 +1,37 @@
|
||||
# Change Log
|
||||
|
||||
All notable changes to this project will be documented in this file.
|
||||
|
||||
The format is based on [Keep a Changelog](http://keepachangelog.com/)
|
||||
and this project adheres to [Semantic Versioning](http://semver.org/).
|
||||
|
||||
## [Unreleased]
|
||||
|
||||
## [v0.1.3] - 2020-07-07
|
||||
|
||||
This release of the 0.1 version exists for compatibility with 1.0.0.
|
||||
There are no functional changes compared to 0.1.2.
|
||||
|
||||
## [v0.1.2] - 2019-04-21
|
||||
|
||||
### Added
|
||||
|
||||
- `Error<E>` gained a `map` method that lets you transform the error in the
|
||||
`Error::Other` variant into a different type.
|
||||
|
||||
- `Error<E>` now implements the `From<E>` trait.
|
||||
|
||||
## [v0.1.1] - 2018-01-10
|
||||
|
||||
### Fixed
|
||||
|
||||
- The `await!` macro now works when the expression `$e` mutably borrows `self`.
|
||||
|
||||
## v0.1.0 - 2018-01-10
|
||||
|
||||
Initial release
|
||||
|
||||
[Unreleased]: https://github.com/rust-embedded/nb/compare/v0.1.3...HEAD
|
||||
[v0.1.3]: https://github.com/rust-embedded/nb/compare/v0.1.2...v0.1.3
|
||||
[v0.1.2]: https://github.com/rust-embedded/nb/compare/v0.1.1...v0.1.2
|
||||
[v0.1.1]: https://github.com/rust-embedded/nb/compare/v0.1.0...v0.1.1
|
||||
Vendored
+34
@@ -0,0 +1,34 @@
|
||||
# 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 believe there's an error in this file please file an
|
||||
# issue against the rust-lang/cargo repository. If you're
|
||||
# editing this file be aware that the upstream Cargo.toml
|
||||
# will likely look very different (and much more reasonable)
|
||||
|
||||
[package]
|
||||
name = "nb"
|
||||
version = "0.1.3"
|
||||
authors = ["Jorge Aparicio <jorge@japaric.io>"]
|
||||
description = "Minimal non-blocking I/O layer"
|
||||
homepage = "https://github.com/rust-embedded/nb"
|
||||
documentation = "https://docs.rs/nb"
|
||||
readme = "README.md"
|
||||
keywords = ["await", "futures", "IO"]
|
||||
categories = ["asynchronous", "embedded", "no-std"]
|
||||
license = "MIT OR Apache-2.0"
|
||||
repository = "https://github.com/rust-embedded/nb"
|
||||
|
||||
[lib]
|
||||
doctest = false
|
||||
[dependencies.nb]
|
||||
version = "1"
|
||||
[dev-dependencies.futures]
|
||||
version = "0.1.17"
|
||||
|
||||
[features]
|
||||
unstable = []
|
||||
Vendored
+201
@@ -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.
|
||||
Vendored
+25
@@ -0,0 +1,25 @@
|
||||
Copyright (c) 2017 Jorge Aparicio
|
||||
|
||||
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.
|
||||
Vendored
+25
@@ -0,0 +1,25 @@
|
||||
# `nb`
|
||||
|
||||
> Minimal and reusable non-blocking I/O layer
|
||||
|
||||
This project is developed and maintained by the [HAL team][team].
|
||||
|
||||
## [Documentation](https://docs.rs/nb)
|
||||
|
||||
## 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.
|
||||
|
||||
[team]: https://github.com/rust-embedded/wg#the-hal-team
|
||||
Vendored
+3
@@ -0,0 +1,3 @@
|
||||
status = [
|
||||
"continuous-integration/travis-ci/push",
|
||||
]
|
||||
Vendored
+438
@@ -0,0 +1,438 @@
|
||||
//! Minimal and reusable non-blocking I/O layer
|
||||
//!
|
||||
//! The ultimate goal of this crate is *code reuse*. With this crate you can
|
||||
//! write *core* I/O APIs that can then be adapted to operate in either blocking
|
||||
//! or non-blocking manner. Furthermore those APIs are not tied to a particular
|
||||
//! asynchronous model and can be adapted to work with the `futures` model or
|
||||
//! with the `async` / `await` model.
|
||||
//!
|
||||
//! # Core idea
|
||||
//!
|
||||
//! The [`WouldBlock`](enum.Error.html) error variant signals that the operation
|
||||
//! can't be completed *right now* and would need to block to complete.
|
||||
//! [`WouldBlock`](enum.Error.html) is a special error in the sense that's not
|
||||
//! *fatal*; the operation can still be completed by retrying again later.
|
||||
//!
|
||||
//! [`nb::Result`](type.Result.html) is based on the API of
|
||||
//! [`std::io::Result`](https://doc.rust-lang.org/std/io/type.Result.html),
|
||||
//! which has a `WouldBlock` variant in its
|
||||
//! [`ErrorKind`](https://doc.rust-lang.org/std/io/enum.ErrorKind.html).
|
||||
//!
|
||||
//! We can map [`WouldBlock`](enum.Error.html) to different blocking and
|
||||
//! non-blocking models:
|
||||
//!
|
||||
//! - In blocking mode: [`WouldBlock`](enum.Error.html) means try again right
|
||||
//! now (i.e. busy wait)
|
||||
//! - In `futures` mode: [`WouldBlock`](enum.Error.html) means
|
||||
//! [`Async::NotReady`](https://docs.rs/futures)
|
||||
//! - In `await` mode: [`WouldBlock`](enum.Error.html) means `yield`
|
||||
//! (suspend the generator)
|
||||
//!
|
||||
//! # How to use this crate
|
||||
//!
|
||||
//! Application specific errors can be put inside the `Other` variant in the
|
||||
//! [`nb::Error`](enum.Error.html) enum.
|
||||
//!
|
||||
//! So in your API instead of returning `Result<T, MyError>` return
|
||||
//! `nb::Result<T, MyError>`
|
||||
//!
|
||||
//! ```
|
||||
//! enum MyError {
|
||||
//! ThisError,
|
||||
//! ThatError,
|
||||
//! // ..
|
||||
//! }
|
||||
//!
|
||||
//! // This is a blocking function, so it returns a normal `Result`
|
||||
//! fn before() -> Result<(), MyError> {
|
||||
//! // ..
|
||||
//! # Ok(())
|
||||
//! }
|
||||
//!
|
||||
//! // This is now a potentially (read: *non*) blocking function so it returns `nb::Result`
|
||||
//! // instead of blocking
|
||||
//! fn after() -> nb::Result<(), MyError> {
|
||||
//! // ..
|
||||
//! # Ok(())
|
||||
//! }
|
||||
//! ```
|
||||
//!
|
||||
//! You can use the *never type* (`!`) to signal that some API has no fatal
|
||||
//! errors but may block:
|
||||
//!
|
||||
//! ```
|
||||
//! #![feature(never_type)]
|
||||
//!
|
||||
//! // This returns `Ok(())` or `Err(nb::Error::WouldBlock)`
|
||||
//! fn maybe_blocking_api() -> nb::Result<(), !> {
|
||||
//! // ..
|
||||
//! # Ok(())
|
||||
//! }
|
||||
//! ```
|
||||
//!
|
||||
//! Once your API uses [`nb::Result`](type.Result.html) you can leverage the
|
||||
//! [`block!`], [`try_nb!`] and [`await!`] macros to adapt it for blocking
|
||||
//! operation, or for non-blocking operation with `futures` or `await`.
|
||||
//!
|
||||
//! **NOTE** Currently, both `try_nb!` and `await!` are feature gated behind the `unstable` Cargo
|
||||
//! feature.
|
||||
//!
|
||||
//! [`block!`]: macro.block.html
|
||||
//! [`try_nb!`]: macro.try_nb.html
|
||||
//! [`await!`]: macro.await.html
|
||||
//!
|
||||
//! # Examples
|
||||
//!
|
||||
//! ## A Core I/O API
|
||||
//!
|
||||
//! Imagine the code (crate) below represents a Hardware Abstraction Layer for some microcontroller
|
||||
//! (or microcontroller family).
|
||||
//!
|
||||
//! *In this and the following examples let's assume for simplicity that peripherals are treated
|
||||
//! as global singletons and that no preemption is possible (i.e. interrupts are disabled).*
|
||||
//!
|
||||
//! ```
|
||||
//! #![feature(never_type)]
|
||||
//!
|
||||
//! // This is the `hal` crate
|
||||
//! // Note that it doesn't depend on the `futures` crate
|
||||
//!
|
||||
//! extern crate nb;
|
||||
//!
|
||||
//! /// An LED
|
||||
//! pub struct Led;
|
||||
//!
|
||||
//! impl Led {
|
||||
//! pub fn off(&self) {
|
||||
//! // ..
|
||||
//! }
|
||||
//! pub fn on(&self) {
|
||||
//! // ..
|
||||
//! }
|
||||
//! }
|
||||
//!
|
||||
//! /// Serial interface
|
||||
//! pub struct Serial;
|
||||
//! pub enum Error {
|
||||
//! Overrun,
|
||||
//! // ..
|
||||
//! }
|
||||
//!
|
||||
//! impl Serial {
|
||||
//! /// Reads a single byte from the serial interface
|
||||
//! pub fn read(&self) -> nb::Result<u8, Error> {
|
||||
//! // ..
|
||||
//! # Ok(0)
|
||||
//! }
|
||||
//!
|
||||
//! /// Writes a single byte to the serial interface
|
||||
//! pub fn write(&self, byte: u8) -> nb::Result<(), Error> {
|
||||
//! // ..
|
||||
//! # Ok(())
|
||||
//! }
|
||||
//! }
|
||||
//!
|
||||
//! /// A timer used for timeouts
|
||||
//! pub struct Timer;
|
||||
//!
|
||||
//! impl Timer {
|
||||
//! /// Waits until the timer times out
|
||||
//! pub fn wait(&self) -> nb::Result<(), !> {
|
||||
//! //^ NOTE the `!` indicates that this operation can block but has no
|
||||
//! // other form of error
|
||||
//!
|
||||
//! // ..
|
||||
//! # Ok(())
|
||||
//! }
|
||||
//! }
|
||||
//! ```
|
||||
//!
|
||||
//! ## Blocking mode
|
||||
//!
|
||||
//! Turn on an LED for one second and *then* loops back serial data.
|
||||
//!
|
||||
//! ```
|
||||
//! # #![feature(never_type)]
|
||||
//! #[macro_use(block)]
|
||||
//! extern crate nb;
|
||||
//!
|
||||
//! use hal::{Led, Serial, Timer};
|
||||
//!
|
||||
//! fn main() {
|
||||
//! // Turn the LED on for one second
|
||||
//! Led.on();
|
||||
//! block!(Timer.wait()).unwrap(); // NOTE(unwrap) E = !
|
||||
//! Led.off();
|
||||
//!
|
||||
//! // Serial interface loopback
|
||||
//! # return;
|
||||
//! loop {
|
||||
//! let byte = block!(Serial.read()).unwrap();
|
||||
//! block!(Serial.write(byte)).unwrap();
|
||||
//! }
|
||||
//! }
|
||||
//!
|
||||
//! # mod hal {
|
||||
//! # use nb;
|
||||
//! # pub struct Led;
|
||||
//! # impl Led {
|
||||
//! # pub fn off(&self) {}
|
||||
//! # pub fn on(&self) {}
|
||||
//! # }
|
||||
//! # pub struct Serial;
|
||||
//! # impl Serial {
|
||||
//! # pub fn read(&self) -> nb::Result<u8, ()> { Ok(0) }
|
||||
//! # pub fn write(&self, _: u8) -> nb::Result<(), ()> { Ok(()) }
|
||||
//! # }
|
||||
//! # pub struct Timer;
|
||||
//! # impl Timer {
|
||||
//! # pub fn wait(&self) -> nb::Result<(), !> { Ok(()) }
|
||||
//! # }
|
||||
//! # }
|
||||
//! ```
|
||||
//!
|
||||
//! ## `futures`
|
||||
//!
|
||||
//! Blinks an LED every second *and* loops back serial data. Both tasks run
|
||||
//! concurrently.
|
||||
//!
|
||||
//! ```
|
||||
//! #![feature(conservative_impl_trait)]
|
||||
//! #![feature(never_type)]
|
||||
//!
|
||||
//! extern crate futures;
|
||||
//! #[macro_use(try_nb)]
|
||||
//! extern crate nb;
|
||||
//!
|
||||
//! use futures::{Async, Future};
|
||||
//! use futures::future::{self, Loop};
|
||||
//! use hal::{Error, Led, Serial, Timer};
|
||||
//!
|
||||
//! /// `futures` version of `Timer.wait`
|
||||
//! ///
|
||||
//! /// This returns a future that must be polled to completion
|
||||
//! fn wait() -> impl Future<Item = (), Error = !> {
|
||||
//! future::poll_fn(|| {
|
||||
//! Ok(Async::Ready(try_nb!(Timer.wait())))
|
||||
//! })
|
||||
//! }
|
||||
//!
|
||||
//! /// `futures` version of `Serial.read`
|
||||
//! ///
|
||||
//! /// This returns a future that must be polled to completion
|
||||
//! fn read() -> impl Future<Item = u8, Error = Error> {
|
||||
//! future::poll_fn(|| {
|
||||
//! Ok(Async::Ready(try_nb!(Serial.read())))
|
||||
//! })
|
||||
//! }
|
||||
//!
|
||||
//! /// `futures` version of `Serial.write`
|
||||
//! ///
|
||||
//! /// This returns a future that must be polled to completion
|
||||
//! fn write(byte: u8) -> impl Future<Item = (), Error = Error> {
|
||||
//! future::poll_fn(move || {
|
||||
//! Ok(Async::Ready(try_nb!(Serial.write(byte))))
|
||||
//! })
|
||||
//! }
|
||||
//!
|
||||
//! fn main() {
|
||||
//! // Tasks
|
||||
//! let mut blinky = future::loop_fn::<_, (), _, _>(true, |state| {
|
||||
//! wait().map(move |_| {
|
||||
//! if state {
|
||||
//! Led.on();
|
||||
//! } else {
|
||||
//! Led.off();
|
||||
//! }
|
||||
//!
|
||||
//! Loop::Continue(!state)
|
||||
//! })
|
||||
//! });
|
||||
//!
|
||||
//! let mut loopback = future::loop_fn::<_, (), _, _>((), |_| {
|
||||
//! read().and_then(|byte| {
|
||||
//! write(byte)
|
||||
//! }).map(|_| {
|
||||
//! Loop::Continue(())
|
||||
//! })
|
||||
//! });
|
||||
//!
|
||||
//! // Event loop
|
||||
//! loop {
|
||||
//! blinky.poll().unwrap(); // NOTE(unwrap) E = !
|
||||
//! loopback.poll().unwrap();
|
||||
//! # break
|
||||
//! }
|
||||
//! }
|
||||
//!
|
||||
//! # mod hal {
|
||||
//! # use nb;
|
||||
//! # pub struct Led;
|
||||
//! # impl Led {
|
||||
//! # pub fn off(&self) {panic!()}
|
||||
//! # pub fn on(&self) {}
|
||||
//! # }
|
||||
//! # #[derive(Debug)]
|
||||
//! # pub enum Error {}
|
||||
//! # pub struct Serial;
|
||||
//! # impl Serial {
|
||||
//! # pub fn read(&self) -> nb::Result<u8, Error> { Err(nb::Error::WouldBlock) }
|
||||
//! # pub fn write(&self, _: u8) -> nb::Result<(), Error> { Err(nb::Error::WouldBlock) }
|
||||
//! # }
|
||||
//! # pub struct Timer;
|
||||
//! # impl Timer {
|
||||
//! # pub fn wait(&self) -> nb::Result<(), !> { Err(nb::Error::WouldBlock) }
|
||||
//! # }
|
||||
//! # }
|
||||
//! ```
|
||||
//!
|
||||
//! ## `await!`
|
||||
//!
|
||||
//! This is equivalent to the `futures` example but with much less boilerplate.
|
||||
//!
|
||||
//! ```
|
||||
//! #![feature(generator_trait)]
|
||||
//! #![feature(generators)]
|
||||
//! #![feature(never_type)]
|
||||
//!
|
||||
//! #[macro_use(await)]
|
||||
//! extern crate nb;
|
||||
//!
|
||||
//! use std::ops::Generator;
|
||||
//!
|
||||
//! use hal::{Led, Serial, Timer};
|
||||
//!
|
||||
//! fn main() {
|
||||
//! // Tasks
|
||||
//! let mut blinky = || {
|
||||
//! let mut state = false;
|
||||
//! loop {
|
||||
//! // `await!` means suspend / yield instead of blocking
|
||||
//! await!(Timer.wait()).unwrap(); // NOTE(unwrap) E = !
|
||||
//!
|
||||
//! state = !state;
|
||||
//!
|
||||
//! if state {
|
||||
//! Led.on();
|
||||
//! } else {
|
||||
//! Led.off();
|
||||
//! }
|
||||
//! }
|
||||
//! };
|
||||
//!
|
||||
//! let mut loopback = || {
|
||||
//! loop {
|
||||
//! let byte = await!(Serial.read()).unwrap();
|
||||
//! await!(Serial.write(byte)).unwrap();
|
||||
//! }
|
||||
//! };
|
||||
//!
|
||||
//! // Event loop
|
||||
//! loop {
|
||||
//! blinky.resume();
|
||||
//! loopback.resume();
|
||||
//! # break
|
||||
//! }
|
||||
//! }
|
||||
//!
|
||||
//! # mod hal {
|
||||
//! # use nb;
|
||||
//! # pub struct Led;
|
||||
//! # impl Led {
|
||||
//! # pub fn off(&self) {}
|
||||
//! # pub fn on(&self) {}
|
||||
//! # }
|
||||
//! # pub struct Serial;
|
||||
//! # impl Serial {
|
||||
//! # pub fn read(&self) -> nb::Result<u8, ()> { Err(nb::Error::WouldBlock) }
|
||||
//! # pub fn write(&self, _: u8) -> nb::Result<(), ()> { Err(nb::Error::WouldBlock) }
|
||||
//! # }
|
||||
//! # pub struct Timer;
|
||||
//! # impl Timer {
|
||||
//! # pub fn wait(&self) -> nb::Result<(), !> { Err(nb::Error::WouldBlock) }
|
||||
//! # }
|
||||
//! # }
|
||||
//! ```
|
||||
|
||||
#![no_std]
|
||||
#![doc(html_root_url = "https://docs.rs/nb/0.1.3")]
|
||||
|
||||
extern crate nb;
|
||||
pub use nb::{block, Error, Result};
|
||||
|
||||
/// Await operation (*won't work until the language gains support for
|
||||
/// generators*)
|
||||
///
|
||||
/// This macro evaluates the expression `$e` *cooperatively* yielding control
|
||||
/// back to the (generator) caller whenever `$e` evaluates to
|
||||
/// `Error::WouldBlock`.
|
||||
///
|
||||
/// # Requirements
|
||||
///
|
||||
/// This macro must be called within a generator body.
|
||||
///
|
||||
/// # Input
|
||||
///
|
||||
/// An expression `$e` that evaluates to `nb::Result<T, E>`
|
||||
///
|
||||
/// # Output
|
||||
///
|
||||
/// - `Ok(t)` if `$e` evaluates to `Ok(t)`
|
||||
/// - `Err(e)` if `$e` evaluates to `Err(nb::Error::Other(e))`
|
||||
#[cfg(feature = "unstable")]
|
||||
#[macro_export]
|
||||
macro_rules! await {
|
||||
($e:expr) => {
|
||||
loop {
|
||||
#[allow(unreachable_patterns)]
|
||||
match $e {
|
||||
Err($crate::Error::Other(e)) =>
|
||||
{
|
||||
#[allow(unreachable_code)]
|
||||
break Err(e)
|
||||
}
|
||||
Err($crate::Error::WouldBlock) => {} // yield (see below)
|
||||
Ok(x) => break Ok(x),
|
||||
}
|
||||
|
||||
yield
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/// Future adapter
|
||||
///
|
||||
/// This is a *try* operation from a `nb::Result` to a `futures::Poll`
|
||||
///
|
||||
/// # Requirements
|
||||
///
|
||||
/// This macro must be called within a function / closure that has signature
|
||||
/// `fn(..) -> futures::Poll<T, E>`.
|
||||
///
|
||||
/// This macro requires that the [`futures`] crate is in the root of the crate.
|
||||
///
|
||||
/// [`futures`]: https://crates.io/crates/futures
|
||||
///
|
||||
/// # Input
|
||||
///
|
||||
/// An expression `$e` that evaluates to `nb::Result<T, E>`
|
||||
///
|
||||
/// # Early return
|
||||
///
|
||||
/// - `Ok(Async::NotReady)` if `$e` evaluates to `Err(nb::Error::WouldBlock)`
|
||||
/// - `Err(e)` if `$e` evaluates to `Err(nb::Error::Other(e))`
|
||||
///
|
||||
/// # Output
|
||||
///
|
||||
/// `t` if `$e` evaluates to `Ok(t)`
|
||||
#[cfg(feature = "unstable")]
|
||||
#[macro_export]
|
||||
macro_rules! try_nb {
|
||||
($e:expr) => {
|
||||
match $e {
|
||||
Err($crate::Error::Other(e)) => return Err(e),
|
||||
Err($crate::Error::WouldBlock) => return Ok(::futures::Async::NotReady),
|
||||
Ok(x) => x,
|
||||
}
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user