Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add binding for IS31FL3730 LED driver #1930

Merged
merged 48 commits into from Feb 12, 2023
Merged
Show file tree
Hide file tree
Changes from 37 commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
8299345
Binding for IS31FL3731 LED Matrix driver
richlander Sep 21, 2022
ccd9b9a
Add sample
richlander Sep 22, 2022
be62d43
Update sample
richlander Sep 23, 2022
7b78e6f
Update comments
richlander Sep 23, 2022
f0a1458
Add Matrix5x7 class
richlander Sep 24, 2022
1c6130a
Add higher-level bindings
richlander Sep 29, 2022
c795dbf
Rename sample project
richlander Sep 29, 2022
b4c9e37
Rename sample project
richlander Sep 29, 2022
13a95a8
Apply suggestions from code review
richlander Sep 29, 2022
bc4b4a4
Update src/devices/Is31fl3730/Bonnet5x7x6.cs
richlander Sep 29, 2022
565fa6e
Use lists/arrays more
richlander Sep 29, 2022
cd0dbc3
Add MicrDotpHat binding
richlander Sep 29, 2022
e920490
Cleanup
richlander Oct 5, 2022
3e81307
Update per feedback
richlander Nov 30, 2022
f7524ad
Merge branch 'main' into Is31fl3730
richlander Nov 30, 2022
d82f7ff
Add Index/Range support for netstandard2.0
richlander Nov 30, 2022
8f7d961
Enable decimal point
richlander Dec 14, 2022
5b8c341
Update target matrix
richlander Dec 14, 2022
0d1e95f
Add support for Micro Dot pHat
richlander Dec 17, 2022
5923b31
Add Length concept
richlander Dec 17, 2022
a900e7c
Update ordering of matrices
richlander Dec 17, 2022
a3800da
Update README
richlander Dec 17, 2022
bcd92dc
Change comment
richlander Dec 17, 2022
5712706
Merge branch 'main' into Is31fl3730
richlander Dec 17, 2022
ac4d819
Switch to Slice syntax
richlander Dec 17, 2022
230a212
Remove magic numbers
richlander Dec 18, 2022
57eacd2
Remove code duplication
richlander Dec 19, 2022
096bdce
Fix type name
richlander Dec 19, 2022
0992e82
Update README
richlander Dec 19, 2022
6322a24
Switch dimensional fields to properties
richlander Dec 20, 2022
0b24344
Remove code by subclassing
richlander Dec 20, 2022
62459d7
Add more value bounds checking
richlander Dec 20, 2022
4ef5b67
Update bounds checking
richlander Dec 20, 2022
6a34bc0
Add IMatrix interface
richlander Dec 20, 2022
18a685b
Fix markdown error
richlander Dec 20, 2022
c2c6916
Update fill value
richlander Dec 20, 2022
b151d50
Update README
richlander Dec 20, 2022
5fad1d5
Add solution file
richlander Jan 12, 2023
ecab9e3
Update src/devices/Is31fl3730/Current.cs
richlander Jan 16, 2023
bf10f22
Update per feedback
richlander Jan 16, 2023
9396154
Update to PinValue
richlander Jan 16, 2023
de781f6
Update per feedback
richlander Jan 16, 2023
0001fab
Update per feedback
richlander Jan 16, 2023
d0d0964
Remove IMatrix interface
richlander Jan 16, 2023
0921724
Update type names
richlander Jan 17, 2023
2a81f18
Update per feedback
richlander Jan 26, 2023
a4222e8
Fix fill logic
richlander Feb 2, 2023
67ca483
Update per feedback
richlander Feb 2, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions src/devices/Display/Display.csproj
Expand Up @@ -15,6 +15,7 @@
<Compile Include="Large4Digit7SegmentDisplay.cs" />
<Compile Include="BiColorBarGraph.cs" />
<Compile Include="BarColor.cs" />
<Compile Include="IMatrix.cs" />
</ItemGroup>
<ItemGroup>
<None Include="README.md" />
Expand Down
33 changes: 33 additions & 0 deletions src/devices/Display/IMatrix.cs
@@ -0,0 +1,33 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;

namespace Iot.Device.Display
{
/// <summary>
/// Interface for basic LED matrix functionality.
/// </summary>
public interface IMatrix
{
/// <summary>
/// Width (x-axis) of LED matrix.
/// </summary>
int Width { get; }

/// <summary>
/// Height (y-axis) of LED matrix.
/// </summary>
int Height { get; }

/// <summary>
/// Indexer for matrix.
/// </summary>
int this[int x, int y] { get; set; }

/// <summary>
/// Fill LEDs (0 is dark; 255 is all lit).
Ellerbach marked this conversation as resolved.
Show resolved Hide resolved
/// </summary>
public void Fill(int value);
}
}
48 changes: 48 additions & 0 deletions src/devices/Is31fl3730/Current.cs
@@ -0,0 +1,48 @@
// 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.Gpio;
using System.Device.Spi;
using System.Threading;
using System.Threading.Tasks;
using Iot.Device.Multiplexing.Utility;

namespace Iot.Device.Display
{
/// <summary>
/// Represents an IS31FL3731 LED Matrix driver
/// </summary>
public enum Current
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Documentation is apparently wrong. Should be something about (maximum?) LED current.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. I copied the text from the datasheet.

{
/// <summary>
/// 5 mA
/// </summary>
CmA5 = 0b1000,

/// <summary>
/// 10 mA
/// </summary>
CmA10 = 0b1001,

/// <summary>
/// 35 mA
/// </summary>
CmA35 = 0b1110,

/// <summary>
/// 40 mA
/// </summary>
CmA40 = 0b0,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

even if those are just 0, maybe better to align with 4 0 like the rest?


/// <summary>
/// 45 mA
/// </summary>
CMA45 = 0b0001,
richlander marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// 75 mA
/// </summary>
CmA75 = 0b0111,
}
}
33 changes: 33 additions & 0 deletions src/devices/Is31fl3730/DisplayMode.cs
@@ -0,0 +1,33 @@
// 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.Gpio;
using System.Device.Spi;
using System.Threading;
using System.Threading.Tasks;
using Iot.Device.Multiplexing.Utility;

namespace Iot.Device.Display
{
/// <summary>
/// Represents an IS31FL3731 LED Matrix driver
/// </summary>
public enum DisplayMode
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again: This documentation doesn't describe the enum

{
/// <summary>
/// Enable matrix one only.
/// </summary>
MatrixOneOnly = 0x0,

/// <summary>
/// Enable matrix two only.
/// </summary>
MatrixTwoOnly = 0x8,

/// <summary>
/// Enable matrix one and two.
/// </summary>
MatrixOneAndTwo = 0x18,
}
}
98 changes: 98 additions & 0 deletions src/devices/Is31fl3730/DotMatrix.cs
@@ -0,0 +1,98 @@
// 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.Display
{
/// <summary>
/// Represents a virtual LED Matrix.
/// </summary>
// Datasheet: https://cdn-shop.adafruit.com/product-files/3017/31FL3730.pdf
// Product: https://shop.pimoroni.com/products/led-dot-matrix-breakout
// Related repo: https://github.com/pimoroni/microdot-phat
public class DotMatrix : IMatrix
{
private readonly DotMatrix5x7[] _matrices;

/// <summary>
/// Initialize virtual Dot Matrix.
/// </summary>
/// <param name="matrices">The to create with.</param>
Ellerbach marked this conversation as resolved.
Show resolved Hide resolved
public DotMatrix(DotMatrix5x7[] matrices)
{
_matrices = matrices;
Width = DotMatrix5x7.BaseWidth * matrices.Length;
}

/// <inheritdoc/>
public int Width { get; }

/// <inheritdoc/>
public int Height { get; } = DotMatrix5x7.BaseHeight;

/// <summary>
/// Indexer for matrices.
/// </summary>
public DotMatrix5x7 this[int matrix] => _matrices[matrix];

/// <summary>
/// Length (or count) of matrices.
/// </summary>
public int Length => _matrices.Length;

/// <inheritdoc/>
public int this[int x, int y]
{
get
{
var (matrixIndex, index) = GetMatrixIndex(x, y);
return _matrices[matrixIndex][index, y];
}
set
Ellerbach marked this conversation as resolved.
Show resolved Hide resolved
{
var (matrixIndex, index) = GetMatrixIndex(x, y);
_matrices[matrixIndex][index, y] = value;
}
}

/// <inheritdoc/>
public void Fill(int value)
{
foreach (DotMatrix5x7 matrix in _matrices)
{
matrix.Fill(value);
}
}

private (int MatrixIndex, int Index) GetMatrixIndex(int x, int y)
{
if (x < 0 || x >= Width)
{
throw new ArgumentException($"{nameof(x)} value ({x}) out of range.");
}

if (y < 0 || y >= Height)
{
throw new ArgumentException($"{nameof(y)} value ({y}) out of range.");
}

int matrixIndex = x / DotMatrix5x7.BaseWidth;
int index = x % DotMatrix5x7.BaseWidth;
return (matrixIndex, index);
}

/// <summary>
/// Default Is31fl3730 initialization
/// </summary>
public static Is31fl3730 InitializeI2c(I2cDevice i2cDevice)
{
i2cDevice = i2cDevice ?? throw new ArgumentException($"{nameof(i2cDevice)} is null.");
Is31fl3730 is31fl3730 = new(i2cDevice);
is31fl3730.DisplayMode = DisplayMode.MatrixOneAndTwo;
is31fl3730.Initialize();
return is31fl3730;
}
}
}
53 changes: 53 additions & 0 deletions src/devices/Is31fl3730/DotMatrix10x7.cs
@@ -0,0 +1,53 @@
// 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;
using System.Collections.Generic;
using System.Device.I2c;
using System.Threading;

namespace Iot.Device.Display
{
/// <summary>
/// Represents an IS31FL3731 LED Matrix driver
/// </summary>
// Datasheet: https://cdn-shop.adafruit.com/product-files/3017/31FL3730.pdf
Ellerbach marked this conversation as resolved.
Show resolved Hide resolved
// Product: https://shop.pimoroni.com/products/led-dot-matrix-breakout
// Related repo: https://github.com/pimoroni/microdot-phat
public class DotMatrix10x7 : DotMatrix
{
/// <summary>
/// Initialize Dot Matrix Breakout IS31FL3730 device.
/// </summary>
/// <param name="i2cDevice">The <see cref="System.Device.I2c.I2cDevice"/> to create with.</param>
public DotMatrix10x7(I2cDevice i2cDevice)
: this(DotMatrix.InitializeI2c(i2cDevice))
{
}

/// <summary>
/// Initialize Dot Matrix Breakout IS31FL3730 device.
/// </summary>
/// <param name="is31fl3730">The <see cref="Iot.Device.Display.Is31fl3730"/> to create with.</param>
public DotMatrix10x7(Is31fl3730 is31fl3730)
: base(Initialize(is31fl3730))
{
}

/// <summary>
/// Default I2C address for device.
/// </summary>
public const int DefaultI2cAddress = 0x61;

private static DotMatrix5x7[] Initialize(Is31fl3730 is31fl3730)
{
is31fl3730 = is31fl3730 ?? throw new ArgumentException($"{nameof(is31fl3730)} is null");
return new DotMatrix5x7[]
{
is31fl3730[1],
is31fl3730[0]
};
}
}
}
61 changes: 61 additions & 0 deletions src/devices/Is31fl3730/DotMatrix5x7.cs
@@ -0,0 +1,61 @@
// 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.Device.I2c;
using System.Threading;

namespace Iot.Device.Display
{
/// <summary>
/// Represents LED Dot Matrix driven by IS31FL3730.
/// </summary>
public class DotMatrix5x7 : IMatrix
{
private readonly Is31fl3730 _is31fl3730;
private readonly int _matrix;

/// <summary>
/// Initialize Dot Matrix IS31FL3730 device.
/// </summary>
/// <param name="is31fl3730">The <see cref="Iot.Device.Display.Is31fl3730"/> to create with.</param>
/// <param name="matrix">The index of the matrix (of a pair).</param>
public DotMatrix5x7(Is31fl3730 is31fl3730, int matrix)
{
_is31fl3730 = is31fl3730;
_matrix = matrix;
}

/// <inheritdoc/>
public int Width { get; } = BaseWidth;

/// <inheritdoc/>
public int Height { get; } = BaseHeight;

/// <inheritdoc/>
public int this[int x, int y]
{
get => _is31fl3730.Read(_matrix, x, y);
set => _is31fl3730.Write(_matrix, x, y, value);
}

/// <inheritdoc/>
public void Fill(int value) => _is31fl3730.Fill(_matrix, value);

/// <summary>
/// Fill matrix (0 is dark; 1 is lit).
/// </summary>
public void WriteDecimalPoint(int value) => _is31fl3730.WriteDecimalPoint(_matrix, value);

/// <summary>
/// Width of LED matrix (x axis).
/// </summary>
public static readonly int BaseWidth = 5;

/// <summary>
/// Height of LED matrix (y axis).
/// </summary>
public static readonly int BaseHeight = 7;
}
}