as3935_bbn/interface/
mod.rs

1use std::fmt::{Display, Formatter};
2use std::time::Duration;
3
4pub(crate) mod conversion;
5pub mod i2c;
6
7pub(crate) const CLOCK_GENERATION_DELAY: Duration = Duration::from_millis(2);
8pub(crate) const IRQ_TRIGGER_TO_READY_DELAY: Duration = Duration::from_millis(2);
9pub(crate) const LIGHTNING_CALCULATION_DELAY: Duration = Duration::from_millis(2);
10pub const DISTURBER_DEACTIVATION_PERIOD: Duration = Duration::from_millis(1500);
11pub const APPROXIMATE_MINIMUM_LIGHTNING_INTERVAL: Duration = Duration::from_secs(1);
12
13pub(crate) type Result<T> = ::std::result::Result<T, Error>;
14
15#[derive(Debug)]
16pub enum Error {
17    Spi(::rppal::spi::Error),
18    I2c(::rppal::i2c::Error),
19}
20
21impl ::std::error::Error for Error {}
22
23impl Display for Error {
24    fn fmt(&self, f: &mut Formatter) -> ::std::result::Result<(), ::std::fmt::Error> {
25        match self {
26            Error::Spi(e) => e.fmt(f),
27            Error::I2c(e) => e.fmt(f),
28        }
29    }
30}
31
32impl From<::rppal::i2c::Error> for Error {
33    fn from(error: ::rppal::i2c::Error) -> Self {
34        Error::I2c(error)
35    }
36}
37
38impl From<::rppal::spi::Error> for Error {
39    fn from(error: ::rppal::spi::Error) -> Self {
40        Error::Spi(error)
41    }
42}
43
44pub(crate) enum Irq {
45    DistanceEstimationChanged,
46    /// INT_NH
47    NoiseLevelTooHigh,
48    /// INT_D
49    DisturberDetected,
50    /// INT_L
51    Lightning,
52}
53
54pub(crate) trait Interface: Send {
55    fn read(&mut self, register: Box<dyn crate::device::registers::Register>) -> Result<u8>;
56    fn write(
57        &mut self,
58        register: Box<dyn crate::device::registers::Register>,
59        payload: u8,
60    ) -> Result<()>;
61}
62
63pub(crate) fn calculate_bitshift(mask: u8) -> u8 {
64    for i in 0..7 {
65        if (mask & (1 << i)) == 1 {
66            return i;
67        }
68    }
69
70    0
71}