EMX – Hello world May 23, 2010Posted by wesaday in Programming.
Tags: EMX, GHIElectronics
Today we will explore the ever popular “Hello world” using the .NET Micro Framework and see it running in the Emulator. Since the Micro Framework is targeted for embedded systems, there is very limited support for controls. What support there is, is based on WPF but there are some differences.
When you install the Micro Framwork SDK it installs some new project templates into Visual Studio. We are going to use these templates to base our program on because creating a project from scratch in the Micro Framework is a bit of a pain. Fire up Visual Studio and create a new project using the Micro Framework Window Application template. In truth, it probably doesn’t matter if you choose the Console or Window template since we are going to delete everything and build it up step by step.
Click Ok and let the project get created. You should wind up with something like this:
The template files are all well and good but you should know exactly what the template contains and what it does. Select the Resources folder, GPIOButtonInputProvider.cs, Program,cs and Resources.resx file and delete them. That should pretty much wipe out everything, except for the references. Right click the project in the solution explorer, select Add and then click Class. Name the class Program and click Ok. This class is going to be the startup class. Add a public static Main method that returns void to the Program class and have Program derive from Application. Then add the following lines to the Main method:
Nothing to it right? The first line instantiates the Program class. The second line instantiates the MainWindow. The third line tells the program to run using win as the main window. The entire program class looks like this :
Now we need to add another class that will be our main window. Add a new class to the solution and call it MainWindow. MainWindow needs to derive from the Window class. Window is contained within the Microsoft.SPOT.Presentation namespace so add that using statement to the top of the file. Add a constructor for MainWindow and add these lines to it:
The first two lines setup the window width and height. The third line sets the background color to blue. Note that the hex values for color are backwards from RGB. The hex values are BGR.
Lines 5 – 10 are an intializer for the Text class. Text is the equivalent of a Label in Winforms or TextBlock in WPF. I guess if they named it same as in WPF then there wouldn’t be anything new to learn….
The Font for Text is set by getting the font from the resources. TextContent is the Text property. Then the horizontal and vertical alignments are set. In this case we are setting the text to be centered in the screen.
The last thing that we need to do is to add a Resource file to contain the font that we are going to be using. Right click the Project, select Add and then click New Item. Then select the Resources File to add. Once the file is created, click the Add Resource drop down and select Add Existing File. Browse to the .NET Micro Framework installation directory, default location is C:\Program Files\Microsoft >NET Micro Framework\v4.0. Open up the Fonts folder and select the small.tinyfnt file and click Ok. The default installation of the Framework only comes with two fonts. You solution should now look like this: You should be able to build your solution now.
Almost done. Now you want to see the result of all that work right? Remember this is for embedded systems. So what if you don’t have an embedded system to run it on? Or you have your system on order but have not go it yet? The Micro Framework comes with an emulator that you can use to run your program in. Click the Project menu item and select Properties. Select the .NET Micro Framework tab. On that page you will see a combo box labeled “Transport”. This is where you would select where to run your program. If you have Emulator already selected then you are good to go. Press F5 to start your program. Visual Studio will build and “deploy” the program to the emulator. And the final outcome should look like this: