Skip to content

Commit 7366d14

Browse files
authored
Adding Max7219 (#46)
1 parent 0e796d4 commit 7366d14

25 files changed

+3041
-0
lines changed

devices/Max7219/DeviceIdDigit.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
3+
namespace Iot.Device.Max7219
4+
{
5+
public class DeviceIdDigit
6+
{
7+
public DeviceIdDigit(int deviceId, int digit)
8+
{
9+
DeviceId = deviceId;
10+
Digit = digit;
11+
}
12+
13+
public int DeviceId { get; set; }
14+
public int Digit { get; set; }
15+
}
16+
}

devices/Max7219/FixedSizeFont.cs

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
using System;
5+
6+
namespace Iot.Device.Max7219
7+
{
8+
/// <summary>
9+
/// Implementation of a <see cref="IFont"/> that uses a common array for all characters.
10+
/// The number of bytes per character is constant and zero values between the characters are trimmed.
11+
/// </summary>
12+
public class FixedSizeFont : IFont
13+
{
14+
private readonly byte[] _data;
15+
private readonly int _bytesPerCharacter;
16+
private readonly byte[] _space;
17+
18+
/// <summary>
19+
/// Constructs FixedSizeFont instance
20+
/// </summary>
21+
/// <param name="bytesPerCharacter">number of bytes per character</param>
22+
/// <param name="data">Font data</param>
23+
/// <param name="spaceWidth">Space width</param>
24+
public FixedSizeFont(int bytesPerCharacter, byte[] data, int spaceWidth = 3)
25+
{
26+
_data = data;
27+
_bytesPerCharacter = bytesPerCharacter;
28+
_space = new byte[spaceWidth];
29+
}
30+
31+
/// <summary>
32+
/// Get character information
33+
/// </summary>
34+
public ListByte this[char chr]
35+
{
36+
get
37+
{
38+
int start = chr * _bytesPerCharacter;
39+
int end = start + _bytesPerCharacter;
40+
if (end > _data.Length)
41+
{
42+
return new ListByte(_space); // character is not defined
43+
}
44+
45+
if (chr == ' ')
46+
{
47+
return new ListByte(_space);
48+
}
49+
50+
// trim the font
51+
while (start < end && _data[start] == 0)
52+
{
53+
start++;
54+
}
55+
56+
while (end > start && _data[end - 1] == 0)
57+
{
58+
end--;
59+
}
60+
61+
return new ListByte(new SpanByte(_data, start, end - start).ToArray());
62+
}
63+
}
64+
}
65+
}

devices/Max7219/Fonts.cs

Lines changed: 1366 additions & 0 deletions
Large diffs are not rendered by default.

devices/Max7219/IFont.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
namespace Iot.Device.Max7219
5+
{
6+
/// <summary>
7+
/// A font contains one list of bytes per character which can be written to the matrix to represent the character.
8+
/// </summary>
9+
/// <remarks>
10+
/// Each character consists of a list of bytes where a single byte represents a column of the display.
11+
/// </remarks>
12+
///
13+
/// <example>
14+
/// This example shows how the 'A' char could by encoded:
15+
/// <code>
16+
/// var aBytes = new byte[] {
17+
/// 0b1111100,
18+
/// 0b1111110,
19+
/// 0b0010011,
20+
/// 0b0010011,
21+
/// 0b1111110,
22+
/// 0b1111100,
23+
/// 0b0000000,
24+
/// 0b0000000
25+
/// };
26+
/// </code>
27+
///
28+
/// </example>
29+
public interface IFont
30+
{
31+
/// <summary>
32+
/// Returns a list of bytes for a given character to be written to a matrix.
33+
/// </summary>
34+
ListByte this[char chr]
35+
{
36+
get;
37+
}
38+
}
39+
}

0 commit comments

Comments
 (0)