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
//! MSP structures

use prelude::v1::*;

#[derive(PackedStruct, Serialize, Deserialize, Debug, Copy, Clone)]
pub struct MspApiVersion {
    pub protocol_version: u8,
    pub api_version_major: u8,
    pub api_version_minor: u8,
}

#[derive(PackedStruct, Serialize, Deserialize, Debug, Copy, Clone)]
pub struct MspFlightControllerVariant {
    pub identifier: [u8; 4],
}

#[derive(PackedStruct, Serialize, Deserialize, Debug, Copy, Clone)]
pub struct MspFlightControllerVersion {
    pub major: u8,
    pub minor: u8,
    pub patch: u8,
}

#[derive(PackedStruct, Serialize, Deserialize, Debug, Copy, Clone)]
#[packed_struct(endian = "lsb")]
pub struct MspBoardInfo {
    pub board_id: [u8; 4],
    pub hardware_revision: u16,
    pub fc_type: u8,
}

#[derive(PackedStruct, Serialize, Deserialize, Debug, Copy, Clone)]
pub struct MspBuildInfo {
    pub date_str: [u8; 11],
    pub time_str: [u8; 8],
    pub git_str: [u8; 7],
}

#[derive(PackedStruct, Serialize, Deserialize, Debug, Copy, Clone)]
pub struct MspUniqueId {
    pub uid: [u8; 12],
}

#[derive(PackedStruct, Serialize, Deserialize, Debug, Copy, Clone)]
#[packed_struct(bytes = "1", endian = "lsb", bit_numbering = "msb0")]
pub struct MspAvailableSensors {
    #[packed_field(bits = "2")]
    pub sonar: bool,
    #[packed_field(bits = "4")]
    pub gps: bool,
    #[packed_field(bits = "5")]
    pub mag: bool,
    #[packed_field(bits = "6")]
    pub baro: bool,
    #[packed_field(bits = "7")]
    pub acc: bool,
}

#[derive(PackedStruct, Serialize, Deserialize, Debug, Copy, Clone)]
#[packed_struct(endian = "lsb")]
pub struct MspStatus {
    pub cycle_time: u16,
    pub i2c_errors: u16,
    #[packed_field(size_bits = "8")]
    pub sensors: MspAvailableSensors,
    pub null1: u8,
    pub flight_mode: u32,
    pub profile: u8,
    pub system_load: u16,
}

#[derive(PackedStruct, Serialize, Deserialize, Debug, Copy, Clone)]
#[packed_struct(endian = "lsb")]
pub struct MspStatusEx {
    pub cycle_time: u16,
    pub i2c_errors: u16,
    #[packed_field(size_bits = "8")]
    pub sensors: MspAvailableSensors,
    pub null1: u8,
    pub flight_mode: u32,
    pub current_pid_profile_index: u8,
    pub average_system_load_percent: u16,
    pub max_profile_count: u8,
    pub current_control_rate_profile_index: u8,
}

#[derive(PackedStruct, Serialize, Deserialize, Debug, Copy, Clone, Default)]
#[packed_struct(endian = "lsb")]
pub struct MspBfConfig {
    pub mixer_configuration: u8,
    pub features: u32,
    pub serial_rx_provider: u8,
    pub board_align_roll: i16,
    pub board_align_pitch: i16,
    pub board_align_yaw: i16,
    pub current_scale: i16,
    pub current_offset: i16,
}

#[derive(PackedStruct, Serialize, Deserialize, Debug, Copy, Clone)]
#[packed_struct(endian = "lsb")]
pub struct MspRawImu {
    pub acc_x: i16,
    pub acc_y: i16,
    pub acc_z: i16,
    pub gyro_x: i16,
    pub gyro_y: i16,
    pub gyro_z: i16,
    pub mag_x: i16,
    pub mag_y: i16,
    pub mag_z: i16,
}

#[derive(PackedStruct, Serialize, Deserialize, Debug, Copy, Clone)]
#[packed_struct(bytes = "1", endian = "lsb", bit_numbering = "msb0")]
pub struct MspDataFlashSummaryReply {
    #[packed_field(bits = "6")]
    pub supported: bool,
    #[packed_field(bits = "7")]
    pub ready: bool,
    pub sectors: u32,
    pub total_size_bytes: u32,
    pub used_size_bytes: u32,
}

#[derive(PackedStruct, Serialize, Deserialize, Debug, Copy, Clone)]
#[packed_struct(bytes = "1", endian = "lsb", bit_numbering = "msb0")]
pub struct MspDataFlashReply {
    pub read_address: u32,
    // pub payload: Vec<u8>, // TODO: packed_struct should support dynamic size too the end
}

#[derive(PackedStruct, Debug, Copy, Clone)]
#[packed_struct(bytes = "6", endian = "lsb", bit_numbering = "msb0")]
pub struct MspDataFlashRead {
    pub read_address: u32,
    pub read_length: u16,
}

#[derive(PackedStruct, Serialize, Deserialize, Debug, Copy, Clone)]
#[packed_struct(endian = "lsb")]
pub struct MspAccTrim {
    pub pitch: u16,
    pub roll: u16,
}

#[derive(PackedStruct, Serialize, Deserialize, Debug, Copy, Clone)]
#[packed_struct(endian = "lsb")]
pub struct MspIdent {
    pub version: u8,
    pub mixer_mode: u8,
    pub protocol_version: u8,
    pub capability: u32,
}

#[derive(PackedStruct, Serialize, Deserialize, Debug, Copy, Clone)]
#[packed_struct(endian = "lsb")]
pub struct MspMisc {
    pub rx_mid_rc: u16,
    pub min_throttle: u16,
    pub max_throttle: u16,
    pub min_command: u16,
    pub failsafe_throttle: u16,

    pub gps_type: u8,
    pub gps_baudrate: u8,
    pub gps_sbas_mode: u8,

    pub current_meter_output: u8,
    pub rssi_channel: u8,
    pub null1: u8,

    pub compass_mag_declination: u16,
}

#[derive(PackedStruct, Serialize, Deserialize, Debug, Copy, Clone)]
#[packed_struct(endian = "lsb")]
pub struct MspAttitude {
    pub roll: i16,
    pub pitch: i16,
    pub yaw: i16,
}

#[derive(PackedStruct, Serialize, Deserialize, Debug, Copy, Clone)]
#[packed_struct(endian = "lsb")]
pub struct MspAltitude {
    /// [centimeters]
    pub altitude: i32,
    /// variometer [cm/s]
    pub vario: i16,
}

#[derive(PackedStruct, Serialize, Deserialize, Debug, Copy, Clone)]
#[packed_struct(endian = "lsb")]
pub struct MspBatteryConfig {
    pub vbat_min_cell_voltage: u8,
    pub vbat_max_cell_voltage: u8,
    pub vbat_warning_cell_voltage: u8,
    pub battery_capacity: u16,
    pub voltage_meter_source: u8,
    pub current_meter_source: u8,
}

#[derive(PackedStruct, Serialize, Deserialize, Debug, Copy, Clone)]
#[packed_struct(endian = "lsb")]
pub struct MspAnalog {
    pub battery_voltage: u8,
    pub mah_drawn: u16,
    pub rssi: u16,
    /// Current in 0.01A steps, range is -320A to 320A
    pub amperage: i16,
}

#[derive(PackedStruct, Serialize, Deserialize, Debug, Copy, Clone)]
#[packed_struct(endian = "lsb")]
pub struct MspRssiConfig {
    pub rssi_channel: u8,
}

#[derive(PackedStruct, Serialize, Deserialize, Debug, Copy, Clone)]
pub struct MspVoltageMeter {
    pub id: u8,
    pub value: u8,
}

#[derive(PackedStruct, Serialize, Deserialize, Debug, Copy, Clone)]
#[packed_struct(endian = "lsb")]
pub struct MspCurrentMeter {
    pub id: u8,
    pub mah_drawn: u16,
    /// In 0.001A steps (mA)
    pub amperage: u16,
}

#[derive(PackedStruct, Serialize, Deserialize, Debug, Copy, Clone)]
#[packed_struct(endian = "lsb")]
pub struct MspBatteryState {
    pub battery_cell_count: u8,
    /// mAh
    pub battery_capacity: u16,

    pub battery_voltage: u8,
    pub mah_drawn: u16,
    /// 0.01A
    pub amperage: i16,

    pub alerts: u8,
}

#[derive(PackedStruct, Serialize, Deserialize, Debug, Copy, Clone, Default)]
#[packed_struct(endian = "lsb")]
pub struct MspRcTuning {
    pub rc_rate8: u8,
    pub rc_expo8: u8,

    pub rate_roll: u8,
    pub rate_pitch: u8,
    pub rate_yaw: u8,

    pub dyn_thr_pid: u8,
    pub thr_mid8: u8,
    pub thr_expo8: u8,
    pub tpa_breakpoint: u16,
    pub rc_yaw_expo8: u8,
    pub rc_yaw_rate8: u8,
}

#[derive(PackedStruct, Serialize, Deserialize, Debug, Copy, Clone, Default)]
#[packed_struct(endian = "lsb")]
pub struct MspRxConfig {
    pub serialrx_provider: u8,
    pub maxcheck: u16,
    pub midrc: u16,
    pub mincheck: u16,
    pub spektrum_sat_bind: u8,
    pub rx_min_usec: u16,
    pub rx_max_usec: u16,
    pub rc_interpolation: u8,
    pub rc_interpolation_interval: u8,
    pub air_mode_activate_threshold: u16,
    pub rx_spi_protocol: u8,
    pub rx_spi_id: u32,
    pub rx_spi_rf_channel_count: u8,
    pub fpv_cam_angle_degrees: u8,
}

#[derive(PackedStruct, Serialize, Deserialize, Debug, Copy, Clone, Default)]
#[packed_struct(endian = "lsb")]
pub struct MspRcChannelValue {
    pub value: u16,
}

#[derive(PrimitiveEnum, Serialize, Deserialize, Debug, Copy, Clone, PartialEq)]
pub enum MspRcChannel {
    /// Ailerons
    Roll = 0,
    /// Elevators
    Pitch = 1,
    /// Rudder
    Yaw = 2,
    Throttle = 3,
    Aux1 = 4,
    Aux2 = 5,
    Aux3 = 6,
    Aux4 = 7,
    Aux5 = 8,
    Aux6 = 9,
    Aux7 = 10,
    Aux8 = 11,
    Aux9 = 12,
    Aux10 = 13,
    Aux11 = 14,
    Aux12 = 15,
    Aux13 = 16,
    Aux14 = 17,
    Aux15 = 18,
    Aux16 = 19,
}

#[derive(PackedStruct, Serialize, Deserialize, Debug, Copy, Clone)]
pub struct MspRcMappedChannel {
    #[packed_field(size_bits = "8", ty = "enum")]
    pub channel: MspRcChannel,
}

#[derive(PackedStruct, Serialize, Deserialize, Debug, Copy, Clone, Default)]
pub struct MspFeatures {
    pub features: [bool; 32],
}

#[derive(PackedStruct, Serialize, Deserialize, Debug, Copy, Clone, Default)]
#[packed_struct(endian = "lsb")]
pub struct MspMotor {
    pub motors: [u16; 8],
}

#[derive(PackedStruct, Serialize, Deserialize, Debug, Copy, Clone, Default)]
#[packed_struct(endian = "lsb")]
pub struct MspMotor3DConfig {
    pub deadband_3d_low: u16,
    pub deadband_3d_high: u16,
    pub neutral_3d: u16,
}

#[derive(PackedStruct, Serialize, Deserialize, Debug, Copy, Clone, Default)]
#[packed_struct(endian = "lsb")]
pub struct MspMotorConfig {
    pub min_throttle: u16,
    pub max_throttle: u16,
    pub min_command: u16,
}

#[derive(PackedStruct, Serialize, Deserialize, Debug, Copy, Clone, Default)]
#[packed_struct(endian = "lsb")]
pub struct MspRcDeadband {
    pub deadband: u8,
    pub yaw_deadband: u8,
    pub alt_hold_deadband: u8,
    pub deadband_3d_throttle: u16,
}

#[derive(PackedStruct, Serialize, Deserialize, Debug, Copy, Clone, Default)]
#[packed_struct(endian = "lsb")]
pub struct MspSensorAlignment {
    pub gyro_alignment: u8,
    pub acc_alignment: u8,
    pub mag_alignment: u8,
}

#[derive(PackedStruct, Serialize, Deserialize, Debug, Copy, Clone, Default)]
#[packed_struct(endian = "lsb")]
pub struct MspAdvancedConfig {
    pub gyro_sync_denom: u8,
    pub pid_process_denom: u8,
    pub use_unsynced_pwm: u8,
    pub motor_pwm_protocol: u8,
    pub motor_pwm_rate: u16,
    pub digital_idle_offset_percent: u16,
    pub gyro_use_32khz: u8,
    pub motor_pwm_inversion: u8,
}

#[derive(PackedStruct, Serialize, Deserialize, Debug, Copy, Clone, Default)]
#[packed_struct(endian = "lsb")]
pub struct MspFilterConfig {
    pub gyro_soft_lpf_hz: u8,
    pub dterm_lpf_hz: u16,
    pub yaw_lpf_hz: u16,
    pub gyro_soft_notch_hz_1: u16,
    pub gyro_soft_notch_cutoff_1: u16,
    pub dterm_notch_hz: u16,
    pub dterm_notch_cutoff: u16,
    pub gyro_soft_notch_hz_2: u16,
    pub gyro_soft_notch_cutoff_2: u16,
}

#[derive(PackedStruct, Serialize, Deserialize, Debug, Copy, Clone, Default)]
#[packed_struct(endian = "lsb")]
pub struct MspPidAdvanced {
    pub _r1: u16,
    pub _r2: u16,
    pub _r3: u16,
    pub _r4: u8,
    pub vbat_pid_compensation: u8,
    pub setpoint_relax_ratio: u8,
    pub dterm_setpoint_weight: u8,
    pub _r5: u8,
    pub _r6: u8,
    pub _r7: u8,
    pub rate_accel_limit: u16,
    pub yaw_rate_accel_limit: u16,
    pub level_angle_limit: u8,
    pub level_sensitivity: u8,
}

#[derive(PackedStruct, Serialize, Deserialize, Debug, Copy, Clone, Default)]
#[packed_struct(endian = "lsb")]
pub struct MspSensorConfig {
    pub acc_hardware: u8,
    pub baro_hardware: u8,
    pub mag_hardware: u8,
}

#[derive(PackedStruct, Serialize, Deserialize, Debug, Copy, Clone, Default)]
#[packed_struct(endian = "lsb")]
pub struct MspServos {
    pub servos: [u16; 8],
}

#[derive(PackedStruct, Debug, Copy, Clone)]
#[packed_struct(bytes = "14", endian = "lsb", bit_numbering = "msb0")]
pub struct MspServoConfig {
    pub min: u16,
    pub max: u16,
    pub middle: u16,
    pub rate: i8,
    pub unused1: u8,
    pub unused2: u8,
    pub forward_from_channel: u8, // Depracted, set to 255 for backward compatibility
    pub reverse_input: u32, // Depracted, Input reversing is not required since it can be done on mixer level
}

#[derive(PackedStruct, Debug, Copy, Clone)]
#[packed_struct(bytes = "1", endian = "lsb", bit_numbering = "msb0")]
pub struct MspSetServoConfig {
    pub index: u8,
    #[packed_field(size_bytes = "14")]
    pub servo_config: MspServoConfig,
}

#[derive(PackedStruct, Serialize, Deserialize, Debug, Copy, Clone)]
#[packed_struct(endian = "lsb")]
pub struct MspMixerConfig {
    #[packed_field(size_bits = "8", ty = "enum")]
    pub mixer_mode: MixerMode,
}

#[derive(PackedStruct, Debug, Copy, Clone)]
#[packed_struct(bytes = "4", endian = "lsb", bit_numbering = "msb0")]
pub struct MspModeRange {
    pub box_id: u8,
    #[packed_field(size_bits = "8", ty = "enum")]
    pub aux_channel_index: MspRcChannel,
    pub start_step: u8,
    pub end_step: u8,
}

#[derive(PackedStruct, Debug, Copy, Clone)]
#[packed_struct(bytes = "5", endian = "lsb", bit_numbering = "msb0")]
pub struct MspSetModeRange {
    pub index: u8,
    #[packed_field(size_bytes = "4")]
    pub mode_range: MspModeRange,
}

#[derive(PrimitiveEnum, Serialize, Deserialize, Debug, Copy, Clone, PartialEq)]
pub enum MixerMode {
    Tri = 1,
    QuadPlus = 2,
    QuadX = 3,
    Bicopter = 4,
    Gimbal = 5,
    Y6 = 6,
    Hex6 = 7,
    FlyingWing = 8,
    Y4 = 9,
    Hex6X = 10,
    OctoX8 = 11,
}

#[derive(PackedStruct, Debug, Copy, Clone)]
#[packed_struct(bytes = "8", endian = "lsb", bit_numbering = "msb0")]
pub struct MspMotorMixer {
    pub throttle: u16,
    pub roll: u16,
    pub pitch: u16,
    pub yaw: u16,
}

#[derive(PackedStruct, Debug, Copy, Clone)]
#[packed_struct(bytes = "9", endian = "lsb", bit_numbering = "msb0")]
pub struct MspSetMotorMixer {
    pub index: u8,
    #[packed_field(size_bytes = "8")]
    pub motor_mixer: MspMotorMixer,
}

#[derive(PackedStruct, Debug, Copy, Clone)]
#[packed_struct(bytes = "13", endian = "lsb", bit_numbering = "msb0")]
pub struct MspOsdConfig {
    pub video_system: u8,
    pub units: u8,
    pub rssi_alarm: u8,
    pub capacity_warning: u16,
    pub time_alarm: u16,
    pub alt_alarm: u16,
    pub dist_alarm: u16,
    pub neg_alt_alarm: u16,
}

#[derive(PackedStruct, Debug, Copy, Clone)]
#[packed_struct(bytes = "1", endian = "lsb", bit_numbering = "msb0")]
pub struct MspSetGetOsdConfig {
    pub item_index: u8,
    #[packed_field(size_bytes = "13")]
    pub config: MspOsdConfig,
}

#[derive(PackedStruct, Debug, Copy, Clone)]
#[packed_struct(bytes = "2", endian = "lsb", bit_numbering = "msb0")]
pub struct MspOsdItemPosition {
    pub col: u8,
    pub row: u8,
}

#[derive(PackedStruct, Debug, Copy, Clone)]
#[packed_struct(bytes = "1", endian = "lsb", bit_numbering = "msb0")]
pub struct MspSetOsdLayout {
    pub item_index: u8,
    #[packed_field(size_bytes = "2")]
    pub item: MspOsdItemPosition,
}

// inav msp layout item
#[derive(PackedStruct, Debug, Copy, Clone)]
#[packed_struct(bytes = "1", endian = "lsb", bit_numbering = "msb0")]
pub struct MspSetOsdLayoutItem {
    pub layout_index: u8,
    #[packed_field(size_bytes = "3")]
    pub item: MspSetOsdLayout,
}

#[derive(Debug)]
pub struct MspOsdSettings {
    pub osd_support: u8,
    pub config: MspOsdConfig,
    pub item_positions: Vec<MspOsdItemPosition>,
}

#[derive(PackedStruct, Debug, Copy, Clone)]
#[packed_struct(bytes = "2", endian = "lsb", bit_numbering = "msb0")]
pub struct MspOsdLayouts {
    pub layout_count: u8,
    pub item_count: u8,
}

#[derive(PrimitiveEnum, Serialize, Deserialize, Debug, Copy, Clone, PartialEq)]
pub enum SerialIdentifier {
    None = 255,
    USART1 = 0,
    USART2 = 1,
    USART3 = 2,
    USART4 = 3,
    USART5 = 4,
    USART6 = 5,
    USART7 = 6,
    USART8 = 7,
    UsbVcp = 20,
    SoftSerial1 = 30,
    SoftSerial2 = 31,
}

impl TryFrom<u8> for SerialIdentifier {
    type Error = &'static str;

    fn try_from(value: u8) -> Result<Self, Self::Error> {
        let serial = match value {
            255 => SerialIdentifier::None,
            0 => SerialIdentifier::USART1,
            1 => SerialIdentifier::USART2,
            2 => SerialIdentifier::USART3,
            3 => SerialIdentifier::USART4,
            4 => SerialIdentifier::USART5,
            5 => SerialIdentifier::USART6,
            6 => SerialIdentifier::USART7,
            7 => SerialIdentifier::USART8,
            20 => SerialIdentifier::UsbVcp,
            30 => SerialIdentifier::SoftSerial1,
            31 => SerialIdentifier::SoftSerial2,
            _ => return Err("Serial identifier not found"),
        };

        Ok(serial)
    }
}

#[derive(PrimitiveEnum, Serialize, Deserialize, Debug, Copy, Clone, PartialEq)]
pub enum Baudrate {
    BaudAuto = 0,
    Baud1200 = 1,
    Baud2400 = 2,
    Baud4800 = 3,
    Baud9600 = 4,
    Baud19200 = 5,
    Baud38400 = 6,
    Baud57600 = 7,
    Baud115200 = 8,
    Baud230400 = 9,
    Baud250000 = 10,
    Baud460800 = 11,
    Baud921600 = 12,
    Baud1000000 = 13,
    Baud1500000 = 14,
    Baud2000000 = 15,
    Baud2470000 = 16,
}

impl TryFrom<&str> for Baudrate {
    type Error = &'static str;

    fn try_from(value: &str) -> Result<Self, Self::Error> {
        let baudrate = match value {
            "0" => Baudrate::BaudAuto,
            "1200" => Baudrate::Baud1200,
            "2400" => Baudrate::Baud2400,
            "4800" => Baudrate::Baud4800,
            "9600" => Baudrate::Baud9600,
            "19200" => Baudrate::Baud19200,
            "38400" => Baudrate::Baud38400,
            "57600" => Baudrate::Baud57600,
            "115200" => Baudrate::Baud115200,
            "230400" => Baudrate::Baud230400,
            "250000" => Baudrate::Baud250000,
            "460800" => Baudrate::Baud460800,
            "921600" => Baudrate::Baud921600,
            "1000000" => Baudrate::Baud1000000,
            "1500000" => Baudrate::Baud1500000,
            "2000000" => Baudrate::Baud2000000,
            "2470000" => Baudrate::Baud2470000,
            _ => return Err("Baudrate identifier not found"),
        };

        Ok(baudrate)
    }
}

impl From<Baudrate> for String {
    fn from(value: Baudrate) -> Self {
        match value {
            Baudrate::BaudAuto => "0",
            Baudrate::Baud1200 => "1200",
            Baudrate::Baud2400 => "2400",
            Baudrate::Baud4800 => "4800",
            Baudrate::Baud9600 => "9600",
            Baudrate::Baud19200 => "19200",
            Baudrate::Baud38400 => "38400",
            Baudrate::Baud57600 => "57600",
            Baudrate::Baud115200 => "115200",
            Baudrate::Baud230400 => "230400",
            Baudrate::Baud250000 => "250000",
            Baudrate::Baud460800 => "460800",
            Baudrate::Baud921600 => "921600",
            Baudrate::Baud1000000 => "1000000",
            Baudrate::Baud1500000 => "1500000",
            Baudrate::Baud2000000 => "2000000",
            Baudrate::Baud2470000 => "2470000",
        }
        .to_owned()
    }
}

#[derive(PackedStruct, Debug, Copy, Clone)]
#[packed_struct(endian = "lsb", bit_numbering = "msb0")]
pub struct MspSerialSetting {
    #[packed_field(size_bits = "8", ty = "enum")]
    pub identifier: SerialIdentifier,
    pub function_mask: u32,
    #[packed_field(size_bits = "8", ty = "enum")]
    pub msp_baudrate_index: Baudrate,
    #[packed_field(size_bits = "8", ty = "enum")]
    pub gps_baudrate_index: Baudrate,
    #[packed_field(size_bits = "8", ty = "enum")]
    pub telemetry_baudrate_index: Baudrate,
    #[packed_field(size_bits = "8", ty = "enum")]
    pub peripheral_baudrate_index: Baudrate,
}

#[derive(PackedStruct, Debug, Copy, Clone)]
#[packed_struct(bytes = "1", endian = "lsb", bit_numbering = "msb0")]
pub struct MspSetServoMixRule {
    pub index: u8,
    #[packed_field(size_bytes = "8")]
    pub servo_rule: MspServoMixRule,
}

#[derive(PackedStruct, Debug, Copy, Clone)]
#[packed_struct(bytes = "8", endian = "lsb", bit_numbering = "msb0")]
pub struct MspServoMixRule {
    pub target_channel: u8,
    pub input_source: u8,
    pub rate: u16,
    pub speed: u8,
    pub min: u8,
    pub max: u8,
    pub box_id: u8,
}

#[derive(PackedStruct, Debug, Copy, Clone)]
#[packed_struct(bytes = "1", endian = "lsb", bit_numbering = "msb0")]
pub struct MspSetServoMixer {
    pub index: u8,
    #[packed_field(size_bytes = "6")]
    pub servo_rule: MspServoMixer,
}

#[derive(PackedStruct, Debug, Copy, Clone)]
#[packed_struct(bytes = "6", endian = "lsb", bit_numbering = "msb0")]
pub struct MspServoMixer {
    pub target_channel: u8,
    pub input_source: u8,
    pub rate: i16,
    pub speed: u8,
    pub condition_id: i8,
}

#[derive(PackedStruct, Debug, Copy, Clone)]
#[packed_struct(endian = "lsb", bit_numbering = "msb0")]
pub struct MspRxMap {
    pub map: [u8; 4], // MAX_MAPPABLE_RX_INPUTS
}

#[derive(PackedStruct, Debug, Copy, Clone)]
#[packed_struct(endian = "lsb", bit_numbering = "msb0")]
pub struct MspSettingGroup {
    pub group_id: u16,
    pub start_id: u16,
    pub end_id: u16,
}

#[derive(PackedStruct, Debug, Copy, Clone)]
#[packed_struct(endian = "lsb", bit_numbering = "msb0")]
pub struct MspSettingInfoRequest {
    pub null: u8,
    pub id: u16,
}

#[derive(PrimitiveEnum, Serialize, Deserialize, Debug, Copy, Clone, PartialEq)]
pub enum SettingMode {
    ModeDirect = 0,
    ModeLookup = 0x40,
}

#[derive(PrimitiveEnum, Serialize, Deserialize, Debug, Copy, Clone, PartialEq)]
pub enum SettingType {
    VarUint8 = 0,
    VarInt8,
    VarUint16,
    VarInt16,
    VarUint32,
    #[cfg(feature = "suppport_int32_setting_type")]
    VarInt32,
    VarFloat,
    VarString,
}

#[derive(PackedStruct, Debug, Copy, Clone)]
#[packed_struct(bytes = "15", endian = "lsb", bit_numbering = "msb0")]
pub struct MspSettingInfo {
    // pub name: [u8; ?], null terminated strings

    // Parameter Group ID
    pub group_id: u16,

    // Type, section and mode
    #[packed_field(size_bits = "8", ty = "enum")]
    pub setting_type: SettingType,
    pub setting_section: u8,
    #[packed_field(size_bits = "8", ty = "enum")]
    pub setting_mode: SettingMode,

    pub min: u32,
    pub max: u32,

    // Absolute setting index
    pub absolute_index: u16,

    // If the setting is profile based, send the current one
    // and the count, both as uint8_t. For MASTER_VALUE, we
    // send two zeroes, so the MSP client can assume there
    pub profile_id: u8,
    pub profile_count: u8,
    // if setting uses enum values, it will be written here
    // pub enum_names: [String; ?] // TODO: packed_struct should support null terminated string parsing
    // pub value: [u8; ?]
}

#[test]
fn test_mixer() {
    use packed_struct::prelude::*;

    let m = MspMixerConfig {
        mixer_mode: MixerMode::QuadX,
    };
    assert_eq!(3, m.mixer_mode.to_primitive());
    let p = m.pack();
    assert_eq!(&[3], &p);
}