-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add time_rtc example #23
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e06d7ac
Add time_rtc example
ian-h-chamberlain b032fd7
Merge branch 'master' into example/time-rtc
ian-h-chamberlain 5502758
Update ctru-rs/Cargo.toml
ian-h-chamberlain b504b6f
Address PR comments
ian-h-chamberlain aaca346
Merge branch 'master' into example/time-rtc
ian-h-chamberlain 03678e0
Update example for Console API change
ian-h-chamberlain 7913f83
Merge branch 'example/time-rtc' of github.com:ian-h-chamberlain/ctru-…
ian-h-chamberlain File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
use ctru::console::Console; | ||
use ctru::gfx::Gfx; | ||
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) | ||
} | ||
|
||
/// <http://en.wikipedia.org/wiki/Calculating_the_day_of_the_week> | ||
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.top_screen.borrow_mut()); | ||
|
||
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 { *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(); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be great if this used std, though I see there's some discussion about how that might require some more work...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It wouldn't really require more work. It requires
libc
's PR to be merged...