logitech_led_sdk/
lib.rs

1mod color_percent;
2mod sdk;
3mod target_device;
4
5pub use self::color_percent::ColorPercent;
6pub use self::sdk::Sdk;
7pub use self::target_device::TargetDevice;
8pub use logitech_led_sdk_sys as sys;
9use std::sync::Mutex;
10pub use sys::LogiLed_DeviceType as DeviceType;
11pub use sys::LogiLed_KeyName as KeyName;
12
13/// The lock that syncs accesses to the SDK.
14///
15/// If you use raw sdk api functions anywhere, you MUST use this lock to wrap accesses to the sdk in order to prevent data races.
16/// This library does all this for you, this is exposed only for users who want to use raw sdk functions safely.
17pub static SDK_LOCK: Mutex<()> = Mutex::new(());
18
19#[cfg(test)]
20mod test {
21    use super::*;
22    use std::time::Duration;
23
24    // A simple lock to ensure that only one test runs as a time.
25    static TEST_LOCK: Mutex<()> = Mutex::new(());
26
27    #[test]
28    fn sanity_check() {
29        let _test_lock = TEST_LOCK.lock().expect("test lock poisoned");
30
31        // 1st init
32        let sdk = Sdk::new().expect("failed to init LG SDK");
33        std::thread::sleep(Duration::from_secs(5));
34        drop(sdk);
35        std::thread::sleep(Duration::from_secs(5));
36
37        // 2nd init
38        let sdk = Sdk::new_with_name("Test").expect("failed to init LG SDK");
39
40        // 3rd init fails, we already opened the 2nd.
41        assert!(Sdk::new().is_none());
42
43        std::thread::sleep(Duration::from_secs(5));
44        let _version = sdk.get_version().expect("failed to get LG SDK version");
45        assert!(sdk.set_target(TargetDevice::All));
46        assert!(sdk.set_lighting(ColorPercent::new_rgb(255, 255, 255)));
47        assert!(sdk.set_lighting_for_key_with_name(KeyName::L, ColorPercent::new_rgb(0, 255, 255)));
48        assert!(sdk.set_lighting_for_target_zone(
49            DeviceType::Mouse,
50            1,
51            ColorPercent::new_rgb(255, 0, 0)
52        ));
53        assert!(sdk.flash_lighting(
54            ColorPercent::new_rgb(255, 0, 0),
55            Some(Duration::from_millis(10_000)),
56            Duration::from_millis(100)
57        ));
58        assert!(sdk.stop_effects());
59        assert!(sdk.pulse_lighting(
60            ColorPercent::new_rgb(255, 0, 0),
61            Some(Duration::from_millis(10_000)),
62            Duration::from_millis(100)
63        ));
64        assert!(sdk.stop_effects());
65        assert!(sdk.set_lighting_for_key_with_scan_code(16, ColorPercent::new_rgb(255, 255, 255)));
66        assert!(sdk.set_lighting_for_key_with_hid_code(26, ColorPercent::new_rgb(255, 255, 255)));
67        assert!(sdk.save_lighting_for_key(KeyName::L));
68        assert!(sdk.restore_lighting_for_key(KeyName::L));
69        assert!(sdk.flash_single_key(
70            KeyName::L,
71            ColorPercent::new_rgb(255, 0, 0),
72            Some(Duration::from_millis(10_000)),
73            Duration::from_millis(100)
74        ));
75        assert!(sdk.pulse_single_key(
76            KeyName::L,
77            ColorPercent::new_rgb(255, 0, 0),
78            ColorPercent::new_rgb(255, 255, 0),
79            Duration::from_millis(10_000),
80            true
81        ));
82        assert!(sdk.stop_effects_on_key(KeyName::L));
83        drop(sdk);
84        std::thread::sleep(Duration::from_secs(5));
85    }
86
87    #[test]
88    fn logi_set_target_zone_sample() {
89        let _test_lock = TEST_LOCK.lock().expect("test lock poisoned");
90
91        let sdk = Sdk::new_with_name("Test").expect("failed to init LG SDK");
92        std::thread::sleep(Duration::from_secs(5));
93        assert!(sdk.set_target(TargetDevice::All));
94        assert!(sdk.set_lighting_for_key_with_name(KeyName::L, ColorPercent::new_rgb(0, 255, 255)));
95        assert!(sdk.set_lighting_for_key_with_name(KeyName::O, ColorPercent::new_rgb(0, 255, 255)));
96        assert!(sdk.set_lighting_for_key_with_name(KeyName::G, ColorPercent::new_rgb(0, 255, 255)));
97        assert!(sdk.set_lighting_for_key_with_name(KeyName::I, ColorPercent::new_rgb(0, 255, 255)));
98
99        assert!(sdk.set_lighting_for_target_zone(
100            DeviceType::Mouse,
101            1,
102            ColorPercent::new_rgb(255, 0, 0)
103        ));
104
105        assert!(sdk.set_lighting_for_target_zone(
106            DeviceType::Keyboard,
107            1,
108            ColorPercent::new_rgb(255, 0, 0)
109        ));
110        assert!(sdk.set_lighting_for_target_zone(
111            DeviceType::Keyboard,
112            2,
113            ColorPercent::new_rgb(255, 255, 0)
114        ));
115        assert!(sdk.set_lighting_for_target_zone(
116            DeviceType::Keyboard,
117            3,
118            ColorPercent::new_rgb(0, 255, 0)
119        ));
120        assert!(sdk.set_lighting_for_target_zone(
121            DeviceType::Keyboard,
122            4,
123            ColorPercent::new_rgb(0, 255, 255)
124        ));
125        assert!(sdk.set_lighting_for_target_zone(
126            DeviceType::Keyboard,
127            5,
128            ColorPercent::new_rgb(0, 0, 255)
129        ));
130
131        assert!(sdk.set_lighting_for_target_zone(
132            DeviceType::Headset,
133            0,
134            ColorPercent::new_rgb(255, 255, 255)
135        ));
136        assert!(sdk.set_lighting_for_target_zone(
137            DeviceType::Headset,
138            1,
139            ColorPercent::new_rgb(255, 0, 255)
140        ));
141        drop(sdk);
142        std::thread::sleep(Duration::from_secs(5));
143    }
144}