Updated control flow + cleaned some code

This commit is contained in:
Mirna
2020-11-05 10:38:59 +02:00
parent 0eaa2b5291
commit 203367b081

View File

@@ -32,15 +32,15 @@ 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]) {
let mut console = Console::new();
write!(console, "RX:").unwrap();
if let Some((last, bytes)) = buf.split_last() { if let Some((last, bytes)) = buf.split_last() {
let mut console = Console::new();
write!(console, "RX:").unwrap();
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.
@@ -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() {}
// Configure Type 4 tag
while !NfcTag::configure(4) {}
state_change_counter += 1;
loop {
let mut rx_buf = [0; 256]; let mut rx_buf = [0; 256];
loop { // Await a successful receive
receive_packet(&mut console, &mut rx_buf); 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 {
NfcTag::enable_emulation();
// Configure Type 4 tag
if NfcTag::configure(4) {
is_enabled = true;
} }
} }
state_change_counter += 1; if state_change_counter > 100 {
if !is_enabled && state_change_counter > 100 {
break; break;
} }
} }