pub fn read_rtc(&mut self, century_handler: CMOSCenturyHandler) -> RTCDateTime {
		let mut rtc_time = RTCDateTime { second: 0, minute: 0, hour: 0, day: 0, month: 0, year: 0 };

		// Note: This uses the "read registers until you get the same values twice in a row" technique to avoid getting
		// dodgy/inconsistent values due to RTC updates
		self.read_into_rtc(&mut rtc_time);

		let mut century = 0;
		if let CMOSCenturyHandler::CenturyRegister(century_reg) = century_handler {
			century = self.read(century_reg);
		}

		let mut last_second;
		let mut last_minute;
		let mut last_hour;
		let mut last_day;
		let mut last_month;
		let mut last_year;
		let mut last_century;

		loop {
			last_second = rtc_time.second;
			last_minute = rtc_time.minute;
			last_hour = rtc_time.hour;
			last_day = rtc_time.day;
			last_month = rtc_time.month;
			last_year = rtc_time.year;
			last_century = century;

			self.read_into_rtc(&mut rtc_time);

			if last_second != rtc_time.second
				|| last_minute != rtc_time.minute
				|| last_hour != rtc_time.hour
				|| last_day != rtc_time.day
				|| last_month != rtc_time.month
				|| last_year != rtc_time.year
				|| last_century != century
			{
				break;
			}
		}

		let register_b = self.read(0x0B);

		// Convert BCD to binary values if necessary
		if (register_b & 0x04) == 0 {
			rtc_time.second = (rtc_time.second & 0x0F) + ((rtc_time.second / 16) * 10);
			rtc_time.minute = (rtc_time.minute & 0x0F) + ((rtc_time.minute / 16) * 10);
			rtc_time.hour = ((rtc_time.hour & 0x0F) + (((rtc_time.hour & 0x70) / 16) * 10)) | (rtc_time.hour & 0x80);
			rtc_time.day = (rtc_time.day & 0x0F) + ((rtc_time.day / 16) * 10);
			rtc_time.month = (rtc_time.month & 0x0F) + ((rtc_time.month / 16) * 10);
			rtc_time.year = (rtc_time.year & 0x0F) + ((rtc_time.year / 16) * 10);

			if let CMOSCenturyHandler::CenturyRegister(_) = century_handler {
				century = (century & 0x0F) + ((century / 16) * 10);
			}
		}

		// Convert 12 hour clock to 24 hour clock if necessary
		if ((register_b & 0x02) == 0) && ((rtc_time.hour & 0x80) != 0) {
			rtc_time.hour = ((rtc_time.hour & 0x7F) + 12) % 24;
		}

		// Calculate the full (4-digit) year
		match century_handler {
			CMOSCenturyHandler::CenturyRegister(_) => rtc_time.year += (century * 100) as usize,
			CMOSCenturyHandler::CurrentYear(current_year) => {
				rtc_time.year += (current_year / 100) * 100;
				if rtc_time.year < current_year {
					rtc_time.year += 100;
				}
			}
		}

		rtc_time
	}
/// Reads from the RTC part of CMOS
/// Returns an [`RTCDateTime`] struct, which includes all date time fields.
/// This method automatically converts BCD to binary values and 12 hours to 24 hour if necessary.
///
/// # Examples
/// ```rust,no_run
/// # use cmos::{CMOS, CMOSCenturyHandler};
/// let mut cmos = unsafe { CMOS::new() };
/// // Get current RTC by current year of 2019
/// let rtc = cmos.read_rtc(CMOSCenturyHandler::CurrentYear(2019));
/// ```
/// [`RTCDateTime`]: struct.RTCDateTime.html
Reads from the RTC part of CMOS
Returns an [`RTCDateTime`] struct, which includes all date time fields.
This method automatically converts BCD to binary values and 12 hours to 24 hour if necessary.
Reads from the RTC part of CMOS Returns an [ ` RTCDateTime ` ] struct which includes all date time fields . This method automatically converts BCD to binary values and 12 hours to 24 hour if necessary .
pub fn read_rtc(&mut self, century_handler: CMOSCenturyHandler) -> RTCDateTime {
		let mut rtc_time = RTCDateTime { second: 0, minute: 0, hour: 0, day: 0, month: 0, year: 0 };
		// Note: This uses the "read registers until you get the same values twice in a row" technique to avoid getting
		// dodgy/inconsistent values due to RTC updates
		self.read_into_rtc(&mut rtc_time);
		let mut century = 0;
		if let CMOSCenturyHandler::CenturyRegister(century_reg) = century_handler {
			century = self.read(century_reg);
		}
		let mut last_second;
		let mut last_minute;
		let mut last_hour;
		let mut last_day;
		let mut last_month;
		let mut last_year;
		let mut last_century;
		loop {
			last_second = rtc_time.second;
			last_minute = rtc_time.minute;
			last_hour = rtc_time.hour;
			last_day = rtc_time.day;
			last_month = rtc_time.month;
			last_year = rtc_time.year;
			last_century = century;
			self.read_into_rtc(&mut rtc_time);
			if last_second != rtc_time.second
				|| last_minute != rtc_time.minute
				|| last_hour != rtc_time.hour
				|| last_day != rtc_time.day
				|| last_month != rtc_time.month
				|| last_year != rtc_time.year
				|| last_century != century
			{
				break;
			}
		}
		let register_b = self.read(0x0B);
		// Convert BCD to binary values if necessary
		if (register_b & 0x04) == 0 {
			rtc_time.second = (rtc_time.second & 0x0F) + ((rtc_time.second / 16) * 10);
			rtc_time.minute = (rtc_time.minute & 0x0F) + ((rtc_time.minute / 16) * 10);
			rtc_time.hour = ((rtc_time.hour & 0x0F) + (((rtc_time.hour & 0x70) / 16) * 10)) | (rtc_time.hour & 0x80);
			rtc_time.day = (rtc_time.day & 0x0F) + ((rtc_time.day / 16) * 10);
			rtc_time.month = (rtc_time.month & 0x0F) + ((rtc_time.month / 16) * 10);
			rtc_time.year = (rtc_time.year & 0x0F) + ((rtc_time.year / 16) * 10);
			if let CMOSCenturyHandler::CenturyRegister(_) = century_handler {
				century = (century & 0x0F) + ((century / 16) * 10);
			}
		}
		// Convert 12 hour clock to 24 hour clock if necessary
		if ((register_b & 0x02) == 0) && ((rtc_time.hour & 0x80) != 0) {
			rtc_time.hour = ((rtc_time.hour & 0x7F) + 12) % 24;
		}
		// Calculate the full (4-digit) year
		match century_handler {
			CMOSCenturyHandler::CenturyRegister(_) => rtc_time.year += (century * 100) as usize,
			CMOSCenturyHandler::CurrentYear(current_year) => {
				rtc_time.year += (current_year / 100) * 100;
				if rtc_time.year < current_year {
					rtc_time.year += 100;
				}
			}
		}
		rtc_time
	}
pub fn read_rtc ( & mut self , century_handler : CMOSCenturyHandler ) -> RTCDateTime { let mut rtc_time = RTCDateTime { second : 0 , minute : 0 , hour : 0 , day : 0 , month : 0 , year : 0 } ; // Note: This uses the "read registers until you get the same values twice in a row" technique to avoid getting // dodgy/inconsistent values due to RTC updates self . read_into_rtc ( & mut rtc_time ) ; let mut century = 0 ; if let CMOSCenturyHandler :: CenturyRegister ( century_reg ) = century_handler { century = self . read ( century_reg ) ; } let mut last_second ; let mut last_minute ; let mut last_hour ; let mut last_day ; let mut last_month ; let mut last_year ; let mut last_century ; loop { last_second = rtc_time . second ; last_minute = rtc_time . minute ; last_hour = rtc_time . hour ; last_day = rtc_time . day ; last_month = rtc_time . month ; last_year = rtc_time . year ; last_century = century ; self . read_into_rtc ( & mut rtc_time ) ; if last_second != rtc_time . second || last_minute != rtc_time . minute || last_hour != rtc_time . hour || last_day != rtc_time . day || last_month != rtc_time . month || last_year != rtc_time . year || last_century != century { break ; } } let register_b = self . read ( 0x0B ) ; // Convert BCD to binary values if necessary if ( register_b & 0x04 ) == 0 { rtc_time . second = ( rtc_time . second & 0x0F ) + ( ( rtc_time . second / 16 ) * 10 ) ; rtc_time . minute = ( rtc_time . minute & 0x0F ) + ( ( rtc_time . minute / 16 ) * 10 ) ; rtc_time . hour = ( ( rtc_time . hour & 0x0F ) + ( ( ( rtc_time . hour & 0x70 ) / 16 ) * 10 ) ) | ( rtc_time . hour & 0x80 ) ; rtc_time . day = ( rtc_time . day & 0x0F ) + ( ( rtc_time . day / 16 ) * 10 ) ; rtc_time . month = ( rtc_time . month & 0x0F ) + ( ( rtc_time . month / 16 ) * 10 ) ; rtc_time . year = ( rtc_time . year & 0x0F ) + ( ( rtc_time . year / 16 ) * 10 ) ; if let CMOSCenturyHandler :: CenturyRegister ( _ ) = century_handler { century = ( century & 0x0F ) + ( ( century / 16 ) * 10 ) ; } } // Convert 12 hour clock to 24 hour clock if necessary if ( ( register_b & 0x02 ) == 0 ) && ( ( rtc_time . hour & 0x80 ) != 0 ) { rtc_time . hour = ( ( rtc_time . hour & 0x7F ) + 12 ) % 24 ; } // Calculate the full (4-digit) year match century_handler { CMOSCenturyHandler :: CenturyRegister ( _ ) => rtc_time . year += ( century * 100 ) as usize , CMOSCenturyHandler :: CurrentYear ( current_year ) => { rtc_time . year += ( current_year / 100 ) * 100 ; if rtc_time . year < current_year { rtc_time . year += 100 ; } } } rtc_time }
Reads from the RTC part of CMOS
Returns an [`RTCDateTime`] struct, which includes all date time fields.
Reads from the RTC part of CMOS Returns an [ ` RTCDateTime ` ] struct which includes all date time fields .
&mut self
century_handler
CMOSCenturyHandler
&mut self
century_handler
CMOSCenturyHandler
examples
None
use cmos::{cmos, cmoscenturyhandler};
let mut cmos = unsafe { CMOS::new() };
Get current RTC by current year of 2019
let rtc = cmos.read_rtc(CMOSCenturyHandler::CurrentYear(2019));
```
[`RTCDateTime`]: struct.RTCDateTime.html
let mut cmos = unsafe { CMOS :: new () } ; Get current RTC by current year of 2019 let rtc = cmos . read_rtc ( CMOSCenturyHandler :: CurrentYear ( 2019 )) ; ` ` ` [ ` RTCDateTime ` ] : struct . RTCDateTime . html