Jens Kühner

Geek stuff about the .NET, Compact and Micro Framework.
.NET Open Space 2008

.NET Open Space vom 18.10. bis 19.10.2008 in Leipzig

The best discussions happen in the coffee breaks of a conference. So why not arranging an event around the coffee breaks? As the name suggests, .NET Open Space 2008 is organized as an open space.

This isn't a typical developers conference. There are no designated speakers and no agenda.

You decide what to talk about! 

The conference itself is free, but the available places are limited.

The .NET Open Space 2008 will cover these four topics:

  • ALT.NET
  • Mobile Computing
  • Soft Skills
  • .NET on embedded systems and microcontroller electronics


What will be discussed under those topics depends on you. Join us for free.

Please note that the "conference" is held in German language only.

I will be there and I am the contact person for the .NET Micro Framework topic. 

.NET Micro Framework Beta 3.0 Firmware available for GHI Electronics Embedded Master Hardware

Only one week after Microsoft made the .NET Micro Framework Beta 3.0 SDK available for all developers, GHI Electronics provides a free Beta 3.0  firmware update for their Embedded Master hardware.  

Already available:

  • 4 UART ports
  • SPI
  • I2C
  • GPIO
  • Debug/deploy over COM1

Features to come very soon:

  • Some of GHI exclusive features to verify interops
  • USB device
  • TCP/IP
  • SSL
  • Full exclusive GHI library (ADC, DAC, PWM...etc.)
  • And many, many more!

 

.NET Micro Framework v3.0 SDK beta now available!

The .NET Micro Framework team announced the v3.0 SDK beta, that is now available to all developers through the Microsoft Connect website (http://connect.microsoft.com/netmf).  Simply fill out a short survey and you have full access to the downloads, newsgroup, and feedback.

Check the site for details about the RicaVision VAVE100 universal remote control (running SideShow and .NET Micro Framework) that Microsoft is giving away as part of the beta!

And remember to check the FAQ in the beta site for the most up-to-date information about hardware supported in the 3.0 beta, how to migrate projects to VS2008, and what features are supported in the current version.

v3.0 of the .NET MF SDK is planned to ship later this calendar year.

The following features are included in the first public beta:

  1. Visual Studio 2008 now supported
    .NET Micro Framework V3.0 now runs under Visual Studio 2008. Additionally, .NET Micro Framework V2.5 projects can also be built using VS 2008 and the .NET Micro Framework V3.0 (seems to work like the .NET multi targeting introduced with VS 2008)
  2. DPWS CodeGen tools
    The .NET Micro Framework v3.0 now includes a tool, MFSvcUtil.exe that can be used to generate .NET Micro Framework DPWS client proxies and DPWS Service stubs from WSDL files.
  3. USB Device support
    In addition to allowing debugging over USB, hardware running the .NET Micro Framework 3.0 can now act as USB devices using the Microsoft.SPOT.Hardware.UsbClient namespace.
  4. Stylus/Touch Panel support
    Using touchscreen-enabled hardware, or the new touchscreen emulator, you can create code that reacts when UI elements are pressed, captures “ink” for signatures or jotted notes, or handles gestures for UI navigation. You can access this functionality through Microsoft.SPOT.Touch, Microsoft.SPOT.Ink, and new extensions to the Microsoft.SPOT.Presentation namespaces.
  5. Secure Sockets support
    The .NET Micro Framework v3.0 now supports Secure Sockets Layer (*SSL) networking for secure communications.

Following features are not available in the first public beta:

  1. File system
  2. 802.11 Wi-Fi infrastructure
  3. Support for more cores and processor architectures
  4. Publicly available Porting Kit for purchase (separate product not included with the SDK)

Device Solutions has announced support for their Tahoe development kit during the v3.0 beta and GHI Electronics has announced support for their USBizi and EmbeddedMaster development kits with the v3.0 beta.

Jan Kučera has upodated his .NET Micro Framework tools and resources website

A proven and essential site (www.MicroFramework.eu) with tons of information about the .NET Micro Framework has now a better design, more and updated information, and an online shop for European citizens to get Device Solutions hardware.

Using Touchscreens with the .NET Micro Framework

Although announced for the .NET Micro Framework V3, I found a way to use touchscreens with existing WPF apps without significant modifications and put together a library.

The PointingDeviceInputProvider class
The abstract base class PointingDeviceInputProvider posses the three methods DevicePointerDown, DevicePointerMove and DevicePointerUp that all accept screen coordinates. To implement touch support you need to derive a custom input provider like TouchInputProvider and pass the coordinates to the methods. It does not matter where the coordinates come from. Beside touchscreens you can think of a mouse or even a joystick would be possible. The PointingDeviceInputProvider class handles it all.

How can I use touchscreens without changing existing WPF applications?
When you pass coordinates to the DevicePointerDown method of the PointingDeviceInputProvider class, then determines the underlying WPF control on the screen and sets the focus to the element. Then it sends a Select-key down-event to the control.
If you have a button element with nested elements like text and you want to have your button focused, you need to disable all the child elements to prevent one of them to get the focus instead of the surrounding button.
To add touchscreen support to your WPF application you just need to have a device with a touchscreen like the Hi-CO.ARM9 from emtrion and add a concrete input provider implementation like the emtrion.SPOT.Input.TouchInputProvider class instead or in addidtion to a GPIOInputProvider object.

emtrion`s HiCO-ARM9 hardware
emtrions HiCO-ARM9 with touchscreenIn the following you can see the emtrion.SPOT.Input.TouchInputProvider implementation based on the PointingDeviceInputProvider and using emtrion´s managed touch drivers:

//universal TouchInput Provider for Emulator and ADS7843 on HiCO.ARM

//written by Jens Kühner

using System;

using Microsoft.SPOT;

using Microsoft.SPOT.Presentation;

using Kuehner.SPOT.Input;

using Microsoft.SPOT.Hardware.HiCO_ARM9;

 

namespace emtrion.SPOT.Input

{

    public class TouchInputProvider : PointingDeviceInputProvider

    {

        public TouchInputProvider(PresentationSource source)

            : base(source, false)

        {

            TouchDriver.Init(new ADS7843());

            TouchDriver.AddHandler(
                new TouchDriver.TouchEventHandler(PenDown));

        }

 

        private void PenDown(TouchDriver.Event rEvent, object rId)

        {

            int x = rEvent.Pos.X;

            int y = rEvent.Pos.Y;

 

            //we currently only get a down event,
            //so simulated down and up

            DevicePointerDown(x, y);

            Thread.Sleep(20);

            DevicePointerUp(x, y);

       }

    }

Emulating a touchscreen
emtrion HiCO-ARM9 emulator with touch supportIt is even possible to emulate a touchscreen with the .NET Micro Framework extensible emulator. The following figure shows the emtrion´s HiCO-ARM9 hardware emulator with touchscreen support. You can simulate touch events by clicking the emulator display with the mouse.

Extended touch support
If you need an even better support than just simulating key events, you need to implement a custom WPF element that implements the Kuehner.SPOT.Input.IExtendedPointDeviceControl interface with the methods OnPointerDown, OnPointerUp, and PointerMove. The attached sample include a paint box sample to show that feature.

Conclusion
To play with touchscreen support and the .NET Micro Framework, you can download my touchscreen sample projects including an emulator, the button sample and paint box sample.
Further you should visit the emtrion's support area, where you can download their nice touch-enabled HiCO-ARM9 emulator and sample code.
Soon I will post how to use GHI Electronics EmbeddedMaster hardware to use a mouse or joystick to interface with WPF applications.

Data acquisition with SPI and the MF

Carlo Mendoza has a .NET Micro Framework related post about data acquisition with SPI.

Get a Sneak Peak and a free Copy of my "Expert .NET Micro Framework" Book at the Tech-Ed

There are several .NET Micro Framework sessions at the Tech-Ed Developers Converence 2008.
The first 200 attendees of the .NET Micro Framework conference breakout sessions will receive a free copy of my book.

Microsoft Tech·Ed North America 2008 Developers

Accelerometer based Remote Control for Container Terminal using the .NET Micro Framework

Here is a really interesting project from a Danish company that uses the .NET Micro Framework. Upon my request the embedded software specialist Hans Skytt Steffensen provided me information in English:

The Center for Software Innovation has evaluated accelerometer technology in remote control (RC) application for crane operated container terminals. Rather than using regular joystick control of the crane, it is controlled by accelerometer. The accelerometer detects all motions in 3D which is turned into crane control commands. Hazardous crane operations are avoided by build-in protection against sudden moves.
By emphasizing accelerometer technology we put the spot on new and more user-friendly ways of advanced device control.The setup is brought to life in only two weeks using Microsoft’s brand new .NET Micro Framework. The .NET Micro Framework is the version of the .NET Common Language Runtime (CLR) designated for small embedded devices. The application is developed in the managed code C# language in Visual Studio 2005.

 

The RC contains the following development kits:

  • Tahoe developer kit for .NET Micro Framework from www.DeviceSolutions.net. The kit is based on a tiny 35mm x 35mm cpu module. Hosting a Freescale i.MXS processor, RAM, Flash and Power supply and with SMD attachment scheme.
  • Accelerometer add-on module from www.DeviceSolutions.net. Utilizing Freescale’s MMA7260Q accelerometer for 3D sensing.
  • nanoNET wireless developer kit from www.nanotron.de.

The toy manufacturer www.heljan.dk has sponsored the Container Terminal.

You can find other concepts, prototypes, and products using the .NET Micro Framework here.

TOC and further sample chapter of my "Expert .NET Micro Framework" book available

A further sample chapter (Chapter 1 - Introducing the .NET Micro Framework)
and the table of contents of "Expert .NET Micro Framework" is available at
www.apress.com/book/view/9781590599730 now.

Sample chapter for "Expert .NET Micro Framework" book

Expert .NET Micro Framework will be published end of April and is available by preorder from the publisher's website, here, or on Amazon, here. Meanwhile, you can get a sample chapter from the WindowsForDevices.com website.
Chapter 4, "Introducing the .NET Micro Framework base class library" reviews the differences between the full .NET Framework and .NET Micro Framework, explains how to effectively use strings, numbers, arrays, and lists, and shows how to handle exceptions.

My book "Expert .NET Micro Framework" is done!

The book is now officially done and has been shipped off to the printer.
Please find more information on the official book page from Apress and on the Amazon page.

 

Book Description:
The Microsoft .NET Micro Framework is a small and efficient .NET runtime environment used to run managed code on embedded devices that are too small and resource constrained for Windows CE and the .NET Compact Framework.

As a .NET programmer of desktop and smart device applications, I have been enthusiastic for some time about programming embedded microcontrollers with everyday development tools and programming languages such as Microsoft Visual Studio and C#. Since I first saw the .NET Micro Framework presented at Microsoft’s Mobile and Embedded Developers Conference (MEDC), I have been an active beta tester and a regular contributor to the technology’s forums.

My passion for this technology has motivated me to write this book for you, in which I provide all the resources you will need to program the .NET Micro Framework. I start by introducing you to the .NET Micro Framework and tools, presenting the available devices and touring the whole base class library. After that, I teach you how to use and write managed drivers to access the hardware components, how to use and provide web services on devices with the new Device Profile for Web Services (DPWS), and how to write applications with rich graphical user interfaces. We also explore the extensible emulator and emulator components. You’ll find many practical and reusable samples and tips throughout this book, so you’ll be able to create your own projects. Whether you are a .NET developer or a traditional embedded developer with a background in assembly language, C, or C++, I am confident that you will be impressed by the benefits that managed code and the .NET Micro Framework bring to embedded development. In this book, I provide you with a guide that enables you to get everything you possibly can from the .NET Micro Framework, so you can write powerful, effective, embedded applications of your own.

List box with a background (image) for the .NET Micro Framework

The development boards and devices for the .NET Micro Framework have great color lcd displays. So a list box menu with a white or solid background looks quite boring. Why not add a background to a listbox?

Therefore I derived my custom list box called BackgroundImageListBox from ListBox. It has a property BackgroundIBitmap to specify a certain bitmap as background. If StretchImage is true the bitmap will be stretched to fill the whole list box, if false the bitmap will be centered. If no bitmap is specified, a color gradient background is drawn. This is done by overriding the OnRender method.

List box with color gradient background

And here the custom list box control:

using System;

using Microsoft.SPOT;

using Microsoft.SPOT.Presentation.Controls;

using Microsoft.SPOT.Presentation.Media;

 

namespace Kuehner.SPOT.Presentation.Controls

{

    public class BackgroundImageListBox : ListBox

    {

        private Bitmap backgroundBitmap;

        private bool stretchImage;

 

        public BackgroundImageListBox()

        {

            ScrollViewer v = base.LogicalChildren[0] as ScrollViewer;

            v.Background = null;

            this.Background = null;

        }

 

        public override void OnRender(DrawingContext dc)

        {

            base.OnRender(dc);

            if (this.backgroundBitmap != null)

            {

                if (this.stretchImage)

                    dc.Bitmap.StretchImage(0, 0, this.backgroundBitmap, this.Width, this.Height, 0xFF); //stretch to fit

                else

                {

                    //center

                    int x = (this.Width - this.backgroundBitmap.Width) / 2;

                    int y = (this.Height - this.backgroundBitmap.Height) / 2;

                    dc.DrawImage(this.backgroundBitmap, x, y); //draw unscaled

                }

            }

            else

            {

                //draw gradient if no bitmap specified

                dc.Bitmap.DrawRectangle(Color.Black, 0,

                                        0, 0, this.Width, this.Height, 0, 0,

                                        ColorUtility.ColorFromRGB(255, 96, 96), 0, 0,

                                        ColorUtility.ColorFromRGB(96, 96, 255), 0, this.Height,

                                        0xFF);

            }

        }

 

        public Bitmap BackgroundBitmap

        {

            get { return this.backgroundBitmap; }

            set { this.backgroundBitmap = value; }

        }

 

        public bool StretchImage

        {

            get { return this.stretchImage; }

            set { this.stretchImage = value; }

        }

    }

}

When you polulate your list box you need to set the Background of a list box item to null so that it is painted transparently.

Heres some code to populate your list box. 

        public Window CreateWindow()

        {

            // Create a window object and set its size to the

            // size of the display.

            mainWindow = new Window();

            mainWindow.Height = SystemMetrics.ScreenHeight;

            mainWindow.Width = SystemMetrics.ScreenWidth;

 

            BackgroundImageListBox listBox = new BackgroundImageListBox();

            listBox.BackgroundBitmap = Resources.GetBitmap(Resources.BitmapResources.MFsnowflake);

            //listBox.StretchImage = true;

            listBox.Width = mainWindow.Width;

            listBox.Height = mainWindow.Height;

 

            Font font = Resources.GetFont(Resources.FontResources.small);

            foreach (String s in new String[] { "One", "Two", "Three",

                                                "Four", "Five", "Six", "Seven", "Eight" })

            {

                Text text = new Text(font, "Menu " + s);

                text.Width = listBox.Width;

                text.Height = 20;

                text.TextAlignment = TextAlignment.Center;

                ListBoxItem item = new ListBoxItem();

                item.Child = text;

                item.Background = null; //important

                listBox.Items.Add(item);

            }

 

            mainWindow.Child = listBox;

 

            // Connect the button handler to all of the buttons.

            mainWindow.AddHandler(Buttons.ButtonUpEvent, new ButtonEventHandler(OnButtonUp), false);

 

            // Set the window visibility to visible.

            mainWindow.Visibility = Visibility.Visible;

 

            // Attach the button focus to the window.

            Buttons.Focus(mainWindow);

 

            return mainWindow;

        }

Extreme HAL debugging

I really like the  .NET Micro Framework and especially the emulator story. There is nearly nothing that you cannot do. It could be a little bit easier if some classes and methods where not internal. But ok, excellent and a huge step ahead anyway.

I found a way to decorate the low level HAL (hardware abstraction layer) drivers to monitor when they are accessed by an emulated MF application. So you could debug and monitor low level access eg. to GPIO-Ports and serial ports.

First we need a driver that wraps the original driver (so that we do not need to write all from scratch) and delegates the method calls to the original after logging the call and the parameters.

using System;

using System.Diagnostics;

using Microsoft.SPOT.Emulator;

using Microsoft.SPOT.Emulator.Gpio;

 

namespace Kuehner.SPOT.Emulator

{

    internal class GpioDriverDecorator : EmulatorComponent, IGpioDriver

    {

        private readonly IGpioDriver decoratedDriver;

 

        public GpioDriverDecorator(IGpioDriver decoratedDriver)

        {

            if (decoratedDriver == null)

                throw new ArgumentNullException("decoratedDriver");

            this.decoratedDriver = decoratedDriver;

        }

 

        #region IGpioDriver Members

        uint IGpioDriver.Attributes(uint pin)

        {

            return this.decoratedDriver.Attributes(pin);

        }

 

        void IGpioDriver.DisablePin(uint pin, int resistorState, uint direction, uint altFunction)

        {

            this.decoratedDriver.DisablePin(pin, resistorState, direction, altFunction);

        }

 

        bool IGpioDriver.EnableInputPin(uint pin, bool glitchFilterEnable, IntPtr isr, IntPtr isrParam, int interruptEdge, int resistorState)

        {

            return this.decoratedDriver.EnableInputPin(pin, glitchFilterEnable, isr, isrParam, interruptEdge, resistorState);

        }

 

        bool IGpioDriver.EnableInputPin(uint pin, bool glitchFilterEnable, IntPtr isr, int interruptEdge, int resistorState)

        {

            return this.decoratedDriver.EnableInputPin(pin, glitchFilterEnable, isr, interruptEdge, resistorState);

        }

 

        void IGpioDriver.EnableOutputPin(uint pin, bool initialState)

        {

            this.decoratedDriver.EnableOutputPin(pin, initialState);

        }

 

        uint IGpioDriver.GetDebounce()

        {

            return this.decoratedDriver.GetDebounce();

        }

 

        int IGpioDriver.GetPinCount()

        {

            return this.decoratedDriver.GetPinCount();

        }

 

        bool IGpioDriver.GetPinState(uint pin)

        {

            bool pinState = this.decoratedDriver.GetPinState(pin);

            Trace.WriteLine(string.Format("GetPinState pin={0} is {1}", pin, pinState), "IGpioDriver");

            return pinState;

        }

 

        bool IGpioDriver.Initialize()

        {

            return this.decoratedDriver.Initialize();

        }

 

        bool IGpioDriver.PinIsBusy(uint pin)

        {

            return this.decoratedDriver.PinIsBusy(pin);

        }

 

        bool IGpioDriver.ReservePin(uint pin, bool reserve)

        {

            return this.decoratedDriver.ReservePin(pin, reserve);

        }

 

        bool IGpioDriver.SetDebounce(long debounceTime)

        {

            return this.decoratedDriver.SetDebounce(debounceTime);

        }

 

        void IGpioDriver.SetPinState(uint pin, bool pinState)

        {

            Trace.WriteLine(string.Format("SetPinState pin={0}, pinState={1}", pin, pinState), "IGpioDriver");

            this.decoratedDriver.SetPinState(pin, pinState);

        }

 

        bool IGpioDriver.Uninitialize()

        {

            return this.decoratedDriver.Uninitialize();

        }

        #endregion

    }

}

Then we need a managed HAL to hook up the drivers.

 

using System;

using System.Reflection;

using Microsoft.SPOT.Emulator;

using Microsoft.SPOT.Emulator.Com;

using Microsoft.SPOT.Emulator.Gpio;

 

namespace Kuehner.SPOT.Emulator

{

    public class MonitoringHal : Hal

    {

        private readonly IComDriver decoratedComDriver;

        private readonly IGpioDriver decoratedGpioDriver;

 

        public MonitoringHal()

        {

            this.decoratedComDriver = this.Com;

            this.Com = new ComDriverDecorator(this.decoratedComDriver);

            this.decoratedGpioDriver = this.Gpio;

            this.Gpio = new GpioDriverDecorator(this.decoratedGpioDriver);

        }

 

        public override void SetupComponent()

        {

            base.SetupComponent();

            //set the internal emulator property for the original component since this property

            //is only set if the component is registered and it is needed to work correctly

            SetEmulator(this.decoratedComDriver);

            SetEmulator(this.decoratedGpioDriver);

        }

 

        private void SetEmulator(object component)

        {

            System.Diagnostics.Debug.Assert(component is EmulatorComponent);

            MethodInfo mi = typeof(EmulatorComponent).GetMethod("SetEmulator", BindingFlags.NonPublic | BindingFlags.Instance);

            mi.Invoke(component, new object[] { this.Emulator });

        }

    }

}

 

This was very extreme stuff and you usually might not need it. But it helps to understand how the emulator works.

 

Fun with Emulator-COM-Ports - Part II

In my last post I described how you can route data between a .NET Micro Framework Emulator and Sockets to debug a .NET Micro Framework App with Hyper-Terminal. It is possible with the extensible emulator to route data to all kind of streams!

No you will learn how to let an emulated Microsoft .NET Micro Framework application communicate with physical COM-Ports on your host PC (also virtual Ports).Therefore I wrote the emulator component ComPortToPhysicalPcPort.This allows you to debug your app with feeding and
verifying serial test data with the socket component on Hyper-Teminal first.And then by just exchanging the socket component with the physical
component (only a few XML lines) allows you to connect a real device to a PC COM Port and debug without having a MF development board but your real serial device. At last if you have your real hardware platform and real serial device connect to it your code should run like a charm.

The emulator component:
using System;

using System.IO.Ports;

using Microsoft.SPOT.Emulator.Com;

 

namespace Kuehner.SPOT.Emulator

{

    public class ComPortToPhysicalPcSerialPort : ComPortToStream

    {

        private SerialPort serialPort;

        private string physicalPortName = "COM1";

        private int baudrate = 9600;

        private int readTimeout = 1000;

        private Handshake handshake = Handshake.None;

 

        protected override void InitializeProtected()

        {

            base.InitializeProtected();

            if (this.Stream == null)

            {

                this.serialPort = new SerialPort(this.physicalPortName, this.baudrate);

                this.serialPort.ReadTimeout = this.readTimeout;

                this.serialPort.Handshake = this.handshake;

                this.serialPort.Open();

                this.Stream = this.serialPort.BaseStream;

            }

        }

 

        protected override void UninitializeProtected()

        {

            base.UninitializeProtected();

            if (this.Stream != null)

            {

                this.serialPort.Close(); //also closes the underlying stream

                this.serialPort = null;

                this.Stream = null;

            }

        }

 

        #region properties

        public string PhysicalPortName

        {

            get { return this.physicalPortName; }

            set { this.physicalPortName = value; }

        }

 

        public int Baudrate

        {

            get { return this.baudrate; }

            set { this.baudrate = value; }

        }

 

        public int ReadTimeout

        {

            get { return this.readTimeout; }

     &nbs