diff --git a/src/application/gps.rs b/src/application/gps.rs index 3a7b355..90a9448 100644 --- a/src/application/gps.rs +++ b/src/application/gps.rs @@ -12,6 +12,7 @@ impl App { if let Some(result) = self.gps_parser.parse_from_byte(byte) { match result { Ok(ParseResult::GGA(Some(gga))) => { + defmt::info!("Got a GGA, setting time"); time::set(&gga.time); } Ok(_) => {} // Some other sentences.. diff --git a/src/application/mod.rs b/src/application/mod.rs index d166f2d..f63c22d 100644 --- a/src/application/mod.rs +++ b/src/application/mod.rs @@ -1,5 +1,5 @@ use cortex_m::{asm::wfi, prelude::*}; -use embedded_hal::digital::v2::{OutputPin, ToggleableOutputPin}; +use embedded_hal::digital::v2::{InputPin, OutputPin, ToggleableOutputPin}; use nb::{self, block}; use nmea0183::Parser; use stm32f1xx_hal::{ @@ -36,14 +36,17 @@ pub struct App { delay: Delay, board_led: gpioc::PC13>, serial: Serial< - stm32::USART3, + stm32::USART1, ( - gpiob::PB10>, - gpiob::PB11>, + gpioa::PA9>, + gpioa::PA10>, ), >, gps_parser: Parser, + button1: gpiob::PB0>, + button2: gpiob::PB1>, + spi: AppSPI, disp_strobe: gpioa::PA0>, } @@ -60,44 +63,69 @@ impl App { pub fn run(mut self) -> ! { defmt::info!("Application Startup!"); + let mut cnt = 0; + + let mut utc_offset = 2; + let mut prev_button1_state = self.button1.is_high().unwrap(); + let mut prev_button2_state = self.button2.is_high().unwrap(); + loop { self.poll_gps(); - let time = time::get(); + if prev_button1_state && !self.button1.is_high().unwrap() { + utc_offset = if utc_offset == 0 { 23 } else { utc_offset - 1 }; + defmt::info!("Button 1 pressed, new offset: {}", utc_offset); + } + if prev_button2_state && !self.button2.is_high().unwrap() { + utc_offset = if utc_offset == 23 { 0 } else { utc_offset + 1 }; + defmt::info!("Button 2 pressed, new offset: {}", utc_offset); + } + prev_button1_state = self.button1.is_high().unwrap(); + prev_button2_state = self.button2.is_high().unwrap(); - let hours_high = (time.hours as u8) / 10; - let hours_low = (time.hours as u8) % 10; - let minutes_high = (time.minutes as u8) / 10; - let minutes_low = (time.minutes as u8) % 10; - let seconds_high = (time.seconds as u8) / 10; - let seconds_low = (time.seconds as u8) % 10; + if cnt >= 250 { + let mut time = time::get(); + time.hours = (time.hours + utc_offset) % 24; - defmt::info!( - "Time: {} {} : {} {} : {} {}", - hours_high, - hours_low, - minutes_high, - minutes_low, - seconds_high, - seconds_low - ); + let hours_high = (time.hours as u8) / 10; + let hours_low = (time.hours as u8) % 10; + let minutes_high = (time.minutes as u8) / 10; + let minutes_low = (time.minutes as u8) % 10; + let seconds_high = (time.seconds as u8) / 10; + let seconds_low = (time.seconds as u8) % 10; - let hours_byte = fendangle_digit(hours_high) << 4 | fendangle_digit(hours_low); - let minutes_byte = fendangle_digit(minutes_high) << 4 | fendangle_digit(minutes_low); - let seconds_byte = fendangle_digit(seconds_high) << 4 | fendangle_digit(seconds_low); + defmt::info!( + "Display Time: {} {} : {} {} : {} {}", + hours_high, + hours_low, + minutes_high, + minutes_low, + seconds_high, + seconds_low + ); - self.spi - .write(&[hours_byte, minutes_byte, hours_byte]) - .unwrap(); + let hours_byte = fendangle_digit(hours_high) << 4 | fendangle_digit(hours_low); + let minutes_byte = + fendangle_digit(minutes_high) << 4 | fendangle_digit(minutes_low); + let seconds_byte = + fendangle_digit(seconds_high) << 4 | fendangle_digit(seconds_low); - defmt::debug!("Bytes: {=[u8]:x}", [hours_byte, minutes_byte, hours_byte]); + self.spi + .write(&[seconds_byte, minutes_byte, hours_byte]) + .unwrap(); - self.disp_strobe.set_high().unwrap(); - self.delay.delay_ms(10u16); - self.disp_strobe.set_low().unwrap(); + defmt::debug!("Bytes: {=[u8]:x}", [hours_byte, minutes_byte, hours_byte]); - self.board_led.toggle().unwrap(); - self.delay.delay_ms(240u16); + self.disp_strobe.set_high().unwrap(); + self.delay.delay_ms(1u16); + self.disp_strobe.set_low().unwrap(); + + //self.board_led.toggle().unwrap(); + + cnt = 10; + } + cnt += 1; + self.delay.delay_ms(1u16); } } } diff --git a/src/application/setup.rs b/src/application/setup.rs index c0a4731..dcc3e11 100644 --- a/src/application/setup.rs +++ b/src/application/setup.rs @@ -42,21 +42,24 @@ pub fn setup(cp: cortex_m::peripheral::Peripherals, dp: stm32::Peripherals) -> A let delay = Delay::new(cp.SYST, clocks); - // USART3 - // Configure pb10 as a push_pull output, this will be the tx pin - let tx = gpiob.pb10.into_alternate_push_pull(&mut gpiob.crh); - // Take ownership over pb11 - let rx = gpiob.pb11; + let button1 = gpiob.pb0.into_floating_input(&mut gpiob.crl); + let button2 = gpiob.pb1.into_floating_input(&mut gpiob.crl); + + // USART1 + // Configure pa9 as a push_pull output, this will be the tx pin + let tx = gpioa.pa9.into_alternate_push_pull(&mut gpioa.crh); + // Take ownership over pa10 + let rx = gpioa.pa10; // Set up the usart device. Taks ownership over the USART register and tx/rx pins. The rest of // the registers are used to enable and configure the device. - let serial = Serial::usart3( - dp.USART3, + let serial = Serial::usart1( + dp.USART1, (tx, rx), &mut afio.mapr, Config::default().baudrate(9600.bps()), clocks, - &mut rcc.apb1, + &mut rcc.apb2, ); let gps_parser = Parser::new(); @@ -88,6 +91,8 @@ pub fn setup(cp: cortex_m::peripheral::Peripherals, dp: stm32::Peripherals) -> A board_led, serial, gps_parser, + button1, + button2, spi, disp_strobe, }