From b3170f0cdb1b3b2ff13225f583041dc678250a37 Mon Sep 17 00:00:00 2001 From: "Matthias P. Braendli" Date: Sun, 6 Jun 2021 18:13:54 +0200 Subject: Improve USB handling and send switch messages --- sw/eval-clock-cw-tx/src/main.rs | 8 +++- sw/eval-clock-cw-tx/src/usb.rs | 92 ++++++++++++++++++++--------------------- 2 files changed, 53 insertions(+), 47 deletions(-) (limited to 'sw') diff --git a/sw/eval-clock-cw-tx/src/main.rs b/sw/eval-clock-cw-tx/src/main.rs index 716d184..2dce7bc 100644 --- a/sw/eval-clock-cw-tx/src/main.rs +++ b/sw/eval-clock-cw-tx/src/main.rs @@ -197,7 +197,7 @@ fn main() -> ! { ui, cw_pwm, cw_keyer : cw::Keyer::new(12, TICKS_PER_SECOND), - cw_paddle_tip, cw_paddle_ring, ptt_out, seq_switch, led + cw_paddle_tip, cw_paddle_ring, ptt_out, seq_switch, led, }; si_clock::SiClock::new(i2c_busmanager.acquire_i2c(), 0, shared.state.vfo_display()) @@ -264,6 +264,12 @@ fn main() -> ! { update_disp_required = true; } + match (previous_state.clone(), state.sequence_state.clone()) { + (SequenceState::Rx, SequenceState::Switching(_)) => usb.send_transmit(), + (SequenceState::Switching(_), SequenceState::Rx) => usb.send_receive(), + _ => (), + } + let vfo = state.vfo_display(); if previous_vfo != vfo || previous_state != state.sequence_state { siclock.set_vfo(state.vfo_siclock()); 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(); - } - _ => { } - }; - } _ => {} } -- cgit v1.2.3