<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>/dev/mobile &#187; .NET Micro Framework</title>
	<atom:link href="http://www.christec.co.nz/blog/archives/category/net-micro-framework/feed" rel="self" type="application/rss+xml" />
	<link>http://www.christec.co.nz</link>
	<description>Development for a mobile world - Making a quality platform one application at a time</description>
	<lastBuildDate>Tue, 27 Oct 2009 09:56:21 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.5</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Using VB.NET with the .NET Micro Framework</title>
		<link>http://www.christec.co.nz/blog/archives/317</link>
		<comments>http://www.christec.co.nz/blog/archives/317#comments</comments>
		<pubDate>Tue, 01 Apr 2008 10:37:06 +0000</pubDate>
		<dc:creator>Christopher Fairbairn</dc:creator>
				<category><![CDATA[.NET Micro Framework]]></category>

		<guid isPermaLink="false">http://www.christec.co.nz/blog/archives/317</guid>
		<description><![CDATA[This blog entry explores if it is possible to run VB.NET code on a .NET Micro Framework device. The current Visual Studio integration for the .NET Micro Framework only supports C# development, but since the framework is based upon the CLR runtime it theoretically supports development in any language that can be compiled down to [...]]]></description>
			<content:encoded><![CDATA[<p>This blog entry explores if it is possible to run VB.NET code on a .NET Micro Framework device. The current Visual Studio integration for the .NET Micro Framework only supports C# development, but since the framework is based upon the CLR runtime it theoretically supports development in any language that can be compiled down to Microsoft Intermediate Language (MSIL) code. So can we compile a VB.NET application for a .NET Micro Framework device and if so what kind of limitations or issues are we likely to come across? Read on to find out the results of some early experiments&#8230;</p>
<h3>A sample application</h3>
<p>The first thing we need is some VB.NET code to attempt to compile. For this I have written the following WPF application.</p>

<div class="wp_syntax"><div class="code"><pre class="vbnet" style="font-family:monospace;"><span style="color: #0600FF;">Imports</span> System
<span style="color: #0600FF;">Imports</span> Microsoft.<span style="color: #0000FF;">Spot</span>
<span style="color: #0600FF;">Imports</span> Microsoft.<span style="color: #0000FF;">SPOT</span>.<span style="color: #0000FF;">Presentation</span>
<span style="color: #0600FF;">Imports</span> Microsoft.<span style="color: #0000FF;">SPOT</span>.<span style="color: #0000FF;">Presentation</span>.<span style="color: #0000FF;">Media</span>
<span style="color: #0600FF;">Imports</span> Microsoft.<span style="color: #0000FF;">SPOT</span>.<span style="color: #0000FF;">Presentation</span>.<span style="color: #0000FF;">Shapes</span>
&nbsp;
<span style="color: #0600FF;">NameSpace</span> HelloWorldVB
  <span style="color: #FF8000;">Public</span> <span style="color: #0600FF;">Class</span> Program
    <span style="color: #FF8000;">Public</span> <span style="color: #FF8000;">Shared</span> <span style="color: #0600FF;">Sub</span> Main<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
      <span style="color: #0600FF;">Dim</span> app <span style="color: #FF8000;">as</span> Application <span style="color: #008000;">=</span> <span style="color: #FF8000;">New</span> Application<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
&nbsp;
      <span style="color: #008080; font-style: italic;">' Create a window object and set the size to</span>
      <span style="color: #008080; font-style: italic;">' the size of the display</span>
      <span style="color: #0600FF;">Dim</span> mainWindow <span style="color: #FF8000;">as</span> Window<span style="color: #008000;">=</span> <span style="color: #FF8000;">New</span> Window<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
      mainWindow.<span style="color: #0000FF;">Height</span> <span style="color: #008000;">=</span> SystemMetrics.<span style="color: #0000FF;">ScreenHeight</span>
      mainWindow.<span style="color: #0600FF;">Width</span> <span style="color: #008000;">=</span> SystemMetrics.<span style="color: #0000FF;">ScreenWidth</span>
&nbsp;
      <span style="color: #008080; font-style: italic;">' Add a green rectangle as the sole child</span>
      <span style="color: #008080; font-style: italic;">' control of the window</span>
      <span style="color: #0600FF;">Dim</span> rect <span style="color: #FF8000;">As</span> Rectangle <span style="color: #008000;">=</span> <span style="color: #FF8000;">new</span> Rectangle<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
      rect.<span style="color: #0000FF;">Fill</span> <span style="color: #008000;">=</span> <span style="color: #FF8000;">new</span> SolidColorBrush<span style="color: #000000;">&#40;</span><span style="color: #0600FF;">CType</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">&amp;</span>HFF00, Color<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
      mainWindow.<span style="color: #0000FF;">Child</span> <span style="color: #008000;">=</span> rect
&nbsp;
      <span style="color: #008080; font-style: italic;">' Make the window visible</span>
      mainWindow.<span style="color: #0000FF;">Visibility</span> <span style="color: #008000;">=</span> Visibility.<span style="color: #0000FF;">Visible</span>
      app.<span style="color: #0000FF;">Run</span><span style="color: #000000;">&#40;</span>mainWindow<span style="color: #000000;">&#41;</span>
   <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Sub</span>
  <span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Class</span>
<span style="color: #0600FF;">End</span> <span style="color: #0600FF;">Namespace</span></pre></div></div>

<p>This application simply displays a full screen window that is coloured Green. This is a reasonably simple application that doesn&#8217;t make use of too many VB.NET language or .NET Micro Framework features so is an ideal starting point.</p>
<h3>Compiling the application</h3>
<p>The first step is to use the standard <a href="http://msdn2.microsoft.com/en-us/library/s4kbxexc.aspx">VB.NET command line compiler</a> (vbc.exe) to compile the source code into a standard executable. We can do this by using the following commands at a command prompt:</p>

<div class="wp_syntax"><div class="code"><pre class="batch" style="font-family:monospace;">SET NET_BIN=C:\Windows\Microsoft.NET\Framework\v2.0.50727
SET MF_BIN=C:\Program Files\Microsoft .NET Micro Framework\v2.0.3036\Tools
SET MF_LIB=C:\Program Files\Microsoft .NET Micro Framework\v2.0.3036\Assemblies
&nbsp;
&quot;%NET_BIN%\vbc.exe&quot;
  /netcf
  /define:_MYTYPE=\&quot;EMPTY\&quot;
  /reference:&quot;%MF_LIB%\Microsoft.SPOT.Graphics.dll&quot;
  /reference:&quot;%MF_LIB%\Microsoft.SPOT.TinyCore.dll&quot;
  /out:HelloWorldVB.exe
  /target:exe Program.vb</pre></div></div>

<p>This is all fairly standard stuff for command line VB.NET compilation, however there are two things to note. First we have used the <a href="http://msdn2.microsoft.com/en-us/library/s7wb4kfs.aspx">/netcf</a> argument which causes the compiler to target the .NET Compact Framework. This removes language functionality (such as late binding) that depend upon aspects of the full .NET framework.</p>
<p>The second thing to note is the define which sets _MYTYPE to EMPTY. As discussed by the &#8220;<a href="http://msdn2.microsoft.com/en-us/library/ms233781.aspx">Customizing Which Objects are Available in My</a>&#8221; article on MSDN setting this define to EMPTY will cause the VB.NET compiler to not generate a <a href="http://msdn2.microsoft.com/en-us/magazine/cc163972.aspx">My namespace</a>. The My namespace is typically used by VB.NET developers to get quick access to convenience functions for things such as File IO and networking functionality, but the code emitted by the VB.NET compiler is not suitable for the .NET Micro Framework base class libraries.</p>
<h3>Transforming the executable</h3>
<p>Now that we have compiled the VB.NET code into MSIL we need to run the executable through an application called the Meta Data Processor. This pre-processes the MSIL based executable into a more efficient and smaller format that is suitable for use by the interpreter that forms the core part of the .NET Micro Framework runtime.</p>
<p>The following command line will convert the <i>HelloWorldVB.exe</i> executable into a <i>HelloWorldVB.pe</i> file that is suitable for execution by the .NET Micro Framework.</p>

<div class="wp_syntax"><div class="code"><pre class="batch" style="font-family:monospace;">&quot;%MF_BIN%\MetaDataProcessor.exe&quot;
  -loadHints mscorlib &quot;%MF_LIB%\mscorlib.dll&quot;
  -parse HelloWorldVB.exe
  -minimize
  -compile HelloWorldVB.pe</pre></div></div>

<p>You should notice that <i>HelloWorldVB.pe</i> is significantly smaller than <i>HelloWorldVB.exe</i> and even while viewing it using Notepad it clearly shows that there is less unnecessary &#8220;baggage&#8221;. For instance there is no string stating &#8220;This program cannot be run in DOS mode&#8221;.</p>
<h3>Running the application</h3>
<p>Now that we finally have what appears to be a compiled VB.NET application in a form that is usable by the .NET Micro Framework we need to figure out a way to load it into our .NET Micro Framework emulator or device.</p>
<p>The following command line launches <i>HelloWorldVB.pe</i> within the sample emulator that comes with the .NET Micro Framework SDK.</p>

<div class="wp_syntax"><div class="code"><pre class="batch" style="font-family:monospace;">&quot;%MF_BIN%/Microsoft.SPOT.Emulator.Sample.SampleEmulator.exe&quot;
  /load:HelloWorldVB.pe
  /load:&quot;%MF_LIB%\mscorlib.pe&quot;
  /load:&quot;%MF_LIB%\Microsoft.SPOT.Native.pe&quot;
  /load:&quot;%MF_LIB%\Microsoft.SPOT.TinyCore.pe&quot;
  /load:&quot;%MF_LIB%\Microsoft.SPOT.Graphics.pe&quot;</pre></div></div>

<p>At this point in time I have not investigated how to deploy *.pe files to actual devices.</p>
<h3>Conclusion</h3>
<p>This experiment demonstrates that at a technical level it should be possible for the .NET Micro Framework to eventually support the execution of applications written in VB.NET. It does however demonstrate that at present there are a couple of hurdles to overcome, mainly in the code generated by the VB.NET compiler having dependencies on framework functionality not present by the .NET Micro Framework. Not being a VB.NET expert I wonder how far you can go before finding other problems? My next step is to figure out how to deploy *.pe files to an actual device so that I can verify that <i>HelloWorldVB.pe</i> actually does run on a physical device.</p>
<p>I am also interested in investigating the MetaDataProcessor.exe application in further depth. Is anyone aware of additional documentation for this tool? By running it from the command line it is apparent that it has a wide range of command line arguments, but I have presently been unable to find information about it usage.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.christec.co.nz/blog/archives/317/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>WPF &#8220;Hello World&#8221; for the .NET Micro Framework</title>
		<link>http://www.christec.co.nz/blog/archives/174</link>
		<comments>http://www.christec.co.nz/blog/archives/174#comments</comments>
		<pubDate>Tue, 27 Nov 2007 10:03:59 +0000</pubDate>
		<dc:creator>Christopher Fairbairn</dc:creator>
				<category><![CDATA[.NET Micro Framework]]></category>

		<guid isPermaLink="false">http://www.christec.co.nz/blog/archives/174</guid>
		<description><![CDATA[
The first time I heard that the .NET Micro Framework&#8217;s GUI framework was Windows Presentation Foundation (WPF) I was excited but wanted to know more. How could such a &#8220;big&#8221; framework be compressed for use on such a small device, with little to no OS for support?
This blog entry discusses the structure of simple WPF [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.christec.co.nz/blog/wp-content/uploads/2007/11/wpf-hello-world.png" alt="Screenshot of WPF Hello World application running within an emulator" class="center" /></p>
<p>The first time I heard that the .NET Micro Framework&#8217;s GUI framework was Windows Presentation Foundation (WPF) I was excited but wanted to know more. How could such a &#8220;big&#8221; framework be compressed for use on such a small device, with little to no OS for support?</p>
<p>This blog entry discusses the structure of simple WPF applications written for the .NET Micro Framework and outlines some of the differences when compared against the WPF framework found on the desktop.</p>
<p>The WPF implementation found within the .NET Micro Framework shares many of the same underlying structures and concepts as the desktop version of WPF, but the implementation and capabilities are completely different. For example there is no declarative markup language (XAML), no data binding, no visual form designer and the namespaces are slightly different.</p>
<p><b>Getting Started</b><br />
To get started create a new .NET Micro Framework Console application from within Visual Studio 2005 and delete the contents of Program.cs. We would typically use the &#8220;Windows Application&#8221; template, but since we want to learn the purpose of the code generated by that template we will code it all from scratch.</p>
<p><img src="http://www.christec.co.nz/blog/wp-content/uploads/2007/11/wpf-references.png" class="alignright" />We also need to add a couple of additional references to our Project in order for the compiler to find the WPF related classes:</p>
<ul>
<li>Microsoft.SPOT.TinyCore</li>
<li>Microsoft.SPOT.Graphics</li>
</ul>
<p><b>Creating a window</b><br />
A window is represented by the <a href="http://msdn2.microsoft.com/en-us/library/microsoft.spot.presentation.window.aspx">Microsoft.SPOT.Presentation.Window</a> class. Typically a subclass of this class will be produced to encapsulate as much window specific logic as possible for each type of window within our application. Since we don&#8217;t have a Visual Form designer or XAML we must hand code this in C# directly.</p>
<p>Our main window could be represented as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">Microsoft.SPOT.Presentation</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">Microsoft.SPOT.Presentation.Media</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> MainWindow <span style="color: #008000;">:</span> Window
<span style="color: #000000;">&#123;</span>
  <span style="color: #0600FF;">public</span> MainWindow<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> 
  <span style="color: #000000;">&#123;</span> 
    <span style="color: #008080; font-style: italic;">// Make the window the same size as the LCD screen</span>
    <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">Width</span> <span style="color: #008000;">=</span> SystemMetrics.<span style="color: #0000FF;">ScreenWidth</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">Height</span> <span style="color: #008000;">=</span> SystemMetrics.<span style="color: #0000FF;">ScreenHeight</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">// Make the window have a red background</span>
    <span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">Background</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> SolidColorBrush<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>Color<span style="color: #000000;">&#41;</span>0x0000FF<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>The constructor provides an ideal place to configure the look and feel of our window. In this example we configure the initial width and height of our window to be the same size as the LCD screen found on our device. We also give it a solid red background fill.</p>
<p><b>Creating an Application</b><br />
A WPF application is represented by the <a href="http://msdn2.microsoft.com/en-us/library/microsoft.spot.application.aspx">Microsoft.SPOT.Application</a> class. This class manages the display of windows and routes events from the hardware to the active window&#8217;s controls etc. </p>
<p>To display our main window on the screen we must create an instance of our application and call it&#8217;s Run method, passing in the main window. Like standard desktop .NET applications, the main entry point of an application is its static Main method and this provides an ideal place to perform this initialisation step as shown below:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span> 
<span style="color: #0600FF;">using</span> <span style="color: #008080;">Microsoft.SPOT</span><span style="color: #008000;">;</span> 
&nbsp;
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Program <span style="color: #008000;">:</span> Application 
<span style="color: #000000;">&#123;</span>
  <span style="color: #008080; font-style: italic;">// This is the method the .NET Micro Framework</span>
  <span style="color: #008080; font-style: italic;">// will call when our application starts running </span>
  <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> Main<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> 
  <span style="color: #000000;">&#123;</span> 
    <span style="color: #008080; font-style: italic;">// Create an instance of the Application</span>
    <span style="color: #008080; font-style: italic;">// and our main Window class</span>
    Application myApplication <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Program<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> 
    Window myWindow <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> MainWindow<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> 
&nbsp;
    <span style="color: #008080; font-style: italic;">// Ask the application to display the</span>
    <span style="color: #008080; font-style: italic;">// initial window, and don't return until</span>
    <span style="color: #008080; font-style: italic;">// it is closed.</span>
    myApplication.<span style="color: #0000FF;">Run</span><span style="color: #000000;">&#40;</span>myWindow<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>When you run this application you should see a solid red window covering the entire screen. It&#8217;s pretty boring! Since there is no way (presently) to close this window the call to myApplication.Run will never return.</p>
<p><b>Adding content</b><br />
A window can hold a single control, this is because it derives from <a href="http://msdn2.microsoft.com/en-us/library/microsoft.spot.presentation.controls.contentcontrol.aspx">ContentControl</a>. This class provides a Child property to specify the control which should be displayed within the client area of the window.</p>
<p>Most of the standard controls can be found within the <a href="http://msdn2.microsoft.com/en-us/library/microsoft.spot.presentation.controls.aspx">Microsoft.SPOT.Presentation.Controls</a> namespace. The two we will cover in this blog posting are the <a href="http://msdn2.microsoft.com/en-us/library/microsoft.spot.presentation.controls.text.aspx">Text</a> and <a href="http://msdn2.microsoft.com/en-us/library/microsoft.spot.presentation.controls.image.aspx">Image</a> controls.</p>
<dl>
<dt>Displaying Text</dt>
<dd>To place a string of text on a window we can use the following code snippet at the end of our Window&#8217;s constructor:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">Font font <span style="color: #008000;">=</span> Resources.<span style="color: #0000FF;">GetFont</span><span style="color: #000000;">&#40;</span>Resources.<span style="color: #0000FF;">FontResources</span>.<span style="color: #0000FF;">small</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">Child</span> <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Text<span style="color: #000000;">&#40;</span>font, <span style="color: #666666;">&quot;Hello World!&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>This code refers to a font called <i>&#8220;small&#8221;</i>. The .NET Micro Framework by default has no fonts, so we must include this font resource as an embedded resource within our application. We do this by following these steps:</p>
<ol>
<li>Open Resources.resx within the designer</li>
<li>Open the Files section and select to add an existing file</li>
<li>Navigate to the C:\Program Files\Microsoft .Net Micro Framework\v2.0.2036\Fonts\ directory and select &#8220;small.tinyfnt&#8221;.</li>
</ol>
<p>By default there are only a couple of fonts to choose from. With Service Pack 1 of the .NET Micro Framework there is also a conversion utility which allows you to convert any TrueType font you may have into the required format.
</dd>
<dt>Displaying an Image</dt>
<dd>To place an image on a window we make use of the Image control. The easiest way is to simply pass the bitmap we want to display in as a constructor parameter as shown in the below code snippet:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">Bitmap bmp <span style="color: #008000;">=</span> Resources.<span style="color: #0000FF;">GetBitmap</span><span style="color: #000000;">&#40;</span>Resources.<span style="color: #0000FF;">BitmapResources</span>.<span style="color: #0000FF;">sample</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
Image img <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Image<span style="color: #000000;">&#40;</span>bmp<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">this</span>.<span style="color: #0000FF;">Child</span> <span style="color: #008000;">=</span> img<span style="color: #008000;">;</span></pre></div></div>

<p>We could use the <a href="http://www.christec.co.nz/blog/archives/175">bitmap drawing techniques</a> discussed last time to dynamically create a bitmap to display, or we could embedded an image into our executable. To embedded an image such as the &#8220;sample&#8221; image in the above example, we follow a similar process to adding a font to our resources.resx file, only this time we select the &#8220;Image&#8221; section instead of the &#8220;Files&#8221; section.</dd>
</dl>
<p>By default the image or text will be rendered in the top left corner of the area provided by the window. By changing the <a href="http://msdn2.microsoft.com/en-us/library/microsoft.spot.presentation.uielement.horizontalalignment.aspx">HorizontalAlignment</a> and <a href="http://msdn2.microsoft.com/en-us/library/microsoft.spot.presentation.uielement.verticalalignment.aspx">VerticalAlignment</a> properties we can modify this to move the image into the center of the area given to the control. For example to center the image in the previous example we could add the following two lines of code to the Window&#8217;s constructor:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">img.<span style="color: #0000FF;">VerticalAlignment</span> <span style="color: #008000;">=</span> VerticalAlignment.<span style="color: #0000FF;">Center</span><span style="color: #008000;">;</span> 
img.<span style="color: #0000FF;">HorizontalAlignment</span> <span style="color: #008000;">=</span> HorizontalAlignment.<span style="color: #0000FF;">Center</span><span style="color: #008000;">;</span></pre></div></div>

<p><b>Sample Application</b></p>
<blockquote><p>[<a href="http://www.christec.co.nz/blog/wp-content/uploads/2007/11/wpf-hello-world.zip">Download wpf-hello-world.zip - 6.7 KB</a>]</p></blockquote>
<p>A small sample application is available for download. This application demonstrates the techniques outlined within this blog entry and displays the classic &#8220;<a href="http://en.wikipedia.org/wiki/Hello_world_program">Hello World</a>&#8221; message to the user.</p>
<p>The next post on the .NET Micro Framework will discuss how to add more than one control to a window and how to control the size and location of these controls. After that posting we will finally be at the stage where we are ready to discuss how to generate and respond to keyboard or button press events. This will also enable us to discuss how to create multiple windows and close them etc.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.christec.co.nz/blog/archives/174/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Drawing shapes with the .NET Micro Framework</title>
		<link>http://www.christec.co.nz/blog/archives/175</link>
		<comments>http://www.christec.co.nz/blog/archives/175#comments</comments>
		<pubDate>Mon, 19 Nov 2007 11:00:03 +0000</pubDate>
		<dc:creator>Christopher Fairbairn</dc:creator>
				<category><![CDATA[.NET Micro Framework]]></category>

		<guid isPermaLink="false">http://www.christec.co.nz/blog/archives/175</guid>
		<description><![CDATA[This blog entry is a lead in to a new series of articles about developing GUIs with the .NET Micro Framework.
A good place to start when learning a new GUI framework is to learn how to draw simple graphics. This blog entry discusses how to draw simple graphics with the .NET Micro Framework.
Creating a Bitmap
The [...]]]></description>
			<content:encoded><![CDATA[<p>This blog entry is a lead in to a new series of articles about developing GUIs with the .NET Micro Framework.</p>
<p>A good place to start when learning a new GUI framework is to learn how to draw simple graphics. This blog entry discusses how to draw simple graphics with the .NET Micro Framework.</p>
<p><b>Creating a Bitmap</b><br />
The <a href="http://msdn2.microsoft.com/en-us/library/microsoft.spot.aspx">Microsoft.SPOT namespace</a> contains a <a href="http://msdn2.microsoft.com/en-us/library/microsoft.spot.bitmap.aspx">Bitmap</a> class which represents a bitmap image. To create a bitmap the same size as your physical screen you could use a code snippet such as the following:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">Microsoft.SPOT</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">Microsoft.SPOT.Presentation</span><span style="color: #008000;">;</span>
&nbsp;
Bitmap bmp <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Bitmap<span style="color: #000000;">&#40;</span>SystemMetrics.<span style="color: #0000FF;">ScreenWidth</span>,
     SystemMetrics.<span style="color: #0000FF;">ScreenHeight</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>Once you have a bitmap you can draw on it by using the various instance <a href="http://msdn2.microsoft.com/en-us/library/microsoft.spot.bitmap_members.aspx">methods</a> of the Bitmap class. When your drawing is completed, you need to copy the bitmap to the LCD screen in order for it to become visible. The framework provides a <a href="http://msdn2.microsoft.com/en-us/library/bb285517.aspx">Flush()</a> method to perform this task. Calling Flush() on your bitmap will copy the bitmap data to the LCD screen.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">Bitmap bmp <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Bitmap<span style="color: #000000;">&#40;</span>SystemMetrics.<span style="color: #0000FF;">ScreenWidth</span>,
     SystemMetrics.<span style="color: #0000FF;">ScreenHeight</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008080; font-style: italic;">// ... do drawing stuff here ...</span>
bmp.<span style="color: #0000FF;">Flush</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// copy bitmap to LCD</span></pre></div></div>

<p>It is important to note that to use the Flush() method your bitmap must be exactly the same size as the LCD display. Otherwise the flush will simply not work, even though no exception or debug diagnostic will indicate a problem while debugging. This is a common trend with many of the .NET Micro Framework Base Class Library methods.</p>
<p><b>Representing Colours</b><br />
A colour is represented by the <a href="http://msdn2.microsoft.com/en-us/library/microsoft.spot.presentation.media.color.aspx">Color</a> enumeration found within the  <a href="http://msdn2.microsoft.com/en-us/library/microsoft.spot.presentation.media.aspx">Microsoft.SPOT.Presentation.Media</a> namespace.</p>
<p>This enumeration only has the values Black and White pre-defined. For example to specify the colour White you could use a code snippet such as the following:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">Microsoft.SPOT.Presentation.Media</span><span style="color: #008000;">;</span>
Color white <span style="color: #008000;">=</span> Color.<span style="color: #0000FF;">White</span><span style="color: #008000;">;</span></pre></div></div>

<p>It is possible to specify other colours by specifying the red, green and blue intensity values that make up the desired colour. To do this you use a static method within the <a href="http://msdn2.microsoft.com/en-us/library/microsoft.spot.presentation.media.colorutility.aspx">ColorUtility</a> class called <a href="http://msdn2.microsoft.com/en-us/library/microsoft.spot.presentation.media.colorutility.colorfromrgb.aspx">ColorFromRGB</a> as shown below:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">Microsoft.SPOT.Presentation.Media</span><span style="color: #008000;">;</span>
<span style="color: #008080; font-style: italic;">// Specify full intensity red</span>
Color red <span style="color: #008000;">=</span> ColorUtility.<span style="color: #0000FF;">ColorFromRGB</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">255</span>, <span style="color: #FF0000;">0</span>, <span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>The parameters passed to ColorFromRGB are the Red, Green and Blue components of the desired colour. These values are all bytes which range from 0 to 255 (full brightness).</p>
<p>ColorFromRGB basically encapsulates some simple bit shifts and a typecast. Internally the .NET Micro Framework represents colours as 3 8bit fields packed into a single 32bit unsigned integer. Instead of using the ColorFromRGB method we can perform a manual typecast between a suitable number and the Color enumeration as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">Microsoft.SPOT.Presentation.Media</span><span style="color: #008000;">;</span>
<span style="color: #008080; font-style: italic;">//                 0xBBGGRR</span>
Color red <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span>Color<span style="color: #000000;">&#41;</span>0x0000FF<span style="color: #008000;">;</span></pre></div></div>

<p>The format when the value is expressed in hexadecimal is 0xBBGGRR, i.e. 8 bits red (R), 8 bits green (G), followed by 8 bits blue (B). So the above example creates a red colour with full intensity.</p>
<p><b>Drawing Shapes</b><br />
The bitmap class has numerous methods available for drawing the outlines of basic shapes such as lines, rectangles and ellipses.</p>
<dl>
<dt>Drawing lines:</dt>
<dd><img src='http://www.christec.co.nz/blog/wp-content/uploads/2007/11/draw_line.png' alt='draw_line.png' class="alignright" /></p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">// Draw a red line 10 pixels thick</span>
<span style="color: #008080; font-style: italic;">// between (x=20, y=30) and (x=40, y=50).</span>
bmp.<span style="color: #0000FF;">DrawLine</span><span style="color: #000000;">&#40;</span>red, <span style="color: #008080; font-style: italic;">// colour</span>
     <span style="color: #FF0000;">10</span>,  <span style="color: #008080; font-style: italic;">// thickness</span>
     <span style="color: #FF0000;">20</span>,  <span style="color: #008080; font-style: italic;">// x0</span>
     <span style="color: #FF0000;">30</span>,  <span style="color: #008080; font-style: italic;">// y0</span>
     <span style="color: #FF0000;">40</span>,  <span style="color: #008080; font-style: italic;">// x1</span>
     <span style="color: #FF0000;">50</span><span style="color: #000000;">&#41;</span> <span style="color: #008080; font-style: italic;">// y1;</span></pre></div></div>

<p>A line is specified by providing the colour, thickness and start and end co-ordinates of the line. The current implementation of the .NET Micro Framework base class library appears to ignore the thickness parameter, all lines are drawn 1 pixel wide.</dd>
<dt>Drawing rectangles:</dt>
<dd><img src='http://www.christec.co.nz/blog/wp-content/uploads/2007/11/draw_rectangle.png' alt='draw_rectangle.png' class="alignright" /></p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">// Draw a rectangle which is 40 pixels</span>
<span style="color: #008080; font-style: italic;">// wide and 50 pixels high. The top left</span>
<span style="color: #008080; font-style: italic;">// corner is at (x=20, y=30). The outline is</span>
<span style="color: #008080; font-style: italic;">// 10 pixels wide in red. </span>
bmp.<span style="color: #0000FF;">DrawRectangle</span><span style="color: #000000;">&#40;</span>red, <span style="color: #008080; font-style: italic;">// outline colour</span>
     <span style="color: #FF0000;">10</span>, <span style="color: #008080; font-style: italic;">// outline thickness</span>
     <span style="color: #FF0000;">20</span>, <span style="color: #008080; font-style: italic;">// x</span>
     <span style="color: #FF0000;">30</span>, <span style="color: #008080; font-style: italic;">// y</span>
     <span style="color: #FF0000;">40</span>, <span style="color: #008080; font-style: italic;">// width</span>
     <span style="color: #FF0000;">50</span>, <span style="color: #008080; font-style: italic;">// height</span>
     <span style="color: #FF0000;">0</span>,  <span style="color: #008080; font-style: italic;">// xCornerRadius,</span>
     <span style="color: #FF0000;">0</span>,  <span style="color: #008080; font-style: italic;">// yCornerRadius,</span>
     <span style="color: #FF0000;">0</span>, <span style="color: #FF0000;">0</span>, <span style="color: #FF0000;">0</span>, <span style="color: #FF0000;">0</span>, <span style="color: #FF0000;">0</span>, <span style="color: #FF0000;">0</span>, <span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></div>

<p>Drawing a rectangle involves using the <a href="http://msdn2.microsoft.com/en-us/library/bb329293.aspx">DrawRectangle</a> method which potentially requires setting a number of parameters. We will initially ignore the last 7 parameters and set them to zero (we will discuss them later when we cover gradient fills.).</p>
<p>If the outline thickness is greater than 1 then the co-ordinates specified indicate the center of the outline, i.e. half the outline is drawn on each side.</p>
<p>Rectangles with rounded corners can be specified by setting the xCornerRadius and yCornerRadius parameters to the desired radius. If the radius is larger than zero the outline thickness is ignored by the current version of the BCL and the framework reverts to drawing a 1 pixel thick outline.</dd>
<dt>Drawing Ellipses:</dt>
<dd><img src='http://www.christec.co.nz/blog/wp-content/uploads/2007/11/draw_ellipse.png' alt='draw_ellipse.png' class="alignright" /></p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #008080; font-style: italic;">// Draw an ellipse centred at (x=30, y=60)</span>
<span style="color: #008080; font-style: italic;">// with a radius of 10 on the x axis and</span>
<span style="color: #008080; font-style: italic;">// 20 on the y axis.</span>
bmp.<span style="color: #0000FF;">DrawEllipse</span><span style="color: #000000;">&#40;</span>red, <span style="color: #008080; font-style: italic;">// colour</span>
     <span style="color: #FF0000;">30</span>,  <span style="color: #008080; font-style: italic;">// x</span>
     <span style="color: #FF0000;">60</span>,  <span style="color: #008080; font-style: italic;">// y</span>
     <span style="color: #FF0000;">10</span>,  <span style="color: #008080; font-style: italic;">// x radius</span>
     <span style="color: #FF0000;">20</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// y radius</span></pre></div></div>

<p>The simplest way to draw an ellipse is to specify the colour, center co-ordinates, and then the radiuses for the x and y axis respectively. This allows drawing not only ellipses, but also circles (which simply have the x and y radiuses the same). </p>
<p>There is a more complex overload of the <a href="http://msdn2.microsoft.com/en-us/library/bb329276.aspx">DrawEllipse method</a> which enables you to specify the thickness of the outline and/or fill the inside of the shape. However both features are not implemented by the current version of the base class library.</dd>
</dl>
<p><b>Filling Shapes</b><br />
<img src="http://www.christec.co.nz/blog/wp-content/uploads/2007/11/linear-gradient-fill.png" alt="linear-gradient-fill.png" class="alignright" /></p>
<p>DrawEllipse and DrawRectangle both have overloads that support specifying a gradient fill to colour in the internal area of the shape (the 7 parameters set to 0 in the above examples).</p>
<p>The specification of a gradient fill consists of a start and end co-ordinate and associated colours at those two points. The framework will then apply a linear gradient between those two points. Any point &#8220;before&#8221; the start co-ordinate will be the starting colour, while any point &#8220;after&#8221; the end point will be the end colour. If both the start and end colours are the same a solid fill will be obtained.</p>
<p>The co-ordinates for the gradient start and end points are measured in screen co-ordinates. I.e. they are relative to the top left corner of the LCD and could refer to locations outside the area of the shape being drawn. This fact can be used to produce some interesting rendering and animation effects.</p>
<p>The opacity parameter allows the fill to be semitransparent and show previous content drawn to the same region of the bitmap. The opacity is a byte value with 0 indicating fully transparent, and 255 indicating full opaque (solid fill).</p>
<p>The fill effect shown in the image above was achieved via the following code sample. Notice the direction of the linear fill (as dictated by it&#8217;s start and end co-ordinates), and the fact that the bottom right half of the rectangle is a solid white fill due to this region being &#8220;after&#8221; the gradient&#8217;s end point.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;">bmp.<span style="color: #0000FF;">DrawRectangle</span><span style="color: #000000;">&#40;</span>Color.<span style="color: #0000FF;">White</span>, <span style="color: #008080; font-style: italic;">// outline colour</span>
     <span style="color: #FF0000;">0</span>,    <span style="color: #008080; font-style: italic;">// outline thickness (no outline)</span>
     <span style="color: #FF0000;">50</span>,    <span style="color: #008080; font-style: italic;">// x</span>
     <span style="color: #FF0000;">50</span>,    <span style="color: #008080; font-style: italic;">// y</span>
     <span style="color: #FF0000;">100</span>,   <span style="color: #008080; font-style: italic;">// width</span>
     <span style="color: #FF0000;">100</span>,   <span style="color: #008080; font-style: italic;">// height</span>
     <span style="color: #FF0000;">0</span>,     <span style="color: #008080; font-style: italic;">// x corner radius</span>
     <span style="color: #FF0000;">0</span>,     <span style="color: #008080; font-style: italic;">// y corner radius</span>
     red,   <span style="color: #008080; font-style: italic;">// start gradient colour</span>
     <span style="color: #FF0000;">50</span>,    <span style="color: #008080; font-style: italic;">// start gradient x</span>
     <span style="color: #FF0000;">50</span>,    <span style="color: #008080; font-style: italic;">// start gradient y</span>
     Color.<span style="color: #0000FF;">White</span>, <span style="color: #008080; font-style: italic;">// end gradient colour</span>
     <span style="color: #FF0000;">100</span>,   <span style="color: #008080; font-style: italic;">// end gradient x</span>
     <span style="color: #FF0000;">100</span>,   <span style="color: #008080; font-style: italic;">// end gradient y</span>
     0xFF<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #008080; font-style: italic;">// opacity of fill</span></pre></div></div>

<p><b>Sample Applications</b></p>
<blockquote><p>[<a href="http://www.christec.co.nz/blog/wp-content/uploads/2007/11/drawingexample.zip">Download drawingexample.zip - 8.6 KB</a>]</p></blockquote>
<p><img src="http://www.christec.co.nz/blog/wp-content/uploads/2007/11/alpha-blended-rectangles.png" alt="alpha-blended-rectangles.png" class="alignright" />The sample application available for download demonstrates a number of basic drawing operations as discussed above. The application cycles through a number of demonstrations. The sample application also demonstrates the use of System.Reflection functionality within the .NET Micro Framework to find the examples. If you would like to experiment with the drawing APIs, this sample application would be an ideal test harness, just add another &#8220;Example_XYZ&#8221; method that contains your drawing code and your example will be automatically picked up.</p>
<blockquote><p>[<a href="http://www.christec.co.nz/blog/wp-content/uploads/2007/11/randomshapes.zip">Download randomshapes.zip - 35 KB</a>]</p></blockquote>
<p><img src="http://www.christec.co.nz/blog/wp-content/uploads/2007/11/random-shapes-demo.png" alt="random-shapes-demo.png" class="alignleft" />Another sample application is available for download (without explanation as to how it is implemented). This example helps demonstrates the rendering capabilities of the .NET Micro Framework by creating and animating up to 50 random rectangles of different size, colour and alpha transparency over top of the .NET Micro Framework snowflake logo. It also demonstrates the fact that the .NET Micro Framework emulator is really a simulator. You will notice that running this example under the emulator produces very impressive rendering speeds which are not matched when running on actual hardware.</p>
<p>My next blog entry about the .NET Micro Framework will discuss how to create a basic WPF style application. Eventually I will outline an alternative approach for drawing basic shapes that enables the WPF framework to take care of compositing the individual shapes onto the screen, enabling basic shapes to be animated and moved around in a more object orientated manor.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.christec.co.nz/blog/archives/175/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Visual Introduction to .NET Micro Framework</title>
		<link>http://www.christec.co.nz/blog/archives/98</link>
		<comments>http://www.christec.co.nz/blog/archives/98#comments</comments>
		<pubDate>Mon, 30 Jul 2007 11:45:59 +0000</pubDate>
		<dc:creator>Christopher Fairbairn</dc:creator>
				<category><![CDATA[.NET Micro Framework]]></category>

		<guid isPermaLink="false">http://www.christec.co.nz/blog/archives/98</guid>
		<description><![CDATA[Confused about the .NET Micro Framework and what scenarios it enables? Or wondering how it compares to the .NET Compact Framework? Finding the documentation heavy going?
The following videos may help explain the differences, and give you some neat ideas for possible applications of the technology.


Mike Hall discusses a version of Space Invaders he developed to [...]]]></description>
			<content:encoded><![CDATA[<p>Confused about the .NET Micro Framework and what scenarios it enables? Or wondering how it compares to the .NET Compact Framework? Finding the <a href="http://msdn2.microsoft.com/en-us/embedded/bb267253.aspx">documentation</a> heavy going?</p>
<p>The following videos may help explain the differences, and give you some neat ideas for possible applications of the technology.</p>
<p><object width="425" height="350"><param name="movie" value="http://www.youtube.com/v/5iRniRqTRP0"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/5iRniRqTRP0" type="application/x-shockwave-flash" wmode="transparent" width="425" height="350"></embed></object></p>
<ul>
<li><a href="http://blogs.msdn.com/mikehall/archive/2007/07/20/net-micro-framework-book-is-now-shipping-guess-who-has-a-copy.aspx">Mike Hall</a> discusses a version of <a href="http://www.youtube.com/watch?v=5iRniRqTRP0">Space Invaders</a> he developed to demonstrate the capabilities of the .NET Micro Framework.</li>
<li>A <a href="http://msdn.microsoft.com/embedded/media/tutorials/netmf.wmv">day in your life</a> video produced by MSN Direct. This demonstrates how a &#8220;well-connected mobile professional&#8221; may use a variety of devices <a href="http://www.microsoft.com">Microsoft</a> envisages the .NET Micro Framework will enable.</li>
<li>A video produced for MEDC 2006 which discusses the <a href="http://casting.dlservice.microsoft.com/download/9/c/d/9cd1795d-74bf-43e7-9b27-1bb0f7faafd1/dotnet_micro_framework.wmv">background of the .NET Micro Framework</a>, with a demonstration of it&#8217;s use in a sumobot.</li>
<li>A <a href="http://www.virtualteched.com/Videos/Embedded_500.asx">video</a> produced at Tech-Ed 2007 which discusses the .NET Micro Framework with Donald Thompson and Rob Miles, the authors of the <a href="http://www.amazon.com/Embedded-Programming-Microsoft-Micro-Framework/dp/0735623651">Embedded Programming with the Microsoft .NET Micro Framework</a> book.</li>
<li>A webcast on the subject of <a href="http://msevents.microsoft.com/CUI/WebCastEventDetails.aspx?culture=en-US&#038;EventID=1032343089&#038;CountryCode=US">.NET Micro Framework-Embedded Development with C# and Visual Studio Development System</a>.</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.christec.co.nz/blog/archives/98/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
<enclosure url="http://msdn.microsoft.com/embedded/media/tutorials/netmf.wmv" length="7367487" type="video/x-ms-wmv" />
<enclosure url="http://casting.dlservice.microsoft.com/download/9/c/d/9cd1795d-74bf-43e7-9b27-1bb0f7faafd1/dotnet_micro_framework.wmv" length="35776042" type="video/x-ms-wmv" />
<enclosure url="http://www.virtualteched.com/Videos/Embedded_500.asx" length="92" type="video/x-ms-asf" />
		</item>
		<item>
		<title>.NET Micro Framework resources</title>
		<link>http://www.christec.co.nz/blog/archives/96</link>
		<comments>http://www.christec.co.nz/blog/archives/96#comments</comments>
		<pubDate>Sun, 29 Jul 2007 11:29:57 +0000</pubDate>
		<dc:creator>Christopher Fairbairn</dc:creator>
				<category><![CDATA[.NET Micro Framework]]></category>

		<guid isPermaLink="false">http://www.christec.co.nz/blog/archives/96</guid>
		<description><![CDATA[Over the weekend I have found some more .NET Micro Framework developer resources available on the Internet.

Kudzu World &#8211; &#8220;Programming is an art form that fights back&#8221;. This is the personal website of Chad Z. Hower, which has a .NET Micro Framework related section. It is essentially an index to blogs, development boards and other [...]]]></description>
			<content:encoded><![CDATA[<p>Over the weekend I have found some more <a href="http://msdn2.microsoft.com/en-us/embedded/bb267253.aspx">.NET Micro Framework</a> developer resources available on the Internet.</p>
<ul>
<li><a href="http://www.kudzuworld.com/MicroFramework/index.EN.aspx">Kudzu World</a> &#8211; &#8220;Programming is an art form that fights back&#8221;. This is the personal website of Chad Z. Hower, which has a .NET Micro Framework related section. It is essentially an index to blogs, development boards and other resources Chad has discovered.</li>
<li><a href="http://dotnetmf.sdstechnologies.com/2007/05/here-we-go.html">.NET Micro Framework for Dummies</a> &#8211; This is a promising new blog by an unknown developer (I could not find any contact details on the blog) who is in a similar situation to me. He has brought a <a href="http://www.embeddedfusion.com/default.aspx?id=90">Tahoe</a> development board, and is blogging about his experiences learning to develop for the new platform.</li>
<li><a href="http://bloggingabout.net/blogs/jens/archive/2007/03/14/welcome-to-my-blog.aspx">Jens Kühner</a> &#8211; &#8220;Geek stuff about the .NET, Compact and Micro Framework&#8221;, a reasonably new blog by Jens Kühner who is currently <a href="http://bloggingabout.net/blogs/jens/archive/2007/05/17/i-am-writing-a-book-about-the-net-micro-framework.aspx">writing  a book</a> about the .NET Micro Framework. He promises to provide further tips and tricks as his book writing continues.</li>
</ul>
<p>It will be interesting to see how these resources develop over time, many are at an earlier stage of their development (similiar to my own blog).</p>
<p>Another link of interest is a presentation titled &#8220;<a href="http://www3.hermia.fi/mp/db/file_library/x/IMG/14040/file/NET_Micro_Framework-March07.pdf">.NET Micro Framework &#8211; Brining .NET to smaller embedded devices</a>&#8221; which appears to be developed by Microsoft. This is a worthwhile read, for those wanting to get a good overview of the platform. Sorry about the link (it points to a random PDF on someones website), but I discovered it via a Google search results page, and have not managed to find a better page to link to.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.christec.co.nz/blog/archives/96/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>When is C# 2.0 not equal to C# 2.0?</title>
		<link>http://www.christec.co.nz/blog/archives/86</link>
		<comments>http://www.christec.co.nz/blog/archives/86#comments</comments>
		<pubDate>Fri, 27 Jul 2007 11:26:10 +0000</pubDate>
		<dc:creator>Christopher Fairbairn</dc:creator>
				<category><![CDATA[.NET Micro Framework]]></category>

		<guid isPermaLink="false">http://www.christec.co.nz/blog/archives/86</guid>
		<description><![CDATA[As I have mentioned previously I have only recently started learning about the .NET Micro Framework, as such I am constantly learning new things about this constrained development environment and today was no exception, here is what I learnt&#8230;
The lack of generics
I was investigating the use of the ExtendedWeakReference class, and as part of a [...]]]></description>
			<content:encoded><![CDATA[<p>As I have mentioned previously I have only recently started learning about the <a href="http://msdn2.microsoft.com/en-us/embedded/bb267253.aspx">.NET Micro Framework</a>, as such I am constantly learning new things about this constrained development environment and today was no exception, here is what I learnt&#8230;</p>
<p><b>The lack of generics</b><br />
I was investigating the use of the <a href="http://msdn2.microsoft.com/en-us/library/microsoft.spot.extendedweakreference.aspx">ExtendedWeakReference</a> class, and as part of a test application I was putting together I thought it would be nice to make use of some generics. <a href="http://msdn2.microsoft.com/en-us/library/512aeb7t(VS.80).aspx">Generics</a> were a new feature in C# v2.0 which enable you to produce efficient, reusable type safe code.</p>
<p>As the following example program demonstrates, you can make use of certain C# 2.0 features such as anonymous delegates when targeting the .NET Micro Framework. However any attempt to use generics will cause a build time error to occur.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Threading</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">Microsoft.SPOT</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Program
<span style="color: #000000;">&#123;</span>
  <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> Main<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
  <span style="color: #000000;">&#123;</span>
    <span style="color: #008080; font-style: italic;">// Configure a timer to print out a message to the debug window once</span>
    <span style="color: #008080; font-style: italic;">// every 5 seconds. Notice that we use an anonymous delegate for the</span>
    <span style="color: #008080; font-style: italic;">// TimerCallback delegate parameter. This is a C# 2.0 language feature.</span>
    Timer t <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Timer<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">delegate</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">object</span> o<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span> Debug.<span style="color: #0000FF;">Print</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Hello World!&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span>,
                                  <span style="color: #0600FF;">null</span>, <span style="color: #FF0000;">0</span>, <span style="color: #FF0000;">5000</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
    <span style="color: #008080; font-style: italic;">// Place the main thread into an infinite sleep. The</span>
    <span style="color: #008080; font-style: italic;">// timer callbacks will occur on a separate thread.</span>
    <span style="color: #0600FF;">while</span> <span style="color: #000000;">&#40;</span><span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span>
      Thread.<span style="color: #0000FF;">Sleep</span><span style="color: #000000;">&#40;</span>Timeout.<span style="color: #0000FF;">Infinite</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
  <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>If you do attempt to use generics within your code the C# compiler will execute successfully (i.e. csc.exe indicates no errors in the build log) but the Meta Data Processor (MetaDataProcessor.exe) which runs automatically after the C# compiler will produce the following error:</p>
<pre>METADATAPROCESSOR: Error MMP0000: CLR_E_PARSER_UNSUPPORTED_GENERICS</pre>
<p>The Meta Data Processor is the utility which converts assemblies from the standard PE Executable file format (as used on the desktop), into a smaller more efficient representation utilised by .NET Micro Framework devices.</p>
<p>My assumption here is that any C# v2.0 feature which ends up being a chunk of &#8220;compiler magic&#8221; which essentially converts your code behind the scenes into a form that would have been compatible with the V1.0 compiler is supported on the .NET Micro Framework. While features such as Generics, which required CLR changes in addition to compiler changes, are currently not supported by the .NET Micro Framework.</p>
<p><b>No support within the BCL to parse numbers</b><br />
A typical programming task is to parse strings and convert them into numbers. Typically a .NET developer would use a static method within the Base Class Library such as <a href="http://msdn2.microsoft.com/en-us/library/system.int32.parse.aspx">Int32.Parse()</a> to perform this operation.</p>
<p>However the numeric classes within the .NET Micro Framework BCL are pretty empty, and don&#8217;t include methods such as Parse. Likewise the <a href="http://msdn2.microsoft.com/en-us/library/system.convert.aspx">System.Convert</a> class does not exist. This means if you need to parse a number, you need to write the number parsing code yourself.</p>
<p>This issue has been discussed on numerous blogs and online forums. Some interesting coverage on this subject is as follows:</p>
<ul>
<li>In a thread titled &#8220;<a href="http://www.eggheadcafe.com/software/aspnet/29921215/best-way-to-convert-strin.aspx">Best way to convert string to float/double/decimal?</a>&#8221; James Webster a Test Manager within the Microsoft .Net Micro Framework team mentions this is an issue which may be resolved in a later release of the framework. He also provides a quick suggestion as to a work around. This sample has a number of performance issues, which are then worked upon in the rest of the thread.</li>
<li>A post by <a href="http://bloggingabout.net/blogs/jens/about.aspx">Jens Kühner</a> titled <a href="http://bloggingabout.net/blogs/jens/archive/2007/05/17/number-parsing-with-the-net-micro-framework.aspx">Number parsing with the .NET Micro Framework</a> sounds very promising, however at present the download link to the source code appears to be broken.</li>
</ul>
<p>While I was on <a href="http://bloggingabout.net/blogs/jens/about.aspx">Jens Kühner&#8217;s</a> blog I noticed that he is currently writing a <a href="http://bloggingabout.net/blogs/jens/archive/2007/05/17/i-am-writing-a-book-about-the-net-micro-framework.aspx">.NET Micro Framework book</a>. He promises to release more details, and samples as time goes on. So his blog could be an interesting one to watch. It would be interesting to know how his book compares to Microsoft Press&#8217;s <a href="http://www.amazon.com/Embedded-Programming-Microsoft-Micro-Framework/dp/0735623651">Embedded Programming with the Microsoft .NET Micro Framework</a>.</p>
<p><b>The System.Math class</b><br />
In a similar manor the <a href="http://msdn2.microsoft.com/en-us/library/system.math.aspx">System.Math</a> class which provides various math functions is rather limited on the .NET Micro Framework&#8217;s version of the BCL. There are no SIN or COS functions for instance.</p>
<p><a href="http://www.embeddedfusion.com/">EmbeddedFusion</a> recently released the source code for their &#8220;<a href="http://www.embeddedfusion.com/downloads/lab403.zip">Ball-In-Maze</a>&#8221; tutorial designed for their <a href="http://www.embeddedfusion.com/default.aspx?id=90">Tahoe Development Board</a>. This lab was originally presented at MEDC 2007. Reading through the source code you come across a number of useful tips and tricks. Including an integer square root implementation, which I assume was done for speed.</p>
<p>Perhaps it is time to dust off my copy of Jack Crenshaw&#8217;s <a href="http://www.amazon.com/Math-Toolkit-Real-Time-Programming-Crenshaw/dp/1929629095">MATH Toolkit for REAL-TIME Programming</a> book and develop a small support library of additional math functions.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.christec.co.nz/blog/archives/86/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Handling GPIO Interrupts within the .NET Micro Framework</title>
		<link>http://www.christec.co.nz/blog/archives/65</link>
		<comments>http://www.christec.co.nz/blog/archives/65#comments</comments>
		<pubDate>Wed, 25 Jul 2007 10:59:46 +0000</pubDate>
		<dc:creator>Christopher Fairbairn</dc:creator>
				<category><![CDATA[.NET Micro Framework]]></category>

		<guid isPermaLink="false">http://www.christec.co.nz/blog/archives/65</guid>
		<description><![CDATA[My previous .NET Micro Framework example demonstrated how to determine the state of a GPIO input signal. This blog entry outlines how to implement an improved application which instead makes use of an interrupt event handler, for improved performance and power efficiency.
The problem with the InputPort based example was that it utilised a polling mechanism [...]]]></description>
			<content:encoded><![CDATA[<p>My previous <a href="http://www.christec.co.nz/blog/archives/56">.NET Micro Framework example</a> demonstrated how to determine the state of a GPIO input signal. This blog entry outlines how to implement an improved application which instead makes use of an interrupt event handler, for improved performance and power efficiency.</p>
<p>The problem with the <a href="http://msdn2.microsoft.com/en-us/library/microsoft.spot.hardware.inputport.aspx">InputPort</a> based example was that it utilised a polling mechanism to constantly determine the current state of the GPIO port, this mode of operation is not ideal for a number of reasons, including:</p>
<ul>
<li>Power Management &#8211; The polling implementation may become a busy loop, spinning around as quickly as possible, burning CPU cycles to check the state of the input which only occasionally changes state.</li>
<li>Responsiveness &#8211; A common resolution to the power management issue is to only periodically probe the hardware. The example application from the last blog entry sampled the GPIO state once per second. This has the side effect of introducing latency, since we will only detect a change in state once the current delay has completed. We may also miss a change completely if it happens too quickly.</li>
</ul>
<p>To resolve some of these issues we can utilise the <a href="http://msdn2.microsoft.com/en-us/library/microsoft.spot.hardware.interruptport.aspx">InterruptPort</a> class. This class derives from the <a href="http://msdn2.microsoft.com/en-us/library/microsoft.spot.hardware.inputport.aspx">InputPort</a> class mentioned in the previous blog entry, and hence by default inherits all of the InputPort&#8217;s properties and functionality.</p>
<p>The main difference is an additional constructor parameter which specifies when an &#8216;OnInterrupt&#8217; event associated with the port should fire.</p>
<p><strong>Determining when the interrupt should trigger</strong><br />
The additional constructor parameter specifies the conditions under which the interrupt should trigger. You can use one of the following <a href="http://msdn2.microsoft.com/en-us/library/microsoft.spot.hardware.port.interruptmode.aspx">Port.InterruptMode enumeration values</a>:</p>
<ul>
<li>InterruptEdgeBoth &#8211; The interrupt is triggered on both rising and falling edges.</li>
<li>InterruptEdgeHigh &#8211; The interrupt is triggered on the rising edge.</li>
<li>InterruptEdgeLow &#8211; The interrupt is triggered on the falling edge.</li>
<li>InterruptEdgeLevelHigh &#8211; The interrupt is triggered whenever the input signal is high.</li>
<li>InterruptEdgeLevelLow &#8211; The interrupt is triggered whenever the input signal is low.</li>
</ul>
<p><strong>Clearing Interrupt flags</strong><br />
Edge interrupts trigger when the specified signal transition occurs and automatically re-enable themself, ready to trigger on the next state transition. Level based interrupts however behave slightly differently.</p>
<p>Whenever a level interrupt (InterruptEdgeLevelHigh or InterruptEdgeLevelLow) is triggered it sets a flag which disables the interrupt from re-triggering until the flag is explicitly cleared by means of the <a href="http://msdn2.microsoft.com/en-us/library/microsoft.spot.hardware.interruptport_methods.aspx">ClearInterrupt</a> method.</p>
<p>The main purpose for this is to stop a constant stream of interrupts occurring when the specified level is reached on the GPIO signal. By disabling the interrupt once it has triggered, the software can decide when it wants to be interrupted again. When the ClearInterupt method is invoked, it is possible that the interrupt event handler will fire immediately, if the specified level is still held on the signal.</p>
<p><strong>Example Application</strong><br />
The following is a simple example of how the InterruptPort class can be utilised to produce asynchronous behavior.</p>
<p>The main loop of the program, simply prints the message &#8220;House Keeping Ping&#8230;&#8221; to the debug console every 60 seconds. This is to provide an indication of when the main thread is running. In a typical embedded device this main loop may do menial house keeping tasks, such as timer management, and status LED flashing etc.</p>
<p>While the main loop is sleeping (or printing debug messages), the hardware is constantly monitoring the state of the GPIO_Pin_3 input (attached to the Select button within the Sample Emulator). Whenever the signal goes low, an interrupt is triggered, which causes our button_OnInterrupt event handler to execute on a second thread.</p>
<p>You will notice that button presses are detected immediately even while the main thread is sleeping, unlike the polling example which only detected the button press when the main thread woke up.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">System</span><span style="color: #008000;">;</span> 
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Threading</span><span style="color: #008000;">;</span> 
<span style="color: #0600FF;">using</span> <span style="color: #008080;">Microsoft.SPOT</span><span style="color: #008000;">;</span> 
&nbsp;
<span style="color: #0600FF;">using</span> <span style="color: #008080;">Microsoft.SPOT.Hardware</span><span style="color: #008000;">;</span>namespace InterruptPortApplication 
<span style="color: #000000;">&#123;</span> 
  <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Program 
  <span style="color: #000000;">&#123;</span> 
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> Main<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span> 
    <span style="color: #000000;">&#123;</span> 
      <span style="color: #008080; font-style: italic;">// Specify the GPIO pin we want to use as an interrupt </span>
      <span style="color: #008080; font-style: italic;">// source, specify the edges the interrupt should trigger on </span>
      InterruptPort button <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> InterruptPort<span style="color: #000000;">&#40;</span>Cpu.<span style="color: #0000FF;">Pin</span>.<span style="color: #0000FF;">GPIO_Pin3</span>, <span style="color: #0600FF;">true</span>, 
        Port.<span style="color: #0000FF;">ResistorMode</span>.<span style="color: #0000FF;">PullDown</span>, Port.<span style="color: #0000FF;">InterruptMode</span>.<span style="color: #0000FF;">InterruptEdgeLow</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> 
&nbsp;
      <span style="color: #008080; font-style: italic;">// Hook up an event handler (delegate) to the OnInterrupt event </span>
      button.<span style="color: #0000FF;">OnInterrupt</span> <span style="color: #008000;">+=</span> <span style="color: #008000;">new</span> GPIOInterruptEventHandler<span style="color: #000000;">&#40;</span>button_OnInterrupt<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> 
&nbsp;
      <span style="color: #0600FF;">while</span> <span style="color: #000000;">&#40;</span><span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span> 
      <span style="color: #000000;">&#123;</span> 
        <span style="color: #008080; font-style: italic;">// The main thread can now essentially sleep </span>
        <span style="color: #008080; font-style: italic;">// forever. </span>
        Thread.<span style="color: #0000FF;">Sleep</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">60</span> <span style="color: #008000;">*</span> <span style="color: #FF0000;">1000</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> 
        Debug.<span style="color: #0000FF;">Print</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;House Keeping Ping...&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> 
      <span style="color: #000000;">&#125;</span> 
    <span style="color: #000000;">&#125;</span> 
&nbsp;
    <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> button_OnInterrupt<span style="color: #000000;">&#40;</span>Cpu.<span style="color: #0000FF;">Pin</span> port, <span style="color: #FF0000;">bool</span> state, TimeSpan time<span style="color: #000000;">&#41;</span> 
    <span style="color: #000000;">&#123;</span> 
      <span style="color: #008080; font-style: italic;">// This method is called whenever an interrupt occurrs</span>
      Debug.<span style="color: #0000FF;">Print</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;The button is pressed&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> 
    <span style="color: #000000;">&#125;</span> 
  <span style="color: #000000;">&#125;</span> 
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>The three parameters in the GPIOInterruptEventHandler delegate type are as follows:</p>
<ul>
<li>pin &#8211; a reference to the pin that caused the interrupt to occur. This is useful because you can hook up the same event handler to more than one InterruptPort instance.</li>
<li>state &#8211; the logic level of the GPIO signal when the interrupt occurred.</li>
<li>timestamp &#8211; a relative time-stamp which allows you to accurately determine when the interrupt occurred.</li>
</ul>
<p><strong>Performance</strong><br />
The InterruptPort class is not designed for use in hard real time applications. In my mind it is more a Deferred Procedure Call (DPC) implementation triggered by hardware events than a true Interrupt Service Routine (ISR).</p>
<p>Since I have no physical .NET Micro Framework hardware yet, it is a bit difficult for me to tell exactly how the performance characteristics of interrupt handlers within the .NET Micro Framework stack up. This is defiantly an area I would like to characterise further once my hardware arrives.</p>
<p>In the mean time here are some links to low level performance related material that I have managed to find. Both articles are written by <a href="http://blog.opennetcf.org/ctacke/">Chris Tacke</a> and give an excellent insight into the performance characteristics of the .NET Micro Framework (and managed code in general) for embedded development.</p>
<ul>
<li><a href="http://blog.opennetcf.org/ctacke/PermaLink,guid,e561473e-5978-4419-b5a4-b7c446328022.aspx">Profiling the National 2F43ES (SPOT) microprocessor</a></li>
<li><a href="http://msdn2.microsoft.com/en-us/library/aa446521.aspx">Examining Execution Speed of JITed Code with CF 2.0</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://www.christec.co.nz/blog/archives/65/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Detecting button presses</title>
		<link>http://www.christec.co.nz/blog/archives/56</link>
		<comments>http://www.christec.co.nz/blog/archives/56#comments</comments>
		<pubDate>Tue, 17 Jul 2007 09:17:56 +0000</pubDate>
		<dc:creator>Christopher Fairbairn</dc:creator>
				<category><![CDATA[.NET Micro Framework]]></category>

		<guid isPermaLink="false">http://www.christec.co.nz/blog/archives/56</guid>
		<description><![CDATA[My post introducing the .NET Micro Framework covered how to use the OutputPort class to interface to a single GPIO output pin as part of an example to blink a LED.
The matching class to interface to a GPIO input pin, is not too surprisingly called the InputPort class. The InputPort class functions very similar to [...]]]></description>
			<content:encoded><![CDATA[<p>My post introducing the .NET Micro Framework covered how to use the <a href="http://msdn2.microsoft.com/en-us/library/microsoft.spot.hardware.outputport.aspx<br />
">OutputPort</a> class to interface to a single GPIO output pin as part of an example to blink a LED.</p>
<p>The matching class to interface to a GPIO input pin, is not too surprisingly called the <a href="http://msdn2.microsoft.com/en-us/library/microsoft.spot.hardware.inputport.aspx<br />
">InputPort</a> class. The InputPort class functions very similar to the OutputPort class discussed last time, in fact they share a common base class.</p>
<p>The constructor for the InputPort class has a couple of additional parameters, in addition to the one which specifies which GPIO pin it should be connected to.</p>
<p>The first is a boolean parameter which enables a glitch filter. Mechanical switches can be &#8220;noisy&#8221;, meaning that a single press by the user could translate into multiple open and close events, which digitial hardware can potentially detect. This problem is commonly refered to as <a href="http://en.wikipedia.org/wiki/Switch#Contact_bounce">Contact Bounce</a> or Switch Debouncing. At this stage I have not current managed to find out what technique the .NET Micro Framework utilises for glitch filtering, or even if this is device specific (I suspect it is).</p>
<p>The second parameter is of much more interest, since it deals with setting the type of internal <a href="http://en.wikipedia.org/wiki/Pull-down_resistor">pull up resistor</a> present on the GPIO pin. It can have one of following values from the <a href="http://msdn2.microsoft.com/en-us/library/microsoft.spot.hardware.port.resistormode.aspx">ResistorMode</a> enumeration:</p>
<ul>
<li>ResistorMode.PullUp &#8211; A resistor internal to the CPU is connected between the GPIO pin and VCC, i.e. the pin is pulled up to the positive supply rail.</li>
<li>ResistorMode.PullDown &#8211; A resistor internal to the CPU is connected between the GPIO pin and GND, i.e. the pin is pulled down to ground.</li>
<li>ResistorMode.None &#8211; no internal resistor is enabled.  In this mode if a pin is left unconnected, it could produce spurious readings due to noise induced into the pin.</li>
</ul>
<p>The pull up and pull down resistor modes can be handy when interfacing to external hardware, in particular push buttons. By relying upon the internal pull up or pull down resistors, you can get by without requiring additional components, as shown in the following schematics.</p>
<p><img src="http://www.christec.co.nz/blog/wp-content/uploads/2007/07/pullup-configurations.PNG" alt="Schematic showing pull up and pull down resistor configurations for push button interfacing" class="centered" /></p>
<p>It is important to note that if a push button is connected with a pull up resistor, it&#8217;s logic levels will be inverted. I.e. the GPIO pin will read a logic high (true) logic level when the push button is not pressed, and will read a logic low (false) logic level when the push button is pressed.</p>
<p><b>Code Sample</b><br />
Here is a sample program which will write a message to the debug window each time a push button connected to the  GPIO_Pin3 pin is pressed.</p>
<p>To reduce the amount of log messages written to the Visual Studio output window, we only sample the push button once every second. This means we do not need to enable the glitch filter because we are sampling it at a slow enough rate that it should not be a significant issue.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">Microsoft.SPOT</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">Microsoft.SPOT.Hardware</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Threading</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF;">namespace</span> InputTestApplication
<span style="color: #000000;">&#123;</span>
  <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Program
  <span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> Main<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
      <span style="color: #008080; font-style: italic;">// Monitor the &quot;select&quot; push button</span>
      InputPort button <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> InputPort<span style="color: #000000;">&#40;</span>Cpu.<span style="color: #0000FF;">Pin</span>.<span style="color: #0000FF;">GPIO_Pin3</span>,
         <span style="color: #0600FF;">false</span>, Port.<span style="color: #0000FF;">ResistorMode</span>.<span style="color: #0000FF;">PullDown</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
      <span style="color: #0600FF;">while</span> <span style="color: #000000;">&#40;</span><span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span>
      <span style="color: #000000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">// Test the state of the button</span>
        <span style="color: #0600FF;">if</span> <span style="color: #000000;">&#40;</span>button.<span style="color: #0000FF;">Read</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>
             Debug.<span style="color: #0000FF;">Print</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;The button is pressed&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">// Wait 1 second before sampling the button again</span>
        Thread.<span style="color: #0000FF;">Sleep</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">1000</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
      <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
  <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>NOTE: The GPIO pin used for this sample program has been selected for use with the .NET Micro Framework Sample Emulator. If you attempt to run this application on your own .NET Micro Framework module, you may need to adjust the GPIO signal utilised to suit your hardware.</p>
<p><b>Push Buttons within the Emulator</b><br />
The &#8220;Sample Emulator&#8221; released with the .NET Micro Framework SDK has 5 push buttons &#8220;wired up&#8221; into a D-PAD configuration.</p>
<p>An interesting aspect to the emulated hardware is that the push buttons can be made to act as if there were wired up with pull up or pull down resistors (as outlined above) depending upon the state of the ResistorMode parameter passed into the constructor of the InputPort instance which accesses them. Typical hardware wouldn&#8217;t have this flexibility, with the incorrect ResistorMode choice potentially rendering a push button unreadable.</p>
<p>The mapping of emulator buttons to GPIO pins for the Sample Emulator is as follows:</p>
<ul>
<li>Select &#8211; GPIO_Pin3</li>
<li>Up &#8211; GPIO_Pin2</li>
<li>Down &#8211; GPIO_Pin4</li>
<li>Left &#8211; GPIO_Pin0</li>
<li>Right &#8211; GPIO_Pin1</li>
</ul>
<p>The code sample provided in this blog posting constantly polls the state of the push button. This is not power efficient. It is better to request that the hardware notifies you whenever the GPIO pin changes state. Next time I will discuss how you can use the <a href="http://msdn2.microsoft.com/en-us/library/microsoft.spot.hardware.interruptport.aspx">InterruptPort</a> class to achieve this.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.christec.co.nz/blog/archives/56/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Introduction to the .NET Micro Framework</title>
		<link>http://www.christec.co.nz/blog/archives/46</link>
		<comments>http://www.christec.co.nz/blog/archives/46#comments</comments>
		<pubDate>Sun, 15 Jul 2007 06:03:47 +0000</pubDate>
		<dc:creator>Christopher Fairbairn</dc:creator>
				<category><![CDATA[.NET Micro Framework]]></category>

		<guid isPermaLink="false">http://www.christec.co.nz/blog/archives/46</guid>
		<description><![CDATA[As I mentioned in an earlier posting I have started to become interested in Microsoft&#8217;s latest EMCA Common Language Infrastructure  (CIL) implementation, aka the .NET Micro Framework.
The .NET Micro Framework is designed for devices where even the .NET Compact Framework requires too many system resources to be feasible. It&#8217;s original design specification was to [...]]]></description>
			<content:encoded><![CDATA[<p>As I mentioned in an <a href="http://www.christec.co.nz/blog/archives/10">earlier posting</a> I have started to become interested in Microsoft&#8217;s latest <a href="http://www.ecma-international.org/publications/standards/Ecma-335.htm">EMCA Common Language Infrastructure</a>  (CIL) implementation, aka the <a href="http://msdn2.microsoft.com/en-us/embedded/bb267253.aspx">.NET Micro Framework</a>.</p>
<p>The .NET Micro Framework is designed for devices where even the <a href="http://msdn2.microsoft.com/en-us/netframework/Aa497273.aspx">.NET Compact Framework</a> requires too many system resources to be feasible. It&#8217;s original design specification was to run on a custom ARM7 ASIC running at 27Mhz with 512KB ROM, 384 KB RAM and 8KB EEPROM, without an form of Operating System.</p>
<p><b>Operating Environment</b><br />
One feature that makes the .NET Micro Framework unique is it&#8217;s ability to optionally run on hardware directly without the need for a traditional operating system. This is achieved by providing two levels of integration within the porting kit utilised to port the .NET Micro Framework to a new device. An OEM may decide to provide support for the .NET Micro Framework, by hooking in at one of two levels, depending upon the capabilities of their device:</p>
<ul>
<li>Platform Adoption Layer (PAL) &#8211; This layer is a high level interface which provides a set of high level primitives such as timers, memory management and data structures. These are typically features provided by a host operating system, leading to a reasonably transparent PAL being developed by the OEM.</li>
<li>Hardware Abstraction Layer (HAL) &#8211; If no host operating system is in use on the target device, the OEM can utilise a standardised PAL, which hooks into the functionality provided by a hardware abstraction layer. The HAL provides a standardised interface to low level hardware features such as GPIO signals, interrupt handlers and power management etc, which the PAL uses to implement it&#8217;s own OS like functionality.</li>
</ul>
<p>Implementing a Hardware Abstraction Layer can lead to the situation where the .NET Micro Framework is running directly on the CPU as the only &#8220;process&#8221;.</p>
<p><b>Compilation Process</b><br />
Another unique feature of the .NET MIcro Framework, is that it has no Just In Timer (JIT) compiler. The framework instead interperts the MSIL bytecode, in a similiar manor to <a href="http://www.mono-project.com/Mono:Runtime">Mono&#8217;s mint interpreter</a>.</p>
<p>Similar to the <a href="http://en.wikipedia.org/wiki/Java_ME">Java Micro Edition</a> platform, the standard bytecode format utilised on the desktop is not understood by a .NET Micro Framework device. Instead, as part of the compilation process, the bytecode is passed through a pre-verification and optomisation stage, which produces an optimised representation of the bytecode understood by the .NET Micro Framework device.</p>
<p><b>Hello World &#8211; Software Developer style</b><br />
The traditional first program for a software developer learning a new language or development environment is to write an application which simply prints &#8220;<a href="http://en.wikipedia.org/wiki/Hello_world_program">Hello World</a>&#8221; and then exits. This tradition has it&#8217;s roots in an example provided in a book called &#8220;<a href="http://www.amazon.com/C-Programming-Language-Ritchie-Kernighan">The C Programming Language</a>&#8221; and was apparently sourced from a 1974 Bell Laboratories internal memorandum written by <a href="http://en.wikipedia.org/wiki/Brian_Kernighan">Brian Kernighan</a> (one of the book&#8217;s authors).</p>
<p>Within the Visual Studio 2005 environment for the .NET Micro Framework, a Hello World application may be written as follows:</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">Microsoft.SPOT</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF;">namespace</span> HelloWorldApplication
<span style="color: #000000;">&#123;</span>
  <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Program
  <span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> Main<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
      Debug.<span style="color: #0000FF;">Print</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">&quot;Hello World!&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
  <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>The <a href="https://msdn2.microsoft.com/en-us/library/microsoft.spot.debug.aspx">Microsoft.Spot.Debug</a> class is somewhat similiar to the <a href="http://msdn2.microsoft.com/en-us/library/system.diagnostics.debug.aspx">System.Diagnostics.Debug</a> class present in the full framework in its intent.</p>
<p>The <a href="https://msdn2.microsoft.com/en-us/library/microsoft.spot.debug.print.aspx">Debug.Print</a> method can be utilised to emit a string to the standard output stream, which by default is routed to the Visual Studio IDE from the development board while debugging an application.</p>
<p>This code sample is fairly similiar to that produced by the Micro Framework Console Application project wizard within Visual Studio 2005. The main difference being that the project wizard emits code which utilises a string resource to store the string, this aids in internationalisation of the application.</p>
<p><b>Hello World &#8211; Hardware Engineer style</b><br />
The equvialent to a &#8220;Hello World&#8221; application for a hardware engineer bringing up a new microprocessor based board is to flash a LED connected to a GPIO pin.</p>

<div class="wp_syntax"><div class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">System.Threading</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">using</span> <span style="color: #008080;">Microsoft.SPOT.Hardware</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #0600FF;">namespace</span> HelloWorldApplication
<span style="color: #000000;">&#123;</span>
  <span style="color: #0600FF;">public</span> <span style="color: #FF0000;">class</span> Program
  <span style="color: #000000;">&#123;</span>
    <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #0600FF;">void</span> Main<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
      <span style="color: #008080; font-style: italic;">// The LED is hooked up to GPIO Pin 1. The second parameter</span>
      <span style="color: #008080; font-style: italic;">// within the constructor indicates to set the pin to logic high</span>
      <span style="color: #008080; font-style: italic;">// initially</span>
      OutputPort led <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> OutputPort<span style="color: #000000;">&#40;</span>Cpu.<span style="color: #0000FF;">Pin</span>.<span style="color: #0000FF;">GPIO_Pin1</span>, <span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
      <span style="color: #0600FF;">while</span> <span style="color: #000000;">&#40;</span><span style="color: #0600FF;">true</span><span style="color: #000000;">&#41;</span>
      <span style="color: #000000;">&#123;</span>
        <span style="color: #008080; font-style: italic;">// Sleep for 5 seconds (5000 ms)</span>
        Thread.<span style="color: #0000FF;">Sleep</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">5000</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
        <span style="color: #008080; font-style: italic;">// Toggle the state of the LED</span>
        led.<span style="color: #0000FF;">Write</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">!</span>led.<span style="color: #0000FF;">Read</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
      <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
  <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></div></div>

<p>NOTICE: If you attempt to run this sample application on actual hardware, you may need to alter the GPIO Pin assignment to suit your hardware, and add a reference to a device specific assembly.</p>
<p>The <a href="https://msdn2.microsoft.com/en-us/library/microsoft.spot.hardware.outputport.aspx">Microsoft.Spot.Hardware.OutputPort</a> class accesses a specified pin on the physical hardware and can toggle the pin between the high and low logic levels.</p>
<p>The <a href="">Read()</a> method queries the pin for it&#8217;s current state, while the <a href="https://msdn2.microsoft.com/en-us/library/microsoft.spot.hardware.outputport.write.aspx">Write(bool)</a> method allows the user to explicitly set the pin&#8217;s current state. For those familiar with traditional Micro-controller firmware development, you will notice that the hardware is accessed in an abstracted sense, where all the <a href="http://en.wikipedia.org/wiki/Bit_manipulation">bit twiddling</a> typically required to set or reset a single GPIO pin is hidden behind the scenes. This makes for a nice portable application, but is not suitable in applications which need precise real time functionality etc.</p>
<p><b>Trivia</b><br />
I initially wondered why the .NET Micro Framework related classes were in namespaces starting with Microsoft.SPOT. SPOT which is an acronym for &#8220;Smart Personal Object Technology&#8221;, was the <a href="http://research.microsoft.com/">Microsoft Research</a> project which initially produced the technology that became the .NET Micro Framework. The Microsoft SPOT group developed a range of devices, with the most well known probably being the <a href="http://www.microsoft.com/presspass/press/2006/jan06/01-04ExpandsWeatherStationsPR.mspx">smart watch</a>.</p>
<p>One of the proof of concepts the research team produced was a tiny CLR to allow a managed software development environment for their projects. They named the initial namespaces after their group, since this was a technology for internal use only. When it was decided to commercialise the .NET Micro Framework as a seperate product, they decided to keep the namespaces, since a number of internal and external projects were already using them and it helped with backwards compatibility.</p>
<p>I also assume this is why what appears to be the first publically available version of the .NET Micro Framework is versioned as version 2.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.christec.co.nz/blog/archives/46/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
