Implements a custom I2C interface for the DS1307 DTC.
Inheritance: Porrey.Uwp.IoT.Sensors.I2c, IDs1307
Ejemplo n.º 1
0
		private async Task<DateTimeOffset> CurrentDateTime()
		{
			DateTimeOffset currentDateTime = DateTimeOffset.Now;

			try
			{
				// ***
				// *** Make sure I2C is available. This will
				// *** allow the code to be run on a normal PC
				// *** without error.
				// ***
				if (ApiInformation.IsTypePresent(typeof(I2cDevice).FullName))
				{
					// ***
					// *** Get the time
					// ***
					Ds1307 rtc = new Ds1307();
					currentDateTime = await rtc.GetAsync();
				}
			}
			catch
			{
				currentDateTime = DateTimeOffset.Now;
			}

			return currentDateTime;
        }
Ejemplo n.º 2
0
		private async Task TestClock()
		{
			// ***
			// *** Clock
			// ***
			Ds1307 dtc = new Ds1307();
			await dtc.InitializeAsync();

			// ***
			// *** Get the date and time from the clock
			// ***
			DateTimeOffset dt = await dtc.GetAsync();

			// ***
			// *** Create an NTP client and get the date and time
			// ***
			NtpClient ntp = new NtpClient();
			DateTimeOffset? ndt = await ntp.GetAsync("0.pool.ntp.org", "1.pool.ntp.org", "2.pool.ntp.org", "3.pool.ntp.org");

			// ***
			// *** Update the clock if we have a result from the servers
			// ***
			if (ndt.HasValue)
			{
				await dtc.SetAsync(ndt.Value);
			}
		}
Ejemplo n.º 3
0
		/// <summary>
		/// Get the current date and time from an NTP server(s) and update
		/// the DS1307 RTC (Real Time Clock).
		/// </summary>
		/// <returns></returns>
		private async Task UpdateClock()
		{
			// ***
			// *** Make sure I2C is available. This will
			// *** allow the code to be run on a normal PC
			// *** without error.
			// ***
			if (ApiInformation.IsTypePresent(typeof(I2cDevice).FullName))
			{
				// ***
				// *** Create the clint object and set a timeout value.
				// ***
				NtpClient ntp = new NtpClient();
				ntp.Timeout = TimeSpan.FromSeconds(15);

				// ***
				// *** Make the call to the NTP server(s)
				// ***
				DateTime? dateTimeValue = await ntp.GetAsync(this.ApplicationSettngs.NtpServers);

				// ***
				// *** Set the date and time on the RTC
				// ***
				if (dateTimeValue.HasValue)
				{
					Ds1307 rtc = new Ds1307();
					await rtc.SetAsync(dateTimeValue.Value);
				}
			}
		}