// Mikroelektronika 7seg Click MIKROE-1201 (https://www.mikroe.com/7seg-click)
// Services
// Copyright (C)2020-2023, Philip Munts dba Munts Technologies.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
namespace IO.Devices.ClickBoards.SimpleIO.SevenSegment
{
///
/// Encapsulates the Mikroelektronika 7Seg Click Board.
/// MIKROE-1201.
///
///
/// The MISO aka SDI pin should be removed from the
/// 7seg click, because it is not tri-state and will interfere with other
/// devices on the same SPI bus.
///
public class Board
{
// The segments of the Mikroelektronika Seven Segment Display
// Click Board are wired in an odd fashion. The following
// permutation table transforms from standard seven segment
// layout to that of the Click Board.
private static readonly byte[] PermutationTable =
{
0x00, 0x04, 0x02, 0x06, 0x08, 0x0C, 0x0A, 0x0E,
0x10, 0x14, 0x12, 0x16, 0x18, 0x1C, 0x1A, 0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0x91, 0x95, 0x93, 0x97, 0x99, 0x9D, 0x9B, 0x9F,
0xA1, 0xA5, 0xA3, 0xA7, 0xA9, 0xAD, 0xAB, 0xAF,
0xB1, 0xB5, 0xB3, 0xB7, 0xB9, 0xBD, 0xBB, 0xBF,
0xC1, 0xC5, 0xC3, 0xC7, 0xC9, 0xCD, 0xCB, 0xCF,
0xD1, 0xD5, 0xD3, 0xD7, 0xD9, 0xDD, 0xDB, 0xDF,
0xE1, 0xE5, 0xE3, 0xE7, 0xE9, 0xED, 0xEB, 0xEF,
0xF1, 0xF5, 0xF3, 0xF7, 0xF9, 0xFD, 0xFB, 0xFF,
};
// The following glyph pattern values came from:
// https://en.wikipedia.org/wiki/Seven-segment_display
private static readonly byte[] GlyphTable =
{
0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07,
0x7F, 0x6F, 0x77, 0x7C, 0x39, 0x5E, 0x79, 0x71,
};
private readonly IO.Interfaces.GPIO.Pin myRSTgpio;
private readonly IO.Interfaces.GPIO.Pin myPWMgpio;
private readonly IO.Interfaces.PWM.Output myPWMout;
private readonly IO.Devices.SN74HC595.Device mychain;
private Base myradix;
private ZeroBlanking myblanking;
private bool myleftdp = false;
private bool myrightdp = false;
private byte[] outbuf = { 0, 0 };
// Write the output buffer to the display
private void Post()
{
if (myrightdp)
outbuf[0] |= 0x01;
else
outbuf[0] &= 0xFE;
if (myleftdp)
outbuf[1] |= 0x01;
else
outbuf[1] &= 0xFE;
mychain.state = outbuf;
}
///
/// Numeral systems.
///
public enum Base
{
///
/// Base 10.
///
Decimal,
///
/// Base 16.
///
Hexadecimal,
}
///
/// Zero blanking modes.
///
public enum ZeroBlanking
{
///
/// No zero blanking.
///
None,
///
/// Leading zero blanking.
///
Leading,
///
/// Full zero blanking.
///
Full
}
///
/// Constructor for a single 7seg click.
///
/// mikroBUS socket number.
/// Numerical base or radix. Allowed values are
/// Decimal and Hexadecimal.
/// Zero blanking. Allowed values are
/// None, Leading, and Full.
/// PWM frequency. Set to zero to use GPIO
/// instead of PWM.
public Board(int socket, Base radix = Base.Decimal,
ZeroBlanking blanking = ZeroBlanking.None,
int pwmfreq = 100)
{
// Create a mikroBUS socket object
IO.Objects.SimpleIO.mikroBUS.Socket S =
new IO.Objects.SimpleIO.mikroBUS.Socket(socket);
// Configure hardware reset GPIO pin
myRSTgpio = new IO.Objects.SimpleIO.GPIO.Pin(S.RST,
IO.Interfaces.GPIO.Direction.Output, true);
// Issue hardware reset
Reset();
// Configure PWM pin -- Prefer PWM over GPIO, if possible, and
// assume full brightness until otherwise changed.
myPWMgpio = null;
myPWMout = null;
if ((pwmfreq > 0) && (S.PWMOut.available))
{
myPWMout = new IO.Objects.SimpleIO.PWM.Output(S.PWMOut,
pwmfreq, 100.0);
}
else if (S.PWM.available)
{
myPWMgpio = new IO.Objects.SimpleIO.GPIO.Pin(S.PWM,
IO.Interfaces.GPIO.Direction.Output, true);
}
// Configure 74HC595 shift register chain
mychain = new SN74HC595.Device(new IO.Objects.SimpleIO.SPI.Device(S.SPIDev,
IO.Devices.SN74HC595.Device.SPI_Mode, 8,
IO.Devices.SN74HC595.Device.SPI_MaxFreq), 2);
myradix = radix;
myblanking = blanking;
Clear();
}
///
/// Numerical base or radix. Allowed values are Decimal and
/// Hexadecimal.
///
public Base radix
{
get
{
return myradix;
}
set
{
myradix = value;
}
}
///
/// Zero blanking mode. Allowed values are None,
/// Leading, and Full.
///
public ZeroBlanking blanking
{
get
{
return myblanking;
}
set
{
myblanking = value;
}
}
///
/// Write-only property for setting the brightness of the display.
/// Allowed values are 0.0 to 100.0 percent.
///
public double brightness
{
set
{
if (value < IO.Interfaces.PWM.DutyCycles.Minimum)
throw new System.Exception("LED brightness value out of range.");
if (value > IO.Interfaces.PWM.DutyCycles.Maximum)
throw new System.Exception("LED brightness value out of range.");
if (myPWMgpio != null)
myPWMgpio.state = (value != 0.0);
if (myPWMout != null)
myPWMout.dutycycle = value;
}
}
///
/// Write-only property for setting the state of the display.
/// Allowed values are 0 to 99 for decimal mode and 0 to 255
/// for hexadecimal mode.
///
public int state
{
set
{
switch (myradix)
{
case Base.Decimal:
// Validate input range
if ((value < 0) || (value > 99))
throw new System.Exception("Value out of range.");
// Convert digits to seven segment patterns
outbuf[0] = PermutationTable[GlyphTable[value % 10]];
outbuf[1] = PermutationTable[GlyphTable[value / 10]];
// Handle zero blanking
if (value % 10 == 0)
if (myblanking == ZeroBlanking.Full)
outbuf[0] = 0;
if (value / 10 == 0)
if ((myblanking == ZeroBlanking.Full) ||
(myblanking == ZeroBlanking.Leading))
outbuf[1] = 0;
break;
case Base.Hexadecimal:
// Validate input range
if ((value < 0) || (value > 255))
throw new System.Exception("Value out of range.");
// Convert digits to seven segment patterns
outbuf[0] = PermutationTable[GlyphTable[value % 16]];
outbuf[1] = PermutationTable[GlyphTable[value / 16]];
// Handle zero blanking
if (value % 16 == 0)
if (myblanking == ZeroBlanking.Full)
outbuf[0] = 0;
if (value / 16 == 0)
if ((myblanking == ZeroBlanking.Full) ||
(myblanking == ZeroBlanking.Leading))
outbuf[1] = 0;
break;
}
Post();
}
}
///
/// Write-only property for setting the left digit decimal point.
///
public bool leftdp
{
set
{
myleftdp = value;
Post();
}
}
///
/// Write-only property for setting the right digit decimal point.
///
public bool rightdp
{
set
{
myrightdp = value;
Post();
}
}
///
/// Issue hardware reset to the 74HC595 shift register chain.
///
public void Reset()
{
myRSTgpio.state = false;
System.Threading.Thread.Sleep(1);
myRSTgpio.state = true;
}
///
/// Clear the display.
///
public void Clear()
{
outbuf[0] = 0;
outbuf[1] = 0;
myleftdp = false;
myrightdp = false;
Post();
}
}
}