From e06d7ac9befb691608715cc1489f90faaa1a539a Mon Sep 17 00:00:00 2001 From: Ian Chamberlain Date: Tue, 18 Jan 2022 00:21:05 -0500 Subject: [PATCH 1/4] Add time_rtc example --- ctru-rs/Cargo.toml | 2 +- ctru-rs/examples/time_rtc.rs | 118 +++++++++++++++++++++++++++++++++++ 2 files changed, 119 insertions(+), 1 deletion(-) create mode 100644 ctru-rs/examples/time_rtc.rs diff --git a/ctru-rs/Cargo.toml b/ctru-rs/Cargo.toml index f33e177..036f6e6 100644 --- a/ctru-rs/Cargo.toml +++ b/ctru-rs/Cargo.toml @@ -15,6 +15,6 @@ ctru-sys = { path = "../ctru-sys", version = "0.4" } const-zero = "0.1.0" linker-fix-3ds = { git = "https://github.com/Meziu/rust-linker-fix-3ds.git" } pthread-3ds = { git = "https://github.com/Meziu/pthread-3ds.git" } -libc = { git = "https://github.com/Meziu/libc.git" } +libc = { version = "0.2" } bitflags = "1.0.0" widestring = "0.2.2" diff --git a/ctru-rs/examples/time_rtc.rs b/ctru-rs/examples/time_rtc.rs new file mode 100644 index 0000000..06a2685 --- /dev/null +++ b/ctru-rs/examples/time_rtc.rs @@ -0,0 +1,118 @@ +use std::ptr; + +use ctru::console::Console; +use ctru::gfx::{Gfx, Screen}; +use ctru::services::apt::Apt; +use ctru::services::hid::{Hid, KeyPad}; + +const MONTHS: [&str; 12] = [ + "January", + "February", + "March", + "April", + "May", + "June", + "July", + "August", + "September", + "October", + "November", + "December", +]; + +const WEEKDAYS: [&str; 7] = [ + "Sunday", + "Monday", + "Tuesday", + "Wednesday", + "Thursday", + "Friday", + "Saturday", +]; + +const MONTH_TO_WEEKDAY_TABLE: [i32; 12] = [ + 0 % 7, //january 31 + 31 % 7, //february 28+1(leap year) + 59 % 7, //march 31 + 90 % 7, //april 30 + 120 % 7, //may 31 + 151 % 7, //june 30 + 181 % 7, //july 31 + 212 % 7, //august 31 + 243 % 7, //september 30 + 273 % 7, //october 31 + 304 % 7, //november 30 + 334 % 7, //december 3 +]; + +fn is_leap_year(year: i32) -> bool { + year % 4 == 0 && !(year % 100 == 0 && year % 400 != 0) +} + +/// +fn get_day_of_week(mut day: i32, month: usize, mut year: i32) -> usize { + day += 2 * (3 - ((year / 100) % 4)); + year %= 100; + day += year + (year / 4); + day += MONTH_TO_WEEKDAY_TABLE[month] + - if is_leap_year(year) && month <= 1 { + 1 + } else { + 0 + }; + (day % 7).try_into().expect("day is not valid usize") +} + +fn main() { + ctru::init(); + + let gfx = Gfx::default(); + let hid = Hid::init().expect("Couldn't obtain HID controller"); + let apt = Apt::init().expect("Couldn't obtain APT controller"); + + let _console = Console::init(&gfx, Screen::Top); + + print!("\x1b[30;16HPress Start to exit."); + + // Main loop + while apt.main_loop() { + // Scan all the inputs. This should be done once for each frame + hid.scan_input(); + + if hid.keys_down().contains(KeyPad::KEY_START) { + break; + } + + // Get the current time. ctru_sys bindings should be used rather than + // plain libc ones, for reasons I don't understand yet... + let unix_time: ctru_sys::time_t = unsafe { ctru_sys::time(std::ptr::null_mut()) }; + let time = unsafe { ptr::read(ctru_sys::gmtime(&unix_time as *const _)) }; + + let hours = time.tm_hour; + let minutes = time.tm_min; + let seconds = time.tm_sec; + let day = time.tm_mday; + let month = time.tm_mon; + let year = time.tm_year + 1900; + + let month: usize = month.try_into().expect("month is not valid usize"); + + println!("\x1b[1;1H{:0>2}:{:0>2}:{:0>2}", hours, minutes, seconds); + print!( + "{} {} {} {}", + WEEKDAYS + .get(get_day_of_week(day, month, year)) + .expect("invalid weekday"), + MONTHS[month], + day, + year + ); + + // Flush and swap framebuffers + gfx.flush_buffers(); + gfx.swap_buffers(); + + //Wait for VBlank + gfx.wait_for_vblank(); + } +} From 550275825548512f0c061414a6dd3a6e92cc04c8 Mon Sep 17 00:00:00 2001 From: Ian Chamberlain Date: Thu, 20 Jan 2022 13:09:10 -0500 Subject: [PATCH 2/4] Update ctru-rs/Cargo.toml Co-authored-by: Mark Drobnak --- ctru-rs/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ctru-rs/Cargo.toml b/ctru-rs/Cargo.toml index 036f6e6..3743f07 100644 --- a/ctru-rs/Cargo.toml +++ b/ctru-rs/Cargo.toml @@ -15,6 +15,6 @@ ctru-sys = { path = "../ctru-sys", version = "0.4" } const-zero = "0.1.0" linker-fix-3ds = { git = "https://github.com/Meziu/rust-linker-fix-3ds.git" } pthread-3ds = { git = "https://github.com/Meziu/pthread-3ds.git" } -libc = { version = "0.2" } +libc = "0.2" bitflags = "1.0.0" widestring = "0.2.2" From b504b6f988a4b76900f26a051ea90a633584cfd1 Mon Sep 17 00:00:00 2001 From: Ian Chamberlain Date: Mon, 24 Jan 2022 18:23:24 -0500 Subject: [PATCH 3/4] Address PR comments --- ctru-rs/Cargo.toml | 2 +- ctru-rs/examples/time_rtc.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/ctru-rs/Cargo.toml b/ctru-rs/Cargo.toml index 036f6e6..3743f07 100644 --- a/ctru-rs/Cargo.toml +++ b/ctru-rs/Cargo.toml @@ -15,6 +15,6 @@ ctru-sys = { path = "../ctru-sys", version = "0.4" } const-zero = "0.1.0" linker-fix-3ds = { git = "https://github.com/Meziu/rust-linker-fix-3ds.git" } pthread-3ds = { git = "https://github.com/Meziu/pthread-3ds.git" } -libc = { version = "0.2" } +libc = "0.2" bitflags = "1.0.0" widestring = "0.2.2" diff --git a/ctru-rs/examples/time_rtc.rs b/ctru-rs/examples/time_rtc.rs index 06a2685..c537069 100644 --- a/ctru-rs/examples/time_rtc.rs +++ b/ctru-rs/examples/time_rtc.rs @@ -86,7 +86,7 @@ fn main() { // Get the current time. ctru_sys bindings should be used rather than // plain libc ones, for reasons I don't understand yet... let unix_time: ctru_sys::time_t = unsafe { ctru_sys::time(std::ptr::null_mut()) }; - let time = unsafe { ptr::read(ctru_sys::gmtime(&unix_time as *const _)) }; + let time = unsafe { *ctru_sys::gmtime(&unix_time as *const _) }; let hours = time.tm_hour; let minutes = time.tm_min; From 03678e0e95715c81bbded3a5b08cfea0ab1730b9 Mon Sep 17 00:00:00 2001 From: Ian Chamberlain Date: Mon, 24 Jan 2022 18:46:17 -0500 Subject: [PATCH 4/4] Update example for Console API change --- ctru-rs/examples/time_rtc.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/ctru-rs/examples/time_rtc.rs b/ctru-rs/examples/time_rtc.rs index c537069..f28159f 100644 --- a/ctru-rs/examples/time_rtc.rs +++ b/ctru-rs/examples/time_rtc.rs @@ -1,7 +1,5 @@ -use std::ptr; - use ctru::console::Console; -use ctru::gfx::{Gfx, Screen}; +use ctru::gfx::Gfx; use ctru::services::apt::Apt; use ctru::services::hid::{Hid, KeyPad}; @@ -70,7 +68,7 @@ fn main() { let hid = Hid::init().expect("Couldn't obtain HID controller"); let apt = Apt::init().expect("Couldn't obtain APT controller"); - let _console = Console::init(&gfx, Screen::Top); + let _console = Console::init(gfx.top_screen.borrow_mut()); print!("\x1b[30;16HPress Start to exit.");