as3935/interface/
conversion.rs1use crate::interface::i2c::I2cAddress;
2use crate::interface::Irq;
3use crate::{
4 HeadOfStormDistance, IgnoreDisturbances, MinimumLightningThreshold, NoiseFloorThreshold,
5 SensorPlacing, SignalVerificationThreshold,
6};
7
8impl From<u8> for Irq {
9 fn from(irq: u8) -> Self {
10 match irq {
11 0b_0000 => Irq::DistanceEstimationChanged,
12 0b_0001 => Irq::NoiseLevelTooHigh,
13 0b_0100 => Irq::DisturberDetected,
14 0b_1000 => Irq::Lightning,
15 _ => panic!(),
16 }
17 }
18}
19
20impl Into<u8> for MinimumLightningThreshold {
21 fn into(self) -> u8 {
22 match self {
23 MinimumLightningThreshold::One => 0b_00_u8,
24 MinimumLightningThreshold::Five => 0b_01_u8,
25 MinimumLightningThreshold::Nine => 0b_10_u8,
26 MinimumLightningThreshold::Sixteen => 0b_11_u8,
27 }
28 }
29}
30
31impl From<u8> for HeadOfStormDistance {
32 fn from(raw_distance: u8) -> Self {
33 match raw_distance {
34 0b_11_1111 => HeadOfStormDistance::OutOfRange,
35 0b_10_1000 => HeadOfStormDistance::Kilometers(40),
36 0b_10_0101 => HeadOfStormDistance::Kilometers(37),
37 0b_10_0010 => HeadOfStormDistance::Kilometers(34),
38 0b_01_1111 => HeadOfStormDistance::Kilometers(31),
39 0b_01_1011 => HeadOfStormDistance::Kilometers(27),
40 0b_01_1000 => HeadOfStormDistance::Kilometers(24),
41 0b_01_0100 => HeadOfStormDistance::Kilometers(20),
42 0b_01_0001 => HeadOfStormDistance::Kilometers(17),
43 0b_00_1110 => HeadOfStormDistance::Kilometers(14),
44 0b_00_1100 => HeadOfStormDistance::Kilometers(12),
45 0b_00_1010 => HeadOfStormDistance::Kilometers(10),
46 0b_00_1000 => HeadOfStormDistance::Kilometers(8),
47 0b_00_0110 => HeadOfStormDistance::Kilometers(6),
48 0b_00_0101 => HeadOfStormDistance::Kilometers(5),
49 0b_00_0001 => HeadOfStormDistance::Overhead,
50 _ => panic!(),
51 }
52 }
53}
54
55impl Into<u8> for SensorPlacing {
56 fn into(self) -> u8 {
57 match self {
58 SensorPlacing::Indoor => 0b_1_0010_u8,
59 SensorPlacing::Outdoor => 0b_0_1110_u8,
60 }
61 }
62}
63
64impl Into<u8> for SignalVerificationThreshold {
65 fn into(self) -> u8 {
66 self.0
67 }
68}
69
70impl Into<u8> for NoiseFloorThreshold {
71 fn into(self) -> u8 {
72 self.0
73 }
74}
75
76impl Into<u8> for IgnoreDisturbances {
77 fn into(self) -> u8 {
78 match self {
79 IgnoreDisturbances::Yes => 0b_1,
80 IgnoreDisturbances::No => 0b_0,
81 }
82 }
83}
84
85impl Into<u16> for I2cAddress {
86 fn into(self) -> u16 {
87 return self.0 as u16;
88 }
89}