Skip to content

Commit

Permalink
Updating serial after review
Browse files Browse the repository at this point in the history
  • Loading branch information
ryan-summers committed Oct 16, 2023
1 parent 8e04452 commit 2bd057c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 17 deletions.
48 changes: 33 additions & 15 deletions src/hardware/serial_terminal.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,45 @@
use super::UsbBus;
use core::fmt::Write;

static OUTPUT_BUFFER: bbqueue::BBBuffer<1024> = bbqueue::BBBuffer::new();
static OUTPUT_BUFFER: bbqueue::BBBuffer<512> = bbqueue::BBBuffer::new();

pub struct OutputBuffer {
producer: bbqueue::Producer<'static, 512>,
}

impl Write for OutputBuffer {
fn write_str(&mut self, s: &str) -> core::fmt::Result {
let data = s.as_bytes();

// Write as much data as possible to the output buffer.
let Ok(mut grant) = self.producer.grant_max_remaining(data.len()) else {
// Output buffer is full, silently drop the data.
return Ok(());
};

let len = grant.buf().len();
grant.buf().copy_from_slice(&data[..len]);
grant.commit(len);
Ok(())
}
}

pub struct SerialTerminal {
usb_device: usb_device::device::UsbDevice<'static, UsbBus>,
usb_serial: usbd_serial::SerialPort<'static, UsbBus>,
output: bbqueue::Consumer<'static, 1024>,
output: bbqueue::Consumer<'static, 512>,
buffer: OutputBuffer,
}

impl SerialTerminal {
pub fn new(
usb_device: usb_device::device::UsbDevice<'static, UsbBus>,
usb_serial: usbd_serial::SerialPort<'static, UsbBus>,
) -> Self {
let (_producer, consumer) = OUTPUT_BUFFER.try_split().unwrap();
let (producer, consumer) = OUTPUT_BUFFER.try_split().unwrap();

Self {
buffer: OutputBuffer { producer },
usb_device,
usb_serial,
output: consumer,
Expand Down Expand Up @@ -54,23 +78,17 @@ impl SerialTerminal {
match self.usb_serial.read(&mut buffer) {
Ok(count) => {
for &value in &buffer[..count] {
// Currently, just echo it back.
self.usb_serial.write(b"echo: ").ok();
self.usb_serial.write(&[value]).ok();
self.usb_serial.write(b"\n\r").ok();
writeln!(self.buffer, "echo: {}", value as char).unwrap();
}
}

Err(usbd_serial::UsbError::WouldBlock) => {}
Err(_) => {
// Flush the output buffer if USB is not connected.
self.output
.read()
.map(|grant| {
let len = grant.buf().len();
grant.release(len);
})
.ok();
// Clear the output buffer if USB is not connected.
while let Ok(grant) = self.output.read() {
let len = grant.buf().len();
grant.release(len);
}
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/hardware/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1030,10 +1030,10 @@ pub fn setup(

// Generate a device serial number from the MAC address.
let serial_number =
cortex_m::singleton!(: Option<heapless::String<64>> = None)
cortex_m::singleton!(: Option<heapless::String<17>> = None)
.unwrap();
{
let mut serial_string: heapless::String<64> =
let mut serial_string: heapless::String<17> =
heapless::String::new();
let octets = mac_addr.0;

Expand Down

0 comments on commit 2bd057c

Please sign in to comment.