pub struct Stream { /* private fields */ }Expand description
A bi-directional communication stream between the user and a Dac.
Implementations§
Source§impl Stream
impl Stream
Sourcepub fn dac(&self) -> &Addressed
pub fn dac(&self) -> &Addressed
Borrow the inner DAC to examine its state.
Examples found in repository?
5fn main() {
6 println!("Listening for an Ether Dream DAC...");
7
8 let (dac_broadcast, source_addr) = ether_dream::recv_dac_broadcasts()
9 .expect("failed to bind to UDP socket")
10 .filter_map(Result::ok)
11 .next()
12 .unwrap();
13 let mac_address = dac::MacAddress(dac_broadcast.mac_address);
14
15 println!(
16 "Discovered DAC \"{}\" at \"{}\"! Connecting...",
17 mac_address, source_addr
18 );
19
20 // Establish the TCP connection.
21 let mut stream = dac::stream::connect(&dac_broadcast, source_addr.ip().clone()).unwrap();
22
23 // If we want to create an animation (in our case a moving sine wave) we need a frame rate.
24 let frames_per_second = 60.0;
25 // Lets use the DAC at an eighth the maximum scan rate.
26 let points_per_second = stream.dac().max_point_rate / 32;
27 // Determine the number of points per frame given our target frame and point rates.
28 let points_per_frame = (points_per_second as f32 / frames_per_second) as u16;
29
30 println!(
31 "Preparing for playback:\n\tframe_hz: {}\n\tpoint_hz: {}\n\tpoints_per_frame: {}\n",
32 frames_per_second, points_per_second, points_per_frame
33 );
34
35 // Prepare the DAC's playback engine and await the repsonse.
36 stream
37 .queue_commands()
38 .prepare_stream()
39 .submit()
40 .err()
41 .map(|err| {
42 eprintln!(
43 "err occurred when submitting PREPARE_STREAM \
44 command and listening for response: {}",
45 err
46 );
47 });
48
49 println!("Beginning playback!");
50
51 // The sine wave used to generate points.
52 let mut sine_wave = SineWave {
53 point: 0,
54 points_per_frame,
55 frames_per_second,
56 };
57
58 // Queue the initial frame and tell the DAC to begin producing output.
59 let n_points = points_to_generate(stream.dac());
60 stream
61 .queue_commands()
62 .data(sine_wave.by_ref().take(n_points))
63 .begin(0, points_per_second)
64 .submit()
65 .err()
66 .map(|err| {
67 eprintln!(
68 "err occurred when submitting initial DATA and BEGIN \
69 commands and listening for response: {}",
70 err
71 );
72 });
73
74 // Loop and continue to send points forever.
75 loop {
76 // Determine how many points the DAC can currently receive.
77 let n_points = points_to_generate(stream.dac());
78 if let Err(err) = stream
79 .queue_commands()
80 .data(sine_wave.by_ref().take(n_points))
81 .submit()
82 {
83 eprintln!(
84 "err occurred when submitting DATA command and listening \
85 for response: {}",
86 err
87 );
88 break;
89 }
90 }
91
92 // Tell the DAC to stop producing output and return to idle. Wait for the response.
93 //
94 // Note that the DAC is commanded to stop on `Drop` if this is not called and any errors
95 // produced are ignored.
96 stream
97 .queue_commands()
98 .stop()
99 .submit()
100 .expect("err occurred when submitting STOP command and listening for response");
101}Sourcepub fn queue_commands(&mut self) -> CommandQueue<'_>
pub fn queue_commands(&mut self) -> CommandQueue<'_>
Queue one or more commands to be submitted to the DAC at once.
Examples found in repository?
5fn main() {
6 println!("Listening for an Ether Dream DAC...");
7
8 let (dac_broadcast, source_addr) = ether_dream::recv_dac_broadcasts()
9 .expect("failed to bind to UDP socket")
10 .filter_map(Result::ok)
11 .next()
12 .unwrap();
13 let mac_address = dac::MacAddress(dac_broadcast.mac_address);
14
15 println!(
16 "Discovered DAC \"{}\" at \"{}\"! Connecting...",
17 mac_address, source_addr
18 );
19
20 // Establish the TCP connection.
21 let mut stream = dac::stream::connect(&dac_broadcast, source_addr.ip().clone()).unwrap();
22
23 // If we want to create an animation (in our case a moving sine wave) we need a frame rate.
24 let frames_per_second = 60.0;
25 // Lets use the DAC at an eighth the maximum scan rate.
26 let points_per_second = stream.dac().max_point_rate / 32;
27 // Determine the number of points per frame given our target frame and point rates.
28 let points_per_frame = (points_per_second as f32 / frames_per_second) as u16;
29
30 println!(
31 "Preparing for playback:\n\tframe_hz: {}\n\tpoint_hz: {}\n\tpoints_per_frame: {}\n",
32 frames_per_second, points_per_second, points_per_frame
33 );
34
35 // Prepare the DAC's playback engine and await the repsonse.
36 stream
37 .queue_commands()
38 .prepare_stream()
39 .submit()
40 .err()
41 .map(|err| {
42 eprintln!(
43 "err occurred when submitting PREPARE_STREAM \
44 command and listening for response: {}",
45 err
46 );
47 });
48
49 println!("Beginning playback!");
50
51 // The sine wave used to generate points.
52 let mut sine_wave = SineWave {
53 point: 0,
54 points_per_frame,
55 frames_per_second,
56 };
57
58 // Queue the initial frame and tell the DAC to begin producing output.
59 let n_points = points_to_generate(stream.dac());
60 stream
61 .queue_commands()
62 .data(sine_wave.by_ref().take(n_points))
63 .begin(0, points_per_second)
64 .submit()
65 .err()
66 .map(|err| {
67 eprintln!(
68 "err occurred when submitting initial DATA and BEGIN \
69 commands and listening for response: {}",
70 err
71 );
72 });
73
74 // Loop and continue to send points forever.
75 loop {
76 // Determine how many points the DAC can currently receive.
77 let n_points = points_to_generate(stream.dac());
78 if let Err(err) = stream
79 .queue_commands()
80 .data(sine_wave.by_ref().take(n_points))
81 .submit()
82 {
83 eprintln!(
84 "err occurred when submitting DATA command and listening \
85 for response: {}",
86 err
87 );
88 break;
89 }
90 }
91
92 // Tell the DAC to stop producing output and return to idle. Wait for the response.
93 //
94 // Note that the DAC is commanded to stop on `Drop` if this is not called and any errors
95 // produced are ignored.
96 stream
97 .queue_commands()
98 .stop()
99 .submit()
100 .expect("err occurred when submitting STOP command and listening for response");
101}Sourcepub fn set_nodelay(&self, b: bool) -> Result<()>
pub fn set_nodelay(&self, b: bool) -> Result<()>
This directly calls set_nodelay on the inner TcpStream. In other words, this sets the
value of the TCP_NODELAY option for this socket.
Note that due to the necessity for very low-latency communication with the DAC, this API enables TCP_NODELAY by default. This method is exposed in order to allow the user to disable this if they wish.
When not set, data is buffered until there is a sufficient amount to send out, thereby avoiding the frequent sending of small packets. Although perhaps more efficient for the network, this may result in DAC underflows if Data commands are delayed for too long.
Sourcepub fn nodelay(&self) -> Result<bool>
pub fn nodelay(&self) -> Result<bool>
Gets the value of the TCP_NODELAY option for this socket.
For more infnormation about this option, see set_nodelay.
Sourcepub fn set_ttl(&self, ttl: u32) -> Result<()>
pub fn set_ttl(&self, ttl: u32) -> Result<()>
This directly calls set_ttl on the inner TcpStream. In other words, this sets the
value for the IP_TTL option on this socket.
This value sets the time-to-live field that is used in every packet sent from this socket. Time-to-live describes the number of hops between devices that a packet may make before it is discarded/ignored.
Sourcepub fn ttl(&self) -> Result<u32>
pub fn ttl(&self) -> Result<u32>
Gets the value of the IP_TTL option for this socket.
For more information about this option see set_ttl.
Sourcepub fn set_read_timeout(&self, duration: Option<Duration>) -> Result<()>
pub fn set_read_timeout(&self, duration: Option<Duration>) -> Result<()>
Sets the read timeout of the underlying TcpStream.
See the following for details:
Sourcepub fn set_write_timeout(&self, duration: Option<Duration>) -> Result<()>
pub fn set_write_timeout(&self, duration: Option<Duration>) -> Result<()>
Sets the write timeout of the underlying TcpStream.
See the following for details: