1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
// phidget-rs/src/lib.rs
//
// Copyright (c) 2023, Frank Pagliughi
//
// This file is part of the 'phidget-rs' library.
//
// Licensed under the MIT license:
// <LICENSE or http://opensource.org/licenses/MIT>
// This file may not be copied, modified, or distributed except according
// to those terms.
//
//! Safe Rust bindings to the phidget22 library.
//!
//! # Basic usage
//!
//! This example shows how to access a simple Digital Input, connected to the first available channel of a Vint HUB.
//! See the `examples` directory for more thorough code snippets.
//! ```rust,no_run
//! use phidget::{DigitalOutput, Phidget};
//! # use std::time::Duration;
//!
//! // Create a handle to a Digital Output device
//! let mut out = DigitalOutput::new();
//! // Before opening the device, set its VINT hub port
//! out.set_is_hub_port_device(true).unwrap();
//! out.set_hub_port(0).unwrap();
//!
//! // Start connection. Make sure to handle the result
//! // to check the device is available
//! out.open_wait_default().unwrap();
//!
//! // Control the output device
//! loop {
//! println!("Turn on LED");
//! out.set_state(1).unwrap();
//! std::thread::sleep(Duration::from_secs(3));
//!
//! println!("Turn off LED");
//! out.set_state(0).unwrap();
//! std::thread::sleep(Duration::from_secs(3));
//! }
//! ```
//!
//! # Callbacks
//! In order to activate an output phidget device depending on the state of other sensors,
//! for instance by turning on an LED whenever another sensor detects something,
//! you need to set a callback listening for sensor value changes, and keep a valid handle to the output device to set its state.
//!
//! The problem is, Phidget callbacks do run in a different thread. A Phidget handle can already be sent
//! to a different thread, as it implements [Send], but it doesn't implement [Sync].
//! Hence, if you desire to access the same handle from different callbacks, it has to be wrapped in a
//! Sync container, such as a [Mutex](std::sync::Mutex).
//!
//! ```rust,no_run
//! # use phidget::{Phidget, DigitalOutput, DigitalInput};
//! # use std::sync::Mutex;
//! # fn main()
//! # {
//! # // Open a digitalInput to detect a button
//! let mut button = DigitalInput::new();
//! # button.set_channel(0).unwrap();
//! // Open the digital output where
//! // a LED is connected to.
//! // In this example, it is initialized
//! // and wrapped in a Mutex
//! let led = Mutex::new({
//! let mut tmp = DigitalOutput::new();
//! tmp.set_channel(1).unwrap();
//! tmp.open_wait_default().unwrap();
//! tmp
//! });
//!
//! // Make the button alternate the LED state
//! button.set_on_state_change_handler(move |_, s: u8| {
//! let lock = led.lock().unwrap();
//! match s {
//! // Access the device inside the Mutex and change its state
//! 0 => lock.set_state(0).unwrap(),
//! _ => lock.set_state(1).unwrap()
//! }
//! }).unwrap();
//! # }
//! ```
// Platform dependent, whether this is necessary
// Lints
use ;
pub use ;
/// The error types for the crate
pub use crate*;
/// The enumerated types for the crate
pub use crate*;
/// The main Phidget trait
pub use crate;
/// Network API
pub use crateServerType;
/// Module containing all implemented devices
pub use crate;
pub use crate;
/// An infinite timeout (wait forever)
pub const TIMEOUT_INFINITE: Duration = from_millis;
/// The default timeout for the library
pub const TIMEOUT_DEFAULT: Duration = from_millis;
/////////////////////////////////////////////////////////////////////////////
/// Gets a string from a phidget22 call.
/// This can be any function that takes a pointer to a c-str as the lone
/// argument.
pub
/// Release the memory held in a double-boxed callback function/lambda.
pub Sized>
/////////////////////////////////////////////////////////////////////////////
/// The the full version of the phidget22 library as a string.
/// This is something like, "Phidget22 - Version 1.14 - Built Mar 31 2023 22:44:59"
/// Gets just the version number of the phidget22 library as a string.
/// This is something like, "1.14"
/// Closes all channels, and stops all threads. The library is reset to a
/// newly loaded state. All channel handles have been freed.
///
/// This function is intended for use in special cases where the library
/// cannot be unloaded between program runs, such as LabVIEW and Unity
/// Editor.
///
/// # Safety
///
/// This invalidates all Phidget objects that are running.
///
pub unsafe
/////////////////////////////////////////////////////////////////////////////