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
use std::fmt::{Display, Formatter};
use std::time::Duration;

pub(crate) mod conversion;
pub mod i2c;

pub(crate) const CLOCK_GENERATION_DELAY: Duration = Duration::from_millis(2);
pub(crate) const IRQ_TRIGGER_TO_READY_DELAY: Duration = Duration::from_millis(2);
pub(crate) const LIGHTNING_CALCULATION_DELAY: Duration = Duration::from_millis(2);
pub const DISTURBER_DEACTIVATION_PERIOD: Duration = Duration::from_millis(1500);
pub const APPROXIMATE_MINIMUM_LIGHTNING_INTERVAL: Duration = Duration::from_secs(1);

pub(crate) type Result<T> = ::std::result::Result<T, Error>;

#[derive(Debug)]
pub enum Error {
    Spi(::rppal::spi::Error),
    I2c(::rppal::i2c::Error),
}

impl ::std::error::Error for Error {}

impl Display for Error {
    fn fmt(&self, f: &mut Formatter) -> ::std::result::Result<(), ::std::fmt::Error> {
        match self {
            Error::Spi(e) => e.fmt(f),
            Error::I2c(e) => e.fmt(f),
        }
    }
}

impl From<::rppal::i2c::Error> for Error {
    fn from(error: ::rppal::i2c::Error) -> Self {
        Error::I2c(error)
    }
}

impl From<::rppal::spi::Error> for Error {
    fn from(error: ::rppal::spi::Error) -> Self {
        Error::Spi(error)
    }
}

pub(crate) enum Irq {
    DistanceEstimationChanged,
    /// INT_NH
    NoiseLevelTooHigh,
    /// INT_D
    DisturberDetected,
    /// INT_L
    Lightning,
}

pub(crate) trait Interface: Send {
    fn read(&mut self, register: Box<dyn crate::device::registers::Register>) -> Result<u8>;
    fn write(
        &mut self,
        register: Box<dyn crate::device::registers::Register>,
        payload: u8,
    ) -> Result<()>;
}

pub(crate) fn calculate_bitshift(mask: u8) -> u8 {
    for i in 0..7 {
        if (mask & (1 << i)) == 1 {
            return i;
        }
    }

    0
}