Welcome!

Microsoft Cloud Authors: Pat Romanski, Andreas Grabner, Nick Basinger, Kevin Benedict, Liz McMillan

Related Topics: Microsoft Cloud

Microsoft Cloud: Article

Making the .NET Micro Framework Work for You

The ultimate flashlight - building a device

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.
For the purpose of the exercise, we can use the device emulation features of the .NET Micro Framework, so there will be no need to create a physical device, although there is, of course, nothing stopping us from building one if we wish

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.


More Stories By 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.

More Stories By 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

Comments (0)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


IoT & Smart Cities Stories
Moroccanoil®, the global leader in oil-infused beauty, is thrilled to announce the NEW Moroccanoil Color Depositing Masks, a collection of dual-benefit hair masks that deposit pure pigments while providing the treatment benefits of a deep conditioning mask. The collection consists of seven curated shades for commitment-free, beautifully-colored hair that looks and feels healthy.
The textured-hair category is inarguably the hottest in the haircare space today. This has been driven by the proliferation of founder brands started by curly and coily consumers and savvy consumers who increasingly want products specifically for their texture type. This trend is underscored by the latest insights from NaturallyCurly's 2018 TextureTrends report, released today. According to the 2018 TextureTrends Report, more than 80 percent of women with curly and coily hair say they purcha...
The textured-hair category is inarguably the hottest in the haircare space today. This has been driven by the proliferation of founder brands started by curly and coily consumers and savvy consumers who increasingly want products specifically for their texture type. This trend is underscored by the latest insights from NaturallyCurly's 2018 TextureTrends report, released today. According to the 2018 TextureTrends Report, more than 80 percent of women with curly and coily hair say they purcha...
We all love the many benefits of natural plant oils, used as a deap treatment before shampooing, at home or at the beach, but is there an all-in-one solution for everyday intensive nutrition and modern styling?I am passionate about the benefits of natural extracts with tried-and-tested results, which I have used to develop my own brand (lemon for its acid ph, wheat germ for its fortifying action…). I wanted a product which combined caring and styling effects, and which could be used after shampo...
The platform combines the strengths of Singtel's extensive, intelligent network capabilities with Microsoft's cloud expertise to create a unique solution that sets new standards for IoT applications," said Mr Diomedes Kastanis, Head of IoT at Singtel. "Our solution provides speed, transparency and flexibility, paving the way for a more pervasive use of IoT to accelerate enterprises' digitalisation efforts. AI-powered intelligent connectivity over Microsoft Azure will be the fastest connected pat...
There are many examples of disruption in consumer space – Uber disrupting the cab industry, Airbnb disrupting the hospitality industry and so on; but have you wondered who is disrupting support and operations? AISERA helps make businesses and customers successful by offering consumer-like user experience for support and operations. We have built the world’s first AI-driven IT / HR / Cloud / Customer Support and Operations solution.
Codete accelerates their clients growth through technological expertise and experience. Codite team works with organizations to meet the challenges that digitalization presents. Their clients include digital start-ups as well as established enterprises in the IT industry. To stay competitive in a highly innovative IT industry, strong R&D departments and bold spin-off initiatives is a must. Codete Data Science and Software Architects teams help corporate clients to stay up to date with the mod...
At CloudEXPO Silicon Valley, June 24-26, 2019, Digital Transformation (DX) is a major focus with expanded DevOpsSUMMIT and FinTechEXPO programs within the DXWorldEXPO agenda. Successful transformation requires a laser focus on being data-driven and on using all the tools available that enable transformation if they plan to survive over the long term. A total of 88% of Fortune 500 companies from a generation ago are now out of business. Only 12% still survive. Similar percentages are found throug...
Druva is the global leader in Cloud Data Protection and Management, delivering the industry's first data management-as-a-service solution that aggregates data from endpoints, servers and cloud applications and leverages the public cloud to offer a single pane of glass to enable data protection, governance and intelligence-dramatically increasing the availability and visibility of business critical information, while reducing the risk, cost and complexity of managing and protecting it. Druva's...
BMC has unmatched experience in IT management, supporting 92 of the Forbes Global 100, and earning recognition as an ITSM Gartner Magic Quadrant Leader for five years running. Our solutions offer speed, agility, and efficiency to tackle business challenges in the areas of service management, automation, operations, and the mainframe.