Skip to content

Commit

Permalink
build: create file with current datetime on build
Browse files Browse the repository at this point in the history
The real-time clock needs an initial date. Extend build.rs to write a file with
the local datetime into OUT_DIR.
  • Loading branch information
cmnord committed Sep 4, 2023
1 parent e0f50ba commit dd0f259
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 9 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Expand Up @@ -23,6 +23,9 @@ rp-pico = "0.8"
# rp2040-hal = { version="0.9", features=["rt", "critical-section-impl"] }
# rp2040-boot2 = "0.3"

[build-dependencies]
chrono = "0.4"

# cargo build/run
[profile.dev]
codegen-units = 1
Expand Down
48 changes: 48 additions & 0 deletions build.rs
Expand Up @@ -7,7 +7,11 @@
//! Cargo re-run the build script whenever `memory.x` is changed,
//! updating `memory.x` ensures a rebuild of the application with the
//! new memory settings.
//!
//! The build script also stores the current time in a Rust file so the
//! program can set the real-time clock.

use chrono::{self, Datelike, Timelike};
use std::env;
use std::fs::File;
use std::io::Write;
Expand All @@ -28,4 +32,48 @@ fn main() {
// here, we ensure the build script is only re-run when
// `memory.x` is changed.
println!("cargo:rerun-if-changed=memory.x");

let now = chrono::Local::now();

let mut file = File::create(out.join("build_time.rs")).unwrap();
write!(
file,
"// This file is generated by build.rs and should not be modified manually.
/// Structure containing date and time information
pub struct DateTime {{
/// 0..4095
pub year: u16,
/// 1..12, 1 is January
pub month: u8,
/// 1..28,29,30,31 depending on month
pub day: u8,
///
pub day_of_week: u8,
/// 0..23
pub hour: u8,
/// 0..59
pub minute: u8,
/// 0..59
pub second: u8,
}}
pub const BUILD_TIME: DateTime = DateTime {{
year: {},
month: {},
day: {},
day_of_week: {},
hour: {},
minute: {},
second: {},
}};",
now.year(),
now.month(),
now.day(),
now.weekday().number_from_sunday(),
now.hour(),
now.minute(),
now.second()
)
.unwrap();
}
18 changes: 17 additions & 1 deletion src/digit.rs
Expand Up @@ -6,6 +6,22 @@ pub type DigitArray = [[u8; NUM_ROWS]; NUM_COLS];
// X is an alias for readability
const X: u8 = 1;

const DIGIT_ZERO: DigitArray = transpose([
[0, X, X, 0],
[X, X, X, X],
[X, 0, 0, X],
[X, 0, 0, X],
[X, 0, 0, X],
[X, 0, 0, X],
[X, 0, 0, X],
[X, 0, 0, X],
[X, 0, 0, X],
[X, 0, 0, X],
[X, 0, 0, X],
[X, X, X, X],
[0, X, X, 0],
]);

const DIGIT_ONE: DigitArray = transpose([
[0, X, X, 0],
[X, X, X, 0],
Expand Down Expand Up @@ -38,7 +54,7 @@ const DIGIT_TWO: DigitArray = transpose([
[0, 0, X, 0],
]);

pub const DIGITS: [DigitArray; 2] = [DIGIT_ONE, DIGIT_TWO];
pub const DIGITS: [DigitArray; 3] = [DIGIT_ZERO, DIGIT_ONE, DIGIT_TWO];

const fn transpose(arr: [[u8; NUM_COLS]; NUM_ROWS]) -> DigitArray {
let mut transposed = [[0; NUM_ROWS]; NUM_COLS];
Expand Down
31 changes: 23 additions & 8 deletions src/main.rs
Expand Up @@ -5,6 +5,8 @@
#![no_main]
#![feature(const_for)]

include!(concat!(env!("OUT_DIR"), "/build_time.rs"));

use bsp::{
entry,
hal::gpio::{DynPinId, FunctionSio, Pin, PullDown, SioOutput},
Expand All @@ -28,6 +30,19 @@ use bsp::hal::{

mod digit;

fn day_of_week_from_u8(v: u8) -> Result<rtc::DayOfWeek, rtc::DateTimeError> {
Ok(match v {
0 => rtc::DayOfWeek::Sunday,
1 => rtc::DayOfWeek::Monday,
2 => rtc::DayOfWeek::Tuesday,
3 => rtc::DayOfWeek::Wednesday,
4 => rtc::DayOfWeek::Thursday,
5 => rtc::DayOfWeek::Friday,
6 => rtc::DayOfWeek::Saturday,
x => return Err(rtc::DateTimeError::InvalidDayOfWeek(x)),
})
}

#[entry]
fn main() -> ! {
info!("Program start");
Expand All @@ -52,13 +67,13 @@ fn main() -> ! {

// TODO: read initial datetime from serial
let initial_date = rtc::DateTime {
year: 2023,
month: 9,
day: 3,
day_of_week: rtc::DayOfWeek::Sunday,
hour: 14,
minute: 14,
second: 0,
year: BUILD_TIME.year,
month: BUILD_TIME.month,
day: BUILD_TIME.day,
day_of_week: day_of_week_from_u8(BUILD_TIME.day_of_week).unwrap(),
hour: BUILD_TIME.hour,
minute: BUILD_TIME.minute,
second: BUILD_TIME.second,
};

let real_time_clock =
Expand Down Expand Up @@ -128,7 +143,7 @@ fn main() -> ! {
let k: usize = 0;
let char_for_digit = now_digits[k];

if let Some(digit) = digit::DIGITS.get(char_for_digit - 1) {
if let Some(digit) = digit::DIGITS.get(char_for_digit) {
print_digit(digit, &mut row_pins, &mut col_pins, &mut delay, 100, 0.1);
}
}
Expand Down

0 comments on commit dd0f259

Please sign in to comment.