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

55 lines
1.6 KiB
Rust
Raw Normal View History

2020-06-22 13:23:59 +00:00
#![deny(unused_must_use)]
#![no_main]
#![no_std]
use cortex_m_rt::entry;
use dk::ieee802154::{Channel, Packet};
2021-06-04 11:41:31 +00:00
use heapless::LinearMap;
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
const TEN_MS: u32 = 10_000;
#[entry]
fn main() -> ! {
let board = dk::init().unwrap();
let mut radio = board.radio;
let mut timer = board.timer;
// puzzle.hex uses channel 25
radio.set_channel(Channel::_25);
// capacity (128) should be large enough for the ASCII range
2021-06-04 11:41:31 +00:00
let mut dict = LinearMap::<u8, u8, 128>::new();
2020-06-22 13:23:59 +00:00
let mut packet = Packet::new();
// TODO do the whole ASCII range [0, 127]
for source in b'A'..=b'B' {
packet.copy_from_slice(&[source]);
2021-03-16 12:26:51 +00:00
radio.send(&mut packet);
2020-06-22 13:23:59 +00:00
if radio.recv_timeout(&mut packet, &mut timer, TEN_MS).is_ok() {
// response should be one byte large
if packet.len() == 1 {
let _destination = packet[0];
2020-06-22 13:23:59 +00:00
// TODO insert the key-value pair
// dict.insert(/* ? */, /* ? */).expect("dictionary full");
} else {
2021-04-12 09:51:44 +00:00
defmt::error!("response packet was not a single byte");
2020-06-22 13:23:59 +00:00
dk::exit()
}
} else {
2021-04-12 09:51:44 +00:00
defmt::error!("no response or response packet was corrupted");
2020-06-22 13:23:59 +00:00
dk::exit()
}
}
2021-04-12 09:51:44 +00:00
defmt::info!("{:?}", defmt::Debug2Format(&dict));
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 `LinearMap` here) with `defmt`
2020-06-22 13:23:59 +00:00
dk::exit()
}