-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9cc20b6
commit ac4ff66
Showing
1 changed file
with
51 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
#![no_main] | ||
#![no_std] | ||
|
||
use cortex_m_rt::entry; | ||
use core::fmt::Write; | ||
use heapless::Vec; | ||
use microbit::hal::uarte::{self, Baudrate, Parity}; | ||
use panic_rtt_target as _; | ||
use rtt_target::rtt_init_print; | ||
use serial_setup::UartePort; | ||
|
||
#[entry] | ||
fn main() -> ! { | ||
rtt_init_print!(); | ||
let board = microbit::Board::take().unwrap(); | ||
|
||
let mut serial = { | ||
let serial = uarte::Uarte::new( | ||
board.UARTE0, | ||
board.uart.into(), | ||
Parity::EXCLUDED, | ||
Baudrate::BAUD115200, | ||
); | ||
UartePort::new(serial) | ||
}; | ||
|
||
// A buffer with 32 bytes of capacity | ||
let mut buffer: Vec<u8, 32> = Vec::new(); | ||
|
||
loop { | ||
buffer.clear(); | ||
|
||
loop { | ||
// We assume that the receiving cannot fail | ||
let byte = serial.read().unwrap(); | ||
|
||
if buffer.push(byte).is_err() { | ||
write!(serial, "error: buffer full\r\n").unwrap(); | ||
break; | ||
} | ||
|
||
if byte == b'\r' { | ||
for byte in buffer.iter().rev().chain(&[b'\n', b'\r']) { | ||
serial.write(*byte).unwrap(); | ||
} | ||
break; | ||
} | ||
} | ||
serial.flush().unwrap() | ||
} | ||
} |