forked from nanoframework/nanoFramework.IoT.Device
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Unit4Relay.cs
84 lines (75 loc) · 2.6 KB
/
Unit4Relay.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
82
83
84
// 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;
namespace Iot.Device.Relay
{
/// <summary>
/// Unit 4 Relay. This module only contains 4 relays and 4 leds. Each can be piloted independly or being in sync.
/// </summary>
public class Unit4Relay : Base4Relay
{
/// <summary>
/// Gets or sets a value indicating whether synchronize the Led with the Relay.
/// </summary>
public bool SynchronizedMode
{
get
{
return Read(Register.UnitSetup) == 1;
}
set
{
Write(Register.UnitSetup, (byte)(value ? 1 : 0));
}
}
/// <summary>
/// Initializes a new instance of the <see cref="Unit4Relay" /> class.
/// </summary>
/// <param name="i2cDevice">The I2C device.</param>
public Unit4Relay(I2cDevice i2cDevice) : base(i2cDevice, RelayType.Unit)
{
}
/// <summary>
/// Sets all the Led in the same state. This preserved the relay state.
/// </summary>
/// <param name="state">The state On or Off.</param>
public void SetAllLeds(State state)
{
byte relays = (byte)(Read(Register.UnitRelay) & 0x0F);
relays |= (byte)(state == State.On ? 0xF0 : 0x00);
Write(Register.UnitRelay, relays);
}
/// <summary>
/// Sets all the Realys and the Leds to the same state.
/// </summary>
/// <param name="state">The state On or Off.</param>
public void SetAllLedAndRelay(State state)
{
Write(Register.UnitRelay, (byte)(state == State.On ? 0xFF : 0x00));
}
/// <summary>
/// Sets the state of a specific Led.
/// </summary>
/// <param name="number">The Led number from 0 to 3.</param>
/// <param name="state">The state On or Off.</param>
/// <exception cref="ArgumentException">Led number must be between 0 and 3.</exception>
public void SetLed(byte number, State state)
{
if (number > 3)
{
throw new ArgumentException();
}
byte status = Read(Register.UnitRelay);
if (state == State.On)
{
status &= (byte)~(0x10 << number);
}
else
{
status |= (byte)(0x10 << number);
}
Write(Register.UnitRelay, status);
}
}
}