Skip to content

Commit f1864dc

Browse files
Update servo_macro.rs
1 parent 81b8193 commit f1864dc

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed

src/servo/servo_macro.rs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ macro_rules! generate_servo {
3838
use pyo3::prelude::*;
3939

4040
$crate::generate_protocol_constructor!($servo_name, $protocol);
41+
$crate::generate_addr_read_write!($servo_name);
4142

4243
$(
4344
$crate::generate_reg_access!($servo_name, $reg_name, $reg_access, $reg_addr, $reg_type, $conv);
@@ -111,6 +112,71 @@ macro_rules! generate_protocol_constructor {
111112
};
112113
}
113114

115+
#[macro_export]
116+
macro_rules! generate_addr_read_write {
117+
($servo_name:ident) => {
118+
paste::paste! {
119+
impl [<$servo_name:camel Controller>] {
120+
pub fn read_raw_data(
121+
&mut self,
122+
ids: &[u8],
123+
addr: u8,
124+
length: u8,
125+
) -> $crate::Result<Vec<Vec<u8>>> {
126+
let dph = self.dph.as_ref().unwrap();
127+
let serial_port = self.serial_port.as_mut().unwrap().as_mut();
128+
dph.sync_read(serial_port, ids, addr, length)
129+
}
130+
131+
pub fn write_raw_data(
132+
&mut self,
133+
ids: &[u8],
134+
addr: u8,
135+
data: &[Vec<u8>],
136+
) -> $crate::Result<()> {
137+
let dph = self.dph.as_ref().unwrap();
138+
let serial_port = self.serial_port.as_mut().unwrap().as_mut();
139+
dph.sync_write(serial_port, ids, addr, data)
140+
}
141+
}
142+
143+
#[cfg(feature = "python")]
144+
#[pymethods]
145+
impl [<$servo_name:camel SyncController>] {
146+
pub fn read_raw_data(
147+
&self,
148+
py: Python,
149+
ids: &Bound<'_, pyo3::types::PyList>,
150+
addr: u8,
151+
length: u8,
152+
) -> PyResult<PyObject> {
153+
let ids = ids.extract::<Vec<u8>>()?;
154+
155+
let x = self.0.lock().unwrap().read_raw_data(&ids, addr, length)
156+
.map_err(|e| pyo3::exceptions::PyRuntimeError::new_err(e.to_string()))?;
157+
let l = pyo3::types::PyList::new(py, x.clone())?;
158+
159+
Ok(l.into())
160+
}
161+
162+
pub fn write_raw_data(
163+
&self,
164+
ids: &Bound<'_, pyo3::types::PyList>,
165+
addr: u8,
166+
data: &Bound<'_, pyo3::types::PyList>,
167+
) -> PyResult<()> {
168+
let ids = ids.extract::<Vec<u8>>()?;
169+
let data = data.extract::<Vec<Vec<u8>>>()?;
170+
171+
self.0.lock().unwrap().write_raw_data(&ids, addr, &data)
172+
.map_err(|e| pyo3::exceptions::PyRuntimeError::new_err(e.to_string()))?;
173+
Ok(())
174+
}
175+
}
176+
}
177+
};
178+
}
179+
114180
#[macro_export]
115181
macro_rules! generate_reg_access {
116182
($servo_name:ident, $reg_name:ident, r, $reg_addr:expr, $reg_type:ty, $conv:ident) => {

0 commit comments

Comments
 (0)