Skip to content

Commit

Permalink
Refactoring RTC Devices & Adding PCF8563 Binding (#489)
Browse files Browse the repository at this point in the history
* Added PCF8563 Binding

* add number tool

* update readme

* remove namespace

* refactoring completion

* resolve comments
  • Loading branch information
ZhangGaoxing authored and krwq committed Jul 24, 2019
1 parent 2453561 commit d35c009
Show file tree
Hide file tree
Showing 33 changed files with 387 additions and 333 deletions.
32 changes: 32 additions & 0 deletions src/devices/Common/Iot/Device/Common/NumberHelper.cs
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));
}
}
}
110 changes: 0 additions & 110 deletions src/devices/Ds1307/Ds1307.cs

This file was deleted.

16 changes: 0 additions & 16 deletions src/devices/Ds1307/Ds1307.csproj

This file was deleted.

2 changes: 0 additions & 2 deletions src/devices/Ds1307/category.txt

This file was deleted.

40 changes: 0 additions & 40 deletions src/devices/Ds1307/samples/Program.cs

This file was deleted.

22 changes: 0 additions & 22 deletions src/devices/Ds3231/Ds3231.csproj

This file was deleted.

2 changes: 0 additions & 2 deletions src/devices/Ds3231/category.txt

This file was deleted.

Binary file removed src/devices/Ds3231/samples/DS3231_circuit.fzz
Binary file not shown.
Binary file removed src/devices/Ds3231/samples/DS3231_circuit_bb.png
Binary file not shown.
22 changes: 0 additions & 22 deletions src/devices/Ds3231/samples/Ds3231.Samples.csproj

This file was deleted.

45 changes: 0 additions & 45 deletions src/devices/Ds3231/samples/README.md

This file was deleted.

Binary file removed src/devices/Ds3231/samples/RunningResult.jpg
Binary file not shown.
88 changes: 88 additions & 0 deletions src/devices/Rtc/Devices/Ds1307/Ds1307.cs
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);
}
}
}
Loading

0 comments on commit d35c009

Please sign in to comment.