File: blocking.rs

package info (click to toggle)
rust-nusb 0.1.13-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 548 kB
  • sloc: makefile: 2
file content (83 lines) | stat: -rw-r--r-- 2,317 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
use std::time::Duration;

use nusb::transfer::{Control, ControlType, Recipient};

fn main() {
    env_logger::init();
    let di = nusb::list_devices()
        .unwrap()
        .find(|d| d.vendor_id() == 0x59e3 && d.product_id() == 0x0a23)
        .expect("device should be connected");

    println!("Device info: {di:?}");

    let device = di.open().unwrap();

    // Linux can make control transfers without claiming an interface
    #[cfg(any(target_os = "linux", target_os = "macos"))]
    {
        let result = device.control_out_blocking(
            Control {
                control_type: ControlType::Vendor,
                recipient: Recipient::Device,
                request: 0x81,
                value: 0x9999,
                index: 0x9999,
            },
            &[1, 2, 3, 4],
            Duration::from_secs(1),
        );
        println!("{result:?}");

        let mut buf = [0; 64];

        let len = device
            .control_in_blocking(
                Control {
                    control_type: ControlType::Vendor,
                    recipient: Recipient::Device,
                    request: 0x81,
                    value: 0x9999,
                    index: 0x9999,
                },
                &mut buf,
                Duration::from_secs(1),
            )
            .unwrap();

        println!("{result:?}, {data:?}", data = &buf[..len]);
    }

    // but we also provide an API on the `Interface` to support Windows
    let interface = device.claim_interface(0).unwrap();

    let result = interface.control_out_blocking(
        Control {
            control_type: ControlType::Vendor,
            recipient: Recipient::Device,
            request: 0x81,
            value: 0x9999,
            index: 0x9999,
        },
        &[1, 2, 3, 4, 5],
        Duration::from_secs(1),
    );
    println!("{result:?}");

    let mut buf = [0; 64];

    let len = interface
        .control_in_blocking(
            Control {
                control_type: ControlType::Vendor,
                recipient: Recipient::Device,
                request: 0x81,
                value: 0x9999,
                index: 0x9999,
            },
            &mut buf,
            Duration::from_secs(1),
        )
        .unwrap();
    println!("{data:?}", data = &buf[..len]);
}