remove all unwraps :)

This commit is contained in:
Vladan Popovic 2022-06-16 01:39:16 +02:00
parent 43424ba997
commit 17b54fbe3a
4 changed files with 11 additions and 6 deletions

View file

@ -16,6 +16,7 @@ opt-level = "z"
pio = ["esp-idf-sys/pio"] pio = ["esp-idf-sys/pio"]
[dependencies] [dependencies]
anyhow = "1.0.57"
embedded-hal = "0.2.7" embedded-hal = "0.2.7"
esp-idf-hal = "0.37.4" esp-idf-hal = "0.37.4"
esp-idf-sys = { version = "0.31.5", features = ["binstart"] } esp-idf-sys = { version = "0.31.5", features = ["binstart"] }

View file

@ -1,5 +1,3 @@
#[allow(dead_code)]
use std::time::Duration; use std::time::Duration;
pub struct Command { pub struct Command {

View file

@ -3,11 +3,12 @@ mod modem;
#[allow(dead_code)] #[allow(dead_code)]
mod command; mod command;
use anyhow;
use esp_idf_hal::prelude::*; use esp_idf_hal::prelude::*;
use esp_idf_hal::peripherals::Peripherals; use esp_idf_hal::peripherals::Peripherals;
use esp_idf_hal::serial; use esp_idf_hal::serial;
fn main() { fn main() -> anyhow::Result<()> {
esp_idf_sys::link_patches(); esp_idf_sys::link_patches();
let dp = Peripherals::take().unwrap(); let dp = Peripherals::take().unwrap();
@ -36,15 +37,17 @@ fn main() {
let modem_rst = dp.pins.gpio5.into_output().unwrap(); let modem_rst = dp.pins.gpio5.into_output().unwrap();
let modem_power = dp.pins.gpio23.into_output().unwrap(); let modem_power = dp.pins.gpio23.into_output().unwrap();
modem::init(modem_pwrkey, modem_rst, modem_power).unwrap(); modem::init(modem_pwrkey, modem_rst, modem_power)?;
let (tx, rx) = serial.split(); let (tx, rx) = serial.split();
let mut mdm = modem::Modem::new(tx, rx); let mut mdm = modem::Modem::new(tx, rx);
let _ = mdm.modem_info().unwrap(); let _ = mdm.modem_info()?;
let _ = mdm.connect_to_gprs_ap( let _ = mdm.connect_to_gprs_ap(
config::A1_GPRS_AP.apn, config::A1_GPRS_AP.apn,
config::A1_GPRS_AP.username, config::A1_GPRS_AP.username,
config::A1_GPRS_AP.password, config::A1_GPRS_AP.password,
).unwrap(); )?;
Ok(())
} }

View file

@ -2,6 +2,7 @@ use crate::command::Command;
use std::iter::FromIterator; use std::iter::FromIterator;
use std::thread; use std::thread;
use std::error::Error;
use std::time::{Duration, Instant}; use std::time::{Duration, Instant};
use embedded_hal::serial::{Read, Write}; use embedded_hal::serial::{Read, Write};
@ -21,6 +22,8 @@ pub enum ModemError {
SetupError(String), SetupError(String),
} }
impl Error for ModemError {}
impl std::fmt::Display for ModemError { impl std::fmt::Display for ModemError {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{:?}", self) write!(f, "{:?}", self)