forked from nanoframework/nanoFramework.IoT.Device
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PowerEvent.cs
81 lines (70 loc) · 3.41 KB
/
PowerEvent.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Device.I2c;
using Iot.Device.Common;
namespace Iot.Device.Mcp7940xx
{
/// <summary>
/// Battery-Backed I2C Real-Time Clock/Calendar with SRAM.
/// </summary>
public partial class Mcp7940n
{
/// <summary>
/// Represents a power event on a Mcp7940xx series device.
/// </summary>
public struct PowerEvent
{
/// <summary>
/// Gets the minute the event occurred on.
/// </summary>
public byte Minute { get; private set; }
/// <summary>
/// Gets the hour the event occurred on.
/// </summary>
public byte Hour { get; private set; }
/// <summary>
/// Gets the day-of-the-week the event occurred on.
/// </summary>
public DayOfWeek DayOfWeek { get; private set; }
/// <summary>
/// Gets the day-of-the-month the event occurred on.
/// </summary>
public byte Day { get; private set; }
/// <summary>
/// Gets the month the event occurred on.
/// </summary>
public byte Month { get; private set; }
/// <summary>
/// Initializes a new instance of the <see cref="PowerEvent" /> structure.
/// </summary>
/// <param name="i2cDevice">The I2C device to use for communication.</param>
/// <param name="eventRegister">The minute register of the power event to be read.</param>
/// <remarks>
/// Parameter <paramref name="eventRegister"/> must only be set to <see cref="Register.PowerUpMinute" /> or <see cref="Register.PowerDownMinute" />.
/// </remarks>
/// <exception cref="ArgumentNullException">Thrown when <paramref name="i2cDevice"/> is <c>null</c>.</exception>
/// <exception cref="ArgumentException">Thrown when <paramref name="eventRegister"/> is not <see cref="Register.PowerUpMinute" /> or <see cref="Register.PowerDownMinute" />.</exception>
internal PowerEvent(I2cDevice i2cDevice, Register eventRegister)
{
if (i2cDevice == null)
{
throw new ArgumentNullException();
}
if (eventRegister != Register.PowerUpMinute && eventRegister != Register.PowerDownMinute)
{
throw new ArgumentException();
}
// Read minute, hour, day, and month registers for the power down event.
SpanByte readBuffer = new byte[4];
i2cDevice.WriteByte((byte)eventRegister);
i2cDevice.Read(readBuffer);
Minute = (byte)NumberHelper.Bcd2Dec((byte)(readBuffer[0] & (byte)RegisterMask.PowerEventMinuteMask));
Hour = (byte)NumberHelper.Bcd2Dec((byte)(readBuffer[1] & (byte)RegisterMask.PowerEventHourMask));
Day = (byte)NumberHelper.Bcd2Dec((byte)(readBuffer[2] & (byte)RegisterMask.PowerEventDayMask));
DayOfWeek = (DayOfWeek)((readBuffer[3] & (byte)RegisterMask.PowerEventWeekdayMask) >> 5);
Month = (byte)NumberHelper.Bcd2Dec((byte)(readBuffer[3] & (byte)RegisterMask.PowerEventMonthMask));
}
}
}
}