/
US2066DisplayModuleBase.cs
236 lines (184 loc) · 7.47 KB
/
US2066DisplayModuleBase.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// SPDX-FileCopyrightText: 2021 smdn <smdn@smdn.jp>
// SPDX-License-Identifier: MIT
using System;
using System.Linq;
using System.Text;
using Iot.Device.CharacterLcd;
namespace Smdn.Devices.US2066;
public abstract class US2066DisplayModuleBase : ICharacterLcd {
protected abstract DisplayLineNumber DisplayLineNumber { get; }
protected abstract DisplayDotFormat DisplayDotFormat { get; }
public abstract int NumberOfCharsPerLine { get; }
public int NumberOfLines => DisplayLineNumber switch {
DisplayLineNumber.Lines1 => 1,
DisplayLineNumber.Lines2 => 2,
DisplayLineNumber.Lines3 => 3,
DisplayLineNumber.Lines4 => 4,
_ => throw new NotSupportedException($"unsupported display line number (DisplayLineNumber)"),
};
private US2066 oledInterface;
private US2066 OLEDInterface => oledInterface ?? throw new ObjectDisposedException(GetType().FullName);
[CLSCompliant(false)]
public US2066 Controller => OLEDInterface;
[CLSCompliant(false)]
protected US2066DisplayModuleBase(US2066 oledInterface)
{
this.oledInterface = oledInterface;
this.oledInterface.Initialize(
numberOfLines: DisplayLineNumber,
dotFormat: DisplayDotFormat
);
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (!disposing)
return;
oledInterface?.Dispose();
oledInterface = null;
}
public int Address => OLEDInterface.Address;
public int PartID => OLEDInterface.PartID;
public bool IsBusy => OLEDInterface.IsBusy;
public int CursorLeft {
get => OLEDInterface.CursorPosition;
set => OLEDInterface.SetCursorPosition(OLEDInterface.CursorLine, value);
}
public int CursorTop {
get => OLEDInterface.CursorLine;
set => OLEDInterface.SetCursorPosition(value, OLEDInterface.CursorPosition);
}
private static bool ThrowBacklightNotSupportedException()
=> throw new NotSupportedException("Backlight is not supported with US2066");
bool ICharacterLcd.BacklightOn {
get => ThrowBacklightNotSupportedException();
set => ThrowBacklightNotSupportedException();
}
public bool DisplayOn {
get => OLEDInterface.DisplayOn;
set => OLEDInterface.DisplayOn = value;
}
public bool UnderlineCursorVisible {
get => OLEDInterface.UnderlineCursorVisible;
set => OLEDInterface.UnderlineCursorVisible = value;
}
public bool BlinkingCursorVisible {
get => OLEDInterface.BlinkingCursorVisible;
set => OLEDInterface.BlinkingCursorVisible = value;
}
SixLabors.ImageSharp.Size ICharacterLcd.Size => new(NumberOfCharsPerLine, NumberOfLines);
public int NumberOfCustomCharactersSupported => OLEDInterface.NumberOfUserDefinedCharactersSupported;
public int Contrast {
get => OLEDInterface.Contrast;
set => OLEDInterface.Contrast = value;
}
public FadeOutMode FadeOutMode {
get => OLEDInterface.FadeOutMode;
set => OLEDInterface.FadeOutMode = value;
}
public FadeOutInterval FadeOutInterval {
get => OLEDInterface.FadeOutInterval;
set => OLEDInterface.FadeOutInterval = value;
}
public ClockDivideRatio ClockDivideRatio {
get => OLEDInterface.ClockDivideRatio;
set => OLEDInterface.ClockDivideRatio = value;
}
public InternalOscillatorFrequency InternalOscillatorFrequency {
get => OLEDInterface.InternalOscillatorFrequency;
set => OLEDInterface.InternalOscillatorFrequency = value;
}
public CharacterGeneratorEncoding CharacterGenerator {
get => OLEDInterface.CharacterGenerator;
set => OLEDInterface.CharacterGenerator = value;
}
public CGRamUsage CGRamUsage {
get => OLEDInterface.CGRamUsage;
set => OLEDInterface.CGRamUsage = value;
}
public void Clear()
=> OLEDInterface.Clear();
public void Home()
=> OLEDInterface.Home();
public void CreateCustomCharacter(int location, ReadOnlySpan<byte> characterMap)
=> CreateCustomCharacter((CGRamCharacter)location, default(Rune), characterMap);
/// <returns>Returns alternative <see cref="char"><c>char</c></see> value for registered character.</returns>
public char CreateCustomCharacter(CGRamCharacter character, ReadOnlySpan<byte> characterData)
=> OLEDInterface.RegisterCGRamCharacter(character, default, characterData);
/// <returns>Returns alternative <see cref="char"><c>char</c></see> value for registered character.</returns>
public char CreateCustomCharacter(CGRamCharacter character, int characterCodePoint, ReadOnlySpan<byte> characterData)
=> OLEDInterface.RegisterCGRamCharacter(character, new Rune(characterCodePoint), characterData);
/// <returns>Returns alternative <see cref="char"><c>char</c></see> value for registered character.</returns>
public char CreateCustomCharacter(CGRamCharacter character, char characterCodePoint, ReadOnlySpan<byte> characterData)
=> OLEDInterface.RegisterCGRamCharacter(character, new Rune(characterCodePoint), characterData);
/// <returns>Returns alternative <see cref="char"><c>char</c></see> value for registered character.</returns>
public char CreateCustomCharacter(CGRamCharacter character, string characterCodePointString, ReadOnlySpan<byte> characterData)
=> OLEDInterface.RegisterCGRamCharacter(
character,
string.IsNullOrEmpty(characterCodePointString) ? throw new ArgumentException("must be non-null and non-empty string", nameof(characterCodePointString)) : characterCodePointString.EnumerateRunes().First(),
characterData
);
/// <returns>Returns alternative <see cref="char"><c>char</c></see> value for registered character.</returns>
public char CreateCustomCharacter(CGRamCharacter character, Rune characterCodePoint, ReadOnlySpan<byte> characterData)
=> OLEDInterface.RegisterCGRamCharacter(character, characterCodePoint, characterData);
public void SetCursorPosition(int left, int top)
=> OLEDInterface.SetCursorPosition(top, left);
public void SetCursorPosition((int Left, int Top) position)
=> OLEDInterface.SetCursorPosition(position.Top, position.Left);
public (int Left, int Top) GetCursorPosition()
=> (OLEDInterface.CursorPosition, OLEDInterface.CursorLine);
public void Write(string text)
=> Write((text ?? throw new ArgumentNullException(nameof(text))).AsSpan());
public void WriteLine(string text)
{
Write(text);
WriteLine();
}
public void Write(ReadOnlySpan<char> text)
=> OLEDInterface.SendData(text);
public void WriteLine(ReadOnlySpan<char> text)
{
Write(text);
WriteLine();
}
public void Write(ReadOnlySpan<byte> text)
=> OLEDInterface.SendData(text);
public void WriteLine(ReadOnlySpan<byte> text)
{
Write(text);
WriteLine();
}
public void Write(byte character)
=> OLEDInterface.SendData(character);
public void WriteLine(byte character)
{
Write(character);
WriteLine();
}
public void Write(CGRamCharacter character)
=> OLEDInterface.SendData(
OLEDInterface.GetUserDefinedCharacterByte(
character is >= CGRamCharacter.Min and <= CGRamCharacter.Max
? (int)character
: throw new ArgumentException($"invalid value ({character})", nameof(character))
)
);
public void WriteLine(CGRamCharacter character)
{
Write(character);
WriteLine();
}
public void WriteLine()
{
if (OLEDInterface.CursorLine + 1 == NumberOfLines)
SetCursorPosition(OLEDInterface.DDRamAddressWidth - 1, OLEDInterface.CursorLine);
else
SetCursorPosition(0, OLEDInterface.CursorLine + 1);
}
public void ResetFadeOutStep()
=> OLEDInterface.ResetFadeOutStep();
}