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 character support to As1115 + a minor Max7219 fix #394

Merged
merged 1 commit into from
Aug 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ void Initialize(Max7219Mode maxMode)
public void SetNumber(int value, int deviceId = 0)
{
//12345678
if(value > 999999999)
if(value > 99999999)
{
throw new ArgumentOutOfRangeException();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,18 @@ public enum Addresses : byte
/// <summary>
/// The decode mode used for displaying pixels or characters
/// </summary>
public enum DecodeMode : byte
public enum DecodeType : byte
{
/// <summary>
/// Hexicemial charcter encoding for 7-segment displays
/// characters 0 to 9, E, H, L, P, and -
/// </summary>
//Hexidecimal,
Hexidecimal,
/// <summary>
/// BCD character encoding for 7-segment displays
/// characters 0 to 9 and A to F
/// </summary>
//BCD,
BCD,
/// <summary>
/// Direct pixel mapping for 8x8 matrix displays (default)
/// </summary>
Expand Down Expand Up @@ -128,5 +128,155 @@ public enum KeyScanButtonType : byte
/// </summary>
None,
}

/// <summary>
/// BCD Character type
/// </summary>
public enum BcdCharacterType : byte
{
/// <summary>
/// Zero (0)
/// </summary>
_0 = 0x00,
/// <summary>
/// One (1)
/// </summary>
_1 = 0x01,
/// <summary>
/// Two (2)
/// </summary>
_2 = 0x02,
/// <summary>
/// Three (3)
/// </summary>
_3 = 0x03,
/// <summary>
/// Four (4)
/// </summary>
_4 = 0x04,
/// <summary>
/// Five (5)
/// </summary>
_5 = 0x05,
/// <summary>
/// Six (6)
/// </summary>
_6 = 0x06,
/// <summary>
/// Seven (7)
/// </summary>
_7 = 0x07,
/// <summary>
/// Eight (8)
/// </summary>
_8 = 0x08,
/// <summary>
/// Nine (9)
/// </summary>
_9 = 0x09,
/// <summary>
/// Hyphen (-)
/// </summary>
Hyphen = 0x0A,
/// <summary>
/// E
/// </summary>
E = 0x0B,
/// <summary>
/// H
/// </summary>
H = 0x0C,
/// <summary>
/// L
/// </summary>
L = 0x0D,
/// <summary>
/// P
/// </summary>
P = 0x0E,
/// <summary>
/// Space ( )
/// </summary>
Blank = 0x0F,
/// <summary>
/// Count of characters
/// </summary>
Count = 16
}

/// <summary>
/// Hex Character type
/// </summary>
public enum HexCharacterType : byte
{
/// <summary>
/// Zero (0)
/// </summary>
_0 = 0x00,
/// <summary>
/// One (1)
/// </summary>
_1 = 0x01,
/// <summary>
/// Two (2)
/// </summary>
_2 = 0x02,
/// <summary>
/// Three (3)
/// </summary>
_3 = 0x03,
/// <summary>
/// Four (4)
/// </summary>
_4 = 0x04,
/// <summary>
/// Five (5)
/// </summary>
_5 = 0x05,
/// <summary>
/// Six (6)
/// </summary>
_6 = 0x06,
/// <summary>
/// Seven (7)
/// </summary>
_7 = 0x07,
/// <summary>
/// Eight (8)
/// </summary>
_8 = 0x08,
/// <summary>
/// Nine (9)
/// </summary>
_9 = 0x09,
/// <summary>
/// A
/// </summary>
A = 0x0A,
/// <summary>
/// B
/// </summary>
B = 0x0B,
/// <summary>
/// C
/// </summary>
C = 0x0C,
/// <summary>
/// D
/// </summary>
D = 0x0D,
/// <summary>
/// E
/// </summary>
E = 0x0E,
/// <summary>
/// F
/// </summary>
F = 0x0F,
/// <summary>
/// Count of characters
/// </summary>
Count = 16
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

namespace Meadow.Foundation.ICs.IOExpanders
{
/// <summary>
/// Represents an As1115 led driver and key scanner
/// </summary>
public partial class As1115 : IGraphicsDisplay, IDisposable
{
/// <summary>
Expand Down Expand Up @@ -67,6 +70,12 @@ public partial class As1115 : IGraphicsDisplay, IDisposable
/// </summary>
readonly Buffer1bpp buffer = new Buffer1bpp(8, 8);

/// <summary>
/// The display decode mode
/// BCD character / Hex character / Pixel
/// </summary>
public DecodeType DecodeMode { get; private set; }

/// <summary>
/// Is the peripheral disposed
/// </summary>
Expand Down Expand Up @@ -111,7 +120,7 @@ void Initialize()

i2cPeripheral.WriteRegister(REG_SCAN_LIMIT, 0x07);

SetDecodeMode(DecodeMode.Pixel);
SetDecodeMode(DecodeType.Pixel);

byte[] data = new byte[2];

Expand Down Expand Up @@ -225,18 +234,89 @@ public void EnableBlink(bool isEnabled, bool fastBlink = true)
/// </summary>
/// <param name="mode">The decode mode enum</param>
/// Not currently supported - driver is pixel mode only
void SetDecodeMode(DecodeMode mode)
void SetDecodeMode(DecodeType mode)
{
DecodeMode = mode;

buffer.Clear(true);

switch (mode)
{
case DecodeMode.Pixel:
case DecodeType.Pixel:
i2cPeripheral.WriteRegister(REG_DECODE_MODE, 0);
break;
}
}

/// <summary>
/// Set number to display (left aligned)
/// </summary>
/// <param name="value">the number to display</param>
/// <exception cref="ArgumentOutOfRangeException"></exception>
public void SetNumber(int value)
{
//12345678
if (value > 99999999)
{
throw new ArgumentOutOfRangeException();
}

for (int i = 0; i < 8; i++)
{
if(DecodeMode == DecodeType.Hexidecimal)
{
SetCharacter((HexCharacterType)(value % 10), i, false);
}
else
{
SetCharacter((BcdCharacterType)(value % 10), i, false);
}

value /= 10;

if (value == 0)
{
break;
}
}
}

/// <summary>
/// Set a single character
/// </summary>
/// <param name="character">the chracter to display</param>
/// <param name="digit">the digit index starting from the left</param>
/// <param name="showDecimal">show the decimal with the character</param>
public void SetCharacter(BcdCharacterType character, int digit, bool showDecimal = false)
{
if(DecodeMode != DecodeType.BCD)
{
throw new Exception("SetCharacterBcd requires DecodeMode to be BCD");
}

var data = (byte)((byte)character + (showDecimal ? 0x10000000 : 0));

buffer.Buffer[digit] = data;
}

/// <summary>
/// Set a single character
/// </summary>
/// <param name="character">the chracter to display</param>
/// <param name="digit">the digit index starting from the left</param>
/// <param name="showDecimal">show the decimal with the character</param>
public void SetCharacter(HexCharacterType character, int digit, bool showDecimal = false)
{
if (DecodeMode != DecodeType.Hexidecimal)
{
throw new Exception("SetCharacterBcd requires DecodeMode to be Hexidecimal");
}

var data = (byte)((byte)character + (showDecimal ? 0x10000000 : 0));

buffer.Buffer[digit] = data;
}

/// <summary>
/// Set display intensity (0-15)
/// </summary>
Expand Down