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
//! Semtech SX1276/77/78/79 device.
//!
//! See [`Device`] documentation for more details.
//!
//! [`Device`]: struct.Device.html

use conn::{self, Conn, Reset};
use core::fmt;
use core::ops::{Deref, DerefMut};
use mode::{FskOok, Lora, Mode, Undefined};

/// Semtech SX1276/77/78/79 device.
pub struct Device<C: Conn, M: Mode> {
  _mode: M,
  tokens: C,
}

/// The error returned from [`Device::reset`].
///
/// [`Device::reset`]: struct.Device.html#method.reset
pub struct ResetError<C: Reset<T>, T> {
  /// Device.
  pub device: Device<C, Undefined>,
  /// Timer.
  pub timer: T,
  /// Kind of the error.
  pub kind: C::ResetErrorKind,
}

impl<C: Conn, M: Mode> Device<C, M> {
  #[inline(always)]
  pub(crate) fn from_tokens(tokens: C) -> Self {
    Self {
      _mode: M::default(),
      tokens,
    }
  }

  /// Initializes connected pins.
  #[inline(always)]
  pub fn init(&self) {
    self.tokens.init();
  }

  /// Returns underlying connection bindings.
  #[inline(always)]
  pub fn into_tokens(self) -> C {
    let Self { _mode, tokens } = self;
    tokens
  }

  /// Runs a reset procedure, and returns a future for its result. The mode will
  /// be set to LoRa on success.
  pub fn reset<T>(
    self,
    timer: T,
  ) -> impl Future<Item = (Device<C, FskOok>, T), Error = ResetError<C, T>>
  where
    C: Reset<T>,
  {
    let future = self.into_tokens().reset(timer);
    AsyncFuture::new(move || {
      let (tokens, timer) = await!(future)?;
      Ok((Device::from_tokens(tokens), timer))
    })
  }
}

impl<C: Conn> Device<C, FskOok> {
  /// Creates a new `Device` in FSK/OOK mode from provided `tokens`.
  #[inline(always)]
  pub fn new(tokens: C) -> Self {
    Self::from_tokens(tokens)
  }

  /// Converts into LoRa mode.
  #[inline(always)]
  pub unsafe fn into_lora(self) -> Device<C, Lora> {
    Device::from_tokens(self.into_tokens())
  }
}

impl<C: Conn> Device<C, Lora> {
  /// Converts into FSK/OOK mode.
  #[inline(always)]
  pub unsafe fn into_fsk_ook(self) -> Device<C, FskOok> {
    Device::from_tokens(self.into_tokens())
  }
}

impl<C: Conn> Device<C, Undefined> {
  /// Converts into LoRa mode.
  #[inline(always)]
  pub unsafe fn into_lora(self) -> Device<C, Lora> {
    Device::from_tokens(self.into_tokens())
  }

  /// Converts into FSK/OOK mode.
  #[inline(always)]
  pub unsafe fn into_fsk_ook(self) -> Device<C, FskOok> {
    Device::from_tokens(self.into_tokens())
  }
}

impl<C: Reset<T>, T> From<conn::ResetError<C, T>> for ResetError<C, T> {
  #[inline(always)]
  fn from(error: conn::ResetError<C, T>) -> Self {
    let conn::ResetError {
      tokens,
      timer,
      kind,
    } = error;
    let device = Device::from_tokens(tokens);
    Self {
      device,
      timer,
      kind,
    }
  }
}

impl<C: Conn, M: Mode> Deref for Device<C, M> {
  type Target = C;

  #[inline(always)]
  fn deref(&self) -> &C {
    &self.tokens
  }
}

impl<C: Conn, M: Mode> DerefMut for Device<C, M> {
  #[inline(always)]
  fn deref_mut(&mut self) -> &mut C {
    &mut self.tokens
  }
}

impl<C: Reset<T>, T> fmt::Debug for ResetError<C, T> {
  #[inline(always)]
  fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
    self.kind.fmt(f)
  }
}

impl<C: Reset<T>, T> PartialEq for ResetError<C, T> {
  #[inline(always)]
  fn eq(&self, other: &Self) -> bool {
    self.kind.eq(&other.kind)
  }
}

impl<C: Reset<T>, T> Eq for ResetError<C, T> {}