Skip to content

Commit

Permalink
Merge pull request #88 from matheuswhite/86-scope-info
Browse files Browse the repository at this point in the history
Add `scope.info` to plugins
  • Loading branch information
matheuswhite authored May 10, 2024
2 parents cec8f0c + 25b816e commit 3e8dd17
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 38 deletions.
4 changes: 4 additions & 0 deletions plugins/scope.lua
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,9 @@ scope = {
quiet = quiet or false
local _, stdout, stderr = coroutine.yield({ ":exec", cmd, quiet })
return stdout, stderr
end,
info = function()
local _, output = coroutine.yield({ ":info" })
return output
end
}
14 changes: 10 additions & 4 deletions plugins/test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,23 @@
require "scope"

function serial_rx(msg)
scope.connect('/dev/ttyACM0', 115200)
scope.disconnect()
scope.serial_tx(msg)
scope.connect('COM1', 115200)
--scope.serial_tx(msg)
scope.println('Sent ' .. bytes2str(msg))
scope.eprintln('Timeout')
end

function user_command(arg_list)
scope.connect('/dev/ttyACM0', 115200)
scope.disconnect()
scope.serial_tx(str2bytes(arg_list[1]))
scope.connect('COM1', 115200)
--scope.serial_tx({ 1, 2, 3 })
scope.println('Sent ' .. arg_list[2])
scope.eprintln('Timeout')
scope.exec('echo hello', true)
scope.exec('echo hello')
info = scope.info()
-- info: {'serial': {port: 'COM1', baudrate: '115200', is_connected: true}}
text = '[' .. tostring(info.serial.is_connected) .. '] serial: ' .. info.serial.port .. '@' .. tostring(info.serial.baudrate) .. 'bps'
scope.println(text)
end
170 changes: 138 additions & 32 deletions src/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,33 @@ pub enum PluginRequest {
cmd: String,
quiet: bool,
},
Info,
}

pub enum PluginRequestResult {
Exec {
stdout: Vec<String>,
stderr: Vec<String>,
},
Info {
serial: SerialInfoResult,
},
}

pub struct SerialInfoResult {
port: String,
baudrate: u32,
is_connected: bool,
}

impl SerialInfoResult {
pub fn new(port: String, baudrate: u32, is_connected: bool) -> Self {
Self {
port,
baudrate,
is_connected,
}
}
}

pub struct SerialRxCall {
Expand Down Expand Up @@ -153,6 +173,7 @@ impl<'a> TryFrom<Table<'a>> for PluginRequest {
cmd: value.get(2).map_err(|err| err.to_string())?,
quiet: value.get(3).map_err(|err| err.to_string())?,
}),
":info" => Ok(PluginRequest::Info),
_ => Err("Unknown function".to_string()),
}
}
Expand Down Expand Up @@ -349,6 +370,40 @@ impl Iterator for SerialRxCall {
Err(_) => None,
}
}
PluginRequestResult::Info {
serial:
SerialInfoResult {
port,
baudrate,
is_connected,
},
} => {
let serial = self
.lua
.create_table()
.expect("Cannot create serial lua table");
serial.set("port", port).expect("Cannot add port");
serial
.set("baudrate", baudrate)
.expect("Cannot add baudrate");
serial
.set("is_connected", is_connected)
.expect("Cannot add baudrate");

let table = self.lua.create_table().expect("Cannot create a lua table");
table.set("serial", serial).expect("Cannot add serial");

match serial_rx.resume::<_, Table>((msg, table)) {
Ok(req) => {
let req: PluginRequest = match req.try_into() {
Ok(req) => req,
Err(msg) => return Some(PluginRequest::Eprintln { msg }),
};
Some(req)
}
Err(_) => None,
}
}
}
}
}
Expand Down Expand Up @@ -383,6 +438,40 @@ impl Iterator for UserCommandCall {
Err(_) => None,
}
}
PluginRequestResult::Info {
serial:
SerialInfoResult {
port,
baudrate,
is_connected,
},
} => {
let serial = self
.lua
.create_table()
.expect("Cannot create serial lua table");
serial.set("port", port).expect("Cannot add port");
serial
.set("baudrate", baudrate)
.expect("Cannot add baudrate");
serial
.set("is_connected", is_connected)
.expect("Cannot add baudrate");

let table = self.lua.create_table().expect("Cannot create a lua table");
table.set("serial", serial).expect("Cannot add serial");

match user_command.resume::<_, Table>((arg_list, table)) {
Ok(req) => {
let req: PluginRequest = match req.try_into() {
Ok(req) => req,
Err(msg) => return Some(PluginRequest::Eprintln { msg }),
};
Some(req)
}
Err(_) => None,
}
}
}
}
}
Expand Down Expand Up @@ -419,20 +508,28 @@ mod tests {
let plugin = Plugin::new(PathBuf::from("plugins/test.lua"))?;
let serial_rx_call = plugin.serial_rx_call(msg.as_bytes().to_vec());
let expected = [
PluginRequest::Disconnect,
PluginRequest::Connect {
port: Some("/dev/ttyACM0".to_string()),
port: Some("COM1".to_string()),
baud_rate: Some(115200),
},
PluginRequest::Disconnect,
PluginRequest::SerialTx {
msg: msg.as_bytes().to_vec(),
},
// PluginRequest::SerialTx {
// msg: msg.as_bytes().to_vec(),
// },
PluginRequest::Println {
msg: format!("Sent {}", msg),
},
PluginRequest::Eprintln {
msg: "Timeout".to_string(),
},
PluginRequest::Exec {
cmd: "echo hello".to_string(),
quiet: false,
},
PluginRequest::Info,
PluginRequest::Println {
msg: "".to_string(),
},
];

for (i, req) in serial_rx_call.enumerate() {
Expand All @@ -455,25 +552,25 @@ mod tests {
plugin[1].serial_rx_call(msg[1].as_bytes().to_vec()),
];
let expected = vec![
(PluginRequest::Disconnect, PluginRequest::Disconnect),
(
PluginRequest::Connect {
port: Some("/dev/ttyACM0".to_string()),
port: Some("COM1".to_string()),
baud_rate: Some(115200),
},
PluginRequest::Connect {
port: Some("/dev/ttyACM0".to_string()),
port: Some("COM1".to_string()),
baud_rate: Some(115200),
},
),
(PluginRequest::Disconnect, PluginRequest::Disconnect),
(
PluginRequest::SerialTx {
msg: msg[0].as_bytes().to_vec(),
},
PluginRequest::SerialTx {
msg: msg[1].as_bytes().to_vec(),
},
),
// (
// PluginRequest::SerialTx {
// msg: msg[0].as_bytes().to_vec(),
// },
// PluginRequest::SerialTx {
// msg: msg[1].as_bytes().to_vec(),
// },
// ),
(
PluginRequest::Println {
msg: format!("Sent {}", msg[0]),
Expand Down Expand Up @@ -513,20 +610,29 @@ mod tests {
let plugin = Plugin::new(PathBuf::from("plugins/test.lua"))?;
let user_command_call = plugin.user_command_call(arg_list);
let expected = [
PluginRequest::Disconnect,
PluginRequest::Connect {
port: Some("/dev/ttyACM0".to_string()),
port: Some("COM1".to_string()),
baud_rate: Some(115200),
},
PluginRequest::Disconnect,
PluginRequest::SerialTx {
msg: "Hello".as_bytes().to_vec(),
},
// PluginRequest::SerialTx {
// msg: "Hello".as_bytes().to_vec(),
// },
PluginRequest::Println {
msg: "Sent World!".to_string(),
},
PluginRequest::Eprintln {
msg: "Timeout".to_string(),
},
PluginRequest::Exec {
cmd: "echo hello".to_string(),
quiet: true,
},
PluginRequest::Exec {
cmd: "echo hello".to_string(),
quiet: false,
},
PluginRequest::Info,
];

for (i, req) in user_command_call.enumerate() {
Expand Down Expand Up @@ -557,25 +663,25 @@ mod tests {
plugin[1].user_command_call(arg_list[1].clone()),
];
let expected = vec![
(PluginRequest::Disconnect, PluginRequest::Disconnect),
(
PluginRequest::Connect {
port: Some("/dev/ttyACM0".to_string()),
port: Some("COM1".to_string()),
baud_rate: Some(115200),
},
PluginRequest::Connect {
port: Some("/dev/ttyACM0".to_string()),
port: Some("COM1".to_string()),
baud_rate: Some(115200),
},
),
(PluginRequest::Disconnect, PluginRequest::Disconnect),
(
PluginRequest::SerialTx {
msg: arg_list[0][0].as_bytes().to_vec(),
},
PluginRequest::SerialTx {
msg: arg_list[1][0].as_bytes().to_vec(),
},
),
// (
// PluginRequest::SerialTx {
// msg: arg_list[0][0].as_bytes().to_vec(),
// },
// PluginRequest::SerialTx {
// msg: arg_list[1][0].as_bytes().to_vec(),
// },
// ),
(
PluginRequest::Println {
msg: format!("Sent {}", arg_list[0][1]),
Expand Down
17 changes: 15 additions & 2 deletions src/plugin_manager.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::messages::{SerialRxData, UserTxData};
use crate::plugin::{
Plugin, PluginRequest, PluginRequestResult, PluginRequestResultHolder, SerialRxCall,
UserCommandCall,
Plugin, PluginRequest, PluginRequestResult, PluginRequestResultHolder, SerialInfoResult,
SerialRxCall, UserCommandCall,
};
use crate::process::ProcessRunner;
use crate::serial::SerialIF;
Expand Down Expand Up @@ -322,6 +322,19 @@ impl PluginManager {
return res;
}
},
PluginRequest::Info => {
let serial_if = interface.lock().await;
let serial_info = serial_if.get_info();
let serial_is_connected = serial_if.is_connected();

return Some(PluginRequestResult::Info {
serial: SerialInfoResult::new(
serial_info.port().to_owned(),
serial_info.baudrate(),
serial_is_connected,
),
});
}
}

None
Expand Down
14 changes: 14 additions & 0 deletions src/serial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ impl SerialIF {
)
}

pub fn get_info(&self) -> SerialInfo {
self.synchronized().info.clone()
}

pub fn is_connected(&self) -> bool {
self.synchronized().is_connected.load(Ordering::SeqCst)
}
Expand Down Expand Up @@ -321,3 +325,13 @@ pub struct SerialInfo {
port: String,
baudrate: u32,
}

impl SerialInfo {
pub fn port(&self) -> &str {
&self.port
}

pub fn baudrate(&self) -> u32 {
self.baudrate
}
}

0 comments on commit 3e8dd17

Please sign in to comment.