-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
lib.rs
72 lines (60 loc) · 1.98 KB
/
lib.rs
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
#![allow(clippy::cast_possible_truncation)]
#![allow(clippy::cast_precision_loss)]
use std::{
collections::HashMap,
net::UdpSocket,
time::{Duration, SystemTime},
};
use anyhow::Result;
use derive_config::DeriveTomlConfig;
use rosc::{OscMessage, OscPacket, OscType};
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, DeriveTomlConfig, Deserialize, Serialize)]
pub struct Config {
pub mode: bool,
pub polling: u64,
pub smooth: bool,
}
impl Default for Config {
fn default() -> Self {
Self {
mode: false,
polling: 1000,
smooth: false,
}
}
}
#[no_mangle]
#[allow(clippy::needless_pass_by_value)]
#[tokio::main(flavor = "current_thread")]
async extern "Rust" fn load(socket: UdpSocket) -> Result<()> {
let config = Config::load()?;
loop {
let duration = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH)?;
let seconds = duration.as_secs();
let mut hours = (seconds / 3600) as f64;
let mut minutes = ((seconds % 3600) / 60) as f64;
let mut seconds = (seconds % 60) as f64;
if config.smooth {
let millis = f64::from(duration.subsec_millis());
seconds += millis / 1000.0;
minutes += seconds / 60.0;
hours += minutes / 60.0;
}
let mode = if config.mode { 24.0 } else { 12.0 };
let parameters = HashMap::from([
("Hours", hours % mode / mode),
("Minutes", minutes / 60.0),
("Seconds", seconds / 60.0),
]);
for (parameter, arg) in parameters {
let packet = OscPacket::Message(OscMessage {
addr: "/avatar/parameters/VRCOSC/Clock/".to_owned() + parameter,
args: vec![OscType::Float(arg as f32)],
});
let msg_buf = rosc::encoder::encode(&packet)?;
socket.send(&msg_buf)?;
}
std::thread::sleep(Duration::from_millis(config.polling));
}
}