move to heapless 0.7

This commit is contained in:
Lotte Steenbrink 2021-06-04 13:41:31 +02:00 committed by Mirabellensaft
parent b672967d8a
commit 1752956de2
13 changed files with 40 additions and 38 deletions

View file

@ -18,11 +18,14 @@ cortex-m-rtic = "0.5.1"
defmt = "0.2.1"
defmt-rtt = "0.2.0"
dk = { path = "../../boards/dk", features = ["advanced"] }
heapless = "0.5.5"
panic-probe = { version = "0.2.0", features = ["print-defmt"] }
usb = { path = "../common/usb" }
usb2 = "0.0.1"
[dependencies.heapless]
version = "0.7.1"
features = ["defmt-impl"]
# optimize code in both profiles
[profile.dev]
codegen-units = 1

View file

@ -91,7 +91,6 @@ fn ep0setup(usbd: &USBD, ep0in: &mut Ep0In, state: &mut State) -> Result<(), ()>
defmt::info!("EP0: {:?}", defmt::Debug2Format(&request));
// ^^^^^^^^^^^^^^^^^^^ this adapter is currently needed to log
// `StandardRequest` with `defmt`
match request {
// section 9.4.3
// this request is valid in any state
@ -116,7 +115,7 @@ fn ep0setup(usbd: &USBD, ep0in: &mut Ep0In, state: &mut State) -> Result<(), ()>
Descriptor::Configuration { index } => {
if index == 0 {
let mut resp = heapless::Vec::<u8, heapless::consts::U64>::new();
let mut resp = heapless::Vec::<u8, 64>::new();
let conf_desc = usb2::configuration::Descriptor {
wTotalLength: (usb2::configuration::Descriptor::SIZE

View file

@ -116,7 +116,7 @@ fn ep0setup(usbd: &USBD, ep0in: &mut Ep0In, state: &mut State) -> Result<(), ()>
Descriptor::Configuration { index } => {
if index == 0 {
let mut resp = heapless::Vec::<u8, heapless::consts::U64>::new();
let mut resp = heapless::Vec::<u8, 64>::new();
let conf_desc = usb2::configuration::Descriptor {
wTotalLength: (usb2::configuration::Descriptor::SIZE

View file

@ -3,7 +3,7 @@
#![no_std]
use cortex_m_rt::entry;
use heapless::{consts, Vec};
use heapless::Vec;
// this imports `beginner/apps/lib.rs` to retrieve our global logger + panicking-behavior
use firmware as _;
@ -12,21 +12,18 @@ fn main() -> ! {
dk::init().unwrap();
// a stack-allocated `Vec` with capacity for 6 bytes
let mut buffer = Vec::<u8, consts::U6>::new();
// ^^ capacity; this is a type not a value
let mut buffer = Vec::<u8, 6>::new();
// content type ^^ ^ capacity
// `heapless::Vec` exposes the same API surface as `std::Vec` but some of its methods return a
// `Result` to indicate whether the operation failed due to the `heapless::Vec` being full
defmt::info!("start: {:?}", defmt::Debug2Format(&buffer));
// ^^^^^^^^^^^^^^^^^^^ this adapter is currently needed to log
// `heapless` data structures (like `Vec` here)
// with `defmt`
defmt::info!("start: {:?}", buffer);
buffer.push(0).expect("buffer full");
defmt::info!("after `push`: {:?}", defmt::Debug2Format(&buffer));
defmt::info!("after `push`: {:?}", buffer);
buffer.extend_from_slice(&[1, 2, 3]).expect("buffer full");
defmt::info!("after `extend`: {:?}", defmt::Debug2Format(&buffer));
defmt::info!("after `extend`: {:?}", &buffer);
// TODO try this operation
// buffer.extend_from_slice(&[4, 5, 6, 7]).expect("buffer full");

View file

@ -14,6 +14,10 @@ panic-probe = { version = "0.3.0", features = ["print-defmt"] }
defmt = "0.3.0"
defmt-rtt = "0.3.1"
[dependencies.heapless]
version = "0.7.1"
features = ["defmt-impl"]
# optimize code in both profiles
[profile.dev]
codegen-units = 1

View file

@ -6,7 +6,7 @@ use cortex_m_rt::entry;
// NOTE you can use `FnvIndexMap` instead of `LinearMap`; the former may have better
// lookup performance when the dictionary contains a large number of items but performance is
// not important for this exercise
use heapless::{consts, LinearMap};
use heapless::LinearMap;
// this imports `beginner/apps/lib.rs` to retrieve our global logger + panicking-behavior
use apps as _;
@ -15,8 +15,9 @@ fn main() -> ! {
dk::init().unwrap();
// a dictionary with capacity for 2 elements
let mut dict = LinearMap::<_, _, consts::U2>::new();
// ^^ capacity; this is a type not a value
let mut dict = LinearMap::<_, _, 2>::new();
// content types ^^ ^^ ^ capacity
// (inferred by rust)
// do some insertions
dict.insert(b'A', b'*').expect("dictionary full");

View file

@ -4,7 +4,7 @@
use cortex_m_rt::entry;
use dk::ieee802154::{Channel, Packet};
use heapless::{consts, LinearMap};
use heapless::LinearMap;
// this imports `beginner/apps/lib.rs` to retrieve our global logger + panicking-behavior
use apps as _;
@ -20,7 +20,7 @@ fn main() -> ! {
radio.set_channel(Channel::_25);
// capacity (128) should be large enough for the ASCII range
let dict = LinearMap::<u8, u8, consts::U128>::new();
let mut dict = LinearMap::<u8, u8, 128>::new();
let mut packet = Packet::new();
// TODO do the whole ASCII range [0, 127]

View file

@ -5,7 +5,7 @@
use core::str;
use cortex_m_rt::entry;
use heapless::{consts, Vec};
use heapless::Vec;
// this imports `beginner/apps/lib.rs` to retrieve our global logger + panicking-behavior
use apps as _;
@ -14,17 +14,15 @@ 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
let mut buffer = Vec::<u8, 2>::new();
// content type ^^ ^ capacity
// do some insertions
buffer.push(b'H').expect("buffer full");
buffer.push(b'i').expect("buffer full");
// look into the contents so far
defmt::info!("{:?}", defmt::Debug2Format(&buffer));
// ^^^^^^^^^^^^^^^^^^^ this adapter is currently needed to log `heapless`
// data structures (like `Vec` here) with `defmt`
defmt::info!("{:?}", buffer);
// or more readable
// NOTE utf-8 conversion works as long as you only push bytes in the ASCII range (0..=127)

View file

@ -6,7 +6,7 @@ use core::str;
use cortex_m_rt::entry;
use dk::ieee802154::{Channel, Packet};
use heapless::{consts, Vec};
use heapless::Vec;
// this imports `beginner/apps/lib.rs` to retrieve our global logger + panicking-behavior
use apps as _;
@ -38,7 +38,7 @@ fn main() -> ! {
);
/* # Decrypt the string */
let mut buf = Vec::<u8, consts::U128>::new();
let mut buf = Vec::<u8, 128>::new();
// iterate over the bytes
for input in packet.iter() {

View file

@ -6,7 +6,7 @@ use core::str;
use cortex_m_rt::entry;
use dk::ieee802154::{Channel, Packet};
use heapless::{consts, LinearMap, Vec};
use heapless::{LinearMap, Vec};
// this imports `beginner/apps/lib.rs` to retrieve our global logger + panicking-behavior
use apps as _;
@ -22,7 +22,7 @@ fn main() -> ! {
radio.set_channel(Channel::_25);
/* # Build a dictionary */
let dict = LinearMap::<u8, u8, consts::U128>::new();
let dict = LinearMap::<u8, u8, 128>::new();
let mut packet = Packet::new();
for source in 0..=127 {
@ -62,7 +62,7 @@ fn main() -> ! {
);
/* # Decrypt the string */
let mut buffer = Vec::<u8, consts::U128>::new();
let mut buffer = Vec::<u8, 128>::new();
// iterate over the bytes
for byte in packet.iter() {

View file

@ -6,7 +6,7 @@ use core::str;
use cortex_m_rt::entry;
use dk::ieee802154::{Channel, Packet};
use heapless::{consts, LinearMap, Vec};
use heapless::{LinearMap, Vec};
// this imports `beginner/apps/lib.rs` to retrieve our global logger + panicking-behavior
use apps as _;
@ -22,7 +22,7 @@ fn main() -> ! {
radio.set_channel(Channel::_25);
/* # Build a dictionary */
let dict = LinearMap::<u8, u8, consts::U128>::new();
let dict = LinearMap::<u8, u8, 128>::new();
let mut packet = Packet::new();
for source in 0..=127 {
@ -62,7 +62,7 @@ fn main() -> ! {
);
/* # Decrypt the string */
let mut buffer = Vec::<u8, consts::U128>::new();
let mut buffer = Vec::<u8, 128>::new();
// iterate over the bytes
for byte in packet.iter() {

View file

@ -6,7 +6,7 @@ use core::str;
use cortex_m_rt::entry;
use dk::ieee802154::{Channel, Packet};
use heapless::{consts, LinearMap};
use heapless::LinearMap;
// this imports `beginner/apps/lib.rs` to retrieve our global logger + panicking-behavior
use apps as _;
@ -22,7 +22,7 @@ fn main() -> ! {
radio.set_channel(Channel::_25);
/* # Build a dictionary */
let mut dict = LinearMap::<u8, u8, consts::U128>::new();
let mut dict = LinearMap::<u8, u8, 128>::new();
// the IEEE 802.15.4 packet that will carry our data
let mut packet = Packet::new();

View file

@ -6,7 +6,7 @@ use core::str;
use cortex_m_rt::entry;
use dk::ieee802154::{Channel, Packet};
use heapless::{consts, LinearMap, Vec};
use heapless::{LinearMap, Vec};
// this imports `beginner/apps/lib.rs` to retrieve our global logger + panicking-behavior
use apps as _;
@ -22,8 +22,8 @@ fn main() -> ! {
radio.set_channel(Channel::_25);
/* # Build a dictionary */
let mut dict = LinearMap::<u8, u8, consts::U128>::new();
// ^^^^^^^^^^^^ NOTE larger capacity
let mut dict = LinearMap::<u8, u8, 128>::new();
// ^^^ NOTE larger capacity
// the IEEE 802.15.4 packet that will carry our data
let mut packet = Packet::new();
@ -66,7 +66,7 @@ fn main() -> ! {
);
/* # Decrypt the string */
let mut buffer = Vec::<u8, consts::U128>::new();
let mut buffer = Vec::<u8, 128>::new();
// iterate over the bytes
for cipherletter in packet.iter() {