Skip to content

Commit

Permalink
Add binding for IS31FL3730 LED driver (#1930)
Browse files Browse the repository at this point in the history
* Binding for IS31FL3731 LED Matrix driver

Co-authored-by: Günther Foidl <gue@korporal.at>
Co-authored-by: Laurent Ellerbach <laurelle@microsoft.com>
  • Loading branch information
3 people authored Feb 12, 2023
1 parent 0716f17 commit 58c8b14
Show file tree
Hide file tree
Showing 16 changed files with 1,237 additions and 0 deletions.
41 changes: 41 additions & 0 deletions src/devices/Is31fl3730/Current.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Iot.Device.Display
{
/// <summary>
/// IS31FL3730 current setting for row output.
/// </summary>
public enum Current
{
/// <summary>
/// 5 mA.
/// </summary>
CmA5 = 0b1000,

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

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

/// <summary>
/// 40 mA (default).
/// </summary>
CmA40 = 0b0000,

/// <summary>
/// 45 mA.
/// </summary>
CmA45 = 0b0001,

/// <summary>
/// 75 mA.
/// </summary>
CmA75 = 0b0111,
}
}
26 changes: 26 additions & 0 deletions src/devices/Is31fl3730/DisplayMode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Iot.Device.Display
{
/// <summary>
/// IS31FL3730 display mode, controlling which matrices are used.
/// </summary>
public enum DisplayMode
{
/// <summary>
/// Enable matrix one only.
/// </summary>
MatrixOneOnly = 0x00,

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

/// <summary>
/// Enable matrix one and two.
/// </summary>
MatrixOneAndTwo = 0x18,
}
}
107 changes: 107 additions & 0 deletions src/devices/Is31fl3730/DotMatrix.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// 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.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
{
private readonly DotMatrix5x7[] _matrices;

/// <summary>
/// Initialize virtual Dot Matrix.
/// </summary>
/// <param name="matrices">The matrices to use.</param>
public DotMatrix(DotMatrix5x7[] matrices)
{
_matrices = matrices;
Width = DotMatrix5x7.BaseWidth * matrices.Length;
}

/// <summary>
/// Width (x-axis) of matrix.
/// </summary>
public int Width { get; }

/// <summary>
/// Height (y-axis) for matrix.
/// </summary>
public int Height { get; } = DotMatrix5x7.BaseHeight;

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

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

/// <summary>
/// Indexer for matrix.
/// </summary>
public PinValue this[int x, int y]
{
get
{
var (matrixIndex, index) = GetMatrixIndex(x, y);
return _matrices[matrixIndex][index, y];
}

set
{
var (matrixIndex, index) = GetMatrixIndex(x, y);
_matrices[matrixIndex][index, y] = value;
}
}

/// <summary>
/// Fill LEDs with value.
/// </summary>
public void Fill(PinValue 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 ArgumentNullException(nameof(i2cDevice));
Is31fl3730 is31fl3730 = new(i2cDevice);
is31fl3730.UpdateConfiguration(ShowdownMode.Normal, MatrixMode.Size8x8, DisplayMode.MatrixOneAndTwo);
return is31fl3730;
}
}
}
50 changes: 50 additions & 0 deletions src/devices/Is31fl3730/DotMatrix10x7.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// 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 an IS31FL3731 LED Matrix driver
/// </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 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 ArgumentNullException(nameof(is31fl3730));
return new DotMatrix5x7[]
{
is31fl3730[1],
is31fl3730[0]
};
}
}
}
60 changes: 60 additions & 0 deletions src/devices/Is31fl3730/DotMatrix5x7.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// 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.Gpio;
using System.Device.I2c;
using System.Threading;

namespace Iot.Device.Display
{
/// <summary>
/// Represents LED Dot Matrix driven by IS31FL3730.
/// </summary>
public class DotMatrix5x7
{
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;
}

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

/// <summary>
/// Fill LEDs with value.
/// </summary>
public void Fill(PinValue value) => _is31fl3730.Fill(_matrix, value);

/// <summary>
/// Fill matrix (0 is dark; 1 is lit).
/// </summary>
public void WriteDecimalPoint(PinValue 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;
}
}
46 changes: 46 additions & 0 deletions src/devices/Is31fl3730/FunctionRegister.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

namespace Iot.Device.Display.Internal
{
/// <summary>
/// Register addresses for the Function Register.
/// </summary>
internal static class Is31fl3730FunctionRegister
{
/// <summary>
/// Address for Configuration Register.
/// </summary>
internal static byte Configuration = 0x0;

/// <summary>
/// Address for Matrix 1 Data Register.
/// </summary>
internal static byte Matrix1 = 0x01;

/// <summary>
/// Address for Update Column Register.
/// </summary>
internal static byte UpdateColumn = 0x0C;

/// <summary>
/// Address for Lighting Effect Register.
/// </summary>
internal static byte LightingEffect = 0x0D;

/// <summary>
/// Address for Matrix 2 Data Register.
/// </summary>
internal static byte Matrix2 = 0x0E;

/// <summary>
/// Address for PWM Register.
/// </summary>
internal static byte Pwm = 0x19;

/// <summary>
/// Address for Reset Register.
/// </summary>
internal static byte Reset = 0xFF;
}
}
Loading

0 comments on commit 58c8b14

Please sign in to comment.