Skip to content

Commit

Permalink
USB: code clean-up
Browse files Browse the repository at this point in the history
  • Loading branch information
arteme committed Oct 27, 2024
1 parent 37ace4d commit f09f487
Show file tree
Hide file tree
Showing 5 changed files with 8 additions and 16 deletions.
12 changes: 2 additions & 10 deletions usb/src/dev_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ use std::sync::atomic::{AtomicBool, Ordering};
use std::time::Duration;
use async_trait::async_trait;
use log::{debug, error, info, trace};
use rusb::{Direction, UsbContext};
use rusb::Direction;
use tokio::sync::mpsc;
use pod_core::midi_io::{MidiIn, MidiOut};
use crate::devices::UsbDevice;
use crate::endpoint::{Endpoint, find_endpoint};
use crate::framer::{BoxedInFramer, BoxedOutFramer, InFramer};
use crate::framer::{BoxedInFramer, BoxedOutFramer};
use crate::line6::line6_read_serial;
use crate::midi_framer::new_usb_midi_framer;
use crate::podxt_framer::new_pod_xt_framer;
Expand Down Expand Up @@ -51,14 +51,6 @@ pub struct DeviceOutput {
inner: Arc<DeviceInner>
}

pub struct DevHandler {
handle: Arc<DeviceHandle>,
read_ep: Endpoint,
write_ep: Endpoint,
tx: mpsc::UnboundedSender<Vec<u8>>,
rx: mpsc::UnboundedReceiver<Vec<u8>>
}

const READ_DURATION: Duration = Duration::from_millis(10 * 1000);
const WRITE_DURATION: Duration = Duration::from_millis(10 * 1000);

Expand Down
2 changes: 1 addition & 1 deletion usb/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ pub fn usb_device_for_address(dev_addr: &str) -> Result<(impl MidiIn, impl MidiO
let port_n_re = Regex::new(r"\d+").unwrap();
let port_id_re = Regex::new(r"\d+:\d+").unwrap();

let mut found;
let found;
if port_id_re.is_match(dev_addr) {
found = devices.get_mut(dev_addr);
} else if port_n_re.is_match(dev_addr) {
Expand Down
2 changes: 1 addition & 1 deletion usb/src/midi_framer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl InFramer for UsbMidiInFramer {
let mut ret = vec![];
bytes.chunks_exact(4).for_each(|b| {
let mut push_sysex = false;
let mut sysex_ptr = &mut self.sysex_buffer[self.sysex_offset .. self.sysex_offset + 3];
let sysex_ptr = &mut self.sysex_buffer[self.sysex_offset .. self.sysex_offset + 3];
match b[0] {
0x0b => ret.push( b[1 .. 4].to_vec() ),
0x0c => ret.push( b[1 .. 3].to_vec() ),
Expand Down
4 changes: 2 additions & 2 deletions usb/src/podxt_framer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ impl PodXtInFramer {
impl InFramer for PodXtInFramer {
fn decode_incoming(&mut self, bytes: &[u8]) -> Vec<Vec<u8>> {
// add received data to the read buffer at current read offset
let mut read_ptr = &mut self.read_buffer[self.read_offset .. self.read_offset + bytes.len()];
let read_ptr = &mut self.read_buffer[self.read_offset .. self.read_offset + bytes.len()];
read_ptr.copy_from_slice(bytes);

// go through the whole receive buffer from offset 0, check for
// for messages as send them to the MIDI thread
let process_len = self.read_offset + read_ptr.len();
let mut process_buf = self.read_buffer[..process_len].as_mut();
let process_buf = self.read_buffer[..process_len].as_mut();
let mut process_offset = 0;
let mut ret = vec![];
loop {
Expand Down
4 changes: 2 additions & 2 deletions usb/src/usb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::thread;
use std::time::Duration;
use log::{debug, error, info};
use rusb::{Context, Hotplug, HotplugBuilder, Registration, UsbContext};
use rusb::constants::{LIBUSB_ENDPOINT_DIR_MASK, LIBUSB_ENDPOINT_IN, LIBUSB_ENDPOINT_OUT, LIBUSB_ERROR_INTERRUPTED, LIBUSB_TRANSFER_CANCELLED, LIBUSB_TRANSFER_TYPE_BULK};
use rusb::constants::{LIBUSB_ENDPOINT_DIR_MASK, LIBUSB_ENDPOINT_IN, LIBUSB_ENDPOINT_OUT, LIBUSB_TRANSFER_TYPE_BULK};
use rusb::ffi::{libusb_alloc_transfer, libusb_cancel_transfer, libusb_free_transfer, libusb_submit_transfer, libusb_transfer};
use crate::check;
use crate::util::usb_address_string;
Expand Down Expand Up @@ -390,7 +390,7 @@ pub mod libusb {
}

/// Resets the specified device handle.
pub(super) fn reset<T: UsbContext>(mut hdl: DeviceHandle<T>) -> rusb::Result<DeviceHandle<T>> {
pub(super) fn reset<T: UsbContext>(hdl: DeviceHandle<T>) -> rusb::Result<DeviceHandle<T>> {
let dev = hdl.device();
let port = dev.port_numbers()?;
// WinUSB API with libusbK driver requires interface 0 to be claimed in
Expand Down

0 comments on commit f09f487

Please sign in to comment.