-
Notifications
You must be signed in to change notification settings - Fork 598
/
Copy pathBoardLed.cs
182 lines (158 loc) · 6.36 KB
/
BoardLed.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
// 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 System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Diagnostics.CodeAnalysis;
namespace Iot.Device.BoardLed
{
/// <summary>
/// On-board LED on the device.
/// </summary>
public class BoardLed : IDisposable
{
private const string DefaultDevicePath = "/sys/class/leds";
private StreamReader _brightnessReader;
private StreamWriter _brightnessWriter;
private StreamReader _triggerReader;
private StreamWriter _triggerWriter;
private StreamReader _maxBrightnessReader;
/// <summary>
/// The name of the LED.
/// </summary>
public string Name { get; }
/// <summary>
/// The kernel controls the trigger of the LED.
/// </summary>
/// <remarks>
/// The kernel provides some triggers which let the kernel control the LED.
/// For example, the red light of Raspberry Pi, whose trigger is "default-on", which makes it keep lighting up.
/// If you want to operate the LED, you need to remove the trigger, which is to set its trigger to none.
/// Use <see cref="EnumerateTriggers()"/> to get all triggers.
/// </remarks>
public string Trigger
{
get => GetTrigger();
set => SetTrigger(value);
}
/// <summary>
/// The current brightness of the LED.
/// </summary>
public int Brightness
{
get => GetBrightness();
set => SetBrightness(value);
}
/// <summary>
/// The max brightness of the LED.
/// </summary>
public int MaxBrightness => GetMaxBrightness();
/// <summary>
/// Creates a new instance of the BoardLed.
/// </summary>
/// <param name="name">The name of the LED to control.</param>
public BoardLed(string name)
{
Name = name;
Initialize();
#if !NET5_0_OR_GREATER
if (_brightnessReader is null ||
_brightnessWriter is null ||
_maxBrightnessReader is null ||
_triggerReader is null ||
_triggerWriter is null)
{
throw new Exception($"{nameof(BoardLed)} incorrectly configured");
}
#endif
}
/// <summary>
/// Get all BoardLed instances of on-board LEDs.
/// </summary>
/// <returns>BoardLed instances.</returns>
public static IEnumerable<BoardLed> EnumerateLeds()
{
IEnumerable<DirectoryInfo> infos = Directory.GetDirectories(DefaultDevicePath)
.Select(x => new DirectoryInfo(x));
// Make sure it's a real LED
return infos.Where(x => x.EnumerateFiles().Select(f => f.Name).Contains("brightness"))
.Select(x => new BoardLed(x.Name));
}
/// <summary>
/// Get all triggers of current LED.
/// </summary>
/// <returns>The name of triggers.</returns>
public IEnumerable<string> EnumerateTriggers()
{
_triggerReader.BaseStream.Position = 0;
// Remove selected chars
return Regex.Replace(_triggerReader.ReadToEnd(), @"\[|\]", string.Empty)
.Split(' ');
}
private int GetBrightness()
{
_brightnessReader.BaseStream.Position = 0;
return int.Parse(_brightnessReader.ReadToEnd());
}
private int GetMaxBrightness()
{
_maxBrightnessReader.BaseStream.Position = 0;
return int.Parse(_maxBrightnessReader.ReadToEnd());
}
private void SetBrightness(int value)
{
value = MathExtensions.Clamp(value, 0, 255);
_brightnessWriter.BaseStream.SetLength(0);
_brightnessWriter.Write(value);
_brightnessWriter.Flush();
}
private string GetTrigger()
{
_triggerReader.BaseStream.Position = 0;
return Regex.Match(_triggerReader.ReadToEnd(), @"(?<=\[)(.*)(?=\])").Value;
}
private void SetTrigger(string name)
{
IEnumerable<string> triggers = EnumerateTriggers();
if (!triggers.Contains(name))
{
throw new Exception($"System does not contain a trigger called {name}.");
}
_triggerWriter.BaseStream.SetLength(0);
_triggerWriter.Write(name);
_triggerWriter.Flush();
}
#if NET5_0_OR_GREATER
[MemberNotNull(nameof(_brightnessReader), nameof(_brightnessWriter), nameof(_triggerReader), nameof(_triggerWriter), nameof(_maxBrightnessReader))]
#endif
private void Initialize()
{
FileStream brightnessStream = File.Open($"{DefaultDevicePath}/{Name}/brightness", FileMode.Open);
_brightnessReader = new StreamReader(stream: brightnessStream, encoding: Encoding.ASCII, detectEncodingFromByteOrderMarks: false, bufferSize: 4, leaveOpen: true);
_brightnessWriter = new StreamWriter(stream: brightnessStream, encoding: Encoding.ASCII, bufferSize: 4, leaveOpen: false);
FileStream triggerStream = File.Open($"{DefaultDevicePath}/{Name}/trigger", FileMode.Open);
_triggerReader = new StreamReader(stream: triggerStream, encoding: Encoding.ASCII, detectEncodingFromByteOrderMarks: false, bufferSize: 512, leaveOpen: true);
_triggerWriter = new StreamWriter(stream: triggerStream, encoding: Encoding.ASCII, bufferSize: 512, leaveOpen: false);
_maxBrightnessReader = new StreamReader(File.Open($"{DefaultDevicePath}/{Name}/max_brightness", FileMode.Open, FileAccess.Read));
}
/// <summary>
/// Cleanup
/// </summary>
public void Dispose()
{
_brightnessReader?.Dispose();
_brightnessWriter?.Dispose();
_triggerReader?.Dispose();
_triggerWriter?.Dispose();
_maxBrightnessReader?.Dispose();
_brightnessReader = null!;
_brightnessWriter = null!;
_triggerReader = null!;
_triggerWriter = null!;
_maxBrightnessReader = null!;
}
}
}