DMXSerial

Struct DMXSerial 

Source
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

Source

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?
examples/basic.rs (line 4)
3fn main() {
4    let mut dmx = DMXSerial::open("COM3").unwrap();
5    dmx.set_channels([255; 512]);
6    dmx.set_channel(1, 0).unwrap();
7}
More examples
Hide additional examples
examples/checkerboard.rs (line 3)
2fn main() {
3    let mut dmx = DMXSerial::open("COM3").unwrap();
4    let mut channels = [0; DMX_CHANNELS];
5    channels.iter_mut().enumerate().for_each(|(i, value)| *value = if i % 2 == 0 { 255 } else { 0 });
6    dmx.set_channels(channels);
7}
examples/sync_strobe.rs (line 4)
3fn main() -> Result<(), Box<dyn std::error::Error>>{
4    let mut dmx = DMXSerial::open("COM3").unwrap();
5    dmx.set_sync();
6    //strobe
7    loop {
8        dmx.set_channels([255; 512]);
9        dmx.update()?;
10        dmx.set_channels([0; 512]);
11        dmx.update()?;
12    }
13}
examples/reopen.rs (line 4)
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}
Source

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();
    }
}
Source

pub fn reopen(&mut self) -> Result<(), Error>

Reopens the DMXSerial on the same path.

It keeps the current channel values.

Examples found in repository?
examples/reopen.rs (line 9)
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}
Source

pub fn name(&self) -> &str

Gets the name of the Path on which the DMXSerial is opened.

§Example

Basic usage:

let mut dmx = DMXSerial::open("COM3").unwrap();
assert_eq!(dmx.name(), "COM3");
Source

pub fn set_channel( &mut self, channel: usize, value: u8, ) -> Result<(), DMXChannelValidityError>

Sets the specified channel to the given value.

§Example

Basic usage:

dmx.set_channel(1, 255); //sets the first channel to 255
Examples found in repository?
examples/basic.rs (line 6)
3fn main() {
4    let mut dmx = DMXSerial::open("COM3").unwrap();
5    dmx.set_channels([255; 512]);
6    dmx.set_channel(1, 0).unwrap();
7}
Source

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?
examples/basic.rs (line 5)
3fn main() {
4    let mut dmx = DMXSerial::open("COM3").unwrap();
5    dmx.set_channels([255; 512]);
6    dmx.set_channel(1, 0).unwrap();
7}
More examples
Hide additional examples
examples/checkerboard.rs (line 6)
2fn main() {
3    let mut dmx = DMXSerial::open("COM3").unwrap();
4    let mut channels = [0; DMX_CHANNELS];
5    channels.iter_mut().enumerate().for_each(|(i, value)| *value = if i % 2 == 0 { 255 } else { 0 });
6    dmx.set_channels(channels);
7}
examples/reopen.rs (line 23)
20fn strobe(dmx: &mut DMXSerial) -> Result<(), Box<dyn std::error::Error>>{
21    println!("Sending strobe packets...");
22    loop {
23        dmx.set_channels([255; 512]);
24        dmx.update()?;
25        dmx.set_channels([0; 512]);
26        dmx.update()?;
27    }
28}
examples/sync_strobe.rs (line 8)
3fn main() -> Result<(), Box<dyn std::error::Error>>{
4    let mut dmx = DMXSerial::open("COM3").unwrap();
5    dmx.set_sync();
6    //strobe
7    loop {
8        dmx.set_channels([255; 512]);
9        dmx.update()?;
10        dmx.set_channels([0; 512]);
11        dmx.update()?;
12    }
13}
Source

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);
Source

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]);
Source

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]);
Source

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

Basic Usage

Examples found in repository?
examples/reopen.rs (line 24)
20fn strobe(dmx: &mut DMXSerial) -> Result<(), Box<dyn std::error::Error>>{
21    println!("Sending strobe packets...");
22    loop {
23        dmx.set_channels([255; 512]);
24        dmx.update()?;
25        dmx.set_channels([0; 512]);
26        dmx.update()?;
27    }
28}
More examples
Hide additional examples
examples/sync_strobe.rs (line 9)
3fn main() -> Result<(), Box<dyn std::error::Error>>{
4    let mut dmx = DMXSerial::open("COM3").unwrap();
5    dmx.set_sync();
6    //strobe
7    loop {
8        dmx.set_channels([255; 512]);
9        dmx.update()?;
10        dmx.set_channels([0; 512]);
11        dmx.update()?;
12    }
13}
Source

pub fn update_async(&self) -> Result<(), DMXDisconnectionError>

Updates the DMX data but returns immediately.

Useless in async mode.

Source

pub fn set_sync(&mut self)

Sets the DMX mode to sync.

Examples found in repository?
examples/sync_strobe.rs (line 5)
3fn main() -> Result<(), Box<dyn std::error::Error>>{
4    let mut dmx = DMXSerial::open("COM3").unwrap();
5    dmx.set_sync();
6    //strobe
7    loop {
8        dmx.set_channels([255; 512]);
9        dmx.update()?;
10        dmx.set_channels([0; 512]);
11        dmx.update()?;
12    }
13}
More examples
Hide additional examples
examples/reopen.rs (line 5)
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}
Source

pub fn set_async(&mut self)

Sets the DMX mode to async.

Source

pub fn is_sync(&self) -> bool

Returns true if the DMX mode is sync.

Source

pub fn is_async(&self) -> bool

Returns true if the DMX mode is async.

Source

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.

Source

pub fn get_packet_time(&self) -> Duration

Returns the minimum Duration between two DMX packets.

Source

pub fn check_agent(&self) -> Result<(), DMXDisconnectionError>

Checks if the DMXSerial device is still connected.

§Example

Basic usage:

assert!(dmx.check_agent().is_ok()); // If not, the device got disconnected

Trait Implementations§

Source§

impl Debug for DMXSerial

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.