-
Notifications
You must be signed in to change notification settings - Fork 586
/
LedBar.cs
185 lines (163 loc) · 5.81 KB
/
LedBar.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// 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.Collections.Generic;
using Iot.Device.GrovePiDevice.Models;
namespace Iot.Device.GrovePiDevice.Sensors
{
/// <summary>
/// The led bar orientation
/// </summary>
public enum LedBarOrientation
{
/// <summary>Red to green</summary>
RedToGreen = 0,
/// <summary>Green to red</summary>
GreenToRed = 1
}
/// <summary>
/// LedBar class to support grove LedBar http://wiki.seeedstudio.com/Grove-LED_Bar/
/// </summary>
public class LedBar
{
/// <summary>
/// Only Digital ports only but you can't create more than 4 bars as each bar is using 2 Pins
/// So you have to have at least 1 Grove Port empty between 2 bars
/// </summary>
public static List<GrovePort> SupportedPorts => new List<GrovePort>()
{
GrovePort.DigitalPin2,
GrovePort.DigitalPin3,
GrovePort.DigitalPin4,
GrovePort.DigitalPin5,
GrovePort.DigitalPin6,
GrovePort.DigitalPin7,
GrovePort.DigitalPin8,
};
private GrovePi _grovePi;
private byte _level;
private LedBarOrientation _orientation;
/// <summary>
/// grove sensor port
/// </summary>
private GrovePort _port;
/// <summary>
/// LedBar constructor
/// </summary>
/// <param name="grovePi">The GrovePi class</param>
/// <param name="port">The grove Port, need to be in the list of SupportedPorts</param>
public LedBar(GrovePi grovePi, GrovePort port)
: this(grovePi, port, LedBarOrientation.GreenToRed)
{
}
/// <summary>
/// LedBar constructor
/// </summary>
/// <param name="grovePi">The GrovePi class</param>
/// <param name="port">The grove Port, need to be in the list of SupportedPorts</param>
/// <param name="orientation">Orientation, Green to red is default</param>
public LedBar(GrovePi grovePi, GrovePort port, LedBarOrientation orientation)
{
if (!SupportedPorts.Contains(port))
{
throw new ArgumentException("Grove port not supported.", nameof(port));
}
_grovePi = grovePi;
_port = port;
_orientation = orientation;
_grovePi.WriteCommand(GrovePiCommand.LedBarInitialization, port, (byte)_orientation, 0);
_level = 0;
}
/// <summary>
/// Get/Set the Led bar orientation
/// </summary>
public LedBarOrientation Orientation
{
get
{
return _orientation;
}
set
{
if (_orientation != value)
{
_orientation = value;
_grovePi.WriteCommand(GrovePiCommand.LedBarOrientation, _port, (byte)_orientation, 0);
}
}
}
/// <summary>
/// Get/Set the level from 0 (no led on) to 10 (all leds on)
/// </summary>
public byte Value
{
get
{
return _level;
}
set
{
if (value > 10)
{
throw new ArgumentException("Only 10 leds can be controlled, 1-10.", nameof(Value));
}
_grovePi.WriteCommand(GrovePiCommand.LedBarLevel, _port, _level, 0);
}
}
/// <summary>
/// Set one led
/// </summary>
/// <param name="led">The led number from 0 to 10</param>
/// <param name="status">true for on, false for off</param>
public void SetOneLed(byte led, bool status)
{
if (led > 10)
{
throw new ArgumentException("Only 10 leds can be controlled, 1-10.", nameof(led));
}
_grovePi.WriteCommand(GrovePiCommand.LedBarSetOneLed, _port, led, status ? (byte)1 : (byte)0);
}
/// <summary>
/// Set all leds to either on or off by setting 1 as byte for on
/// 0 for off. lowser bit is led 0
/// </summary>
/// <param name="leds">Pixel array, 1 bit for each LED</param>
public void SetAllLeds(int leds)
{
leds = leds & 0b00000111111111;
_grovePi.WriteCommand(GrovePiCommand.LedBarSet, _port, (byte)(leds & 0xFF), (byte)((leds >> 8) & 0xFF));
}
/// <summary>
/// Get the status of all leds
/// </summary>
/// <returns>Returns all leds status in an int, lower bit is led 0, 1 is on, 0 is off</returns>
public int GetAllLeds()
{
_grovePi.WriteCommand(GrovePiCommand.LetBarGet, _port, 0, 0);
var ret = _grovePi.ReadCommand(GrovePiCommand.LetBarGet, _port);
if (ret is object)
{
return ret[1] + (ret[2] >> 8);
}
throw new Exception("Cannot find all LEDs");
}
/// <summary>
/// Toggle the state of a led
/// </summary>
/// <param name="led">The led from 0 to 10</param>
public void ToggleLeds(byte led)
{
led = MathExtensions.Clamp(led, (byte)0, (byte)10);
_grovePi.WriteCommand(GrovePiCommand.LedBarToggleOneLed, _port, led, 0);
}
/// <summary>
/// Returns the Level formatted
/// </summary>
/// <returns>Returns the LED Level formatted</returns>
public override string ToString() => $"Level {_level}";
/// <summary>
/// Get the name Led Bar
/// </summary>
public string SensorName => "Led Bar";
}
}