2020-06-09 09:52:27 +00:00
|
|
|
#![deny(unused_must_use)]
|
|
|
|
#![no_main]
|
|
|
|
#![no_std]
|
|
|
|
|
2020-07-13 14:56:04 +00:00
|
|
|
use core::{convert::TryFrom, fmt::Write as _};
|
2020-06-09 09:52:27 +00:00
|
|
|
|
2020-07-13 14:56:04 +00:00
|
|
|
use async_core::unsync::Mutex;
|
2020-06-09 09:52:27 +00:00
|
|
|
use hal::{
|
2020-07-13 14:56:04 +00:00
|
|
|
radio::{self, Channel},
|
2020-06-09 09:52:27 +00:00
|
|
|
usbd,
|
|
|
|
};
|
|
|
|
use heapless::{consts, String};
|
|
|
|
use panic_abort as _;
|
|
|
|
|
|
|
|
#[no_mangle]
|
|
|
|
fn main() -> ! {
|
2020-07-13 14:56:04 +00:00
|
|
|
let stx = Mutex::new(usbd::serial());
|
|
|
|
let (mut hidout, _) = usbd::hid();
|
|
|
|
let (rtx, mut rrx) = radio::claim(Channel::_20);
|
|
|
|
|
|
|
|
let mut output = String::<consts::U128>::new();
|
2020-06-09 09:52:27 +00:00
|
|
|
|
|
|
|
output.push_str("deviceid=").ok();
|
|
|
|
write!(output, "{:08x}{:08x}", hal::deviceid1(), hal::deviceid0()).ok();
|
2020-07-13 14:56:04 +00:00
|
|
|
write!(
|
|
|
|
output,
|
|
|
|
" channel={} TxPower=+8dBm app=loopback.hex\n",
|
|
|
|
rtx.channel()
|
|
|
|
)
|
|
|
|
.ok();
|
|
|
|
|
|
|
|
let rtx = Mutex::new(rtx);
|
|
|
|
|
|
|
|
let t1 = async {
|
|
|
|
let mut output = String::<consts::U128>::new();
|
|
|
|
let mut hidbuf = usbd::Packet::new().await;
|
|
|
|
let zlp = radio::Packet::new().await;
|
|
|
|
|
|
|
|
loop {
|
|
|
|
hidout.recv(&mut hidbuf).await;
|
|
|
|
semidap::info!("HID: {}", *hidbuf);
|
|
|
|
|
|
|
|
let arg = if hidbuf.len() == 1 {
|
|
|
|
// Linux / macOS
|
|
|
|
Some(hidbuf[0])
|
|
|
|
} else if hidbuf.len() == 64 {
|
|
|
|
// Windows (it zero pads the packet)
|
|
|
|
Some(hidbuf[0])
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(arg) = arg {
|
|
|
|
if let Ok(chan) = Channel::try_from(arg) {
|
|
|
|
let mut rtx = rtx.lock().await;
|
|
|
|
rtx.set_channel(chan);
|
|
|
|
// send a zero-length packet to force the radio to listen on the new channel
|
|
|
|
rtx.write(&zlp).await.ok();
|
|
|
|
drop(rtx);
|
|
|
|
|
|
|
|
output.clear();
|
|
|
|
writeln!(output, "now listening on channel {}", chan).ok();
|
|
|
|
stx.lock().await.write(output.as_bytes());
|
|
|
|
} else {
|
|
|
|
stx.lock()
|
|
|
|
.await
|
|
|
|
.write(b"requested channel is out of range (11-26)\n");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
stx.lock().await.write(b"invalid HID packet\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2020-06-09 09:52:27 +00:00
|
|
|
|
2020-07-13 14:56:04 +00:00
|
|
|
let t2 = async {
|
|
|
|
let mut packet = radio::Packet::new().await;
|
|
|
|
stx.lock().await.write(output.as_bytes());
|
2020-06-09 09:52:27 +00:00
|
|
|
|
|
|
|
loop {
|
|
|
|
let crcres = rrx.read(&mut packet).await;
|
2020-07-09 16:58:02 +00:00
|
|
|
let len = packet.len();
|
|
|
|
let lqi = if len >= 3 {
|
2020-06-09 09:52:27 +00:00
|
|
|
Some(packet.lqi())
|
|
|
|
} else {
|
|
|
|
// packet is too small; LQI is not valid
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut busy = false;
|
|
|
|
if crcres.is_ok() {
|
2020-07-09 16:58:02 +00:00
|
|
|
packet.reverse();
|
2020-07-13 14:56:04 +00:00
|
|
|
busy = rtx.lock().await.write(&packet).await.is_err();
|
2020-06-09 09:52:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
output.clear();
|
|
|
|
write!(
|
|
|
|
&mut output,
|
|
|
|
"received {} byte{}",
|
|
|
|
len,
|
|
|
|
if len == 1 { "" } else { "s" }
|
|
|
|
)
|
|
|
|
.ok();
|
|
|
|
|
|
|
|
let (res, crc) = match crcres {
|
|
|
|
Ok(x) => ("Ok", x),
|
|
|
|
Err(x) => ("Err", x),
|
|
|
|
};
|
|
|
|
|
|
|
|
write!(&mut output, " (CRC={}({:#06x})", res, crc).ok();
|
|
|
|
if let Some(lqi) = lqi {
|
|
|
|
write!(&mut output, ", LQI={}", lqi).ok();
|
|
|
|
}
|
|
|
|
output.push_str(")\n").ok();
|
|
|
|
|
|
|
|
if busy {
|
|
|
|
output.push_str("didn't reply -- channel was busy\n").ok();
|
2020-07-13 14:56:04 +00:00
|
|
|
stx.lock().await.write(output.as_bytes());
|
2020-06-09 09:52:27 +00:00
|
|
|
}
|
|
|
|
|
2020-07-13 14:56:04 +00:00
|
|
|
stx.lock().await.write(output.as_bytes());
|
2020-06-09 09:52:27 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-07-13 14:56:04 +00:00
|
|
|
executor::run!(t1, t2)
|
2020-06-09 09:52:27 +00:00
|
|
|
}
|