pub struct DMXSerial { /* private fields */ }Expand description
A DMX-Interface which writes to the SerialPort independently from the main thread.
It uses the RS-485 standard (aka. Open DMX) to send DMX data over a SerialPort.
Implementations§
Source§impl DMXSerial
impl DMXSerial
Sourcepub fn open(port: &str) -> Result<DMXSerial, Error>
pub fn open(port: &str) -> Result<DMXSerial, Error>
Opens a new DMX-Interface on the given path. Returns an [DMXError] if the port could not be opened.
The path should look something like this:
- Windows:
COM3 - Linux:
/dev/ttyUSB0
The interface can be set to synchronous or asynchronous mode (default).
In synchronous mode, no data will be sent to the SerialPort unti DMXSerial::update() is called.
If updates are not sent regularly in synchronous mode, DMX-Devices might not react to the changes.
In asynchronous mode, the data will be polled automatically to the SerialPort.
§Example
Basic usage:
use open_dmx::DMXSerial;
fn main() {
let mut dmx = DMXSerial::open("COM3").unwrap();
dmx.set_channels([255; 512]);
dmx.set_channel(1, 0).unwrap();
}Examples found in repository?
More examples
3fn main() {
4 let mut dmx = DMXSerial::open("COM3").unwrap();
5 dmx.set_sync();
6 loop {
7 let _ = strobe(&mut dmx);
8 println!("Device has been disconnected! Reopening...");
9 match dmx.reopen() {
10 Ok(_) => println!("Device has been reopened!"),
11 Err(e) => {
12 println!("Error reopening device: {}", e);
13 println!("Waiting 1 second before trying again...");
14 std::thread::sleep(std::time::Duration::from_secs(1));
15 },
16 }
17 }
18}Sourcepub fn open_sync(port: &str) -> Result<DMXSerial, Error>
pub fn open_sync(port: &str) -> Result<DMXSerial, Error>
Does the same as DMXSerial::open but sets the DMXSerial to sync mode.
§Example
Basic strobe effect:
use open_dmx::DMXSerial;
fn main() {
let mut dmx = DMXSerial::open_sync("COM3").unwrap();
//strobe
loop {
dmx.set_channels([255; 512]);
dmx.update(); //returns once the data is sent
dmx.set_channels([0; 512]);
dmx.update();
}
}Sourcepub fn reopen(&mut self) -> Result<(), Error>
pub fn reopen(&mut self) -> Result<(), Error>
Examples found in repository?
3fn main() {
4 let mut dmx = DMXSerial::open("COM3").unwrap();
5 dmx.set_sync();
6 loop {
7 let _ = strobe(&mut dmx);
8 println!("Device has been disconnected! Reopening...");
9 match dmx.reopen() {
10 Ok(_) => println!("Device has been reopened!"),
11 Err(e) => {
12 println!("Error reopening device: {}", e);
13 println!("Waiting 1 second before trying again...");
14 std::thread::sleep(std::time::Duration::from_secs(1));
15 },
16 }
17 }
18}Sourcepub fn set_channel(
&mut self,
channel: usize,
value: u8,
) -> Result<(), DMXChannelValidityError>
pub fn set_channel( &mut self, channel: usize, value: u8, ) -> Result<(), DMXChannelValidityError>
Sourcepub fn set_channels(&mut self, channels: [u8; 512])
pub fn set_channels(&mut self, channels: [u8; 512])
Sets all channels to the given value via a array of size DMX_CHANNELS.
§Example
Checkerboard effect:
let mut dmx = DMXSerial::open("COM3").unwrap();
let mut channels = [0; DMX_CHANNELS];
channels.iter_mut().enumerate().for_each(|(i, value)| *value = if i % 2 == 0 { 255 } else { 0 });
dmx.set_channels(channels);Examples found in repository?
More examples
Sourcepub fn get_channel(&self, channel: usize) -> Result<u8, DMXChannelValidityError>
pub fn get_channel(&self, channel: usize) -> Result<u8, DMXChannelValidityError>
Tries to get the value of the specified channel.
Returns [DMXError::NotValid] if the given channel is not in the range of DMX_CHANNELS.
§Example
Basic usage:
dmx.set_channel(1, 255).unwrap();
assert_eq!(dmx.get_channel(1).unwrap(), 255);Sourcepub fn get_channels(&self) -> [u8; 512]
pub fn get_channels(&self) -> [u8; 512]
Returns the value of all channels via a array of size DMX_CHANNELS.
§Example
Basic usage:
dmx.set_channels([255; DMX_CHANNELS]).unwrap();
assert_eq!(dmx.get_channels(), [255; DMX_CHANNELS]);Sourcepub fn reset_channels(&mut self)
pub fn reset_channels(&mut self)
Resets all channels to 0.
§Example
Basic usage:
dmx.set_channels([255; DMX_CHANNELS]).unwrap();
assert_eq!(dmx.get_channels(), [255; DMX_CHANNELS]);
dmx.reset_channels();
assert_eq!(dmx.get_channels(), [0; DMX_CHANNELS]);Sourcepub fn update(&mut self) -> Result<(), DMXDisconnectionError>
pub fn update(&mut self) -> Result<(), DMXDisconnectionError>
Updates the DMX data.
Returns after the data has been sent.
Works both in sync and async mode.
§Example
Examples found in repository?
More examples
Sourcepub fn update_async(&self) -> Result<(), DMXDisconnectionError>
pub fn update_async(&self) -> Result<(), DMXDisconnectionError>
Updates the DMX data but returns immediately.
Useless in async mode.
Sourcepub fn set_sync(&mut self)
pub fn set_sync(&mut self)
Sets the DMX mode to sync.
Examples found in repository?
More examples
3fn main() {
4 let mut dmx = DMXSerial::open("COM3").unwrap();
5 dmx.set_sync();
6 loop {
7 let _ = strobe(&mut dmx);
8 println!("Device has been disconnected! Reopening...");
9 match dmx.reopen() {
10 Ok(_) => println!("Device has been reopened!"),
11 Err(e) => {
12 println!("Error reopening device: {}", e);
13 println!("Waiting 1 second before trying again...");
14 std::thread::sleep(std::time::Duration::from_secs(1));
15 },
16 }
17 }
18}Sourcepub fn set_packet_time(&mut self, time: Duration)
pub fn set_packet_time(&mut self, time: Duration)
Sets the minimum Duration between two DMX packets.
§Default
- 22.7 ms
Some devices may require a longer time between two packets.
See the DMX512-Standard for timing.
Sourcepub fn get_packet_time(&self) -> Duration
pub fn get_packet_time(&self) -> Duration
Returns the minimum Duration between two DMX packets.