1
Fork 0

Compare commits

..

No commits in common. "17f484acd56ef06899ffb3a774f6ae37c1baba53" and "9e43e6a943d0855b792d9d4f8ad89c7340febfb4" have entirely different histories.

14 changed files with 312 additions and 592 deletions

117
auto/Cargo.lock generated
View file

@ -38,7 +38,7 @@ dependencies = [
"accesskit_consumer",
"atspi-common",
"serde",
"thiserror 1.0.69",
"thiserror",
"zvariant",
]
@ -49,7 +49,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f47983a1084940ba9a39c077a8c63e55c619388be5476ac04c804cfbd1e63459"
dependencies = [
"accesskit",
"hashbrown 0.15.2",
"hashbrown",
"immutable-chunkmap",
]
@ -61,7 +61,7 @@ checksum = "7329821f3bd1101e03a7d2e03bd339e3ac0dc64c70b4c9f9ae1949e3ba8dece1"
dependencies = [
"accesskit",
"accesskit_consumer",
"hashbrown 0.15.2",
"hashbrown",
"objc2",
"objc2-app-kit",
"objc2-foundation",
@ -93,7 +93,7 @@ checksum = "24fcd5d23d70670992b823e735e859374d694a3d12bfd8dd32bd3bd8bedb5d81"
dependencies = [
"accesskit",
"accesskit_consumer",
"hashbrown 0.15.2",
"hashbrown",
"paste",
"static_assertions",
"windows 0.58.0",
@ -198,7 +198,7 @@ dependencies = [
"ndk-context",
"ndk-sys 0.6.0+11769913",
"num_enum",
"thiserror 1.0.69",
"thiserror",
]
[[package]]
@ -747,7 +747,7 @@ dependencies = [
"polling",
"rustix 0.38.44",
"slab",
"thiserror 1.0.69",
"thiserror",
]
[[package]]
@ -1088,21 +1088,6 @@ version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "96a6ac251f4a2aca6b3f91340350eab87ae57c3f127ffeb585e92bd336717991"
[[package]]
name = "dashmap"
version = "6.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5041cc499144891f3790297212f32a74fb938e5136a14943f338ef9e0ae276cf"
dependencies = [
"cfg-if",
"crossbeam-utils",
"hashbrown 0.14.5",
"lock_api",
"once_cell",
"parking_lot_core",
"serde",
]
[[package]]
name = "dasp_sample"
version = "0.11.0"
@ -1119,27 +1104,6 @@ dependencies = [
"crypto-common",
]
[[package]]
name = "directories"
version = "6.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "16f5094c54661b38d03bd7e50df373292118db60b585c08a411c6d840017fe7d"
dependencies = [
"dirs-sys",
]
[[package]]
name = "dirs-sys"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e01a3366d27ee9890022452ee61b2b63a67e6f13f58900b651ff5665f0bb1fab"
dependencies = [
"libc",
"option-ext",
"redox_users",
"windows-sys 0.59.0",
]
[[package]]
name = "dispatch"
version = "0.2.0"
@ -1276,7 +1240,7 @@ dependencies = [
"epaint",
"log",
"profiling",
"thiserror 1.0.69",
"thiserror",
"type-map",
"web-time",
"wgpu",
@ -1837,7 +1801,7 @@ checksum = "dcf29e94d6d243368b7a56caa16bc213e4f9f8ed38c4d9557069527b5d5281ca"
dependencies = [
"bitflags 2.9.0",
"gpu-descriptor-types",
"hashbrown 0.15.2",
"hashbrown",
]
[[package]]
@ -1868,12 +1832,6 @@ dependencies = [
"byteorder",
]
[[package]]
name = "hashbrown"
version = "0.14.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1"
[[package]]
name = "hashbrown"
version = "0.15.2"
@ -2165,7 +2123,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8c9c992b02b5b4c94ea26e32fe5bccb7aa7d9f390ab5c1221ff895bc7ea8b652"
dependencies = [
"equivalent",
"hashbrown 0.15.2",
"hashbrown",
]
[[package]]
@ -2177,8 +2135,6 @@ dependencies = [
"chrono",
"common",
"cpal",
"dashmap",
"directories",
"eframe",
"egui-toast",
"egui_extras",
@ -2234,7 +2190,7 @@ dependencies = [
"combine",
"jni-sys",
"log",
"thiserror 1.0.69",
"thiserror",
"walkdir",
"windows-sys 0.45.0",
]
@ -2512,7 +2468,7 @@ dependencies = [
"rustc-hash",
"spirv",
"termcolor",
"thiserror 1.0.69",
"thiserror",
"unicode-xid",
]
@ -2542,7 +2498,7 @@ dependencies = [
"log",
"ndk-sys 0.5.0+25.2.9519653",
"num_enum",
"thiserror 1.0.69",
"thiserror",
]
[[package]]
@ -2557,7 +2513,7 @@ dependencies = [
"ndk-sys 0.6.0+11769913",
"num_enum",
"raw-window-handle",
"thiserror 1.0.69",
"thiserror",
]
[[package]]
@ -2956,12 +2912,6 @@ version = "1.20.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "945462a4b81e43c4e3ba96bd7b49d834c6f61198356aa858733bc4acf3cbe62e"
[[package]]
name = "option-ext"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d"
[[package]]
name = "orbclient"
version = "0.3.48"
@ -3288,7 +3238,7 @@ dependencies = [
"rand_chacha",
"simd_helpers",
"system-deps",
"thiserror 1.0.69",
"thiserror",
"v_frame",
"wasm-bindgen",
]
@ -3352,17 +3302,6 @@ dependencies = [
"bitflags 2.9.0",
]
[[package]]
name = "redox_users"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dd6f9d3d47bdd2ad6945c5015a226ec6155d0bcdfd8f7cd29f86b71f8de99d2b"
dependencies = [
"getrandom 0.2.15",
"libredox",
"thiserror 2.0.12",
]
[[package]]
name = "regex"
version = "1.11.1"
@ -3667,7 +3606,7 @@ dependencies = [
"log",
"memmap2",
"rustix 0.38.44",
"thiserror 1.0.69",
"thiserror",
"wayland-backend",
"wayland-client",
"wayland-csd-frame",
@ -3864,16 +3803,7 @@ version = "1.0.69"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b6aaf5339b578ea85b50e080feb250a3e8ae8cfcdff9a461c9ec2904bc923f52"
dependencies = [
"thiserror-impl 1.0.69",
]
[[package]]
name = "thiserror"
version = "2.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "567b8a2dae586314f7be2a752ec7474332959c6460e02bde30d702a66d488708"
dependencies = [
"thiserror-impl 2.0.12",
"thiserror-impl",
]
[[package]]
@ -3887,17 +3817,6 @@ dependencies = [
"syn 2.0.99",
]
[[package]]
name = "thiserror-impl"
version = "2.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f7cf42b4507d8ea322120659672cf1b9dbb93f8f2d4ecfd6e51350ff5b17a1d"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.99",
]
[[package]]
name = "tiff"
version = "0.9.1"
@ -4455,7 +4374,7 @@ dependencies = [
"raw-window-handle",
"rustc-hash",
"smallvec",
"thiserror 1.0.69",
"thiserror",
"wgpu-hal",
"wgpu-types",
]
@ -4493,7 +4412,7 @@ dependencies = [
"renderdoc-sys",
"rustc-hash",
"smallvec",
"thiserror 1.0.69",
"thiserror",
"wasm-bindgen",
"web-sys",
"wgpu-types",

View file

@ -25,7 +25,7 @@ pub enum ControlPacket {
EStop,
}
#[derive(Clone, Debug, Serialize, Deserialize, Default)]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct SensorData {
/// left distance, mm
pub tof_l: Option<u16>,
@ -45,12 +45,6 @@ pub struct TelemetryPacket {
pub cam_state: CamState,
}
impl Default for TelemetryPacket {
fn default() -> Self {
Self { sensors: Default::default(), cam_state: CamState::Idle }
}
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum CamState {
Firing,

View file

@ -89,7 +89,7 @@ async fn main(spawner: Spawner) {
spawner.spawn(logger_task(driver)).unwrap();
let limit_switch = Input::new(p.PIN_16, Pull::Up);
let mut auto_status = Output::new(p.PIN_2, Level::High);
let mut auto_status = Output::new(p.PIN_2, Level::Low);
let mut d: pwm::Config = Default::default();
d.divider = 40.into();

117
interface/Cargo.lock generated
View file

@ -38,7 +38,7 @@ dependencies = [
"accesskit_consumer",
"atspi-common",
"serde",
"thiserror 1.0.69",
"thiserror",
"zvariant",
]
@ -49,7 +49,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f47983a1084940ba9a39c077a8c63e55c619388be5476ac04c804cfbd1e63459"
dependencies = [
"accesskit",
"hashbrown 0.15.2",
"hashbrown",
"immutable-chunkmap",
]
@ -61,7 +61,7 @@ checksum = "7329821f3bd1101e03a7d2e03bd339e3ac0dc64c70b4c9f9ae1949e3ba8dece1"
dependencies = [
"accesskit",
"accesskit_consumer",
"hashbrown 0.15.2",
"hashbrown",
"objc2",
"objc2-app-kit",
"objc2-foundation",
@ -93,7 +93,7 @@ checksum = "24fcd5d23d70670992b823e735e859374d694a3d12bfd8dd32bd3bd8bedb5d81"
dependencies = [
"accesskit",
"accesskit_consumer",
"hashbrown 0.15.2",
"hashbrown",
"paste",
"static_assertions",
"windows 0.58.0",
@ -198,7 +198,7 @@ dependencies = [
"ndk-context",
"ndk-sys 0.6.0+11769913",
"num_enum",
"thiserror 1.0.69",
"thiserror",
]
[[package]]
@ -739,7 +739,7 @@ dependencies = [
"polling",
"rustix",
"slab",
"thiserror 1.0.69",
"thiserror",
]
[[package]]
@ -1080,21 +1080,6 @@ version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "96a6ac251f4a2aca6b3f91340350eab87ae57c3f127ffeb585e92bd336717991"
[[package]]
name = "dashmap"
version = "6.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5041cc499144891f3790297212f32a74fb938e5136a14943f338ef9e0ae276cf"
dependencies = [
"cfg-if",
"crossbeam-utils",
"hashbrown 0.14.5",
"lock_api",
"once_cell",
"parking_lot_core",
"serde",
]
[[package]]
name = "dasp_sample"
version = "0.11.0"
@ -1111,27 +1096,6 @@ dependencies = [
"crypto-common",
]
[[package]]
name = "directories"
version = "6.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "16f5094c54661b38d03bd7e50df373292118db60b585c08a411c6d840017fe7d"
dependencies = [
"dirs-sys",
]
[[package]]
name = "dirs-sys"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e01a3366d27ee9890022452ee61b2b63a67e6f13f58900b651ff5665f0bb1fab"
dependencies = [
"libc",
"option-ext",
"redox_users",
"windows-sys 0.59.0",
]
[[package]]
name = "dispatch"
version = "0.2.0"
@ -1268,7 +1232,7 @@ dependencies = [
"epaint",
"log",
"profiling",
"thiserror 1.0.69",
"thiserror",
"type-map",
"web-time",
"wgpu",
@ -1829,7 +1793,7 @@ checksum = "dcf29e94d6d243368b7a56caa16bc213e4f9f8ed38c4d9557069527b5d5281ca"
dependencies = [
"bitflags 2.8.0",
"gpu-descriptor-types",
"hashbrown 0.15.2",
"hashbrown",
]
[[package]]
@ -1860,12 +1824,6 @@ dependencies = [
"byteorder",
]
[[package]]
name = "hashbrown"
version = "0.14.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e5274423e17b7c9fc20b6e7e208532f9b19825d82dfd615708b70edd83df41f1"
[[package]]
name = "hashbrown"
version = "0.15.2"
@ -2157,7 +2115,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "62f822373a4fe84d4bb149bf54e584a7f4abec90e072ed49cda0edea5b95471f"
dependencies = [
"equivalent",
"hashbrown 0.15.2",
"hashbrown",
]
[[package]]
@ -2169,8 +2127,6 @@ dependencies = [
"chrono",
"common",
"cpal",
"dashmap",
"directories",
"eframe",
"egui-toast",
"egui_extras",
@ -2226,7 +2182,7 @@ dependencies = [
"combine",
"jni-sys",
"log",
"thiserror 1.0.69",
"thiserror",
"walkdir",
"windows-sys 0.45.0",
]
@ -2498,7 +2454,7 @@ dependencies = [
"rustc-hash",
"spirv",
"termcolor",
"thiserror 1.0.69",
"thiserror",
"unicode-xid",
]
@ -2528,7 +2484,7 @@ dependencies = [
"log",
"ndk-sys 0.5.0+25.2.9519653",
"num_enum",
"thiserror 1.0.69",
"thiserror",
]
[[package]]
@ -2543,7 +2499,7 @@ dependencies = [
"ndk-sys 0.6.0+11769913",
"num_enum",
"raw-window-handle",
"thiserror 1.0.69",
"thiserror",
]
[[package]]
@ -2942,12 +2898,6 @@ version = "1.20.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1261fe7e33c73b354eab43b1273a57c8f967d0391e80353e51f764ac02cf6775"
[[package]]
name = "option-ext"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "04744f49eae99ab78e0d5c0b603ab218f515ea8cfe5a456d7629ad883a3b6e7d"
[[package]]
name = "orbclient"
version = "0.3.48"
@ -3274,7 +3224,7 @@ dependencies = [
"rand_chacha",
"simd_helpers",
"system-deps",
"thiserror 1.0.69",
"thiserror",
"v_frame",
"wasm-bindgen",
]
@ -3338,17 +3288,6 @@ dependencies = [
"bitflags 2.8.0",
]
[[package]]
name = "redox_users"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dd6f9d3d47bdd2ad6945c5015a226ec6155d0bcdfd8f7cd29f86b71f8de99d2b"
dependencies = [
"getrandom 0.2.15",
"libredox",
"thiserror 2.0.12",
]
[[package]]
name = "regex"
version = "1.11.1"
@ -3640,7 +3579,7 @@ dependencies = [
"log",
"memmap2",
"rustix",
"thiserror 1.0.69",
"thiserror",
"wayland-backend",
"wayland-client",
"wayland-csd-frame",
@ -3837,16 +3776,7 @@ version = "1.0.69"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b6aaf5339b578ea85b50e080feb250a3e8ae8cfcdff9a461c9ec2904bc923f52"
dependencies = [
"thiserror-impl 1.0.69",
]
[[package]]
name = "thiserror"
version = "2.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "567b8a2dae586314f7be2a752ec7474332959c6460e02bde30d702a66d488708"
dependencies = [
"thiserror-impl 2.0.12",
"thiserror-impl",
]
[[package]]
@ -3860,17 +3790,6 @@ dependencies = [
"syn 2.0.96",
]
[[package]]
name = "thiserror-impl"
version = "2.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7f7cf42b4507d8ea322120659672cf1b9dbb93f8f2d4ecfd6e51350ff5b17a1d"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.96",
]
[[package]]
name = "tiff"
version = "0.9.1"
@ -4428,7 +4347,7 @@ dependencies = [
"raw-window-handle",
"rustc-hash",
"smallvec",
"thiserror 1.0.69",
"thiserror",
"wgpu-hal",
"wgpu-types",
]
@ -4466,7 +4385,7 @@ dependencies = [
"renderdoc-sys",
"rustc-hash",
"smallvec",
"thiserror 1.0.69",
"thiserror",
"wasm-bindgen",
"web-sys",
"wgpu-types",

View file

@ -23,5 +23,3 @@ chrono = "0.4.40"
image = "0.25.5"
libloading = "0.8.6"
emath = "0.31.1"
dashmap = { version = "6.1.0", features = ["serde"] }
directories = "6.0.0"

View file

@ -1,47 +1,28 @@
use std::{future::Future, ops::{Deref, Range}, pin::Pin, sync::{atomic::{AtomicBool, Ordering}, Arc}, thread::sleep, time::Duration};
use std::{collections::VecDeque, ops::{Deref, Div, Index, Range, Sub}, sync::Arc};
use common::{CamState, ControlPacket, SensorData, TelemetryPacket};
use anyhow::{anyhow, Ok, Result};
use anyhow::{Context, Ok, Result};
use eframe::Storage;
use libloading::{Library, Symbol};
use tokio::{sync::watch, task::JoinHandle};
use tokio::sync::{self, broadcast, mpsc, watch};
use crate::storage::StorageManager;
pub struct AutoConfig {
turn_gain: f32,
auto_fire_distance: f32,
}
#[derive(Clone)]
pub struct AutoInterface {
command_sender: watch::Sender<ControlPacket>,
command_sender: Option<mpsc::Sender<ControlPacket>>,
data_receiver: watch::Receiver<TelemetryPacket>,
config: StorageManager,
enabled: Arc<AtomicBool>,
enable_permission: Arc<AtomicBool>,
config: &'static dyn Storage,
enabled: bool,
}
impl AutoInterface {
/// new
///
/// returns command channel from auto, enable status
pub fn new(data: watch::Receiver<TelemetryPacket>, config: StorageManager, enable_permission: Arc<AtomicBool>) -> (Self, watch::Receiver<ControlPacket>, Arc<AtomicBool>) {
let (commands, receiver) = watch::channel(ControlPacket::Fire);
let enabled = Arc::new(AtomicBool::new(false));
let interface = Self {
command_sender: commands,
data_receiver: data,
config,
enabled: enabled.clone(),
enable_permission,
};
(interface, receiver, enabled)
}
pub fn subscribe(&self) -> watch::Receiver<ControlPacket> {
self.command_sender.subscribe()
}
/// change active command, fails if not in control
pub fn run_command(&self, command: ControlPacket) -> Result<()> {
self.command_sender.send(command)?;
pub async fn run_command(&self, command: ControlPacket) -> Result<()> {
self.command_sender.as_ref().context("no sender")?.send(command).await?;
Ok(())
}
pub fn sensor_data(&mut self) -> SensorData {
@ -53,33 +34,22 @@ impl AutoInterface {
pub async fn sensor_update(&mut self) -> SensorData {
self.data_receiver.changed().await.unwrap();
self.data_receiver.borrow().sensors.clone()
}
pub fn sensor_update_blocking(&mut self) -> SensorData {
loop {
if self.data_receiver.has_changed().unwrap() {
break;
}
sleep(Duration::from_millis(10)); // TODO: exponential or condvars
}
self.data_receiver.borrow_and_update().sensors.clone()
}
/// disable auto
pub fn disable(&mut self) {
self.enabled.store(false, Ordering::Release);
}
pub fn disable(&self) {unimplemented!()}
/// request auto enable, fails if the driver does not grant it
pub fn enable(&mut self) -> Result<()> {
if self.enable_permission.load(Ordering::Acquire) {
self.enabled.store(true, Ordering::Release);
Ok(())
} else {
Err(anyhow!("no enable"))?
}
self.enabled = self.command_sender.is_some();
Ok(())
}
pub fn enabled(&self) -> bool { self.enabled.load(Ordering::Acquire) }
pub fn enabled(&self) -> bool { self.enabled}
pub fn conf(&self, key: &'static Configurable) -> f32 {
self.config.load(key)
self.config.get_string(&key.name)
.map(|s| s.parse().ok())
.flatten().unwrap_or(key.default)
}
fn send_message(&self, message: String) {unimplemented!()}
}
pub struct Configurable {
@ -123,45 +93,13 @@ impl Default for Configurable {
}
}
pub struct Auto {
library: Arc<Library>,
// these aren't actually static, they have the same lifetime as the library xP
configs: &'static [Configurable],
name: &'static str,
}
impl Auto {
pub fn new(path: &str) -> Result<Self> {
let lib = unsafe {Library::new(path)?};
let lib = Arc::new(lib);
let configurations: Symbol<&&[Configurable]> = unsafe {lib.get(b"CONFIGS").unwrap()};
let name: Symbol<&&'static str> = unsafe {lib.get(b"NAME").unwrap()};
Ok(Self {
configs: *configurations,
name: *name,
library: lib,
})
}
/// A fake trait in the sense that these methods are exposed as symbols, not trait methods
pub trait Auto {
/// entrypoint
pub fn run(&self, interface: AutoInterface) -> Result<()> {
let func: Symbol<unsafe extern "C" fn(AutoInterface) -> ()> = unsafe {self.library.get(b"entry")?};
unsafe { func(interface.clone()) };
Ok(())
}
pub fn configs(&self) -> &'static [Configurable] {
self.configs
}
pub fn name(&self) -> &'static str {
self.name
}
async fn run(interface: &AutoInterface);
/// register
fn default_configs() -> [Configurable];
fn name() -> &'static str;
}
@ -180,3 +118,4 @@ pub fn get_confs() -> Result<()> {
Ok(())
}

View file

@ -1,146 +0,0 @@
use std::{collections::VecDeque, future::Future, ops::Sub, pin::Pin};
use common::CamState;
use crate::auto::{AutoInterface, Configurable};
#[unsafe(no_mangle)]
pub fn entry(interface: AutoInterface) -> Pin<Box<dyn Future<Output = ()> + Send>> {
Box::pin(auto(interface))
}
#[unsafe(no_mangle)]
pub static NAME: &'static str = "scanseek v1";
const AUTO_GAP: Configurable = Configurable::new("auto minimum gap").range(0. .. 300.).default(140.)
.description("distance (mm) distance measurements must instantaneously drop to indicate a detection. This should line up with the size of the smallest robot you compete against");
const AUTO_SELF_OCCLUSION: Configurable = Configurable::new("auto self occlusion").range(0. .. 200.).default(143.)
.description("distance (mm) below which measurements are considered noise in the scan phase");
const AUTO_CONVERGENCE_POINT: Configurable = Configurable::new("auto convergence").range(100. .. 300.).default(190.)
.description("distance (mm) where the tof beams intersect");
const AUTO_SEEK_RANGE: Configurable = Configurable::new("auto seek range").range(200. .. 8000.).default(500.);
#[unsafe(no_mangle)]
pub static CONFIGS: &[Configurable] = &[
AUTO_GAP,
AUTO_SELF_OCCLUSION,
AUTO_CONVERGENCE_POINT,
AUTO_SEEK_RANGE,
];
pub async fn auto(mut interface: AutoInterface) {
let mut tof_l = Stats::new();
let mut tof_r = Stats::new();
loop {
let data = interface.sensor_update().await;
println!("auto got on data");
let cam = interface.cam_state();
let CamState::Charged = cam else {
continue;
};
let Some(latest_tof_l) = data.tof_l else {
continue;
};
tof_l.update(latest_tof_l as i16);
let Some(latest_tof_r) = data.tof_r else {
continue;
};
tof_r.update(latest_tof_r as i16);
let auto_gap = interface.conf(&AUTO_GAP);
let auto_self_occlusion = interface.conf(&AUTO_SELF_OCCLUSION);
let detection = |latest: u16, delta: i16| {
delta < auto_gap as i16 && latest > auto_self_occlusion as u16
};
if detection(latest_tof_l, tof_l.delta()) || detection(latest_tof_l, tof_l.delta()) {
if let Ok(()) = interface.enable() {
println!("found, now seek");
seek(interface.clone(), &mut tof_l, &mut tof_r).await;
} else {
println!("requested enable")
}
}
}
}
async fn seek(mut interface: AutoInterface, tof_l: &mut Stats<i16>, tof_r: &mut Stats<i16>) {
let crossover = interface.conf(&AUTO_CONVERGENCE_POINT) as i16;
let range = interface.conf(&AUTO_SEEK_RANGE) as i16;
loop {
let data = interface.sensor_data();
data.tof_l.map(|d| tof_l.update(d as i16));
data.tof_r.map(|d| tof_r.update(d as i16));
let left_near = tof_l.latest() < crossover && tof_l.delta() < 70;
let right_near = tof_r.latest() < crossover && tof_r.delta() < 70;
let left_far = tof_l.latest() > crossover && tof_l.latest() < range && tof_l.delta() < 70;
let right_far = tof_r.latest() > crossover && tof_r.latest() < range && tof_r.delta() < 70;
let near = left_near || right_near;
let far = !near && (left_far || right_far);
let mut twist = 0.0;
if near {
if tof_l.max() > tof_r.max() {
twist = 1.0; // right
} else {
twist = -1.0; // left
}
} else if far {
if tof_l.max() > tof_r.max() {
twist = -1.0; // left
} else {
twist = 1.0; // right
}
}
let _ = interface.run_command(common::ControlPacket::Twist(1.0, twist));
interface.sensor_update_blocking();
}
}
struct Stats<T> {
table: VecDeque<T>
}
impl<T: Copy> Stats<T> {
pub fn new() -> Self {
Self { table: VecDeque::new() }
}
const MAX_ELEMENTS: usize = 3;
pub fn update(&mut self, elem: T) {
self.table.push_front(elem);
if self.table.len() > Self::MAX_ELEMENTS {
self.table.pop_back();
}
}
/// panics if no values have been written
pub fn latest(&self) -> T {
self.table.front().unwrap().to_owned()
}
}
impl<T: Ord + Sub<Output = T> + Copy + From<u8>> Stats<T> {
pub fn delta(&self) -> T {
*self.table.get(0).unwrap_or(&T::from(0)) - *self.table.get(1).unwrap_or(&T::from(0))
}
pub fn max(&self) -> T {
*self.table.iter().max().unwrap_or(&T::from(0))
}
pub fn min(&self) -> T {
*self.table.iter().max().unwrap_or(&T::from(0))
}
}

View file

@ -1,10 +1,12 @@
use std::{path::Path, time::{Duration, SystemTime, UNIX_EPOCH}};
use std::time::{Duration, SystemTime, UNIX_EPOCH};
use anyhow::Ok;
use common::{ControlPacket, TelemetryPacket};
use serde::{Deserialize, Serialize};
use tokio::{fs::File, io::AsyncWriteExt, sync::mpsc::Receiver, time::Instant};
use crate::storage_dir::storage;
#[derive(Serialize, Deserialize)]
pub enum CombatData {
@ -14,8 +16,8 @@ pub enum CombatData {
type CombatLogRow = (CombatData, u128);
pub async fn combat_logger(mut data: Receiver<CombatData>, path: &Path) -> anyhow::Result<()> {
let mut path = path.to_path_buf();
pub async fn combat_logger(mut data: Receiver<CombatData>) -> anyhow::Result<()> {
let mut path = storage();
let time = chrono::offset::Utc::now();
let formatted = time.to_rfc3339_opts(chrono::SecondsFormat::Secs, false);
path.push(format!("{formatted}.combatlog"));

View file

@ -1,16 +1,16 @@
use std::{cell::OnceCell, collections::HashMap, path::PathBuf, sync::{atomic::{AtomicBool, Ordering}, Arc}, time::Duration};
use std::{cell::OnceCell, collections::HashMap, path::PathBuf, sync::{atomic::Ordering, Arc}, time::Duration};
use common::{ControlPacket, TelemetryPacket};
use eframe::{egui::{self, containers, Align2, Checkbox, Context, IconData, Id, ImageSource, Label, Ui}, Storage};
use image::ImageFormat;
use tokio::{runtime::Runtime, sync::{mpsc, watch::{self, Receiver}}};
use tokio::{runtime::Runtime, sync::{mpsc, watch::Receiver}};
use egui_toast::{Toast, Toasts};
use crate::{auto::Configurable, auto_impl::CONFIGS, storage::StorageManager, POWER_THRESHOLD};
use crate::{auto::Configurable, storage_dir::storage_dir, POWER_THRESHOLD};
pub const GUI: OnceCell<Context> = OnceCell::new();
pub fn gui(data: Receiver<GUIData>, toasts: mpsc::Receiver<Toast>, executor: Runtime, autoconf: watch::Receiver<&'static [Configurable]>, storage: StorageManager, auto_allowed: Arc<AtomicBool>, auto_enabled: Arc<AtomicBool>) -> eframe::Result {
pub fn gui(data: Receiver<GUIData>, toasts: mpsc::Receiver<Toast>, executor: Runtime) -> eframe::Result {
let icon = egui::include_image!("../assets/lizard.png");
let icon = image::load_from_memory_with_format(include_bytes!("../assets/lizard.png"), ImageFormat::Png).unwrap();
@ -35,7 +35,7 @@ pub fn gui(data: Receiver<GUIData>, toasts: mpsc::Receiver<Toast>, executor: Run
// This gives us image support:
egui_extras::install_image_loaders(&cc.egui_ctx);
Ok(Box::new(GUI::with_receivers(data, toasts, executor, storage, autoconf, auto_allowed, auto_enabled)))
Ok(Box::new(GUI::with_receivers(data, toasts, executor, cc.storage.unwrap())))
}),
)
}
@ -46,13 +46,10 @@ pub struct GUIData {
pub last_command: Option<ControlPacket>,
}
#[cfg(target_os = "linux")]
pub const DEFAULT_VOLUME_THRESHOLD: f32 = 5.0;
#[cfg(target_os = "windows")]
pub const DEFAULT_VOLUME_THRESHOLD: f32 = 1.0;
const VOLUME_THRESHOLD: Configurable = Configurable::new("Volume").range(0.0..10.).default(DEFAULT_VOLUME_THRESHOLD)
.description("higher accepts less noise (better)");
const DEFAULT_TURN_GAIN: f32 = 0.3;
const DEFAULT_FIRE_DISTANCE: f32 = 55.0;
@ -60,45 +57,72 @@ struct GUI {
data: Receiver<GUIData>,
toasts: mpsc::Receiver<Toast>,
executor: Option<Runtime>,
volume_threshold: f32,
auto_turn_gain: f32,
/// mm
auto_fire_distance: f32,
selected_auto: usize,
autoconf: watch::Receiver<&'static [Configurable]>,
storage: StorageManager,
auto_allowed: Arc<AtomicBool>,
auto_enabled: Arc<AtomicBool>,
}
impl GUI {
fn with_receivers(data: Receiver<GUIData>, toasts: mpsc::Receiver<Toast>, executor: Runtime, storage: StorageManager, autoconf: watch::Receiver<&'static [Configurable]>, auto_allowed: Arc<AtomicBool>, auto_enabled: Arc<AtomicBool>) -> Self {
fn with_receivers(data: Receiver<GUIData>, toasts: mpsc::Receiver<Toast>, executor: Runtime, storage: &dyn Storage) -> Self {
let volume_threshold: f32 = storage.get_string("volume_threshold").map(|s| s.parse().ok()).flatten().unwrap_or(DEFAULT_VOLUME_THRESHOLD);
let auto_turn_gain: f32 = storage.get_string("auto_turn_gain").map(|s| s.parse().ok()).flatten().unwrap_or(DEFAULT_TURN_GAIN);
let auto_fire_distance: f32 = storage.get_string("auto_fire_distance").map(|s| s.parse().ok()).flatten().unwrap_or(DEFAULT_FIRE_DISTANCE);
let selected_auto: usize = storage.get_string("selected_auto").map(|s| s.parse().ok()).flatten().unwrap_or(0);
Self {
data,
toasts,
executor: Some(executor),
selected_auto: 0,
autoconf,
storage,
auto_allowed,
auto_enabled,
volume_threshold,
auto_turn_gain,
auto_fire_distance,
selected_auto,
}
}
}
// dupe from auto crate for testing
const AUTO_GAP: Configurable = Configurable::new("auto minimum gap").range(0. .. 300.).default(140.)
.description("distance (mm) distance measurements must instantaneously drop to indicate a detection. This should line up with the size of the smallest robot you compete against");
const AUTO_SELF_OCCLUSION: Configurable = Configurable::new("auto self occlusion").range(0. .. 200.).default(143.)
.description("distance (mm) below which measurements are considered noise in the scan phase");
pub static CONFIGS: &[Configurable] = &[
AUTO_GAP,
AUTO_SELF_OCCLUSION,
];
impl eframe::App for GUI {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
let _ = GUI.set(ctx.clone());
egui::SidePanel::right(Id::new("config")).resizable(false).show(ctx, |ui| {
ui.heading("configuration");
configurator(ui, &VOLUME_THRESHOLD, &self.storage);
ui.add(egui::Slider::new(&mut self.volume_threshold, 0.0..=10.0).text("volume threshold"));
ui.label("higher accepts less noise (better)");
ui.add(egui::Slider::new(&mut self.auto_turn_gain, 0.0..=1.0).text("auto turn kP"));
ui.add(egui::Slider::new(&mut self.auto_fire_distance, 30.0..=100.0).text("auto fire distance (mm)"));
ui.horizontal(|ui| {
egui::ComboBox::new("auto selector", "select auto")
.show_index(ui, &mut self.selected_auto, 3, |n| ["a","b","c"][n]);
ui.button("refresh").clicked();
});
POWER_THRESHOLD.store(self.storage.load(&VOLUME_THRESHOLD), Ordering::Relaxed);
let storage = _frame.storage_mut().unwrap();
storage.set_string("volume_threshold", format!("{}",self.volume_threshold));
storage.set_string("auto_turn_gain", format!("{}",self.auto_turn_gain));
storage.set_string("auto_fire_distance", format!("{}",self.auto_fire_distance));
storage.set_string("selected_auto", format!("{}",self.selected_auto));
POWER_THRESHOLD.store(self.volume_threshold, Ordering::Relaxed);
for conf in CONFIGS {
configurator(ui, conf, &self.storage);
configurator(ui, conf, storage);
}
});
@ -108,9 +132,6 @@ impl eframe::App for GUI {
if let Some(ref command) = self.data.borrow().last_command {
ui.label(format!("sending {command:?}"));
}
ui.label(format!("auto authorized: {}", if self.auto_allowed.load(Ordering::Acquire) {""} else {""}));
ui.label(format!("auto running: {}", if self.auto_enabled.load(Ordering::Acquire) {"✅ zoom vroom"} else {""}));
if let Some(ref telem) = self.data.borrow().telemetry {
ui.label(format!("Left tof: {}", if let Some(tof) = telem.sensors.tof_l {format!("{tof}mm")} else {"".into()}));
ui.label(format!("Right tof: {}", if let Some(tof) = telem.sensors.tof_r {format!("{tof}mm")} else {"".into()}));
@ -138,8 +159,17 @@ impl eframe::App for GUI {
}
}
fn configurator(ui: &mut Ui, config: &'static Configurable, map: &StorageManager) {
ui.add(egui::Slider::from_get_set(config.min as f64 ..= config.max as f64, map.get_set(config)).text(config.name));
fn configurator(ui: &mut Ui, config: &Configurable, map: &mut dyn Storage) {
ui.add(egui::Slider::from_get_set(config.min as f64 ..= config.max as f64, |value| {
match value {
Some(value) => {
map.set_string(&config.name,format!("{value}"));
value
},
None => map.get_string(&config.name).map(|s| s.parse().ok()).flatten().unwrap_or(config.default as f64),
}
}).text(config.name));
if let Some(description) = config.description {
ui.label(description);
}

View file

@ -4,10 +4,9 @@ use atomic_float::AtomicF32;
use gui::DEFAULT_VOLUME_THRESHOLD;
pub mod gui;
pub mod storage_dir;
pub mod combatlog;
pub mod auto;
pub mod auto_impl;
pub mod storage;
pub const POWER_THRESHOLD: AtomicF32 = AtomicF32::new(DEFAULT_VOLUME_THRESHOLD);

View file

@ -1,9 +1,8 @@
use std::{fmt::format, ops::ControlFlow, path::Path, result, sync::{atomic::{AtomicBool, Ordering}, Arc}, thread::{self, sleep}, time::Duration};
use std::{fmt::format, ops::ControlFlow, result, sync::{atomic::Ordering, Arc}, thread::{self, sleep}, time::Duration};
use anyhow::{Context, Ok, Result};
use atomic_float::AtomicF32;
use directories::ProjectDirs;
use interface::{auto::{get_confs, Auto, AutoInterface}, auto_impl, combatlog::{combat_logger, CombatData}, gui::CONFIGS, storage::StorageManager, POWER_THRESHOLD};
use interface::{auto::get_confs, combatlog::{combat_logger, CombatData}, POWER_THRESHOLD};
use common::{ControlPacket, TelemetryPacket};
use cpal::traits::{DeviceTrait, HostTrait, StreamTrait};
use egui_toast::{Toast, ToastKind};
@ -14,22 +13,8 @@ use tokio::{io::{AsyncReadExt, AsyncWriteExt, BufWriter, WriteHalf}, net::{tcp::
fn main() -> Result<()> {
let directories = ProjectDirs::from("dev", "ank", "Cruise Control").context("no home")?;
let auto = Auto::new("../auto/target/release/libauto.so")?;
let storage = StorageManager::new(directories.config_dir().to_path_buf().join(Path::new("conf.dat")))?;
let auto_allowed = Arc::new(AtomicBool::new(false));
let (auto_telem_sender, auto_telem) = watch::channel(TelemetryPacket::default());
let (interface, auto_command, auto_enabled) = AutoInterface::new(auto_telem.clone(), storage.clone(), auto_allowed.clone());
println!("name: {}", auto.name());
for config in auto.configs().iter() {
println!("co {}", config.name);
println!("co {:?}", config.description);
}
get_confs().unwrap();
panic!("got too far");
let (logging_sender, combatlog) = mpsc::channel(64);
@ -99,20 +84,17 @@ fn main() -> Result<()> {
let spawner = executor.handle().clone();
let auto_allowed_ = auto_allowed.clone();
let interface_ = interface.clone();
thread::spawn(move || {
spawner.block_on(async {
let log_toasts = toast_sender.clone();
tokio::spawn(async move {
if let Err(e) = combat_logger(combatlog, directories.data_dir()).await {
if let Err(e) = combat_logger(combatlog).await {
let _ = log_toasts.clone().send(Toast::new().text(format!("logger crashed: {e:?}")).kind(ToastKind::Error)).await;
}
});
loop {
if let Err(e) = connect(toast_sender.clone(), notes.resubscribe(), data_sender.clone(), logging_sender.clone(), auto_telem_sender.clone(), auto_allowed_.clone(), interface_.clone()).await {
if let Err(e) = connect(toast_sender.clone(), notes.resubscribe(), data_sender.clone(), logging_sender.clone()).await {
if let Err(_) = toast_sender.send(Toast::new().text(format!("{e:?}")).kind(ToastKind::Error)).await {
break;
};
@ -122,30 +104,18 @@ fn main() -> Result<()> {
});
let interface_ = interface.clone();
let spawner = executor.handle().clone();
thread::spawn(move || {
spawner.block_on(async {
let _stoppie = auto_impl::auto(interface_).await;
println!("auto ended");
});
});
println!("launching gui");
let (_conf_sender, conf) = watch::channel(CONFIGS);
gui(gui_data, toasts, executor, conf, storage.clone(), auto_allowed, auto_enabled).unwrap();
gui(gui_data, toasts, executor).unwrap();
drop(stream);
storage.save();
Ok(())
}
/// frequency, volume
type Detection = (Option<f32>, f32);
async fn connect(toast_sender: mpsc::Sender<Toast>, notes: broadcast::Receiver<Detection>, data_sender: watch::Sender<GUIData>, logging_sender: mpsc::Sender<CombatData>, auto_sender: watch::Sender<TelemetryPacket>, auto_allowed: Arc<AtomicBool>, interface: AutoInterface) -> Result<()>{
async fn connect(toast_sender: mpsc::Sender<Toast>, notes: broadcast::Receiver<Detection>, data_sender: watch::Sender<GUIData>, logging_sender: mpsc::Sender<CombatData>) -> Result<()>{
toast_sender.send(Toast::new().text("connecting to bot").kind(ToastKind::Info)).await?;
let cruisecontrol = TcpStream::connect("192.168.1.2:1234").await?;
println!("connected");
@ -153,13 +123,13 @@ async fn connect(toast_sender: mpsc::Sender<Toast>, notes: broadcast::Receiver<D
cruisecontrol.set_nodelay(true)?;
let (telem, control) = cruisecontrol.into_split();
tokio::spawn(telemetry_handler(telem, data_sender.clone(), logging_sender.clone(), auto_sender.clone()));
tokio::spawn(telemetry_handler(telem, data_sender.clone(), logging_sender.clone()));
controller(notes, control, data_sender.clone(), logging_sender.clone(), auto_allowed, interface).await?;
controller(notes, control, data_sender.clone(), logging_sender.clone()).await?;
Ok(())
}
async fn telemetry_handler(mut telem: OwnedReadHalf, gui: watch::Sender<GUIData>, logging_sender: mpsc::Sender<CombatData>, auto_sender: watch::Sender<TelemetryPacket>) -> Result<()> {
async fn telemetry_handler(mut telem: OwnedReadHalf, gui: watch::Sender<GUIData>, logging_sender: mpsc::Sender<CombatData>) -> Result<()> {
let mut buf = vec![0; 2048];
loop {
let len = telem.read_u32().await.context("bad length")? as usize;
@ -169,8 +139,7 @@ async fn telemetry_handler(mut telem: OwnedReadHalf, gui: watch::Sender<GUIData>
//println!("telem: {telem:?}");
auto_sender.send(telem.clone())?;
let _ = logging_sender.send(CombatData::Telemetry(telem.clone())).await;
logging_sender.send(CombatData::Telemetry(telem.clone())).await?;
gui.send_modify(|gui| {
gui.telemetry = Some(telem);
@ -179,7 +148,7 @@ async fn telemetry_handler(mut telem: OwnedReadHalf, gui: watch::Sender<GUIData>
}
}
async fn controller(mut notes: broadcast::Receiver<Detection>, controller: OwnedWriteHalf, gui: watch::Sender<GUIData>, logging_sender: mpsc::Sender<CombatData>, enable: Arc<AtomicBool>, interface: AutoInterface) -> Result<()> {
async fn controller(mut notes: broadcast::Receiver<Detection>, controller: OwnedWriteHalf, gui: watch::Sender<GUIData>, logging_sender: mpsc::Sender<CombatData>) -> Result<()> {
let mut controller = BufWriter::new(controller);
//send_packet(&mut controller, ControlPacket::Arm(true)).await?;
//println!("armed flipper");
@ -189,8 +158,6 @@ async fn controller(mut notes: broadcast::Receiver<Detection>, controller: Owned
//auto = Some(spawn(seek))
let mut auto_data = interface.subscribe();
loop {
let mut control = ControlPacket::Stop;
@ -203,18 +170,14 @@ async fn controller(mut notes: broadcast::Receiver<Detection>, controller: Owned
let (note,vol) = note.context("channel closed")?;
if let Some(note) = note {
dbg!(note);
if let ControlFlow::Break(_) = sax_control(&mut control, vol, note, enable.clone()) {
if let ControlFlow::Break(_) = recorder_control(&mut control, vol, note, enable.clone()) {
if let ControlFlow::Break(_) = sax_control(&mut control, vol, note) {
if let ControlFlow::Break(_) = recorder_control(&mut control, vol, note) {
continue;
}
}
if enable.load(Ordering::Acquire) && interface.enabled() {
control = auto_data.borrow_and_update().clone();
}
send_packet(&mut controller, control.clone()).await?;
let _ = logging_sender.send(CombatData::Control(control.clone())).await;
logging_sender.send(CombatData::Control(control.clone())).await?;
gui.send_modify(|gui| gui.last_command = Some(control));
} else {
send_packet(&mut controller, ControlPacket::Twist(0.0, 0.0)).await?;
@ -227,7 +190,7 @@ async fn controller(mut notes: broadcast::Receiver<Detection>, controller: Owned
/// Weapon enabled
const ARMED: bool = true;
fn sax_control(control: &mut ControlPacket, vol: f32, frequency: f32, enable: Arc<AtomicBool>) -> ControlFlow<()> {
fn sax_control(control: &mut ControlPacket, vol: f32, frequency: f32) -> ControlFlow<()> {
if frequency < 150. {
println!("too low");
return ControlFlow::Break(());
@ -280,7 +243,7 @@ fn sax_control(control: &mut ControlPacket, vol: f32, frequency: f32, enable: Ar
}
fn recorder_control(control: &mut ControlPacket, vol: f32, frequency: f32, enable: Arc<AtomicBool>) -> ControlFlow<()> {
fn recorder_control(control: &mut ControlPacket, vol: f32, frequency: f32) -> ControlFlow<()> {
if frequency < 300. {
println!("too low");
return ControlFlow::Break(());
@ -320,12 +283,15 @@ fn recorder_control(control: &mut ControlPacket, vol: f32, frequency: f32, enabl
*control = ControlPacket::Stop;
}
Pitch { letter: NoteLetter::F, accidental: 0} => {
println!("auto en");
enable.store(true, Ordering::Release);
println!("stop");
*control = ControlPacket::Stop;
}
Pitch { letter: NoteLetter::E, accidental: 0} => {
println!("auto disable");
enable.store(false, Ordering::Release);
println!("stop");
*control = ControlPacket::Stop;
//if let result::Result::Ok(command) = auto_rx.try_recv() {
// control = command;
//}
}
pitch => {
if vol > 3000. {

View file

@ -1,57 +0,0 @@
use std::{cell::RefCell, collections::HashMap, fs::File, io::{Read, Write}, path::PathBuf, sync::Arc};
use anyhow::Result;
use dashmap::DashMap;
use crate::auto::Configurable;
#[derive(Clone)]
pub struct StorageManager {
map: Arc<DashMap<String, f32>>,
storage: Arc<PathBuf>,
}
impl StorageManager {
pub fn new(path: PathBuf) -> Result<Self> {
let map = if let Ok(mut file) = File::open(path.clone()) {
let mut vec = Vec::new();
file.read_to_end(&mut vec)?;
postcard::from_bytes_cobs(&mut vec)?
} else {
DashMap::new()
};
let map = Arc::new(map);
Ok(Self { map, storage: Arc::new(path) })
}
pub fn save(&self) {
let map = postcard::to_stdvec_cobs(&self.map).unwrap();
// TODO: mkdir
let mut backing_file = File::options().write(true).truncate(true).create(true)
.open(self.storage.as_path()).unwrap();
backing_file.write_all(&map).unwrap();
}
pub fn load(&self, setting: &'static Configurable) -> f32 {
self.map.get(setting.name).map(|v| *v).unwrap_or(setting.default)
}
pub fn store(&self, setting: &'static Configurable, value: f32) {
self.map.insert(setting.name.to_owned(), value);
}
pub fn get_set(&self, setting: &'static Configurable) -> impl 'static + FnMut(Option<f64>) -> f64 {
let self_ = self.clone();
let setting_ = setting;
move |new| {
if let Some(new) = new {
self_.store(setting_, new as f32);
}
self_.load(setting_) as f64
}
}
}

View file

@ -0,0 +1,157 @@
// copied from eframe (private item)
// https://docs.rs/eframe/latest/src/eframe/native/file_storage.rs.html#17
use std::path::PathBuf;
use eframe::egui;
pub fn storage() -> PathBuf {
storage_dir("cruisecontrol").unwrap()
}
pub fn storage_dir(app_id: &str) -> Option<PathBuf> {
use egui::os::OperatingSystem as OS;
use std::env::var_os;
match OS::from_target_os() {
OS::Nix => var_os("XDG_DATA_HOME")
.map(PathBuf::from)
.filter(|p| p.is_absolute())
.or_else(|| home::home_dir().map(|p| p.join(".local").join("share")))
.map(|p| {
p.join(
app_id
.to_lowercase()
.replace(|c: char| c.is_ascii_whitespace(), ""),
)
}),
OS::Mac => home::home_dir().map(|p| {
p.join("Library")
.join("Application Support")
.join(app_id.replace(|c: char| c.is_ascii_whitespace(), "-"))
}),
OS::Windows => roaming_appdata().map(|p| p.join(app_id).join("data")),
OS::Unknown | OS::Android | OS::IOS => None,
}
}
// Adapted from
// https://github.com/rust-lang/cargo/blob/6e11c77384989726bb4f412a0e23b59c27222c34/crates/home/src/windows.rs#L19-L37
#[cfg(all(windows, not(target_vendor = "uwp")))]
#[allow(unsafe_code)]
fn roaming_appdata() -> Option<PathBuf> {
use std::ffi::OsString;
use std::os::windows::ffi::OsStringExt;
use std::ptr;
use std::slice;
use windows_sys::Win32::Foundation::S_OK;
use windows_sys::Win32::System::Com::CoTaskMemFree;
use windows_sys::Win32::UI::Shell::{
FOLDERID_RoamingAppData, SHGetKnownFolderPath, KF_FLAG_DONT_VERIFY,
};
extern "C" {
fn wcslen(buf: *const u16) -> usize;
}
let mut path_raw = ptr::null_mut();
// SAFETY: SHGetKnownFolderPath allocates for us, we don't pass any pointers to it.
// See https://learn.microsoft.com/en-us/windows/win32/api/shlobj_core/nf-shlobj_core-shgetknownfolderpath
let result = unsafe {
SHGetKnownFolderPath(
&FOLDERID_RoamingAppData,
KF_FLAG_DONT_VERIFY as u32,
std::ptr::null_mut(),
&mut path_raw,
)
};
let path = if result == S_OK {
// SAFETY: SHGetKnownFolderPath indicated success and is supposed to allocate a nullterminated string for us.
let path_slice = unsafe { slice::from_raw_parts(path_raw, wcslen(path_raw)) };
Some(PathBuf::from(OsString::from_wide(path_slice)))
} else {
None
};
// SAFETY:
// This memory got allocated by SHGetKnownFolderPath, we didn't touch anything in the process.
// A null ptr is a no-op for `CoTaskMemFree`, so in case this failed we're still good.
// https://learn.microsoft.com/en-us/windows/win32/api/combaseapi/nf-combaseapi-cotaskmemfree
unsafe { CoTaskMemFree(path_raw.cast()) };
path
}
#[cfg(any(not(windows), target_vendor = "uwp"))]
fn roaming_appdata() -> Option<PathBuf> {
None
}