Skip to content

Commit

Permalink
Add option to save mounting location on server.
Browse files Browse the repository at this point in the history
This will save sensor id's in the config file, and
send that to SlimeVR Server. Needs version v0.6.1 of
server or newser.
  • Loading branch information
carl-anders committed Mar 4, 2023
1 parent 2fbc69d commit abaf38c
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 37 deletions.
37 changes: 18 additions & 19 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 17 additions & 15 deletions src/joycon/communication.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ impl Display for DeviceStatus {
struct Device {
imu: Imu,
design: JoyconDesign,
id: u8,
send_id: u8,
battery: Battery,
status: DeviceStatus,
imu_times: Vec<Instant>,
Expand All @@ -67,7 +67,7 @@ impl Device {
pub fn handshake(&self, socket: &UdpSocket, address: &SocketAddr) {
let sensor_info = PacketType::SensorInfo {
packet_id: 0,
sensor_id: self.id,
sensor_id: self.send_id,
sensor_status: 1,
sensor_type: 0,
};
Expand Down Expand Up @@ -99,13 +99,6 @@ pub enum ChannelInfo {
Reset,
Disconnected,
}
/*
fn serial_number_to_mac(serial: &str) -> [u8; 6] {
let mut hasher = Md5::new();
hasher.update(serial);
hasher.finalize()[0..6].try_into().unwrap()
}
*/

#[derive(Debug, Copy, Clone)]
struct Xyz {
Expand Down Expand Up @@ -156,6 +149,7 @@ pub struct Communication {

devices: HashMap<String, Device>,

use_keep_ids: bool,
socket: UdpSocket,
address: SocketAddr,
connected: ServerStatus,
Expand All @@ -177,6 +171,7 @@ impl Communication {
let socket = UdpSocket::bind(&addrs[..]).unwrap();
socket.set_nonblocking(true).ok();
let address = { settings.load().get_socket_address() };
let use_keep_ids = { settings.load().keep_ids };

server_tx.send(ServerStatus::Disconnected).ok();

Expand All @@ -186,6 +181,7 @@ impl Communication {
server_tx,
settings,
devices: HashMap::new(),
use_keep_ids,
socket,
address,
connected: ServerStatus::Disconnected,
Expand Down Expand Up @@ -232,15 +228,21 @@ impl Communication {
device.imu_times = vec![];
return;
}
let id = self.devices.len() as _;

let send_id = if self.use_keep_ids {
self.settings.joycon_keep_id(sn.clone())
} else {
self.devices.len() as _
};
let device = Device {
design,
imu: Imu::new(),
id,
design,
send_id,
battery: Battery::Full,
status: DeviceStatus::NoIMU,
imu_times: vec![],
};

device.handshake(&self.socket, &self.address);
self.devices.insert(sn, device);
}
Expand All @@ -262,7 +264,7 @@ impl Communication {

let rotation_packet = PacketType::RotationData {
packet_id: 0,
sensor_id: device.id,
sensor_id: device.send_id,
data_type: 1,
quat: (*rotated_quat).into(),
calibration_info: 0,
Expand All @@ -275,7 +277,7 @@ impl Communication {
let acceleration_packet = PacketType::Acceleration {
packet_id: 0,
vector: (acc.x as f32, acc.y as f32, acc.z as f32),
sensor_id: Some(device.id),
sensor_id: Some(device.send_id),
};
self.socket
.send_to(&acceleration_packet.to_bytes().unwrap(), self.address)
Expand Down Expand Up @@ -339,7 +341,7 @@ impl Communication {
{
self.last_handshake = Instant::now();
self.send_handshake();
for device in self.devices.values().sorted_by_key(|d| d.id) {
for device in self.devices.values().sorted_by_key(|d| d.send_id) {
device.handshake(&self.socket, &self.address);
}
}
Expand Down
9 changes: 9 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ enum Message {
JoyconRotate(String, bool),
JoyconScale(String, f64),
SettingsResetToggled(bool),
SettingsIdsToggled(bool),
}

#[derive(Default)]
Expand Down Expand Up @@ -170,6 +171,9 @@ impl Application for MainState {
Message::SettingsResetToggled(new) => {
self.settings.change(|ws| ws.send_reset = new);
}
Message::SettingsIdsToggled(new) => {
self.settings.change(|ws| ws.keep_ids = new);
}
}
Command::none()
}
Expand Down Expand Up @@ -238,6 +242,11 @@ impl MainState {
self.settings.load().send_reset,
Message::SettingsResetToggled,
))
.push(checkbox(
"Save mounting location on server. Requires SlimeVR Server v0.6.1 or newer. (Restart wrangler after changing this)",
self.settings.load().keep_ids,
Message::SettingsIdsToggled,
))
}
}

Expand Down
43 changes: 40 additions & 3 deletions src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,25 @@ use serde::{Deserialize, Serialize};
fn file_name() -> Option<PathBuf> {
ProjectDirs::from("", "", "SlimeVR Wrangler").map(|pd| pd.config_dir().join("config.json"))
}
#[derive(Serialize, Deserialize, Clone)]
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct Joycon {
#[serde(default)]
pub rotation: i32,
#[serde(default = "return_f64_one")]
pub gyro_scale_factor: f64,
#[serde(default)]
pub keep_id: u8,
}
fn return_f64_one() -> f64 {
1.0
}

impl Default for Joycon {
fn default() -> Self {
Joycon {
rotation: 0,
gyro_scale_factor: 1.0,
keep_id: 0,
}
}
}
Expand All @@ -34,12 +42,16 @@ pub struct WranglerSettings {
pub send_reset: bool,
#[serde(default = "return_mac")]
pub emulated_mac: [u8; 6],
#[serde(default = "return_false")]
pub keep_ids: bool,
}

fn return_true() -> bool {
true
}

fn return_false() -> bool {
false
}
fn return_mac() -> [u8; 6] {
let mut r = rand::thread_rng();
[0x00, 0x0F, r.gen(), r.gen(), r.gen(), r.gen()]
Expand All @@ -55,7 +67,7 @@ impl WranglerSettings {
}
File::create(file)
.ok()
.and_then(|file| serde_json::to_writer(file, self).ok());
.and_then(|file| serde_json::to_writer_pretty(file, self).ok());
}
pub fn load_and_save() -> Self {
let settings = file_name()
Expand All @@ -66,6 +78,7 @@ impl WranglerSettings {
joycon: HashMap::new(),
send_reset: true,
emulated_mac: return_mac(),
keep_ids: false,
});
settings.save();
settings
Expand All @@ -86,6 +99,15 @@ impl WranglerSettings {
.get(serial_number)
.map_or(1.0, |j| j.gyro_scale_factor)
}
fn joycon_keep_id_set_new(&mut self, serial_number: String) {
let max = self.joycon.values().map(|j| j.keep_id).max();
let entry = self.joycon.entry(serial_number).or_default();
entry.keep_id = max.unwrap_or_default().saturating_add(1);
if entry.keep_id == u8::MAX {
println!("\x1b[0;31m[ERROR]\x1b[0m TOO MANY JOYCONS SAVED! THIS WILL BREAK THINGS!");
println!(" YOU NEED TO DISABLE THE \"Save mounting location on server\" SETTING!!!");
}
}
pub fn get_socket_address(&self) -> SocketAddr {
self.address
.parse::<SocketAddr>()
Expand Down Expand Up @@ -115,4 +137,19 @@ impl Handler {
current.save();
self.arc.store(Arc::new(current));
}
pub fn joycon_keep_id(&self, serial_number: String) -> u8 {
let keep_id = self
.load()
.joycon
.get(&serial_number)
.map_or(0, |j| j.keep_id);
if keep_id != 0 {
return keep_id;
}
self.change(|ws| ws.joycon_keep_id_set_new(serial_number.clone()));
self.load()
.joycon
.get(&serial_number)
.map_or(0, |j| j.keep_id)
}
}

0 comments on commit abaf38c

Please sign in to comment.