Skip to content

Commit

Permalink
Refactor eventing on HW bridge
Browse files Browse the repository at this point in the history
  • Loading branch information
larsbaunwall committed Feb 23, 2024
1 parent d33003e commit f343cfb
Show file tree
Hide file tree
Showing 4 changed files with 280 additions and 25 deletions.
22 changes: 16 additions & 6 deletions src/ui/src-tauri/src/hw_controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,23 @@ impl HWController {
tauri::async_runtime::spawn(async move {
let app_handle_clone = app_handle.clone();
controller_clone.lock().unwrap().register_wheel_event_callback(Arc::new(Mutex::new(move |(wheel, pos): (Wheel, u8)| {
let payload = WheelEvent {
wheel,
position: pos,
let payload = HardwareEvent {
kind: "wheel".to_string(),
source: wheel.,
value: pos,
};

app_handle_clone.emit_all("wheelEvent", Some(payload)).unwrap();
app_handle_clone.emit_all("hardwareEvent", Some(payload)).unwrap();

Ok(())
})));

let app_handle_clone = app_handle.clone();
controller_clone.lock().unwrap().register_button_event_callback(Arc::new(Mutex::new(move |button: Button| {
let payload = ButtonEvent {
button,
let payload = HardwareEvent {
kind: "button".to_string(),
source: button,
value: 0
};

app_handle_clone.emit_all("buttonEvent", Some(payload)).unwrap();
Expand Down Expand Up @@ -88,6 +91,13 @@ struct ButtonEvent {
button: Button,
}

#[derive(Clone, serde::Serialize)]
struct HardwareEvent {
kind: String,
source: String,
value: u8,
}

#[derive(Clone, serde::Serialize)]
struct WheelEvent {
wheel: Wheel,
Expand Down
49 changes: 38 additions & 11 deletions src/ui/src/hardware/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,61 @@ import {listen} from "@tauri-apps/api/event";
import arcs from "../utils/arcs.ts";
import {useUIStore} from "../stores/ui.ts";

export enum Wheel {
Front = "Front",
Angle = "Angle",
Back = "Back"
}

export enum Button {
Left = "Left",
Right = "Right",
Go = "Go",
Standby = "Standby"
}

export interface WheelEvent {
payload: {
position: number;
wheel: string;
};
position: number,
wheel: string,
},
}

export interface HardwareEvent {
payload: {
kind: 'wheel' | 'button' | string,
source: Wheel | Button | string,
value: number
},
}

export const startHardwareBridge = () => {

const uiStore = useUIStore();
const allWheelEvents$ = new Subject<WheelEvent>();
const diagnostics$ = new Subject<any>();
const frontWheelEvents$ = allWheelEvents$.pipe(

const allWheelEventsSubject = new Subject<WheelEvent>();
const diagnosticsSubject = new Subject<any>();

const frontWheelEvents$ = allWheelEventsSubject.pipe(
filter(event => event.payload.wheel === 'Front')
).pipe(bufferCount(10));
const backWheelEvents$ = allWheelEvents$.pipe(
const backWheelEvents$ = allWheelEventsSubject.pipe(
filter(event => event.payload.wheel === 'Back')
).pipe(bufferCount(10));

const unlisten = listen('wheelEvent', (event: WheelEvent) => {
allWheelEvents$.next(event);
allWheelEventsSubject.next(event);
});
const diags = listen('diagnostics', (event) => {
diagnostics$.next(event);
diagnosticsSubject.next(event);
console.log({event});
});

allWheelEvents$.subscribe((event) => {
const unlistenAllEvents = listen('hardwareEvent', (event: HardwareEvent) => {
uiStore.hardwareEvents.next(event);
});

allWheelEventsSubject.subscribe((event) => {
if (event.payload.wheel == 'Angular') {
uiStore.wheelPointerAngle = arcs.translateToRange(event.payload.position, 152, 195);
}
Expand All @@ -47,7 +74,7 @@ export const startHardwareBridge = () => {
uiStore.volume = Math.max(0, Math.min(newVolume, 100));
});

return {wheelEvents: allWheelEvents$, diagnostics: diagnostics$};
return {wheelEvents: allWheelEventsSubject, diagnostics: diagnosticsSubject};
}

function wheelSpinDifference(value: number): number {
Expand Down
5 changes: 4 additions & 1 deletion src/ui/src/stores/ui.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { defineStore } from "pinia";
import { ref } from "vue";
import {Subject} from "rxjs";
import {HardwareEvent} from "../hardware/events.ts";

//import { invoke } from "@tauri-apps/api";

Expand All @@ -8,10 +10,11 @@ export const useUIStore = defineStore('ui', () => {
const wheelPointerAngle = ref(180);
const topWheelPosition = ref(0);
const isNowPLayingOverlayActive = ref(false);
const hardwareEvents = new Subject<HardwareEvent>();

const tick = () => {
//invoke('tick');
}

return {volume, wheelPointerAngle, topWheelPosition, isNowPLayingOverlayActive, tick}
return {hardwareEvents, volume, wheelPointerAngle, topWheelPosition, isNowPLayingOverlayActive, tick}
})
Loading

0 comments on commit f343cfb

Please sign in to comment.