-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Poll for serial port changes from Rust
Signed-off-by: xychen <xychen@listenai.com>
- Loading branch information
xychen
committed
May 27, 2024
1 parent
75c5638
commit a97f52a
Showing
7 changed files
with
175 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
mod poll; | ||
|
||
pub trait SerialPortEventHandler: Send + 'static { | ||
fn handle_event(&mut self); | ||
} | ||
|
||
pub trait SerialPortWatcher { | ||
fn new<F: SerialPortEventHandler>(event_handler: F) -> Self | ||
where | ||
Self: Sized; | ||
|
||
fn watch(&mut self) -> (); | ||
|
||
fn unwatch(&mut self) -> (); | ||
} | ||
|
||
pub type SerialPortWatcherImpl = poll::SerialPortPollWatcher; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
use std::{ | ||
sync::{Arc, Mutex}, | ||
thread::{sleep, spawn}, | ||
time::Duration, | ||
}; | ||
|
||
use super::{SerialPortEventHandler, SerialPortWatcher}; | ||
|
||
pub struct SerialPortPollWatcher { | ||
polling: Arc<Mutex<bool>>, | ||
event_handler: Arc<Mutex<dyn SerialPortEventHandler>>, | ||
} | ||
|
||
impl SerialPortWatcher for SerialPortPollWatcher { | ||
fn new<F: SerialPortEventHandler>(event_handler: F) -> Self { | ||
Self { | ||
polling: Arc::new(Mutex::new(false)), | ||
event_handler: Arc::new(Mutex::new(event_handler)), | ||
} | ||
} | ||
|
||
fn watch(&mut self) -> () { | ||
let polling = self.polling.clone(); | ||
let event_handler = self.event_handler.clone(); | ||
|
||
spawn(move || { | ||
let mut is_polling = polling.lock().unwrap(); | ||
*is_polling = true; | ||
drop(is_polling); | ||
|
||
loop { | ||
let is_polling = polling.lock().unwrap(); | ||
if !*is_polling { | ||
break; | ||
} | ||
drop(is_polling); | ||
|
||
let mut event_handler = event_handler.lock().unwrap(); | ||
event_handler.handle_event(); | ||
drop(event_handler); | ||
|
||
sleep(Duration::from_secs(1)); | ||
} | ||
}); | ||
} | ||
|
||
fn unwatch(&mut self) -> () { | ||
let mut polling = self.polling.lock().unwrap(); | ||
*polling = false; | ||
} | ||
} | ||
|
||
impl Drop for SerialPortPollWatcher { | ||
fn drop(&mut self) { | ||
let _ = self.unwatch(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,13 @@ | ||
import '@tauri-apps/api/core'; | ||
|
||
declare module '@tauri-apps/api/core' { | ||
import type { Channel } from '@tauri-apps/api/core'; | ||
import type { ISection } from '@/utils/readHex'; | ||
import type { IPartition } from '@/utils/images'; | ||
function invoke(cmd: 'read_hex', args: { path: string }): Promise<ISection[]>; | ||
function invoke(cmd: 'read_lpk', args: { path: string }): Promise<IPartition[]>; | ||
function invoke(cmd: 'list_ports'): Promise<string[]>; | ||
function invoke<T>(cmd: 'watch_ports', args: { onEvent: Channel<T> }): Promise<number>; | ||
function invoke(cmd: 'unwatch_ports', args: { rid: number }): Promise<void>; | ||
function invoke(cmd: 'decode', args: { data: ArrayLike<number> }): Promise<string>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { invoke, Channel } from '@tauri-apps/api/core'; | ||
|
||
type UnwatchFn = () => void; | ||
|
||
export async function listPorts(): Promise<string[]> { | ||
return await invoke('list_ports'); | ||
} | ||
|
||
export async function watchPorts(cb: (ports: string[]) => void): Promise<UnwatchFn> { | ||
const onEvent = new Channel<string[]>(); | ||
onEvent.onmessage = cb; | ||
|
||
const rid = await invoke('watch_ports', { | ||
onEvent: onEvent, | ||
}); | ||
|
||
return () => { | ||
void invoke('unwatch_ports', { rid }); | ||
}; | ||
} |