Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP
Browse files Browse the repository at this point in the history
Nicoretti committed Apr 17, 2024
1 parent c0c9969 commit fb313e7
Showing 7 changed files with 418 additions and 10 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@ target/
.idea/
.venv/
.vscode/
.env
RND_320-KA3000-COMMANDS.pdf
RND_320-KA3000-User-Manual.pdf

238 changes: 229 additions & 9 deletions Cargo.lock
8 changes: 7 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -21,10 +21,16 @@ anyhow = "1.*"
clap = { version = "4.*", features = ["derive"] }
log = "0.4.21"
env_logger = "0.11.3"
pyo3 = "0.21.2"

[lib]
name = "ka3005p"
path = "src/lib.rs"
crate-type = ["rlib", "cdylib"]

[[bin]]
name = "ka3005p"
path = "src/bin/ka3005p.rs"
path = "src/bin/main.rs"

[profile.release]
lto = true
17 changes: 17 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[build-system]
requires = ["maturin>=1.5,<2.0"]
build-backend = "maturin"

[project]
name = "ka3005p"
requires-python = ">=3.8"
classifiers = [
"Programming Language :: Rust",
"Programming Language :: Python :: Implementation :: CPython",
"Programming Language :: Python :: Implementation :: PyPy",
]
dynamic = ["version"]

[tool.maturin]
features = ["pyo3/extension-module"]

File renamed without changes.
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -38,6 +38,7 @@

#[doc(hidden)] // Users of the library shouldn't use this
pub mod cli;
pub mod py_module;
pub use serialport;

/// On / Off
@@ -308,7 +309,7 @@
let printable_ascii = |bytes: Vec<u8>| -> String {
bytes
.into_iter()
.filter(|&b| b >= 32 && b <= 126)

Check failure on line 312 in src/lib.rs

GitHub Actions / clippy

manual `RangeInclusive::contains` implementation

error: manual `RangeInclusive::contains` implementation --> src/lib.rs:312:30 | 312 | .filter(|&b| b >= 32 && b <= 126) | ^^^^^^^^^^^^^^^^^^^ help: use: `(32..=126).contains(&b)` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#manual_range_contains = note: `#[deny(clippy::manual_range_contains)]` implied by `#[deny(warnings)]`
.collect::<Vec<u8>>()
.into_iter()
.map(|b| b as char)
163 changes: 163 additions & 0 deletions src/py_module.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,163 @@
//! doc
use crate::{find_serial_port, Command, Ka3005p, Status, Switch};
use anyhow::Error;
use pyo3::exceptions::PyException;
use pyo3::prelude::*;
use pyo3::PyErr;

/// doc
#[pyfunction]
fn list_power_supplies() -> PyResult<Vec<String>> {
let ports: Vec<String> = crate::list_serial_ports()
.into_iter()
.map(|p| p.port_name)
.collect();
Ok(ports)
}

struct Ka3005pError(Error);

impl From<Ka3005pError> for PyErr {
fn from(error: Ka3005pError) -> Self {
PyException::new_err(error.0.to_string())
}
}

impl From<Error> for Ka3005pError {
fn from(error: Error) -> Self {
Self(error)
}
}

#[pyclass]
struct PowerSupply {
inner: Ka3005p,
}

// helper methods
impl PowerSupply {
fn execute(&mut self, command: Command) -> PyResult<()> {
Ok(self
.inner
.execute(command)
.map_err(|e| Into::<Ka3005pError>::into(e))?)

Check failure on line 43 in src/py_module.rs

GitHub Actions / clippy

redundant closure

error: redundant closure --> src/py_module.rs:43:22 | 43 | .map_err(|e| Into::<Ka3005pError>::into(e))?) | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace the closure with the function itself: `Into::<Ka3005pError>::into` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_closure note: the lint level is defined here --> src/lib.rs:30:9 | 30 | #![deny(warnings)] | ^^^^^^^^ = note: `#[deny(clippy::redundant_closure)]` implied by `#[deny(warnings)]`
}

fn status(&mut self) -> PyResult<Status> {
Ok(self
.inner
.status()
.map_err(|e| Into::<Ka3005pError>::into(e))?)

Check failure on line 50 in src/py_module.rs

GitHub Actions / clippy

redundant closure

error: redundant closure --> src/py_module.rs:50:22 | 50 | .map_err(|e| Into::<Ka3005pError>::into(e))?) | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace the closure with the function itself: `Into::<Ka3005pError>::into` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_closure
}
}

// TODO: Remove unwraps and add proper error handling/conversion
#[pymethods]
impl PowerSupply {
#[new]
fn new(serial_port: Option<&str>) -> PyResult<Self> {
let supply = match serial_port {
Some(port) => PowerSupply {
inner: Ka3005p::new(port).unwrap(),
},
None => PowerSupply {
inner: find_serial_port().unwrap(),
},
};
Ok(supply)
}

fn raw_command(&mut self, command: &str) -> PyResult<Vec<u8>> {
Ok(self
.inner
.run_command(command)
.map_err(|e| Into::<Ka3005pError>::into(e))?)

Check failure on line 74 in src/py_module.rs

GitHub Actions / clippy

redundant closure

error: redundant closure --> src/py_module.rs:74:22 | 74 | .map_err(|e| Into::<Ka3005pError>::into(e))?) | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: replace the closure with the function itself: `Into::<Ka3005pError>::into` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#redundant_closure
}

fn save(&mut self, id: u32) -> PyResult<()> {
let command = Command::Save(id);
self.execute(command)
}

fn load(&mut self, id: u32) -> PyResult<()> {
let command = Command::Load(id);
self.execute(command)
}

#[getter]
fn get_voltage(&mut self) -> PyResult<f32> {
let status = self.status()?;
Ok(status.voltage)
}

#[setter]
fn set_voltage(&mut self, v: f32) -> PyResult<()> {
let command = Command::Voltage(v);
self.execute(command)
}

#[getter]
fn get_current(&mut self) -> PyResult<f32> {
let status = self.status()?;
Ok(status.current)
}

#[setter]
fn set_current(&mut self, i: f32) -> PyResult<()> {
let command = Command::Current(i);
self.execute(command)
}

//#[getter]
//fn get_power(&mut self) -> PyResult<bool> {
// let status = self
// .inner
// .status()
// .map_err(|e| Into::<Ka3005pError>::into(e))?;
// Ok(status.current)
//}

#[setter]
fn set_power(&mut self, enable: bool) -> PyResult<()> {
let command = Command::Power(Switch::from(enable));
self.execute(command)
}

//#[getter]
//fn get_ocp(&mut self) -> PyResult<bool> {}

#[setter]
fn set_ocp(&mut self, enable: bool) -> PyResult<()> {
let command = Command::Ocp(Switch::from(enable));
self.execute(command)
}

//#[getter]
//fn get_ovp(&mut self) -> PyResult<bool> {}

#[setter]
fn set_ovp(&mut self, enable: bool) -> PyResult<()> {
let command = Command::Ovp(Switch::from(enable));
self.execute(command)
}

//#[getter]
//fn get_beep(&mut self) -> PyResult<bool> {
// let status = self.status()?;
// Ok(status.beep.into())
//}

#[setter]
fn set_beep(&mut self, enable: bool) -> PyResult<()> {
let command = Command::Beep(Switch::from(enable));
self.execute(command)
}
}

/// doc
#[pymodule]
fn ka3005p(m: &Bound<'_, PyModule>) -> PyResult<()> {
m.add_function(wrap_pyfunction!(list_power_supplies, m)?)?;
m.add_class::<PowerSupply>()?;
Ok(())
}

0 comments on commit fb313e7

Please sign in to comment.