refactor: lcd-clock lazy updates

This commit is contained in:
cscherr 2025-05-20 11:20:33 +02:00
parent 93abdfb521
commit 87a8b04c35
Signed by: cscherrNT
GPG key ID: 8E2B45BC51A27EA7
3 changed files with 190 additions and 25 deletions

139
Cargo.lock generated
View file

@ -23,7 +23,7 @@ version = "0.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5deb64efa5bd81e31fcd1938615a6d98c82eafcbcd787162b6f63b91d6bac5b3"
dependencies = [
"rustc_version",
"rustc_version 0.2.3",
]
[[package]]
@ -58,13 +58,26 @@ checksum = "37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5"
[[package]]
name = "chrono"
version = "0.4.40"
version = "0.4.41"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1a7964611d71df112cb1730f2ee67324fcf4d0fc6606acbbe9bfe06df124637c"
checksum = "c469d952047f47f91b68d1cba3f10d63c11d73e4636f24f08daf0278abf01c4d"
dependencies = [
"num-traits",
]
[[package]]
name = "compile-time"
version = "0.2.0"
source = "git+https://github.com/cscherrNT/compile-time-rs#085aecbf1d9fd593b112fcf1b218ec8a6841d1df"
dependencies = [
"once_cell",
"proc-macro2",
"quote",
"rustc_version 0.4.1",
"semver 1.0.26",
"time",
]
[[package]]
name = "cortex-m"
version = "0.7.7"
@ -155,6 +168,15 @@ dependencies = [
"defmt 1.0.1",
]
[[package]]
name = "deranged"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c9e6a11ca8224451684bc0d7d5a7adbf8f2fd6887261a1cfc3c0432f9d4068e"
dependencies = [
"powerfmt",
]
[[package]]
name = "embedded-hal"
version = "0.2.7"
@ -202,6 +224,18 @@ dependencies = [
"stable_deref_trait",
]
[[package]]
name = "itoa"
version = "1.0.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4a5f13b858c8d314ee3e8f639011f7ccefe71f97f96e50151fb991f267928e2c"
[[package]]
name = "libc"
version = "0.2.172"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d750af042f7ef4f724306de029d18836c26c1765a54a6a3f094cbd23a7267ffa"
[[package]]
name = "nb"
version = "0.1.3"
@ -221,6 +255,8 @@ checksum = "8d5439c4ad607c3c23abf66de8c8bf57ba8adcd1f129e699851a6e43935d339d"
name = "nucleo-l053r8-blink"
version = "0.1.0"
dependencies = [
"chrono",
"compile-time",
"cortex-m",
"cortex-m-rt",
"defmt 1.0.1",
@ -254,6 +290,12 @@ dependencies = [
"num-traits",
]
[[package]]
name = "num-conv"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "51d515d32fb182ee37cda2ccdcb92950d6a3c2893aa280e540671c2cd0f3b1d9"
[[package]]
name = "num-integer"
version = "0.1.46"
@ -294,6 +336,21 @@ dependencies = [
"autocfg",
]
[[package]]
name = "num_threads"
version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c7398b9c8b70908f6371f47ed36737907c87c52af34c268fed0bf0ceb92ead9"
dependencies = [
"libc",
]
[[package]]
name = "once_cell"
version = "1.21.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d"
[[package]]
name = "panic-halt"
version = "1.0.0"
@ -310,6 +367,12 @@ dependencies = [
"defmt 0.3.100",
]
[[package]]
name = "powerfmt"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391"
[[package]]
name = "proc-macro-error-attr2"
version = "2.0.0"
@ -365,7 +428,16 @@ version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a"
dependencies = [
"semver",
"semver 0.9.0",
]
[[package]]
name = "rustc_version"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cfcb3a22ef46e85b45de6ee7e79d063319ebb6594faafcf1c225ea92ab6e9b92"
dependencies = [
"semver 1.0.26",
]
[[package]]
@ -377,12 +449,38 @@ dependencies = [
"semver-parser",
]
[[package]]
name = "semver"
version = "1.0.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "56e6fa9c48d24d85fb3de5ad847117517440f6beceb7798af16b4a87d616b8d0"
[[package]]
name = "semver-parser"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3"
[[package]]
name = "serde"
version = "1.0.219"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5f0e2c6ed6606019b4e29e69dbaba95b11854410e5347d525002456dbbb786b6"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.219"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b0276cf7f2c73365f7157c8123c21cd9a50fbbd844757af28ca1f5925fc2a00"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "stable_deref_trait"
version = "1.2.0"
@ -450,6 +548,39 @@ dependencies = [
"syn",
]
[[package]]
name = "time"
version = "0.3.41"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a7619e19bc266e0f9c5e6686659d394bc57973859340060a69221e57dbc0c40"
dependencies = [
"deranged",
"itoa",
"libc",
"num-conv",
"num_threads",
"powerfmt",
"serde",
"time-core",
"time-macros",
]
[[package]]
name = "time-core"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c9e9a38711f559d9e3ce1cdb06dd7c5b8ea546bc90052da6d06bb76da74bb07c"
[[package]]
name = "time-macros"
version = "0.2.22"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3526739392ec93fd8b359c8e98514cb3e8e021beb4e5f597b00a0221f8ed8a49"
dependencies = [
"num-conv",
"time-core",
]
[[package]]
name = "unicode-ident"
version = "1.0.18"

View file

@ -17,6 +17,8 @@ heapless = "0.8.0"
defmt = {version="1.0.1", optional= true}
defmt-rtt = {version="1.0.0", optional=true}
panic-probe = { version = "0.3", features = ["print-defmt"], optional=true }
compile-time = {git = "https://github.com/cscherrNT/compile-time-rs"}
chrono = { version = "0.4.41", default-features = false }
[profile.release]
debug = "full" # those are not on the board

View file

@ -1,6 +1,8 @@
#![no_main]
#![no_std]
use chrono::NaiveDateTime;
use defmt::info;
use heapless::String;
use panic_probe as _;
@ -36,6 +38,9 @@ type Lcd = HD44780<
>,
>;
const UNIXTIME_OF_COMPILATION: i64 = compile_time::unix_local!();
const BUF_SIZE: usize = 20;
#[entry]
fn main() -> ! {
let dp = pac::Peripherals::take().unwrap();
@ -65,9 +70,9 @@ fn main() -> ! {
let mut delay = cp.SYST.delay(rcc.clocks);
let mut rtc = Rtc::new(dp.RTC, &mut rcc, &pwr, None).unwrap();
let mut led = gpioa.pa5.into_push_pull_output();
let mut buf: String<BUF_SIZE> = String::new();
let mut lcd: Lcd = HD44780::new_4bit(rs, en, d4, d5, d6, d7, &mut delay)
.expect("could not init HD44780 driver");
lcd.set_display_mode(
@ -80,39 +85,66 @@ fn main() -> ! {
)
.expect("could not set display properties");
lcd.reset(&mut delay).expect("could not reset the lcd");
lcd.write_str("Hello world!", &mut delay)
.expect("could not write to LCD");
let mut buf: String<20> = String::new();
let start_time = chrono::DateTime::from_timestamp(UNIXTIME_OF_COMPILATION, 0)
.expect("The Compilation time was invalid")
.naive_utc();
info!("Compiled time: {}", UNIXTIME_OF_COMPILATION);
let mut rtc = Rtc::new(dp.RTC, &mut rcc, &pwr, Some(start_time)).expect("Could not setup RTC");
let mut i: u32 = 0;
let mut timestamp = rtc.now();
info!("First RTC time: {}", timestamp.and_utc().timestamp());
loop {
led.set_high().unwrap();
display_time(&mut rtc, &mut lcd, &mut delay, &mut buf);
timestamp = rtc.now();
display_time(
&timestamp,
&mut lcd,
&mut delay,
&mut buf,
timestamp.second() == 0 || i == 0,
);
led.set_low().unwrap();
delay.delay_ms(1000_u16);
delay.delay_us(990_u32);
i += 1;
}
}
fn display_time(rtc: &mut Rtc, lcd: &mut Lcd, delay: &mut Delay, buf: &mut String<20>) {
let timestamp = rtc.now();
fn display_time(
timestamp: &NaiveDateTime,
lcd: &mut Lcd,
delay: &mut Delay,
buf: &mut String<BUF_SIZE>,
full_update: bool,
) {
if full_update {
lcd.clear(delay).expect("could not clear the display");
lcd.clear(delay).expect("could not clear the display");
lcd.set_cursor_pos(0, delay)
.expect("could not move cursor to start");
lcd.set_cursor_pos(0, delay)
.expect("could not move cursor to start");
buf.clear();
write!(
buf,
" {:04}-{:02}-{:02} ",
timestamp.year(),
timestamp.month(),
timestamp.day()
)
.expect("could not format text content for display");
lcd.write_str(buf, delay).expect("could not write to LCD");
buf.clear();
write!(
buf,
" {:04}-{:02}-{:02} ",
timestamp.year(),
timestamp.month(),
timestamp.day()
)
.expect("could not format text content for display");
lcd.write_str(buf, delay).expect("could not write to LCD");
lcd.set_cursor_pos(40, delay)
.expect("could not move cursor to start");
buf.clear();
write!(buf, " (UTC) ",).expect("could not format text content for display");
lcd.write_str(buf, delay).expect("could not write to LCD");
}
lcd.set_cursor_pos(20, delay)
.expect("could not move cursor to line 2");