feat: Half working state

This commit is contained in:
2023-10-13 09:43:42 +02:00
parent bac28c30ad
commit 4ae2e63add
20 changed files with 1521 additions and 183 deletions

View File

@@ -2,6 +2,27 @@
# It is not intended for manual editing.
version = 3
[[package]]
name = "CoreFoundation-sys"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d0e9889e6db118d49d88d84728d0e964d973a5680befb5f85f55141beea5c20b"
dependencies = [
"libc",
"mach",
]
[[package]]
name = "IOKit-sys"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "99696c398cbaf669d2368076bdb3d627fb0ce51a26899d7c61228c5c0af3bf4a"
dependencies = [
"CoreFoundation-sys",
"libc",
"mach",
]
[[package]]
name = "addr2line"
version = "0.21.0"
@@ -1417,6 +1438,26 @@ version = "0.2.148"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9cdc71e17332e86d2e1d38c1f99edcb6288ee11b815fb1a4b049eaa2114d369b"
[[package]]
name = "libudev"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "78b324152da65df7bb95acfcaab55e3097ceaab02fb19b228a9eb74d55f135e0"
dependencies = [
"libc",
"libudev-sys",
]
[[package]]
name = "libudev-sys"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3c8469b4a23b962c1396b9b451dda50ef5b283e8dd309d69033475fa9b334324"
dependencies = [
"libc",
"pkg-config",
]
[[package]]
name = "line-wrap"
version = "0.1.1"
@@ -1469,6 +1510,24 @@ version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c41e0c4fef86961ac6d6f8a82609f55f31b05e4fce149ac5710e439df7619ba4"
[[package]]
name = "mach"
version = "0.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2fd13ee2dd61cc82833ba05ade5a30bb3d63f7ced605ef827063c63078302de9"
dependencies = [
"libc",
]
[[package]]
name = "mach2"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6d0d1830bcd151a6fc4aea1369af235b36c1528fe976b8ff678683c9995eade8"
dependencies = [
"libc",
]
[[package]]
name = "malloc_buf"
version = "0.0.6"
@@ -1580,6 +1639,17 @@ version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e4a24736216ec316047a1fc4252e27dabb04218aa4a3f37c6e7ddbf1f9782b54"
[[package]]
name = "nix"
version = "0.26.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "598beaf3cc6fdd9a5dfb1630c2800c7acd31df7aaf0f565796fba2b53ca1af1b"
dependencies = [
"bitflags 1.3.2",
"cfg-if",
"libc",
]
[[package]]
name = "nodrop"
version = "0.1.14"
@@ -2366,6 +2436,24 @@ dependencies = [
"syn 1.0.109",
]
[[package]]
name = "serialport"
version = "4.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c32634e2bd4311420caa504404a55fad2131292c485c97014cbed89a5899885f"
dependencies = [
"CoreFoundation-sys",
"IOKit-sys",
"bitflags 1.3.2",
"cfg-if",
"libudev",
"mach2",
"nix",
"regex",
"scopeguard",
"winapi",
]
[[package]]
name = "servo_arc"
version = "0.1.1"
@@ -2997,6 +3085,7 @@ version = "0.0.0"
dependencies = [
"serde",
"serde_json",
"serialport",
"tauri",
"tauri-build",
]

View File

@@ -16,6 +16,7 @@ tauri-build = { version = "1.5", features = [] }
tauri = { version = "1.5", features = ["shell-open"] }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
serialport = "4.2.2"
[features]
# this feature is used for production builds or when `devPath` points to the filesystem

View File

@@ -1,15 +1,93 @@
// Prevents additional console window on Windows in release, DO NOT REMOVE!!
#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
// Learn more about Tauri commands at https://tauri.app/v1/guides/features/command
use std::sync:: Mutex;
use std::io::ErrorKind;
use std::time::Duration;
use std::collections::HashMap;
use tauri::State;
#[derive(Default)]
struct PortMap(Mutex<HashMap<String, Box<dyn serialport::SerialPort>>>);
#[tauri::command]
fn greet(name: &str) -> String {
format!("Hello, {}! You've been greeted from Rust!", name)
async fn read_serial(port_name: &str, port_map: State<'_, PortMap>) -> Result<(), String> {
match port_map.0.lock().unwrap().get(port_name) {
Some(port) => {
let mut clone = port.try_clone().expect("Failed to clone");
let mut buffer: Vec<u8> = vec![0; 1024];
println!("Reading serial");
match clone.read(buffer.as_mut_slice()) {
Ok(bytes_read) => {
if bytes_read > 0 {
let data = &buffer[..bytes_read];
let data = String::from_utf8_lossy(data).to_string();
if !data.trim().is_empty() {
println!("{}", data.trim());
}
}
Ok(())
},
Err(ref e) if e.kind() == ErrorKind::TimedOut => Err(String::from("Timed out")),
Err(e) => Err(e.to_string()),
}
}
None => {
Ok(())
}
}
}
#[tauri::command]
async fn write_serial(input: &str, port_name: &str, port_map: State<'_, PortMap>) -> Result<(), String> {
println!("Trying to write");
match port_map.0.lock().unwrap().get(port_name) {
Some(port) => {
let mut clone = port.try_clone().expect("Failed to clone");
println!("Writing to serial");
match clone.write(input.as_bytes()) {
Ok(_) => {
Ok(())
},
Err(err) => Err(err.to_string()),
}
}
None => {
println!("Writing to serial not found");
Ok(())
}
}
}
#[tauri::command]
fn get_serial_ports() -> Vec<String> {
let ports = serialport::available_ports().expect("No ports found");
let mut vec: Vec<String> = Vec::new();
for p in ports {
vec.push(p.port_name);
}
vec
}
#[tauri::command]
async fn open_port(app_handle: tauri::AppHandle, port_map: State<'_, PortMap>) -> Result<(), String> {
match serialport::new("/dev/ttyUSB0", 9600).timeout(Duration::from_millis(10)).open() {
Ok(port) => {
port_map.0.lock().unwrap().insert("/dev/ttyUSB0".to_string(), port);
println!("Port open");
},
Err(err) => return Err(err.to_string()),
};
Ok(())
}
fn main() {
tauri::Builder::default()
.invoke_handler(tauri::generate_handler![greet])
.manage(PortMap::default())
.invoke_handler(tauri::generate_handler![get_serial_ports, read_serial, write_serial, open_port])
.run(tauri::generate_context!())
.expect("error while running tauri application");
}

View File

@@ -37,7 +37,7 @@
{
"fullscreen": false,
"resizable": true,
"title": "toos-dashboard",
"title": "Frankenstein Dashboard",
"width": 800,
"height": 600
}