Updated control flow + cleaned some code
This commit is contained in:
@@ -32,16 +32,16 @@ mod example {
|
|||||||
|
|
||||||
/// Helper function to write on console the received packet.
|
/// Helper function to write on console the received packet.
|
||||||
fn print_rx_buffer(buf: &mut [u8]) {
|
fn print_rx_buffer(buf: &mut [u8]) {
|
||||||
|
if let Some((last, bytes)) = buf.split_last() {
|
||||||
let mut console = Console::new();
|
let mut console = Console::new();
|
||||||
write!(console, "RX:").unwrap();
|
write!(console, "RX:").unwrap();
|
||||||
if let Some((last, bytes)) = buf.split_last() {
|
|
||||||
for byte in bytes {
|
for byte in bytes {
|
||||||
write!(console, " {:02x?}", byte).unwrap();
|
write!(console, " {:02x?}", byte).unwrap();
|
||||||
}
|
}
|
||||||
writeln!(console, " {:02x?}", last).unwrap();
|
writeln!(console, " {:02x?}", last).unwrap();
|
||||||
}
|
|
||||||
console.flush();
|
console.flush();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Function to identify the time elapsed for a transmission request.
|
/// Function to identify the time elapsed for a transmission request.
|
||||||
fn bench_transmit(console: &mut Console, timer: &Timer, title: &str, mut buf: &mut [u8]) {
|
fn bench_transmit(console: &mut Console, timer: &Timer, title: &str, mut buf: &mut [u8]) {
|
||||||
@@ -65,25 +65,27 @@ mod example {
|
|||||||
console.flush();
|
console.flush();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn receive_packet(console: &mut Console, mut buf: &mut [u8; 256]) {
|
fn receive_packet(console: &mut Console, mut buf: &mut [u8; 256]) -> bool {
|
||||||
match NfcTag::receive(&mut buf) {
|
match NfcTag::receive(&mut buf) {
|
||||||
Ok(RecvOp {
|
Ok(RecvOp {
|
||||||
recv_amount: amount,
|
recv_amount: amount,
|
||||||
..
|
..
|
||||||
}) => {
|
}) => {
|
||||||
if amount > 0 && amount <= buf.len() {
|
if amount <= buf.len() {
|
||||||
print_rx_buffer(&mut buf[..amount]);
|
print_rx_buffer(&mut buf[..amount]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(TockError::Command(CommandError {
|
Err(TockError::Command(CommandError {
|
||||||
return_code: -4, /* EOFF: Not Ready */
|
return_code: -4, /* EOFF: Not Ready */
|
||||||
..
|
..
|
||||||
})) => (),
|
})) => return false,
|
||||||
|
// For the example app, just print any other received error without handling.
|
||||||
Err(TockError::Command(CommandError {
|
Err(TockError::Command(CommandError {
|
||||||
return_code: value, ..
|
return_code: value, ..
|
||||||
})) => writeln!(console, " -- Err({})!", value).unwrap(),
|
})) => writeln!(console, " -- Err({})!", value).unwrap(),
|
||||||
Err(_) => writeln!(console, " -- RX Err").unwrap(),
|
Err(_) => writeln!(console, " -- RX Err").unwrap(),
|
||||||
}
|
}
|
||||||
|
true
|
||||||
}
|
}
|
||||||
|
|
||||||
fn transmit_reply(mut console: &mut Console, timer: &Timer, buf: &[u8]) -> bool {
|
fn transmit_reply(mut console: &mut Console, timer: &Timer, buf: &[u8]) -> bool {
|
||||||
@@ -156,29 +158,24 @@ mod example {
|
|||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
let mut is_enabled = false;
|
|
||||||
let mut state_change_counter = 0;
|
let mut state_change_counter = 0;
|
||||||
loop {
|
loop {
|
||||||
if is_enabled {
|
while !NfcTag::enable_emulation() {}
|
||||||
let mut rx_buf = [0; 256];
|
// Configure Type 4 tag
|
||||||
|
while !NfcTag::configure(4) {}
|
||||||
|
state_change_counter += 1;
|
||||||
loop {
|
loop {
|
||||||
receive_packet(&mut console, &mut rx_buf);
|
let mut rx_buf = [0; 256];
|
||||||
|
// Await a successful receive
|
||||||
|
while !receive_packet(&mut console, &mut rx_buf) {}
|
||||||
// If the reader restarts the communication and we can't
|
// If the reader restarts the communication and we can't
|
||||||
// reply to the received packet, then disable the tag.
|
// reply to the received packet, then disable the tag.
|
||||||
if !transmit_reply(&mut console, &timer, &rx_buf) {
|
if !transmit_reply(&mut console, &timer, &rx_buf) {
|
||||||
is_enabled = false;
|
state_change_counter += 1;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
if state_change_counter > 100 {
|
||||||
NfcTag::enable_emulation();
|
|
||||||
// Configure Type 4 tag
|
|
||||||
if NfcTag::configure(4) {
|
|
||||||
is_enabled = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
state_change_counter += 1;
|
|
||||||
if !is_enabled && state_change_counter > 100 {
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user