-
Notifications
You must be signed in to change notification settings - Fork 582
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactoring RTC Devices & Adding PCF8563 Binding (#489)
* Added PCF8563 Binding * add number tool * update readme * remove namespace * refactoring completion * resolve comments
- Loading branch information
1 parent
2453561
commit d35c009
Showing
33 changed files
with
387 additions
and
333 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
namespace Iot.Device.Common | ||
{ | ||
/// <summary> | ||
/// Helpers for number. | ||
/// </summary> | ||
internal static class NumberHelper | ||
{ | ||
/// <summary> | ||
/// BCD To decimal | ||
/// </summary> | ||
/// <param name="bcd">BCD Code</param> | ||
/// <returns>decimal</returns> | ||
public static int Bcd2Dec(byte bcd) | ||
{ | ||
return ((bcd >> 4) * 10) + (bcd % 16); | ||
} | ||
|
||
/// <summary> | ||
/// Decimal To BCD | ||
/// </summary> | ||
/// <param name="dec">decimal</param> | ||
/// <returns>BCD Code</returns> | ||
public static byte Dec2Bcd(int dec) | ||
{ | ||
return (byte)(((dec / 10) << 4) + (dec % 10)); | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Binary file not shown.
Binary file not shown.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Binary file not shown.
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,88 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
using System; | ||
using System.Device.I2c; | ||
using Iot.Device.Common; | ||
|
||
namespace Iot.Device.Rtc | ||
{ | ||
/// <summary> | ||
/// Realtime Clock DS1307 | ||
/// </summary> | ||
public class Ds1307 : RtcBase | ||
{ | ||
/// <summary> | ||
/// DS1307 Default I2C Address | ||
/// </summary> | ||
public const byte DefaultI2cAddress = 0x68; | ||
|
||
private I2cDevice _i2cDevice; | ||
|
||
/// <summary> | ||
/// Creates a new instance of the DS1307 | ||
/// </summary> | ||
/// <param name="i2cDevice">The I2C device used for communication.</param> | ||
public Ds1307(I2cDevice i2cDevice) | ||
{ | ||
_i2cDevice = i2cDevice; | ||
} | ||
|
||
/// <summary> | ||
/// Read Time from DS1307 | ||
/// </summary> | ||
/// <returns>DS1307 Time</returns> | ||
protected override DateTime ReadTime() | ||
{ | ||
Span<byte> readBuffer = stackalloc byte[7]; | ||
|
||
// Read all registers at the same time | ||
_i2cDevice.WriteByte((byte)Ds1307Register.RTC_SEC_REG_ADDR); | ||
_i2cDevice.Read(readBuffer); | ||
|
||
// Details in the Datasheet P8 | ||
return new DateTime(2000 + NumberHelper.Bcd2Dec(readBuffer[6]), | ||
NumberHelper.Bcd2Dec(readBuffer[5]), | ||
NumberHelper.Bcd2Dec(readBuffer[4]), | ||
NumberHelper.Bcd2Dec(readBuffer[2]), | ||
NumberHelper.Bcd2Dec(readBuffer[1]), | ||
NumberHelper.Bcd2Dec((byte)(readBuffer[0] & 0b_0111_1111))); | ||
} | ||
|
||
/// <summary> | ||
/// Set DS1307 Time | ||
/// </summary> | ||
/// <param name="time">Time</param> | ||
protected override void SetTime(DateTime time) | ||
{ | ||
Span<byte> writeBuffer = stackalloc byte[8]; | ||
|
||
writeBuffer[0] = (byte)Ds1307Register.RTC_SEC_REG_ADDR; | ||
|
||
// Details in the Datasheet P8 | ||
// | bit 7: CH | bit 6-0: sec | | ||
writeBuffer[1] = (byte)(NumberHelper.Dec2Bcd(time.Second) & 0b_0111_1111); | ||
writeBuffer[2] = NumberHelper.Dec2Bcd(time.Minute); | ||
// | bit 7: 0 | bit 6: 12/24 hour | bit 5-0: hour | | ||
writeBuffer[3] = (byte)(NumberHelper.Dec2Bcd(time.Hour) & 0b_0011_1111); | ||
writeBuffer[4] = NumberHelper.Dec2Bcd((int)time.DayOfWeek + 1); | ||
writeBuffer[5] = NumberHelper.Dec2Bcd(time.Day); | ||
writeBuffer[6] = NumberHelper.Dec2Bcd(time.Month); | ||
writeBuffer[7] = NumberHelper.Dec2Bcd(time.Year - 2000); | ||
|
||
_i2cDevice.Write(writeBuffer); | ||
} | ||
|
||
/// <summary> | ||
/// Cleanup | ||
/// </summary> | ||
protected override void Dispose(bool disposing) | ||
{ | ||
_i2cDevice?.Dispose(); | ||
_i2cDevice = null; | ||
|
||
base.Dispose(disposing); | ||
} | ||
} | ||
} |
Oops, something went wrong.