diff options
Diffstat (limited to 'sw/eval-clock-cw-tx/src/usb.rs')
-rw-r--r-- | sw/eval-clock-cw-tx/src/usb.rs | 92 |
1 files changed, 46 insertions, 46 deletions
diff --git a/sw/eval-clock-cw-tx/src/usb.rs b/sw/eval-clock-cw-tx/src/usb.rs index 467c148..a093850 100644 --- a/sw/eval-clock-cw-tx/src/usb.rs +++ b/sw/eval-clock-cw-tx/src/usb.rs @@ -64,7 +64,6 @@ enum Font { // Shared between application and interrupt context struct USBSharedData { incoming_buf: ArrayVec::<[u8; MESSAGE_LEN]>, - message : ArrayString::<[u8; MESSAGE_LEN]>, feldhell_pattern: ArrayVec::<[u16; FELDHELL_PATTERN_LEN]>, // FELDHELL columns to transmit } @@ -117,7 +116,6 @@ impl USBData { let shared_usb = &mut *SHARED_USB.as_mut_ptr(); *shared_usb = USBSharedData { incoming_buf : ArrayVec::new(), - message : ArrayString::new(), feldhell_pattern : ArrayVec::new(), } } @@ -131,23 +129,56 @@ impl USBData { } } + fn send_once(&mut self, message : &[u8]) { + cortex_m::interrupt::free(|_cs| { + let serial = unsafe { USB_SERIAL.as_mut().unwrap() }; + serial.write(message).ok(); + }); + } + + pub fn send_receive(&mut self) { + self.send_once(b"Switch RX\n"); + } + + pub fn send_transmit(&mut self) { + self.send_once(b"Switch TX\n"); + } + pub fn handle(&mut self) { let in_message = cortex_m::interrupt::free(|_cs| { let shared_usb = unsafe { &mut *SHARED_USB.as_mut_ptr() }; - if shared_usb.message.len() > 0 { - let mut outgoing = ArrayString::<[u8; MESSAGE_LEN]>::new(); + let mut message = ArrayString::<[u8; MESSAGE_LEN]>::new(); + + let mut split_at = None; + for (i, c) in shared_usb.incoming_buf.iter().enumerate() { + if *c == b'\n' { + split_at = Some(i); + break; + } + } + + match split_at { + Some(i) if i+1 == shared_usb.incoming_buf.len() => { + write!(&mut message, "{}", core::str::from_utf8(&shared_usb.incoming_buf).unwrap()).ok(); + shared_usb.incoming_buf.clear(); + } + Some(i) => { + let (front, back) = shared_usb.incoming_buf.split_at(i+1); - if let Err(_) = write!(outgoing, "{}", &shared_usb.message) { - outgoing.clear(); - write!(outgoing, "EXT FAIL\n").ok(); + write!(&mut message, "{}", core::str::from_utf8(front).unwrap()).ok(); + + let mut keep = ArrayVec::<[u8; MESSAGE_LEN]>::new(); + keep.try_extend_from_slice(back).ok(); + + shared_usb.incoming_buf.clear(); + shared_usb.incoming_buf.try_extend_from_slice(&keep).ok(); } - let serial = unsafe { USB_SERIAL.as_mut().unwrap() }; - serial.write(outgoing.as_bytes()).ok(); + _ => { } + }; - let in_message = shared_usb.message.clone(); - shared_usb.message.clear(); - Some(in_message) + if message.len() > 0 { + Some(message) } else { None @@ -162,6 +193,7 @@ impl USBData { } else if m.as_str() == "rx\n" { outgoing.try_extend_from_slice(b"RX ok\n").ok(); + self.current_str.clear(); self.transmit = false; } else if m.chars().nth(0).unwrap() == 'm' { @@ -171,10 +203,6 @@ impl USBData { } outgoing.try_extend_from_slice(b"m ok\n").ok(); } - else if m.as_str() == "clear\n" { - self.current_str.clear(); - outgoing.try_extend_from_slice(b"clear ok\n").ok(); - } else if m.as_str() == "fontw\n" { self.font = Font::Wide; outgoing.try_extend_from_slice(b"fontw ok\n").ok(); @@ -275,44 +303,16 @@ fn usb_interrupt() { return; } - let mut buf = [0u8; 64]; + let mut buf = [0u8; MESSAGE_LEN]; let shared_usb = unsafe { &mut *SHARED_USB.as_mut_ptr() }; match serial.read(&mut buf) { Ok(count) if count > 0 => { - if count > 64 { + if count > MESSAGE_LEN { panic!("illegal count"); } shared_usb.incoming_buf.try_extend_from_slice(&buf[0..count]).ok(); - - let mut split_at = None; - for (i, c) in shared_usb.incoming_buf.iter().enumerate() { - if *c == b'\n' { - split_at = Some(i); - break; - } - } - - match split_at { - Some(i) if i+1 == shared_usb.incoming_buf.len() => { - write!(&mut shared_usb.message, "{}", core::str::from_utf8(&shared_usb.incoming_buf).unwrap()).ok(); - shared_usb.incoming_buf.clear(); - } - Some(i) => { - let (front, back) = shared_usb.incoming_buf.split_at(i+1); - - write!(&mut shared_usb.message, "{}", core::str::from_utf8(front).unwrap()).ok(); - - let mut keep = ArrayVec::<[u8; MESSAGE_LEN]>::new(); - keep.try_extend_from_slice(back).ok(); - - shared_usb.incoming_buf.clear(); - shared_usb.incoming_buf.try_extend_from_slice(&keep).ok(); - } - _ => { } - }; - } _ => {} } |