|
375 | 375 | //! # }
|
376 | 376 | //! ```
|
377 | 377 | //!
|
378 |
| -//! ### `await` |
379 |
| -//! |
380 |
| -//! Same example as above but using `await!` instead of `futures` |
381 |
| -//! (same remark concerning the availability of `libstd` on the |
382 |
| -//! target). |
383 |
| -//! |
384 |
| -//! ```no_run |
385 |
| -//! #![feature(generator_trait)] |
386 |
| -//! #![feature(generators)] |
387 |
| -//! |
388 |
| -//! extern crate embedded_hal as hal; |
389 |
| -//! |
390 |
| -//! #[macro_use(r#await)] |
391 |
| -//! extern crate nb; |
392 |
| -//! |
393 |
| -//! use core::ops::Generator; |
394 |
| -//! use core::pin::Pin; |
395 |
| -//! |
396 |
| -//! use hal::prelude::*; |
397 |
| -//! use stm32f30x_hal::{Led, Serial1, Timer6}; |
398 |
| -//! |
399 |
| -//! fn main() { |
400 |
| -//! // HAL implementers |
401 |
| -//! let mut timer: Timer6 = { |
402 |
| -//! // .. |
403 |
| -//! # Timer6 |
404 |
| -//! }; |
405 |
| -//! let mut serial: Serial1 = { |
406 |
| -//! // .. |
407 |
| -//! # Serial1 |
408 |
| -//! }; |
409 |
| -//! let mut led: Led = { |
410 |
| -//! // .. |
411 |
| -//! # Led |
412 |
| -//! }; |
413 |
| -//! |
414 |
| -//! // Tasks |
415 |
| -//! let mut blinky = (move || { |
416 |
| -//! let mut state = false; |
417 |
| -//! loop { |
418 |
| -//! // `await!` means "suspend / yield here" instead of "block until |
419 |
| -//! // completion" |
420 |
| -//! nb::r#await!(timer.try_wait()).unwrap(); // NOTE(unwrap) E = Infallible |
421 |
| -//! |
422 |
| -//! state = !state; |
423 |
| -//! |
424 |
| -//! if state { |
425 |
| -//! led.on(); |
426 |
| -//! } else { |
427 |
| -//! led.off(); |
428 |
| -//! } |
429 |
| -//! } |
430 |
| -//! }); |
431 |
| -//! |
432 |
| -//! let mut loopback = (move || { |
433 |
| -//! loop { |
434 |
| -//! let byte = nb::r#await!(serial.try_read()).unwrap(); |
435 |
| -//! nb::r#await!(serial.try_write(byte)).unwrap(); |
436 |
| -//! } |
437 |
| -//! }); |
438 |
| -//! |
439 |
| -//! // Event loop |
440 |
| -//! loop { |
441 |
| -//! Pin::new(&mut blinky).resume(()); |
442 |
| -//! Pin::new(&mut loopback).resume(()); |
443 |
| -//! # break; |
444 |
| -//! } |
445 |
| -//! } |
446 |
| -//! |
447 |
| -//! # mod stm32f30x_hal { |
448 |
| -//! # use core::convert::Infallible; |
449 |
| -//! # pub struct Serial1; |
450 |
| -//! # impl Serial1 { |
451 |
| -//! # pub fn try_read(&mut self) -> ::nb::Result<u8, Infallible> { Err(::nb::Error::WouldBlock) } |
452 |
| -//! # pub fn try_write(&mut self, _: u8) -> ::nb::Result<(), Infallible> { Err(::nb::Error::WouldBlock) } |
453 |
| -//! # } |
454 |
| -//! # pub struct Timer6; |
455 |
| -//! # impl Timer6 { |
456 |
| -//! # pub fn try_wait(&mut self) -> ::nb::Result<(), Infallible> { Err(::nb::Error::WouldBlock) } |
457 |
| -//! # } |
458 |
| -//! # pub struct Led; |
459 |
| -//! # impl Led { |
460 |
| -//! # pub fn off(&mut self) {} |
461 |
| -//! # pub fn on(&mut self) {} |
462 |
| -//! # } |
463 |
| -//! # } |
464 |
| -//! ``` |
465 |
| -//! |
466 | 378 | //! ## Generic programming and higher level abstractions
|
467 | 379 | //!
|
468 | 380 | //! The core of the HAL has been kept minimal on purpose to encourage building **generic** higher
|
|
0 commit comments