embedded-trainings-2020/beginner/apps/src/bin/radio-puzzle-4.rs

38 lines
1.1 KiB
Rust
Raw Normal View History

2020-06-22 13:23:59 +00:00
#![deny(unused_must_use)]
#![no_main]
#![no_std]
use core::str;
use cortex_m_rt::entry;
use heapless::{consts, Vec};
2021-04-12 09:51:44 +00:00
// this imports `beginner/apps/lib.rs` to retrieve our global logger + panicking-behavior
use apps as _;
2020-06-22 13:23:59 +00:00
#[entry]
fn main() -> ! {
dk::init().unwrap();
// a buffer with capacity for 2 bytes
let mut buffer = Vec::<u8, consts::U2>::new();
// ^^ capacity; this is a type not a value
2020-06-22 13:23:59 +00:00
// do some insertions
buffer.push(b'H').expect("buffer full");
buffer.push(b'i').expect("buffer full");
// look into the contents so far
2021-04-12 09:51:44 +00:00
defmt::info!("{:?}", defmt::Debug2Format(&buffer));
2021-04-14 10:02:02 +00:00
// ^^^^^^^^^^^^^^^^^^^ this adapter is currently needed to log `heapless`
2021-04-12 09:51:44 +00:00
// data structures (like `Vec` here) with `defmt`
2020-06-22 13:23:59 +00:00
// or more readable
2021-04-12 09:51:44 +00:00
// NOTE utf-8 conversion works as long as you only push bytes in the ASCII range (0..=127)
defmt::info!(
2020-06-22 13:23:59 +00:00
"{}",
str::from_utf8(&buffer).expect("content was not UTF-8")
);
dk::exit()
}