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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
// SPDX-License-Identifier: LGPL-3.0-or-later
// Copyright (c) 2020 Takashi Sakamoto

//! Protocol defined by TC Electronic for Konnekt 24d.
//!
//! The module includes structure, enumeration, and trait and its implementation for protocol
//! defined by TC Electronic for Konnekt 24d.
//!
//! ## Diagram of internal signal flow
//!
//! ```text
//!
//! XLR input 1 ----or--+
//! Phone input 1 --+   |
//!                     +--> analog-input-1/2 --------------------------> stream-output-1/2
//!                     |        |
//! XLR input 2 ----or--+        |
//! Phone input 2 --+            |
//!                              |
//! Phone input 3/4  -----------------------> analog-input-3/4 ---+-----> stream-output-3/4
//!                              |                  |             |
//!                        (internal mode)          |             |
//!                              v                  |             |
//!                         ++=========++           |             |
//!                         || channel ||           |             |
//!                    +--> || strip   || -------+-------------+--------> stream-output-5/6
//!                    |    || effects ||        |  |          |  |
//!           (plugin mode) ||   1/2   ||        |  |          |  |
//!                    |    ++=========++        |  |          |  |
//!                    |                         |  |          |  |
//!                    |        ++=========++    |  |          |  |
//! Coaxial input -----|------> || digital ||    |  |          |  |
//!                    |        || input   || ---------+-------------+--> stream-output-9..16
//! Optical input -----|------> || select  ||    |  |  |       |  |  |
//!                    |        ++=========++    |  |  |       |  |  |
//!                    |                     (internal mode)   |  |  |
//!                    |                         v  v  v       |  |  |
//!                    |                     ++============++  |  |  |
//!                    |                     ||   12 x 2   ||  |  |  |
//!                    |  +--(plugin mode)-> ||            || ----------> stream-output-7/8
//!                    |  |                  ||   reverb   ||  |  |  |
//!                    |  |                  ++============++  |  |  |
//!                    |  |                         v          v  v  v
//!                    |  |                     ++=====================++
//!                    |  |                     ||       16 x 2        ||
//! stream-input-1/2 --|--|-------------------> ||                     ||
//!                    |  |                     ||       mixer         ||
//!                    |  |                     ++=====================++
//!                    |  |                                v
//!                    |  |                         mixer-output-1/2 ---> analog-output-1/2
//!                    |  |
//! stream-input-3/4 --|--|---------------------------------------------> analog-output-3/4
//!                    |  |
//! stream-input-5/6 --+  |
//!                       |
//! stream-input-7/8 -----+
//!
//! stream-input-9..16 -------------------------------------------------> digital-output-1..8
//!
//! ```

use super::*;

/// Protocol implementation of Konnekt 24d.
#[derive(Default, Debug)]
pub struct K24dProtocol;

impl TcatOperation for K24dProtocol {}

impl TcatGlobalSectionSpecification for K24dProtocol {}

/// Segment for knob. 0x0000..0x0027 (36 quads).
pub type K24dKnobSegment = TcKonnektSegment<K24dKnob>;

/// Segment for configuration. 0x0028..0x0073 (76 quads).
pub type K24dConfigSegment = TcKonnektSegment<K24dConfig>;

/// Segment for state of mixer. 0x0074..0x01cf (87 quads).
pub type K24dMixerStateSegment = TcKonnektSegment<K24dMixerState>;

/// Segment for state of reverb effect. 0x01d0..0x0213. (17 quads)
pub type K24dReverbStateSegment = TcKonnektSegment<K24dReverbState>;

/// Segment for states of channel strip effect. 0x0218..0x0337 (72 quads).
pub type K24dChStripStatesSegment = TcKonnektSegment<K24dChStripStates>;

// NOTE: Segment for tuner. 0x0338..0x033b (8 quads).

/// Segment for mixer meter. 0x105c..0x10b7 (23 quads).
pub type K24dMixerMeterSegment = TcKonnektSegment<K24dMixerMeter>;

/// Segment for state of hardware. 0x100c..0x1027 (7 quads).
pub type K24dHwStateSegment = TcKonnektSegment<K24dHwState>;

/// Segment for meter of reverb effect. 0x10b8..0x010cf (6 quads).
pub type K24dReverbMeterSegment = TcKonnektSegment<K24dReverbMeter>;

/// Segment for meters of channel strip effect. 0x10d0..0x110b (15 quads).
pub type K24dChStripMetersSegment = TcKonnektSegment<K24dChStripMeters>;

macro_rules! segment_default {
    ($p:ty, $t:ty) => {
        impl Default for TcKonnektSegment<$t> {
            fn default() -> Self {
                Self {
                    data: <$t>::default(),
                    raw: vec![0; <$p as TcKonnektSegmentSerdes<$t>>::SIZE],
                }
            }
        }
    };
}

segment_default!(K24dProtocol, K24dKnob);
segment_default!(K24dProtocol, K24dConfig);
segment_default!(K24dProtocol, K24dMixerState);
segment_default!(K24dProtocol, K24dReverbState);
segment_default!(K24dProtocol, K24dChStripStates);
segment_default!(K24dProtocol, K24dMixerMeter);
segment_default!(K24dProtocol, K24dHwState);
segment_default!(K24dProtocol, K24dReverbMeter);
segment_default!(K24dProtocol, K24dChStripMeters);

/// State of knob.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct K24dKnob {
    /// Target of 1st knob.
    pub knob0_target: ShellKnob0Target,
    /// Target of 2nd knob.
    pub knob1_target: ShellKnob1Target,
    /// Loaded program number.
    pub prog: TcKonnektLoadedProgram,
}

impl Default for K24dKnob {
    fn default() -> Self {
        Self {
            knob0_target: K24dProtocol::KNOB0_TARGETS[0],
            knob1_target: K24dProtocol::KNOB1_TARGETS[0],
            prog: Default::default(),
        }
    }
}

impl ShellKnob0TargetSpecification for K24dProtocol {
    const KNOB0_TARGETS: &'static [ShellKnob0Target] = &[
        ShellKnob0Target::Analog0,
        ShellKnob0Target::Analog1,
        ShellKnob0Target::Analog2_3,
        ShellKnob0Target::Configurable,
    ];
}

impl ShellKnob1TargetSpecification for K24dProtocol {
    const KNOB1_TARGETS: &'static [ShellKnob1Target] = &[
        ShellKnob1Target::Digital0_1,
        ShellKnob1Target::Digital2_3,
        ShellKnob1Target::Digital4_5,
        ShellKnob1Target::Digital6_7,
        ShellKnob1Target::Stream,
        ShellKnob1Target::Reverb,
        ShellKnob1Target::Mixer,
        ShellKnob1Target::TunerPitchTone,
    ];
}

impl TcKonnektSegmentSerdes<K24dKnob> for K24dProtocol {
    const NAME: &'static str = "knob";
    const OFFSET: usize = 0x0004;
    const SIZE: usize = SHELL_KNOB_SEGMENT_SIZE;

    fn serialize(params: &K24dKnob, raw: &mut [u8]) -> Result<(), String> {
        serialize_knob0_target::<K24dProtocol>(&params.knob0_target, &mut raw[..4])?;
        serialize_knob1_target::<K24dProtocol>(&params.knob1_target, &mut raw[4..8])?;
        serialize_loaded_program(&params.prog, &mut raw[8..12])?;
        Ok(())
    }

    fn deserialize(params: &mut K24dKnob, raw: &[u8]) -> Result<(), String> {
        deserialize_knob0_target::<K24dProtocol>(&mut params.knob0_target, &raw[..4])?;
        deserialize_knob1_target::<K24dProtocol>(&mut params.knob1_target, &raw[4..8])?;
        deserialize_loaded_program(&mut params.prog, &raw[8..12])?;
        Ok(())
    }
}

impl TcKonnektMutableSegmentOperation<K24dKnob> for K24dProtocol {}

impl TcKonnektNotifiedSegmentOperation<K24dKnob> for K24dProtocol {
    const NOTIFY_FLAG: u32 = SHELL_KNOB_NOTIFY_FLAG;
}

impl AsRef<ShellKnob0Target> for K24dKnob {
    fn as_ref(&self) -> &ShellKnob0Target {
        &self.knob0_target
    }
}

impl AsMut<ShellKnob0Target> for K24dKnob {
    fn as_mut(&mut self) -> &mut ShellKnob0Target {
        &mut self.knob0_target
    }
}

impl AsRef<ShellKnob1Target> for K24dKnob {
    fn as_ref(&self) -> &ShellKnob1Target {
        &self.knob1_target
    }
}

impl AsMut<ShellKnob1Target> for K24dKnob {
    fn as_mut(&mut self) -> &mut ShellKnob1Target {
        &mut self.knob1_target
    }
}

impl AsRef<TcKonnektLoadedProgram> for K24dKnob {
    fn as_ref(&self) -> &TcKonnektLoadedProgram {
        &self.prog
    }
}

impl AsMut<TcKonnektLoadedProgram> for K24dKnob {
    fn as_mut(&mut self) -> &mut TcKonnektLoadedProgram {
        &mut self.prog
    }
}

/// Configuration.
#[derive(Default, Debug, Copy, Clone, PartialEq, Eq)]
pub struct K24dConfig {
    /// Configuration for optical interface.
    pub opt: ShellOptIfaceConfig,
    /// Source of coaxial output.
    pub coax_out_src: ShellCoaxOutPairSrc,
    /// Source of analog output 3/4.
    pub out_23_src: ShellPhysOutSrc,
    /// Source of sampling clock at standalone mode.
    pub standalone_src: ShellStandaloneClockSource,
    /// Rate of sampling clock at standalone mode.
    pub standalone_rate: TcKonnektStandaloneClockRate,
}

impl ShellStandaloneClockSpecification for K24dProtocol {
    const STANDALONE_CLOCK_SOURCES: &'static [ShellStandaloneClockSource] = &[
        ShellStandaloneClockSource::Optical,
        ShellStandaloneClockSource::Coaxial,
        ShellStandaloneClockSource::Internal,
    ];
}

impl TcKonnektSegmentSerdes<K24dConfig> for K24dProtocol {
    const NAME: &'static str = "configuration";
    const OFFSET: usize = 0x0028;
    const SIZE: usize = 76;

    fn serialize(params: &K24dConfig, raw: &mut [u8]) -> Result<(), String> {
        serialize_opt_iface_config(&params.opt, &mut raw[..12])?;
        serialize_coax_out_pair_source(&params.coax_out_src, &mut raw[12..16])?;
        serialize_phys_out_src(&params.out_23_src, &mut raw[16..20])?;
        serialize_standalone_clock_source::<K24dProtocol>(
            &params.standalone_src,
            &mut raw[20..24],
        )?;
        serialize_standalone_clock_rate(&params.standalone_rate, &mut raw[24..28])?;
        Ok(())
    }

    fn deserialize(params: &mut K24dConfig, raw: &[u8]) -> Result<(), String> {
        deserialize_opt_iface_config(&mut params.opt, &raw[..12])?;
        deserialize_coax_out_pair_source(&mut params.coax_out_src, &raw[12..16])?;
        deserialize_phys_out_src(&mut params.out_23_src, &raw[16..20])?;
        deserialize_standalone_clock_source::<K24dProtocol>(
            &mut params.standalone_src,
            &raw[20..24],
        )?;
        deserialize_standalone_clock_rate(&mut params.standalone_rate, &raw[24..28])?;
        Ok(())
    }
}

impl TcKonnektMutableSegmentOperation<K24dConfig> for K24dProtocol {}

impl TcKonnektNotifiedSegmentOperation<K24dConfig> for K24dProtocol {
    const NOTIFY_FLAG: u32 = SHELL_CONFIG_NOTIFY_FLAG;
}

impl AsRef<ShellOptIfaceConfig> for K24dConfig {
    fn as_ref(&self) -> &ShellOptIfaceConfig {
        &self.opt
    }
}

impl AsMut<ShellOptIfaceConfig> for K24dConfig {
    fn as_mut(&mut self) -> &mut ShellOptIfaceConfig {
        &mut self.opt
    }
}

impl AsRef<ShellCoaxOutPairSrc> for K24dConfig {
    fn as_ref(&self) -> &ShellCoaxOutPairSrc {
        &self.coax_out_src
    }
}

impl AsMut<ShellCoaxOutPairSrc> for K24dConfig {
    fn as_mut(&mut self) -> &mut ShellCoaxOutPairSrc {
        &mut self.coax_out_src
    }
}

impl AsRef<ShellStandaloneClockSource> for K24dConfig {
    fn as_ref(&self) -> &ShellStandaloneClockSource {
        &self.standalone_src
    }
}

impl AsMut<ShellStandaloneClockSource> for K24dConfig {
    fn as_mut(&mut self) -> &mut ShellStandaloneClockSource {
        &mut self.standalone_src
    }
}

impl AsRef<TcKonnektStandaloneClockRate> for K24dConfig {
    fn as_ref(&self) -> &TcKonnektStandaloneClockRate {
        &self.standalone_rate
    }
}

impl AsMut<TcKonnektStandaloneClockRate> for K24dConfig {
    fn as_mut(&mut self) -> &mut TcKonnektStandaloneClockRate {
        &mut self.standalone_rate
    }
}

/// State of mixer.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct K24dMixerState {
    /// The common structure for state of mixer.
    pub mixer: ShellMixerState,
    /// The parameter of return from reverb effect.
    pub reverb_return: ShellReverbReturn,
    /// Whether to use channel strip effect as plugin. It results in output of channel strip effect
    /// on tx stream.
    pub use_ch_strip_as_plugin: bool,
    /// Whether to use reverb effect at middle sampling rate (88.2/96.0 kHz).
    pub use_reverb_at_mid_rate: bool,
    /// Whether to use mixer function.
    pub enabled: bool,
}

impl Default for K24dMixerState {
    fn default() -> Self {
        K24dMixerState {
            mixer: K24dProtocol::create_mixer_state(),
            reverb_return: Default::default(),
            use_ch_strip_as_plugin: Default::default(),
            use_reverb_at_mid_rate: Default::default(),
            enabled: Default::default(),
        }
    }
}

impl ShellMixerStateSpecification for K24dProtocol {
    const MONITOR_SRC_MAP: [Option<ShellMixerMonitorSrcType>; SHELL_MIXER_MONITOR_SRC_COUNT] = [
        Some(ShellMixerMonitorSrcType::Stream),
        None,
        None,
        None,
        Some(ShellMixerMonitorSrcType::Analog),
        Some(ShellMixerMonitorSrcType::Analog),
        Some(ShellMixerMonitorSrcType::AdatSpdif),
        Some(ShellMixerMonitorSrcType::Adat),
        Some(ShellMixerMonitorSrcType::Adat),
        Some(ShellMixerMonitorSrcType::AdatSpdif),
    ];
}

impl TcKonnektSegmentSerdes<K24dMixerState> for K24dProtocol {
    const NAME: &'static str = "mixer-state";
    const OFFSET: usize = 0x0074;
    const SIZE: usize = ShellMixerState::SIZE + 32;

    fn serialize(params: &K24dMixerState, raw: &mut [u8]) -> Result<(), String> {
        serialize_mixer_state::<K24dProtocol>(&params.mixer, raw)?;
        serialize_reverb_return(&params.reverb_return, &mut raw[316..328])?;
        serialize_bool(&params.use_ch_strip_as_plugin, &mut raw[328..332]);
        serialize_bool(&params.use_reverb_at_mid_rate, &mut raw[332..336]);
        serialize_bool(&params.enabled, &mut raw[340..344]);
        Ok(())
    }

    fn deserialize(params: &mut K24dMixerState, raw: &[u8]) -> Result<(), String> {
        deserialize_mixer_state::<K24dProtocol>(&mut params.mixer, raw)?;
        deserialize_reverb_return(&mut params.reverb_return, &raw[316..328])?;
        deserialize_bool(&mut params.use_ch_strip_as_plugin, &raw[328..332]);
        deserialize_bool(&mut params.use_reverb_at_mid_rate, &raw[332..336]);
        deserialize_bool(&mut params.enabled, &raw[340..344]);
        Ok(())
    }
}

impl TcKonnektMutableSegmentOperation<K24dMixerState> for K24dProtocol {}

impl TcKonnektNotifiedSegmentOperation<K24dMixerState> for K24dProtocol {
    const NOTIFY_FLAG: u32 = SHELL_MIXER_NOTIFY_FLAG;
}

impl AsRef<ShellMixerState> for K24dMixerState {
    fn as_ref(&self) -> &ShellMixerState {
        &self.mixer
    }
}

impl AsMut<ShellMixerState> for K24dMixerState {
    fn as_mut(&mut self) -> &mut ShellMixerState {
        &mut self.mixer
    }
}

impl AsRef<ShellReverbReturn> for K24dMixerState {
    fn as_ref(&self) -> &ShellReverbReturn {
        &self.reverb_return
    }
}

impl AsMut<ShellReverbReturn> for K24dMixerState {
    fn as_mut(&mut self) -> &mut ShellReverbReturn {
        &mut self.reverb_return
    }
}

/// Configuration for reverb effect.
#[derive(Default, Debug, Copy, Clone, PartialEq, Eq)]
pub struct K24dReverbState(pub ReverbState);

impl TcKonnektSegmentSerdes<K24dReverbState> for K24dProtocol {
    const NAME: &'static str = "reverb-state";
    const OFFSET: usize = 0x01d0;
    const SIZE: usize = ReverbState::SIZE;

    fn serialize(params: &K24dReverbState, raw: &mut [u8]) -> Result<(), String> {
        serialize_reverb_state(&params.0, raw)
    }

    fn deserialize(params: &mut K24dReverbState, raw: &[u8]) -> Result<(), String> {
        deserialize_reverb_state(&mut params.0, raw)
    }
}

impl TcKonnektMutableSegmentOperation<K24dReverbState> for K24dProtocol {}

impl TcKonnektNotifiedSegmentOperation<K24dReverbState> for K24dProtocol {
    const NOTIFY_FLAG: u32 = SHELL_REVERB_NOTIFY_FLAG;
}

impl AsRef<ReverbState> for K24dReverbState {
    fn as_ref(&self) -> &ReverbState {
        &self.0
    }
}

impl AsMut<ReverbState> for K24dReverbState {
    fn as_mut(&mut self) -> &mut ReverbState {
        &mut self.0
    }
}

/// Configuration for channel strip effect.
#[derive(Default, Debug, Copy, Clone, PartialEq, Eq)]
pub struct K24dChStripStates(pub [ChStripState; SHELL_CH_STRIP_COUNT]);

impl TcKonnektSegmentSerdes<K24dChStripStates> for K24dProtocol {
    const NAME: &'static str = "channel-strip-state";
    const OFFSET: usize = 0x0218;
    const SIZE: usize = ChStripState::SIZE * SHELL_CH_STRIP_COUNT + 4;

    fn serialize(params: &K24dChStripStates, raw: &mut [u8]) -> Result<(), String> {
        serialize_ch_strip_states(&params.0, raw)
    }

    fn deserialize(params: &mut K24dChStripStates, raw: &[u8]) -> Result<(), String> {
        deserialize_ch_strip_states(&mut params.0, raw)
    }
}

impl TcKonnektMutableSegmentOperation<K24dChStripStates> for K24dProtocol {}

impl TcKonnektNotifiedSegmentOperation<K24dChStripStates> for K24dProtocol {
    const NOTIFY_FLAG: u32 = SHELL_CH_STRIP_NOTIFY_FLAG;
}

impl AsRef<[ChStripState]> for K24dChStripStates {
    fn as_ref(&self) -> &[ChStripState] {
        &self.0
    }
}

impl AsMut<[ChStripState]> for K24dChStripStates {
    fn as_mut(&mut self) -> &mut [ChStripState] {
        &mut self.0
    }
}

/// Hardware state.
#[derive(Default, Debug, Copy, Clone, PartialEq, Eq)]
pub struct K24dHwState(pub ShellHwState);

impl TcKonnektSegmentSerdes<K24dHwState> for K24dProtocol {
    const NAME: &'static str = "hardware-state";
    const OFFSET: usize = 0x100c;
    const SIZE: usize = ShellHwState::SIZE;

    fn serialize(params: &K24dHwState, raw: &mut [u8]) -> Result<(), String> {
        serialize_hw_state(&params.0, raw)
    }

    fn deserialize(params: &mut K24dHwState, raw: &[u8]) -> Result<(), String> {
        deserialize_hw_state(&mut params.0, raw)
    }
}

impl TcKonnektMutableSegmentOperation<K24dHwState> for K24dProtocol {}

impl TcKonnektNotifiedSegmentOperation<K24dHwState> for K24dProtocol {
    const NOTIFY_FLAG: u32 = SHELL_HW_STATE_NOTIFY_FLAG;
}

impl AsRef<ShellHwState> for K24dHwState {
    fn as_ref(&self) -> &ShellHwState {
        &self.0
    }
}

impl AsMut<ShellHwState> for K24dHwState {
    fn as_mut(&mut self) -> &mut ShellHwState {
        &mut self.0
    }
}

impl AsRef<FireWireLedState> for K24dHwState {
    fn as_ref(&self) -> &FireWireLedState {
        &self.0.firewire_led
    }
}

impl AsMut<FireWireLedState> for K24dHwState {
    fn as_mut(&mut self) -> &mut FireWireLedState {
        &mut self.0.firewire_led
    }
}

const K24D_METER_ANALOG_INPUT_COUNT: usize = 2;
const K24D_METER_DIGITAL_INPUT_COUNT: usize = 2;

/// Hardware metering for mixer function.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct K24dMixerMeter(pub ShellMixerMeter);

impl Default for K24dMixerMeter {
    fn default() -> Self {
        K24dMixerMeter(K24dProtocol::create_meter_state())
    }
}

impl ShellMixerMeterSpecification for K24dProtocol {
    const ANALOG_INPUT_COUNT: usize = K24D_METER_ANALOG_INPUT_COUNT;
    const DIGITAL_INPUT_COUNT: usize = K24D_METER_DIGITAL_INPUT_COUNT;
}

impl TcKonnektSegmentSerdes<K24dMixerMeter> for K24dProtocol {
    const NAME: &'static str = "mixer-meter";
    const OFFSET: usize = 0x105c;
    const SIZE: usize = ShellMixerMeter::SIZE;

    fn serialize(params: &K24dMixerMeter, raw: &mut [u8]) -> Result<(), String> {
        serialize_mixer_meter::<K24dProtocol>(&params.0, raw)
    }

    fn deserialize(params: &mut K24dMixerMeter, raw: &[u8]) -> Result<(), String> {
        deserialize_mixer_meter::<K24dProtocol>(&mut params.0, raw)
    }
}

impl AsRef<ShellMixerMeter> for K24dMixerMeter {
    fn as_ref(&self) -> &ShellMixerMeter {
        &self.0
    }
}

impl AsMut<ShellMixerMeter> for K24dMixerMeter {
    fn as_mut(&mut self) -> &mut ShellMixerMeter {
        &mut self.0
    }
}

/// Hardware metering for reverb effect.
#[derive(Default, Debug, Copy, Clone, PartialEq, Eq)]
pub struct K24dReverbMeter(pub ReverbMeter);

impl TcKonnektSegmentSerdes<K24dReverbMeter> for K24dProtocol {
    const NAME: &'static str = "reverb-meter";
    const OFFSET: usize = 0x10b8;
    const SIZE: usize = ReverbMeter::SIZE;

    fn serialize(params: &K24dReverbMeter, raw: &mut [u8]) -> Result<(), String> {
        serialize_reverb_meter(&params.0, raw)
    }

    fn deserialize(params: &mut K24dReverbMeter, raw: &[u8]) -> Result<(), String> {
        deserialize_reverb_meter(&mut params.0, raw)
    }
}

impl AsRef<ReverbMeter> for K24dReverbMeter {
    fn as_ref(&self) -> &ReverbMeter {
        &self.0
    }
}

impl AsMut<ReverbMeter> for K24dReverbMeter {
    fn as_mut(&mut self) -> &mut ReverbMeter {
        &mut self.0
    }
}

/// Hardware metering for channel strip effect.
#[derive(Default, Debug, Copy, Clone, PartialEq, Eq)]
pub struct K24dChStripMeters(pub [ChStripMeter; SHELL_CH_STRIP_COUNT]);

impl TcKonnektSegmentSerdes<K24dChStripMeters> for K24dProtocol {
    const NAME: &'static str = "channel-strip-meter";
    const OFFSET: usize = 0x10d0;
    const SIZE: usize = ChStripMeter::SIZE * SHELL_CH_STRIP_COUNT + 4;

    fn serialize(params: &K24dChStripMeters, raw: &mut [u8]) -> Result<(), String> {
        serialize_ch_strip_meters(&params.0, raw)
    }

    fn deserialize(params: &mut K24dChStripMeters, raw: &[u8]) -> Result<(), String> {
        deserialize_ch_strip_meters(&mut params.0, raw)
    }
}

impl AsRef<[ChStripMeter]> for K24dChStripMeters {
    fn as_ref(&self) -> &[ChStripMeter] {
        &self.0
    }
}

impl AsMut<[ChStripMeter]> for K24dChStripMeters {
    fn as_mut(&mut self) -> &mut [ChStripMeter] {
        &mut self.0
    }
}