-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
7 changed files
with
418 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
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 | ||
|
Oops, something went wrong.
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 @@ | ||
[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.
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,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 / clippyredundant closure
|
||
} | ||
|
||
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 / clippyredundant 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 / clippyredundant 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(()) | ||
} |