diff --git a/src/gps.rs b/src/gps.rs index 717c605..ef3f90a 100644 --- a/src/gps.rs +++ b/src/gps.rs @@ -8,24 +8,22 @@ use std::{ }; use esp_idf_hal::prelude::*; -use esp_idf_hal::serial; -use esp_idf_hal::gpio::*; +use esp_idf_hal::serial::{self, Rx, Tx}; use ublox::*; use crate::modem::Msg; use crate::serial::SerialIO; -struct Device { +struct GpsModule { port: SerialIO, parser: Parser>, } -impl Device { - pub fn new(port: serial::Serial, Gpio13>) -> Device { +impl GpsModule { + pub fn new(tx: Tx, rx: Rx) -> Self { let parser = Parser::default(); - let (tx, rx) = port.split(); - Device { port: SerialIO::new(tx, rx), parser } + GpsModule { port: SerialIO::new(tx, rx), parser } } pub fn write_all(&mut self, data: &[u8]) -> std::io::Result<()> { @@ -39,6 +37,7 @@ impl Device { let mut local_buf = [0; 1024]; let nbytes = self.read_port(&mut local_buf)?; if nbytes == 0 { + println!("no bytes to read :("); break; } // parser.consume adds the buffer to its internal buffer, and @@ -52,7 +51,7 @@ impl Device { Ok(()) } - pub fn wait_for_ack(&mut self, timeout: Duration) -> std::io::Result<()> { + pub fn wait_for_ack(&mut self, timeout: Duration) -> std::io::Result { let mut found_packet = false; println!("LOOKING FOR ACK ..."); let start = Instant::now(); @@ -64,9 +63,13 @@ impl Device { found_packet = true; } } + else if let PacketRef::AckNak(nak) = packet { + println!("NAK PACKET: {} {}", nak.class(), nak.msg_id()); + } })?; } - Ok(()) + println!("exiting wait_for_ack"); + Ok(found_packet) } /// Reads the serial port, converting timeouts into "no data received" @@ -110,7 +113,8 @@ pub fn main( serial_config, )?; - let mut device = Device::new(serial); + let (tx, rx) = serial.split(); + let mut device = GpsModule::new(tx, rx); // Configure the device to talk UBX device @@ -127,25 +131,30 @@ pub fn main( reserved5: 0, } .into_packet_bytes(), - ) - .unwrap(); + )?; device.wait_for_ack::(Duration::from_millis(3000)).unwrap(); println!("CfgPrtUart acked!"); - // Enable the NavPosVelTime packet - device - .write_all( - &CfgMsgAllPortsBuilder::set_rate_for::([0, 0, 1, 0, 0, 0]) + // Set interval for the NavPosVelTime packet + println!("Sending set_rate_for:: ..."); + for i in 1..5 { + device + .write_all( + &CfgMsgAllPortsBuilder::set_rate_for::([0, 1, 1, 0, 0, 0]) .into_packet_bytes(), - ) - .unwrap(); - device.wait_for_ack::(Duration::from_millis(3000)).unwrap(); - println!("CfgMsgAllPorts acked!"); + ) + .unwrap(); + println!("SENT set_rate_for::({}) !!!", i); + if let Ok(true) = device.wait_for_ack::(Duration::from_millis(3000)) { + println!("Setting rate for NavPosVelTime acked! Exiting loop ..."); + break + } + } // Send a packet request for the MonVer packet - device - .write_all(&UbxPacketRequest::request_for::().into_packet_bytes()) - .unwrap(); + //device + // .write_all(&UbxPacketRequest::request_for::().into_packet_bytes()) + // .unwrap(); // Start reading data println!("Opened u-blox device, waiting for solutions..."); @@ -181,7 +190,8 @@ pub fn main( _ => { println!("{:?}", packet); } - })? + })?; + thread::sleep(Duration::from_millis(1000)); } println!("exiting GPS sender loop :)");