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
84
85
86
87
88
89
pub mod threads;
pub mod util;
pub mod core;
pub mod font;
pub mod modules;
pub mod versions;
pub use streamdeck;
pub use hidapi;
pub use palette;
pub use image;
use std::sync::Arc;
use hidapi::HidApi;
use streamdeck::pids;
use streamdeck::StreamDeck;
use crate::core::{KeyHandler, SDCore};
use crate::modules::ModuleManager;
pub fn connect_any(module_manager: Arc<ModuleManager>, hid: &HidApi, pool_rate: u32) -> Result<(Arc<SDCore>, KeyHandler), Error> {
Ok(SDCore::new(module_manager, attempt_connection_to_any(hid)?, pool_rate))
}
fn attempt_connection_to_any(hid: &HidApi) -> Result<StreamDeck, Error> {
let mut decks = find_decks(hid);
if let Some((vid, pid, serial)) = decks.pop() {
match StreamDeck::connect_with_hid(&hid, vid, pid, serial) {
Ok(streamdeck) => Ok(streamdeck),
Err(err) => Err(Error::StreamDeckError(err))
}
} else {
Err(Error::DeviceNotFound)
}
}
pub fn connect(module_manager: Arc<ModuleManager>, hid: &HidApi, vid: u16, pid: u16, serial: &str, pool_rate: u32) -> Result<(Arc<SDCore>, KeyHandler), Error> {
Ok(SDCore::new(module_manager, attempt_connection(hid, vid, pid, serial)?, pool_rate))
}
fn attempt_connection(hid: &HidApi, vid: u16, pid: u16, serial: &str) -> Result<StreamDeck, Error> {
match StreamDeck::connect_with_hid(&hid, vid, pid, Some(serial.to_string())) {
Ok(streamdeck) => Ok(streamdeck),
Err(err) => Err(Error::StreamDeckError(err))
}
}
pub fn find_decks(hid: &HidApi) -> Vec<(u16, u16, Option<String>)> {
let devices = hid
.device_list()
.filter(|item| check_if_streamdeck(item.product_id()));
devices.map(
|d| (
d.vendor_id(),
d.product_id(),
d.serial_number().map(|f| f.to_string())
))
.collect()
}
pub fn check_if_streamdeck(product_id: u16) -> bool {
match product_id {
pids::MINI | pids::ORIGINAL | pids::ORIGINAL_V2 | pids::XL => true,
_ => false,
}
}
#[derive(Debug)]
pub enum Error {
DeviceNotFound,
StreamDeckError(streamdeck::Error)
}