linux_gpib_rs/
status.rs

1use linux_gpib_sys::{
2    ibsta_bit_numbers_ATN_NUM, ibsta_bit_numbers_CIC_NUM, ibsta_bit_numbers_CMPL_NUM,
3    ibsta_bit_numbers_DCAS_NUM, ibsta_bit_numbers_DTAS_NUM, ibsta_bit_numbers_END_NUM,
4    ibsta_bit_numbers_ERR_NUM, ibsta_bit_numbers_EVENT_NUM, ibsta_bit_numbers_LACS_NUM,
5    ibsta_bit_numbers_LOK_NUM, ibsta_bit_numbers_REM_NUM, ibsta_bit_numbers_RQS_NUM,
6    ibsta_bit_numbers_SPOLL_NUM, ibsta_bit_numbers_SRQI_NUM, ibsta_bit_numbers_TACS_NUM,
7    ibsta_bit_numbers_TIMO_NUM,
8};
9use std::default::Default;
10use std::fmt;
11
12pub struct IbStatus {
13    pub dcas: bool,
14    pub dtas: bool,
15    pub lacs: bool,
16    pub tacs: bool,
17    pub atn: bool,
18    pub cic: bool,
19    pub rem: bool,
20    pub lok: bool,
21    pub cmpl: bool,
22    pub event: bool,
23    pub spoll: bool,
24    pub rqs: bool,
25    pub srqi: bool,
26    pub end: bool,
27    pub timo: bool,
28    pub err: bool,
29}
30
31impl IbStatus {
32    /// Get current value of from Linux-GPIB ibsta global variable
33    pub fn current_status() -> IbStatus {
34        IbStatus::from_ibsta(unsafe { linux_gpib_sys::ibsta })
35    }
36
37    /// Convert c_int status value to IbStatus
38    pub fn from_ibsta(ibsta: i32) -> IbStatus {
39        let dcas = ((1 << ibsta_bit_numbers_DCAS_NUM) & ibsta) != 0;
40        let dtas = ((1 << ibsta_bit_numbers_DTAS_NUM) & ibsta) != 0;
41        let lacs = ((1 << ibsta_bit_numbers_LACS_NUM) & ibsta) != 0;
42        let tacs = ((1 << ibsta_bit_numbers_TACS_NUM) & ibsta) != 0;
43        let atn = ((1 << ibsta_bit_numbers_ATN_NUM) & ibsta) != 0;
44        let cic = ((1 << ibsta_bit_numbers_CIC_NUM) & ibsta) != 0;
45        let rem = ((1 << ibsta_bit_numbers_REM_NUM) & ibsta) != 0;
46        let lok = ((1 << ibsta_bit_numbers_LOK_NUM) & ibsta) != 0;
47        let cmpl = ((1 << ibsta_bit_numbers_CMPL_NUM) & ibsta) != 0;
48        let event = ((1 << ibsta_bit_numbers_EVENT_NUM) & ibsta) != 0;
49        let spoll = ((1 << ibsta_bit_numbers_SPOLL_NUM) & ibsta) != 0;
50        let rqs = ((1 << ibsta_bit_numbers_RQS_NUM) & ibsta) != 0;
51        let srqi = ((1 << ibsta_bit_numbers_SRQI_NUM) & ibsta) != 0;
52        let end = ((1 << ibsta_bit_numbers_END_NUM) & ibsta) != 0;
53        let timo = ((1 << ibsta_bit_numbers_TIMO_NUM) & ibsta) != 0;
54        let err = ((1 << ibsta_bit_numbers_ERR_NUM) & ibsta) != 0;
55        IbStatus {
56            dcas,
57            dtas,
58            lacs,
59            tacs,
60            atn,
61            cic,
62            rem,
63            lok,
64            cmpl,
65            event,
66            spoll,
67            rqs,
68            srqi,
69            end,
70            timo,
71            err,
72        }
73    }
74
75    /// Convert IbStatus to Linux GPIB c_int status
76    pub fn as_ibsta(&self) -> i32 {
77        let mut ibsta = 0;
78        if self.dcas {
79            ibsta = ibsta | (1 << ibsta_bit_numbers_DCAS_NUM);
80        }
81        if self.dtas {
82            ibsta = ibsta | (1 << ibsta_bit_numbers_DTAS_NUM);
83        }
84        if self.lacs {
85            ibsta = ibsta | (1 << ibsta_bit_numbers_LACS_NUM);
86        }
87        if self.tacs {
88            ibsta = ibsta | (1 << ibsta_bit_numbers_TACS_NUM);
89        }
90        if self.atn {
91            ibsta = ibsta | (1 << ibsta_bit_numbers_ATN_NUM);
92        }
93        if self.cic {
94            ibsta = ibsta | (1 << ibsta_bit_numbers_CIC_NUM);
95        }
96        if self.rem {
97            ibsta = ibsta | (1 << ibsta_bit_numbers_REM_NUM);
98        }
99        if self.lok {
100            ibsta = ibsta | (1 << ibsta_bit_numbers_LOK_NUM);
101        }
102        if self.cmpl {
103            ibsta = ibsta | (1 << ibsta_bit_numbers_CMPL_NUM);
104        }
105        if self.event {
106            ibsta = ibsta | (1 << ibsta_bit_numbers_EVENT_NUM);
107        }
108        if self.spoll {
109            ibsta = ibsta | (1 << ibsta_bit_numbers_SPOLL_NUM);
110        }
111        if self.rqs {
112            ibsta = ibsta | (1 << ibsta_bit_numbers_RQS_NUM);
113        }
114        if self.srqi {
115            ibsta = ibsta | (1 << ibsta_bit_numbers_SRQI_NUM);
116        }
117        if self.end {
118            ibsta = ibsta | (1 << ibsta_bit_numbers_END_NUM);
119        }
120        if self.timo {
121            ibsta = ibsta | (1 << ibsta_bit_numbers_TIMO_NUM);
122        }
123        if self.err {
124            ibsta = ibsta | (1 << ibsta_bit_numbers_ERR_NUM);
125        }
126        ibsta
127    }
128
129    pub fn with_dcas(mut self, dcas: bool) -> Self {
130        self.dcas = dcas;
131        self
132    }
133    pub fn with_dtas(mut self, dtas: bool) -> Self {
134        self.dtas = dtas;
135        self
136    }
137    pub fn with_lacs(mut self, lacs: bool) -> Self {
138        self.lacs = lacs;
139        self
140    }
141    pub fn with_tacs(mut self, tacs: bool) -> Self {
142        self.tacs = tacs;
143        self
144    }
145    pub fn with_atn(mut self, atn: bool) -> Self {
146        self.atn = atn;
147        self
148    }
149    pub fn with_cic(mut self, cic: bool) -> Self {
150        self.cic = cic;
151        self
152    }
153    pub fn with_rem(mut self, rem: bool) -> Self {
154        self.rem = rem;
155        self
156    }
157    pub fn with_lok(mut self, lok: bool) -> Self {
158        self.lok = lok;
159        self
160    }
161    pub fn with_cmpl(mut self, cmpl: bool) -> Self {
162        self.cmpl = cmpl;
163        self
164    }
165    pub fn with_event(mut self, event: bool) -> Self {
166        self.event = event;
167        self
168    }
169    pub fn with_spoll(mut self, spoll: bool) -> Self {
170        self.spoll = spoll;
171        self
172    }
173    pub fn with_rqs(mut self, rqs: bool) -> Self {
174        self.rqs = rqs;
175        self
176    }
177    pub fn with_srqi(mut self, srqi: bool) -> Self {
178        self.srqi = srqi;
179        self
180    }
181    pub fn with_end(mut self, end: bool) -> Self {
182        self.end = end;
183        self
184    }
185    pub fn with_timo(mut self, timo: bool) -> Self {
186        self.timo = timo;
187        self
188    }
189    pub fn with_err(mut self, err: bool) -> Self {
190        self.err = err;
191        self
192    }
193}
194
195impl fmt::Debug for IbStatus {
196    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
197        let mut description = String::new();
198        if self.dcas {
199            description.push_str("DCAS (device clear) ");
200        }
201        if self.dtas {
202            description.push_str("DTAS (device trigger) ");
203        }
204        if self.lacs {
205            description.push_str("LACS (board is currently addressed as a listener) ");
206        }
207        if self.tacs {
208            description.push_str("TACS (board is currently addressed as a talker) ");
209        }
210        if self.atn {
211            description.push_str("ATN (ATN line is asserted) ");
212        }
213        if self.cic {
214            description.push_str("CIC (board is controller-in-charge, able to set the ATN line) ");
215        }
216        if self.rem {
217            description.push_str("REM (board is in 'remote' state) ");
218        }
219        if self.lok {
220            description.push_str("LOK (board is in 'lockout' state) ");
221        }
222        if self.cmpl {
223            description.push_str("CMPL (I/O operation complete) ");
224        }
225        if self.event {
226            description
227                .push_str("EVENT (one or more clear, trigger, or interface clear event received) ");
228        }
229        if self.spoll {
230            description.push_str("SPOLL (board is serial polled) ");
231        }
232        if self.rqs {
233            description.push_str("RQS (device has requested service) ");
234        }
235        if self.srqi {
236            description
237                .push_str("SRQI (a device connected to the board is asserting the SRQ line) ");
238        }
239        if self.end {
240            description.push_str("END (last I/O operation ended with the EOI line asserted) ");
241        }
242        if self.timo {
243            description.push_str("TIMO (last I/O operation, or ibwait, timed out) ");
244        }
245        if self.err {
246            description.push_str("ERR (last function call failed)");
247        }
248        if description.len() > 0 {
249            write!(f, "IbStatus({description})")
250        } else {
251            write!(f, "IbStatus(No flag set)")
252        }
253    }
254}
255
256impl fmt::Display for IbStatus {
257    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
258        let mut description = String::new();
259        if self.dcas {
260            description.push_str("DCAS ");
261        }
262        if self.dtas {
263            description.push_str("DTAS ");
264        }
265        if self.lacs {
266            description.push_str("LACS ");
267        }
268        if self.tacs {
269            description.push_str("TACS ");
270        }
271        if self.atn {
272            description.push_str("ATN ");
273        }
274        if self.cic {
275            description.push_str("CIC ");
276        }
277        if self.rem {
278            description.push_str("REM ");
279        }
280        if self.lok {
281            description.push_str("LOK ");
282        }
283        if self.cmpl {
284            description.push_str("CMPL ");
285        }
286        if self.event {
287            description.push_str("EVENT ");
288        }
289        if self.spoll {
290            description.push_str("SPOLL ");
291        }
292        if self.rqs {
293            description.push_str("RQS ");
294        }
295        if self.srqi {
296            description.push_str("SRQI ");
297        }
298        if self.end {
299            description.push_str("END ");
300        }
301        if self.timo {
302            description.push_str("TIMO ");
303        }
304        if self.err {
305            description.push_str("ERR");
306        }
307        if description.len() > 0 {
308            write!(f, "IbStatus({description})")
309        } else {
310            write!(f, "IbStatus(No flag set)")
311        }
312    }
313}
314
315impl Default for IbStatus {
316    fn default() -> Self {
317        Self {
318            dcas: false,
319            dtas: false,
320            lacs: false,
321            tacs: false,
322            atn: false,
323            cic: false,
324            rem: false,
325            lok: false,
326            cmpl: false,
327            event: false,
328            spoll: false,
329            rqs: false,
330            srqi: false,
331            end: false,
332            timo: false,
333            err: false,
334        }
335    }
336}