ublox_cellular/
config.rs

1use crate::APNInfo;
2use embedded_hal::digital::{InputPin, OutputPin};
3use heapless::String;
4
5pub struct NoPin;
6
7impl InputPin for NoPin {
8    type Error = ();
9
10    fn try_is_high(&self) -> Result<bool, Self::Error> {
11        Err(())
12    }
13
14    fn try_is_low(&self) -> Result<bool, Self::Error> {
15        Err(())
16    }
17}
18
19impl OutputPin for NoPin {
20    type Error = core::convert::Infallible;
21
22    fn try_set_low(&mut self) -> Result<(), Self::Error> {
23        Ok(())
24    }
25
26    fn try_set_high(&mut self) -> Result<(), Self::Error> {
27        Ok(())
28    }
29}
30
31#[derive(Debug)]
32pub struct Config<RST, DTR, PWR, VINT> {
33    pub(crate) rst_pin: Option<RST>,
34    pub(crate) dtr_pin: Option<DTR>,
35    pub(crate) pwr_pin: Option<PWR>,
36    pub(crate) vint_pin: Option<VINT>,
37    pub(crate) baud_rate: u32,
38    pub(crate) hex_mode: bool,
39    pub(crate) flow_control: bool,
40    pub(crate) apn_info: APNInfo,
41    pub(crate) pin: String<4>,
42}
43
44impl Default for Config<NoPin, NoPin, NoPin, NoPin> {
45    fn default() -> Self {
46        Config {
47            rst_pin: None,
48            dtr_pin: None,
49            pwr_pin: None,
50            vint_pin: None,
51            baud_rate: 115_200_u32,
52            hex_mode: true,
53            flow_control: false,
54            apn_info: APNInfo::default(),
55            pin: String::new(),
56        }
57    }
58}
59
60impl<RST, DTR, PWR, VINT> Config<RST, DTR, PWR, VINT>
61where
62    RST: OutputPin,
63    PWR: OutputPin,
64    DTR: OutputPin,
65    VINT: InputPin,
66{
67    pub fn new(pin: &str) -> Self {
68        Config {
69            rst_pin: None,
70            dtr_pin: None,
71            pwr_pin: None,
72            vint_pin: None,
73            baud_rate: 115_200_u32,
74            hex_mode: true,
75            flow_control: false,
76            apn_info: APNInfo::default(),
77            pin: String::from(pin),
78        }
79    }
80
81    pub fn with_rst(self, rst_pin: RST) -> Self {
82        Config {
83            rst_pin: Some(rst_pin),
84            ..self
85        }
86    }
87
88    pub fn with_pwr(self, pwr_pin: PWR) -> Self {
89        Config {
90            pwr_pin: Some(pwr_pin),
91            ..self
92        }
93    }
94
95    pub fn with_dtr(self, dtr_pin: DTR) -> Self {
96        Config {
97            dtr_pin: Some(dtr_pin),
98            ..self
99        }
100    }
101
102    pub fn with_vint(self, vint_pin: VINT) -> Self {
103        Config {
104            vint_pin: Some(vint_pin),
105            ..self
106        }
107    }
108
109    pub fn baud_rate<B: Into<u32>>(self, baud_rate: B) -> Self {
110        // FIXME: Validate baudrates
111
112        Config {
113            baud_rate: baud_rate.into(),
114            ..self
115        }
116    }
117
118    pub fn with_flow_control(self) -> Self {
119        Config {
120            flow_control: true,
121            ..self
122        }
123    }
124
125    pub fn with_apn_info(self, apn_info: APNInfo) -> Self {
126        Config { apn_info, ..self }
127    }
128
129    pub fn pin(&self) -> &str {
130        &self.pin
131    }
132}