An Imporved LED Counter


This in an improved version of Keeping Score with LED Counter article originally written by John O'Donnell.

I made some changes to make it more flexible and easier to follow. The function is almost identical to the existing one from John, but about 1/4 the size.

using System;
using System.Drawing;
using System.Drawing.Imaging;
namespace LEDCounter
{
/// <summary>
/// This class creates an LED image based upon a numeric string
/// Uses a flexible byte array versus hard coding the character image creation
/// Based on the LEDCounter from John ODonnell
///
/// Scott Penner - March 28, 2002 - sdpenner@yahoo.com
/// </summary>
public class LED
{
private const int defaultCharacterSize = 31 ;
private byte[,] locationOfLEDs ;
private int characterSize ;
private Color outerColor;
private Color innerColor;
/// <summary>
/// Contructs green LED with default size
/// </summary>
public LED()
{
// Default constructor
characterSize = defaultCharacterSize ;
innerColor = Color.Green;
outerColor = Color.SeaGreen;
FillByteArray();
}
// <summary>
/// Contructs LED with default size and user specified color
/// </summary>
public LED(Color LEDinnerColor, Color LEDouterColor)
{
// Color constructor - sets the inner and outer LED colors
characterSize = defaultCharacterSize ;
innerColor = LEDinnerColor;
outerColor = LEDouterColor;
FillByteArray();
}
/// <summary>
/// Build an image from a numeric string
/// </summary>
/// <param name="number"></param>
/// <returns></returns>
public Image CreateLEDDisplay(string number)
{
int StartPixel = 0;
int StringLength = number.Length;
// Create a rectangular bitmap with string length * character size width and character size height
Image CompleteBitmap = new Bitmap(StringLength * characterSize, characterSize);
// Get graphics object from bitmap
Graphics BitmapGraphicsObject = Graphics.FromImage(CompleteBitmap);
// Loop through the string to build up the bitmap
for(int k=0; k < StringLength ; k++)
{
Image BitmapOfOneCharacter = new Bitmap(characterSize, characterSize);
BitmapOfOneCharacter=CreateLEDDigit(Convert.ToInt32(number.Substring(k,1)));
BitmapGraphicsObject.DrawImage(BitmapOfOneCharacter, StartPixel, 0);
StartPixel += characterSize;
}
BitmapGraphicsObject.Save();
return CompleteBitmap;
}
private void FillByteArray()
{
// Byte array holds the locations of the leds for a particular character
// This can be more easily extended to alphanumeric
// --- 0
// | | 1 2
// --- 3
// | | 4 5
// --- 6
// 0 1 2 3 4 5 6
locationOfLEDs = new byte[10,7] { {1, 1, 1, 0, 1, 1, 1} , // 0
{0, 0, 1, 0, 0, 1, 0} , // 1
{1, 0, 1, 1, 1, 0, 1} , // 2
{1, 0, 1, 1, 0, 1, 1} , // 3
{0, 1, 1, 1, 0, 1, 1} , // 4
{1, 1, 0, 1, 0, 1, 1} , // 5
{1, 1, 0, 1, 1, 1, 1} , // 6
{1, 0, 1, 0, 0, 1, 0} , // 7
{1, 1, 1, 1, 1, 1, 1} , // 8
{1, 1, 1, 1, 0, 1, 1} }; // 9
}
private Image CreateLEDDigit(int val)
{
Image CharacterImage = new Bitmap(characterSize,characterSize);
Graphics g=Graphics.FromImage(CharacterImage);
Pen InnerPen = new Pen(innerColor) ;
Pen OuterPen = new Pen(outerColor) ;
if (val < 0 || val > 9)
return CharacterImage;
// Draw a line for each LED
if (locationOfLEDs[val,0] == 1)
{
//TOP
g.DrawLine(OuterPen,5,2,27,2);
g.DrawLine(InnerPen,4,3,28,3);
g.DrawLine(OuterPen,5,4,27,4);
}
if (locationOfLEDs[val,1] == 1)
{
//TOP LEFT
g.DrawLine(OuterPen,2,5,2,14);
g.DrawLine(InnerPen,3,4,3,15);
g.DrawLine(OuterPen,4,5,4,14);
}
if (locationOfLEDs[val,2] == 1)
{
//TOP RIGHT
g.DrawLine(OuterPen,28,5,28,14);
g.DrawLine(InnerPen,29,4,29,15);
g.DrawLine(OuterPen,30,5,30,14);
}
if (locationOfLEDs[val,3] == 1)
{
//MIDDLE
g.DrawLine(OuterPen,5,15,27,15);
g.DrawLine(InnerPen,4,16,28,16);
g.DrawLine(OuterPen,5,17,27,17);
}
if (locationOfLEDs[val,4] == 1)
{
//BOTTOM LEFT
g.DrawLine(OuterPen,2,18,2,27);
g.DrawLine(InnerPen,3,17,3,28);
g.DrawLine(OuterPen,4,18,4,27);
}
if (locationOfLEDs[val,5] == 1)
{
//BOTTOM RIGHT
g.DrawLine(OuterPen,30,18,30,27);
g.DrawLine(InnerPen,29,17,29,28);
g.DrawLine(OuterPen,28,18,28,27);
}
if (locationOfLEDs[val,6] == 1)
{
//BOTTOM
g.DrawLine(OuterPen,5,28,27,28);
g.DrawLine(InnerPen,4,29,28,29);
g.DrawLine(OuterPen,5,30,27,30);
}
g.Save();
return CharacterImage;
}
}
}