A Complete Program-A Flashlight Using Interrupts


We can now make a flashlight that operates under interrupt control (Listing 4-2).

Listing 4-2: A .NET Micro Framework flashlight that uses interrupts

image from book
 using System; using Microsoft.SPOT; using Microsoft.SPOT.Hardware; namespace MicroFrameworkFlashlight {    public class MicroFrameworkFlashlight    {       static OutputPort lampOutput;       static InterruptPort switchInterrupt;       public static void Main()       {          Cpu.Pin lampPin = Cpu.Pin.GPIO_Pin1;          Cpu.Pin switchPin = Cpu.Pin.GPIO_Pin2;          lampOutput = new OutputPort(lampPin, false);          switchInterrupt =             new InterruptPort(             switchPin,             false,             Port.ResistorMode.PullDown,             Port.InterruptMode.InterruptEdgeBoth);          switchInterrupt.OnInterrupt +=             new GPIOInterruptEventHandler(switchInterrupt_OnInterrupt);          while (true)          {             System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);          }       }       static void switchInterrupt_OnInterrupt(Cpu.Pin port, bool state, TimeSpan time)       {          lampOutput.Write(state);       }    } } 
image from book

When the Main method is called, it first creates the output port, as before, and then makes an interrupt port to monitor the switch. Next, it binds the interrupt handler to the input event. Having done this, the method has nothing further to do, so it enters a loop that repeatedly calls a Sleep method. This is given an infinite timeout, which means it should never return, so the loop is present just in case it ever does.

When the Sleep method is called, the .NET Micro Framework Library is informed that our program no longer wishes to run. This may result in the processor being placed in a lower power mode, thus saving the battery.

Classes and Data Members

If you compare Listing 4-2 to Listing 4-1, you will notice a few differences. The ports are now declared outside the body of the Main method. This is because methods other than Main require access to these variables; in the case of the flashlight, the switch interrupt-handling code needs to have access to the lamp output port so that it can control it. Within a class, data members are visible to all the methods inside that class.

You can also see that all the members of the class have been made static, which makes them part of the class rather than part of an instance of the class. This makes sense in that we never actually make an instance of the MicroFrameworkFlashlight class when our program runs. In effect, then, these members are shared among all instances of the class. In the case of this particular application, this scenario makes perfect sense, because there is no way we could possibly have multiple instances of these hardware components. However, in some situations you might want each instance of a class to hold information for you, and we will explore that later.

Inheritance and Ports

In the first program, we used an InputPort instance to read the switch. In the second program, we use an InterruptPort instance instead. An interrupt-enabled port has to be able to do everything that an input port can do, and has additional commands and properties concerned with interrupt management. It turns out that the object-oriented nature of C# makes this very easy, in that the implementation of InterruptPort is directly based on InputPort, that is, one class is an extension of the other. When designing your own programs, you can take advantage of this aspect of C# by extending a parent class to produce a child that has customized or additional behaviors.

Objects and Devices

All physical devices supported by the .NET Micro Framework are implemented as software objects. RS-232 serial ports, I2C interfaces, and the like are all accessed by means of an instance of the class that connects to the actual hardware. The class will contain the low-level drivers that actually do the work, but it will expose the abilities of the device in terms of methods that can be called and events that it can raise.




Embedded Programming with the Microsoft .Net Micro Framework
Embedded Programming with the Microsoft .NET Micro Framework
ISBN: 0735623651
EAN: 2147483647
Year: 2007
Pages: 118

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net