spd3303x 0.2.1

Library for controlling the *Siglent SPD3303X* programmable power supply.
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
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
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
use std::{net::Ipv4Addr, ops::Neg};

use regex::Regex;
use scpi_client::{
    EmptyResponse, Error, ScpiDeserialize, ScpiSerialize, impl_scpi_request, impl_scpi_serialize,
    match_literal, read_all, read_prefix, read_until, scpi_enum,
};

// 1. *IDN?
// Command format *IDN?
// Description Query the manufacturer, product type, series No., software version and hardware version
pub struct IdentityRequest;
impl_scpi_serialize!(IdentityRequest, ["*IDN?"]);

// Return Info Manufacturer, product type, series No., software version, hardware version
// Typical Return Siglent Technologies, SPD3303X, SPD00001130025, 1.01.01.01.02,V3.0
#[derive(Debug, Clone)]
pub struct IdentityResponse {
    pub company_name: String,
    pub model_number: String,
    pub serial_number: String,
    pub software_version: String,
    pub hardware_version: String,
}

impl ScpiDeserialize for IdentityResponse {
    fn deserialize(input: &mut &str) -> Result<Self, Error> {
        Ok(IdentityResponse {
            company_name: read_until(input, ',')?.trim().to_string(),
            model_number: read_until(input, ',')?.trim().to_string(),
            serial_number: read_until(input, ',')?.trim().to_string(),
            software_version: read_until(input, ',')?.trim().to_string(),
            hardware_version: read_until(input, '\n')?.trim().to_string(),
        })
    }
}

impl_scpi_request!(IdentityRequest, IdentityResponse);

// 2. *SAV
// Command format: *SAV {1|2|3|4|5}
// Description: Save current state in nonvolatile memory
// Example: *SAV 1

scpi_enum! {
    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    pub enum MemorySlot {
        One => "1",
        Two => "2",
        Three => "3",
        Four => "4",
        Five => "5",
    }
}

impl TryFrom<u8> for MemorySlot {
    type Error = ();

    fn try_from(value: u8) -> Result<Self, Self::Error> {
        match value {
            1 => Ok(MemorySlot::One),
            2 => Ok(MemorySlot::Two),
            3 => Ok(MemorySlot::Three),
            4 => Ok(MemorySlot::Four),
            5 => Ok(MemorySlot::Five),
            _ => Err(()),
        }
    }
}

pub struct SaveRequest {
    pub slot: MemorySlot,
}
impl_scpi_serialize!(SaveRequest, ["*SAV ", slot]);
impl_scpi_request!(SaveRequest, EmptyResponse);

// 3. *RCL
// Command format *RCL {1|2|3|4|5}
// Description Recall state that had been saved from nonvolatile memory.
// Example *RCL 1
pub struct RecallRequest {
    pub slot: MemorySlot,
}
impl_scpi_serialize!(RecallRequest, ["*RCL ", slot]);
impl_scpi_request!(RecallRequest, EmptyResponse);

// 4. INSTrument
// Command format INSTrument {CH1|CH2}
// Description Select the channel that will be operated.
// Example INSTrument CH1

scpi_enum! {
    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    pub enum Channel {
        One => "CH1",
        Two => "CH2",
    }
}

impl TryFrom<u8> for Channel {
    type Error = ();

    fn try_from(value: u8) -> Result<Self, Self::Error> {
        match value {
            1 => Ok(Channel::One),
            2 => Ok(Channel::Two),
            _ => Err(()),
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SetInstrumentRequest {
    pub channel: Channel,
}
impl_scpi_serialize!(SetInstrumentRequest, ["INSTrument ", channel]);
impl_scpi_request!(SetInstrumentRequest, EmptyResponse);

// Command format INSTrument?
// Description Query the current operating channel
// Example INSTrument?
// Typical Return CH1
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct GetInstrumentRequest;
impl_scpi_serialize!(GetInstrumentRequest, ["INSTrument?"]);

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct GetInstrumentResponse {
    pub channel: Channel,
}

impl ScpiDeserialize for GetInstrumentResponse {
    fn deserialize(input: &mut &str) -> Result<Self, Error> {
        Ok(GetInstrumentResponse {
            channel: Channel::deserialize(input)?,
        })
    }
}

impl_scpi_request!(GetInstrumentRequest, GetInstrumentResponse);

// 5. MEASure
// Command format MEASure:CURRent? [{CH1|CH2}]
// Description Query current value for specified channel, if there is no specified channel,
// query the current channel
// Example MEASure:CURRent? CH1
// Typical Return 3.000
// Command format MEASure:VOLTage? [{CH1|CH2}]
// Description Query voltage value for specified channel, if there is no specified channel,
// query the current channel
// Example MEASure:VOLTage? CH1
// Typical Return 30.000
// Command format MEASure:POWEr? [{CH1|CH2}]
// Description Query power value for specified channel, if there is no specified channel,
// query the current channel.
// Example MEASure:POWEr? CH1
// Typical Return 90.000

scpi_enum! {
    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    pub enum Quantity {
        Current => "CURRent",
        Voltage => "VOLTage",
        Power => "POWEr",
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct MeasureRequest {
    pub quantity: Quantity,
    pub channel: Option<Channel>,
}
impl_scpi_serialize!(MeasureRequest, ["MEASure:", quantity, "? ", channel]);

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct MeasureResponse(pub Reading);

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Reading {
    millis: u16,
}

impl Reading {
    pub fn from_millis(millis: u16) -> Reading {
        Reading { millis }
    }

    pub fn get_millis(&self) -> u16 {
        self.millis
    }
}

impl From<Reading> for f64 {
    fn from(value: Reading) -> Self {
        f64::from(value.get_millis()) / 1000.0
    }
}

impl From<Reading> for f32 {
    fn from(value: Reading) -> Self {
        f32::from(value.get_millis()) / 1000.0
    }
}

impl From<f32> for Reading {
    fn from(value: f32) -> Self {
        // TODO should not silently clamp below zero
        Reading::from_millis((value * 1000.0).round().max(0.0) as u16)
    }
}

impl From<f64> for Reading {
    fn from(value: f64) -> Self {
        Reading::from_millis((value * 1000.0).round().max(0.0) as u16)
    }
}

impl ScpiSerialize for Reading {
    fn serialize(&self, out: &mut String) {
        let whole = self.millis / 1000;
        let frac = self.millis % 1000;
        use std::fmt::Write;
        write!(out, "{whole}.{frac:03}").expect("Failed to format number");
    }
}

impl ScpiDeserialize for Reading {
    fn deserialize(input: &mut &str) -> Result<Self, Error> {
        let whole_part = u16::deserialize(input)?;
        match_literal(input, ".")?;
        let frac_part = u16::deserialize(input)?;
        Ok(Reading::from_millis(whole_part * 1000 + frac_part))
    }
}

impl ScpiDeserialize for MeasureResponse {
    fn deserialize(input: &mut &str) -> Result<Self, Error> {
        let value = Reading::deserialize(input)?;
        match_literal(input, "\n")?;
        Ok(MeasureResponse(value))
    }
}

impl_scpi_request!(MeasureRequest, MeasureResponse);

// 6. CURRent
// Command format [{CH1|CH2}:]CURRent <current>
// Description Set current value of the selected channel
// Example CH1:CURRent 0.5
// Command format [{CH1|CH2}:]CURRent?
// Description Query the current value of the selected channel.
// Example CH1:CURRent?
// Typical Return 0.500
// 7. VOLTage
// Command format [{CH1|CH2}:]VOLTage <voltage>
// Description Set voltage value of the selected channel
// Example CH1:VOLTage 25
// Command format [{CH1|CH2}:]VOLTage?
// Description Query the voltage value of the selected channel.
// Example CH1:VOLTage?
// Typical Return 25.000

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum LimitQuantity {
    Current,
    Voltage,
}
impl From<&LimitQuantity> for Quantity {
    fn from(value: &LimitQuantity) -> Self {
        match value {
            LimitQuantity::Current => Quantity::Current,
            LimitQuantity::Voltage => Quantity::Voltage,
        }
    }
}

impl ScpiSerialize for LimitQuantity {
    fn serialize(&self, out: &mut String) {
        Quantity::from(self).serialize(out);
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SetLimitRequest {
    pub quantity: LimitQuantity,
    pub value: Reading,
    pub channel: Option<Channel>,
}
impl ScpiSerialize for SetLimitRequest {
    fn serialize(&self, out: &mut String) {
        self.channel.serialize(out);
        if self.channel.is_some() {
            out.push(':');
        }
        self.quantity.serialize(out);
        out.push(' ');
        self.value.serialize(out);
    }
}
impl_scpi_request!(SetLimitRequest, EmptyResponse);

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct GetLimitRequest {
    pub quantity: LimitQuantity,
    pub channel: Option<Channel>,
}
impl ScpiSerialize for GetLimitRequest {
    fn serialize(&self, out: &mut String) {
        self.channel.serialize(out);
        if self.channel.is_some() {
            out.push(':');
        }
        self.quantity.serialize(out);
        out.push('?');
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct GetLimitResponse(pub Reading);
impl ScpiDeserialize for GetLimitResponse {
    fn deserialize(input: &mut &str) -> Result<Self, Error> {
        let value = Reading::deserialize(input)?;
        match_literal(input, "\n")?;
        Ok(GetLimitResponse(value))
    }
}

impl_scpi_request!(GetLimitRequest, GetLimitResponse);

// 8. OUTPut
// Command format OUTPut {CH1|CH2|CH3},{ON|OFF}
// Description Turn on/off the specified channel output.
// Example OUTPut CH1,ON

scpi_enum! {
    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    pub enum State {
        On => "ON",
        Off => "OFF",
    }
}

impl From<bool> for State {
    fn from(value: bool) -> Self {
        match value {
            true => State::On,
            false => State::Off,
        }
    }
}

impl From<State> for bool {
    fn from(value: State) -> Self {
        match value {
            State::On => true,
            State::Off => false,
        }
    }
}

impl Neg for State {
    type Output = State;

    fn neg(self) -> Self::Output {
        match self {
            State::On => State::Off,
            State::Off => State::On,
        }
    }
}

scpi_enum! {
    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    pub enum OutputChannel {
        One => "CH1",
        Two => "CH2",
        Three => "CH3",
    }
}

impl TryFrom<OutputChannel> for Channel {
    type Error = ();

    fn try_from(value: OutputChannel) -> Result<Self, Self::Error> {
        match value {
            OutputChannel::One => Ok(Channel::One),
            OutputChannel::Two => Ok(Channel::Two),
            OutputChannel::Three => Err(()),
        }
    }
}

impl From<Channel> for OutputChannel {
    fn from(value: Channel) -> Self {
        match value {
            Channel::One => OutputChannel::One,
            Channel::Two => OutputChannel::Two,
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SetOutputStateRequest {
    pub channel: OutputChannel,
    pub state: State,
}
impl_scpi_serialize!(SetOutputStateRequest, ["OUTPut ", channel, ",", state]);
impl_scpi_request!(SetOutputStateRequest, EmptyResponse);

// Command format OUTPut:TRACK {0|1|2}
// Description Select operation mode. Parameters {0|1|2} mean independent, series and
// parallel respectively
// Example OUTPut:TRACK 0

scpi_enum! {
    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    pub enum OperationMode {
        Independent => "0",
        Series => "1",
        Parallel => "2",
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SetOperationModeRequest {
    pub mode: OperationMode,
}

impl_scpi_serialize!(SetOperationModeRequest, ["OUTPut:TRACK ", mode]);
impl_scpi_request!(SetOperationModeRequest, EmptyResponse);

// Command format OUTPut:WAVE {CH1|CH2},{ON|OFF}
// Description Turn on/off the Waveform Display function of specified channel
// Example OUTPut:WAVE CH1,ON

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct WaveformDisplayRequest {
    pub channel: Channel,
    pub state: State,
}
impl_scpi_serialize!(
    WaveformDisplayRequest,
    ["OUTPut:WAVE ", channel, ",", state]
);
impl_scpi_request!(WaveformDisplayRequest, EmptyResponse);

// 9. TIMEr
// Command format TIMEr:SET
// {CH1|CH2},{1|2|3|4|5},<voltage>,<current>,<time>
// Description Set timing parameters of specified channel, including group{1|2|3|4|5}
// voltage, current, time
// Example TIMEr:SET CH1,2,3,0.5,2

scpi_enum! {
    #[derive(Debug, Clone, Copy, PartialEq, Eq)]
    pub enum TimingGroup {
        One => "1",
        Two => "2",
        Three => "3",
        Four => "4",
        Five => "5",
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct TimeInterval(u16);

impl From<u16> for TimeInterval {
    fn from(value: u16) -> Self {
        if value > 10000 {
            panic!("Time interval value {value} exceeds accepted range for SPD3303X (max. 10000)");
        }
        TimeInterval(value)
    }
}

impl ScpiSerialize for TimeInterval {
    fn serialize(&self, out: &mut String) {
        use std::fmt::Write;
        write!(out, "{}", self.0).expect("Failed to format number");
    }
}

impl ScpiDeserialize for TimeInterval {
    fn deserialize(input: &mut &str) -> Result<Self, Error> {
        Ok(TimeInterval(u16::deserialize(input)?))
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SetTimingParametersRequest {
    pub channel: Channel,
    pub group: TimingGroup,
    pub voltage: Reading,
    pub current: Reading,
    pub time: TimeInterval,
}
impl_scpi_serialize!(
    SetTimingParametersRequest,
    [
        "TIMEr:SET ",
        channel,
        ",",
        group,
        ",",
        voltage,
        ",",
        current,
        ",",
        time
    ]
);
impl_scpi_request!(SetTimingParametersRequest, EmptyResponse);

// Command format TIMEr:SET? {CH1|CH2},{1|2|3|4|5};
// Description Query the voltage/current/time parameters of specified group of specified
// channel
// Example TIMEr:SET? CH1,2
// Typical Return 3,0.5,2
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct GetTimingParametersRequest {
    pub channel: Channel,
    pub group: TimingGroup,
}

impl_scpi_serialize!(
    GetTimingParametersRequest,
    ["TIMEr:SET? ", channel, ",", group]
);

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct GetTimingParametersResponse {
    pub voltage: Reading,
    pub current: Reading,
    pub time: Reading,
}

impl ScpiDeserialize for GetTimingParametersResponse {
    fn deserialize(input: &mut &str) -> Result<Self, Error> {
        let voltage = Reading::deserialize(input)?;
        match_literal(input, ",")?;
        let current = Reading::deserialize(input)?;
        match_literal(input, ",")?;
        let time = Reading::deserialize(input)?;

        Ok(GetTimingParametersResponse {
            voltage,
            current,
            time,
        })
    }
}

impl_scpi_request!(GetTimingParametersRequest, GetTimingParametersResponse);

// Command format TIMEr {CH1|CH2},{ON|OFF};
// Description Turn on/off Timer function of specified channel
// Example TIMEr CH1,ON
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SetTimerStateRequest {
    pub channel: Channel,
    pub state: State,
}
impl_scpi_serialize!(SetTimerStateRequest, ["TIMEr ", channel, ",", state]);
impl_scpi_request!(SetTimerStateRequest, EmptyResponse);

// 10. SYSTem
// Command format SYSTem:ERRor?
// Description Query the error code and the information of the equipment.
// Typical Return 0 No Error
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SystemErrorRequest;
impl_scpi_serialize!(SystemErrorRequest, ["SYSTem:ERRor?"]);

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SystemErrorResponse {
    pub content: String,
}

impl ScpiDeserialize for SystemErrorResponse {
    fn deserialize(input: &mut &str) -> Result<Self, Error> {
        Ok(SystemErrorResponse {
            content: read_all(input)?,
        })
    }
}

impl_scpi_request!(SystemErrorRequest, SystemErrorResponse);

// Command format SYSTem:VERSion?
// Description Query the software version of the equipment
// Typical Return 1.01.01.01.02
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SystemVersionRequest;
impl_scpi_serialize!(SystemVersionRequest, ["SYSTem:VERSion?"]);

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SystemVersionResponse {
    pub version: String,
}

impl ScpiDeserialize for SystemVersionResponse {
    fn deserialize(input: &mut &str) -> Result<Self, Error> {
        Ok(SystemVersionResponse {
            version: input.to_string(),
        })
    }
}

impl_scpi_request!(SystemVersionRequest, SystemVersionResponse);

// Command format SYSTem:STATus?
// Description Query the current working state of the equipment.
// Typical Return 0x0224
// Note The return info is hexadecimal format, but the actual state is binary, so you
// must change the return info into a binary. The state correspondence
// relationship is as follow.
// Bit No Corresponding state
// 0 0: CH1 CV mode; 1: CH1 CC mode
// 1 0: CH2 CV mode; 1: CH2 CC mode
// 2,3 01: Independent mode; 10: Parallel mode
// 4 0: CH1 OFF; 1: CH1 ON
// 5 0: CH2 OFF; 1: CH2 ON
// 6 0: TIMER1 OFF; 1: TIMER1 ON
// 7 0: TIMER2 OFF; 1: TIMER2 ON
// 8 0: CH1 digital display; 1: CH1 waveform display
// 9 0: CH2 digital display; 1: CH2 waveform display
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SystemStatusRequest;
impl_scpi_serialize!(SystemStatusRequest, ["SYSTem:STATus?"]);

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ChannelMode {
    ConstantVoltage,
    ConstantCurrent,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DisplayMode {
    DigitalDisplay,
    WaveformDisplay,
}

impl From<bool> for DisplayMode {
    fn from(value: bool) -> Self {
        match value {
            true => DisplayMode::WaveformDisplay,
            false => DisplayMode::DigitalDisplay,
        }
    }
}

impl From<bool> for ChannelMode {
    fn from(value: bool) -> Self {
        match value {
            true => ChannelMode::ConstantCurrent,
            false => ChannelMode::ConstantVoltage,
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ChannelStatus {
    pub mode: ChannelMode,
    pub output: State,
    pub timer: State,
    pub display: DisplayMode,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SystemStatus {
    pub operation_mode: OperationMode,
    pub channel_one: ChannelStatus,
    pub channel_two: ChannelStatus,
}

impl SystemStatus {
    pub fn get(&self, channel: Channel) -> &ChannelStatus {
        match channel {
            Channel::One => &self.channel_one,
            Channel::Two => &self.channel_two,
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SystemStatusResponse {
    pub value: u16,
}
impl SystemStatusResponse {
    const CHANNEL_1_MODE_BIT: usize = 0;
    const CHANNEL_2_MODE_BIT: usize = 1;
    // const OPERATION_MODE_BITS: (usize, usize) = (2, 3);
    const CHANNEL_1_OUTPUT_STATE: usize = 4;
    const CHANNEL_2_OUTPUT_STATE: usize = 5;
    const TIMER_1_STATE: usize = 6;
    const TIMER_2_STATE: usize = 7;
    const CHANNEL_1_DISPLAY: usize = 8;
    const CHANNEL_2_DISPLAY: usize = 9;

    fn read_bit(&self, bit: usize) -> bool {
        let mask = 1 << bit;
        (mask & self.value) > 0
    }

    fn decode_operation_mode(&self) -> OperationMode {
        match self.value & 0b1100 {
            0b0100 => OperationMode::Independent,
            0b1000 => OperationMode::Parallel,
            0b1100 => OperationMode::Series,
            0b0000 => panic!("Received unexpected, invalid bit pattern `00` for operation mode!"),
            _ => unreachable!(),
        }
    }

    pub fn decode(&self) -> SystemStatus {
        SystemStatus {
            operation_mode: self.decode_operation_mode(),
            channel_one: ChannelStatus {
                mode: self.read_bit(Self::CHANNEL_1_MODE_BIT).into(),
                output: self.read_bit(Self::CHANNEL_1_OUTPUT_STATE).into(),
                timer: self.read_bit(Self::TIMER_1_STATE).into(),
                display: self.read_bit(Self::CHANNEL_1_DISPLAY).into(),
            },
            channel_two: ChannelStatus {
                mode: self.read_bit(Self::CHANNEL_2_MODE_BIT).into(),
                output: self.read_bit(Self::CHANNEL_2_OUTPUT_STATE).into(),
                timer: self.read_bit(Self::TIMER_2_STATE).into(),
                display: self.read_bit(Self::CHANNEL_2_DISPLAY).into(),
            },
        }
    }
}

lazy_static::lazy_static! {
    static ref REGEX_HEX_DIGIT: Regex = Regex::new(r"^[0-9a-fA-F]+").unwrap();
}

impl ScpiDeserialize for SystemStatusResponse {
    fn deserialize(input: &mut &str) -> Result<Self, Error> {
        match_literal(input, "0x")?;
        let value = u16::from_str_radix(read_prefix(input, &REGEX_HEX_DIGIT), 16).map_err(|e| {
            scpi_client::Error::ResponseDecoding(format!("Failed to parse hex: {e}"))
        })?;
        match_literal(input, "\n")?;
        Ok(SystemStatusResponse { value })
    }
}

impl_scpi_request!(SystemStatusRequest, SystemStatusResponse);

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct DottedDecimalNotationIpv4Addr(Ipv4Addr);

impl From<DottedDecimalNotationIpv4Addr> for Ipv4Addr {
    fn from(value: DottedDecimalNotationIpv4Addr) -> Self {
        value.0
    }
}

impl From<Ipv4Addr> for DottedDecimalNotationIpv4Addr {
    fn from(value: Ipv4Addr) -> Self {
        Self(value)
    }
}

// 11. IPaddr
// Command format IPaddr <IP address>
// Description Assign a static Internet Protocol (IP) address for the instrument
// Example IPaddr 10.11.13.214
// Note The command is invalid when the state of DHCP is on
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SetIpAddressRequest {
    pub addr: DottedDecimalNotationIpv4Addr,
}
impl_scpi_serialize!(SetIpAddressRequest, ["IPaddr ", addr]);
impl_scpi_request!(SetIpAddressRequest, EmptyResponse);

impl ScpiSerialize for DottedDecimalNotationIpv4Addr {
    fn serialize(&self, out: &mut String) {
        let result = format!(
            "{}.{}.{}.{}",
            self.0.octets()[0],
            self.0.octets()[1],
            self.0.octets()[2],
            self.0.octets()[3]
        );
        out.push_str(result.as_str());
    }
}

impl ScpiDeserialize for DottedDecimalNotationIpv4Addr {
    fn deserialize(input: &mut &str) -> Result<Self, Error> {
        let a = u8::deserialize(input)?;
        match_literal(input, ".")?;
        let b = u8::deserialize(input)?;
        match_literal(input, ".")?;
        let c = u8::deserialize(input)?;
        match_literal(input, ".")?;
        let d = u8::deserialize(input)?;
        Ok(DottedDecimalNotationIpv4Addr(Ipv4Addr::new(a, b, c, d)))
    }
}

// Command format IPaddr?
// Description Query the current IP address of the instrument
// Typical Return 10.11.13.214
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct GetIpAddressRequest;
impl_scpi_serialize!(GetIpAddressRequest, ["IPaddr?"]);

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct GetIpAddressResponse {
    pub address: Ipv4Addr,
}
impl ScpiDeserialize for GetIpAddressResponse {
    fn deserialize(input: &mut &str) -> Result<Self, Error> {
        let address = DottedDecimalNotationIpv4Addr::deserialize(input)?;
        match_literal(input, "\n")?;
        Ok(GetIpAddressResponse { address: address.0 })
    }
}

impl_scpi_request!(GetIpAddressRequest, GetIpAddressResponse);

// 12. MASKaddr
// Command format MASKaddr <NetMasK>
// Description Assign a subnet mask for the instrument
// Example MASKadd 255.255.255.0
// Note The command is invalid when the state of DHCP is on
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SetSubnetMaskRequest {
    pub mask: DottedDecimalNotationIpv4Addr,
}
impl_scpi_serialize!(SetSubnetMaskRequest, ["MASKaddr ", mask]);
impl_scpi_request!(SetSubnetMaskRequest, EmptyResponse);

// Command format MASKaddr?
// Description Query the current subnet mask of the instrument
// Typical Return 255.255.255.0
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct GetSubnetMaskRequest;
impl_scpi_serialize!(GetSubnetMaskRequest, ["MASKaddr?"]);

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct GetSubnetMaskResponse {
    pub mask: Ipv4Addr,
}
impl ScpiDeserialize for GetSubnetMaskResponse {
    fn deserialize(input: &mut &str) -> Result<Self, Error> {
        let mask = DottedDecimalNotationIpv4Addr::deserialize(input)?;
        match_literal(input, "\n")?;
        Ok(GetSubnetMaskResponse { mask: mask.0 })
    }
}

impl_scpi_request!(GetSubnetMaskRequest, GetSubnetMaskResponse);

// 13. GATEaddr
// Command format GATEaddr <GateWay>
// Description Assign a gateway for the instrument
// Example GATEaddr 10.11.13.1
// Note The command is invalid when the state of DHCP is on
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SetGatewayRequest {
    pub gateway: DottedDecimalNotationIpv4Addr,
}
impl_scpi_serialize!(SetGatewayRequest, ["GATEaddr ", gateway]);
impl_scpi_request!(SetGatewayRequest, EmptyResponse);

// Command format GATEaddr?
// Description Query the current gateway of the instrument
// Typical Return 10.11.13.1
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct GetGatewayRequest;
impl_scpi_serialize!(GetGatewayRequest, ["GATEaddr?"]);

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct GetGatewayResponse {
    pub gateway: Ipv4Addr,
}
impl ScpiDeserialize for GetGatewayResponse {
    fn deserialize(input: &mut &str) -> Result<Self, Error> {
        let gateway = DottedDecimalNotationIpv4Addr::deserialize(input)?;
        match_literal(input, "\n")?;
        Ok(GetGatewayResponse { gateway: gateway.0 })
    }
}

impl_scpi_request!(GetGatewayRequest, GetGatewayResponse);

// 14. DHCP
// Command format DHCP {ON|OFF}
// Description Assign the network parameters (such as the IP address) for the instrument
// automatically.
// Example DHCP ON
pub struct SetDhcpRequest {
    pub state: State,
}
impl_scpi_serialize!(SetDhcpRequest, ["DHCP ", state]);
impl_scpi_request!(SetDhcpRequest, EmptyResponse);

// Command format DHCP?
// Description Query whether the automatic network parameters configuration function is
// turn on
// Typical Return DHCP:ON
pub struct GetDhcpRequest;
impl_scpi_serialize!(GetDhcpRequest, ["DHCP?"]);

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct GetDhcpResponse {
    pub state: State,
}

impl ScpiDeserialize for GetDhcpResponse {
    fn deserialize(input: &mut &str) -> Result<Self, Error> {
        match_literal(input, "DHCP:")?;
        let state = State::deserialize(input)?;
        match_literal(input, "\n")?;
        Ok(GetDhcpResponse { state })
    }
}

impl_scpi_request!(GetDhcpRequest, GetDhcpResponse);

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_idn() {
        let request = IdentityRequest;
        let mut out = String::new();
        request.serialize(&mut out);
        assert_eq!(out, "*IDN?");
        let response = &mut "Siglent Technologies, SPD3303X, SPD00001130025, 1.01.01.01.02,V3.0\n";
        let response = IdentityResponse::deserialize(response).unwrap();
        assert_eq!(response.company_name, "Siglent Technologies");
        assert_eq!(response.model_number, "SPD3303X");
        assert_eq!(response.serial_number, "SPD00001130025");
        assert_eq!(response.software_version, "1.01.01.01.02");
        assert_eq!(response.hardware_version, "V3.0");
    }

    #[test]
    fn test_ip_deserialization() {
        let mut input: &str = "104.3.255.200";
        let result = DottedDecimalNotationIpv4Addr::deserialize(&mut input);
        assert_eq!(result.unwrap().0.to_string(), "104.3.255.200");
    }
}