nixie-clock-firmware/src/application/mod.rs

104 lines
2.6 KiB
Rust

use cortex_m::{asm::wfi, prelude::*};
use embedded_hal::digital::v2::{OutputPin, ToggleableOutputPin};
use nb::{self, block};
use nmea0183::Parser;
use stm32f1xx_hal::{
delay::Delay,
gpio::{gpioa, gpiob, gpioc, Alternate, Floating, Input, OpenDrain, Output, PushPull},
i2c::BlockingI2c,
pac::I2C1,
pac::SPI1,
prelude::*,
serial::Serial,
spi::{Spi, Spi1NoRemap},
stm32,
};
mod gps;
mod setup;
//use crate::exit;
pub use setup::setup;
use crate::time;
type AppSPI = Spi<
SPI1,
Spi1NoRemap,
(
gpioa::PA5<Alternate<PushPull>>,
gpioa::PA6<Input<Floating>>,
gpioa::PA7<Alternate<PushPull>>,
),
>;
pub struct App {
delay: Delay,
board_led: gpioc::PC13<Output<PushPull>>,
serial: Serial<
stm32::USART3,
(
gpiob::PB10<Alternate<PushPull>>,
gpiob::PB11<Input<Floating>>,
),
>,
gps_parser: Parser,
spi: AppSPI,
disp_strobe: gpioa::PA0<Output<PushPull>>,
}
fn fendangle_digit(num: u8) -> u8 {
if num <= 9 {
(9 - num).reverse_bits() >> 4
} else {
15
}
}
impl App {
pub fn run(mut self) -> ! {
defmt::info!("Application Startup!");
loop {
self.poll_gps();
let time = time::get();
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;
defmt::info!(
"Time: {} {} : {} {} : {} {}",
hours_high,
hours_low,
minutes_high,
minutes_low,
seconds_high,
seconds_low
);
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);
self.spi
.write(&[hours_byte, minutes_byte, hours_byte])
.unwrap();
defmt::debug!("Bytes: {=[u8]:x}", [hours_byte, minutes_byte, hours_byte]);
self.disp_strobe.set_high().unwrap();
self.delay.delay_ms(10u16);
self.disp_strobe.set_low().unwrap();
self.board_led.toggle().unwrap();
self.delay.delay_ms(240u16);
}
}
}