laser-dac 0.11.4

Unified laser DAC abstraction supporting multiple protocols
Documentation
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
//! A simple abstraction around a single Ether Dream DAC.

pub mod stream;

pub use self::stream::Stream;
use crate::protocols::ether_dream::protocol::{
    self, command, Command as CommandTrait, ReadFromBytes, WriteToBytes,
};
use bitflags::bitflags;
use std::error::Error;
use std::{fmt, io, ops};

/// A DAC along with its broadcasted MAC address.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct Addressed {
    pub mac_address: MacAddress,
    pub dac: Dac,
}

/// A simple abstraction around a single Ether Dream DAC.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct Dac {
    pub hw_revision: u16,
    pub sw_revision: u16,
    pub buffer_capacity: u16,
    pub max_point_rate: u32,
    pub status: Status,
}

/// The fixed-size array used to represent the MAC address of a DAC.
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
pub struct MacAddress(pub [u8; 6]);

/// DAC status information.
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
pub struct Status {
    pub protocol: u8,
    pub light_engine: LightEngine,
    pub playback: Playback,
    pub data_source: DataSource,
    pub light_engine_flags: LightEngineFlags,
    pub playback_flags: PlaybackFlags,
    pub buffer_fullness: u16,
    pub point_rate: u32,
    pub point_count: u32,
}

#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
pub enum LightEngine {
    Ready,
    Warmup,
    Cooldown,
    EmergencyStop,
}

#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
pub enum Playback {
    Idle,
    Prepared,
    Playing,
}

#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
pub enum DataSource {
    NetworkStreaming,
    IldaPlayback(IldaPlaybackFlags),
    InternalAbstractGenerator(InternalAbstractGeneratorFlags),
}

bitflags! {
    #[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
    pub struct LightEngineFlags: u16 {
        const EMERGENCY_STOP_PACKET_OR_INVALID_COMMAND = 0b00000001;
        const EMERGENCY_STOP_PROJECTOR_INPUT = 0b00000010;
        const EMERGENCY_STOP_PROJECTOR_INPUT_ACTIVE = 0b00000100;
        const EMERGENCY_STOP_OVER_TEMPERATURE = 0b00001000;
        const EMERGENCY_STOP_OVER_TEMPERATURE_ACTIVE = 0b00010000;
        const EMERGENCY_STOP_LOST_ETHERNET_LINK = 0b00100000;
    }
}

bitflags! {
    #[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
    pub struct PlaybackFlags: u16 {
        const SHUTTER_OPEN = 0b00000001;
        const UNDERFLOWED = 0b00000010;
        const EMERGENCY_STOP = 0b00000100;
    }
}

bitflags! {
    #[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
    pub struct IldaPlaybackFlags: u16 {
        const PLAYING = 0b0;
        const REPEAT = 0b1;
    }
}

bitflags! {
    #[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
    pub struct InternalAbstractGeneratorFlags: u16 {
        const PLAYING = 0;
    }
}

bitflags! {
    #[derive(Copy, Clone, Debug, Eq, Hash, PartialEq)]
    pub struct PointControl: u16 {
        const CHANGE_RATE = 0b10000000_00000000;
    }
}

/// A runtime representation of any command.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum Command<'a> {
    PrepareStream(command::PrepareStream),
    Begin(command::Begin),
    Update(command::Update),
    PointRate(command::PointRate),
    Data(command::Data<'a>),
    Stop(command::Stop),
    EmergencyStop(command::EmergencyStop),
    ClearEmergencyStop(command::ClearEmergencyStop),
    Ping(command::Ping),
}

#[derive(Debug)]
pub enum ProtocolError {
    UnknownLightEngineState,
    UnknownPlaybackState,
    UnknownDataSource,
}

impl Addressed {
    pub fn from_broadcast(dac_broadcast: &protocol::DacBroadcast) -> Result<Self, ProtocolError> {
        let protocol::DacBroadcast {
            mac_address,
            hw_revision,
            sw_revision,
            buffer_capacity,
            max_point_rate,
            dac_status,
        } = *dac_broadcast;
        let mac_address = MacAddress(mac_address);
        let status = Status::from_protocol(&dac_status)?;
        let dac = Dac {
            hw_revision,
            sw_revision,
            buffer_capacity,
            max_point_rate,
            status,
        };
        Ok(Addressed { mac_address, dac })
    }
}

impl Dac {
    pub fn update_status(&mut self, status: &protocol::DacStatus) -> Result<(), ProtocolError> {
        self.status.update(status)
    }
}

impl Status {
    pub fn from_protocol(status: &protocol::DacStatus) -> Result<Self, ProtocolError> {
        let protocol = status.protocol;
        let light_engine = LightEngine::from_protocol(status.light_engine_state)
            .ok_or(ProtocolError::UnknownLightEngineState)?;
        let playback = Playback::from_protocol(status.playback_state)
            .ok_or(ProtocolError::UnknownPlaybackState)?;
        let data_source = DataSource::from_protocol(status.source, status.source_flags)
            .ok_or(ProtocolError::UnknownDataSource)?;
        let light_engine_flags = LightEngineFlags::from_bits_truncate(status.light_engine_flags);
        let playback_flags = PlaybackFlags::from_bits_truncate(status.playback_flags);
        Ok(Status {
            protocol,
            light_engine,
            playback,
            data_source,
            light_engine_flags,
            playback_flags,
            buffer_fullness: status.buffer_fullness,
            point_rate: status.point_rate,
            point_count: status.point_count,
        })
    }

    pub fn update(&mut self, status: &protocol::DacStatus) -> Result<(), ProtocolError> {
        self.protocol = status.protocol;
        self.light_engine = LightEngine::from_protocol(status.light_engine_state)
            .ok_or(ProtocolError::UnknownLightEngineState)?;
        self.playback = Playback::from_protocol(status.playback_state)
            .ok_or(ProtocolError::UnknownPlaybackState)?;
        self.data_source = DataSource::from_protocol(status.source, status.source_flags)
            .ok_or(ProtocolError::UnknownDataSource)?;
        self.light_engine_flags = LightEngineFlags::from_bits_truncate(status.light_engine_flags);
        self.playback_flags = PlaybackFlags::from_bits_truncate(status.playback_flags);
        self.buffer_fullness = status.buffer_fullness;
        self.point_rate = status.point_rate;
        self.point_count = status.point_count;
        Ok(())
    }

    pub fn to_protocol(&self) -> protocol::DacStatus {
        let (source, source_flags) = self.data_source.to_protocol();
        protocol::DacStatus {
            protocol: self.protocol,
            light_engine_state: self.light_engine.to_protocol(),
            playback_state: self.playback.to_protocol(),
            source,
            light_engine_flags: self.light_engine_flags.bits(),
            playback_flags: self.playback_flags.bits(),
            source_flags,
            buffer_fullness: self.buffer_fullness,
            point_rate: self.point_rate,
            point_count: self.point_count,
        }
    }
}

impl LightEngine {
    pub fn from_protocol(state: u8) -> Option<Self> {
        Some(match state {
            protocol::DacStatus::LIGHT_ENGINE_READY => LightEngine::Ready,
            protocol::DacStatus::LIGHT_ENGINE_WARMUP => LightEngine::Warmup,
            protocol::DacStatus::LIGHT_ENGINE_COOLDOWN => LightEngine::Cooldown,
            protocol::DacStatus::LIGHT_ENGINE_EMERGENCY_STOP => LightEngine::EmergencyStop,
            _ => return None,
        })
    }

    pub fn to_protocol(&self) -> u8 {
        match *self {
            LightEngine::Ready => protocol::DacStatus::LIGHT_ENGINE_READY,
            LightEngine::Warmup => protocol::DacStatus::LIGHT_ENGINE_WARMUP,
            LightEngine::Cooldown => protocol::DacStatus::LIGHT_ENGINE_COOLDOWN,
            LightEngine::EmergencyStop => protocol::DacStatus::LIGHT_ENGINE_EMERGENCY_STOP,
        }
    }
}

impl Playback {
    pub fn from_protocol(state: u8) -> Option<Self> {
        Some(match state {
            protocol::DacStatus::PLAYBACK_IDLE => Playback::Idle,
            protocol::DacStatus::PLAYBACK_PREPARED => Playback::Prepared,
            protocol::DacStatus::PLAYBACK_PLAYING => Playback::Playing,
            _ => return None,
        })
    }

    pub fn to_protocol(&self) -> u8 {
        match *self {
            Playback::Idle => protocol::DacStatus::PLAYBACK_IDLE,
            Playback::Prepared => protocol::DacStatus::PLAYBACK_PREPARED,
            Playback::Playing => protocol::DacStatus::PLAYBACK_PLAYING,
        }
    }
}

impl DataSource {
    pub fn from_protocol(source: u8, flags: u16) -> Option<Self> {
        Some(match source {
            protocol::DacStatus::SOURCE_NETWORK_STREAMING => DataSource::NetworkStreaming,
            protocol::DacStatus::SOURCE_ILDA_PLAYBACK_SD => {
                DataSource::IldaPlayback(IldaPlaybackFlags::from_bits_truncate(flags))
            }
            protocol::DacStatus::SOURCE_INTERNAL_ABSTRACT_GENERATOR => {
                DataSource::InternalAbstractGenerator(
                    InternalAbstractGeneratorFlags::from_bits_truncate(flags),
                )
            }
            _ => return None,
        })
    }

    pub fn to_protocol(&self) -> (u8, u16) {
        match *self {
            DataSource::NetworkStreaming => (protocol::DacStatus::SOURCE_NETWORK_STREAMING, 0),
            DataSource::IldaPlayback(ref flags) => {
                (protocol::DacStatus::SOURCE_ILDA_PLAYBACK_SD, flags.bits())
            }
            DataSource::InternalAbstractGenerator(ref flags) => (
                protocol::DacStatus::SOURCE_INTERNAL_ABSTRACT_GENERATOR,
                flags.bits(),
            ),
        }
    }
}

impl ops::Deref for Addressed {
    type Target = Dac;
    fn deref(&self) -> &Self::Target {
        &self.dac
    }
}

impl ops::DerefMut for Addressed {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.dac
    }
}

impl From<[u8; 6]> for MacAddress {
    fn from(bytes: [u8; 6]) -> Self {
        MacAddress(bytes)
    }
}

impl From<MacAddress> for [u8; 6] {
    fn from(mac: MacAddress) -> [u8; 6] {
        mac.0
    }
}

impl ops::Deref for MacAddress {
    type Target = [u8; 6];
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl fmt::Display for MacAddress {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        let a = &self.0;
        write!(
            f,
            "{:02X}:{:02X}:{:02X}:{:02X}:{:02X}:{:02X}",
            a[0], a[1], a[2], a[3], a[4], a[5]
        )
    }
}

impl ReadFromBytes for Command<'static> {
    fn read_from_bytes<R: byteorder::ReadBytesExt>(mut reader: R) -> io::Result<Self> {
        let cmd = reader.read_u8()?;
        let kind = match cmd {
            command::PrepareStream::START_BYTE => command::PrepareStream.into(),
            command::Begin::START_BYTE => command::Begin::read_fields(reader)?.into(),
            command::Update::START_BYTE => command::Update::read_fields(reader)?.into(),
            command::PointRate::START_BYTE => command::PointRate::read_fields(reader)?.into(),
            command::Data::START_BYTE => command::Data::read_fields(reader)?.into(),
            command::Stop::START_BYTE => command::Stop.into(),
            command::EmergencyStop::START_BYTE => command::EmergencyStop.into(),
            command::EmergencyStopAlt::START_BYTE => command::EmergencyStop.into(),
            command::ClearEmergencyStop::START_BYTE => command::ClearEmergencyStop.into(),
            command::Ping::START_BYTE => command::Ping.into(),
            unknown => {
                return Err(io::Error::new(
                    io::ErrorKind::InvalidData,
                    format!("invalid command byte \"{}\"", unknown),
                ))
            }
        };
        Ok(kind)
    }
}

impl<'a> WriteToBytes for Command<'a> {
    fn write_to_bytes<W: byteorder::WriteBytesExt>(&self, writer: W) -> io::Result<()> {
        match *self {
            Command::PrepareStream(ref cmd) => cmd.write_to_bytes(writer),
            Command::Begin(ref cmd) => cmd.write_to_bytes(writer),
            Command::Update(ref cmd) => cmd.write_to_bytes(writer),
            Command::PointRate(ref cmd) => cmd.write_to_bytes(writer),
            Command::Data(ref cmd) => cmd.write_to_bytes(writer),
            Command::Stop(ref cmd) => cmd.write_to_bytes(writer),
            Command::EmergencyStop(ref cmd) => cmd.write_to_bytes(writer),
            Command::ClearEmergencyStop(ref cmd) => cmd.write_to_bytes(writer),
            Command::Ping(ref cmd) => cmd.write_to_bytes(writer),
        }
    }
}

impl<'a> From<command::PrepareStream> for Command<'a> {
    fn from(command: command::PrepareStream) -> Self {
        Command::PrepareStream(command)
    }
}
impl<'a> From<command::Begin> for Command<'a> {
    fn from(command: command::Begin) -> Self {
        Command::Begin(command)
    }
}
impl<'a> From<command::Update> for Command<'a> {
    fn from(command: command::Update) -> Self {
        Command::Update(command)
    }
}
impl<'a> From<command::PointRate> for Command<'a> {
    fn from(command: command::PointRate) -> Self {
        Command::PointRate(command)
    }
}
impl<'a> From<command::Data<'a>> for Command<'a> {
    fn from(command: command::Data<'a>) -> Self {
        Command::Data(command)
    }
}
impl<'a> From<command::Stop> for Command<'a> {
    fn from(command: command::Stop) -> Self {
        Command::Stop(command)
    }
}
impl<'a> From<command::EmergencyStop> for Command<'a> {
    fn from(command: command::EmergencyStop) -> Self {
        Command::EmergencyStop(command)
    }
}
impl<'a> From<command::EmergencyStopAlt> for Command<'a> {
    fn from(_: command::EmergencyStopAlt) -> Self {
        Command::EmergencyStop(command::EmergencyStop)
    }
}
impl<'a> From<command::ClearEmergencyStop> for Command<'a> {
    fn from(command: command::ClearEmergencyStop) -> Self {
        Command::ClearEmergencyStop(command)
    }
}
impl<'a> From<command::Ping> for Command<'a> {
    fn from(command: command::Ping) -> Self {
        Command::Ping(command)
    }
}

impl fmt::Display for ProtocolError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            ProtocolError::UnknownLightEngineState => write!(f, "unknown light engine state"),
            ProtocolError::UnknownPlaybackState => write!(f, "unknown playback state"),
            ProtocolError::UnknownDataSource => write!(f, "unknown data source"),
        }
    }
}

impl Error for ProtocolError {}