| By Donald Thompson, Rob Miles | Article Rating: |
|
| May 18, 2007 02:00 PM EDT | Reads: |
11,881 |
In this excerpt, we'll create the ultimate flashlight. With find-in-the-dark flashing behavior and a power-audit mode, this single-button-controlled device will become the ultimate in personal illumination for the twenty-first century.
Figure 1 shows how the flashlight will be constructed. The .NET Micro Framework device will have an input from a push-button switch and will provide an output to a lamp. It will also have a connection to an external device so that it can transfer usage information to the external device in response to a trigger message.
To make the flashlight work, the program inside it must be able to perform a number of tasks:
- It will need to provide a signal to turn on the lamp.
- It will need to read the state of the input button.
- For some of the later applications, it will need some form of a timer.
- It will need a way of communicating with an external device.
Driving the Output
The output from the flashlight will be a lamp of some kind that will be controlled by a pin configured as an output on the target hardware. The connection to be used on a genuine device would be chosen when the circuit that controls the lamp is designed.
CREATING AN OUTPUT-PORT OBJECT
Most pins on a device are general purpose in that whether they are input or output is controlled by software. To control a simple device like the lamp on our flashlight, we can use an instance of the OutputPort class, which is provided as part of the .NET Micro Framework. It is an object whose job is to manage an output connection to an external device. The code to create the object is as follows:
OutputPort lampOutput = new OutputPort(lampPin, false);
The new keyword causes the creation of an object. An object is an instance of a class. The constructor (the code in a class that sets up the object) needs to know the physical pin the output is to be connected to. This is analogous to telling a file stream which file to use. In this case, it is a constant identifying the output pin. (We will see how this is done in detail later.) The constructor for the OutputPort also needs the initial state for the output port when it is created. The second parameter provides this information. In the preceding code, the pin will initially be set to low, which is specified by the Boolean value false. In other words, when the flashlight starts up, the lamp is not lit.
INSTANCES AND REFERENCES
It is important to understand that in C#, objects are managed by reference. Figure 2 shows what happens when the OutputPort instance is created. Note that we do not create an object with the identifier lampOutput; instead we create a reference with that identifier, which is made to refer to a particular object.
CONTROLLING OBJECTS
We can call methods on our reference to give the instance commands. For example, to set the port output to high, we could use the following code:
lampOutput.Write(true);
The Write method accepts a single Boolean parameter that can either be true or false. Note: Remember that, because of the way the lamp is physically connected to the circuit, setting the output to high (true) might not cause the lamp to illuminate. A hardware designer may use buffers that will have the effect of inverting the sense of the signal, meaning that to light the lamp you might have to set the output to low.
The OutputPort class also contains a method called Read that returns a Boolean value indicating the state of the port.
if (lampOutput.Read())
Debug.Print("output high");
This code also shows the use of the reference Debug. This refers to an instance of an object that implements the debug output stream. This object contains a Print method that can be passed a string to be displayed on the debug channel, which is normally the output window in Microsoft Visual Studio 2005. Precisely how this works, and where the output appears, is not the concern of the user of the Debug object. The output could be sent to a file, transmitted down a serial port, or displayed on a screen.
OutputPort Lamp
lampOutput
This shows a very useful feature of object-based design: our program can use an object that supports a particular set of methods without knowing the details of precisely what it does.
Published May 18, 2007 Reads 11,881
Copyright © 2007 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
About Donald Thompson
Donald Thompson is responsible for overseeing the end-to-end design and day-to-day management of the developers, software, protocols, and technology strategy fueling the SPOT initiative. During the Internet boom, he built the centralized ad serving system used by all MSN Web properties, including Hotmail, MSNBC, and MSN.com. Prior to joining Microsoft, he developed an automated loan kiosk and decisioning system for Citibank, a cellular billing and management system for Bell South, and wrote AI and 3D graphics algorithms for commercial game companies. Before committing to a life of software, he was a professional child actor working in movies and television.
About Rob Miles
Rob Miles is a Microsoft Most Valuable Professional. He is a lecturer and Teaching Fellow in the Department of Computer Science at the University of Hull and is presently responsible for delivering the first year programming course and he also lectures on software development and virtual machine architectures. Visit his blog at www.robmiles.com
- AJAX World RIA Conference & Expo Kicks Off in New York City
- Ulitzer’s Amazing First 30 Days in Public Beta
- RIAs for Web 3.0 Using the Microsoft Platform
- SYS-CON Announces Government IT Conference & Expo
- Building a Composite Application Using Multiple Web Services
- SYS-CON's "Government IT Expo" to Highlight Cloud Computing and SOA
- Amazon, Google, Microsoft - Big Three Cloud Providers Examined
- Will Ulitzer Dominate News Content on The Web? -Gartner
- Windows 7 To Launch Publicly May 5
- Cisco Needs to Buy EMC to Own VMware
- AJAX World RIA Conference & Expo Kicks Off in New York City
- Ulitzer’s Amazing First 30 Days in Public Beta
- RIAs for Web 3.0 Using the Microsoft Platform
- SYS-CON Announces Government IT Conference & Expo
- How Did We Get to Windows 7?
- Building a Composite Application Using Multiple Web Services
- SYS-CON's "Government IT Expo" to Highlight Cloud Computing and SOA
- Amazon, Google, Microsoft - Big Three Cloud Providers Examined
- Will Ulitzer Dominate News Content on The Web? -Gartner
- Micro Focus Offers Micro Focus COBOL for Eclipse
- Google Maps and ASP.NET
- Crystal Reports XI & How It Has Changed
- Creating Controls for.NET Compact Framework in Visual Studio 2005
- Converting VB6 to VB.NET, Part I
- How to Write High-Performance C# Code
- AJAX World RIA Conference & Expo Kicks Off in New York City
- Implementing Tab Navigation with ASP.NET 2.0
- i-Technology Photo Exclusive: Bill Gates & Steve Jobs In "Nerds"
- .NET Archives: Getting Reacquainted with the Father of C#
- i-Technology Viewpoint: "SOA Sucks"






































