Skip to content

Commit

Permalink
extract reader loop into run_reader()
Browse files Browse the repository at this point in the history
  • Loading branch information
ahihi committed Jul 1, 2024
1 parent 6e790c2 commit 720c99d
Showing 1 changed file with 68 additions and 184 deletions.
252 changes: 68 additions & 184 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,6 @@ fn run() -> Result<()> {
let config: Config = serde_json::from_reader(reader)?;
println!("config: {:?}", config);

let sock = UdpSocket::bind(config.host_addr).unwrap();

let mut context = Context::new().unwrap();

match open_device(&mut context, config.vendor_id, config.product_id) {
Expand Down Expand Up @@ -270,72 +268,15 @@ fn run() -> Result<()> {
configure_endpoint(&mut handle, &ctrl_in_endpoint).unwrap();
configure_endpoint(&mut handle, &ctrl_out_endpoint).unwrap();

experiment_send_mini_init(&mut handle, ctrl_out_endpoint.address).unwrap();
// experiment_send_init(&mut handle, ctrl_out_endpoint.address).unwrap();
// return Ok(());
write_init(&mut handle, ctrl_out_endpoint.address).unwrap();

thread::scope(|s| {
let writer_thread = s.spawn(|| {
run_writer(&config, &handle, &ctrl_out_endpoint);
run_writer(&config, &handle, &ctrl_out_endpoint).unwrap();
});

let mut all_bytes = [0u8; 8];

let mut xfader_hi = 0x00u8;
let mut xfader_lo = 0x00u8;

loop {
if let Ok(num_bytes) = handle.read_interrupt(ctrl_in_endpoint.address, &mut all_bytes, default_timeout) {
// println!("read({:?}): {:02x?}", num_bytes, &all_bytes[..num_bytes]);
let mut i = 0;
while i+1 < num_bytes {
if all_bytes[i] == 0xb0 {
i += 1;
continue
}

let bytes = &all_bytes[i..i+2];
i += 2;

// println!("bytes: {:02x?}", bytes);

let num = bytes[0];
let val = bytes[1];

let addr: String;
let args: Vec<OscType>;

if let Some(data) = config.match_ctrl(num, val) {
addr = data.osc_addr;
args = data.osc_args;
} else {
println!("unhandled data: {:02x?}", bytes);
continue;
}

// } else if num == 0x48 {
// // xfader hi
// xfader_hi = val;
// continue;
// } else if num == 0x49 {
// // xfader lo
// xfader_lo = val;
// let val8 = (xfader_hi << 1) | (if xfader_lo != 0x00 { 1 } else { 0 });

// addr = "/xfader".to_string();
// args = vec![OscType::Float(val8 as f32 / 255.0)];

let msg = OscPacket::Message(OscMessage {
addr: addr.to_string(), // TODO: dont allocate every time
args: args,
});
// println!("osc: {:?}", msg);
let msg_buf = encoder::encode(&msg).unwrap();

sock.send_to(&msg_buf, config.osc_out_addr).unwrap();
}
}
}

run_reader(&config, &handle, &ctrl_in_endpoint).unwrap();

writer_thread.join().unwrap();

Expand All @@ -348,131 +289,11 @@ fn run() -> Result<()> {
Ok(())
}

fn experiment_send_mini_init<T: UsbContext>(handle: &mut DeviceHandle<T>, address: u8) -> Result<()> {
fn write_init<T: UsbContext>(handle: &mut DeviceHandle<T>, address: u8) -> Result<()> {
let write = |bytes| handle.write_interrupt(address, bytes, default_timeout);

// b0 looks to be a "start" byte, 00 00 is reset (all leds off)
write(&[0xb0, 0x00, 0x00])?;
// ?
// write(&[0x28, 0x00, 0x2b, 0x4a, 0x2c, 0x00, 0x2e, 0x35])?;
// ?
// write(&[0x2a, 0x02, 0x2c, 0x72, 0x2e, 0x30])?;
// set knob 0, button 0, knob 1, button 1
// write(&[0x40, 0x00, 0x70, 0x00, 0x41, 0x00, 0x71, 0x00])?;
// set knob 2, button 2, knob 3, button 3
// write(&[0x42, 0x00, 0x72, 0x00, 0x43, 0x00, 0x73, 0x00])?;
// set knob 4, button 4, knob 5, button 5
// write(&[0x44, 0x00, 0x74, 0x00, 0x45, 0x00, 0x75, 0x00])?;
// set knob 6, button 6, knob 7, button 7
// write(&[0x46, 0x00, 0x76, 0x00, 0x47, 0x00, 0x77, 0x00])?;
// set button 9, button 10, button 11, speed dial
// write(&[0x79, 0x00, 0x7a, 0x00, 0x7b, 0x00, 0x50, 0x00])?;
// set ?, knob 0, ?, knob 1
// write(&[0x48, 0x00, 0x40, 0x0c, 0x49, 0x00, 0x41, 0x0c])?;
// set ?, knob 2, ?, knob 3
// write(&[0x4a, 0x00, 0x42, 0x0c, 0x4b, 0x00, 0x43, 0x0c])?;
// set ?, knob 4, ?, knob 5
// write(&[0x4c, 0x00, 0x44, 0x0c, 0x4d, 0x00, 0x45, 0x0c])?;
// set ?, knob 6, ?, knob 7
// write(&[0x4e, 0x00, 0x46, 0x0c, 0x4f, 0x00, 0x47, 0x0c])?;
// set buttons 8,12,13,14
// write(&[0x78, 0x00, 0x7c, 0x00, 0x7d, 0x00, 0x7e, 0x00])?;
// set button 15
// write(&[0x7f, 0x00])?;
// set knob 0, knob 1, knob 2, knob 3
// write(&[0x40, 0x00, 0x41, 0x00, 0x42, 0x00, 0x43, 0x00])?;
// set knob 4, knob 5, knob 6, knob 7
// write(&[0x44, 0x00, 0x45, 0x00, 0x46, 0x00, 0x47, 0x00])?;
// set knob 0, knob 1, knob 2, knob 3
// write(&[0x40, 0x0c, 0x41, 0x0c, 0x42, 0x0c, 0x43, 0x0c])?;
// set knob 4, knob 5, knob 6, knob 7
// write(&[0x44, 0x0c, 0x45, 0x0c, 0x46, 0x0c, 0x47, 0x0c])?;

Ok(())
}

fn experiment_send_init<T: UsbContext>(handle: &mut DeviceHandle<T>, address: u8) -> Result<()> {
let write = |bytes| handle.write_interrupt(address, bytes, default_timeout);

// b0 looks to be a "start" byte, 00 00 is reset (all leds off)
write(&[0xb0, 0x00, 0x00])?;
// ?
write(&[0x28, 0x00, 0x2b, 0x4a, 0x2c, 0x00, 0x2e, 0x35])?;
// ?
write(&[0x2a, 0x02, 0x2c, 0x72, 0x2e, 0x30])?;
// set knob 0, button 0, knob 1, button 1
write(&[0x40, 0x00, 0x70, 0x00, 0x41, 0x00, 0x71, 0x00])?;
// set knob 2, button 2, knob 3, button 3
write(&[0x42, 0x00, 0x72, 0x00, 0x43, 0x00, 0x73, 0x00])?;
// set knob 4, button 4, knob 5, button 5
write(&[0x44, 0x00, 0x74, 0x00, 0x45, 0x00, 0x75, 0x00])?;
// set knob 6, button 6, knob 7, button 7
write(&[0x46, 0x00, 0x76, 0x00, 0x47, 0x00, 0x77, 0x00])?;
// set button 9, button 10, button 11, speed dial
write(&[0x79, 0x00, 0x7a, 0x00, 0x7b, 0x00, 0x50, 0x00])?;
// set ?, knob 0, ?, knob 1
write(&[0x48, 0x00, 0x40, 0x0c, 0x49, 0x00, 0x41, 0x0c])?;
// set ?, knob 2, ?, knob 3
write(&[0x4a, 0x00, 0x42, 0x0c, 0x4b, 0x00, 0x43, 0x0c])?;
// set ?, knob 4, ?, knob 5
write(&[0x4c, 0x00, 0x44, 0x0c, 0x4d, 0x00, 0x45, 0x0c])?;
// set ?, knob 6, ?, knob 7
write(&[0x4e, 0x00, 0x46, 0x0c, 0x4f, 0x00, 0x47, 0x0c])?;
// set buttons 8,12,13,14
write(&[0x78, 0x00, 0x7c, 0x00, 0x7d, 0x00, 0x7e, 0x00])?;
// set button 15
write(&[0x7f, 0x00])?;
// set knob 0, knob 1, knob 2, knob 3
write(&[0x40, 0x00, 0x41, 0x00, 0x42, 0x00, 0x43, 0x00])?;
// set knob 4, knob 5, knob 6, knob 7
write(&[0x44, 0x00, 0x45, 0x00, 0x46, 0x00, 0x47, 0x00])?;
// set knob 0, knob 1, knob 2, knob 3
write(&[0x40, 0x0c, 0x41, 0x0c, 0x42, 0x0c, 0x43, 0x0c])?;
// set knob 4, knob 5, knob 6, knob 7
write(&[0x44, 0x0c, 0x45, 0x0c, 0x46, 0x0c, 0x47, 0x0c])?;

Ok(())
}

fn experiment_send_captured_init<T: UsbContext>(handle: &mut DeviceHandle<T>, address: u8) -> Result<()> {
let write = |bytes| handle.write_interrupt(address, bytes, default_timeout);

// b0 some general "start" byte?
write(&[0xb0, 0x00, 0x00])?;
// ?
write(&[0x28, 0x00, 0x2b, 0x4a, 0x2c, 0x00, 0x2e, 0x35])?;
// ?
write(&[0x2a, 0x02, 0x2c, 0x72, 0x2e, 0x30])?;
// set knob 0, button 0, ?, button 1
write(&[0x40, 0x00, 0x70, 0x00, 0x31, 0x00, 0x71, 0x00])?;
// set knob 2, button 2, knob 3, button 3
write(&[0x42, 0x00, 0x72, 0x00, 0x43, 0x00, 0x73, 0x00])?;
// set knob 4, button 4, knob 5, button 5
write(&[0x44, 0x00, 0x74, 0x00, 0x45, 0x00, 0x75, 0x00])?;
// set knob 6, button 6, knob 6, button 7
write(&[0x46, 0x00, 0x76, 0x00, 0x47, 0x00, 0x77, 0x00])?;
// set button 9, button 10, button 11, speed dial
write(&[0x79, 0x00, 0x7a, 0x00, 0x7b, 0x00, 0x50, 0x7f])?;
// set ?, knob 0, ?, knob 1
write(&[0x48, 0x00, 0x40, 0x0c, 0x49, 0x00, 0x41, 0x0c])?;
// set ?, knob 2, ?, knob 3
write(&[0x4a, 0x00, 0x42, 0x0c, 0x4b, 0x00, 0x43, 0x0c])?;
// set ?, knob 4, ?, knob 5
write(&[0x4c, 0x00, 0x44, 0x0c, 0x4d, 0x00, 0x45, 0x0c])?;
// set ?, knob 6, ?, knob 7
write(&[0x4e, 0x00, 0x46, 0x0c, 0x4f, 0x00, 0x47, 0x0c])?;
// set buttons 8,12,13,14 - 12(user) on
write(&[0x78, 0x00, 0x7c, 0x7f, 0x7d, 0x00, 0x7e, 0x00])?;
// set button 15
write(&[0x7f, 0x00])?;
// set knob 0, knob 1, knob 2, knob 3
write(&[0x40, 0x00, 0x41, 0x00, 0x42, 0x00, 0x43, 0x00])?;
// set knob 4, knob 5, knob 6, knob 7
write(&[0x44, 0x00, 0x45, 0x00, 0x46, 0x00, 0x47, 0x00])?;
// set knob 0, knob 1, knob 2, knob 3
write(&[0x40, 0x0c, 0x41, 0x0c, 0x42, 0x0c, 0x43, 0x0c])?;
// set knob 4, knob 5, knob 6, knob 7
write(&[0x44, 0x0c, 0x45, 0x0c, 0x46, 0x0c, 0x47, 0x0c])?;

Ok(())
}
Expand Down Expand Up @@ -642,6 +463,69 @@ fn float_to_7bit(val: f32) -> u8 {
(val.max(0.0).min(1.0) * 127.0).round() as u8
}

fn run_reader<T: UsbContext>(config: &Config, handle: &DeviceHandle<T>, endpoint: &Endpoint) -> Result<()> {
let sock = UdpSocket::bind(config.host_addr)?;

let mut all_bytes = [0u8; 8];

let mut xfader_hi = 0x00u8;
let mut xfader_lo = 0x00u8;

loop {
if let Ok(num_bytes) = handle.read_interrupt(endpoint.address, &mut all_bytes, default_timeout) {
// println!("read({:?}): {:02x?}", num_bytes, &all_bytes[..num_bytes]);
let mut i = 0;
while i+1 < num_bytes {
if all_bytes[i] == 0xb0 {
i += 1;
continue
}

let bytes = &all_bytes[i..i+2];
i += 2;

// println!("bytes: {:02x?}", bytes);

let num = bytes[0];
let val = bytes[1];

let addr: String;
let args: Vec<OscType>;

if let Some(data) = config.match_ctrl(num, val) {
addr = data.osc_addr;
args = data.osc_args;
} else {
println!("unhandled data: {:02x?}", bytes);
continue;
}

// } else if num == 0x48 {
// // xfader hi
// xfader_hi = val;
// continue;
// } else if num == 0x49 {
// // xfader lo
// xfader_lo = val;
// let val8 = (xfader_hi << 1) | (if xfader_lo != 0x00 { 1 } else { 0 });

// addr = "/xfader".to_string();
// args = vec![OscType::Float(val8 as f32 / 255.0)];

let msg = OscPacket::Message(OscMessage {
addr: addr.to_string(), // TODO: dont allocate every time
args: args,
});
// println!("osc: {:?}", msg);
let msg_buf = encoder::encode(&msg)?;

sock.send_to(&msg_buf, config.osc_out_addr)?;
}
}
}

}

fn run_writer<T: UsbContext>(config: &Config, handle: &DeviceHandle<T>, endpoint: &Endpoint) -> Result<()> {
let sock = UdpSocket::bind(config.osc_in_addr)?;
println!("listening to {}", config.osc_in_addr);
Expand Down

0 comments on commit 720c99d

Please sign in to comment.