|
YOUR FEEDBACK
|
TOP MICROSOFT .NET LINKS Book Excerpt Making the .NET Micro Framework Work for You
The ultimate flashlight - building a device
By: Donald Thompson; Rob Miles
May. 18, 2007 02:00 PM
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.
To make the flashlight work, the program inside it must be able to perform a number of tasks:
Driving the Output
CREATING AN OUTPUT-PORT OBJECT 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
CONTROLLING OBJECTS 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()) 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 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. MICROSOFT .NET LATEST STORIES
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
|
SYS-CON FEATURED WHITEPAPERS MOST READ THIS WEEK BREAKING NEWS FROM THE WIRES
|
|||||||||||||||||||||||||||||||||||