Blue Theme Orange Theme Green Theme Red Theme
 
Home | Forums | Videos | Photos | Blogs | Beginners
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Secure BlackBox 8
 Resources  
Close
 Our Network  
Close
Search :       Advanced Search »
Home » Windows Forms » LED Control Emulation

LED Control Emulation


This project describes an approach for constructing a hardware emulation of a segmented seven element LED display. This is the same sort of numeric LED used in microwave ovens, cheap alarm clocks, and even in some not so cheap aircraft. It is most commonly used to display things such as the time, radio frequencies, how much money you have deposited into the soda machine, or just how much it is going to cost to fill up the gas tank.

Author Rank:
Total page views :  17799
Total downloads :  839
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
LEDdisplayCode.zip
 
Become a Sponsor

Introduction: 

This project describes an approach for constructing a hardware emulation of a segmented seven element LED display.  This is the same sort of numeric LED used in microwave ovens, cheap alarm clocks, and even in some not so cheap aircraft.  It is most commonly used to display things such as the time, radio frequencies, how much money you have deposited into the soda machine, or just how much it is going to cost to fill up the gas tank.

Whilst it is not of any particular value to display numeric values with an LED emulation within the context of a modern software application, the control does offer some value in situations where you may wish to build a simulation of an actual piece of hardware.

Figure 1:  LED Control Demonstration

Getting Started:

In order to get started, unzip the attachment and load the solution into Visual Studio 2005.  Examine the solution explorer and note that the solution contains two projects:

Figure 2:  The Solution Explorer Showing the Project Files

The LED display project is a Windows Control Library and it contains a single user control entitled, "LED"; as might expect, this LED.vb control contains the class that defines the custom LED control.  Note also that the project  contains a resources folder; the resources used within this project include the images needed to display the LED's numeric values (0 through 9).

The second project, LED User, is a demonstration project that consumes the control .  This project contains a single windows form class entitled, "frmDemo.vb".  This form contains six LED controls which are updated every 1000 milliseconds to display the current time of day (see Figure 1).

The Code:  LED.vb

The LED.vb control is a user control and as such is has a visual designer; the size of the control was set to 40 pixels wide and 60 pixels tall, a picture box control was dropped onto the container and it was set to dock to the full extent of the container.

To supply the correct visual representation of the numeric values, I merely used ten image files, one for each digit from zero to nine.  I used a common drawing package to define a 40 pixel by 60 pixel drawing, gave it a slight gradient background that was mostly black, and I defined a green filled polygon of the correct size and shape to support building each segment of the seven segment LED display.  To generate each number, I just ghosted out the unused LED segments by setting their fill to a dark gray color.  I did this to make the segment still lightly visible much as it is in an actual LED display.  Naturally, this fill color (green) could have been any color and if you are simulating a red, or amber, or cyan LED, you would want to use that color instead of green.  I used green because green is the color the human eye is most adept at perceiving and because it is a color commonly used in military applications where the use of night vision imaging systems is required (greens in the 450 nanometers of wavelength range do not impair use of the NVIS devices).

To embed the necessary images into the document, I opened up "My Project" from the solution, opened the "Resources" tab, and used the interface to load each of the ten images used to supply the numeric readouts used by the control.  Once added to the project; you will notice that a folder entitled, "Resources" is added to the project; if you look inside the folder, you will see each of the image files.  Selecting any of the image files contained in the folder will reveal its properties in the property grid control; I selected all of the images and set the build action property to "embedded resource" although the images can still be retrieved without performing this last step.

Figure 3:  My Project-Adding image files to the control's resources

With the control added, and the image files packed into the control library's available resources, the only thing left to do is to write the small bit of code necessary to drive the display of the correct image in response to setting the current numeric value for the control.

The "Embed.vb" class contains only the following code:

Public Class LED 

 

    Private Sub LED_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles

    Me.Load

        SetNumber(0)

    End Sub 

 

    Public Sub SetNumber(ByVal num As Integer)

        Select Case num

            Case 0

                pbxLED.Image = My.Resources.zero

            Case 1

                pbxLED.Image = My.Resources.one

            Case 2

                pbxLED.Image = My.Resources.two

            Case 3

                pbxLED.Image = My.Resources.three

            Case 4

                pbxLED.Image = My.Resources.four

            Case 5

                pbxLED.Image = My.Resources.five

            Case 6

                pbxLED.Image = My.Resources.six

            Case 7

                pbxLED.Image = My.Resources.seven

            Case 8

                pbxLED.Image = My.Resources.eight

            Case 9

                pbxLED.Image = My.Resources.nine

            Case Else

                pbxLED.Image = My.Resources.zero

        End Select

    End Sub 

End Class

As you can see, the code required to drive the display is trivial.  The class contains no imports and is declared publicly.  The load event handler initializes the LED emulation to display the numeric value of zero; it does this through a public subroutine entitled, "SetNumber" which in turn accepts a single integer argument representing the number that the control should display.

The "SetNumber" subroutine performs a select case on the integer value passed into the subroutine and depending upon which number was passed in, it selects and loads the appropriate image into the control's picture box control using the images contained in the control's resources.

To make the control a bit more robust, you should evaluate the integer passed into the subroutine to make sure that it is greater than or equal zero, and less than or equal to nine.

That pretty much wraps up the control.  Once an instance of the control is created, the control's "SetNumber" subroutine is used to continuously update the numeric value shown in the control.

The Code:  frmDemo.vb.

The demonstration project contains a single form class ("frmDemo") which is used as a test bed for trying out the new LED control.  To make for a more interesting demonstration, the demo uses six LED controls arranged to look like a digital clock.  These six LED controls are arranged into pairs inside a group box with colons placed between each pair.  The colon is simply two small panels sized and arranged between each pair such that each stacked pair of panels looks like a colon; the same approach could be used to place other non-updatable design elements such as a decimal point if one were to use a similar control to display, for example, a UHF radio frequency.  The background color of the panel was set to green so that it appears to match the LED control (see figure 4).

Figure 4:  Demo Form Control Arrangement

The demo form also contains a timer; the timer's interval is set to 1000 milliseconds and it is enabled; the bulk of what happens in this demonstration piece happens on the timer's tick event and on each 1000 millisecond interval, the values for each LED are set based upon the current time of day.

The class contains the following code:

Public Class frmDemo

 

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As

    System.EventArgs) Handles Timer1.Tick

 

        Dim hrs As String

        Dim mins As String

        Dim secs As String

 

        Dim hours() As Char

        Dim minutes() As Char

        Dim seconds() As Char

 

        Dim hr1 As String

        Dim hr2 As String

        Dim mm1 As String

        Dim mm2 As String

        Dim ss1 As String

        Dim ss2 As String

 

        hrs = Now.Hour.ToString()

        mins = Now.Minute.ToString()

        secs = Now.Second.ToString()

 

        hours = hrs.ToCharArray()

        minutes = mins.ToCharArray()

        seconds = secs.ToCharArray()

 

        If hours.Length > 1 Then

            hr1 = hours(0)

            hr2 = hours(1)

        Else

            hr1 = "0"

            hr2 = hours(0)

        End If

 

        If minutes.Length > 1 Then

            mm1 = minutes(0)

            mm2 = minutes(1)

        Else

            mm1 = "0"

            mm2 = minutes(0)

        End If

 

        If seconds.Length > 1 Then

            ss1 = seconds(0)

            ss2 = seconds(1)

        Else

            ss1 = "0"

            ss2 = seconds(0)

        End If

 

        Led1.SetNumber(Convert.ToInt32(hr1))

        Led2.SetNumber(Convert.ToInt32(hr2))

        Led3.SetNumber(Convert.ToInt32(mm1))

        Led4.SetNumber(Convert.ToInt32(mm2))

        Led5.SetNumber(Convert.ToInt32(ss1))

        Led6.SetNumber(Convert.ToInt32(ss2))

 

    End Sub 

 

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As

    System.EventArgs) Handles btnExit.Click

 

        Me.Dispose()

 

    End Sub 

End Class

The demo class is declared publicly and it contains no imports.  The timer's click event handler captures the current time into three string variables and the current time is split up into hours, minutes, and seconds.  The hours, minutes, and seconds are converted into character arrays and the character array values are checked and then used to set the hours, minutes, and seconds for each of the associated LED controls.  The only other code in the class is used to dispose of the form when the user clicks the Exit button.

That concludes the discussion of the design and code of this application.

Summary.

The project demonstrates a simple approach to building a user control that may be used to emulate LED hardware.  This design provides an example of one way in which a simple .NET application could be used to provide an emulation of an actual piece of hardware.  You could take the example of the clock provided and improve upon it to do something like building an emulation of a VCR or an LED clock; who knows, with a little time and effort maybe you could turn your $17000 laptop into a $12 alarm clock.


Login to add your contents and source code to this article
 About the author
 
Scott Lysle
Freelance software developer residing in Alabama. Bachelors, Masters Degrees from Wichita State University. I spent the first half of my career working on aircraft controls and displays and in that time I worked on the cockpits for the OH-58 AHIP, the AH-1W, the V-22, the F-22, the C-130J, the C-5 AMP, AWACS, JPATS, and a few others. Since 1997 I have been largely involved with Windows and web development, GIS application development, consumer electronics development (embedded linux/java), but still sometimes work on aircraft and military projects, the most recent of which was the presidential transport helicopter. I tend to work primarily with C/C++, Java, VB, and C#.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
SQL and .NET performance profiling in one place
Investigate SQL and .NET code side-by-side with ANTS Performance Profiler 6, so you can see which is causing the problem without switching tools.
Go.NET
Build custom interactive diagrams, network, workflow editors, flowcharts, or software design tools. Includes many predefined kinds of nodes, links, and basic shapes. Supports layers, scrolling, zooming, selection, drag-and-drop, clipboard, in-place editing, tooltips, grids, printing, overview window, palette. 100% implemented in C# as a managed .NET Control. Document/View/Tool architecture with many properties&events. Optional automatic layout.
Dundas Software
Dundas Chart for .NET is the most advanced .NET charting package available today.  With an extremely complete feature set, elegant architecture and easy implementation, Dundas Chart can quickly add advanced Charting functionality to enhance and transform ASP.NET and Windows Forms applications.  Whether you are implementing charting into internal projects, or building applications for clients, Dundas Chart offers advanced technology and advanced results to get the most out of data.
60 FREE UI Controls from DevExpress
Register for your FREE copy on over 60 free presentation controls from DevExpress - Absolutely Free-of-Charge without any royalties or distribution costs. Visit Devexpress.com/60 today. Free controls include advanced lists box, dropdown calendar, rich text edit, spin edit, tab control and so much more!

DevExpress engineers feature rich presentation controls and reporting tools for WinForms, ASP.NET, WPF, and Silverlight. Our technologies help you build your best, see complex software with greater clarity and deliver compelling business solutions for Windows and the web in the shortest possible time.
Clickatell's SMS Gateway
Clickatell's Developer Solutions allow you to SMS enable any website or application via a range of API's. Learn More about our API connections.
Free access to .NET Memory Management video
Everything you need to know about Garbage Collection, Temporary Objects, Fragmentation, Finalization and common causes of memory leaks in .NET. Watch the video here.
Microsoft Visual Studio 2010
Visualize your workspace with new multiple monitor support, powerful Web development, new SharePoint support with tons of templates and Web parts, and more accurate targeting of any version of the .NET Framework. Get set to unleash your creativity.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Developer-Ready ASP.NET 2.0 Web Hosting with 3 MONTHS FREE
Now supporting .NET 3.0 Framework with Windows Workflow Foundation, Windows Communication Foundation (WCF), Windows Presentation Foundation (WPF), windows CardSpace (WCS)! Providing more flexibility for Developers with Web Services Support and a User/Permission Manger. Also supporting MS SQL 2005/2000 with Real-Time Backups, FREE Automated Attach .MDF Tool, FREE SQL Restore and Shrink SQL DB Tools, and SQL
Read the Top 10 Books for Microsoft Developers, 15 Days FREE
Read the Top 10 Books for Microsoft Developers, 15 Days FREE
Try Safari Books Online - 15 Days FREE + 15% Off for 1 Year
Try Safari Books Online - 15 Days FREE + 15% Off for 1 Year
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Nevron Chart
Become a Sponsor
 Comments
a nice idea by Ellen On April 26, 2010
I nice idea. I think it is not only for simulation. It is good to create a nostalgia electronic panel on a modern computer
Ellen
Reply | Email | Delete | Modify | 
viagra by watches On July 11, 2010
With the usage of Viagra for a drug cocktail restores virility because the prescription Cheap Cialis main biological action is to increase the nitric oxide production needed to encourage blood flow to the penis. The result is that a lot of young guys-and more than a few in their 30s, 40s and beyond-are suddenly more able to have sex when Viagra UK is least advisabl
Reply | Email | Delete | Modify | 
ANTS Performance Profiler 6.0
 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Suggest an Idea  |  Media Kit
Current Version: 5.2010.8.14
 © 2010  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.