Skip to main content

as3935_generic/
data.rs

1use core::default;
2
3// no_std support
4#[allow(unused_imports)]
5#[allow(dead_code)]
6// use libm::{exp, round, trunc};
7use log::debug;
8use bitfield::bitfield;
9// use const_builder::ConstBuilder;
10use log::info;
11
12
13#[allow(unused_imports)] // for no_std use
14
15
16/// A measurement result from the sensor.
17#[derive(Debug, PartialEq, Clone, Copy)]
18pub struct Measurements {
19    /// distance to storm front in km
20    pub distanceToStorm: f64,
21    /// energy of lightning strike in no specific units (see datasheet)
22    pub energyStrike: f64,
23}
24
25/// location of sensor 
26#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Default)]
27#[allow(non_camel_case_types)]
28#[repr(u8)]
29pub enum Location {
30    #[default]
31    Indoor, 
32    Outdoor,  
33}
34
35
36#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Default)]
37#[allow(non_camel_case_types)]
38#[repr(u8)]
39pub enum LocationMask {
40    #[default]
41    Indoor =   0x09,   // was 0x12 shift right 1, bitfield shifts it
42    Outdoor =  0x07,   // was 0x0e shift right 1, bitfield shifts it
43}
44
45impl From<u8> for LocationMask {
46    fn from(v: u8) -> Self {
47        match (v) {
48            0x09 => Self::Indoor,
49            0x07 => Self::Outdoor,
50            _ => unreachable!(),
51        }
52    }
53}
54
55
56bitfield! {
57    /// AS3935 Reg 0x00 AFE_GAIN register
58    pub struct AFE_GAIN(u8);
59    impl Debug;
60
61    pub  bool, get_powerdown, set_powerdown: 0;  // force power down state with = 1
62    pub  into LocationMask, get_location_mask, set_location_mask: 5,1;   // AFE Gain boost for indoor/outdoor lcoation
63
64}
65
66bitfield! {
67    /// AS3935 Reg 0x01 Thresholds
68    pub struct THRESHOLDS(u8);
69    impl Debug;
70
71    pub u8, get_noise_floor, set_noise_floor: 6, 4; // Noise Floor level
72    pub u8, get_wd_threshold, set_wd_threshold: 3, 0;  // Watchdog threshold
73
74}
75
76#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Default)]
77#[allow(non_camel_case_types)]
78#[repr(u8)]
79/// Minimum Lightning Strikes to trigger interrupt
80pub enum MinStrikes {
81    #[default]
82    ONE =   0x00,  /// 1
83    FIVE =   0x01,  /// 5
84    NINE  =   0x02,  /// 9
85    SIXTEEN  =   0x03,  // 16
86}
87
88impl From<u8> for MinStrikes {
89    fn from(v: u8) -> Self {
90        match (v) {
91            0x00 => Self::ONE,
92            0x01 => Self::FIVE,
93            0x02 => Self::NINE,
94            0x03 => Self::SIXTEEN,
95            _ => Self::ONE,
96        }
97    }
98}
99
100bitfield! {
101    /// AS3935 Lightning register settings
102    pub struct LightningReg(u8);
103    impl Debug;
104
105    pub bool, get_clear_stats, set_clear_stats: 6;    // Clear statistics
106    pub into MinStrikes, get_min_strikes, set_min_strikes: 5, 4; // minimum number of lightning strike, default is 0b00        
107    pub u8, get_spike_reject, set_spike_reject: 3, 0;   // spike rejection , default 0b0010
108
109}
110
111#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Default)]
112#[allow(non_camel_case_types)]
113#[repr(u8)]
114/// Interrupt Type : what triggered interrupt
115pub enum INTType {
116    #[default]
117    NoiseHigh = 0b0001,
118    Disturber = 0b0100,
119    Lightning = 0b1000,
120    Nothing   = 0b0000,
121}
122
123impl From<u8> for INTType {
124    fn from(v: u8) -> Self {
125        match (v) {
126            0b0001 => Self::NoiseHigh,
127            0b0100 => Self::Disturber,
128            0b1000 => Self::Lightning,
129            0b0000 => Self::Nothing,
130            _ => unreachable!(),
131        }
132    }
133}
134
135bitfield! {
136    /// AS3935 INT registry bits
137    pub struct INTReg(u8);
138    impl Debug;
139
140    pub u8, get_freq_div, set_freq_div: 7, 6;   // Frequncy division ration for antenna tuning
141    pub bool, get_mask_dist, set_mask_dist: 5;  // mask disturber
142    pub into INTType, get_int_type, _: 3, 0;    // interrupt type
143}
144
145
146#[derive(Debug, Clone, Copy, PartialEq, Eq)]
147/// Storm Front Distance
148pub enum StormFrontDistance {
149    /// storm front is out of range
150    OutOfRange,
151    /// storm front is in 5-40 km range
152    Range_km(u8),
153    /// storm front is overhead
154    Overhead,
155}
156
157// lightning Strike Enum  ...phase this out
158// #[derive(Debug, Clone, Copy, PartialEq, Eq)]
159// pub enum StrikeEnum {
160//     Lightning(StormFrontDistance),
161//     ExcessiveNoise,
162//     Disturber,
163// }
164
165/// which internal oscillator
166#[derive(Debug, Clone, Copy, PartialEq, Eq)]
167#[repr(u8)]
168pub enum Oscillator {
169    TRCO = 0b00100000,  //  System RCO at 32.768kHz
170    SRCO = 0b01000000,  //  Timer RCO Oscillators 1.1MHz
171    LCO  = 0b10000000,  //  Frequency of the Antenna
172}
173