Fixes for willAppear and willDisappear and remove BaseDevice trait

This commit is contained in:
ninjadev64 committed 2024-01-04 13:22:53 +00:00
1 parent 8504c1c508
commit ea697de483
6 files changed
+44 -67

No files matched your search

-27
View File
@@ -7,20 +7,6 @@ use tokio::sync::Mutex;
use serde::Serialize;
/// A trait implemented by all supported devices.
pub trait BaseDevice {
fn num_sliders(&self) -> u8;
fn num_rows(&self) -> u8;
fn num_columns(&self) -> u8;
fn num_keys(&self) -> u8 {
self.num_rows() * self.num_columns()
}
fn id(&self) -> String;
fn name(&self) -> String;
fn r#type(&self) -> u8;
}
/// Metadata of a device.
#[derive(Clone, Serialize)]
pub struct DeviceInfo {
@@ -32,19 +18,6 @@ pub struct DeviceInfo {
pub r#type: u8
}
impl DeviceInfo {
fn new(device: &impl BaseDevice) -> DeviceInfo {
DeviceInfo {
id: device.id(),
name: device.name(),
rows: device.num_rows(),
columns: device.num_columns(),
sliders: device.num_sliders(),
r#type: device.r#type()
}
}
}
lazy_static! {
pub static ref DEVICES: Mutex<HashMap<String, DeviceInfo>> = Mutex::new(HashMap::new());
}
+19 -31
View File
@@ -1,4 +1,4 @@
use super::BaseDevice;
use super::DeviceInfo;
use crate::events::outbound;
@@ -8,29 +8,13 @@ use std::time::Duration;
use serde_json::Value;
use log::{warn, error};
/// A representation of a ProntoKey device.
pub struct ProntoKeyDevice {
address: String
}
impl BaseDevice for ProntoKeyDevice {
fn num_sliders(&self) -> u8 { 2 }
fn num_rows(&self) -> u8 { 3 }
fn num_columns(&self) -> u8 { 3 }
fn id(&self) -> String {
format!("pk-{}", self.address)
}
fn name(&self) -> String {
String::from("ProntoKey")
}
fn r#type(&self) -> u8 { 7 }
}
pub struct ProntoKeyDevice {}
impl ProntoKeyDevice {
/// Attempt to open a serial connection with the device and handle incoming data.
pub async fn init(port: String) {
let mut device: Option<ProntoKeyDevice> = Option::None;
let mut initialised = false;
let mut device_id = "".to_owned();
let mut last_key: u8 = 0;
let mut last_sliders: Vec<i16> = vec![0; 2];
@@ -68,26 +52,30 @@ impl ProntoKeyDevice {
holding_string = holding_string[(index + 1)..].to_owned();
// If the device is uninitialised, attempt to read its MAC address and initialise.
if device.is_none() {
if !initialised {
if let Value::String(address) = &j["address"] {
device = Some(ProntoKeyDevice { address: address.clone() });
if let Some(device) = &device {
super::DEVICES.lock().await.insert(device.id(), super::DeviceInfo::new(device));
}
initialised = true;
device_id = format!("pk-{}", address);
super::DEVICES.lock().await.insert(device_id.clone(), DeviceInfo {
id: device_id.clone(),
name: "ProntoKey".to_owned(),
rows: 3,
columns: 3,
sliders: 2,
r#type: 7
});
}
continue;
}
let device = device.as_ref().unwrap();
// Handle key presses and releases.
if let Value::Number(num) = &j["key"] {
match num.as_u64().unwrap_or_default() as u8 {
0 => {
let _ = outbound::keypad::key_up(device.id(), last_key - 1).await;
let _ = outbound::keypad::key_up(&device_id, last_key - 1).await;
},
val => {
let _ = outbound::keypad::key_down(device.id(), val - 1).await;
let _ = outbound::keypad::key_down(&device_id, val - 1).await;
last_key = val;
}
}
@@ -99,7 +87,7 @@ impl ProntoKeyDevice {
Some(v) => v as i16,
_ => last_sliders[0]
};
let _ = outbound::encoder::dial_rotate(device.id(), 0, val - last_sliders[0]).await;
let _ = outbound::encoder::dial_rotate(&device_id, 0, val - last_sliders[0]).await;
last_sliders[0] = val;
}
if let Value::Number(val) = &j["slider1"] {
@@ -107,7 +95,7 @@ impl ProntoKeyDevice {
Some(v) => v as i16,
_ => last_sliders[1]
};
let _ = outbound::encoder::dial_rotate(device.id(), 1, val - last_sliders[1]).await;
let _ = outbound::encoder::dial_rotate(&device_id, 1, val - last_sliders[1]).await;
last_sliders[1] = val;
}
}
+14
View File
@@ -124,13 +124,21 @@ pub async fn create_instance(app: tauri::AppHandle, action: Action, context: Act
let instance_ref: &Option<ActionInstance>;
if context.controller == "Encoder" {
if let Some(instance) = &store.value.sliders[context.position as usize] {
let _ = crate::events::outbound::will_appear::will_disappear(instance).await;
}
store.value.sliders[context.position as usize] = Some(instance);
instance_ref = &store.value.sliders[context.position as usize];
} else {
if let Some(instance) = &store.value.keys[context.position as usize] {
let _ = crate::events::outbound::will_appear::will_disappear(instance).await;
}
store.value.keys[context.position as usize] = Some(instance);
instance_ref = &store.value.keys[context.position as usize];
}
let _ = crate::events::outbound::will_appear::will_appear(instance_ref.as_ref().unwrap()).await;
if let Err(error) = store.save() {
return serde_json::to_string(&Error { description: error.to_string() }).unwrap();
}
@@ -152,9 +160,15 @@ pub async fn clear_slot(app: tauri::AppHandle, context: ActionContext) -> String
let instance_ref: &Option<ActionInstance>;
if context.controller == "Encoder" {
if let Some(instance) = &store.value.sliders[context.position as usize] {
let _ = crate::events::outbound::will_appear::will_disappear(instance).await;
}
store.value.sliders[context.position as usize] = None;
instance_ref = &store.value.sliders[context.position as usize];
} else {
if let Some(instance) = &store.value.keys[context.position as usize] {
let _ = crate::events::outbound::will_appear::will_disappear(instance).await;
}
store.value.keys[context.position as usize] = None;
instance_ref = &store.value.keys[context.position as usize];
}
+2 -2
View File
@@ -22,8 +22,8 @@ struct DialRotateEvent {
payload: DialRotatePayload
}
pub async fn dial_rotate(device: String, index: u8, ticks: i16) -> Result<(), anyhow::Error> {
let instance = match get_instance(&device, index, "Encoder").await? {
pub async fn dial_rotate(device: &str, index: u8, ticks: i16) -> Result<(), anyhow::Error> {
let instance = match get_instance(device, index, "Encoder").await? {
Some(instance) => instance,
None => return Ok(())
};
+4 -4
View File
@@ -14,8 +14,8 @@ struct KeyEvent {
payload: GenericInstancePayload
}
pub async fn key_down(device: String, key: u8) -> Result<(), anyhow::Error> {
let instance = match get_instance(&device, key, "Keypad").await? {
pub async fn key_down(device: &str, key: u8) -> Result<(), anyhow::Error> {
let instance = match get_instance(device, key, "Keypad").await? {
Some(instance) => instance,
None => return Ok(())
};
@@ -29,7 +29,7 @@ pub async fn key_down(device: String, key: u8) -> Result<(), anyhow::Error> {
}).await
}
pub async fn key_up(device: String, key: u8) -> Result<(), anyhow::Error> {
pub async fn key_up(device: &str, key: u8) -> Result<(), anyhow::Error> {
let (
app,
mut device_stores,
@@ -38,7 +38,7 @@ pub async fn key_up(device: String, key: u8) -> Result<(), anyhow::Error> {
) = crate::store::profiles::lock_mutexes().await;
let selected_profile = &device_stores.get_device_store(&device, app.as_ref().unwrap())?.value.selected_profile;
let device = devices.get(&device).unwrap();
let device = devices.get(device).unwrap();
let store = profile_stores.get_profile_store(device, selected_profile, app.as_ref().unwrap())?;
let profile = &mut store.value;
+5 -3
View File
@@ -9,8 +9,10 @@
let profiles: string[] = [];
async function getProfiles(device: DeviceInfo) {
profiles = JSON.parse(await invoke("get_profiles", { device: device.id }));
value = JSON.parse(await invoke("get_selected_profile", { device: device.id })).id;
setProfile(value);
profile = JSON.parse(await invoke("get_selected_profile", { device: device.id }));
if (value == profile.id) return;
value = profile.id;
await invoke("set_selected_profile", { device: device.id, id: profile.id });
}
export let profile: Profile;
@@ -27,7 +29,7 @@
if (value == "opendeck_edit_profiles") {
if (oldValue) showPopup = true;
value = oldValue;
} else {
} else if (value && (!profile || profile.id != value)) {
setProfile(value);
oldValue = value;
}