go_pybindings/
liftof_dataclasses.rs1use pyo3::prelude::*;
2use pyo3::exceptions::PyValueError;
3
4extern crate pyo3_log;
5
6use tof_dataclasses as api;
7
8#[pyclass]
9#[pyo3(name = "IPBus")]
10pub struct PyIPBus {
11 ipbus : api::ipbus::IPBus,
12}
13
14#[pymethods]
15impl PyIPBus {
16 #[new]
17 fn new(target_address : &str) -> Self {
18 let ipbus = api::ipbus::IPBus::new(target_address).expect("Unable to connect to {target_address}");
19 Self {
20 ipbus : ipbus,
21 }
22 }
23
24 pub fn get_status(&mut self) -> PyResult<()> {
26 match self.ipbus.get_status() {
27 Ok(_) => {
28 return Ok(());
29 },
30 Err(err) => {
31 return Err(PyValueError::new_err(err.to_string()));
32 }
33 }
34 }
35
36 pub fn get_buffer(&self) -> [u8;api::ipbus::MT_MAX_PACKSIZE] {
37 return self.ipbus.buffer.clone();
38 }
39
40 pub fn set_packet_id(&mut self, pid : u16) {
41 self.ipbus.pid = pid;
42 }
43
44 pub fn get_packet_id(&self) -> u16 {
45 self.ipbus.pid
46 }
47
48 pub fn get_expected_packet_id(&self) -> u16 {
49 self.ipbus.expected_pid
50 }
51
52 pub fn realign_packet_id(&mut self) -> PyResult<()> {
54 match self.ipbus.realign_packet_id() {
55 Ok(_) => {
56 return Ok(());
57 },
58 Err(err) => {
59 return Err(PyValueError::new_err(err.to_string()));
60 }
61 }
62 }
63
64 pub fn get_target_next_expected_packet_id(&mut self)
66 -> PyResult<u16> {
67 match self.ipbus.get_target_next_expected_packet_id() {
68 Ok(result) => {
69 return Ok(result);
70 },
71 Err(err) => {
72 return Err(PyValueError::new_err(err.to_string()));
73 }
74 }
75 }
76
77 pub fn read_multiple(&mut self,
78 addr : u32,
79 nwords : usize,
80 increment_addr : bool)
81 -> PyResult<Vec<u32>> {
82
83 match self.ipbus.read_multiple(addr,
84 nwords,
85 increment_addr) {
86 Ok(result) => {
87 return Ok(result);
88 },
89 Err(err) => {
90 return Err(PyValueError::new_err(err.to_string()));
91 }
92 }
93 }
94
95 pub fn write(&mut self,
96 addr : u32,
97 data : u32)
98 -> PyResult<()> {
99
100 match self.ipbus.write(addr, data) {
101 Ok(_) => Ok(()),
102 Err(err) => {
103 return Err(PyValueError::new_err(err.to_string()));
104 }
105 }
106 }
107
108
109 pub fn read(&mut self, addr : u32)
110 -> PyResult<u32> {
111 match self.ipbus.read(addr) {
112 Ok(result) => {
113 return Ok(result);
114 },
115 Err(err) => {
116 return Err(PyValueError::new_err(err.to_string()));
117 }
118 }
119 }
120}
121